git ssb

10+

Matt McKegg / patchwork



Tree: 13b1df716aa0e61f57f567bce7d3a64e14243907

Files: 13b1df716aa0e61f57f567bce7d3a64e14243907 / modules / page / html / render / channel.js

3201 bytesRaw
1var { h, when, send } = require('mutant')
2var nest = require('depnest')
3
4exports.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 'settings.obs.get': 'first'
15})
16
17exports.gives = nest('page.html.render')
18
19exports.create = function (api) {
20 const i18n = api.intl.sync.i18n
21 return nest('page.html.render', function channel (path) {
22 if (path[0] !== '#') return
23
24 var channel = api.channel.sync.normalize(path.substr(1))
25 var subscribedChannels = api.channel.obs.subscribed(api.keys.sync.id())
26
27 var prepend = [
28 h('PageHeading', [
29 h('h1', `#${channel}`),
30 h('div.meta', [
31 when(subscribedChannels.has(channel),
32 h('a.ToggleButton.-unsubscribe', {
33 'href': '#',
34 'title': i18n('Click to unsubscribe'),
35 'ev-click': send(unsubscribe, channel)
36 }, i18n('Subscribed')),
37 h('a.ToggleButton.-subscribe', {
38 'href': '#',
39 'ev-click': send(subscribe, channel)
40 }, i18n('Subscribe'))
41 )
42 ])
43 ]),
44 api.message.html.compose({
45 meta: {type: 'post', channel},
46 placeholder: i18n('Write a message in this channel')
47 })
48 ]
49
50 const filters = api.settings.obs.get('filters')
51 const channelView = api.feed.html.rollup(
52 api.feed.pull.channel(channel), {
53 prepend,
54 rootFilter: checkFeedFilter,
55 displayFilter: mentionFilter,
56 bumpFilter: mentionFilter
57 })
58
59 // call reload whenever filters changes
60 filters(channelView.reload)
61
62 return channelView
63
64 function checkFeedFilter (msg) {
65 const filterObj = filters() && filters().channelView;
66 if (filterObj) {
67 const msgType = msg && msg.value && msg.value.content &&
68 msg.value.content.type
69 // filter out channel subscription messages
70 if (filterObj.subscriptions && msgType === 'channel')
71 return false
72 }
73 return true
74 }
75
76 function mentionFilter (msg) {
77 // filter out likes
78 if (msg.value.content.type === 'vote') return false
79 if (api.channel.sync.normalize(msg.value.content.channel) === channel) return true
80 if (Array.isArray(msg.value.content.mentions)) {
81 if (msg.value.content.mentions.some(mention => {
82 return mention && getNormalized(mention.link) === `#${channel}`
83 })) {
84 return 'channel-mention'
85 }
86 }
87 }
88
89 function getNormalized (link) {
90 if (typeof link === 'string' && link.startsWith('#')) {
91 return `#${api.channel.sync.normalize(link.slice(1))}`
92 }
93 }
94 })
95
96 function subscribe (id) {
97 // confirm
98 api.message.async.publish({
99 type: 'channel',
100 channel: id,
101 subscribed: true
102 })
103 }
104
105 function unsubscribe (id) {
106 // confirm
107 api.message.async.publish({
108 type: 'channel',
109 channel: id,
110 subscribed: false
111 })
112 }
113}
114

Built with git-ssb-web