git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: d09572761fb01c4513bc32466066d899f32ff4ab

Files: d09572761fb01c4513bc32466066d899f32ff4ab / channel / obs / recent.js

1201 bytesRaw
1var nest = require('depnest')
2var { Value, Dict, Struct, computed } = 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(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