This commit is contained in:
RMidhunSuresh 2023-02-16 21:41:33 +05:30
parent 772d91f924
commit d81864e901
No known key found for this signature in database
4 changed files with 139 additions and 7 deletions

View File

@ -254,6 +254,7 @@ export class Timeline {
/** @package */
addEntries(newEntries) {
console.log("addEntries", newEntries);
this._addLocalRelationsToNewRemoteEntries(newEntries);
this._updateEntriesFetchedFromHomeserver(newEntries);
this._moveEntryToRemoteEntries(newEntries);

View File

@ -15,21 +15,47 @@ limitations under the License.
*/
import type {ILogItem} from "../../../../lib.js";
import type {Room} from "../../../room/Room.js";
import {Disposables} from "../../../../utils/Disposables";
export type UserData = {
userId: string;
deviceId: string;
}
export abstract class BaseSASVerificationStage {
constructor(protected room: Room,
protected ourUser: UserData,
protected otherUserId: string,
protected log: ILogItem) {
export type Options = {
room: Room;
ourUser: UserData;
otherUserId: string;
log: ILogItem;
}
export abstract class BaseSASVerificationStage extends Disposables {
protected room: Room;
protected ourUser: UserData;
protected otherUserId: string;
protected log: ILogItem;
protected requestEventId: string;
protected previousResult: undefined | any;
constructor(options: Options) {
super();
this.room = options.room;
this.ourUser = options.ourUser;
this.otherUserId = options.otherUserId;
this.log = options.log;
}
setRequestEventId(id: string) {
this.requestEventId = id;
// todo: can this race with incoming message?
this.nextStage?.setRequestEventId(id);
}
setResultFromPreviousStage(result?: any) {
this.previousResult = result;
}
abstract get type(): string;
abstract completeStage(): boolean | Promise<boolean>;
abstract completeStage(): undefined | Record<string, any>;
abstract get nextStage(): BaseSASVerificationStage;
}

View File

@ -30,7 +30,10 @@ import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
// const SAS_LIST = Object.keys(sasGenerators);
export class StartVerificationStage extends BaseSASVerificationStage {
private readyMessagePromise: Promise<any>;
private startMessagePromise: Promise<any>;
async completeStage() {
await this.log.wrap("StartVerificationStage.completeStage", async (log) => {
const content = {
@ -41,10 +44,38 @@ export class StartVerificationStage extends BaseSASVerificationStage {
"to": this.otherUserId,
};
await this.room.sendEvent("m.room.message", content, null, log);
const [readyContent, startContent] = await this.fetchMessageEventsFromTimeline();
console.log("readyContent", readyContent, "startContent", startContent);
this.dispose();
});
return true;
}
private fetchMessageEventsFromTimeline() {
let readyResolve, startResolve;
this.readyMessagePromise = new Promise(r => { readyResolve = r; });
this.startMessagePromise = new Promise(r => { startResolve = r; });
this.track(
this.room._timeline.entries.subscribe({
onAdd: (_, entry) => {
if (entry.eventType === "m.key.verification.ready") {
readyResolve(entry.content);
}
else if (entry.eventType === "m.key.verification.start") {
startResolve(entry.content);
}
},
onRemove: () => {
},
onUpdate: () => {
},
})
);
return Promise.all([this.readyMessagePromise, this.startMessagePromise]);
}
get type() {
return "m.key.verification.request";
}

View File

@ -0,0 +1,74 @@
/*
Copyright 2023 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.
*/
import {BaseSASVerificationStage, Options} from "./BaseSASVerificationStage";
export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
constructor(private messageType: string, options: Options) {
super(options);
}
async completeStage() {
await this.log.wrap("WaitForIncomingMessageStage.completeStage", async (log) => {
const content = await this.fetchMessageEventsFromTimeline();
console.log("content found", content);
this.nextStage.setResultFromPreviousStage({
...this.previousResult,
[this.messageType]: content
});
this.dispose();
});
return true;
}
private fetchMessageEventsFromTimeline() {
// todo: add timeout after 10 mins
return new Promise(resolve => {
this.track(
this.room._timeline.entries.subscribe({
onAdd: (_, entry) => {
if (entry.sender === this.ourUser.userId) {
// We only care about incoming / remote message events
return;
}
if (entry.eventType === this.messageType &&
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
resolve(entry.content);
}
},
onRemove: () => { },
onUpdate: () => { },
})
);
const remoteEntries = this.room._timeline.remoteEntries;
// In case we were slow and the event is already added to the timeline,
for (const entry of remoteEntries) {
if (entry.eventType === this.messageType &&
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
resolve(entry.content);
}
}
});
}
get type() {
return this.messageType;
}
get nextStage(): BaseSASVerificationStage {
return this;
}
}