Files: 9429dd07ea740e2d002c966affa5cdaccc0d73f3 / modules_basic / like.js
2009 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 | digs.textContent = votes.length > 4 |
46 | ? votes.length + ' \u2713' |
47 | : Array(votes.length).fill('\u2713').join('') |
48 | |
49 | pull( |
50 | pull.values(votes.map(vote => api.avatar_name(vote.source))), |
51 | pull.collect((err, ary) => { |
52 | if (err) console.error(err) |
53 | digs.title = 'Dug by:\n' + ary.map(x => x.innerHTML).join("\n") |
54 | }) |
55 | ) |
56 | |
57 | return digs |
58 | } |
59 | |
60 | exports.message_action = function (msg, sbot) { |
61 | if(msg.value.content.type !== 'vote') |
62 | return h('a.dig', {href: '#', onclick: function () { |
63 | var dig = { |
64 | type: 'vote', |
65 | vote: { link: msg.key, value: 1, expression: 'Dig' } |
66 | } |
67 | if(msg.value.content.recps) { |
68 | dig.recps = msg.value.content.recps.map(function (e) { |
69 | return e && typeof e !== 'string' ? e.link : e |
70 | }) |
71 | dig.private = true |
72 | } |
73 | //TODO: actually publish... |
74 | |
75 | api.message_confirm(dig) |
76 | }}, 'Dig') |
77 | |
78 | } |
79 | return exports |
80 | } |
81 |
Built with git-ssb-web