git ssb

1+

Daan Patchwork / patchwork



Tree: 4d9f238ff73f7136cd292da88f06c17fe1a8c445

Files: 4d9f238ff73f7136cd292da88f06c17fe1a8c445 / lib / depject / message / obs / likes.js

1443 bytesRaw
1const nest = require('depnest')
2const ref = require('ssb-ref')
3const MutantPullValue = require('../../../mutant-pull-value')
4
5exports.needs = nest({
6 'message.sync.unbox': 'first',
7 'sbot.pull.stream': 'first',
8 'keys.sync.id': 'first'
9})
10
11exports.gives = nest({
12 'sbot.hook.publish': true,
13 'message.obs.likeCount': true,
14 'message.obs.doesLike': true
15})
16
17exports.create = function (api) {
18 const activeLikes = new Set()
19 return nest({
20 'sbot.hook.publish': (msg) => {
21 if (!(msg && msg.value && msg.value.content)) return
22 if (typeof msg.value.content === 'string') {
23 msg = api.message.sync.unbox(msg)
24 if (!msg) return
25 }
26
27 const c = msg.value.content
28 if (c.type !== 'vote') return
29 if (!c.vote || !c.vote.link) return
30
31 activeLikes.forEach((likes) => {
32 if (likes.id === c.vote.link) {
33 likes.push(msg)
34 }
35 })
36 },
37 'message.obs.doesLike': (id) => {
38 const yourId = api.keys.sync.id()
39 return MutantPullValue(() => {
40 return api.sbot.pull.stream((sbot) => sbot.patchwork.likes.feedLikesMsgStream({ msgId: id, feedId: yourId }))
41 })
42 },
43 'message.obs.likeCount': (id) => {
44 if (!ref.isLink(id)) throw new Error('an id must be specified')
45 return MutantPullValue(() => {
46 return api.sbot.pull.stream((sbot) => sbot.patchwork.likes.countStream({ dest: id }))
47 }, { defaultValue: 0 })
48 }
49 })
50}
51

Built with git-ssb-web