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