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