git ssb

10+

Matt McKegg / patchwork



Tree: 593dfd760dee299082f615e02f2747f1c6f660ec

Files: 593dfd760dee299082f615e02f2747f1c6f660ec / modules / page / html / render / channel.js

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

Built with git-ssb-web