git ssb

0+

farewellutopia-dev / patchboot



Tree: 54f45b9e3a2114fd3c234a8de046349c9a3dab1c

Files: 54f45b9e3a2114fd3c234a8de046349c9a3dab1c / src / VotesManager.js

2550 bytesRaw
1import { default as pull, paraMap, collect } from 'pull-stream'
2pull.paraMap = paraMap
3
4class VotesManager {
5
6 constructor(sbot) {
7 this.sbot = sbot
8 }
9
10 vote(id, value) {
11 return new Promise((resolve, reject) => {
12 this.sbot.publish({
13 type: 'vote',
14 vote: {
15 'link': id,
16 value,
17 expression: value ? 'Like' : 'Unlike'
18 }
19 }, function (err, msg) {
20 if (err) {
21 reject(err)
22 } else {
23 resolve(true)
24 }
25 })
26 })
27 }
28
29 getVotes(id) {
30
31 const queryOpts = {
32 reverse: false,
33 query: [{
34 $filter: {
35 value: {
36 content: {
37 type: 'vote',
38 vote: {
39 link: id
40 }
41 }
42 }
43 }
44 },
45 {
46 $map: {
47 author: ['value', 'author'],
48 value: ['value', 'content', 'vote', 'value']
49 }
50 }]
51 }
52 const backlinksOpts = {
53 reverse: false,
54 query: [{
55 $filter: {
56 dest: id,
57 value: {
58 content: {
59 type: 'vote',
60 vote: {
61 link: id
62 }
63 }
64 }
65 }
66 },
67 {
68 $map: {
69 author: ['value', 'author'],
70 value: ['value', 'content', 'vote', 'value']
71 }
72 }]
73 }
74
75 const votesMapPromises = new Promise((resolve, reject) => {
76 const votes = new Map();
77 pull(
78 this.sbot.backlinks ? this.sbot.backlinks.read(backlinksOpts) : this.sbot.query.read(queryOpts),
79 /*pull.drain((msg) => {
80 votes.set(msg.author, msg.value)
81 },
82 () => {
83 resolve(votes);
84 })*/
85 pull.collect((err, msgs) => {
86 if (err) {
87 reject(err)
88 } else {
89 msgs.forEach(msg => {
90 //console.log('msg', msg)
91 votes.set(msg.author, msg.value)
92 });
93 resolve(votes);
94 }
95 })
96 )
97 })
98
99 return votesMapPromises.then(votesMap => votesMap.entries())
100 .then(entries => [...entries].filter(mapping => mapping[1] > 0))
101 .then(filtered => filtered.map(tuple => tuple[0]))
102 }
103
104 getOwnVote(msgID) {
105 return new Promise((resolve, reject) => {
106 this.sbot.whoami().then(thisisme => {
107 const feedID = thisisme.id
108 return this.getVotes(msgID).then(votes => {
109 resolve(votes.indexOf(feedID) > -1)
110 })
111 }).catch(reject)
112 })
113 }
114
115}
116
117export { VotesManager }

Built with git-ssb-web