git ssb

1+

Daan Patchwork / patchwork



Tree: 3451510316992d414ec76ba5b29681fe359b7428

Files: 3451510316992d414ec76ba5b29681fe359b7428 / lib / depject / message / html / actions.js

2321 bytesRaw
1const { h, when } = require('mutant')
2const nest = require('depnest')
3const getRoot = require('../../../message/sync/root')
4
5exports.needs = nest({
6 'intl.sync.i18n': 'first',
7 'app.navigate': 'first',
8 'message.obs.doesLike': 'first',
9 'sbot.async.publish': 'first',
10 'sheet.editTags': 'first'
11})
12
13exports.gives = nest('message.html.actions')
14
15exports.create = (api) => {
16 const i18n = api.intl.sync.i18n
17
18 return nest('message.html.actions', function like (msg) {
19 const liked = api.message.obs.doesLike(msg.key)
20
21 return [
22 when(liked,
23 h('a.like -liked', {
24 href: '#',
25 title: i18n('Click to unlike'),
26 'ev-click': () => publishLike(msg, false)
27 }, i18n('Liked')),
28 h('a.like', {
29 href: '#',
30 'ev-click': () => publishLike(msg, true)
31 }, i18n('Like'))
32 ),
33 h('a.reply', {
34 href: msg.key,
35 anchor: 'reply',
36 'ev-click': { handleEvent, api, msg }
37 }, i18n('Reply')),
38 h('a.tag -right', {
39 href: '#',
40 title: i18n('Add / Edit Tags'),
41 'ev-click': () => api.sheet.editTags({ msgId: msg.key }, console.log)
42 }, i18n('Tags'))
43 ]
44 })
45
46 function publishLike (msg, status = true) {
47 const like = status
48 ? {
49 type: 'vote',
50 channel: msg.value.content.channel,
51 vote: { link: msg.key, value: 1, expression: 'Like' }
52 }
53 : {
54 type: 'vote',
55 channel: msg.value.content.channel,
56 vote: { link: msg.key, value: 0, expression: 'Unlike' }
57 }
58 if (msg.value.content.recps) {
59 like.recps = msg.value.content.recps.map(function (e) {
60 return e && typeof e !== 'string' ? e.link : e
61 })
62 like.private = true
63 }
64 api.sbot.async.publish(like)
65 }
66}
67
68function handleEvent (ev) {
69 const { api, msg } = this
70 const el = getMessageElement(ev.target)
71
72 // HACK: if this is the last message in the list, reply to the root message
73 if (el && !el.nextElementSibling) {
74 api.app.navigate(getRoot(msg), 'reply')
75 ev.preventDefault()
76 }
77}
78
79function getMessageElement (el) {
80 while (el && el.classList) {
81 if (el.classList.contains('Message') && el.parentNode && el.parentNode.classList.contains('replies')) {
82 return el
83 }
84 el = el.parentNode
85 }
86}
87

Built with git-ssb-web