Files: d5f37c13cd95a2e321a1e51db1e64eef746ee0ea / message / obs / backlinks.js
1390 bytesRaw
1 | var nest = require('depnest') |
2 | var MutantSet = require('mutant/set') |
3 | |
4 | exports.needs = nest({ |
5 | 'message.sync.unbox': 'first' |
6 | }) |
7 | |
8 | exports.gives = nest({ |
9 | 'sbot.hook.feed': true, |
10 | 'message.obs.backlinks': true, |
11 | 'message.obs.forks': true |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | var mentionedLookup = {} |
16 | var rootLookup = {} |
17 | |
18 | return nest({ |
19 | 'sbot.hook.feed': (msg) => { |
20 | if (msg.value && typeof msg.value.content === 'string') { |
21 | msg = api.message.sync.unbox(msg) |
22 | } |
23 | |
24 | if (msg && msg.value && msg.value.content) { |
25 | if (Array.isArray(msg.value.content.mentions)) { |
26 | msg.value.content.mentions.forEach(mention => { |
27 | var link = typeof mention === 'object' ? mention.link : mention |
28 | backlinks(link).add(msg.key) |
29 | }) |
30 | } |
31 | |
32 | var root = msg.value.content.root |
33 | var branch = msg.value.content.branch || root |
34 | |
35 | // fork or root reply |
36 | if (root && root === branch) { |
37 | forks(root).add(msg.key) |
38 | } |
39 | } |
40 | }, |
41 | 'message.obs.backlinks': (id) => backlinks(id), |
42 | 'message.obs.forks': (id) => forks(id) |
43 | }) |
44 | |
45 | function backlinks (id) { |
46 | if (!mentionedLookup[id]) { |
47 | mentionedLookup[id] = MutantSet() |
48 | } |
49 | return mentionedLookup[id] |
50 | } |
51 | |
52 | function forks (id) { |
53 | if (!rootLookup[id]) { |
54 | rootLookup[id] = MutantSet() |
55 | } |
56 | return rootLookup[id] |
57 | } |
58 | } |
59 |
Built with git-ssb-web