Files: 02eb138217bf2dd28c3be988febc79c8d62e190b / modules / page / html / render / channel.js
2550 bytesRaw
1 | var { h, when, send } = require('mutant') |
2 | var nest = require('depnest') |
3 | |
4 | exports.needs = nest({ |
5 | 'channel.obs.subscribed': 'first', |
6 | 'message.html.compose': 'first', |
7 | 'channel.sync.normalize': 'first', |
8 | 'feed.html.rollup': 'first', |
9 | 'feed.pull.channel': 'first', |
10 | 'sbot.pull.log': 'first', |
11 | 'message.async.publish': 'first', |
12 | 'keys.sync.id': 'first', |
13 | 'intl.sync.i18n': 'first' |
14 | }) |
15 | |
16 | exports.gives = nest('page.html.render') |
17 | |
18 | exports.create = function (api) { |
19 | const i18n = api.intl.sync.i18n |
20 | return nest('page.html.render', function channel (path) { |
21 | if (path[0] !== '#') return |
22 | |
23 | var channel = api.channel.sync.normalize(path.substr(1)) |
24 | var subscribedChannels = api.channel.obs.subscribed(api.keys.sync.id()) |
25 | |
26 | var prepend = [ |
27 | h('PageHeading', [ |
28 | h('h1', `#${channel}`), |
29 | h('div.meta', [ |
30 | when(subscribedChannels.has(channel), |
31 | h('a.ToggleButton.-unsubscribe', { |
32 | 'href': '#', |
33 | 'title': i18n('Click to unsubscribe'), |
34 | 'ev-click': send(unsubscribe, channel) |
35 | }, i18n('Subscribed')), |
36 | h('a.ToggleButton.-subscribe', { |
37 | 'href': '#', |
38 | 'ev-click': send(subscribe, channel) |
39 | }, i18n('Subscribe')) |
40 | ) |
41 | ]) |
42 | ]), |
43 | api.message.html.compose({ |
44 | meta: {type: 'post', channel}, |
45 | placeholder: i18n('Write a message in this channel') |
46 | }) |
47 | ] |
48 | |
49 | return api.feed.html.rollup(api.feed.pull.channel(channel), { |
50 | prepend, |
51 | displayFilter: mentionFilter, |
52 | bumpFilter: mentionFilter |
53 | }) |
54 | |
55 | function mentionFilter (msg) { |
56 | // filter out likes |
57 | if (msg.value.content.type === 'vote') return false |
58 | if (api.channel.sync.normalize(msg.value.content.channel) === channel) return true |
59 | if (Array.isArray(msg.value.content.mentions)) { |
60 | if (msg.value.content.mentions.some(mention => { |
61 | return mention && getNormalized(mention.link) === `#${channel}` |
62 | })) { |
63 | return 'channel-mention' |
64 | } |
65 | } |
66 | } |
67 | |
68 | function getNormalized (link) { |
69 | if (typeof link === 'string' && link.startsWith('#')) { |
70 | return `#${api.channel.sync.normalize(link.slice(1))}` |
71 | } |
72 | } |
73 | }) |
74 | |
75 | function subscribe (id) { |
76 | // confirm |
77 | api.message.async.publish({ |
78 | type: 'channel', |
79 | channel: id, |
80 | subscribed: true |
81 | }) |
82 | } |
83 | |
84 | function unsubscribe (id) { |
85 | // confirm |
86 | api.message.async.publish({ |
87 | type: 'channel', |
88 | channel: id, |
89 | subscribed: false |
90 | }) |
91 | } |
92 | } |
93 |
Built with git-ssb-web