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

65 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2016-12-16 11:42:41 -05:00
ChatApiHandler = require("./ChatApiHandler")
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')
2017-01-06 07:41:58 -05:00
UserInfoManager = require('../User/UserInfoManager')
UserInfoController = require('../User/UserInfoController')
async = require "async"
2014-07-04 08:05:55 -04:00
module.exports = ChatController =
2016-09-05 10:58:31 -04:00
sendMessage: (req, res, next)->
2016-12-16 11:42:41 -05:00
project_id = req.params.project_id
content = 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)
2016-12-16 11:42:41 -05:00
ChatApiHandler.sendGlobalMessage project_id, user_id, content, (err, message) ->
return next(err) if err?
2017-01-06 07:41:58 -05:00
UserInfoManager.getPersonalInfo message.user_id, (err, user) ->
return next(err) if err?
message.user = UserInfoController.formatPersonalInfo(user)
EditorRealTimeController.emitToRoom project_id, "new-chat-message", message, (err)->
res.send(204)
2014-07-04 08:05:55 -04:00
2016-12-16 11:42:41 -05:00
getMessages: (req, res, next)->
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"
2016-12-16 11:42:41 -05:00
ChatApiHandler.getGlobalMessages project_id, query.limit, query.before, (err, messages) ->
return next(err) if err?
ChatController._injectUserInfoIntoThreads {global: { messages: messages }}, (err) ->
2017-01-06 07:41:58 -05:00
return next(err) if err?
logger.log length: messages?.length, "sending messages to client"
res.json messages
_injectUserInfoIntoThreads: (threads, callback = (error, threads) ->) ->
userCache = {}
getUserDetails = (user_id, callback = (error, user) ->) ->
return callback(null, userCache[user_id]) if userCache[user_id]?
UserInfoManager.getPersonalInfo user_id, (err, user) ->
return callback(error) if error?
user = UserInfoController.formatPersonalInfo user
userCache[user_id] = user
callback null, user
jobs = []
for thread_id, thread of threads
do (thread) ->
if thread.resolved
jobs.push (cb) ->
getUserDetails thread.resolved_by_user_id, (error, user) ->
cb(error) if error?
thread.resolved_by_user = user
cb()
for message in thread.messages
do (message) ->
jobs.push (cb) ->
getUserDetails message.user_id, (error, user) ->
cb(error) if error?
message.user = user
cb()
async.series jobs, (error) ->
return callback(error) if error?
return callback null, threads