git ssb

10+

Matt McKegg / patchwork



Tree: ffb7bedc3c585728df51f065d1d5d2f2d826319e

Files: ffb7bedc3c585728df51f065d1d5d2f2d826319e / modules / channel / obs / recent.js

1227 bytesRaw
1var nest = require('depnest')
2var { Value, Dict, Struct, computed, throttle } = require('mutant')
3
4exports.gives = nest({
5 'sbot.hook.feed': true,
6 'channel.obs.recent': true
7})
8
9exports.create = function (api) {
10 var channelsLookup = Dict()
11
12 var recentChannels = computed(throttle(channelsLookup, 1000), (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