Files: a821b0ce884300e8ff3901678c19c6827e1189e9 / api / info-cache.js
1730 bytesRaw
1 | var Value = require('@mmckegg/mutant/value') |
2 | var computed = require('@mmckegg/mutant/computed') |
3 | var MutantDict = require('@mmckegg/mutant/dict') |
4 | var MutantStruct = require('@mmckegg/mutant/struct') |
5 | |
6 | module.exports = function InfoCache () { |
7 | var likesLookup = {} |
8 | var channelsLookup = MutantDict() |
9 | |
10 | var channels = computed(channelsLookup, (lookup) => { |
11 | var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.updatedAt - a.updatedAt) |
12 | return values |
13 | }, {nextTick: true}) |
14 | |
15 | return { getLikes, channels, updateFrom } |
16 | |
17 | function getLikes (msgId) { |
18 | if (!likesLookup[msgId]) { |
19 | likesLookup[msgId] = Value({}) |
20 | } |
21 | return likesLookup[msgId] |
22 | } |
23 | |
24 | function updateFrom (msg) { |
25 | if (msg.key && msg.value && msg.value.content) { |
26 | var c = msg.value.content |
27 | if (c.type === 'vote') { |
28 | if (msg.value.content.vote && msg.value.content.vote.link) { |
29 | var likes = getLikes(msg.value.content.vote.link)() |
30 | if (!likes[msg.value.author] || likes[msg.value.author][1] < msg.timestamp) { |
31 | likes[msg.value.author] = [msg.value.content.vote.value > 0, msg.timestamp] |
32 | getLikes(msg.value.content.vote.link).set(likes) |
33 | } |
34 | } |
35 | } else if (c.type === 'post' && typeof c.channel === 'string') { |
36 | var name = c.channel.trim() |
37 | if (name) { |
38 | var channel = channelsLookup.get(name) |
39 | if (!channel) { |
40 | channel = MutantStruct({ |
41 | id: name, |
42 | updatedAt: Value() |
43 | }) |
44 | channelsLookup.put(name, channel) |
45 | } |
46 | if (channel.updatedAt() < msg.timestamp) { |
47 | channel.updatedAt.set(msg.timestamp) |
48 | } |
49 | } |
50 | } |
51 | } |
52 | } |
53 | } |
54 |
Built with git-ssb-web