mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #399 from sharelatex/ho-promote-case-study
promote domain alerts
This commit is contained in:
commit
02e6eb4dd1
6 changed files with 117 additions and 19 deletions
|
@ -9,9 +9,9 @@ module.exports =
|
|||
if !settings?.apis?.analytics?.url? or !settings.apis.blog.url?
|
||||
return res.json []
|
||||
|
||||
user_id = AuthenticationController.getLoggedInUserId(req)
|
||||
logger.log {user_id}, "getting unread announcements"
|
||||
AnnouncementsHandler.getUnreadAnnouncements user_id, (err, announcements)->
|
||||
user = AuthenticationController.getSessionUser(req)
|
||||
logger.log {user_id:user?._id}, "getting unread announcements"
|
||||
AnnouncementsHandler.getUnreadAnnouncements user, (err, announcements)->
|
||||
if err?
|
||||
logger.err {err, user_id}, "unable to get unread announcements"
|
||||
next(err)
|
||||
|
|
|
@ -1,24 +1,51 @@
|
|||
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")
|
||||
|
||||
module.exports =
|
||||
|
||||
getUnreadAnnouncements : (user_id, callback = (err, announcements)->)->
|
||||
|
||||
|
||||
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
|
||||
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)
|
||||
|
||||
announcements = _.sortBy(results.announcements, "date").reverse()
|
||||
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
|
||||
|
||||
|
@ -35,6 +62,6 @@ module.exports =
|
|||
announcement.read = read
|
||||
return announcement
|
||||
|
||||
logger.log announcementsLength:announcements?.length, user_id:user_id, "returning announcements"
|
||||
logger.log announcementsLength:announcements?.length, user_id:user?._id, "returning announcements"
|
||||
|
||||
callback null, announcements
|
||||
|
|
|
@ -189,6 +189,7 @@ module.exports = (app, webRouter, apiRouter)->
|
|||
return AuthenticationController.isUserLoggedIn(req)
|
||||
res.locals.getSessionUser = ->
|
||||
return AuthenticationController.getSessionUser(req)
|
||||
|
||||
next()
|
||||
|
||||
webRouter.use (req, res, next) ->
|
||||
|
|
|
@ -6,4 +6,4 @@ define [
|
|||
|
||||
$scope.hasProjects = window.data.projects.length > 0
|
||||
$scope.userHasNoSubscription = window.userHasNoSubscription
|
||||
$scope.randomView = _.shuffle(["default", "dropbox", "github"])[0]
|
||||
|
||||
|
|
|
@ -10,18 +10,21 @@ expect = require("chai").expect
|
|||
describe 'AnnouncementsHandler', ->
|
||||
|
||||
beforeEach ->
|
||||
@user_id = "some_id"
|
||||
@user =
|
||||
_id:"some_id"
|
||||
email: "someone@gmail.com"
|
||||
@AnalyticsManager =
|
||||
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 = [
|
||||
|
@ -44,7 +47,7 @@ describe 'AnnouncementsHandler', ->
|
|||
|
||||
it "should mark all announcements as read is false", (done)->
|
||||
@AnalyticsManager.getLastOccurance.callsArgWith(2, null, [])
|
||||
@handler.getUnreadAnnouncements @user_id, (err, announcements)=>
|
||||
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
||||
announcements[0].read.should.equal false
|
||||
announcements[1].read.should.equal false
|
||||
announcements[2].read.should.equal false
|
||||
|
@ -53,7 +56,7 @@ describe 'AnnouncementsHandler', ->
|
|||
|
||||
it "should should be sorted again to ensure correct order", (done)->
|
||||
@AnalyticsManager.getLastOccurance.callsArgWith(2, null, [])
|
||||
@handler.getUnreadAnnouncements @user_id, (err, announcements)=>
|
||||
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
||||
announcements[3].should.equal @stubbedAnnouncements[2]
|
||||
announcements[2].should.equal @stubbedAnnouncements[3]
|
||||
announcements[1].should.equal @stubbedAnnouncements[1]
|
||||
|
@ -62,7 +65,7 @@ describe 'AnnouncementsHandler', ->
|
|||
|
||||
it "should return older ones marked as read as well", (done)->
|
||||
@AnalyticsManager.getLastOccurance.callsArgWith(2, null, {segmentation:{blogPostId:"/2014/04/12/title-date-irrelivant"}})
|
||||
@handler.getUnreadAnnouncements @user_id, (err, announcements)=>
|
||||
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
||||
announcements[0].id.should.equal @stubbedAnnouncements[0].id
|
||||
announcements[0].read.should.equal false
|
||||
|
||||
|
@ -79,7 +82,7 @@ describe 'AnnouncementsHandler', ->
|
|||
|
||||
it "should return all of them marked as read", (done)->
|
||||
@AnalyticsManager.getLastOccurance.callsArgWith(2, null, {segmentation:{blogPostId:"/2016/11/01/introducting-latex-code-checker"}})
|
||||
@handler.getUnreadAnnouncements @user_id, (err, announcements)=>
|
||||
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
||||
announcements[0].read.should.equal true
|
||||
announcements[1].read.should.equal true
|
||||
announcements[2].read.should.equal true
|
||||
|
@ -87,3 +90,70 @@ describe 'AnnouncementsHandler', ->
|
|||
done()
|
||||
|
||||
|
||||
describe "with custom domain announcements", ->
|
||||
beforeEach ->
|
||||
@stubbedDomainSpecificAnn = [
|
||||
{
|
||||
domains: ["gmail.com", 'yahoo.edu']
|
||||
title: "some message"
|
||||
excerpt: "read this"
|
||||
url:"http://www.sharelatex.com/i/somewhere"
|
||||
id:"iaaa"
|
||||
date: new Date(1308369600000).toString()
|
||||
}
|
||||
]
|
||||
|
||||
@handler._domainSpecificAnnouncements = sinon.stub().returns(@stubbedDomainSpecificAnn)
|
||||
|
||||
it "should insert the domain specific in the correct place", (done)->
|
||||
@AnalyticsManager.getLastOccurance.callsArgWith(2, null, [])
|
||||
@handler.getUnreadAnnouncements @user, (err, announcements)=>
|
||||
announcements[4].should.equal @stubbedAnnouncements[2]
|
||||
announcements[3].should.equal @stubbedAnnouncements[3]
|
||||
announcements[2].should.equal @stubbedAnnouncements[1]
|
||||
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()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue