From 8526461d3c28abab2b55177e5aa98012eaa0493d Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 10 Feb 2022 16:39:54 +0100 Subject: [PATCH] split up create code into separate files --- src/domain/session/CreateRoomViewModel.js | 2 +- src/domain/session/RoomViewModelObservable.js | 2 +- .../rightpanel/MemberDetailsViewModel.js | 2 +- src/matrix/Session.js | 4 +- src/matrix/common.js | 2 +- src/matrix/profile.ts | 51 +++++++++++++++++++ .../room/{create.ts => RoomBeingCreated.ts} | 41 +-------------- src/matrix/room/RoomStatus.ts | 24 --------- src/matrix/room/{common.js => common.ts} | 15 ++++++ src/matrix/room/members/RoomMember.js | 2 +- src/matrix/room/sending/PendingEvent.js | 2 +- src/matrix/room/sending/SendQueue.js | 2 +- src/matrix/room/timeline/Timeline.js | 2 +- .../room/timeline/entries/BaseEventEntry.js | 2 +- .../room/timeline/entries/EventEntry.js | 2 +- .../timeline/persistence/RelationWriter.js | 2 +- src/matrix/room/timeline/relations.js | 2 +- 17 files changed, 82 insertions(+), 77 deletions(-) create mode 100644 src/matrix/profile.ts rename src/matrix/room/{create.ts => RoomBeingCreated.ts} (86%) delete mode 100644 src/matrix/room/RoomStatus.ts rename src/matrix/room/{common.js => common.ts} (81%) diff --git a/src/domain/session/CreateRoomViewModel.js b/src/domain/session/CreateRoomViewModel.js index 6e207e31..9d2835c2 100644 --- a/src/domain/session/CreateRoomViewModel.js +++ b/src/domain/session/CreateRoomViewModel.js @@ -16,7 +16,7 @@ limitations under the License. import {ViewModel} from "../ViewModel.js"; import {imageToInfo} from "./common.js"; -import {RoomType} from "../../matrix/room/create"; +import {RoomType} from "../../matrix/room/common"; export class CreateRoomViewModel extends ViewModel { constructor(options) { diff --git a/src/domain/session/RoomViewModelObservable.js b/src/domain/session/RoomViewModelObservable.js index 8fd0daf3..52833332 100644 --- a/src/domain/session/RoomViewModelObservable.js +++ b/src/domain/session/RoomViewModelObservable.js @@ -15,7 +15,7 @@ limitations under the License. */ import {ObservableValue} from "../../observable/ObservableValue"; -import {RoomStatus} from "../../matrix/room/RoomStatus"; +import {RoomStatus} from "../../matrix/room/common"; /** Depending on the status of a room (invited, joined, archived, or none), diff --git a/src/domain/session/rightpanel/MemberDetailsViewModel.js b/src/domain/session/rightpanel/MemberDetailsViewModel.js index f5169afa..f6cbd747 100644 --- a/src/domain/session/rightpanel/MemberDetailsViewModel.js +++ b/src/domain/session/rightpanel/MemberDetailsViewModel.js @@ -15,7 +15,7 @@ limitations under the License. */ import {ViewModel} from "../../ViewModel.js"; -import {RoomType} from "../../../matrix/room/create"; +import {RoomType} from "../../../matrix/room/common"; import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar.js"; export class MemberDetailsViewModel extends ViewModel { diff --git a/src/matrix/Session.js b/src/matrix/Session.js index a5f48bf3..c5b5ad0d 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -17,8 +17,8 @@ limitations under the License. import {Room} from "./room/Room.js"; import {ArchivedRoom} from "./room/ArchivedRoom.js"; -import {RoomStatus} from "./room/RoomStatus"; -import {RoomBeingCreated} from "./room/create"; +import {RoomStatus} from "./room/common"; +import {RoomBeingCreated} from "./room/RoomBeingCreated"; import {Invite} from "./room/Invite.js"; import {Pusher} from "./push/Pusher"; import { ObservableMap } from "../observable/index.js"; diff --git a/src/matrix/common.js b/src/matrix/common.js index 67a95205..ba7876ed 100644 --- a/src/matrix/common.js +++ b/src/matrix/common.js @@ -34,4 +34,4 @@ export function tests() { assert(!isTxnId("$yS_n5n3cIO2aTtek0_2ZSlv-7g4YYR2zKrk2mFCW_rm")); }, } -} \ No newline at end of file +} diff --git a/src/matrix/profile.ts b/src/matrix/profile.ts new file mode 100644 index 00000000..a95b2139 --- /dev/null +++ b/src/matrix/profile.ts @@ -0,0 +1,51 @@ +/* +Copyright 2020 Bruno Windels + +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 type {HomeServerApi} from "./net/HomeServerApi"; +import type {ILogItem} from "../logging/types"; + +export async function loadProfiles(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise { + const profiles = await Promise.all(userIds.map(async userId => { + const response = await hsApi.profile(userId, {log}).response(); + return new Profile(userId, response.displayname as string, response.avatar_url as string); + })); + profiles.sort((a, b) => a.name.localeCompare(b.name)); + return profiles; +} + +export interface IProfile { + get userId(): string; + get displayName(): string | undefined; + get avatarUrl(): string | undefined; + get name(): string; +} + +export class Profile implements IProfile { + constructor( + public readonly userId: string, + public readonly displayName: string, + public readonly avatarUrl: string | undefined + ) {} + + get name() { return this.displayName || this.userId; } +} + +export class UserIdProfile implements IProfile { + constructor(public readonly userId: string) {} + get displayName() { return undefined; } + get name() { return this.userId; } + get avatarUrl() { return undefined; } +} diff --git a/src/matrix/room/create.ts b/src/matrix/room/RoomBeingCreated.ts similarity index 86% rename from src/matrix/room/create.ts rename to src/matrix/room/RoomBeingCreated.ts index c50aba6b..1ad84a47 100644 --- a/src/matrix/room/create.ts +++ b/src/matrix/room/RoomBeingCreated.ts @@ -19,6 +19,8 @@ import {createRoomEncryptionEvent} from "../e2ee/common"; import {MediaRepository} from "../net/MediaRepository"; import {EventEmitter} from "../../utils/EventEmitter"; import {AttachmentUpload} from "./AttachmentUpload"; +import {loadProfiles, Profile, UserIdProfile} from "../profile"; +import {RoomType} from "./common"; import type {HomeServerApi} from "../net/HomeServerApi"; import type {ILogItem} from "../../logging/types"; @@ -60,12 +62,6 @@ type Options = { alias?: string; } -export enum RoomType { - DirectMessage, - Private, - Public -} - function defaultE2EEStatusForType(type: RoomType): boolean { switch (type) { case RoomType.DirectMessage: @@ -222,36 +218,3 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } } } - -export async function loadProfiles(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise { - const profiles = await Promise.all(userIds.map(async userId => { - const response = await hsApi.profile(userId, {log}).response(); - return new Profile(userId, response.displayname as string, response.avatar_url as string); - })); - profiles.sort((a, b) => a.name.localeCompare(b.name)); - return profiles; -} - -interface IProfile { - get userId(): string; - get displayName(): string | undefined; - get avatarUrl(): string | undefined; - get name(): string; -} - -export class Profile implements IProfile { - constructor( - public readonly userId: string, - public readonly displayName: string, - public readonly avatarUrl: string | undefined - ) {} - - get name() { return this.displayName || this.userId; } -} - -class UserIdProfile implements IProfile { - constructor(public readonly userId: string) {} - get displayName() { return undefined; } - get name() { return this.userId; } - get avatarUrl() { return undefined; } -} diff --git a/src/matrix/room/RoomStatus.ts b/src/matrix/room/RoomStatus.ts deleted file mode 100644 index f66f59d7..00000000 --- a/src/matrix/room/RoomStatus.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* -Copyright 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. -*/ - -export enum RoomStatus { - None = 1 << 0, - BeingCreated = 1 << 1, - Invited = 1 << 2, - Joined = 1 << 3, - Replaced = 1 << 4, - Archived = 1 << 5, -} diff --git a/src/matrix/room/common.js b/src/matrix/room/common.ts similarity index 81% rename from src/matrix/room/common.js rename to src/matrix/room/common.ts index b009a89c..57ab7023 100644 --- a/src/matrix/room/common.js +++ b/src/matrix/room/common.ts @@ -25,3 +25,18 @@ export const REDACTION_TYPE = "m.room.redaction"; export function isRedacted(event) { return !!event?.unsigned?.redacted_because; } + +export enum RoomStatus { + None = 1 << 0, + BeingCreated = 1 << 1, + Invited = 1 << 2, + Joined = 1 << 3, + Replaced = 1 << 4, + Archived = 1 << 5, +} + +export enum RoomType { + DirectMessage, + Private, + Public +} diff --git a/src/matrix/room/members/RoomMember.js b/src/matrix/room/members/RoomMember.js index 78096060..dabff972 100644 --- a/src/matrix/room/members/RoomMember.js +++ b/src/matrix/room/members/RoomMember.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {getPrevContentFromStateEvent} from "../common.js"; +import {getPrevContentFromStateEvent} from "../common"; export const EVENT_TYPE = "m.room.member"; diff --git a/src/matrix/room/sending/PendingEvent.js b/src/matrix/room/sending/PendingEvent.js index d4c10704..1b3567a7 100644 --- a/src/matrix/room/sending/PendingEvent.js +++ b/src/matrix/room/sending/PendingEvent.js @@ -15,7 +15,7 @@ limitations under the License. */ import {createEnum} from "../../../utils/enum"; import {AbortError} from "../../../utils/error"; -import {REDACTION_TYPE} from "../common.js"; +import {REDACTION_TYPE} from "../common"; import {getRelationFromContent, getRelationTarget, setRelationTarget} from "../timeline/relations.js"; export const SendStatus = createEnum( diff --git a/src/matrix/room/sending/SendQueue.js b/src/matrix/room/sending/SendQueue.js index 791ed854..f9950c01 100644 --- a/src/matrix/room/sending/SendQueue.js +++ b/src/matrix/room/sending/SendQueue.js @@ -18,7 +18,7 @@ import {SortedArray} from "../../../observable/list/SortedArray"; import {ConnectionError} from "../../error.js"; import {PendingEvent, SendStatus} from "./PendingEvent.js"; import {makeTxnId, isTxnId} from "../../common.js"; -import {REDACTION_TYPE} from "../common.js"; +import {REDACTION_TYPE} from "../common"; import {getRelationFromContent, getRelationTarget, setRelationTarget, REACTION_TYPE, ANNOTATION_RELATION_TYPE} from "../timeline/relations.js"; export class SendQueue { diff --git a/src/matrix/room/timeline/Timeline.js b/src/matrix/room/timeline/Timeline.js index 90ec29eb..19e9e647 100644 --- a/src/matrix/room/timeline/Timeline.js +++ b/src/matrix/room/timeline/Timeline.js @@ -22,7 +22,7 @@ import {TimelineReader} from "./persistence/TimelineReader.js"; import {PendingEventEntry} from "./entries/PendingEventEntry.js"; import {RoomMember} from "../members/RoomMember.js"; import {getRelation, ANNOTATION_RELATION_TYPE} from "./relations.js"; -import {REDACTION_TYPE} from "../common.js"; +import {REDACTION_TYPE} from "../common"; import {NonPersistedEventEntry} from "./entries/NonPersistedEventEntry.js"; import {DecryptionSource} from "../../e2ee/common.js"; import {EVENT_TYPE as MEMBER_EVENT_TYPE} from "../members/RoomMember.js"; diff --git a/src/matrix/room/timeline/entries/BaseEventEntry.js b/src/matrix/room/timeline/entries/BaseEventEntry.js index ec20f48a..44fdcaec 100644 --- a/src/matrix/room/timeline/entries/BaseEventEntry.js +++ b/src/matrix/room/timeline/entries/BaseEventEntry.js @@ -15,7 +15,7 @@ limitations under the License. */ import {BaseEntry} from "./BaseEntry"; -import {REDACTION_TYPE} from "../../common.js"; +import {REDACTION_TYPE} from "../../common"; import {createAnnotation, ANNOTATION_RELATION_TYPE, getRelationFromContent} from "../relations.js"; import {PendingAnnotation} from "../PendingAnnotation.js"; import {createReplyContent} from "./reply.js" diff --git a/src/matrix/room/timeline/entries/EventEntry.js b/src/matrix/room/timeline/entries/EventEntry.js index 7b957e01..d218a598 100644 --- a/src/matrix/room/timeline/entries/EventEntry.js +++ b/src/matrix/room/timeline/entries/EventEntry.js @@ -15,7 +15,7 @@ limitations under the License. */ import {BaseEventEntry} from "./BaseEventEntry.js"; -import {getPrevContentFromStateEvent, isRedacted} from "../../common.js"; +import {getPrevContentFromStateEvent, isRedacted} from "../../common"; import {getRelationFromContent, getRelatedEventId} from "../relations.js"; export class EventEntry extends BaseEventEntry { diff --git a/src/matrix/room/timeline/persistence/RelationWriter.js b/src/matrix/room/timeline/persistence/RelationWriter.js index 92f97671..ae078bfc 100644 --- a/src/matrix/room/timeline/persistence/RelationWriter.js +++ b/src/matrix/room/timeline/persistence/RelationWriter.js @@ -15,7 +15,7 @@ limitations under the License. */ import {EventEntry} from "../entries/EventEntry.js"; -import {REDACTION_TYPE, isRedacted} from "../../common.js"; +import {REDACTION_TYPE, isRedacted} from "../../common"; import {ANNOTATION_RELATION_TYPE, getRelation} from "../relations.js"; import {redactEvent} from "../common.js"; diff --git a/src/matrix/room/timeline/relations.js b/src/matrix/room/timeline/relations.js index 4009d8c4..2183a6c5 100644 --- a/src/matrix/room/timeline/relations.js +++ b/src/matrix/room/timeline/relations.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {REDACTION_TYPE} from "../common.js"; +import {REDACTION_TYPE} from "../common"; export const REACTION_TYPE = "m.reaction"; export const ANNOTATION_RELATION_TYPE = "m.annotation";