git ssb

16+

Dominic / patchbay



Tree: 07678ab95020edf09925a491966765d79e086d0e

Files: 07678ab95020edf09925a491966765d79e086d0e / modules_extra / channel.js

3300 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var Scroller = require('pull-scroll')
4var mfr = require('map-filter-reduce')
5
6exports.needs = {
7 build_scroller: 'first',
8 message_render: 'first',
9 message_compose: 'first',
10 sbot_log: 'first',
11 sbot_query: 'first',
12}
13
14exports.gives = {
15 message_meta: true,
16 screen_view: true,
17 connection_status: true,
18 suggest_search: true,
19 suggest_mentions: true
20}
21
22exports.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', {
45 href: '##'+chan,
46 style: { order: 98 },
47 }, '#'+chan)
48 }
49
50 function screen_view (path) {
51 if(path[0] === '#') {
52 var channel = path.substr(1)
53
54 var composer = api.message_compose({type: 'post', channel: channel})
55 var { container, content } = api.build_scroller({ prepend: composer })
56
57 function matchesChannel(msg) {
58 if (msg.sync) console.error('SYNC', msg)
59 var c = msg && msg.value && msg.value.content
60 return c && c.channel === channel
61 }
62
63 pull(
64 api.sbot_log({old: false}),
65 pull.filter(matchesChannel),
66 Scroller(container, content, api.message_render, true, false)
67 )
68
69 pull(
70 api.sbot_query({reverse: true, query: [
71 {$filter: {value: {content: {channel: channel}}}}
72 ]}),
73 Scroller(container, content, api.message_render, false, false)
74 )
75
76 return container
77 }
78 }
79
80 function connection_status (err) {
81 if(err) return
82
83 channels = []
84
85 pull(
86 api.sbot_query({query: [filter, map, reduce]}),
87 pull.collect(function (err, chans) {
88 if (err) return console.error(err)
89 channels = chans.concat(channels)
90 })
91 )
92
93 pull(
94 api.sbot_log({old: false}),
95 mfr.filter(filter),
96 mfr.map(map),
97 pull.drain(function (chan) {
98 var c = channels.find(function (e) {
99 return e.name === chan.name
100 })
101 if (c) c.rank++
102 else channels.push(chan)
103 })
104 )
105 }
106
107 function suggest_search (query) {
108 return function (cb) {
109 if(!/^#\w/.test(query)) return cb()
110
111 cb(null, channels.filter(function (chan) {
112 return ('#'+chan.name).substring(0, query.length) === query
113 })
114 .map(function (chan) {
115 var name = '#'+chan.name
116 return {
117 title: name,
118 subtitle: '(' + chan.rank + ')',
119 value: name
120 }
121 }))
122 }
123 }
124
125 function suggest_mentions (query) {
126 return function (cb) {
127 if(!/^#\w/.test(query)) return cb()
128
129 cb(null, channels.filter(function (chan) {
130 return ('#'+chan.name).substring(0, query.length) === query
131 })
132 .map(function (chan) {
133 var name = '#'+chan.name
134 return {
135 title: name,
136 subtitle: '(' + chan.rank + ')',
137 value: '['+name+']('+name+')'
138 }
139 }))
140 }
141 }
142}
143
144

Built with git-ssb-web