-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotifications.js
270 lines (247 loc) · 8.17 KB
/
notifications.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict'
var pull = require('pull-stream')
var mlib = require('ssb-msgs')
var multicb = require('multicb')
var cat = require('pull-cat')
var fs = require('fs')
var os = require('os')
var path = require('path')
var toPull = require('stream-to-pull-stream')
var getAvatar = require('ssb-avatar')
var defaultIcon = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256'
function truncate(str, len) {
if (!str) return ''
str = String(str)
return str.length < len ? str : str.substr(0, len-1) + '…'
}
function trimMessage(msg) {
var text = msg.value.content.text
// Should private messages be shortened, for privacy?
return truncate(text, 255)
}
function isNotFoundError(err) {
return err.notFound || err.name === 'NotFoundError'
}
function decryptPrivateMessage(sbot, msg, cb) {
var content = msg && msg.value && msg.value.content
if (sbot.private && typeof content === 'string' && content.slice(-4) === '.box')
sbot.private.unbox(content, function (err, content) {
if (err && err.message === 'failed to decrypt') err = null
if (err || !content) return cb(err)
return cb(null, {
key: msg.key,
private: true,
value: {
content: content,
author: msg.value.author
}
})
})
else
cb(null, msg)
}
function decryptPrivateMessages(sbot) {
return pull(
pull.asyncMap(decryptPrivateMessage.bind(this, sbot)),
pull.filter()
)
}
function findLink(links, id) {
for (var i = 0; i < (links ? links.length : 0); i++)
if (links[i].link === id)
return links[i]
}
function getMsg(sbot, key, cb) {
sbot.get(key, function (err, value) {
if (err && isNotFoundError(err)) return cb()
else if (err) return cb(err)
else decryptPrivateMessage(sbot, {key: key, value: value}, cb)
})
}
function getLinkedMsg(sbot, content, cb) {
var link = mlib.link(content, 'msg')
if (link) getMsg(sbot, link.link, cb)
else cb()
}
function makeUrl(msg) {
return 'http://localhost:7777/#msg/' + encodeURIComponent(msg.key)
}
// Get filename for a blob
function getBlobFile(sbot, id, cb) {
var fileName = path.join(os.tmpdir(), encodeURIComponent(id))
fs.exists(fileName, function (exists) {
if (exists) return cb(null, fileName)
sbot.blobs.want(id, function (err, has) {
if (err) cb(err)
else if (!has) cb()
else pull(
sbot.blobs.get(id),
toPull.sink(fs.createWriteStream(fileName), function (err) {
cb(err, fileName)
})
)
})
})
}
// Get a name for a thing from multiple fallback sources
function getName(sbot, sources, dest, cb) {
var name
pull(
cat(sources.map(function (source) {
return sbot.links({
source: source,
dest: dest,
rel: 'about',
values: true,
keys: false,
meta: false,
reverse: true
})
})),
pull.drain(function (value) {
name = value && value.content && value.content.name
if (name) return false
}, function (err) {
cb(err === true ? null : err, name)
})
)
}
// through stream to turn messages into notifications
module.exports = function (sbot, myId) {
// get name and icon for a user
function getAbout(id, cb) {
getAvatar(sbot, myId, id, function (err, about) {
if (err) return cb(err)
getBlobFile(sbot, about.image || defaultIcon, function (err, path) {
if (err) return cb(err)
cb(null, {name: about.name || truncate(id, 8), image: path})
})
})
}
return pull(
pull.filter(function (msg) { return msg.sync === undefined }),
decryptPrivateMessages(sbot),
pull.asyncMap(function (msg, cb) {
var c = msg && msg.value && msg.value.content
if (!c || typeof c != 'object') return cb()
// ignore own messages
if (msg.value.author === myId) return cb()
switch (c.type) {
case 'post':
if (findLink(mlib.links(c.mentions), myId)) {
var subject = trimMessage(msg) || 'a message'
return getAbout(msg.value.author, function (err, about) {
cb(err, {
icon: about.image,
title: about.name + ' mentioned you',
message: subject,
open: makeUrl(msg)
})
})
} else if (msg.private) {
return getAbout(msg.value.author, function (err, about) {
if (err) return cb(err)
cb(null, {
icon: about.image,
title: about.name + ' sent you a private message',
message: trimMessage(msg),
open: makeUrl(msg)
})
})
} else if (c.root || c.branch) {
// check if this is a reply to one of our messages
var done = multicb({ pluck: 1, spread: true })
getLinkedMsg(sbot, c.root, done())
getLinkedMsg(sbot, c.branch, done())
return done(function (err, root, branch) {
if (err) return cb(err)
var subject
if (root && root.value.author === myId)
subject = 'your thread'
else if (branch && branch.value.author === myId)
subject = 'your post'
else
return cb()
getAbout(msg.value.author, function (err, about) {
if (err) return cb(err)
cb(null, {
icon: about.image,
title: about.name + ' replied to ' + subject,
message: trimMessage(msg),
open: makeUrl(msg)
})
})
})
}
return cb()
case 'contact':
if (c.contact === myId) {
return getAbout(msg.value.author, function (err, about) {
if (err) return cb(err)
var action =
(c.following === true) ? 'followed' :
(c.blocking === true) ? 'blocked' :
(c.following === false) ? 'unfollowed' :
'???'
cb(null, {
icon: about.image,
title: about.name + ' ' + action + ' you',
open: makeUrl(msg)
})
})
}
return cb()
case 'vote':
var vote = c.vote
if (!vote || typeof vote.value !== 'number')
return cb()
return getLinkedMsg(sbot, vote, function (err, subject) {
if (err) {
if (isNotFoundError(err)) return cb()
else return cb(err)
}
if (!subject || subject.value.author !== myId) return cb()
getAbout(msg.value.author, function (err, about) {
if (err) return cb(err)
var action =
(vote.value > 0) ? 'dug' :
(vote.value < 0) ? 'flagged' :
'removed their vote for'
var reason = vote.reason ? ' as ' + vote.reason : ''
var target = subject.private ? 'private message' : 'message'
cb(null, {
icon: about.image,
title: about.name + ' ' + action + ' your ' + target
+ ' ' + reason,
message: trimMessage(subject),
open: makeUrl(msg)
})
})
})
case 'pull-request':
case 'issue':
return getLinkedMsg(sbot, c.repo || c.project, function (err, repo) {
if (err) return cb(err)
if (!repo || repo.value.author !== myId) return cb()
var done = multicb({ pluck: 1, spread: true })
getAbout(msg.value.author, done())
getName(sbot, [myId, c.repo, null], c.repo, done())
done(function (err, author, repoName) {
if (err) return cb(err)
var what = c.type === 'issue' ? 'an issue' : 'a pull request'
var dest = repoName || truncate(c.repo, 16)
cb(null, {
icon: author.image,
title: author.name + ' opened ' + what + ' on ' + dest,
message: trimMessage(msg),
open: makeUrl(msg)
})
})
})
default:
cb()
}
}),
pull.filter(Boolean)
)
}