Files: c70f4698cfddd30c1bba8c6210958a4614e17e9c / modules / like.js
2031 bytesRaw
1 | var h = require('../lib/h') |
2 | var computed = require('@mmckegg/mutant/computed') |
3 | var when = require('@mmckegg/mutant/when') |
4 | var plugs = require('patchbay/plugs') |
5 | var message_link = plugs.first(exports.message_link = []) |
6 | var get_id = plugs.first(exports.get_id = []) |
7 | var get_likes = plugs.first(exports.get_likes = []) |
8 | var publish = plugs.first(exports.sbot_publish = []) |
9 | var people_names = plugs.first(exports.people_names = []) |
10 | |
11 | exports.message_content = exports.message_content_mini = function (msg, sbot) { |
12 | if (msg.value.content.type !== 'vote') return |
13 | var link = msg.value.content.vote.link |
14 | return [ |
15 | msg.value.content.vote.value > 0 ? 'dug' : 'undug', |
16 | ' ', message_link(link) |
17 | ] |
18 | } |
19 | |
20 | exports.message_meta = function (msg, sbot) { |
21 | return computed(get_likes(msg.key), likeCount) |
22 | } |
23 | |
24 | exports.message_action = function (msg, sbot) { |
25 | var id = get_id() |
26 | var dug = computed([get_likes(msg.key), id], doesLike) |
27 | dug(() => {}) |
28 | |
29 | if (msg.value.content.type !== 'vote') { |
30 | return h('a.dig', { |
31 | href: '#', |
32 | 'ev-click': function () { |
33 | var dig = dug() ? { |
34 | type: 'vote', |
35 | vote: { link: msg.key, value: 0, expression: 'Undig' } |
36 | } : { |
37 | type: 'vote', |
38 | vote: { link: msg.key, value: 1, expression: 'Dig' } |
39 | } |
40 | if (msg.value.content.recps) { |
41 | dig.recps = msg.value.content.recps.map(function (e) { |
42 | return e && typeof e !== 'string' ? e.link : e |
43 | }) |
44 | dig.private = true |
45 | } |
46 | publish(dig) |
47 | } |
48 | }, when(dug, 'Undig', 'Dig')) |
49 | } |
50 | } |
51 | |
52 | function doesLike (likes, userId) { |
53 | return likes && likes[userId] && likes[userId][0] || false |
54 | } |
55 | |
56 | function likeCount (data) { |
57 | var likes = getLikes(data) |
58 | if (likes.length) { |
59 | return [' ', h('span.likes', { |
60 | title: people_names(likes) |
61 | }, ['+', h('strong', `${likes.length}`)])] |
62 | } |
63 | } |
64 | |
65 | function getLikes (likes) { |
66 | return Object.keys(likes).reduce((result, id) => { |
67 | if (likes[id][0]) { |
68 | result.push(id) |
69 | } |
70 | return result |
71 | }, []) |
72 | } |
73 |
Built with git-ssb-web