element-web/src/indexing/EventIndex.ts

968 lines
34 KiB
TypeScript
Raw Normal View History

/*
2021-06-07 09:57:11 +00:00
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-06-07 09:57:11 +00:00
import { EventEmitter } from "events";
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
import { Direction, EventTimeline } from 'matrix-js-sdk/src/models/event-timeline';
2021-06-07 09:57:11 +00:00
import { Room } from 'matrix-js-sdk/src/models/room';
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { EventTimelineSet } from 'matrix-js-sdk/src/models/event-timeline-set';
import { RoomState } from 'matrix-js-sdk/src/models/room-state';
import { TimelineIndex, TimelineWindow } from 'matrix-js-sdk/src/timeline-window';
import { sleep } from "matrix-js-sdk/src/utils";
2021-07-10 14:43:46 +00:00
import { IResultRoomEvents } from "matrix-js-sdk/src/@types/search";
2021-06-07 09:57:11 +00:00
import PlatformPeg from "../PlatformPeg";
2021-06-07 09:57:11 +00:00
import { MatrixClientPeg } from "../MatrixClientPeg";
import SettingsStore from "../settings/SettingsStore";
2021-06-07 09:57:11 +00:00
import { SettingLevel } from "../settings/SettingLevel";
import { ICrawlerCheckpoint, ILoadArgs, ISearchArgs } from "./BaseEventIndexManager";
2021-06-07 09:57:11 +00:00
import { logger } from "matrix-js-sdk/src/logger";
2021-06-07 09:57:11 +00:00
// The time in ms that the crawler will wait loop iterations if there
// have not been any checkpoints to consume in the last iteration.
const CRAWLER_IDLE_TIME = 5000;
// The maximum number of events our crawler should fetch in a single crawl.
const EVENTS_PER_CRAWL = 100;
interface ICrawler {
cancel(): void;
}
/*
* Event indexing class that wraps the platform specific event indexing.
*/
export default class EventIndex extends EventEmitter {
private crawlerCheckpoints: ICrawlerCheckpoint[] = [];
2021-06-07 09:57:11 +00:00
private crawler: ICrawler = null;
private currentCheckpoint: ICrawlerCheckpoint = null;
2021-06-07 09:57:11 +00:00
public async init() {
const indexManager = PlatformPeg.get().getEventIndexingManager();
this.crawlerCheckpoints = await indexManager.loadCheckpoints();
logger.log("EventIndex: Loaded checkpoints", this.crawlerCheckpoints);
this.registerListeners();
}
/**
* Register event listeners that are necessary for the event index to work.
*/
2021-06-07 09:57:11 +00:00
public registerListeners() {
const client = MatrixClientPeg.get();
client.on('sync', this.onSync);
client.on('Room.timeline', this.onRoomTimeline);
client.on('Room.timelineReset', this.onTimelineReset);
client.on('Room.redaction', this.onRedaction);
client.on('RoomState.events', this.onRoomStateEvent);
}
/**
* Remove the event index specific event listeners.
*/
2021-06-07 09:57:11 +00:00
public removeListeners() {
const client = MatrixClientPeg.get();
if (client === null) return;
client.removeListener('sync', this.onSync);
client.removeListener('Room.timeline', this.onRoomTimeline);
client.removeListener('Room.timelineReset', this.onTimelineReset);
client.removeListener('Room.redaction', this.onRedaction);
client.removeListener('RoomState.events', this.onRoomStateEvent);
}
/**
* Get crawler checkpoints for the encrypted rooms and store them in the index.
*/
2021-06-07 09:57:11 +00:00
public async addInitialCheckpoints() {
const indexManager = PlatformPeg.get().getEventIndexingManager();
const client = MatrixClientPeg.get();
const rooms = client.getRooms();
const isRoomEncrypted = (room) => {
return client.isRoomEncrypted(room.roomId);
};
// We only care to crawl the encrypted rooms, non-encrypted
// rooms can use the search provided by the homeserver.
const encryptedRooms = rooms.filter(isRoomEncrypted);
logger.log("EventIndex: Adding initial crawler checkpoints");
// Gather the prev_batch tokens and create checkpoints for
// our message crawler.
await Promise.all(encryptedRooms.map(async (room) => {
const timeline = room.getLiveTimeline();
const token = timeline.getPaginationToken(Direction.Backward);
const backCheckpoint: ICrawlerCheckpoint = {
roomId: room.roomId,
token: token,
2021-07-10 14:43:46 +00:00
direction: Direction.Backward,
fullCrawl: true,
};
const forwardCheckpoint: ICrawlerCheckpoint = {
roomId: room.roomId,
token: token,
2021-07-10 14:43:46 +00:00
direction: Direction.Forward,
};
try {
if (backCheckpoint.token) {
await indexManager.addCrawlerCheckpoint(backCheckpoint);
this.crawlerCheckpoints.push(backCheckpoint);
}
if (forwardCheckpoint.token) {
await indexManager.addCrawlerCheckpoint(forwardCheckpoint);
this.crawlerCheckpoints.push(forwardCheckpoint);
}
} catch (e) {
logger.log(
"EventIndex: Error adding initial checkpoints for room",
room.roomId,
backCheckpoint,
forwardCheckpoint,
e,
);
}
}));
}
/*
* The sync event listener.
*
* The listener has two cases:
* - First sync after start up, check if the index is empty, add
* initial checkpoints, if so. Start the crawler background task.
* - Every other sync, tell the event index to commit all the queued up
* live events
*/
2021-06-07 09:57:11 +00:00
private onSync = async (state: string, prevState: string, data: object) => {
const indexManager = PlatformPeg.get().getEventIndexingManager();
if (prevState === "PREPARED" && state === "SYNCING") {
// If our indexer is empty we're most likely running Element the
// first time with indexing support or running it with an
// initial sync. Add checkpoints to crawl our encrypted rooms.
const eventIndexWasEmpty = await indexManager.isEventIndexEmpty();
if (eventIndexWasEmpty) await this.addInitialCheckpoints();
this.startCrawler();
return;
}
if (prevState === "SYNCING" && state === "SYNCING") {
// A sync was done, presumably we queued up some live events,
// commit them now.
await indexManager.commitLiveEvents();
return;
}
2021-06-29 12:11:58 +00:00
};
/*
* The Room.timeline listener.
*
* This listener waits for live events in encrypted rooms, if they are
* decrypted or unencrypted we queue them to be added to the index,
* otherwise we save their event id and wait for them in the Event.decrypted
* listener.
*/
2021-06-07 09:57:11 +00:00
private onRoomTimeline = async (
ev: MatrixEvent,
room: Room,
toStartOfTimeline: boolean,
removed: boolean,
data: {
liveEvent: boolean;
},
) => {
const client = MatrixClientPeg.get();
// We only index encrypted rooms locally.
if (!client.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;
}
2021-05-18 12:01:38 +00:00
await client.decryptEventIfNeeded(ev);
await this.addLiveEventToIndex(ev);
2021-06-29 12:11:58 +00:00
};
2021-06-07 09:57:11 +00:00
private onRoomStateEvent = async (ev: MatrixEvent, state: RoomState) => {
if (!MatrixClientPeg.get().isRoomEncrypted(state.roomId)) return;
if (ev.getType() === "m.room.encryption" && !(await this.isRoomIndexed(state.roomId))) {
logger.log("EventIndex: Adding a checkpoint for a newly encrypted room", state.roomId);
this.addRoomCheckpoint(state.roomId, true);
}
2021-06-29 12:11:58 +00:00
};
/*
* The Room.redaction listener.
*
* Removes a redacted event from our event index.
*/
2021-06-07 09:57:11 +00:00
private onRedaction = async (ev: MatrixEvent, room: Room) => {
// We only index encrypted rooms locally.
if (!MatrixClientPeg.get().isRoomEncrypted(room.roomId)) return;
const indexManager = PlatformPeg.get().getEventIndexingManager();
try {
await indexManager.deleteEvent(ev.getAssociatedId());
} catch (e) {
logger.log("EventIndex: Error deleting event from index", e);
}
2021-06-29 12:11:58 +00:00
};
/*
* The Room.timelineReset listener.
*
* Listens for timeline resets that are caused by a limited timeline to
* re-add checkpoints for rooms that need to be crawled again.
*/
2021-06-07 09:57:11 +00:00
private onTimelineReset = async (room: Room, timelineSet: EventTimelineSet, resetAllTimelines: boolean) => {
if (room === null) return;
if (!MatrixClientPeg.get().isRoomEncrypted(room.roomId)) return;
logger.log("EventIndex: Adding a checkpoint because of a limited timeline",
2020-05-21 10:35:47 +00:00
room.roomId);
this.addRoomCheckpoint(room.roomId, false);
2021-06-29 12:11:58 +00:00
};
/**
* Check if an event should be added to the event index.
*
* Most notably we filter events for which decryption failed, are redacted
* or aren't of a type that we know how to index.
*
* @param {MatrixEvent} ev The event that should checked.
* @returns {bool} Returns true if the event can be indexed, false
* otherwise.
*/
2021-06-07 09:57:11 +00:00
private isValidEvent(ev: MatrixEvent) {
const isUsefulType = ["m.room.message", "m.room.name", "m.room.topic"].includes(ev.getType());
const validEventType = isUsefulType && !ev.isRedacted() && !ev.isDecryptionFailure();
let validMsgType = true;
let hasContentValue = true;
if (ev.getType() === "m.room.message" && !ev.isRedacted()) {
// Expand this if there are more invalid msgtypes.
const msgtype = ev.getContent().msgtype;
if (!msgtype) validMsgType = false;
else validMsgType = !msgtype.startsWith("m.key.verification");
2020-04-20 08:10:16 +00:00
if (!ev.getContent().body) hasContentValue = false;
} else if (ev.getType() === "m.room.topic" && !ev.isRedacted()) {
if (!ev.getContent().topic) hasContentValue = false;
} else if (ev.getType() === "m.room.name" && !ev.isRedacted()) {
if (!ev.getContent().name) hasContentValue = false;
}
return validEventType && validMsgType && hasContentValue;
}
2021-06-07 09:57:11 +00:00
private eventToJson(ev: MatrixEvent) {
2021-06-17 13:24:53 +00:00
const jsonEvent: any = ev.toJSON();
const e = ev.isEncrypted() ? jsonEvent.decrypted : jsonEvent;
if (ev.isEncrypted()) {
// Let us store some additional data so we can re-verify the event.
// The js-sdk checks if an event is encrypted using the algorithm,
// the sender key and ed25519 signing key are used to find the
// correct device that sent the event which allows us to check the
// verification state of the event, either directly or using cross
// signing.
e.curve25519Key = ev.getSenderKey();
e.ed25519Key = ev.getClaimedEd25519Key();
e.algorithm = ev.getWireContent().algorithm;
e.forwardingCurve25519KeyChain = ev.getForwardingCurve25519KeyChain();
} else {
// Make sure that unencrypted events don't contain any of that data,
// despite what the server might give to us.
delete e.curve25519Key;
delete e.ed25519Key;
delete e.algorithm;
delete e.forwardingCurve25519KeyChain;
}
return e;
}
/**
* Queue up live events to be added to the event index.
*
* @param {MatrixEvent} ev The event that should be added to the index.
*/
2021-06-07 09:57:11 +00:00
private async addLiveEventToIndex(ev: MatrixEvent) {
const indexManager = PlatformPeg.get().getEventIndexingManager();
if (!this.isValidEvent(ev)) return;
const e = this.eventToJson(ev);
const profile = {
displayname: ev.sender.rawDisplayName,
avatar_url: ev.sender.getMxcAvatarUrl(),
};
2020-05-21 08:10:46 +00:00
await indexManager.addEventToIndex(e, profile);
}
/**
* Emmit that the crawler has changed the checkpoint that it's currently
* handling.
*/
2021-06-07 09:57:11 +00:00
private emitNewCheckpoint() {
this.emit("changedCheckpoint", this.currentRoom());
}
2021-06-07 09:57:11 +00:00
private async addEventsFromLiveTimeline(timeline: EventTimeline) {
2020-05-21 10:35:47 +00:00
const events = timeline.getEvents();
for (let i = 0; i < events.length; i++) {
const ev = events[i];
await this.addLiveEventToIndex(ev);
}
}
2021-06-07 09:57:11 +00:00
private async addRoomCheckpoint(roomId: string, fullCrawl = false) {
const indexManager = PlatformPeg.get().getEventIndexingManager();
const client = MatrixClientPeg.get();
const room = client.getRoom(roomId);
if (!room) return;
const timeline = room.getLiveTimeline();
const token = timeline.getPaginationToken(Direction.Backward);
2020-05-21 10:35:47 +00:00
if (!token) {
// The room doesn't contain any tokens, meaning the live timeline
// contains all the events, add those to the index.
await this.addEventsFromLiveTimeline(timeline);
return;
}
const checkpoint = {
roomId: room.roomId,
token: token,
fullCrawl: fullCrawl,
2021-07-10 14:43:46 +00:00
direction: Direction.Backward,
};
logger.log("EventIndex: Adding checkpoint", checkpoint);
2020-05-21 10:35:47 +00:00
try {
await indexManager.addCrawlerCheckpoint(checkpoint);
} catch (e) {
logger.log(
"EventIndex: Error adding new checkpoint for room",
room.roomId,
checkpoint,
e,
);
}
this.crawlerCheckpoints.push(checkpoint);
}
/**
* The main crawler loop.
*
* Goes through crawlerCheckpoints and fetches events from the server to be
* added to the EventIndex.
*
* If a /room/{roomId}/messages request doesn't contain any events, stop the
* crawl, otherwise create a new checkpoint and push it to the
* crawlerCheckpoints queue so we go through them in a round-robin way.
*/
2021-06-07 09:57:11 +00:00
private async crawlerFunc() {
let cancelled = false;
const client = MatrixClientPeg.get();
const indexManager = PlatformPeg.get().getEventIndexingManager();
2021-06-07 09:57:11 +00:00
this.crawler = {
cancel: () => {
cancelled = true;
},
};
let idle = false;
while (!cancelled) {
let sleepTime = SettingsStore.getValueAt(SettingLevel.DEVICE, 'crawlerSleepTime');
// Don't let the user configure a lower sleep time than 100 ms.
sleepTime = Math.max(sleepTime, 100);
if (idle) {
2021-06-07 09:57:11 +00:00
sleepTime = CRAWLER_IDLE_TIME;
}
2021-06-07 09:57:11 +00:00
if (this.currentCheckpoint !== null) {
this.currentCheckpoint = null;
this.emitNewCheckpoint();
}
await sleep(sleepTime);
if (cancelled) {
break;
}
2019-11-13 14:57:12 +00:00
const checkpoint = this.crawlerCheckpoints.shift();
/// There is no checkpoint available currently, one may appear if
// a sync with limited room timelines happens, so go back to sleep.
if (checkpoint === undefined) {
idle = true;
continue;
}
2021-06-07 09:57:11 +00:00
this.currentCheckpoint = checkpoint;
this.emitNewCheckpoint();
idle = false;
// We have a checkpoint, let us fetch some messages, again, very
// conservatively to not bother our homeserver too much.
2021-06-29 12:11:58 +00:00
const eventMapper = client.getEventMapper({ preventReEmit: true });
// TODO we need to ensure to use member lazy loading with this
// request so we get the correct profiles.
let res;
try {
res = await client.createMessagesRequest(
2021-06-07 09:57:11 +00:00
checkpoint.roomId,
checkpoint.token,
EVENTS_PER_CRAWL,
checkpoint.direction,
);
} catch (e) {
if (e.httpStatus === 403) {
logger.log("EventIndex: Removing checkpoint as we don't have ",
"permissions to fetch messages from this room.", checkpoint);
try {
await indexManager.removeCrawlerCheckpoint(checkpoint);
} catch (e) {
logger.log("EventIndex: Error removing checkpoint", checkpoint, e);
// We don't push the checkpoint here back, it will
// hopefully be removed after a restart. But let us
// ignore it for now as we don't want to hammer the
// endpoint.
}
continue;
}
logger.log("EventIndex: Error crawling using checkpoint:", checkpoint, ",", e);
2019-11-13 14:57:12 +00:00
this.crawlerCheckpoints.push(checkpoint);
continue;
}
if (cancelled) {
this.crawlerCheckpoints.push(checkpoint);
break;
}
if (res.chunk.length === 0) {
logger.log("EventIndex: Done with the checkpoint", checkpoint);
// We got to the start/end of our timeline, lets just
// delete our checkpoint and go back to sleep.
try {
await indexManager.removeCrawlerCheckpoint(checkpoint);
} catch (e) {
logger.log("EventIndex: Error removing checkpoint", checkpoint, e);
}
continue;
}
// Convert the plain JSON events into Matrix events so they get
// decrypted if necessary.
const matrixEvents = res.chunk.map(eventMapper);
let stateEvents = [];
if (res.state !== undefined) {
stateEvents = res.state.map(eventMapper);
}
const profiles = {};
stateEvents.forEach(ev => {
if (ev.event.content &&
ev.event.content.membership === "join") {
profiles[ev.event.sender] = {
displayname: ev.event.content.displayname,
avatar_url: ev.event.content.avatar_url,
};
}
});
const decryptionPromises = matrixEvents
.filter(event => event.isEncrypted())
.map(event => {
2021-05-18 12:01:38 +00:00
return client.decryptEventIfNeeded(event, {
isRetry: true,
emit: false,
});
});
// Let us wait for all the events to get decrypted.
await Promise.all(decryptionPromises);
2019-11-18 14:04:44 +00:00
// TODO if there are no events at this point we're missing a lot
// decryption keys, do we want to retry this checkpoint at a later
// stage?
const filteredEvents = matrixEvents.filter(this.isValidEvent);
// Collect the redaction events so we can delete the redacted events
// from the index.
const redactionEvents = matrixEvents.filter((ev) => {
return ev.getType() === "m.room.redaction";
});
// Let us convert the events back into a format that EventIndex can
// consume.
const events = filteredEvents.map((ev) => {
const e = this.eventToJson(ev);
let profile = {};
if (e.sender in profiles) profile = profiles[e.sender];
const object = {
event: e,
profile: profile,
};
return object;
});
let newCheckpoint;
// The token can be null for some reason. Don't create a checkpoint
// in that case since adding it to the db will fail.
if (res.end) {
// Create a new checkpoint so we can continue crawling the room
// for messages.
newCheckpoint = {
roomId: checkpoint.roomId,
token: res.end,
fullCrawl: checkpoint.fullCrawl,
direction: checkpoint.direction,
};
}
try {
2020-03-12 11:20:46 +00:00
for (let i = 0; i < redactionEvents.length; i++) {
const ev = redactionEvents[i];
const eventId = ev.getAssociatedId();
if (eventId) {
await indexManager.deleteEvent(eventId);
} else {
console.warn("EventIndex: Redaction event doesn't contain a valid associated event id", ev);
}
}
const eventsAlreadyAdded = await indexManager.addHistoricEvents(
events, newCheckpoint, checkpoint);
// We didn't get a valid new checkpoint from the server, nothing
// to do here anymore.
if (!newCheckpoint) {
logger.log("EventIndex: The server didn't return a valid ",
"new checkpoint, not continuing the crawl.", checkpoint);
2020-05-20 09:14:37 +00:00
continue;
}
// If all events were already indexed we assume that we catched
// up with our index and don't need to crawl the room further.
// Let us delete the checkpoint in that case, otherwise push
// the new checkpoint to be used by the crawler.
if (eventsAlreadyAdded === true && newCheckpoint.fullCrawl !== true) {
logger.log("EventIndex: Checkpoint had already all events",
"added, stopping the crawl", checkpoint);
await indexManager.removeCrawlerCheckpoint(newCheckpoint);
} else {
if (eventsAlreadyAdded === true) {
logger.log("EventIndex: Checkpoint had already all events",
"added, but continuing due to a full crawl", checkpoint);
}
2019-11-13 14:57:12 +00:00
this.crawlerCheckpoints.push(newCheckpoint);
}
} catch (e) {
logger.log("EventIndex: Error durring a crawl", e);
2019-11-18 14:04:44 +00:00
// An error occurred, put the checkpoint back so we
// can retry.
2019-11-13 14:57:12 +00:00
this.crawlerCheckpoints.push(checkpoint);
}
}
2021-06-07 09:57:11 +00:00
this.crawler = null;
}
/**
* Start the crawler background task.
*/
2021-06-07 09:57:11 +00:00
public startCrawler() {
if (this.crawler !== null) return;
this.crawlerFunc();
}
/**
* Stop the crawler background task.
*/
2021-06-07 09:57:11 +00:00
public stopCrawler() {
if (this.crawler === null) return;
this.crawler.cancel();
}
/**
* Close the event index.
*
* This removes all the MatrixClient event listeners, stops the crawler
* task, and closes the index.
*/
2021-06-07 09:57:11 +00:00
public async close() {
const indexManager = PlatformPeg.get().getEventIndexingManager();
this.removeListeners();
this.stopCrawler();
await indexManager.closeEventIndex();
return;
}
/**
* Search the event index using the given term for matching events.
*
* @param {ISearchArgs} searchArgs The search configuration for the search,
* sets the search term and determines the search result contents.
*
2021-07-10 14:43:46 +00:00
* @return {Promise<IResultRoomEvents[]>} A promise that will resolve to an array
* of search results once the search is done.
*/
2021-07-10 14:43:46 +00:00
public async search(searchArgs: ISearchArgs): Promise<IResultRoomEvents> {
const indexManager = PlatformPeg.get().getEventIndexingManager();
return indexManager.searchEventIndex(searchArgs);
}
/**
* Load events that contain URLs from the event index.
*
* @param {Room} room The room for which we should fetch events containing
* URLs
*
* @param {number} limit The maximum number of events to fetch.
*
* @param {string} fromEvent From which event should we continue fetching
* events from the index. This is only needed if we're continuing to fill
* the timeline, e.g. if we're paginating. This needs to be set to a event
* id of an event that was previously fetched with this function.
*
* @param {string} direction The direction in which we will continue
* fetching events. EventTimeline.BACKWARDS to continue fetching events that
* are older than the event given in fromEvent, EventTimeline.FORWARDS to
* fetch newer events.
*
* @returns {Promise<MatrixEvent[]>} Resolves to an array of events that
* contain URLs.
*/
2021-06-07 09:57:11 +00:00
public async loadFileEvents(
room: Room,
limit = 10,
fromEvent: string = null,
direction: string = EventTimeline.BACKWARDS,
) {
const client = MatrixClientPeg.get();
const indexManager = PlatformPeg.get().getEventIndexingManager();
const loadArgs: ILoadArgs = {
roomId: room.roomId,
2020-01-17 10:52:20 +00:00
limit: limit,
};
if (fromEvent) {
loadArgs.fromEvent = fromEvent;
loadArgs.direction = direction;
}
2020-01-17 10:52:20 +00:00
let events;
// Get our events from the event index.
try {
events = await indexManager.loadFileEvents(loadArgs);
} catch (e) {
logger.log("EventIndex: Error getting file events", e);
2020-01-17 10:52:20 +00:00
return [];
}
2020-01-17 10:52:20 +00:00
const eventMapper = client.getEventMapper();
// Turn the events into MatrixEvent objects.
const matrixEvents = events.map(e => {
const matrixEvent = eventMapper(e.event);
const member = new RoomMember(room.roomId, matrixEvent.getSender());
// We can't really reconstruct the whole room state from our
// EventIndex to calculate the correct display name. Use the
// disambiguated form always instead.
member.name = e.profile.displayname + " (" + matrixEvent.getSender() + ")";
// This is sets the avatar URL.
const memberEvent = eventMapper(
{
content: {
membership: "join",
avatar_url: e.profile.avatar_url,
displayname: e.profile.displayname,
},
type: "m.room.member",
event_id: matrixEvent.getId() + ":eventIndex",
room_id: matrixEvent.getRoomId(),
sender: matrixEvent.getSender(),
origin_server_ts: matrixEvent.getTs(),
2020-01-17 10:52:20 +00:00
state_key: matrixEvent.getSender(),
},
);
// We set this manually to avoid emitting RoomMember.membership and
// RoomMember.name events.
member.events.member = memberEvent;
matrixEvent.sender = member;
return matrixEvent;
});
return matrixEvents;
}
/**
* Fill a timeline with events that contain URLs.
*
* @param {TimelineSet} timelineSet The TimelineSet the Timeline belongs to,
* used to check if we're adding duplicate events.
*
* @param {Timeline} timeline The Timeline which should be filed with
* events.
*
* @param {Room} room The room for which we should fetch events containing
* URLs
*
* @param {number} limit The maximum number of events to fetch.
*
* @param {string} fromEvent From which event should we continue fetching
* events from the index. This is only needed if we're continuing to fill
* the timeline, e.g. if we're paginating. This needs to be set to a event
* id of an event that was previously fetched with this function.
*
* @param {string} direction The direction in which we will continue
* fetching events. EventTimeline.BACKWARDS to continue fetching events that
* are older than the event given in fromEvent, EventTimeline.FORWARDS to
* fetch newer events.
*
* @returns {Promise<boolean>} Resolves to true if events were added to the
* timeline, false otherwise.
*/
2021-06-07 09:57:11 +00:00
public async populateFileTimeline(
timelineSet: EventTimelineSet,
timeline: EventTimeline,
room: Room,
limit = 10,
2021-06-07 09:57:11 +00:00
fromEvent: string = null,
direction: string = EventTimeline.BACKWARDS,
) {
2020-01-17 10:52:20 +00:00
const matrixEvents = await this.loadFileEvents(room, limit, fromEvent, direction);
// If this is a normal fill request, not a pagination request, we need
// to get our events in the BACKWARDS direction but populate them in the
// forwards direction.
// This needs to happen because a fill request might come with an
// exisitng timeline e.g. if you close and re-open the FilePanel.
if (fromEvent === null) {
matrixEvents.reverse();
direction = direction == EventTimeline.BACKWARDS ? EventTimeline.FORWARDS: EventTimeline.BACKWARDS;
}
// Add the events to the timeline of the file panel.
matrixEvents.forEach(e => {
if (!timelineSet.eventIdToTimeline(e.getId())) {
timelineSet.addEventToTimeline(e, timeline, direction == EventTimeline.BACKWARDS);
}
});
let ret = false;
let paginationToken = "";
// Set the pagination token to the oldest event that we retrieved.
if (matrixEvents.length > 0) {
paginationToken = matrixEvents[matrixEvents.length - 1].getId();
ret = true;
}
logger.log("EventIndex: Populating file panel with", matrixEvents.length,
"events and setting the pagination token to", paginationToken);
timeline.setPaginationToken(paginationToken, EventTimeline.BACKWARDS);
return ret;
}
/**
* Emulate a TimelineWindow pagination() request with the event index as the event source
*
* Might not fetch events from the index if the timeline already contains
* events that the window isn't showing.
*
* @param {Room} room The room for which we should fetch events containing
* URLs
*
* @param {TimelineWindow} timelineWindow The timeline window that should be
* populated with new events.
*
* @param {string} direction The direction in which we should paginate.
* EventTimeline.BACKWARDS to paginate back, EventTimeline.FORWARDS to
* paginate forwards.
*
* @param {number} limit The maximum number of events to fetch while
* paginating.
*
* @returns {Promise<boolean>} Resolves to a boolean which is true if more
* events were successfully retrieved.
*/
2021-07-01 09:01:48 +00:00
public paginateTimelineWindow(room: Room, timelineWindow: TimelineWindow, direction: Direction, limit: number) {
const tl = timelineWindow.getTimelineIndex(direction);
if (!tl) return Promise.resolve(false);
if (tl.pendingPaginate) return tl.pendingPaginate;
if (timelineWindow.extend(direction, limit)) {
return Promise.resolve(true);
}
const paginationMethod = async (
timelineWindow: TimelineWindow,
timelineIndex: TimelineIndex,
room: Room,
direction: Direction,
limit: number,
) => {
const timeline = timelineIndex.timeline;
const timelineSet = timeline.getTimelineSet();
const token = timeline.getPaginationToken(direction);
const ret = await this.populateFileTimeline(
timelineSet,
timeline,
room,
limit,
token,
direction,
);
timelineIndex.pendingPaginate = null;
timelineWindow.extend(direction, limit);
return ret;
};
const paginationPromise = paginationMethod(timelineWindow, tl, room, direction, limit);
tl.pendingPaginate = paginationPromise;
return paginationPromise;
}
/**
* Get statistical information of the index.
*
* @return {Promise<IndexStats>} A promise that will resolve to the index
* statistics.
*/
2021-06-07 09:57:11 +00:00
public async getStats() {
const indexManager = PlatformPeg.get().getEventIndexingManager();
return indexManager.getStats();
}
/**
* Check if the room with the given id is already indexed.
*
* @param {string} roomId The ID of the room which we want to check if it
* has been already indexed.
*
* @return {Promise<boolean>} Returns true if the index contains events for
* the given room, false otherwise.
*/
2021-06-07 09:57:11 +00:00
public async isRoomIndexed(roomId) {
const indexManager = PlatformPeg.get().getEventIndexingManager();
return indexManager.isRoomIndexed(roomId);
}
/**
* Get the room that we are currently crawling.
*
2020-01-21 15:40:32 +00:00
* @returns {Room} A MatrixRoom that is being currently crawled, null
* if no room is currently being crawled.
*/
2021-06-07 09:57:11 +00:00
public currentRoom() {
if (this.currentCheckpoint === null && this.crawlerCheckpoints.length === 0) {
return null;
}
const client = MatrixClientPeg.get();
2021-06-07 09:57:11 +00:00
if (this.currentCheckpoint !== null) {
return client.getRoom(this.currentCheckpoint.roomId);
} else {
return client.getRoom(this.crawlerCheckpoints[0].roomId);
}
}
2021-06-07 09:57:11 +00:00
public crawlingRooms() {
const totalRooms = new Set();
const crawlingRooms = new Set();
this.crawlerCheckpoints.forEach((checkpoint, index) => {
crawlingRooms.add(checkpoint.roomId);
});
2021-06-07 09:57:11 +00:00
if (this.currentCheckpoint !== null) {
crawlingRooms.add(this.currentCheckpoint.roomId);
}
const client = MatrixClientPeg.get();
const rooms = client.getRooms();
2021-06-07 09:57:11 +00:00
const isRoomEncrypted = (room: Room) => {
return client.isRoomEncrypted(room.roomId);
};
const encryptedRooms = rooms.filter(isRoomEncrypted);
encryptedRooms.forEach((room, index) => {
totalRooms.add(room.roomId);
});
2021-06-07 09:57:11 +00:00
return { crawlingRooms, totalRooms };
}
}