git ssb

1+

Daan Patchwork / patchwork



Tree: 654bc8db06bf328fa22b234b99ce9bb907694ec0

Files: 654bc8db06bf328fa22b234b99ce9bb907694ec0 / lib / plugins / likes.js

2538 bytesRaw
1const pull = require('pull-stream')
2
3exports.manifest = {
4 read: 'source',
5 countStream: 'source',
6 feedLikesMsgStream: 'source',
7 get: 'async'
8}
9
10exports.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 return null
41 }),
42 pull.filter(present)
43 )
44 },
45 get: function ({ dest }, cb) {
46 const ids = new Set()
47 pull(
48 read({ dest }),
49 pull.drain(msg => {
50 const author = msg.value.author
51 const vote = msg.value.content.vote
52 if (vote) {
53 if (vote.value > 0) {
54 ids.add(author)
55 } else {
56 ids.delete(author)
57 }
58 }
59 }, (err) => {
60 if (err) return cb(err)
61 cb(null, Array.from(ids))
62 })
63 )
64 },
65 countStream: function ({ dest }) {
66 const ids = new Set()
67 let sync = false
68 return pull(
69 read({ dest, live: true }),
70 pull.map(msg => {
71 if (msg.sync) {
72 sync = true
73 return ids.size
74 }
75
76 const author = msg.value.author
77 const vote = msg.value.content.vote
78 if (vote) {
79 if (vote.value > 0) {
80 ids.add(author)
81 } else {
82 ids.delete(author)
83 }
84 }
85
86 if (sync) {
87 return ids.size
88 }
89 return null
90 }),
91 pull.filter(present)
92 )
93 }
94 }
95
96 function read ({
97 reverse = false,
98 limit = null,
99 live = null,
100 dest = null
101 }) {
102 return pull(
103 ssb.backlinks.read({
104 reverse,
105 live,
106 limit,
107 query: [{
108 $filter: {
109 dest,
110 value: { content: { type: 'vote', vote: { link: dest } } }
111 }
112 }]
113 })
114 )
115 }
116}
117
118function present (value) {
119 return value != null
120}
121

Built with git-ssb-web