overleaf/services/web/app/coffee/Features/Announcements/AnnouncementsHandler.coffee
Henry Oswald 1280a2313d add check for id to exist in domain announcments
null check user at start of getUnreadAnnouncements
fix spelling mistake
2017-01-25 09:35:11 +00:00

67 lines
2 KiB
CoffeeScript

AnalyticsManager = require("../Analytics/AnalyticsManager")
BlogHandler = require("../Blog/BlogHandler")
logger = require("logger-sharelatex")
settings = require("settings-sharelatex")
async = require("async")
_ = require("lodash")
module.exports = AnnouncementsHandler =
_domainSpecificAnnouncements : (email)->
domainSpecific = _.filter settings?.domainAnnouncementsToShow, (domainAnnouncment)->
matches = _.filter domainAnnouncment.domains, (domain)->
return email.indexOf(domain) != -1
return matches.length > 0 and domainAnnouncment.id?
return domainSpecific or []
getUnreadAnnouncements : (user, callback = (err, announcements)->)->
if !user? and !user._id?
return callback("user not supplied")
async.parallel {
lastEvent: (cb)->
AnalyticsManager.getLastOccurance user._id, "announcement-alert-dismissed", cb
announcements: (cb)->
BlogHandler.getLatestAnnouncements cb
}, (err, results)->
if err?
logger.err err:err, user_id:user._id, "error getting unread announcements"
return callback(err)
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()
lastSeenBlogId = results?.lastEvent?.segmentation?.blogPostId
announcementIndex = _.findIndex announcements, (announcement)->
announcement.id == lastSeenBlogId
announcements = _.map announcements, (announcement, index)->
if announcementIndex == -1
read = false
else if index >= announcementIndex
read = true
else
read = false
announcement.read = read
return announcement
logger.log announcementsLength:announcements?.length, user_id:user?._id, "returning announcements"
callback null, announcements