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