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