Commit c042715aec13b2e61372d88ff17dea7e4cacecc1
fix handling of realtime channel adding (and channel suggest side effect)
Matt McKegg committed on 6/26/2017, 3:55:14 AMParent: a57d294d9379c382e44e72a14e760b7064123545
Files changed
plugs/channel/obs/recent.js | changed |
sbot/channels.js | changed |
plugs/channel/obs/recent.js | ||
---|---|---|
@@ -31,32 +31,23 @@ | ||
31 | 31 | |
32 | 32 | pull( |
33 | 33 | api.sbot.pull.stream(sbot => sbot.patchwork.channels({live: true})), |
34 | 34 | pull.drain(msg => { |
35 | - if (!sync()) { | |
36 | - channelsLookup.transaction(() => { | |
37 | - for (var channel in msg) { | |
38 | - var obs = ChannelRef(channel) | |
39 | - obs.set({ | |
40 | - id: channel, | |
41 | - updatedAt: msg[channel].timestamp, | |
42 | - count: msg[channel].count | |
43 | - }) | |
35 | + channelsLookup.transaction(() => { | |
36 | + for (var channel in msg) { | |
37 | + var obs = channelsLookup.get(channel) | |
38 | + if (!obs) { | |
39 | + obs = ChannelRef(channel) | |
44 | 40 | channelsLookup.put(channel, obs) |
45 | 41 | } |
46 | - sync.set(true) | |
47 | - }) | |
48 | - } else { | |
49 | - var obs = channelsLookup.get(msg.channel) | |
50 | - if (!obs) { | |
51 | - obs = ChannelRef(msg.dest) | |
52 | - channelsLookup.put(msg.dest, obs) | |
42 | + var count = msg.count != null ? msg.count : obs.count() + 1 | |
43 | + var updatedAt = msg[channel].timestamp | |
44 | + | |
45 | + obs.set({ id: channel, updatedAt, count }) | |
53 | 46 | } |
54 | - obs.set({ | |
55 | - id: msg.channel, | |
56 | - updatedAt: Math.max(resolve(obs.updatedAt), msg.timestamp), | |
57 | - count: resolve(obs.count) + 1 | |
58 | - }) | |
47 | + }) | |
48 | + if (!sync()) { | |
49 | + sync.set(true) | |
59 | 50 | } |
60 | 51 | }) |
61 | 52 | ) |
62 | 53 |
sbot/channels.js | ||
---|---|---|
@@ -5,28 +5,29 @@ | ||
5 | 5 | } |
6 | 6 | |
7 | 7 | function reduce (result, item) { |
8 | 8 | if (!result) result = {} |
9 | - if (item) { | |
10 | - var value = result[item.channel] | |
11 | - if (!value) { | |
12 | - value = result[item.channel] = {count: 0, timestamp: 0} | |
9 | + if (items) { | |
10 | + for (var channel in item) { | |
11 | + var value = result[channel] | |
12 | + if (!value) { | |
13 | + value = result[channel] = {count: 0, timestamp: 0} | |
14 | + } | |
15 | + value.count += 1 | |
16 | + if (item[channel].timestamp > value.timestamp) { | |
17 | + value.timestamp = item[channel].timestamp | |
18 | + } | |
13 | 19 | } |
14 | - value.count += 1 | |
15 | - if (item.timestamp > value.timestamp) { | |
16 | - value.timestamp = item.timestamp | |
17 | - } | |
18 | 20 | } |
19 | 21 | return result |
20 | 22 | } |
21 | 23 | |
22 | 24 | function map (msg) { |
23 | 25 | if (msg.value.content && typeof msg.value.content.channel === 'string') { |
24 | 26 | var channel = msg.value.content.channel |
25 | 27 | if (channel.length > 0 && channel.length < 30) { |
26 | - return { | |
27 | - channel: channel.replace(/\s/g, ''), | |
28 | - timestamp: msg.timestamp | |
29 | - } | |
28 | + return [{ | |
29 | + [channel.replace(/\s/g, '')]: {timestamp: msg.timestamp} | |
30 | + }] | |
30 | 31 | } |
31 | 32 | } |
32 | 33 | } |
Built with git-ssb-web