-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcampaign.coffee
63 lines (55 loc) · 1.88 KB
/
campaign.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class @Campaign extends MeteorModel
###
Only the root user can manage Campaign documents
###
@mongoCollection: new Meteor.Collection('campaigns')
@allowInsert: (userId, doc) ->
# Ensure author and timestamp of creation in every document
doc.createdAt = Date.now()
doc.createdBy = userId
# This is a list of recipients that already received this Campaign
doc.sentTo = []
return User.find(userId)?.isRoot()
@allowUpdate: (userId, doc, fields, modifier) ->
return User.find(userId)?.isRoot()
@allowRemove: (userId, doc) ->
return User.find(userId)?.isRoot()
toJSON: (raw = false) ->
data = super(arguments...)
unless raw
data.sentToUserList = @getSentToUserList()
return data
validate: ->
return "Subject can't be empty" if _.isEmpty(@get('subject'))
getSentToUserList: ->
list = []
return list unless @get('sentTo')
for id in @get('sentTo')
user = User.find(id)
if user
list.push(user.getEmailField())
else
list.push("Removed: id")
return list.join(', ')
getMessage: (user) ->
###
Decorate the campaign message with user fields and the unsubscribe link
###
unsubscribeUrl = Meteor.absoluteUrl("unsubscribe/#{user.get('_id')}")
message = @get('message')
message = message.replace('{{username}}', user.get('username'))
# Default to the username when no full name is provided
if user.get('profile').name
# Get only the first name
name = user.getCleanName().split(' ')[0]
else
name = user.get('username')
message = message.replace('{{name}}', name)
message += "\n\n---\n\n"
message += "Follow this link to never hear from me again: #{unsubscribeUrl}"
return message
Campaign.publish
campaigns: ->
# Only wire to the root user
return [] unless User.find(@userId)?.isRoot()
return Campaign.mongoCollection.find()