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

changed service function signatures

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