Files: 043b2e20476159dcff94dd46890be1c353e9132e / feed / pull / mentions.js
2010 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 | // handle last item passed in as lt |
25 | opts.lt = typeof opts.lt === 'object' |
26 | ? opts.lt.timestamp |
27 | : opts.lt |
28 | |
29 | var stream = pull( |
30 | api.sbot.pull.log(opts), |
31 | unboxIfNeeded(), |
32 | pull.filter((msg) => { |
33 | if (!msg) return false |
34 | if (msg.sync) return true |
35 | return msg.value.author !== id && belongsToUs([ |
36 | msg.value.content.mentions, |
37 | msg.value.content.root, |
38 | msg.value.content.branch, |
39 | msg.value.content.repo, |
40 | msg.value.content.vote, |
41 | msg.value.content.about, |
42 | msg.value.content.contact |
43 | ]) |
44 | }) |
45 | ) |
46 | |
47 | if (take) { |
48 | return pull( |
49 | stream, |
50 | pull.take(take) |
51 | ) |
52 | } else { |
53 | return stream |
54 | } |
55 | } |
56 | |
57 | function belongsToUs (link) { |
58 | if (Array.isArray(link)) { |
59 | return link.some(belongsToUs) |
60 | } |
61 | if (link) { |
62 | link = typeof link === 'object' ? link.link : link |
63 | if (link === id) return true |
64 | var item = cache[link] |
65 | if (item) { |
66 | return (item.author === id) |
67 | } |
68 | } |
69 | } |
70 | }) |
71 | |
72 | function unboxIfNeeded () { |
73 | return pull.map(function (msg) { |
74 | if (msg.sync || (msg.value && typeof msg.value.content === 'object')) { |
75 | return msg |
76 | } else { |
77 | return api.message.sync.unbox(msg) |
78 | } |
79 | }) |
80 | } |
81 | } |
82 |
Built with git-ssb-web