git ssb

1+

Daan Patchwork / patchwork



Tree: 2aba282967b6a030aeb637a4592a0572ac1451b7

Files: 2aba282967b6a030aeb637a4592a0572ac1451b7 / lib / depject / message / html / metas.js

2636 bytesRaw
1const nest = require('depnest')
2const { h, computed, map, send, when, onceTrue } = require('mutant')
3const TagHelper = require('scuttle-tag')
4const msgs = require('ssb-msgs')
5
6exports.gives = nest('message.html.metas')
7exports.needs = nest({
8 'message.obs.likeCount': 'first',
9 'sheet.profiles': 'first',
10 'about.obs.name': 'first',
11 'intl.sync.i18n': 'first',
12 'intl.sync.i18n_n': 'first',
13 'sbot.obs.connection': 'first',
14 'sheet.tags.render': 'first',
15 'about.html.image': 'first'
16})
17
18exports.create = function (api) {
19 const i18n = api.intl.sync.i18n
20 const plural = api.intl.sync.i18n_n
21
22 return nest('message.html.metas', function likes (msg) {
23 const ScuttleTag = TagHelper(api.sbot.obs.connection)
24
25 const result = []
26 if (msg.value.private) {
27 result.push(h('span.private', map(msg.value.content.recps, id => {
28 const feed = msgs.link(id, 'feed')
29 if (!feed) return
30 id = feed.link
31 return h('a', {
32 href: id,
33 title: api.about.obs.name(id)
34 }, [
35 api.about.html.image(id)
36 ])
37 })))
38 }
39
40 if (msg.value.content && msg.value.content.channel && msg.value.content.type !== 'channel') {
41 result.push(h('a.channel', { href: `#${msg.value.content.channel}` }, [`#${msg.value.content.channel}`]))
42 }
43
44 if (msg.key) {
45 const likeCount = api.message.obs.likeCount(msg.key)
46 result.push(
47 h('div.counts', [
48 when(likeCount,
49 h('a.likes', {
50 href: '#', 'ev-click': send(displayLikes, msg)
51 }, computed(['%s likes', likeCount], plural))
52 ),
53 computed(ScuttleTag.obs.messageTags(msg.key), (tags) => tagCount(msg.key, tags))
54 ])
55 )
56 }
57
58 return result
59 })
60
61 function displayLikes (msg) {
62 onceTrue(api.sbot.obs.connection, (sbot) => {
63 sbot.patchwork.likes.get({ dest: msg.key }, (err, likes) => {
64 if (err) return console.log(err)
65 api.sheet.profiles(likes, i18n('Liked by'))
66 })
67 })
68 }
69
70 function tagCount (msgId, tags) {
71 if (tags.length) {
72 return [' ', h('a.tags', {
73 title: tagList('Tags', tags),
74 href: '#',
75 'ev-click': send(displayTags, { msgId, tags })
76 }, [`${tags.length} ${tags.length === 1 ? 'tag' : 'tags'}`])]
77 }
78 }
79
80 function tagList (prefix, ids) {
81 const items = map(ids, api.about.obs.name)
82 return computed([prefix, items], (prefix, names) => {
83 return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n')
84 })
85 }
86
87 function displayTags ({ msgId, tags }) {
88 api.sheet.tags.render(msgId, tags)
89 }
90}
91

Built with git-ssb-web