2016-11-11 12:03:01 -05:00
|
|
|
AnalyticsManager = require("../Analytics/AnalyticsManager")
|
|
|
|
BlogHandler = require("../Blog/BlogHandler")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
settings = require("settings-sharelatex")
|
2017-01-25 04:34:53 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("lodash")
|
2016-11-11 12:03:01 -05:00
|
|
|
|
|
|
|
|
2017-01-24 11:03:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = AnnouncementsHandler =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_domainSpecificAnnouncements : (email)->
|
2017-01-25 04:34:53 -05:00
|
|
|
domainSpecific = _.filter settings?.domainAnnouncementsToShow, (domainAnnouncment)->
|
2017-01-24 11:03:05 -05:00
|
|
|
matches = _.filter domainAnnouncment.domains, (domain)->
|
|
|
|
return email.indexOf(domain) != -1
|
2017-01-25 04:34:53 -05:00
|
|
|
return matches.length > 0 and domainAnnouncment.id?
|
2017-01-24 11:03:05 -05:00
|
|
|
return domainSpecific or []
|
|
|
|
|
|
|
|
|
|
|
|
getUnreadAnnouncements : (user, callback = (err, announcements)->)->
|
2017-01-25 04:34:53 -05:00
|
|
|
if !user? and !user._id?
|
|
|
|
return callback("user not supplied")
|
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
async.parallel {
|
|
|
|
lastEvent: (cb)->
|
2017-01-25 04:34:53 -05:00
|
|
|
AnalyticsManager.getLastOccurance user._id, "announcement-alert-dismissed", cb
|
2016-11-11 12:03:01 -05:00
|
|
|
announcements: (cb)->
|
|
|
|
BlogHandler.getLatestAnnouncements cb
|
|
|
|
}, (err, results)->
|
|
|
|
if err?
|
2017-01-25 04:34:53 -05:00
|
|
|
logger.err err:err, user_id:user._id, "error getting unread announcements"
|
2016-11-11 12:03:01 -05:00
|
|
|
return callback(err)
|
|
|
|
|
2017-01-24 11:03:05 -05:00
|
|
|
domainSpecific = AnnouncementsHandler._domainSpecificAnnouncements(user?.email)
|
|
|
|
|
|
|
|
domainSpecific = _.map domainSpecific, (domainAnnouncment)->
|
|
|
|
try
|
|
|
|
domainAnnouncment.date = new Date(domainAnnouncment.date)
|
|
|
|
return domainAnnouncment
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
|
|
|
|
announcements = results.announcements
|
|
|
|
announcements = _.union announcements, domainSpecific
|
|
|
|
announcements = _.sortBy(announcements, "date").reverse()
|
2016-11-11 12:03:01 -05:00
|
|
|
|
|
|
|
lastSeenBlogId = results?.lastEvent?.segmentation?.blogPostId
|
|
|
|
|
|
|
|
announcementIndex = _.findIndex announcements, (announcement)->
|
|
|
|
announcement.id == lastSeenBlogId
|
|
|
|
|
2016-12-07 06:39:22 -05:00
|
|
|
announcements = _.map announcements, (announcement, index)->
|
|
|
|
if announcementIndex == -1
|
|
|
|
read = false
|
|
|
|
else if index >= announcementIndex
|
|
|
|
read = true
|
|
|
|
else
|
|
|
|
read = false
|
|
|
|
announcement.read = read
|
|
|
|
return announcement
|
2016-11-11 12:03:01 -05:00
|
|
|
|
2017-01-24 11:03:05 -05:00
|
|
|
logger.log announcementsLength:announcements?.length, user_id:user?._id, "returning announcements"
|
2016-11-11 12:03:01 -05:00
|
|
|
|
|
|
|
callback null, announcements
|