Files: 13a42b9020d8fdd3d7cc9eb1b43f0a554166b3e1 / feed / pull / mentions.js
1774 bytesRaw
1 | const nest = require('depnest') |
2 | const extend = require('xtend') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.needs = nest({ |
6 | 'sbot.pull.log': 'first', |
7 | 'message.sync.unbox': 'first', |
8 | 'sbot.sync.cache': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest('feed.pull.mentions') |
12 | |
13 | exports.create = function (api) { |
14 | return nest('feed.pull.mentions', function (id) { |
15 | var cache = api.sbot.sync.cache() |
16 | |
17 | return function getStream (opts) { |
18 | opts = extend(opts) |
19 | var take = opts.limit |
20 | opts.limit = 5000 |
21 | |
22 | var stream = pull( |
23 | api.sbot.pull.log(opts), |
24 | unboxIfNeeded(), |
25 | pull.filter((msg) => { |
26 | if (!msg) return false |
27 | if (msg.sync) return true |
28 | return msg.value.author !== id && belongsToUs([ |
29 | msg.value.content.mentions, |
30 | msg.value.content.root, |
31 | msg.value.content.branch, |
32 | msg.value.content.repo, |
33 | msg.value.content.vote, |
34 | msg.value.content.about, |
35 | msg.value.content.contact |
36 | ]) |
37 | }) |
38 | ) |
39 | |
40 | if (take) { |
41 | return pull( |
42 | stream, |
43 | pull.take(take) |
44 | ) |
45 | } else { |
46 | return stream |
47 | } |
48 | } |
49 | |
50 | function belongsToUs (link) { |
51 | if (Array.isArray(link)) { |
52 | return link.some(belongsToUs) |
53 | } |
54 | if (link) { |
55 | link = typeof link === 'object' ? link.link : link |
56 | if (link === id) return true |
57 | var item = cache[link] |
58 | if (item) { |
59 | return (item.author === id) |
60 | } |
61 | } |
62 | } |
63 | }) |
64 | |
65 | function unboxIfNeeded () { |
66 | return pull.map(function (msg) { |
67 | if (msg.sync || (msg.value && typeof msg.value.content === 'object')) { |
68 | return msg |
69 | } else { |
70 | return api.message.sync.unbox(msg) |
71 | } |
72 | }) |
73 | } |
74 | } |
75 |
Built with git-ssb-web