Files: 3369d94cf2d91a92f0a8fc9fcc8e62b493c92792 / message / html / action / like.js
1412 bytesRaw
1 | var { h, computed, when, send } = 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.action') |
11 | |
12 | exports.create = (api) => { |
13 | return nest('message.html.action', function (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': send(unlike, msg) |
20 | }, 'Unlike'), |
21 | h('a.like', { |
22 | href: '#', |
23 | 'ev-click': send(like, msg) |
24 | }, 'Like') |
25 | ) |
26 | }) |
27 | |
28 | function like (msg) { |
29 | publishLike(msg, true) |
30 | } |
31 | |
32 | function unlike (msg) { |
33 | publishLike(msg, false) |
34 | } |
35 | |
36 | function publishLike (msg, status = true) { |
37 | var like = status ? { |
38 | type: 'vote', |
39 | channel: msg.value.content.channel, |
40 | vote: { link: msg.key, value: 1, expression: 'Like' } |
41 | } : { |
42 | type: 'vote', |
43 | channel: msg.value.content.channel, |
44 | vote: { link: msg.key, value: 0, expression: 'Unlike' } |
45 | } |
46 | if (msg.value.content.recps) { |
47 | like.recps = msg.value.content.recps.map(function (e) { |
48 | return e && typeof e !== 'string' ? e.link : e |
49 | }) |
50 | like.private = true |
51 | } |
52 | api.sbot.async.publish(like) |
53 | } |
54 | } |
55 | |
56 | function doesLike (likes, userId) { |
57 | return likes.includes(userId) |
58 | } |
59 | |
60 |
Built with git-ssb-web