git ssb

10+

Matt McKegg / patchwork



Tree: 9b14c2770a1b327ee389c1c27a56ae36412130c0

Files: 9b14c2770a1b327ee389c1c27a56ae36412130c0 / modules / channel / obs / recent.js

1166 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)
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) {
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