Files: 302ee34806865960fcbafa16cb1419c070150269 / modules_extra / channel.js
3739 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 | suggest_channel: true |
21 | } |
22 | |
23 | exports.create = function (api) { |
24 | |
25 | var channels |
26 | |
27 | var filter = {$filter: {value: {content: {channel: {$gt: ''}}}}} |
28 | var map = {$map: {'name': ['value', 'content', 'channel']}} |
29 | var reduce = {$reduce: { |
30 | name: 'name', |
31 | rank: {$count: true} |
32 | }} |
33 | |
34 | return { |
35 | message_meta, |
36 | screen_view, |
37 | connection_status, |
38 | suggest_search, |
39 | suggest_mentions, |
40 | suggest_channel |
41 | } |
42 | |
43 | function message_meta (msg) { |
44 | var chan = msg.value.content.channel |
45 | if (chan) |
46 | return h('a', {href: '##'+chan}, '#'+chan) |
47 | } |
48 | |
49 | function screen_view (path) { |
50 | if(path[0] === '#') { |
51 | var channel = path.substr(1) |
52 | |
53 | var composer = api.message_compose({type: 'post', channel: channel}) |
54 | var { container, content } = api.build_scroller({ prepend: composer }) |
55 | |
56 | function matchesChannel(msg) { |
57 | if (msg.sync) console.error('SYNC', msg) |
58 | var c = msg && msg.value && msg.value.content |
59 | return c && c.channel === channel |
60 | } |
61 | |
62 | pull( |
63 | api.sbot_log({old: false}), |
64 | pull.filter(matchesChannel), |
65 | Scroller(container, content, api.message_render, true, false) |
66 | ) |
67 | |
68 | pull( |
69 | api.sbot_query({reverse: true, query: [ |
70 | {$filter: {value: {content: {channel: channel}}}} |
71 | ]}), |
72 | Scroller(container, content, api.message_render, false, false) |
73 | ) |
74 | |
75 | return container |
76 | } |
77 | } |
78 | |
79 | function connection_status (err) { |
80 | if(err) return |
81 | |
82 | channels = [] |
83 | |
84 | pull( |
85 | api.sbot_query({query: [filter, map, reduce]}), |
86 | pull.collect(function (err, chans) { |
87 | if (err) return console.error(err) |
88 | channels = chans.concat(channels) |
89 | }) |
90 | ) |
91 | |
92 | pull( |
93 | api.sbot_log({old: false}), |
94 | mfr.filter(filter), |
95 | mfr.map(map), |
96 | pull.drain(function (chan) { |
97 | var c = channels.find(function (e) { |
98 | return e.name === chan.name |
99 | }) |
100 | if (c) c.rank++ |
101 | else channels.push(chan) |
102 | }) |
103 | ) |
104 | } |
105 | |
106 | function suggest_search (query) { |
107 | return function (cb) { |
108 | if(!/^#\w/.test(query)) return cb() |
109 | |
110 | cb(null, channels.filter(function (chan) { |
111 | return ('#'+chan.name).substring(0, query.length) === query |
112 | }) |
113 | .map(function (chan) { |
114 | var name = '#'+chan.name |
115 | return { |
116 | title: name, |
117 | subtitle: '(' + chan.rank + ')', |
118 | value: name |
119 | } |
120 | })) |
121 | } |
122 | } |
123 | |
124 | function suggest_mentions (query) { |
125 | return function (cb) { |
126 | if(!/^#\w/.test(query)) return cb() |
127 | |
128 | cb(null, channels.filter(function (chan) { |
129 | return ('#'+chan.name).substring(0, query.length) === query |
130 | }) |
131 | .map(function (chan) { |
132 | var name = '#'+chan.name |
133 | return { |
134 | title: name, |
135 | subtitle: '(' + chan.rank + ')', |
136 | value: '['+name+']('+name+')' |
137 | } |
138 | })) |
139 | } |
140 | } |
141 | |
142 | function suggest_channel (query) { |
143 | return function (cb) { |
144 | if (!/^#\w/.test(query)) return cb() |
145 | |
146 | cb(null, channels.filter(function (chan) { |
147 | return (`#${chan.name}`).substring(0, query.length) === query |
148 | }) |
149 | .map(function (chan) { |
150 | var name = `#${chan.name}` |
151 | return { |
152 | title: name, |
153 | subtitle: `(${chan.rank})`, |
154 | value: name.substr(1) |
155 | } |
156 | })) |
157 | } |
158 | } |
159 | } |
160 |
Built with git-ssb-web