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