git ssb

16+

Dominic / patchbay



Tree: 261718c426b0d5a333dfa4acc00aacf92c5fe394

Files: 261718c426b0d5a333dfa4acc00aacf92c5fe394 / message / html / like.js

1856 bytesRaw
1const { h, computed, map, Value } = require('mutant')
2const nest = require('depnest')
3const Scuttle = require('scuttle-thread')
4const { isLink } = require('ssb-ref')
5
6exports.gives = nest('message.html.like')
7
8exports.needs = nest({
9 'about.obs.name': 'first',
10 'keys.sync.id': 'first',
11 'message.obs.likes': 'first',
12 'sbot.obs.connection': 'first'
13})
14
15exports.create = (api) => {
16 return nest('message.html.like', like)
17
18 function like (msg) {
19 const id = api.keys.sync.id()
20
21 // TODO make this full-async :
22 // - get whether i like this currently
23 // - only update after I click like/ unlike
24
25 if (!isLink(msg.key)) return
26
27 const likes = api.message.obs.likes(msg.key)
28 const iLikeHack = Value()
29 const names = map(likes, id => api.about.obs.name(id))
30 // TODO should really just calculate this on hover ...
31
32 return computed([likes, iLikeHack, names], (likes, iLikeHack, names) => {
33 const iLike = (iLikeHack !== null) ? iLikeHack : likes.includes(id)
34 var count = likes.length
35 if (iLikeHack === true && !likes.includes(id)) count++
36 else if (iLikeHack === false && likes.includes(id)) count--
37
38 return h('MessageLike',
39 {
40 className: iLike ? '-liked' : '',
41 title: names.join('\n'),
42 'ev-click': () => publishLike(msg, !iLike)
43 },
44 [
45 h('span.count', count || ''),
46 h('i.fa', { className: iLike ? 'fa-heart' : 'fa-heart-o' })
47 ]
48 )
49 })
50
51 function publishLike (msg, value = true) {
52 const _val = iLikeHack()
53 iLikeHack.set(value)
54 const scuttle = Scuttle(api.sbot.obs.connection)
55
56 scuttle.like(msg, { value }, (err, data) => {
57 if (err) {
58 iLikeHack.set(_val)
59 console.error(err)
60 return
61 }
62
63 console.log(data)
64 })
65 }
66 }
67}
68

Built with git-ssb-web