Skip to content

Commit 8b1c2b8

Browse files
author
Eric Kirkham
authored
Merge pull request #1665 from hippware/1651
Addresses #1651
2 parents 478ca15 + 0d1f8ef commit 8b1c2b8

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/store/eventStore.js

+31-24
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ const EVENT_LIST_MAX_SIZE = 50;
3030
export class EventStore {
3131
notifications = xmpp.message.filter(msg => msg.notification);
3232
@observable loading: boolean = false;
33-
processedEvents: number = 0;
3433

3534
constructor() {
3635
this.notifications.onValue(this.onNotification);
3736
}
3837

3938
async start() {
4039
this.loading = true;
41-
if (!model.events.version) {
42-
await this.accumulateItems();
43-
}
40+
await this.accumulateItems(model.events.version);
4441
autorun(() => {
4542
if (model.connected) this.request();
4643
});
@@ -49,7 +46,7 @@ export class EventStore {
4946
}
5047

5148
request() {
52-
home.request(model.events.version);
49+
home.request(model.events.nextVersion || model.events.version);
5350
}
5451

5552
@action
@@ -104,8 +101,7 @@ export class EventStore {
104101
}
105102
}
106103

107-
const eventMessage = bot ? new EventBotShare(id, botFactory.create({id: bot.id, server: bot.server}), time, msg) : new EventMessage(id, msg.from, msg);
108-
return eventMessage;
104+
return bot ? new EventBotShare(id, botFactory.create({id: bot.id, server: bot.server}), time, msg) : new EventMessage(id, msg.from, msg);
109105
} else {
110106
log.log('& UNSUPPORTED ITEM!', item, {level: log.levels.WARNING});
111107
}
@@ -124,22 +120,29 @@ export class EventStore {
124120
nextVersion: ?string = null;
125121

126122
async onNotification({notification, delay}) {
127-
log.log('Notification', notification);
123+
log.log('onNotification', notification);
128124
let item;
129125
if (notification['reference-changed']) {
130126
const {id, server} = notification['reference-changed'].bot;
131127
const bot = botFactory.create({id, server});
132128
// don't download posts for the bot (it will be loaded later)
133129
await botStore.download(bot, false);
134130
} else if (notification.item) {
135-
item = notification.item;
131+
const {item} = notification;
136132
const newItem = this.processItem(item, delay);
137133
if (!newItem) {
138134
return;
139135
}
140-
model.events.listToAdd.push(newItem);
141136
if (newItem.bot) {
142-
await botStore.download(newItem.bot);
137+
if (!model.eventBots.get(newItem.bot.id)) {
138+
botStore.download(newItem.bot, false).then(() => {
139+
model.eventBots.add(newItem.bot);
140+
model.events.listToAdd.push(newItem);
141+
});
142+
} else {
143+
log.log(`Bot ${newItem.bot.id} already exists!`);
144+
model.events.listToAdd.push(newItem);
145+
}
143146
}
144147
if (item.version) model.events.nextVersion = item.version;
145148
else log.log('item has no version!', item);
@@ -154,7 +157,6 @@ export class EventStore {
154157
}
155158

156159
finish() {
157-
this.processedEvents = 0;
158160
this.loading = false;
159161
}
160162

@@ -172,28 +174,37 @@ export class EventStore {
172174
}
173175

174176
@action
175-
async accumulateItems(count: number = EVENT_PAGE_SIZE, current: number = 0): Promise<void> {
177+
async accumulateItems(version, count: number = EVENT_PAGE_SIZE, current: number = 0): Promise<void> {
176178
const {earliestId} = model.events;
177-
const data = await home.items(earliestId, count, true);
178-
if (!data.items.length) {
179+
const data = version ? await home.items(null, count, true, version) : await home.items(earliestId, count, true);
180+
if (!version && !data.items.length) {
179181
model.events.finished = true;
180182
return;
181183
}
182184
data.bots.forEach(bot => model.eventBots.add(botFactory.create(bot)));
183185
let newEventCount = 0;
184186
data.items.map(this.processItem).forEach((p) => {
185187
if (p) {
186-
model.events.add(p);
188+
if (version) {
189+
model.events.listToAdd.push(p);
190+
} else {
191+
model.events.add(p);
192+
}
187193
newEventCount += 1;
188194
}
189195
});
190-
const latest = data.version;
191-
if (latest) model.events.version = latest;
196+
if (data.version) {
197+
if (version) {
198+
model.events.nextVersion = data.version;
199+
} else if (!model.events.version) {
200+
model.events.version = data.version;
201+
}
202+
}
192203

193-
if (newEventCount + current < count && this.processedEvents < data.count) {
204+
if (newEventCount + current < count && data.count) {
194205
// account for the case where none are processed and earliestId remains the same
195206
if (newEventCount === 0 && model.events.earliestId === earliestId) count += EVENT_PAGE_SIZE;
196-
await this.accumulateItems(count, newEventCount + current);
207+
await this.accumulateItems(version, count, newEventCount + current);
197208
}
198209
}
199210

@@ -222,10 +233,6 @@ export class EventStore {
222233
listToAdd.forEach((e) => {
223234
try {
224235
model.events.add(e);
225-
// we must add referenced bot to 'eventBots' for serialization (HS contains only references!)
226-
if (e.bot) {
227-
model.eventBots.add(e.bot);
228-
}
229236
} catch (err) {
230237
log.log('Incorporate updates error, could not add', e, err);
231238
}

src/store/xmpp/homeService.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import * as log from '../../utils/log';
1313

1414
@autobind
1515
class HomeService {
16-
async items(before?: string, limit: number = 3, excludeDeleted: boolean = false): Promise<Object> {
17-
log.log('REQUEST HS EVENTS', before, limit, {level: log.levels.VERBOSE});
18-
const iq = $iq({type: 'get', to: xmpp.provider.username}).c('items', {xmlns: NS, node: 'home_stream'});
16+
async items(before?: string, limit: number = 3, excludeDeleted: boolean = false, version): Promise<Object> {
17+
log.log('REQUEST HS EVENTS', before, limit, excludeDeleted, version, {level: log.levels.VERBOSE});
18+
const iq = $iq({type: 'get', to: xmpp.provider.username});
19+
if (version) {
20+
iq.c('catchup', {xmlns: NS, node: 'home_stream', version});
21+
} else {
22+
iq.c('items', {xmlns: NS, node: 'home_stream'});
23+
}
1924
if (excludeDeleted) {
2025
iq.c('exclude-deleted').up();
2126
}
@@ -49,7 +54,7 @@ class HomeService {
4954
if (!Array.isArray(items)) {
5055
items = [items];
5156
}
52-
return data.items ? {items, bots, version: data.items.version, count: parseInt(data.items.set.count)} : {items, bots};
57+
return data.items ? {items, bots, version: data.items.version, count: data.items.set ? parseInt(data.items.set.count) : 0} : {items, bots};
5358
}
5459

5560
request(version: string): void {

0 commit comments

Comments
 (0)