Files: f68b2a486afb54608328425f7f0315dfaa7d84e5 / modules_extra / channel.js
3253 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var Scroller = require('pull-scroll') |
4 | var mfr = require('map-filter-reduce') |
5 | |
6 | exports.needs = { |
7 | build_scroller: 'first', |
8 | message_render: 'first', |
9 | message_compose: 'first', |
10 | sbot_log: 'first', |
11 | sbot_query: 'first', |
12 | } |
13 | |
14 | exports.gives = { |
15 | message_meta: true, |
16 | screen_view: true, |
17 | connection_status: true, |
18 | suggest_search: true, |
19 | suggest_mentions: true |
20 | } |
21 | |
22 | exports.create = function (api) { |
23 | |
24 | var channels |
25 | |
26 | var filter = {$filter: {value: {content: {channel: {$gt: ''}}}}} |
27 | var map = {$map: {'name': ['value', 'content', 'channel']}} |
28 | var reduce = {$reduce: { |
29 | name: 'name', |
30 | rank: {$count: true} |
31 | }} |
32 | |
33 | return { |
34 | message_meta, |
35 | screen_view, |
36 | connection_status, |
37 | suggest_search, |
38 | suggest_mentions |
39 | } |
40 | |
41 | function message_meta (msg) { |
42 | var chan = msg.value.content.channel |
43 | if (chan) |
44 | return h('a', {href: '##'+chan}, '#'+chan) |
45 | } |
46 | |
47 | function screen_view (path) { |
48 | if(path[0] === '#') { |
49 | var channel = path.substr(1) |
50 | |
51 | var composer = api.message_compose({type: 'post', channel: channel}) |
52 | var { container, content } = api.build_scroller({ prepend: composer }) |
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 | pull( |
61 | api.sbot_log({old: false}), |
62 | pull.filter(matchesChannel), |
63 | Scroller(container, content, api.message_render, true, false) |
64 | ) |
65 | |
66 | pull( |
67 | api.sbot_query({reverse: true, query: [ |
68 | {$filter: {value: {content: {channel: channel}}}} |
69 | ]}), |
70 | Scroller(container, content, api.message_render, false, false) |
71 | ) |
72 | |
73 | return container |
74 | } |
75 | } |
76 | |
77 | function connection_status (err) { |
78 | if(err) return |
79 | |
80 | channels = [] |
81 | |
82 | pull( |
83 | api.sbot_query({query: [filter, map, reduce]}), |
84 | pull.collect(function (err, chans) { |
85 | if (err) return console.error(err) |
86 | channels = chans.concat(channels) |
87 | }) |
88 | ) |
89 | |
90 | pull( |
91 | api.sbot_log({old: false}), |
92 | mfr.filter(filter), |
93 | mfr.map(map), |
94 | pull.drain(function (chan) { |
95 | var c = channels.find(function (e) { |
96 | return e.name === chan.name |
97 | }) |
98 | if (c) c.rank++ |
99 | else channels.push(chan) |
100 | }) |
101 | ) |
102 | } |
103 | |
104 | function suggest_search (query) { |
105 | return function (cb) { |
106 | if(!/^#\w/.test(query)) return cb() |
107 | |
108 | cb(null, channels.filter(function (chan) { |
109 | return ('#'+chan.name).substring(0, query.length) === query |
110 | }) |
111 | .map(function (chan) { |
112 | var name = '#'+chan.name |
113 | return { |
114 | title: name, |
115 | subtitle: '(' + chan.rank + ')', |
116 | value: name |
117 | } |
118 | })) |
119 | } |
120 | } |
121 | |
122 | function suggest_mentions (query) { |
123 | return function (cb) { |
124 | if(!/^#\w/.test(query)) return cb() |
125 | |
126 | cb(null, channels.filter(function (chan) { |
127 | return ('#'+chan.name).substring(0, query.length) === query |
128 | }) |
129 | .map(function (chan) { |
130 | var name = '#'+chan.name |
131 | return { |
132 | title: name, |
133 | subtitle: '(' + chan.rank + ')', |
134 | value: '['+name+']('+name+')' |
135 | } |
136 | })) |
137 | } |
138 | } |
139 | } |
140 | |
141 |
Built with git-ssb-web