Commit 6590640f authored by A. Koch's avatar A. Koch

changed service function signatures

parent 61790dc7
...@@ -9,11 +9,11 @@ const setup = async function () { ...@@ -9,11 +9,11 @@ const setup = async function () {
*/ */
const const
Profiles = require('./lib/profiles'), Profiles = require('./lib/profiles'),
profiles = new Profiles(api._app) profiles = new Profiles(api)
// profiles.on('message', message => api._logger.debug(message)) // profiles.on('message', message => api._logger.debug(message))
const addAuthor = require('./middleware/author') const addAuthor = require('./middleware/author')
addAuthor(api._app, profiles) addAuthor(api, profiles)
/** /**
* Configure resources * Configure resources
...@@ -22,10 +22,10 @@ const setup = async function () { ...@@ -22,10 +22,10 @@ const setup = async function () {
models = require('mbjs-data-models'), models = require('mbjs-data-models'),
Service = require('./lib/service') Service = require('./lib/service')
const annotations = new Service('annotations', api._app, models.Annotation, api._logger, api._acl) const annotations = new Service('annotations', api, models.Annotation)
// annotations.on('message', message => api._sockets.write(message)) // annotations.on('message', message => api._sockets.write(message))
const maps = new Service('maps', api._app, models.Map, api._logger, api._acl) const maps = new Service('maps', api, models.Map)
// maps.on('message', message => api._sockets.write(message)) // maps.on('message', message => api._sockets.write(message))
// const documents = new Service('documents', api._app, models.Document, api._logger, api._acl) // const documents = new Service('documents', api._app, models.Document, api._logger, api._acl)
...@@ -37,7 +37,7 @@ const setup = async function () { ...@@ -37,7 +37,7 @@ const setup = async function () {
const const
Sessions = require('./lib/sessions'), Sessions = require('./lib/sessions'),
sessions = new Sessions(api._app, maps, annotations) sessions = new Sessions(api, maps, annotations)
// sessions.on('message', message => api._logger.write(message)) // sessions.on('message', message => api._logger.write(message))
/** /**
...@@ -45,7 +45,7 @@ const setup = async function () { ...@@ -45,7 +45,7 @@ const setup = async function () {
*/ */
const archives = require('./lib/archives') const archives = require('./lib/archives')
archives.setupArchives(api._app, maps, annotations) archives.setupArchives(api, maps, annotations)
await api.start() await api.start()
} }
......
...@@ -9,9 +9,9 @@ const ...@@ -9,9 +9,9 @@ const
send = require('@polka/send-type'), send = require('@polka/send-type'),
{ Assert, ObjectUtil } = require('mbjs-utils') { Assert, ObjectUtil } = require('mbjs-utils')
module.exports.setupArchives = (app, mapService, annotationService) => { module.exports.setupArchives = (api, mapService, annotationService) => {
const upload = multer({ dest: os.tmpdir() }) const upload = multer({ dest: os.tmpdir() })
app.post('/archives/maps', async (req, res) => { api.app.post('/archives/maps', async (req, res) => {
let data = {} let data = {}
let request = { let request = {
params: { params: {
...@@ -36,7 +36,7 @@ module.exports.setupArchives = (app, mapService, annotationService) => { ...@@ -36,7 +36,7 @@ module.exports.setupArchives = (app, mapService, annotationService) => {
}) })
}) })
}) })
app.get('/archives/maps/:id', async (req, res) => { api.app.get('/archives/maps/:id', async (req, res) => {
const filename = `map_archive_${req.params.id}.zip` const filename = `map_archive_${req.params.id}.zip`
const filePath = path.join(os.tmpdir(), filename) const filePath = path.join(os.tmpdir(), filename)
const file = fs.createReadStream(filePath) const file = fs.createReadStream(filePath)
...@@ -44,7 +44,7 @@ module.exports.setupArchives = (app, mapService, annotationService) => { ...@@ -44,7 +44,7 @@ module.exports.setupArchives = (app, mapService, annotationService) => {
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`) res.setHeader('Content-Disposition', `attachment; filename="${filename}"`)
file.pipe(res) file.pipe(res)
}) })
app.post('/archives/maps/upload', async function (req, res) { api.app.post('/archives/maps/upload', async function (req, res) {
upload.single('file')(req, res, async () => { upload.single('file')(req, res, async () => {
const results = await exports.readArchive(req.file.path) const results = await exports.readArchive(req.file.path)
const copy = req.body.title || false const copy = req.body.title || false
......
...@@ -6,7 +6,7 @@ const ...@@ -6,7 +6,7 @@ const
{ MongoDB } = require('mbjs-persistence') { MongoDB } = require('mbjs-persistence')
class Profiles extends TinyEmitter { class Profiles extends TinyEmitter {
constructor (app) { constructor (api) {
super() super()
// TODO: make db adapter configurable (nedb, etc.) // TODO: make db adapter configurable (nedb, etc.)
...@@ -14,9 +14,9 @@ class Profiles extends TinyEmitter { ...@@ -14,9 +14,9 @@ class Profiles extends TinyEmitter {
const _this = this const _this = this
app.get('/profiles/:id', (req, res) => _this.getHandler(req, res)) api.app.get('/profiles/:id', (req, res) => _this.getHandler(req, res))
app.post('/profiles', async (req, res) => { api.app.post('/profiles', async (req, res) => {
req.body.uuid = ObjectUtil.uuid4() req.body.uuid = ObjectUtil.uuid4()
const result = await this.client.create(req.body) const result = await this.client.create(req.body)
if (result) { if (result) {
...@@ -25,7 +25,7 @@ class Profiles extends TinyEmitter { ...@@ -25,7 +25,7 @@ class Profiles extends TinyEmitter {
send(res, 404) send(res, 404)
}) })
app.put('/profiles/:id', async (req, res) => { api.app.put('/profiles/:id', async (req, res) => {
req.body._id = undefined req.body._id = undefined
req.body.uuid = undefined req.body.uuid = undefined
req.body.user = undefined req.body.user = undefined
...@@ -39,7 +39,7 @@ class Profiles extends TinyEmitter { ...@@ -39,7 +39,7 @@ class Profiles extends TinyEmitter {
send(res, 404) send(res, 404)
}) })
app.patch('/profiles/:id', async (req, res) => { api.app.patch('/profiles/:id', async (req, res) => {
let results = await this.client.find({ user: req.params.id }, req.params) let results = await this.client.find({ user: req.params.id }, req.params)
req.body._id = undefined req.body._id = undefined
req.body.uuid = undefined req.body.uuid = undefined
...@@ -56,7 +56,7 @@ class Profiles extends TinyEmitter { ...@@ -56,7 +56,7 @@ class Profiles extends TinyEmitter {
send(res, 404) send(res, 404)
}) })
app.delete('/profiles/:id', async (req, res) => { api.app.delete('/profiles/:id', async (req, res) => {
let results = await this.client.find({ user: req.params.id }, req.params) let results = await this.client.find({ user: req.params.id }, req.params)
if (results.length) { if (results.length) {
results = await this.client.remove(results[0].uuid, req.params) results = await this.client.remove(results[0].uuid, req.params)
......
...@@ -6,24 +6,24 @@ const ...@@ -6,24 +6,24 @@ const
{ MongoDB } = require('mbjs-persistence') { MongoDB } = require('mbjs-persistence')
class Service extends TinyEmitter { class Service extends TinyEmitter {
constructor (name, app, model, logger, acl) { constructor (name, api, model) {
super() super()
const _this = this const _this = this
this._name = name this._name = name
this._acl = acl this._acl = api.acl
this._logger = logger this._logger = api.logger
this._Model = model this._Model = model
// TODO: make db adapter configurable (nedb, etc.) // TODO: make db adapter configurable (nedb, etc.)
this._client = new MongoDB(ObjectUtil.merge({ name, logger }, config.get('resources.mongodb')), 'uuid') this._client = new MongoDB(ObjectUtil.merge({ name, logger: api.logger }, config.get('resources.mongodb')), 'uuid')
app.get(`/${this._name}`, (req, res) => _this.findHandler(req, res)) api.app.get(`/${this._name}`, (req, res) => _this.findHandler(req, res))
app.get(`/${this._name}/:id`, (req, res) => _this.getHandler(req, res)) api.app.get(`/${this._name}/:id`, (req, res) => _this.getHandler(req, res))
app.post(`/${this._name}`, (req, res) => _this.postHandler(req, res)) api.app.post(`/${this._name}`, (req, res) => _this.postHandler(req, res))
app.put(`/${this._name}/:id`, (req, res) => _this.putHandler(req, res)) api.app.put(`/${this._name}/:id`, (req, res) => _this.putHandler(req, res))
app.patch(`/${this._name}/:id`, (req, res) => _this.patchHandler(req, res)) api.app.patch(`/${this._name}/:id`, (req, res) => _this.patchHandler(req, res))
app.delete(`/${this._name}/:id`, (req, res) => _this.deleteHandler(req, res)) api.app.delete(`/${this._name}/:id`, (req, res) => _this.deleteHandler(req, res))
} }
async findHandler (req, res) { async findHandler (req, res) {
......
...@@ -26,7 +26,7 @@ const resurrectAnnotation = function (annotation) { ...@@ -26,7 +26,7 @@ const resurrectAnnotation = function (annotation) {
return annotation return annotation
} }
const fetchMetaData = async (videos, req) => { const fetchMetaData = async (videos, req, api) => {
for (let v of videos) { for (let v of videos) {
try { try {
const meta = await axios.get(`${config.api.transcoderHost}/metadata/${v.annotation.uuid}`, { const meta = await axios.get(`${config.api.transcoderHost}/metadata/${v.annotation.uuid}`, {
...@@ -36,12 +36,12 @@ const fetchMetaData = async (videos, req) => { ...@@ -36,12 +36,12 @@ const fetchMetaData = async (videos, req) => {
}) })
Object.assign(v.meta, meta.data) Object.assign(v.meta, meta.data)
} }
catch (e) { console.error('fetchMetaData', e.message, e.stack) } catch (e) { api.captureException(e) }
} }
return videos return videos
} }
const groupBySessions = async function (annotations, req, secondsDist = constants.SESSION_DISTANCE_SECONDS) { const groupBySessions = async function (annotations, req, api, secondsDist = constants.SESSION_DISTANCE_SECONDS) {
let millisDist = secondsDist * 1000 let millisDist = secondsDist * 1000
annotations = annotations.map(annotation => resurrectAnnotation(annotation)).sort(Sorting.sortOnTarget) annotations = annotations.map(annotation => resurrectAnnotation(annotation)).sort(Sorting.sortOnTarget)
const videos = annotations.filter(anno => { return anno.body.type === 'Video' }) const videos = annotations.filter(anno => { return anno.body.type === 'Video' })
...@@ -51,7 +51,7 @@ const groupBySessions = async function (annotations, req, secondsDist = constant ...@@ -51,7 +51,7 @@ const groupBySessions = async function (annotations, req, secondsDist = constant
annotation: annotation annotation: annotation
} }
}) })
await fetchMetaData(videos, req) await fetchMetaData(videos, req, api)
annotations = annotations.filter(anno => { return anno.body.type === 'TextualBody' }) annotations = annotations.filter(anno => { return anno.body.type === 'TextualBody' })
const sessions = [] const sessions = []
const defaultSession = { start: undefined, end: undefined, duration: undefined, annotations: [] } const defaultSession = { start: undefined, end: undefined, duration: undefined, annotations: [] }
...@@ -92,7 +92,7 @@ const groupBySessions = async function (annotations, req, secondsDist = constant ...@@ -92,7 +92,7 @@ const groupBySessions = async function (annotations, req, secondsDist = constant
} }
class Sessions extends TinyEmitter { class Sessions extends TinyEmitter {
constructor (app, mapsService, annotationsService) { constructor (api, mapsService, annotationsService) {
super() super()
this._maps = mapsService this._maps = mapsService
...@@ -100,7 +100,7 @@ class Sessions extends TinyEmitter { ...@@ -100,7 +100,7 @@ class Sessions extends TinyEmitter {
const _this = this const _this = this
app.get('/sessions/:id', async (req, res) => { api.app.get('/sessions/:id', async (req, res) => {
let results = await _this._maps.getHandler(req) let results = await _this._maps.getHandler(req)
const map = results.data const map = results.data
if (!map) return _this._errorResponse(res, 404) if (!map) return _this._errorResponse(res, 404)
...@@ -114,7 +114,7 @@ class Sessions extends TinyEmitter { ...@@ -114,7 +114,7 @@ class Sessions extends TinyEmitter {
headers: req.headers headers: req.headers
}) })
let annotations = results.data.items let annotations = results.data.items
const sessions = await groupBySessions(annotations, req) const sessions = await groupBySessions(annotations, req, api)
_this._response(req, res, sessions) _this._response(req, res, sessions)
}) })
} }
......
const setup = async function (app, profileService) { const setup = async function (api, profileService) {
app.use(async (req, res, next) => { api.app.use(async (req, res, next) => {
const r = { const r = {
params: { params: {
id: req.user.uuid id: req.user.uuid
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment