Files: e08d5a8b3a1f4197ef31f35de3e758bb2600f214 / modules / channel / obs / recent.js
1201 bytesRaw
1 | var nest = require('depnest') |
2 | var { Value, Dict, Struct, computed } = require('mutant') |
3 | |
4 | exports.gives = nest({ |
5 | 'sbot.hook.feed': true, |
6 | 'channel.obs.recent': true |
7 | }) |
8 | |
9 | exports.create = function (api) { |
10 | var channelsLookup = Dict() |
11 | |
12 | var recentChannels = computed(channelsLookup, (lookup) => { |
13 | var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.updatedAt - a.updatedAt).map(x => x.id) |
14 | return values |
15 | }, {nextTick: true}) |
16 | |
17 | return nest({ |
18 | 'sbot.hook.feed': (msg) => { |
19 | if (msg.key && msg.value && msg.value.content) { |
20 | var c = msg.value.content |
21 | if (c.type === 'post' && typeof c.channel === 'string') { |
22 | var name = c.channel.trim() |
23 | if (name && name.length < 30) { |
24 | var channel = channelsLookup.get(name) |
25 | if (!channel) { |
26 | channel = Struct({ |
27 | id: name, |
28 | updatedAt: Value() |
29 | }) |
30 | channelsLookup.put(name, channel) |
31 | } |
32 | if (channel.updatedAt() < msg.timestamp) { |
33 | channel.updatedAt.set(msg.timestamp) |
34 | } |
35 | } |
36 | } |
37 | } |
38 | }, |
39 | 'channel.obs.recent': () => recentChannels |
40 | }) |
41 | } |
42 |
Built with git-ssb-web