Files: a3f728451ea4f496b80c42138d030295e43de153 / message / obs / likes.js
1337 bytesRaw
1 | var nest = require('depnest') |
2 | var ref = require('ssb-ref') |
3 | var { Value, computed } = require('mutant') |
4 | |
5 | exports.needs = nest({ |
6 | 'message.sync.unbox': 'first' |
7 | }) |
8 | |
9 | exports.gives = nest({ |
10 | 'sbot.hook.feed': true, |
11 | 'message.obs.likes': true |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | var likesLookup = {} |
16 | return nest({ |
17 | 'sbot.hook.feed': (msg) => { |
18 | if (!(msg && msg.value && msg.value.content)) return |
19 | if (typeof msg.value.content === 'string') { |
20 | msg = api.message.sync.unbox(msg) |
21 | if (!msg) return |
22 | } |
23 | |
24 | var c = msg.value.content |
25 | if (c.type !== 'vote') return |
26 | if (!c.vote || !c.vote.link) return |
27 | |
28 | var likes = get(c.vote.link)() |
29 | var author = msg.value.author |
30 | if (!likes[author] || likes[author][1] < msg.timestamp) { |
31 | likes[author] = [c.vote.value > 0, msg.timestamp] |
32 | get(c.vote.link).set(likes) |
33 | } |
34 | }, |
35 | 'message.obs.likes': (id) => { |
36 | if (!ref.isLink(id)) throw new Error('an id must be specified') |
37 | return computed(get(id), getLikes) |
38 | } |
39 | }) |
40 | |
41 | function get (id) { |
42 | if (!likesLookup[id]) { |
43 | likesLookup[id] = Value({}) |
44 | } |
45 | return likesLookup[id] |
46 | } |
47 | } |
48 | |
49 | function getLikes (likes) { |
50 | return Object.keys(likes).reduce((result, id) => { |
51 | if (likes[id][0]) { |
52 | result.push(id) |
53 | } |
54 | return result |
55 | }, []) |
56 | } |
57 |
Built with git-ssb-web