overleaf/services/web/app/coffee/Features/Chat/ChatController.coffee

34 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2014-07-04 08:05:55 -04:00
ChatHandler = require("./ChatHandler")
EditorRealTimeController = require("../Editor/EditorRealTimeController")
2014-07-04 08:29:59 -04:00
logger = require("logger-sharelatex")
2016-09-05 10:58:31 -04:00
AuthenticationController = require('../Authentication/AuthenticationController')
2014-07-04 08:05:55 -04:00
module.exports =
2016-09-05 10:58:31 -04:00
sendMessage: (req, res, next)->
2014-07-14 17:11:41 -04:00
project_id = req.params.Project_id
messageContent = req.body.content
2016-09-05 10:58:31 -04:00
user_id = AuthenticationController.getLoggedInUserId(req)
if !user_id?
err = new Error('no logged-in user')
return next(err)
ChatHandler.sendMessage project_id, user_id, messageContent, (err, builtMessge)->
2014-07-04 08:29:59 -04:00
if err?
logger.err err:err, project_id:project_id, user_id:user_id, messageContent:messageContent, "problem sending message to chat api"
2015-07-08 11:56:38 -04:00
return res.sendStatus(500)
EditorRealTimeController.emitToRoom project_id, "new-chat-message", builtMessge, (err)->
res.send()
2014-07-04 08:05:55 -04:00
getMessages: (req, res)->
2014-07-14 17:11:41 -04:00
project_id = req.params.Project_id
2014-07-15 13:25:12 -04:00
query = req.query
logger.log project_id:project_id, query:query, "getting messages"
ChatHandler.getMessages project_id, query, (err, messages)->
2014-07-04 08:29:59 -04:00
if err?
logger.err err:err, query:query, "problem getting messages from chat api"
2015-07-08 11:56:38 -04:00
return res.sendStatus 500
logger.log length:messages?.length, "sending messages to client"
res.set 'Content-Type', 'application/json'
res.send messages