git ssb

16+

Dominic / patchbay



Tree: 697e3e1780dfe42cdc94d4e26e2788489ddfb896

Files: 697e3e1780dfe42cdc94d4e26e2788489ddfb896 / modules_extra / channel.js

2540 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var pull = require('pull-stream')
4var Scroller = require('pull-scroll')
5
6var plugs = require('../plugs')
7var message_render = plugs.first(exports.message_render = [])
8var message_compose = plugs.first(exports.message_compose = [])
9var sbot_log = plugs.first(exports.sbot_log = [])
10var sbot_query = plugs.first(exports.sbot_query = [])
11var mfr = require('map-filter-reduce')
12
13exports.message_meta = function (msg) {
14 var chan = msg.value.content.channel
15 if (chan)
16 return h('a', {href: '##'+chan}, '#'+chan)
17}
18
19exports.screen_view = function (path) {
20 if(path[0] === '#') {
21 var channel = path.substr(1)
22
23 var content = h('div.column.scroller__content')
24 var div = h('div.column.scroller',
25 {style: {'overflow':'auto'}},
26 h('div.scroller__wrapper',
27 message_compose({type: 'post', channel: channel}),
28 content
29 )
30 )
31
32 function matchesChannel(msg) {
33 if (msg.sync) console.error('SYNC', msg)
34 var c = msg && msg.value && msg.value.content
35 return c && c.channel === channel
36 }
37
38 pull(
39 sbot_log({old: false}),
40 pull.filter(matchesChannel),
41 Scroller(div, content, message_render, true, false)
42 )
43
44 pull(
45 sbot_query({reverse: true, query: [
46 {$filter: {value: {content: {channel: channel}}}}
47 ]}),
48 Scroller(div, content, message_render, false, false)
49 )
50
51 return div
52 }
53}
54
55var channels
56
57var filter = {$filter: {value: {content: {channel: {$gt: ''}}}}}
58var map = {$map: {'name': ['value', 'content', 'channel']}}
59var reduce = {$reduce: {
60 name: 'name',
61 rank: {$count: true}
62}}
63
64exports.connection_status = function (err) {
65 if(err) return
66
67 channels = []
68
69 pull(
70 sbot_query({query: [filter, map, reduce]}),
71 pull.collect(function (err, chans) {
72 if (err) return console.error(err)
73 channels = chans.concat(channels)
74 })
75 )
76
77 pull(
78 sbot_log({old: false}),
79 mfr.filter(filter),
80 mfr.map(map),
81 pull.drain(function (chan) {
82 var c = channels.find(function (e) {
83 return e.name === chan.name
84 })
85 if (c) c.rank++
86 else channels.push(chan)
87 })
88 )
89}
90
91exports.suggest_search = function (query, cb) {
92 if(!/^#\w/.test(query)) return cb()
93 cb(null, channels.filter(function (chan) {
94 return ('#'+chan.name).substring(0, query.length) === query
95 })
96 .map(function (chan) {
97 var name = '#'+chan.name
98 return {
99 title: name,
100 value: name,
101 subtitle: chan.rank
102 }
103 }))
104}
105

Built with git-ssb-web