CoreID/app/controllers/api/v1/Message.controller.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-05-04 01:16:54 +00:00
const { Controller } = require('libflitter')
class MessageController extends Controller {
static get services() {
return [...super.services, 'models']
}
async get_banners(req, res, next) {
const Message = this.models.get('Message')
const messages = await Message.for_user(req.user)
return res.api(messages.map(x => {
return {
message: x.message,
expires: x.expires,
dismissed: x.dismissed,
type: x.display_type,
id: x.id,
}
}))
}
async read_banner(req, res, next) {
const banner_id = req.params.banner_id
if ( !banner_id )
return res.status(400)
.message(`${req.T('api.missing_field')} banner_id`)
2020-05-04 01:16:54 +00:00
.api()
const Message = this.models.get('Message')
const message = await Message.findById(banner_id)
if ( !message )
return res.status(404)
.message(req.T('api.banner_not_found'))
2020-05-04 01:16:54 +00:00
.api()
if ( message.user_id !== req.user.id )
return res.status(401).api()
await message.dismiss()
return res.api()
}
}
module.exports = exports = MessageController