Files: 58ab0241031aa549a35cce1e678c27065ae66221 / lib / plugins / likes.js
2494 bytesRaw
1 | const pull = require('pull-stream') |
2 | |
3 | exports.manifest = { |
4 | read: 'source', |
5 | countStream: 'source', |
6 | feedLikesMsgStream: 'source', |
7 | get: 'async' |
8 | } |
9 | |
10 | exports.init = function (ssb) { |
11 | return { |
12 | read, |
13 | feedLikesMsgStream: function ({ feedId, msgId }) { |
14 | let value = false |
15 | let sync = false |
16 | return pull( |
17 | ssb.backlinks.read({ |
18 | live: true, |
19 | query: [{ |
20 | $filter: { |
21 | dest: msgId, |
22 | value: { |
23 | author: feedId, |
24 | content: { type: 'vote', vote: { link: msgId } } |
25 | } |
26 | } |
27 | }] |
28 | }), |
29 | pull.map((msg) => { |
30 | if (msg.sync) { |
31 | sync = true |
32 | return value |
33 | } |
34 | |
35 | const vote = msg.value.content.vote |
36 | if (vote) { |
37 | value = vote.value > 0 |
38 | if (sync) return value |
39 | } |
40 | }), |
41 | pull.filter(present) |
42 | ) |
43 | }, |
44 | get: function ({ dest }, cb) { |
45 | const ids = new Set() |
46 | pull( |
47 | read({ dest }), |
48 | pull.drain(msg => { |
49 | const author = msg.value.author |
50 | const vote = msg.value.content.vote |
51 | if (vote) { |
52 | if (vote.value > 0) { |
53 | ids.add(author) |
54 | } else { |
55 | ids.delete(author) |
56 | } |
57 | } |
58 | }, (err) => { |
59 | if (err) return cb(err) |
60 | cb(null, Array.from(ids)) |
61 | }) |
62 | ) |
63 | }, |
64 | countStream: function ({ dest }) { |
65 | const ids = new Set() |
66 | let sync = false |
67 | return pull( |
68 | read({ dest, live: true }), |
69 | pull.map(msg => { |
70 | if (msg.sync) { |
71 | sync = true |
72 | return ids.size |
73 | } |
74 | |
75 | const author = msg.value.author |
76 | const vote = msg.value.content.vote |
77 | if (vote) { |
78 | if (vote.value > 0) { |
79 | ids.add(author) |
80 | } else { |
81 | ids.delete(author) |
82 | } |
83 | } |
84 | |
85 | if (sync) { |
86 | return ids.size |
87 | } |
88 | }), |
89 | pull.filter(present) |
90 | ) |
91 | } |
92 | } |
93 | |
94 | function read ({ |
95 | reverse = false, |
96 | limit = null, |
97 | live = null, |
98 | dest = null |
99 | }) { |
100 | return pull( |
101 | ssb.backlinks.read({ |
102 | reverse, |
103 | live, |
104 | limit, |
105 | query: [{ |
106 | $filter: { |
107 | dest, |
108 | value: { content: { type: 'vote', vote: { link: dest } } } |
109 | } |
110 | }] |
111 | }) |
112 | ) |
113 | } |
114 | } |
115 | |
116 | function present (value) { |
117 | return value != null |
118 | } |
119 |
Built with git-ssb-web