Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] ability to archive and unarchive rooms #535

Open
wants to merge 4 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 4,
"useTabs": false,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 160
}
7 changes: 7 additions & 0 deletions src/definition/accessors/IModifyUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/definition/accessors/IRoomUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IRoom } from '../rooms';

export interface IRoomUpdater {
archiveRoom(room: IRoom): Promise<boolean>;
unarchiveRoom(room: IRoom): Promise<boolean>;
}
1 change: 1 addition & 0 deletions src/definition/rooms/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IRoom {
displayName?: string;
slugifiedName: string;
type: RoomType;
isArchived: boolean;
creator: IUser;
/**
* @deprecated usernames will be removed on version 2.0.0
Expand Down
10 changes: 9 additions & 1 deletion src/server/accessors/ModifyUpdater.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand All @@ -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<IMessageBuilder> {
const msg = await this.bridges.getMessageBridge().doGetById(messageId, this.appId);

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/server/accessors/RoomBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IUser } from '../../definition/users';

export class RoomBuilder implements IRoomBuilder {
public kind: RocketChatAssociationModel.ROOM | RocketChatAssociationModel.DISCUSSION;

protected room: IRoom;
private members: Array<string>;

Expand Down Expand Up @@ -140,11 +141,11 @@ export class RoomBuilder implements IRoomBuilder {
}

public getCustomFields(): { [key: string]: object } {
return this.room.customFields;
return this.room.customFields;
}

public getUserIds(): Array<string> {
return this.room.userIds;
return this.room.userIds;
}

public getRoom(): IRoom {
Expand Down
13 changes: 13 additions & 0 deletions src/server/accessors/RoomUpdater.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
return this.bridges.getRoomBridge().doArchiveRoom(room, this.appId);
}
public async unarchiveRoom(room: IRoom): Promise<boolean> {
return this.bridges.getRoomBridge().doUnarchiveRoom(room, this.appId);
}
}
52 changes: 40 additions & 12 deletions src/server/bridges/RoomBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,25 @@ export abstract class RoomBridge extends BaseBridge {
}
}

public async doCreateDiscussion(room: IRoom, parentMessage: IMessage | undefined,
reply: string | undefined, members: Array<string>, appId: string): Promise<string> {
public async doArchiveRoom(room: IRoom, appId: string): Promise<boolean> {
if (this.hasWritePermission(appId)) {
return this.archiveRoom(room, appId);
}
}

public async doUnarchiveRoom(room: IRoom, appId: string): Promise<boolean> {
if (this.hasWritePermission(appId)) {
return this.unarchiveRoom(room, appId);
}
}

public async doCreateDiscussion(
room: IRoom,
parentMessage: IMessage | undefined,
reply: string | undefined,
members: Array<string>,
appId: string,
): Promise<string> {
if (this.hasWritePermission(appId)) {
return this.createDiscussion(room, parentMessage, reply, members, appId);
}
Expand All @@ -76,19 +93,28 @@ export abstract class RoomBridge extends BaseBridge {
protected abstract getDirectByUsernames(usernames: Array<string>, appId: string): Promise<IRoom | undefined>;
protected abstract getMembers(roomId: string, appId: string): Promise<Array<IUser>>;
protected abstract update(room: IRoom, members: Array<string>, appId: string): Promise<void>;
protected abstract createDiscussion(room: IRoom, parentMessage: IMessage | undefined,
reply: string | undefined, members: Array<string>, appId: string): Promise<string>;
protected abstract createDiscussion(
room: IRoom,
parentMessage: IMessage | undefined,
reply: string | undefined,
members: Array<string>,
appId: string,
): Promise<string>;
protected abstract delete(room: string, appId: string): Promise<void>;
protected abstract archiveRoom(room: IRoom, appId: string): Promise<boolean>;
protected abstract unarchiveRoom(room: IRoom, appId: string): Promise<boolean>;

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;
}
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/server/rooms/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class Room implements IRoom {
public displayName?: string;
public slugifiedName: string;
public type: RoomType;
public isArchived: boolean;
public creator: IUser;
public isDefault?: boolean;
public isReadOnly?: boolean;
Expand Down
8 changes: 8 additions & 0 deletions tests/test-data/bridges/roomBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ export class TestsRoomBridge extends RoomBridge {
public delete(roomId: string, appId: string): Promise<void> {
throw new Error('Method not implemented.');
}

protected archiveRoom(room: IRoom, appId: string): Promise<boolean> {
throw new Error('Method not implemented.');
}

protected unarchiveRoom(room: IRoom, appId: string): Promise<boolean> {
throw new Error('Method not implemented.');
}
}
114 changes: 71 additions & 43 deletions tests/test-data/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.',
},
],
},
],
};
}

Expand All @@ -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<void> => {
executePreviewItem: (
item: ISlashCommandPreviewItem,
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<void> => {
return Promise.resolve();
},
};
Expand All @@ -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<IApiResponse> {
return Promise.resolve({
status: HttpStatusCode.OK,
});
endpoints: [
{
path,
get(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<IApiResponse> {
return Promise.resolve({
status: HttpStatusCode.OK,
});
},
},
}],
],
};
}

Expand Down Expand Up @@ -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,
);
}
}

Expand Down