Files: 2f8b5a42a327baeb86c4cc6af56c65848948d0ec / message / html / likes.js
1296 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 | |
17 | var iLike = computed(likes, likes => likes.includes(id)) |
18 | var count = computed(likes, likes => likes.length ? likes.length : '') |
19 | |
20 | return h('Likes', { 'ev-click': () => publishLike(msg, !iLike()) }, [ |
21 | h('i.fa', { className: when(iLike, 'fa-heart', 'fa-heart-o') }), |
22 | h('div.count', count) |
23 | ]) |
24 | }) |
25 | |
26 | function publishLike (msg, status = true) { |
27 | var like = status ? { |
28 | type: 'vote', |
29 | channel: msg.value.content.channel, |
30 | vote: { link: msg.key, value: 1, expression: 'Like' } |
31 | } : { |
32 | type: 'vote', |
33 | channel: msg.value.content.channel, |
34 | vote: { link: msg.key, value: 0, expression: 'Unlike' } |
35 | } |
36 | if (msg.value.content.recps) { |
37 | like.recps = msg.value.content.recps.map(function (e) { |
38 | return e && typeof e !== 'string' ? e.link : e |
39 | }) |
40 | like.private = true |
41 | } |
42 | api.sbot.async.publish(like) |
43 | } |
44 | } |
45 | |
46 |
Built with git-ssb-web