Files: f282bf4a9ac0e60ba0de219dad5704df86000ec5 / message / html / like.js
1316 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.like') |
11 | |
12 | exports.create = (api) => { |
13 | return nest('message.html.like', function like (msg) { |
14 | var id = api.keys.sync.id() |
15 | var liked = computed([api.message.obs.likes(msg.key), id], doesLike) |
16 | return when(liked, |
17 | h('a.unlike', { |
18 | href: '#', |
19 | 'ev-click': () => publishLike(msg, false) |
20 | }, 'Unlike'), |
21 | h('a.like', { |
22 | href: '#', |
23 | 'ev-click': () => publishLike(msg, true) |
24 | }, 'Like') |
25 | ) |
26 | }) |
27 | |
28 | function publishLike (msg, status = true) { |
29 | var like = status ? { |
30 | type: 'vote', |
31 | channel: msg.value.content.channel, |
32 | vote: { link: msg.key, value: 1, expression: 'Like' } |
33 | } : { |
34 | type: 'vote', |
35 | channel: msg.value.content.channel, |
36 | vote: { link: msg.key, value: 0, expression: 'Unlike' } |
37 | } |
38 | if (msg.value.content.recps) { |
39 | like.recps = msg.value.content.recps.map(function (e) { |
40 | return e && typeof e !== 'string' ? e.link : e |
41 | }) |
42 | like.private = true |
43 | } |
44 | api.sbot.async.publish(like) |
45 | } |
46 | } |
47 | |
48 | function doesLike (likes, userId) { |
49 | return likes.includes(userId) |
50 | } |
51 |
Built with git-ssb-web