Files: b700e0ddf06112a4a31a142a308c150ed1ec1337 / message / html / likes.js
1293 bytesRaw
1 | var { h, computed, when } = require('mutant') |
2 | var nest = require('depnest') |
3 | |
4 | exports.needs = nest({ |
5 | 'keys.sync.id': 'first', |
6 | 'message.obs.likes': 'first', |
7 | 'sbot.async.publish': 'first' |
8 | }) |
9 | |
10 | exports.gives = nest('message.html.likes') |
11 | |
12 | exports.create = (api) => { |
13 | return nest('message.html.likes', function likes (msg) { |
14 | var id = api.keys.sync.id() |
15 | var likes = api.message.obs.likes(msg.key) |
16 | var iLike = computed(likes, likes => likes.includes(id)) |
17 | var count = computed(likes, likes => likes.length ? likes.length : '') |
18 | |
19 | return h('Likes', {'ev-click': () => publishLike(msg, !iLike())}, [ |
20 | h('i.fa', { className: when(iLike, 'fa-heart', 'fa-heart-o') }), |
21 | h('div.count', count) |
22 | ]) |
23 | }) |
24 | |
25 | function publishLike (msg, status = true) { |
26 | var like = status ? { |
27 | type: 'vote', |
28 | channel: msg.value.content.channel, |
29 | vote: { link: msg.key, value: 1, expression: 'Like' } |
30 | } : { |
31 | type: 'vote', |
32 | channel: msg.value.content.channel, |
33 | vote: { link: msg.key, value: 0, expression: 'Unlike' } |
34 | } |
35 | if (msg.value.content.recps) { |
36 | like.recps = msg.value.content.recps.map(function (e) { |
37 | return e && typeof e !== 'string' ? e.link : e |
38 | }) |
39 | like.private = true |
40 | } |
41 | api.sbot.async.publish(like) |
42 | } |
43 | } |
44 | |
45 |
Built with git-ssb-web