MatrixChat: Add live events to the event index as well.

This commit is contained in:
Damir Jelić 2019-10-11 16:28:36 +02:00
parent b23ba5f881
commit 5e7076e985

View file

@ -1271,6 +1271,7 @@ export default createReactClass({
this.firstSyncComplete = false; this.firstSyncComplete = false;
this.firstSyncPromise = Promise.defer(); this.firstSyncPromise = Promise.defer();
this.crawlerChekpoints = []; this.crawlerChekpoints = [];
this.liveEventsForIndex = new Set();
const cli = MatrixClientPeg.get(); const cli = MatrixClientPeg.get();
const IncomingSasDialog = sdk.getComponent('views.dialogs.IncomingSasDialog'); const IncomingSasDialog = sdk.getComponent('views.dialogs.IncomingSasDialog');
@ -1363,6 +1364,14 @@ export default createReactClass({
self.crawlerRef = crawlerHandle; self.crawlerRef = crawlerHandle;
return; return;
} }
if (prevState === "SYNCING" && state === "SYNCING") {
// A sync was done, presumably we queued up some live events,
// commit them now.
console.log("Seshat: Committing events");
await platform.commitLiveEvents();
return;
}
}); });
cli.on('sync', function(state, prevState, data) { cli.on('sync', function(state, prevState, data) {
@ -1447,6 +1456,44 @@ export default createReactClass({
}, null, true); }, null, true);
}); });
cli.on("Room.timeline", async (ev, room, toStartOfTimeline, removed, data) => {
const platform = PlatformPeg.get();
if (!platform.supportsEventIndexing()) return;
// We only index encrypted rooms locally.
if (!MatrixClientPeg.get().isRoomEncrypted(room.roomId)) return;
// If it isn't a live event or if it's redacted there's nothing to
// do.
if (toStartOfTimeline || !data || !data.liveEvent
|| ev.isRedacted()) {
return;
}
// If the event is not yet decrypted mark it for the
// Event.decrypted callback.
if (ev.isBeingDecrypted()) {
const eventId = ev.getId();
self.liveEventsForIndex.add(eventId);
} else {
// If the event is decrypted or is unencrypted add it to the
// index now.
await self.addLiveEventToIndex(ev);
}
});
cli.on("Event.decrypted", async (ev, err) => {
const platform = PlatformPeg.get();
if (!platform.supportsEventIndexing()) return;
const eventId = ev.getId();
// If the event isn't in our live event set, ignore it.
if (!self.liveEventsForIndex.delete(eventId)) return;
if (err) return;
await self.addLiveEventToIndex(ev);
});
cli.on("accountData", function(ev) { cli.on("accountData", function(ev) {
if (ev.getType() === 'im.vector.web.settings') { if (ev.getType() === 'im.vector.web.settings') {
if (ev.getContent() && ev.getContent().theme) { if (ev.getContent() && ev.getContent().theme) {
@ -2009,6 +2056,24 @@ export default createReactClass({
</ErrorBoundary>; </ErrorBoundary>;
}, },
async addLiveEventToIndex(ev) {
const platform = PlatformPeg.get();
if (!platform.supportsEventIndexing()) return;
if (["m.room.message", "m.room.name", "m.room.topic"]
.indexOf(ev.getType()) == -1) {
return;
}
const e = ev.toJSON().decrypted;
const profile = {
displayname: ev.sender.rawDisplayName,
avatar_url: ev.sender.getMxcAvatarUrl(),
};
platform.addEventToIndex(e, profile);
},
async crawlerFunc(handle) { async crawlerFunc(handle) {
// TODO either put this in a better place or find a library provided // TODO either put this in a better place or find a library provided
// method that does this. // method that does this.