Files: f715d01c1a678fca975815e634302f528078efbf / modules_basic / like.js
2053 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | |
4 | exports.needs = { |
5 | avatar_name: 'first', |
6 | message_confirm: 'first', |
7 | message_link: 'first', |
8 | sbot_links: 'first' |
9 | } |
10 | |
11 | exports.gives = { |
12 | message_content: true, |
13 | message_content_mini: true, |
14 | message_meta: true, |
15 | message_action: true |
16 | } |
17 | |
18 | exports.create = function (api) { |
19 | var exports = {} |
20 | |
21 | exports.message_content = |
22 | exports.message_content_mini = function (msg, sbot) { |
23 | if(msg.value.content.type !== 'vote') return |
24 | var link = msg.value.content.vote.link |
25 | return [ |
26 | msg.value.content.vote.value > 0 ? 'dug' : 'undug', |
27 | ' ', api.message_link(link) |
28 | ] |
29 | } |
30 | |
31 | exports.message_meta = function (msg, sbot) { |
32 | var digs = h('a') |
33 | |
34 | var votes = [] |
35 | for(var k in CACHE) { |
36 | if(CACHE[k].content.type == 'vote' && |
37 | (CACHE[k].content.vote == msg.key || |
38 | CACHE[k].content.vote.link == msg.key |
39 | )) |
40 | votes.push({source: CACHE[k].author, dest: k, rel: 'vote'}) |
41 | } |
42 | |
43 | if (votes.length === 0) return |
44 | |
45 | const symbol = '\u2713' // tick 🗸 |
46 | |
47 | digs.textContent = votes.length > 4 |
48 | ? votes.length + ' ' + symbol |
49 | : Array(votes.length).fill(symbol).join('') |
50 | |
51 | pull( |
52 | pull.values(votes.map(vote => api.avatar_name(vote.source))), |
53 | pull.collect((err, ary) => { |
54 | if (err) console.error(err) |
55 | digs.title = 'Dug by:\n' + ary.map(x => x.innerHTML).join("\n") |
56 | }) |
57 | ) |
58 | |
59 | return digs |
60 | } |
61 | |
62 | exports.message_action = function (msg, sbot) { |
63 | if(msg.value.content.type !== 'vote') |
64 | return h('a.dig', {href: '#', onclick: function () { |
65 | var dig = { |
66 | type: 'vote', |
67 | vote: { link: msg.key, value: 1, expression: 'Dig' } |
68 | } |
69 | if(msg.value.content.recps) { |
70 | dig.recps = msg.value.content.recps.map(function (e) { |
71 | return e && typeof e !== 'string' ? e.link : e |
72 | }) |
73 | dig.private = true |
74 | } |
75 | //TODO: actually publish... |
76 | |
77 | api.message_confirm(dig) |
78 | }}, 'Dig') |
79 | |
80 | } |
81 | return exports |
82 | } |
83 |
Built with git-ssb-web