Implement third-party invite waiting room (#10229)

This commit is contained in:
Michael Weimann
2023-03-06 12:08:04 +01:00
committed by GitHub
parent 94950c6987
commit db6ee53535
12 changed files with 388 additions and 40 deletions

View File

@@ -15,7 +15,6 @@ limitations under the License.
*/
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
import { canEncryptToAllUsers } from "../createRoom";
@@ -30,7 +29,7 @@ import { createDmLocalRoom } from "./dm/createDmLocalRoom";
import { startDm } from "./dm/startDm";
import { resolveThreePids } from "./threepids";
export async function startDmOnFirstMessage(client: MatrixClient, targets: Member[]): Promise<Room> {
export async function startDmOnFirstMessage(client: MatrixClient, targets: Member[]): Promise<string | null> {
let resolvedTargets = targets;
try {
@@ -49,7 +48,13 @@ export async function startDmOnFirstMessage(client: MatrixClient, targets: Membe
joining: false,
metricsTrigger: "MessageUser",
});
return existingRoom;
return existingRoom.roomId;
}
if (targets.length === 1 && targets[0] instanceof ThreepidMember && privateShouldBeEncrypted()) {
// Single 3rd-party invite and well-known promotes encryption:
// Directly create a room and invite the other.
return await startDm(client, targets);
}
const room = await createDmLocalRoom(client, resolvedTargets);
@@ -59,7 +64,7 @@ export async function startDmOnFirstMessage(client: MatrixClient, targets: Membe
joining: false,
targets: resolvedTargets,
});
return room;
return room.roomId;
}
/**
@@ -81,6 +86,8 @@ export async function createRoomFromLocalRoom(client: MatrixClient, localRoom: L
return startDm(client, localRoom.targets, false).then(
(roomId) => {
if (!roomId) throw new Error(`startDm for local room ${localRoom.roomId} didn't return a room Id`);
localRoom.actualRoomId = roomId;
return waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
},
@@ -186,6 +193,9 @@ export interface IDMUserTileProps {
*/
export async function determineCreateRoomEncryptionOption(client: MatrixClient, targets: Member[]): Promise<boolean> {
if (privateShouldBeEncrypted()) {
// Enable encryption for a single 3rd party invite.
if (targets.length === 1 && targets[0] instanceof ThreepidMember) return true;
// Check whether all users have uploaded device keys before.
// If so, enable encryption in the new room.
const has3PidMembers = targets.some((t) => t instanceof ThreepidMember);

View File

@@ -15,6 +15,7 @@ limitations under the License.
*/
import { IInvite3PID, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { Optional } from "matrix-events-sdk";
import { Action } from "../../dispatcher/actions";
import { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
@@ -35,7 +36,7 @@ export async function startDm(client: MatrixClient, targets: Member[], showSpinn
const targetIds = targets.map((t) => t.userId);
// Check if there is already a DM with these people and reuse it if possible.
let existingRoom: Room | undefined;
let existingRoom: Optional<Room>;
if (targetIds.length === 1) {
existingRoom = findDMForUser(client, targetIds[0]);
} else {

View File

@@ -0,0 +1,48 @@
/*
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 { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import DMRoomMap from "../DMRoomMap";
import { privateShouldBeEncrypted } from "../rooms";
/**
* Tests whether a DM room with exactly one third-party invite should be encrypted.
* If it should be encrypted, the third-party invitation event is also returned.
*/
export const shouldEncryptRoomWithSingle3rdPartyInvite = (
room: Room,
): { shouldEncrypt: true; inviteEvent: MatrixEvent } | { shouldEncrypt: false; inviteEvent?: undefined } => {
// encryption not promoted via .well-known
if (!privateShouldBeEncrypted()) return { shouldEncrypt: false };
// not a DM room
if (!DMRoomMap.shared().getRoomIds().has(room.roomId)) return { shouldEncrypt: false };
// more than one room member / invite
if (room.getInvitedAndJoinedMemberCount() !== 1) return { shouldEncrypt: false };
const thirdPartyInvites = room.currentState.getStateEvents("m.room.third_party_invite") || [];
if (thirdPartyInvites.length === 1) {
return {
shouldEncrypt: true,
inviteEvent: thirdPartyInvites[0],
};
}
return { shouldEncrypt: false };
};