2016-12-16 16:42:41 +00:00
|
|
|
ChatApiHandler = require("./ChatApiHandler")
|
2014-07-04 12:20:30 +00:00
|
|
|
EditorRealTimeController = require("../Editor/EditorRealTimeController")
|
2014-07-04 12:29:59 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2016-09-05 14:58:31 +00:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
2017-01-06 12:41:58 +00:00
|
|
|
UserInfoManager = require('../User/UserInfoManager')
|
|
|
|
UserInfoController = require('../User/UserInfoController')
|
2017-03-02 16:45:24 +00:00
|
|
|
async = require "async"
|
2014-07-04 12:05:55 +00:00
|
|
|
|
2017-03-02 16:45:24 +00:00
|
|
|
module.exports = ChatController =
|
2016-09-05 14:58:31 +00:00
|
|
|
sendMessage: (req, res, next)->
|
2016-12-16 16:42:41 +00:00
|
|
|
project_id = req.params.project_id
|
|
|
|
content = req.body.content
|
2016-09-05 14:58:31 +00:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
if !user_id?
|
|
|
|
err = new Error('no logged-in user')
|
|
|
|
return next(err)
|
2016-12-16 16:42:41 +00:00
|
|
|
ChatApiHandler.sendGlobalMessage project_id, user_id, content, (err, message) ->
|
|
|
|
return next(err) if err?
|
2017-01-06 12:41:58 +00: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 12:05:55 +00:00
|
|
|
|
2016-12-16 16:42:41 +00:00
|
|
|
getMessages: (req, res, next)->
|
|
|
|
project_id = req.params.project_id
|
2014-07-15 17:25:12 +00:00
|
|
|
query = req.query
|
2014-07-08 09:08:38 +00:00
|
|
|
logger.log project_id:project_id, query:query, "getting messages"
|
2016-12-16 16:42:41 +00:00
|
|
|
ChatApiHandler.getGlobalMessages project_id, query.limit, query.before, (err, messages) ->
|
|
|
|
return next(err) if err?
|
2017-03-02 16:45:24 +00:00
|
|
|
ChatController._injectUserInfoIntoThreads {global: { messages: messages }}, (err) ->
|
2017-01-06 12:41:58 +00:00
|
|
|
return next(err) if err?
|
|
|
|
logger.log length: messages?.length, "sending messages to client"
|
|
|
|
res.json messages
|
2017-03-02 16:45:24 +00:00
|
|
|
|
|
|
|
_injectUserInfoIntoThreads: (threads, callback = (error, threads) ->) ->
|
2017-05-04 16:04:20 +00:00
|
|
|
# There will be a lot of repitition of user_ids, so first build a list
|
|
|
|
# of unique ones to perform db look ups on, then use these to populate the
|
|
|
|
# user fields
|
|
|
|
user_ids = {}
|
2017-03-02 16:45:24 +00:00
|
|
|
for thread_id, thread of threads
|
2017-05-04 16:04:20 +00:00
|
|
|
if thread.resolved
|
|
|
|
user_ids[thread.resolved_by_user_id] = true
|
|
|
|
for message in thread.messages
|
|
|
|
user_ids[message.user_id] = true
|
2017-03-02 16:45:24 +00:00
|
|
|
|
2017-05-04 16:04:20 +00:00
|
|
|
jobs = []
|
|
|
|
users = {}
|
|
|
|
for user_id, _ of user_ids
|
|
|
|
do (user_id) ->
|
|
|
|
jobs.push (cb) ->
|
|
|
|
UserInfoManager.getPersonalInfo user_id, (err, user) ->
|
|
|
|
return cb(error) if error?
|
|
|
|
user = UserInfoController.formatPersonalInfo user
|
|
|
|
users[user_id] = user
|
|
|
|
cb()
|
|
|
|
|
2017-03-02 16:45:24 +00:00
|
|
|
async.series jobs, (error) ->
|
|
|
|
return callback(error) if error?
|
2017-05-04 16:04:20 +00:00
|
|
|
for thread_id, thread of threads
|
|
|
|
if thread.resolved
|
|
|
|
thread.resolved_by_user = users[thread.resolved_by_user_id]
|
|
|
|
for message in thread.messages
|
|
|
|
message.user = users[message.user_id]
|
2017-03-02 16:45:24 +00:00
|
|
|
return callback null, threads
|