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