This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontroller.coffee
144 lines (111 loc) · 4.38 KB
/
controller.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
clearance = require './index'
async = require 'async'
urlHelpers = require 'url'
jugglingInAmericano = 'americano-cozy/node_modules/jugglingdb-cozy-adapter'
module.exports = (options) ->
out = {}
mailSubject = options.mailSubject
mailTemplate = options.mailTemplate
# support both cozydb & americano-cozy
try
cozydb = require 'cozydb'
CozyAdapter = cozydb.api
Contact = cozydb.getModel 'Contact',
fn : String
n : String
_attachments : Object
datapoints : [Object]
catch err
americano = require 'americano-cozy'
CozyAdapter = try require 'americano-cozy/node_modules/' + \
'jugglingdb-cozy-adapter'
catch e then require 'jugglingdb-cozy-adapter'
americano.getModel 'Contact',
fn : String
n : String
_attachments : (x) -> x
datapoints : (x) -> x
# send a share mail
sendMail = (doc, key, cb) ->
rule = doc.clearance.filter((rule) -> rule.key is key)[0]
doc.getPublicURL (err, url) ->
return cb err if err
urlObject = urlHelpers.parse url
urlObject.query = key: rule.key
url = urlObject.format()
emailOptions = {doc, url, rule}
async.parallel [
(cb) -> mailSubject emailOptions, cb
(cb) -> mailTemplate emailOptions, cb
], (err, results) ->
return cb err if err
[subject, htmlContent] = results
emailInfo =
to: rule.email
subject: subject
content: url
html: htmlContent
if options.attachments
emailInfo.attachments = options.attachments
CozyAdapter.sendMailFromUser emailInfo, cb
# change the whole clearance object
out.change = (req, res, next) ->
{clearance} = req.body
req.doc.updateAttributes clearance: clearance, (err) ->
return next err if err
res.send req.doc
# send multiple mails
# expect body = [<rule>]
out.sendAll = (req, res, next) ->
toSend = req.body
sent = []
async.each toSend, (rule, cb) ->
sent.push rule.key
sendMail req.doc, rule.key, cb
, (err) ->
return next err if err
newClearance = req.doc.clearance.map (rule) ->
rule.sent = true if rule.key in sent
return rule
req.doc.updateAttributes clearance: newClearance, (err) ->
return next err if err
res.send req.doc
out.getEmailsFromContactFields = (contact) ->
emails = contact.datapoints?.filter (dp) -> dp.name is 'email'
emails = emails.map (dp) -> dp.value
emails
# take directly full name or build it from the name field.
out.getContactFullName = (contact) ->
contact.fn or contact.n?.split(';')[0..1].join(' ')
out.simplifyContact = (contact) ->
name = out.getContactFullName contact
emails = out.getEmailsFromContactFields contact
return simple =
id: contact.id
hasPicture: contact._attachments?.picture?
name: name or '?'
emails: emails or []
# contact list for autocomplete
out.contactList = (req, res, next) ->
Contact.request 'all', (err, contacts) ->
return next err if err
res.send contacts.map out.simplifyContact
out.contactPicture = (req, res, next) ->
Contact.find req.params.contactid, (err, contact) ->
return next err if err
unless contact._attachments?.picture
err = new Error('not found')
err.status = 404
return next err
stream = contact.getFile 'picture', (err) ->
return res.error 500, "File fetching failed.", err if err
stream.pipe res
out.contact = (req, res, next) ->
Contact.find req.params.contactid, (err, contact) ->
return next err if err
unless contact
err = new Error 'not found'
err.status = 404
return next err
res.send out.simplifyContact contact
return out