forked from ssbc/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.js
More file actions
422 lines (371 loc) · 13.5 KB
/
Copy pathprocessor.js
File metadata and controls
422 lines (371 loc) · 13.5 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
var mlib = require('ssb-msgs')
var u = require('./util')
module.exports = function (sbot, db, state, emit) {
var processors = {
post: function (msg) {
var me = getProfile(sbot.id)
var author = msg.value.author
var by_me = (author === sbot.id)
var c = msg.value.content
var root = mlib.link(c.root, 'msg')
var recps = mlib.links(c.recps)
var mentions = mlib.links(c.mentions)
// inbox index:
// add msgs that mention or address the user
var inboxRow
if (u.findLink(mentions, sbot.id) || u.findLink(recps, sbot.id)) {
// if a reply, make sure we have the root already
if (root)
inboxRow = state.inbox.sortedUpdate(msg.received, root.link)
if (!inboxRow) {
// fallback to inserting the current message, if the root isnt available (or its not a reply)
inboxRow = state.inbox.sortedUpsert(msg.received, msg.key)
}
emit('index-change', { index: 'inbox' })
attachChildIsRead(inboxRow, msg.key)
}
// update for replies
else if (root) {
inboxRow = state.inbox.find(root.link)
if (inboxRow) {
state.inbox.sortedUpsert(msg.received, root.link)
emit('index-change', { index: 'inbox' })
attachChildIsRead(inboxRow, msg.key)
}
}
// inbox sub-indexes
// note, these sub-indexes shouldnt have overlap: thus the else/if
// private posts index
var bookmarkRow
if (u.findLink(recps, sbot.id)) {
var privatePostsRow
// update replies
if (root) {
privatePostsRow = state.privatePosts.find(root.link)
if (privatePostsRow) {
state.privatePosts.sortedUpsert(ts(msg), root.link)
emit('index-change', { index: 'privatePosts' })
attachChildIsRead(privatePostsRow, msg.key)
}
}
// add msgs addressed to the user
else if (!privatePostsRow) {
privatePostsRow = state.privatePosts.sortedUpsert(ts(msg), root ? root.link : msg.key)
emit('index-change', { index: 'privatePosts' })
attachChildIsRead(privatePostsRow, msg.key)
}
}
// bookmarks index: update for replies
else if (root && (bookmarkRow = state.bookmarks.find(root.link))) {
state.bookmarks.sortedUpsert(ts(msg), root.link)
emit('index-change', { index: 'bookmarks' })
attachChildIsRead(bookmarkRow, msg.key)
}
// mentions index: add msgs that mention the user
else if (u.findLink(mentions, sbot.id)) {
var mentionRow = state.mentions.sortedUpsert(ts(msg), root ? root.link : msg.key)
emit('index-change', { index: 'mentions' })
attachChildIsRead(mentionRow, msg.key)
}
// public posts index: add public posts / replies
if (recps.length === 0) {
state.publicPosts.sortedUpsert(ts(msg), root ? root.link : msg.key)
emit('index-change', { index: 'publicPosts' })
}
if (c.channel && typeof c.channel === 'string') {
// channels index: add public posts / replies
var indexName = 'channel-'+c.channel
var index = state[indexName] = (state[indexName] || u.index(indexName))
index.sortedUpsert(msg.value.timestamp, root ? root.link : msg.key)
emit('index-change', { index: indexName })
// inbox index: add watched channels
if (index.watched) {
var inboxRow
if (root)
// only do updates if a reply, we dont want inbox items that dont have a root
inboxRow = state.inbox.sortedUpdate(ts(msg), root.link)
else
inboxRow = state.inbox.sortedInsert(ts(msg), msg.key)
if (inboxRow) {
emit('index-change', { index: 'inbox' })
attachChildIsRead(inboxRow, msg.key)
}
}
}
},
contact: function (msg) {
mlib.links(msg.value.content.contact, 'feed').forEach(function (link) {
// update profiles
var toself = link.link === msg.value.author
if (toself) updateSelfContact(msg.value.author, msg)
else updateOtherContact(msg.value.author, link.link, msg)
// notices index: add follows or blocks
if (link.link === sbot.id && ('following' in msg.value.content || 'blocking' in msg.value.content)) {
state.notices.sortedUpsert(ts(msg), msg.key)
emit('index-change', { index: 'notices' })
}
})
},
about: function (msg) {
// update profiles
mlib.links(msg.value.content.about, 'feed').forEach(function (link) {
var toself = link.link === msg.value.author
if (toself) updateSelfContact(msg.value.author, msg)
else updateOtherContact(msg.value.author, link.link, msg)
})
},
vote: function (msg) {
// notices index: add upvotes on your messages
var msgLink = mlib.link(msg.value.content.vote, 'msg')
if (msgLink && state.mymsgs.indexOf(msgLink.link) >= 0 && msgLink.value > 0) {
state.notices.sortedUpsert(ts(msg), msg.key)
emit('index-change', { index: 'notices' })
}
// user flags
var voteLink = mlib.link(msg.value.content.vote, 'feed')
if (voteLink) {
var target = getProfile(voteLink.link)
if (voteLink.value < 0)
target.flaggers[msg.value.author] = { msgKey: msg.key, reason: voteLink.reason }
else
delete target.flaggers[msg.value.author]
}
}
}
function getProfile (pid) {
if (pid.id) // already a profile?
return pid
var profile = state.profiles[pid]
if (!profile) {
state.profiles[pid] = profile = {
id: pid,
self: { name: null, image: null }, // values set by this user about this user
byMe: { name: null, image: null }, // values set by the local user about this user
names: {}, // map of name -> array of users to use that name
images: {}, // map of images -> array of users to use that pic
followers: {}, // map of followers -> true
flaggers: {}, // map of flaggers -> flag-msg
}
}
return profile
}
function updateSelfContact (author, msg) {
var c = msg.value.content
author = getProfile(author)
// name: a non-empty string
if (nonEmptyStr(c.name)) {
var safeName = makeNameSafe(c.name)
// remove old assignment, if it exists
for (var name in author.names) {
author.names[name] = author.names[name].filter(function (id) { return id !== author.id })
if (!author.names[name][0])
delete author.names[name]
}
// add new assignment
author.self.name = safeName
author.names[safeName] = (author.names[safeName]||[]).concat(author.id)
rebuildNamesFor(author)
}
// image: link to image
if ('image' in c) {
var imageLink = mlib.link(c.image, 'blob')
if (imageLink) {
// remove old assignment, if it exists
for (var image in author.images) {
author.images[image] = author.images[image].filter(function (id) { return id !== author.id })
if (!author.images[image][0])
delete author.images[image]
}
// add new assignment
author.self.image = imageLink
if (author.id == sbot.id)
author.byMe.image = imageLink
author.images[imageLink.link] = (author.images[imageLink.link]||[]).concat(author.id)
}
}
}
function updateOtherContact (source, target, msg) {
var c = msg.value.content
source = getProfile(source)
target = getProfile(target)
// name: a non-empty string
if (nonEmptyStr(c.name)) {
var safeName = makeNameSafe(c.name)
// remove old assignment, if it exists
for (var name in target.names) {
target.names[name] = target.names[name].filter(function (id) { return id !== source.id })
if (!target.names[name][0])
delete target.names[name]
}
// add new assignment
target.names[safeName] = (target.names[safeName]||[]).concat(source.id)
if (source.id === sbot.id)
target.byMe.name = safeName
rebuildNamesFor(target)
}
// image: link to image
if ('image' in c) {
var imageLink = mlib.link(c.image, 'blob')
if (imageLink) {
// remove old assignment, if it exists
for (var image in target.images) {
target.images[image] = target.images[image].filter(function (id) { return id !== source.id })
if (!target.images[image][0])
delete target.images[image]
}
// add new assignment
target.images[imageLink.link] = (target.images[imageLink.link]||[]).concat(source.id)
if (source.id == sbot.id)
target.byMe.image = imageLink
}
}
// following: bool
if (typeof c.following === 'boolean') {
if (c.following)
target.followers[source.id] = true
else
delete target.followers[source.id]
// if from the user, update names (in case un/following changes conflict status)
if (msg.value.author == sbot.id)
rebuildNamesFor(target)
}
}
function rebuildNamesFor (profile) {
profile = getProfile(profile)
// remove oldname from id->name map
var oldname = state.names[profile.id]
if (oldname) {
if (state.ids[oldname] == profile.id) {
// remove
delete state.ids[oldname]
} else if (Array.isArray(state.ids[oldname])) {
// is in a conflict, remove from conflict array
var i = state.ids[oldname].indexOf(profile.id)
if (i !== -1) {
state.ids[oldname].splice(i, 1)
if (state.ids[oldname].length === 1) {
// conflict resolved
delete state.actionItems[oldname]
state.ids[oldname] = state.ids[oldname][0]
}
}
}
}
// default to self-assigned name
var name = profile.self.name
// override with name assigned by the local user
if (profile.id !== sbot.id && profile.byMe.name)
name = profile.byMe.name
// abort if there's no name at all
if (!name)
return
// store
state.names[profile.id] = name
// if following, update id->name map
if (profile.id === sbot.id || profile.followers[sbot.id]) {
if (!state.ids[name]) { // no conflict?
// take it
state.ids[name] = profile.id
} else {
// keep track of all assigned ids
if (Array.isArray(state.ids[name]))
state.ids[name].push(profile.id)
else
state.ids[name] = [state.ids[name], profile.id]
// conflict, this needs to be handled by the user
state.actionItems[name] = {
type: 'name-conflict',
name: name,
ids: state.ids[name]
}
}
}
}
function attachIsRead (indexRow, key) {
key = key || indexRow.key
state.pinc()
db.isread.get(key, function (err, v) {
indexRow.isread = !!v
state.pdec()
})
}
// look up the child and root isread state, combine them with the current row's isread state
function attachChildIsRead (indexRow, childKey, cb) {
state.pinc()
var rootIsRead, childIsRead
// get child isread
db.isread.get(childKey, function (err, v) {
childIsRead = !!v
next()
})
// lookup the root isread from DB if not already on the row
if (typeof indexRow.isread == 'boolean') {
rootIsRead = indexRow.isread
} else {
db.isread.get(indexRow.key, function (err, v) {
rootIsRead = !!v
next()
})
}
function next () {
// wait for both
if (typeof rootIsRead != 'boolean' || typeof childIsRead != 'boolean')
return
// combine child and root isread state
indexRow.isread = rootIsRead && childIsRead
cb && cb(null, indexRow.isread)
state.pdec() // call this last, after all async work is done
}
}
function follows (a, b) {
if (a.id) a = a.id // if `a` is a profile, just get its id
if (b.id) b = b.id // if `b` is a profile, just get its id
return (a != b && getProfile(b).followers[a])
}
// helper to get the most reliable timestamp for a message
// - stops somebody from boosting their ranking (accidentally, maliciously) with a future TS
// - applies only when ordering by most-recent
function ts (msg) {
return Math.min(msg.received, msg.value.timestamp)
}
// exported api
function fn (logkey) {
state.pinc()
var key = logkey.value
sbot.get(logkey.value, function (err, value) {
var msg = { key: key, value: value, received: logkey.key }
try {
// encrypted? try to decrypt
if (typeof value.content == 'string' && value.content.slice(-4) == '.box') {
value.content = sbot.private.unbox(value.content)
if (!value.content)
return state.pdec()
}
// collect keys of user's messages
if (msg.value.author === sbot.id)
state.mymsgs.push(msg.key)
// type processing
var process = processors[msg.value.content.type]
if (process)
process(msg)
}
catch (e) {
// :TODO: use sbot logging plugin
console.error('Failed to process message', e, e.stack, key, value)
}
state.pdec()
})
}
return fn
}
function nonEmptyStr (str) {
return (typeof str === 'string' && !!(''+str).trim())
}
// allow A-z0-9._-, dont allow a trailing .
var badNameCharsRegex = /[^A-z0-9\._-]/g
function makeNameSafe (str) {
str = str.replace(badNameCharsRegex, '_')
if (str.charAt(str.length - 1) == '.')
str = str.slice(0, -1) + '_'
return str
}