Files: ed00f21688c47a809f1724a50ce7a3239f9929a6 / message / html / like.js
1414 bytesRaw
1 | const { h, computed, map } = require('mutant') |
2 | const nest = require('depnest') |
3 | const Scuttle = require('scuttle-thread') |
4 | const { isLink } = require('ssb-ref') |
5 | |
6 | exports.gives = nest('message.html.like') |
7 | |
8 | exports.needs = nest({ |
9 | 'about.obs.name': 'first', |
10 | 'keys.sync.id': 'first', |
11 | 'message.obs.likes': 'first', |
12 | 'sbot.obs.connection': 'first' |
13 | }) |
14 | |
15 | exports.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 names = map(likes, id => api.about.obs.name(id)) |
29 | // TODO should really just calculate this on hover ... |
30 | |
31 | return computed([likes, names], (likes, names) => { |
32 | const iLike = likes.includes(id) |
33 | |
34 | return h('MessageLike', |
35 | { |
36 | className: iLike ? '-liked' : '', |
37 | title: names.join('\n'), |
38 | 'ev-click': () => publishLike(msg, !iLike) |
39 | }, |
40 | [ |
41 | h('span.count', likes.length ? likes.length : ''), |
42 | h('i.fa', { className: iLike ? 'fa-heart' : 'fa-heart-o' }) |
43 | ] |
44 | ) |
45 | }) |
46 | } |
47 | |
48 | function publishLike (msg, value = true) { |
49 | const scuttle = Scuttle(api.sbot.obs.connection) |
50 | |
51 | scuttle.like(msg, { value }, console.log) |
52 | } |
53 | } |
54 |
Built with git-ssb-web