diff --git a/services/web/app/coffee/Features/Announcements/AnnouncementsHandler.coffee b/services/web/app/coffee/Features/Announcements/AnnouncementsHandler.coffee index 1ea675fbf3..161815dc6d 100644 --- a/services/web/app/coffee/Features/Announcements/AnnouncementsHandler.coffee +++ b/services/web/app/coffee/Features/Announcements/AnnouncementsHandler.coffee @@ -1,9 +1,9 @@ AnalyticsManager = require("../Analytics/AnalyticsManager") BlogHandler = require("../Blog/BlogHandler") -async = require("async") -_ = require("lodash") logger = require("logger-sharelatex") settings = require("settings-sharelatex") +async = require("async") +_ = require("lodash") @@ -13,22 +13,25 @@ module.exports = AnnouncementsHandler = _domainSpecificAnnouncements : (email)-> - domainSpecific = _.filter settings?.domainAnnouncmentsToShow, (domainAnnouncment)-> + domainSpecific = _.filter settings?.domainAnnouncementsToShow, (domainAnnouncment)-> matches = _.filter domainAnnouncment.domains, (domain)-> return email.indexOf(domain) != -1 - return matches.length > 0 + 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 + 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" + logger.err err:err, user_id:user._id, "error getting unread announcements" return callback(err) domainSpecific = AnnouncementsHandler._domainSpecificAnnouncements(user?.email) diff --git a/services/web/test/UnitTests/coffee/Announcement/AnnouncementsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Announcement/AnnouncementsHandlerTests.coffee index e1d9fcc7f6..bd5070644e 100644 --- a/services/web/test/UnitTests/coffee/Announcement/AnnouncementsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Announcement/AnnouncementsHandlerTests.coffee @@ -17,13 +17,14 @@ describe 'AnnouncementsHandler', -> getLastOccurance: sinon.stub() @BlogHandler = getLatestAnnouncements:sinon.stub() + @settings = {} @handler = SandboxedModule.require modulePath, requires: "../Analytics/AnalyticsManager":@AnalyticsManager "../Blog/BlogHandler":@BlogHandler + "settings-sharelatex":@settings "logger-sharelatex": log:-> - describe "getUnreadAnnouncements", -> beforeEach -> @stubbedAnnouncements = [ @@ -113,3 +114,46 @@ describe 'AnnouncementsHandler', -> announcements[1].should.equal @stubbedDomainSpecificAnn[0] announcements[0].should.equal @stubbedAnnouncements[0] done() + + describe "_domainSpecificAnnouncements", -> + beforeEach -> + @settings.domainAnnouncementsToShow = [ + { + domains: ["gmail.com", 'yahoo.edu'] + title: "some message" + excerpt: "read this" + url:"http://www.sharelatex.com/i/somewhere" + id:"id1" + date: new Date(1308369600000).toString() + }, { + domains: ["gmail.com", 'yahoo.edu'] + title: "some message" + excerpt: "read this" + url:"http://www.sharelatex.com/i/somewhere" + date: new Date(1308369600000).toString() + }, { + domains: ["gmail.com", 'yahoo.edu'] + title: "some message" + excerpt: "read this" + url:"http://www.sharelatex.com/i/somewhere" + id:"id3" + date: new Date(1308369600000).toString() + } + ] + + it "should filter announcments which don't have an id", (done) -> + result = @handler._domainSpecificAnnouncements "someone@gmail.com" + result.length.should.equal 2 + result[0].id.should.equal "id1" + result[1].id.should.equal "id3" + done() + + + it "should match on domain", (done) -> + @settings.domainAnnouncementsToShow[2].domains = ["yahoo.com"] + result = @handler._domainSpecificAnnouncements "someone@gmail.com" + result.length.should.equal 1 + result[0].id.should.equal "id1" + done() + +