git ssb

10+

Matt McKegg / patchwork



Tree: 0a238b239741686dff3e8eec203f8d00f0a42486

Files: 0a238b239741686dff3e8eec203f8d00f0a42486 / modules / like.js

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

Built with git-ssb-web