Files: 13be5ee9906beb5d8c2a24db46c68ef183c78d3a / modules / channel.js
2224 bytesRaw
1 | var when = require('@mmckegg/mutant/when') |
2 | var send = require('@mmckegg/mutant/send') |
3 | var plugs = require('patchbay/plugs') |
4 | var extend = require('xtend') |
5 | var message_compose = plugs.first(exports.message_compose = []) |
6 | var sbot_log = plugs.first(exports.sbot_log = []) |
7 | var feed_summary = plugs.first(exports.feed_summary = []) |
8 | var h = require('../lib/h') |
9 | var pull = require('pull-stream') |
10 | var sbot_query = plugs.first(exports.sbot_query = []) |
11 | var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = []) |
12 | var get_id = plugs.first(exports.get_id = []) |
13 | var publish = plugs.first(exports.sbot_publish = []) |
14 | |
15 | exports.screen_view = function (path, sbot) { |
16 | if (path[0] === '#') { |
17 | var channel = path.substr(1) |
18 | var subscribedChannels = obs_subscribed_channels(get_id()) |
19 | |
20 | return feed_summary((opts) => { |
21 | if (opts.old === false) { |
22 | return pull( |
23 | sbot_log(opts), |
24 | pull.filter(matchesChannel) |
25 | ) |
26 | } else { |
27 | return sbot_query(extend(opts, {query: [ |
28 | {$filter: {value: {content: {channel: channel}}}} |
29 | ]})) |
30 | } |
31 | }, [ |
32 | h('PageHeading', [ |
33 | h('h1', `#${channel}`), |
34 | h('div.meta', [ |
35 | when(subscribedChannels.has(channel), |
36 | h('a -unsubscribe', { |
37 | 'href': '#', |
38 | 'title': 'Click to unsubscribe', |
39 | 'ev-click': send(unsubscribe, channel) |
40 | }, 'Subscribed'), |
41 | h('a -subscribe', { |
42 | 'href': '#', |
43 | 'ev-click': send(subscribe, channel) |
44 | }, 'Subscribe') |
45 | ) |
46 | ]) |
47 | ]), |
48 | message_compose({type: 'post', channel: channel}) |
49 | ]) |
50 | } |
51 | |
52 | // scoped |
53 | |
54 | function matchesChannel (msg) { |
55 | if (msg.sync) console.error('SYNC', msg) |
56 | var c = msg && msg.value && msg.value.content |
57 | return c && c.channel === channel |
58 | } |
59 | } |
60 | |
61 | exports.message_meta = function (msg) { |
62 | var chan = msg.value.content.channel |
63 | if (chan) { |
64 | return h('a.channel', {href: '##' + chan}, '#' + chan) |
65 | } |
66 | } |
67 | |
68 | function subscribe (id) { |
69 | publish({ |
70 | type: 'channel', |
71 | channel: id, |
72 | subscribed: true |
73 | }) |
74 | } |
75 | |
76 | function unsubscribe (id) { |
77 | publish({ |
78 | type: 'channel', |
79 | channel: id, |
80 | subscribed: false |
81 | }) |
82 | } |
83 |
Built with git-ssb-web