From e10da0434dba0c7be26f74bfda1922b9aaea201b Mon Sep 17 00:00:00 2001 From: Debdut Chakraborty Date: Wed, 20 Jul 2022 20:35:53 +0530 Subject: [PATCH 1/4] Let apps archive/unarchive rooms --- src/definition/accessors/IRoomBuilder.ts | 22 +++++++++++++--- src/definition/rooms/IRoom.ts | 5 ++-- src/server/accessors/RoomBuilder.ts | 33 +++++++++++++++++------- src/server/rooms/Room.ts | 13 ++++++---- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/definition/accessors/IRoomBuilder.ts b/src/definition/accessors/IRoomBuilder.ts index 4c375f460..a96b49233 100644 --- a/src/definition/accessors/IRoomBuilder.ts +++ b/src/definition/accessors/IRoomBuilder.ts @@ -1,6 +1,6 @@ -import { RocketChatAssociationModel } from '../metadata'; -import { IRoom, RoomType } from '../rooms'; -import { IUser } from '../users'; +import { RocketChatAssociationModel } from "../metadata"; +import { IRoom, RoomType } from "../rooms"; +import { IUser } from "../users"; /** * Interface for building out a room. @@ -8,7 +8,9 @@ import { IUser } from '../users'; * be able to successfully save the room object. */ export interface IRoomBuilder { - kind: RocketChatAssociationModel.ROOM | RocketChatAssociationModel.DISCUSSION; + kind: + | RocketChatAssociationModel.ROOM + | RocketChatAssociationModel.DISCUSSION; /** * Provides a convient way to set the data for the room. @@ -67,6 +69,18 @@ export interface IRoomBuilder { */ getCreator(): IUser; + /** + * Archives/Unarchives the room + * + * @param archived archive status of room + */ + setArchived(archived: IRoom["isArchived"]): IRoomBuilder; + + /** + * Gets the archive status of room + */ + getArchived(): IRoom["isArchived"]; + /** * Adds a user to the room, these are by username until further notice. * diff --git a/src/definition/rooms/IRoom.ts b/src/definition/rooms/IRoom.ts index da106db08..92d6bfe02 100644 --- a/src/definition/rooms/IRoom.ts +++ b/src/definition/rooms/IRoom.ts @@ -1,11 +1,12 @@ -import { IUser } from '../users'; -import { RoomType } from './RoomType'; +import { IUser } from "../users"; +import { RoomType } from "./RoomType"; export interface IRoom { id: string; displayName?: string; slugifiedName: string; type: RoomType; + isArchived: boolean; creator: IUser; /** * @deprecated usernames will be removed on version 2.0.0 diff --git a/src/server/accessors/RoomBuilder.ts b/src/server/accessors/RoomBuilder.ts index 7c3de167d..1b5f80118 100644 --- a/src/server/accessors/RoomBuilder.ts +++ b/src/server/accessors/RoomBuilder.ts @@ -1,10 +1,12 @@ -import { IRoomBuilder } from '../../definition/accessors'; -import { RocketChatAssociationModel } from '../../definition/metadata'; -import { IRoom, RoomType } from '../../definition/rooms'; -import { IUser } from '../../definition/users'; +import { IRoomBuilder } from "../../definition/accessors"; +import { RocketChatAssociationModel } from "../../definition/metadata"; +import { IRoom, RoomType } from "../../definition/rooms"; +import { IUser } from "../../definition/users"; export class RoomBuilder implements IRoomBuilder { - public kind: RocketChatAssociationModel.ROOM | RocketChatAssociationModel.DISCUSSION; + public kind: + | RocketChatAssociationModel.ROOM + | RocketChatAssociationModel.DISCUSSION; protected room: IRoom; private members: Array; @@ -13,6 +15,13 @@ export class RoomBuilder implements IRoomBuilder { this.room = (data ? data : { customFields: {} }) as IRoom; this.members = new Array(); } + setArchived(archived: IRoom["isArchived"]): IRoomBuilder { + this.room.isArchived = archived; + return this; + } + getArchived(): IRoom["isArchived"] { + return this.room.isArchived; + } public setData(data: Partial): IRoomBuilder { delete data.id; @@ -89,7 +98,9 @@ export class RoomBuilder implements IRoomBuilder { return this; } - public setMembersToBeAddedByUsernames(usernames: Array): IRoomBuilder { + public setMembersToBeAddedByUsernames( + usernames: Array + ): IRoomBuilder { this.members = usernames; return this; } @@ -116,7 +127,9 @@ export class RoomBuilder implements IRoomBuilder { return this.room.isReadOnly; } - public setDisplayingOfSystemMessages(displaySystemMessages: boolean): IRoomBuilder { + public setDisplayingOfSystemMessages( + displaySystemMessages: boolean + ): IRoomBuilder { this.room.displaySystemMessages = displaySystemMessages; return this; } @@ -126,7 +139,7 @@ export class RoomBuilder implements IRoomBuilder { } public addCustomField(key: string, value: object): IRoomBuilder { - if (typeof this.room.customFields !== 'object') { + if (typeof this.room.customFields !== "object") { this.room.customFields = {}; } @@ -140,11 +153,11 @@ export class RoomBuilder implements IRoomBuilder { } public getCustomFields(): { [key: string]: object } { - return this.room.customFields; + return this.room.customFields; } public getUserIds(): Array { - return this.room.userIds; + return this.room.userIds; } public getRoom(): IRoom { diff --git a/src/server/rooms/Room.ts b/src/server/rooms/Room.ts index 34ae7c3b4..4eeb8cfeb 100644 --- a/src/server/rooms/Room.ts +++ b/src/server/rooms/Room.ts @@ -1,14 +1,15 @@ -import { IRoom, RoomType } from '../../definition/rooms'; -import { IUser } from '../../definition/users'; -import { AppManager } from '../AppManager'; +import { IRoom, RoomType } from "../../definition/rooms"; +import { IUser } from "../../definition/users"; +import { AppManager } from "../AppManager"; -const PrivateManager = Symbol('RoomPrivateManager'); +const PrivateManager = Symbol("RoomPrivateManager"); export class Room implements IRoom { public id: string; public displayName?: string; public slugifiedName: string; public type: RoomType; + public isArchived: boolean; public creator: IUser; public isDefault?: boolean; public isReadOnly?: boolean; @@ -29,7 +30,9 @@ export class Room implements IRoom { public get usernames(): Array { // Get usernames if (!this._USERNAMES) { - this._USERNAMES = this[PrivateManager].getBridges().getInternalBridge().doGetUsernamesOfRoomById(this.id); + this._USERNAMES = this[PrivateManager].getBridges() + .getInternalBridge() + .doGetUsernamesOfRoomById(this.id); } return this._USERNAMES; From b6da95e6c7d78e01edee40b44ac410a632ddd0cb Mon Sep 17 00:00:00 2001 From: Debdut Chakraborty Date: Wed, 20 Jul 2022 20:44:26 +0530 Subject: [PATCH 2/4] Lint stuff fix --- .prettierrc | 7 ++++++ src/definition/accessors/IRoomBuilder.ts | 14 ++++++------ src/definition/rooms/IRoom.ts | 4 ++-- src/server/accessors/RoomBuilder.ts | 27 ++++++++++-------------- src/server/rooms/Room.ts | 12 +++++------ 5 files changed, 31 insertions(+), 33 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..a6c7e4c2d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "tabWidth": 4, + "useTabs": false, + "singleQuote": true, + "trailingComma": "all", + "printWidth": 160 +} diff --git a/src/definition/accessors/IRoomBuilder.ts b/src/definition/accessors/IRoomBuilder.ts index a96b49233..67a4a6b9a 100644 --- a/src/definition/accessors/IRoomBuilder.ts +++ b/src/definition/accessors/IRoomBuilder.ts @@ -1,6 +1,6 @@ -import { RocketChatAssociationModel } from "../metadata"; -import { IRoom, RoomType } from "../rooms"; -import { IUser } from "../users"; +import { RocketChatAssociationModel } from '../metadata'; +import { IRoom, RoomType } from '../rooms'; +import { IUser } from '../users'; /** * Interface for building out a room. @@ -8,9 +8,7 @@ import { IUser } from "../users"; * be able to successfully save the room object. */ export interface IRoomBuilder { - kind: - | RocketChatAssociationModel.ROOM - | RocketChatAssociationModel.DISCUSSION; + kind: RocketChatAssociationModel.ROOM | RocketChatAssociationModel.DISCUSSION; /** * Provides a convient way to set the data for the room. @@ -74,12 +72,12 @@ export interface IRoomBuilder { * * @param archived archive status of room */ - setArchived(archived: IRoom["isArchived"]): IRoomBuilder; + setArchived(archived: IRoom['isArchived']): IRoomBuilder; /** * Gets the archive status of room */ - getArchived(): IRoom["isArchived"]; + getArchived(): IRoom['isArchived']; /** * Adds a user to the room, these are by username until further notice. diff --git a/src/definition/rooms/IRoom.ts b/src/definition/rooms/IRoom.ts index 92d6bfe02..6f837d611 100644 --- a/src/definition/rooms/IRoom.ts +++ b/src/definition/rooms/IRoom.ts @@ -1,5 +1,5 @@ -import { IUser } from "../users"; -import { RoomType } from "./RoomType"; +import { IUser } from '../users'; +import { RoomType } from './RoomType'; export interface IRoom { id: string; diff --git a/src/server/accessors/RoomBuilder.ts b/src/server/accessors/RoomBuilder.ts index 1b5f80118..c640dd592 100644 --- a/src/server/accessors/RoomBuilder.ts +++ b/src/server/accessors/RoomBuilder.ts @@ -1,12 +1,11 @@ -import { IRoomBuilder } from "../../definition/accessors"; -import { RocketChatAssociationModel } from "../../definition/metadata"; -import { IRoom, RoomType } from "../../definition/rooms"; -import { IUser } from "../../definition/users"; +import { IRoomBuilder } from '../../definition/accessors'; +import { RocketChatAssociationModel } from '../../definition/metadata'; +import { IRoom, RoomType } from '../../definition/rooms'; +import { IUser } from '../../definition/users'; export class RoomBuilder implements IRoomBuilder { - public kind: - | RocketChatAssociationModel.ROOM - | RocketChatAssociationModel.DISCUSSION; + public kind: RocketChatAssociationModel.ROOM | RocketChatAssociationModel.DISCUSSION; + protected room: IRoom; private members: Array; @@ -15,11 +14,11 @@ export class RoomBuilder implements IRoomBuilder { this.room = (data ? data : { customFields: {} }) as IRoom; this.members = new Array(); } - setArchived(archived: IRoom["isArchived"]): IRoomBuilder { + public setArchived(archived: IRoom['isArchived']): IRoomBuilder { this.room.isArchived = archived; return this; } - getArchived(): IRoom["isArchived"] { + public getArchived(): IRoom['isArchived'] { return this.room.isArchived; } @@ -98,9 +97,7 @@ export class RoomBuilder implements IRoomBuilder { return this; } - public setMembersToBeAddedByUsernames( - usernames: Array - ): IRoomBuilder { + public setMembersToBeAddedByUsernames(usernames: Array): IRoomBuilder { this.members = usernames; return this; } @@ -127,9 +124,7 @@ export class RoomBuilder implements IRoomBuilder { return this.room.isReadOnly; } - public setDisplayingOfSystemMessages( - displaySystemMessages: boolean - ): IRoomBuilder { + public setDisplayingOfSystemMessages(displaySystemMessages: boolean): IRoomBuilder { this.room.displaySystemMessages = displaySystemMessages; return this; } @@ -139,7 +134,7 @@ export class RoomBuilder implements IRoomBuilder { } public addCustomField(key: string, value: object): IRoomBuilder { - if (typeof this.room.customFields !== "object") { + if (typeof this.room.customFields !== 'object') { this.room.customFields = {}; } diff --git a/src/server/rooms/Room.ts b/src/server/rooms/Room.ts index 4eeb8cfeb..4efdeb7d4 100644 --- a/src/server/rooms/Room.ts +++ b/src/server/rooms/Room.ts @@ -1,8 +1,8 @@ -import { IRoom, RoomType } from "../../definition/rooms"; -import { IUser } from "../../definition/users"; -import { AppManager } from "../AppManager"; +import { IRoom, RoomType } from '../../definition/rooms'; +import { IUser } from '../../definition/users'; +import { AppManager } from '../AppManager'; -const PrivateManager = Symbol("RoomPrivateManager"); +const PrivateManager = Symbol('RoomPrivateManager'); export class Room implements IRoom { public id: string; @@ -30,9 +30,7 @@ export class Room implements IRoom { public get usernames(): Array { // Get usernames if (!this._USERNAMES) { - this._USERNAMES = this[PrivateManager].getBridges() - .getInternalBridge() - .doGetUsernamesOfRoomById(this.id); + this._USERNAMES = this[PrivateManager].getBridges().getInternalBridge().doGetUsernamesOfRoomById(this.id); } return this._USERNAMES; From c64441ee4f91d12d83f644a1ef1638802783e888 Mon Sep 17 00:00:00 2001 From: Debdut Chakraborty Date: Wed, 20 Jul 2022 21:02:15 +0530 Subject: [PATCH 3/4] tests --- tests/server/accessors/RoomBuilder.spec.ts | 8 ++ tests/test-data/utilities.ts | 114 +++++++++++++-------- 2 files changed, 79 insertions(+), 43 deletions(-) diff --git a/tests/server/accessors/RoomBuilder.spec.ts b/tests/server/accessors/RoomBuilder.spec.ts index a83b37ad2..76d3e81e9 100644 --- a/tests/server/accessors/RoomBuilder.spec.ts +++ b/tests/server/accessors/RoomBuilder.spec.ts @@ -84,6 +84,14 @@ export class RoomBuilderAccessorTestFixture { Expect(room.customFields).toBeEmpty(); Expect(rb.getCustomFields()).toBeEmpty(); + Expect(rb.setArchived(true)).toBe(rb); + Expect(room.isArchived).toBe(true); + Expect(rb.getArchived()).toBe(true); + + Expect(rb.setArchived(false)).toBe(rb); + Expect(room.isArchived).toBe(false); + Expect(rb.getArchived()).toBe(false); + Expect(rb.getRoom()).toBe(room); } } diff --git a/tests/test-data/utilities.ts b/tests/test-data/utilities.ts index 19b8aab01..a3613dfbd 100644 --- a/tests/test-data/utilities.ts +++ b/tests/test-data/utilities.ts @@ -96,6 +96,7 @@ export class TestData { slugifiedName: slugifiedName ? slugifiedName : 'testing-room', displayName: 'Testing Room', type: RoomType.CHANNEL, + isArchived: false, creator: TestData.getUser(), usernames: [TestData.getUser().username], isDefault: true, @@ -121,39 +122,41 @@ export class TestData { emoji: ':see_no_evil:', avatarUrl: 'https://avatars0.githubusercontent.com/u/850391?s=88&v=4', alias: 'Testing Bot', - attachments: [{ - collapsed: false, - color: '#00b2b2', - text: 'Just an attachment that is used for testing', - timestamp: new Date(), - timestampLink: 'https://google.com/', - thumbnailUrl: 'https://avatars0.githubusercontent.com/u/850391?s=88&v=4', - author: { - name: 'Author Name', - link: 'https://github.com/graywolf336', - icon: 'https://avatars0.githubusercontent.com/u/850391?s=88&v=4', - }, - title: { - value: 'Attachment Title', - link: 'https://github.com/RocketChat', - displayDownloadLink: false, - }, - imageUrl: 'https://rocket.chat/images/default/logo.svg', - audioUrl: 'http://www.w3schools.com/tags/horse.mp3', - videoUrl: 'http://www.w3schools.com/tags/movie.mp4', - fields: [ - { - short: true, - title: 'Test', - value: 'Testing out something or other', + attachments: [ + { + collapsed: false, + color: '#00b2b2', + text: 'Just an attachment that is used for testing', + timestamp: new Date(), + timestampLink: 'https://google.com/', + thumbnailUrl: 'https://avatars0.githubusercontent.com/u/850391?s=88&v=4', + author: { + name: 'Author Name', + link: 'https://github.com/graywolf336', + icon: 'https://avatars0.githubusercontent.com/u/850391?s=88&v=4', }, - { - short: true, - title: 'Another Test', - value: '[Link](https://google.com/) something and this and that.', + title: { + value: 'Attachment Title', + link: 'https://github.com/RocketChat', + displayDownloadLink: false, }, - ], - }], + imageUrl: 'https://rocket.chat/images/default/logo.svg', + audioUrl: 'http://www.w3schools.com/tags/horse.mp3', + videoUrl: 'http://www.w3schools.com/tags/movie.mp4', + fields: [ + { + short: true, + title: 'Test', + value: 'Testing out something or other', + }, + { + short: true, + title: 'Another Test', + value: '[Link](https://google.com/) something and this and that.', + }, + ], + }, + ], }; } @@ -173,7 +176,14 @@ export class TestData { items: new Array(), } as ISlashCommandPreview); }, - executePreviewItem: (item: ISlashCommandPreviewItem, context: SlashCommandContext, read: IRead, modify: IModify, http: IHttp, persis: IPersistence): Promise => { + executePreviewItem: ( + item: ISlashCommandPreviewItem, + context: SlashCommandContext, + read: IRead, + modify: IModify, + http: IHttp, + persis: IPersistence, + ): Promise => { return Promise.resolve(); }, }; @@ -183,14 +193,23 @@ export class TestData { return { visibility, security, - endpoints: [{ - path, - get(request: IApiRequest, endpoint: IApiEndpointInfo, read: IRead, modify: IModify, http: IHttp, persis: IPersistence): Promise { - return Promise.resolve({ - status: HttpStatusCode.OK, - }); + endpoints: [ + { + path, + get( + request: IApiRequest, + endpoint: IApiEndpointInfo, + read: IRead, + modify: IModify, + http: IHttp, + persis: IPersistence, + ): Promise { + return Promise.resolve({ + status: HttpStatusCode.OK, + }); + }, }, - }], + ], }; } @@ -322,10 +341,19 @@ export class TestData { } public static getMockApp(id: string, name: string): ProxiedApp { - return new ProxiedApp({} as AppManager, { status: AppStatus.UNKNOWN } as IAppStorageItem, { - getName() { return 'testing'; }, - getID() { return 'testing'; }, - } as App, (mod: string) => mod); + return new ProxiedApp( + {} as AppManager, + { status: AppStatus.UNKNOWN } as IAppStorageItem, + { + getName() { + return 'testing'; + }, + getID() { + return 'testing'; + }, + } as App, + (mod: string) => mod, + ); } } From 2996a5200719b49152b9ac82733e32a2b39799c4 Mon Sep 17 00:00:00 2001 From: Debdut Chakraborty Date: Wed, 24 Aug 2022 02:19:09 +0530 Subject: [PATCH 4/4] New RoomBuilder --- src/definition/accessors/IModifyUpdater.ts | 7 +++ src/definition/accessors/IRoomBuilder.ts | 12 ----- src/definition/accessors/IRoomUpdater.ts | 6 +++ src/server/accessors/ModifyUpdater.ts | 10 ++++- src/server/accessors/RoomBuilder.ts | 7 --- src/server/accessors/RoomUpdater.ts | 13 ++++++ src/server/bridges/RoomBridge.ts | 52 +++++++++++++++++----- tests/server/accessors/RoomBuilder.spec.ts | 8 ---- tests/test-data/bridges/roomBridge.ts | 8 ++++ 9 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 src/definition/accessors/IRoomUpdater.ts create mode 100644 src/server/accessors/RoomUpdater.ts diff --git a/src/definition/accessors/IModifyUpdater.ts b/src/definition/accessors/IModifyUpdater.ts index 6142e184f..7923ac393 100644 --- a/src/definition/accessors/IModifyUpdater.ts +++ b/src/definition/accessors/IModifyUpdater.ts @@ -2,6 +2,7 @@ import { IUser } from '../users'; import { ILivechatUpdater } from './ILivechatUpdater'; import { IMessageBuilder } from './IMessageBuilder'; import { IRoomBuilder } from './IRoomBuilder'; +import { IRoomUpdater } from './IRoomUpdater'; import { IUserUpdater } from './IUserUpdater'; export interface IModifyUpdater { @@ -17,6 +18,12 @@ export interface IModifyUpdater { */ getUserUpdater(): IUserUpdater; + /** + * Get the object responsible for + * the methods that let you edit a room + */ + getRoomUpdater(): IRoomUpdater; + /** * Modifies an existing message. * Raises an exception if a non-existent messageId is supplied diff --git a/src/definition/accessors/IRoomBuilder.ts b/src/definition/accessors/IRoomBuilder.ts index 67a4a6b9a..4c375f460 100644 --- a/src/definition/accessors/IRoomBuilder.ts +++ b/src/definition/accessors/IRoomBuilder.ts @@ -67,18 +67,6 @@ export interface IRoomBuilder { */ getCreator(): IUser; - /** - * Archives/Unarchives the room - * - * @param archived archive status of room - */ - setArchived(archived: IRoom['isArchived']): IRoomBuilder; - - /** - * Gets the archive status of room - */ - getArchived(): IRoom['isArchived']; - /** * Adds a user to the room, these are by username until further notice. * diff --git a/src/definition/accessors/IRoomUpdater.ts b/src/definition/accessors/IRoomUpdater.ts new file mode 100644 index 000000000..aa4603912 --- /dev/null +++ b/src/definition/accessors/IRoomUpdater.ts @@ -0,0 +1,6 @@ +import { IRoom } from '../rooms'; + +export interface IRoomUpdater { + archiveRoom(room: IRoom): Promise; + unarchiveRoom(room: IRoom): Promise; +} diff --git a/src/server/accessors/ModifyUpdater.ts b/src/server/accessors/ModifyUpdater.ts index a564398c1..b92f17199 100644 --- a/src/server/accessors/ModifyUpdater.ts +++ b/src/server/accessors/ModifyUpdater.ts @@ -1,4 +1,5 @@ import { ILivechatUpdater, IMessageBuilder, IModifyUpdater, IRoomBuilder } from '../../definition/accessors'; +import { IRoomUpdater } from '../../definition/accessors/IRoomUpdater'; import { IUserUpdater } from '../../definition/accessors/IUserUpdater'; import { RocketChatAssociationModel } from '../../definition/metadata'; import { RoomType } from '../../definition/rooms'; @@ -7,15 +8,18 @@ import { AppBridges } from '../bridges'; import { LivechatUpdater } from './LivechatUpdater'; import { MessageBuilder } from './MessageBuilder'; import { RoomBuilder } from './RoomBuilder'; +import { RoomUpdater } from './RoomUpdater'; import { UserUpdater } from './UserUpdater'; export class ModifyUpdater implements IModifyUpdater { private livechatUpdater: ILivechatUpdater; private userUpdater: IUserUpdater; + private roomUpdater: IRoomUpdater; constructor(private readonly bridges: AppBridges, private readonly appId: string) { this.livechatUpdater = new LivechatUpdater(this.bridges, this.appId); this.userUpdater = new UserUpdater(this.bridges, this.appId); + this.roomUpdater = new RoomUpdater(this.bridges, this.appId); } public getLivechatUpdater(): ILivechatUpdater { @@ -26,6 +30,10 @@ export class ModifyUpdater implements IModifyUpdater { return this.userUpdater; } + public getRoomUpdater(): IRoomUpdater { + return this.roomUpdater; + } + public async message(messageId: string, updater: IUser): Promise { const msg = await this.bridges.getMessageBridge().doGetById(messageId, this.appId); @@ -53,7 +61,7 @@ export class ModifyUpdater implements IModifyUpdater { const result = builder.getMessage(); if (!result.id) { - throw new Error('Invalid message, can\'t update a message without an id.'); + throw new Error(`Invalid message, can't update a message without an id.`); } if (!result.sender || !result.sender.id) { diff --git a/src/server/accessors/RoomBuilder.ts b/src/server/accessors/RoomBuilder.ts index c640dd592..ef175b1ac 100644 --- a/src/server/accessors/RoomBuilder.ts +++ b/src/server/accessors/RoomBuilder.ts @@ -14,13 +14,6 @@ export class RoomBuilder implements IRoomBuilder { this.room = (data ? data : { customFields: {} }) as IRoom; this.members = new Array(); } - public setArchived(archived: IRoom['isArchived']): IRoomBuilder { - this.room.isArchived = archived; - return this; - } - public getArchived(): IRoom['isArchived'] { - return this.room.isArchived; - } public setData(data: Partial): IRoomBuilder { delete data.id; diff --git a/src/server/accessors/RoomUpdater.ts b/src/server/accessors/RoomUpdater.ts new file mode 100644 index 000000000..ee56edfb7 --- /dev/null +++ b/src/server/accessors/RoomUpdater.ts @@ -0,0 +1,13 @@ +import { IRoomUpdater } from '../../definition/accessors/IRoomUpdater'; +import { IRoom } from '../../definition/rooms'; +import { AppBridges } from '../bridges'; + +export class RoomUpdater implements IRoomUpdater { + constructor(private readonly bridges: AppBridges, private readonly appId: string) {} + public async archiveRoom(room: IRoom): Promise { + return this.bridges.getRoomBridge().doArchiveRoom(room, this.appId); + } + public async unarchiveRoom(room: IRoom): Promise { + return this.bridges.getRoomBridge().doUnarchiveRoom(room, this.appId); + } +} diff --git a/src/server/bridges/RoomBridge.ts b/src/server/bridges/RoomBridge.ts index e81d4935f..bb692aa2e 100644 --- a/src/server/bridges/RoomBridge.ts +++ b/src/server/bridges/RoomBridge.ts @@ -55,8 +55,25 @@ export abstract class RoomBridge extends BaseBridge { } } - public async doCreateDiscussion(room: IRoom, parentMessage: IMessage | undefined, - reply: string | undefined, members: Array, appId: string): Promise { + public async doArchiveRoom(room: IRoom, appId: string): Promise { + if (this.hasWritePermission(appId)) { + return this.archiveRoom(room, appId); + } + } + + public async doUnarchiveRoom(room: IRoom, appId: string): Promise { + if (this.hasWritePermission(appId)) { + return this.unarchiveRoom(room, appId); + } + } + + public async doCreateDiscussion( + room: IRoom, + parentMessage: IMessage | undefined, + reply: string | undefined, + members: Array, + appId: string, + ): Promise { if (this.hasWritePermission(appId)) { return this.createDiscussion(room, parentMessage, reply, members, appId); } @@ -76,19 +93,28 @@ export abstract class RoomBridge extends BaseBridge { protected abstract getDirectByUsernames(usernames: Array, appId: string): Promise; protected abstract getMembers(roomId: string, appId: string): Promise>; protected abstract update(room: IRoom, members: Array, appId: string): Promise; - protected abstract createDiscussion(room: IRoom, parentMessage: IMessage | undefined, - reply: string | undefined, members: Array, appId: string): Promise; + protected abstract createDiscussion( + room: IRoom, + parentMessage: IMessage | undefined, + reply: string | undefined, + members: Array, + appId: string, + ): Promise; protected abstract delete(room: string, appId: string): Promise; + protected abstract archiveRoom(room: IRoom, appId: string): Promise; + protected abstract unarchiveRoom(room: IRoom, appId: string): Promise; private hasWritePermission(appId: string): boolean { if (AppPermissionManager.hasPermission(appId, AppPermissions.room.write)) { return true; } - AppPermissionManager.notifyAboutError(new PermissionDeniedError({ - appId, - missingPermissions: [AppPermissions.room.write], - })); + AppPermissionManager.notifyAboutError( + new PermissionDeniedError({ + appId, + missingPermissions: [AppPermissions.room.write], + }), + ); return false; } @@ -98,10 +124,12 @@ export abstract class RoomBridge extends BaseBridge { return true; } - AppPermissionManager.notifyAboutError(new PermissionDeniedError({ - appId, - missingPermissions: [AppPermissions.room.read], - })); + AppPermissionManager.notifyAboutError( + new PermissionDeniedError({ + appId, + missingPermissions: [AppPermissions.room.read], + }), + ); return false; } diff --git a/tests/server/accessors/RoomBuilder.spec.ts b/tests/server/accessors/RoomBuilder.spec.ts index 76d3e81e9..a83b37ad2 100644 --- a/tests/server/accessors/RoomBuilder.spec.ts +++ b/tests/server/accessors/RoomBuilder.spec.ts @@ -84,14 +84,6 @@ export class RoomBuilderAccessorTestFixture { Expect(room.customFields).toBeEmpty(); Expect(rb.getCustomFields()).toBeEmpty(); - Expect(rb.setArchived(true)).toBe(rb); - Expect(room.isArchived).toBe(true); - Expect(rb.getArchived()).toBe(true); - - Expect(rb.setArchived(false)).toBe(rb); - Expect(room.isArchived).toBe(false); - Expect(rb.getArchived()).toBe(false); - Expect(rb.getRoom()).toBe(room); } } diff --git a/tests/test-data/bridges/roomBridge.ts b/tests/test-data/bridges/roomBridge.ts index b28103a22..09488eb64 100644 --- a/tests/test-data/bridges/roomBridge.ts +++ b/tests/test-data/bridges/roomBridge.ts @@ -43,4 +43,12 @@ export class TestsRoomBridge extends RoomBridge { public delete(roomId: string, appId: string): Promise { throw new Error('Method not implemented.'); } + + protected archiveRoom(room: IRoom, appId: string): Promise { + throw new Error('Method not implemented.'); + } + + protected unarchiveRoom(room: IRoom, appId: string): Promise { + throw new Error('Method not implemented.'); + } }