git ssb

16+

Dominic / patchbay



Tree: 34a9a046aafdc75d2175d111e348bca2c5364fb7

Files: 34a9a046aafdc75d2175d111e348bca2c5364fb7 / modules_extra / channel.js

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

Built with git-ssb-web