Apply prettier formatting
This commit is contained in:
@@ -30,28 +30,27 @@ import { determineCreateRoomEncryptionOption, Member } from "../../../src/utils/
|
||||
* @param {Member[]} targets DM partners
|
||||
* @returns {Promise<LocalRoom>} Resolves to the new local room
|
||||
*/
|
||||
export async function createDmLocalRoom(
|
||||
client: MatrixClient,
|
||||
targets: Member[],
|
||||
): Promise<LocalRoom> {
|
||||
export async function createDmLocalRoom(client: MatrixClient, targets: Member[]): Promise<LocalRoom> {
|
||||
const userId = client.getUserId();
|
||||
|
||||
const localRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + client.makeTxnId(), client, userId);
|
||||
const events = [];
|
||||
|
||||
events.push(new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomCreate,
|
||||
content: {
|
||||
creator: userId,
|
||||
room_version: KNOWN_SAFE_ROOM_VERSION,
|
||||
},
|
||||
state_key: "",
|
||||
user_id: userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
origin_server_ts: Date.now(),
|
||||
}));
|
||||
events.push(
|
||||
new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomCreate,
|
||||
content: {
|
||||
creator: userId,
|
||||
room_version: KNOWN_SAFE_ROOM_VERSION,
|
||||
},
|
||||
state_key: "",
|
||||
user_id: userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
origin_server_ts: Date.now(),
|
||||
}),
|
||||
);
|
||||
|
||||
if (await determineCreateRoomEncryptionOption(client, targets)) {
|
||||
localRoom.encrypted = true;
|
||||
@@ -71,45 +70,51 @@ export async function createDmLocalRoom(
|
||||
);
|
||||
}
|
||||
|
||||
events.push(new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomMember,
|
||||
content: {
|
||||
displayname: userId,
|
||||
membership: "join",
|
||||
},
|
||||
state_key: userId,
|
||||
user_id: userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
}));
|
||||
|
||||
targets.forEach((target: Member) => {
|
||||
events.push(new MatrixEvent({
|
||||
events.push(
|
||||
new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomMember,
|
||||
content: {
|
||||
displayname: target.name,
|
||||
avatar_url: target.getMxcAvatarUrl(),
|
||||
membership: "invite",
|
||||
isDirect: true,
|
||||
},
|
||||
state_key: target.userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
}));
|
||||
events.push(new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomMember,
|
||||
content: {
|
||||
displayname: target.name,
|
||||
avatar_url: target.getMxcAvatarUrl(),
|
||||
displayname: userId,
|
||||
membership: "join",
|
||||
},
|
||||
state_key: target.userId,
|
||||
sender: target.userId,
|
||||
state_key: userId,
|
||||
user_id: userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
}));
|
||||
}),
|
||||
);
|
||||
|
||||
targets.forEach((target: Member) => {
|
||||
events.push(
|
||||
new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomMember,
|
||||
content: {
|
||||
displayname: target.name,
|
||||
avatar_url: target.getMxcAvatarUrl(),
|
||||
membership: "invite",
|
||||
isDirect: true,
|
||||
},
|
||||
state_key: target.userId,
|
||||
sender: userId,
|
||||
room_id: localRoom.roomId,
|
||||
}),
|
||||
);
|
||||
events.push(
|
||||
new MatrixEvent({
|
||||
event_id: `~${localRoom.roomId}:${client.makeTxnId()}`,
|
||||
type: EventType.RoomMember,
|
||||
content: {
|
||||
displayname: target.name,
|
||||
avatar_url: target.getMxcAvatarUrl(),
|
||||
membership: "join",
|
||||
},
|
||||
state_key: target.userId,
|
||||
sender: target.userId,
|
||||
room_id: localRoom.roomId,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
localRoom.targets = targets;
|
||||
|
||||
@@ -30,29 +30,30 @@ import { getFunctionalMembers } from "../room/getFunctionalMembers";
|
||||
*/
|
||||
export function findDMForUser(client: MatrixClient, userId: string): Room {
|
||||
const roomIds = DMRoomMap.shared().getDMRoomsForUserId(userId);
|
||||
const rooms = roomIds.map(id => client.getRoom(id));
|
||||
const suitableDMRooms = rooms.filter(r => {
|
||||
// Validate that we are joined and the other person is also joined. We'll also make sure
|
||||
// that the room also looks like a DM (until we have canonical DMs to tell us). For now,
|
||||
// a DM is a room of two people that contains those two people exactly. This does mean
|
||||
// that bots, assistants, etc will ruin a room's DM-ness, though this is a problem for
|
||||
// canonical DMs to solve.
|
||||
if (r && r.getMyMembership() === "join") {
|
||||
if (isLocalRoom(r)) return false;
|
||||
const rooms = roomIds.map((id) => client.getRoom(id));
|
||||
const suitableDMRooms = rooms
|
||||
.filter((r) => {
|
||||
// Validate that we are joined and the other person is also joined. We'll also make sure
|
||||
// that the room also looks like a DM (until we have canonical DMs to tell us). For now,
|
||||
// a DM is a room of two people that contains those two people exactly. This does mean
|
||||
// that bots, assistants, etc will ruin a room's DM-ness, though this is a problem for
|
||||
// canonical DMs to solve.
|
||||
if (r && r.getMyMembership() === "join") {
|
||||
if (isLocalRoom(r)) return false;
|
||||
|
||||
const functionalUsers = getFunctionalMembers(r);
|
||||
const members = r.currentState.getMembers();
|
||||
const joinedMembers = members.filter(
|
||||
m => !functionalUsers.includes(m.userId) && isJoinedOrNearlyJoined(m.membership),
|
||||
);
|
||||
const otherMember = joinedMembers.find(m => m.userId === userId);
|
||||
return otherMember && joinedMembers.length === 2;
|
||||
}
|
||||
return false;
|
||||
}).sort((r1, r2) => {
|
||||
return r2.getLastActiveTimestamp() -
|
||||
r1.getLastActiveTimestamp();
|
||||
});
|
||||
const functionalUsers = getFunctionalMembers(r);
|
||||
const members = r.currentState.getMembers();
|
||||
const joinedMembers = members.filter(
|
||||
(m) => !functionalUsers.includes(m.userId) && isJoinedOrNearlyJoined(m.membership),
|
||||
);
|
||||
const otherMember = joinedMembers.find((m) => m.userId === userId);
|
||||
return otherMember && joinedMembers.length === 2;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.sort((r1, r2) => {
|
||||
return r2.getLastActiveTimestamp() - r1.getLastActiveTimestamp();
|
||||
});
|
||||
if (suitableDMRooms.length) {
|
||||
return suitableDMRooms[0];
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import { findDMForUser } from "./findDMForUser";
|
||||
* @returns {Room | null} Resolved so the room if found, else null
|
||||
*/
|
||||
export function findDMRoom(client: MatrixClient, targets: Member[]): Room | null {
|
||||
const targetIds = targets.map(t => t.userId);
|
||||
const targetIds = targets.map((t) => t.userId);
|
||||
let existingRoom: Room;
|
||||
if (targetIds.length === 1) {
|
||||
existingRoom = findDMForUser(client, targetIds[0]);
|
||||
|
||||
@@ -32,7 +32,7 @@ import createRoom from "../../createRoom";
|
||||
* @returns {Promise<string | null} Resolves to the room id.
|
||||
*/
|
||||
export async function startDm(client: MatrixClient, targets: Member[], showSpinner = true): Promise<string | null> {
|
||||
const targetIds = targets.map(t => t.userId);
|
||||
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;
|
||||
@@ -69,14 +69,14 @@ export async function startDm(client: MatrixClient, targets: Member[], showSpinn
|
||||
createRoomOptions.createOpts = targetIds.reduce(
|
||||
(roomOptions, address) => {
|
||||
const type = getAddressType(address);
|
||||
if (type === 'email') {
|
||||
if (type === "email") {
|
||||
const invite: IInvite3PID = {
|
||||
id_server: client.getIdentityServerUrl(true),
|
||||
medium: 'email',
|
||||
medium: "email",
|
||||
address,
|
||||
};
|
||||
roomOptions.invite_3pid.push(invite);
|
||||
} else if (type === 'mx-user-id') {
|
||||
} else if (type === "mx-user-id") {
|
||||
roomOptions.invite.push(address);
|
||||
}
|
||||
return roomOptions;
|
||||
|
||||
Reference in New Issue
Block a user