Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Revert "feat: add dm notify method (#660)" (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert authored Sep 11, 2023
1 parent dbb7e7c commit b2a842d
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 93 deletions.
10 changes: 0 additions & 10 deletions src/definition/accessors/INotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ export interface INotifier {
*/
notifyRoom(room: IRoom, message: IMessage): Promise<void>;

/**
* Sends a direct message to a user.
* @param user The user to send the message to
* @param partialMsg The partial message to send
* @returns A Promise that resolves when the message has been sent
* @throws {Error} if the room could not be created
* @throws {Error} if the message could not be sent
*/
sendDirectMessage(user: IUser, partialMsg: Omit<IMessage, 'room'>): Promise<void>;

/**
* Notifies all of the users a typing indicator in the provided scope.
*
Expand Down
6 changes: 1 addition & 5 deletions src/definition/messages/IMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Block } from '@rocket.chat/ui-kit';

import type { IRoom, RoomType } from '../rooms';
import type { IRoom } from '../rooms';
import type { IBlock } from '../uikit';
import type { IUser, IUserLookup } from '../users';
import type { IMessageAttachment } from './IMessageAttachment';
Expand Down Expand Up @@ -32,7 +32,3 @@ export interface IMessage {
pinnedAt?: Date;
pinnedBy?: IUserLookup;
}

export interface IDirectMessage extends Omit<IMessage, 'room'> {
room: Pick<IRoom, 'id' | 'creator'> & { type: RoomType.DIRECT_MESSAGE };
}
3 changes: 1 addition & 2 deletions src/definition/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDirectMessage, IMessage } from './IMessage';
import { IMessage } from './IMessage';
import { IMessageAction } from './IMessageAction';
import { IMessageAttachment } from './IMessageAttachment';
import { IMessageAttachmentAuthor } from './IMessageAttachmentAuthor';
Expand Down Expand Up @@ -32,7 +32,6 @@ import { MessageActionType } from './MessageActionType';
import { MessageProcessingType } from './MessageProcessingType';

export {
IDirectMessage,
IMessage,
IMessageAttachment,
IMessageAttachmentAuthor,
Expand Down
2 changes: 1 addition & 1 deletion src/server/accessors/Modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Modify implements IModify {
this.deleter = new ModifyDeleter(this.bridges, this.appId);
this.updater = new ModifyUpdater(this.bridges, this.appId);
this.extender = new ModifyExtender(this.bridges, this.appId);
this.notifier = new Notifier(this.bridges.getUserBridge(), this.bridges.getMessageBridge(), this.bridges.getRoomBridge(), this.appId);
this.notifier = new Notifier(this.bridges.getUserBridge(), this.bridges.getMessageBridge(), this.appId);
this.uiController = new UIController(this.appId, this.bridges);
this.scheduler = new SchedulerModify(this.bridges.getSchedulerBridge(), this.appId);
this.oauthApps = new OAuthAppsModify(this.bridges.getOAuthAppsBridge(), this.appId);
Expand Down
65 changes: 4 additions & 61 deletions src/server/accessors/Notifier.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import type { IMessageBuilder, INotifier } from '../../definition/accessors';
import type { ITypingOptions } from '../../definition/accessors/INotifier';
import { TypingScope } from '../../definition/accessors/INotifier';
import type { IDirectMessage, IMessage } from '../../definition/messages';
import { RoomType, type IRoom } from '../../definition/rooms';
import type { IMessage } from '../../definition/messages';
import type { IRoom } from '../../definition/rooms';
import type { IUser } from '../../definition/users';
import type { MessageBridge, RoomBridge, UserBridge } from '../bridges';
import type { MessageBridge, UserBridge } from '../bridges';
import { MessageBuilder } from './MessageBuilder';

export class Notifier implements INotifier {
constructor(
private readonly userBridge: UserBridge,
private readonly msgBridge: MessageBridge,
private readonly roomBridge: RoomBridge,
private readonly appId: string,
) {}
constructor(private readonly userBridge: UserBridge, private readonly msgBridge: MessageBridge, private readonly appId: string) {}

public async notifyUser(user: IUser, message: IMessage): Promise<void> {
if (!message.sender || !message.sender.id) {
Expand All @@ -35,30 +30,6 @@ export class Notifier implements INotifier {
await this.msgBridge.doNotifyRoom(room, message, this.appId);
}

/**
* Sends a direct message to a user.
*
* @param {IUser} user - The user to send the message to.
* @param {Omit<IMessage, 'room'>} partialMsg - The partial message to send, without `room` parameter.
* @returns {Promise<void>} A Promise that resolves when the message has been sent.
*/
public async sendDirectMessage(user: IUser, partialMsg: Omit<IMessage, 'room'>): Promise<void> {
const sender = partialMsg.sender || (await this.userBridge.doGetAppUser(this.appId));
const dmRoom = (await this.roomBridge.doGetDirectByUsernames([user.username, sender.username], this.appId)) || (await this.createDMRoom(user, sender));

const message: IDirectMessage = {
...partialMsg,
room: {
type: RoomType.DIRECT_MESSAGE,
id: dmRoom.id,
creator: sender,
},
sender,
};

await this.msgBridge.doCreate(message, this.appId);
}

public async typing(options: ITypingOptions): Promise<() => Promise<void>> {
options.scope = options.scope || TypingScope.Room;

Expand All @@ -75,32 +46,4 @@ export class Notifier implements INotifier {
public getMessageBuilder(): IMessageBuilder {
return new MessageBuilder();
}

/**
* Creates a new direct message room between two users.
*
* @param {IUser} user - The first user to create the room for.
* @param {IUser} sender - The second user to create the room for.
* @returns {Promise<Pick<IRoom, 'id'>>} A Promise that resolves with the newly created direct message room id.
*/
private async createDMRoom(user: IUser, sender: IUser): Promise<Pick<IRoom, 'id'>> {
const newDMrid = await this.roomBridge.doCreate(
{
type: RoomType.DIRECT_MESSAGE,
// Leave the usernames field empty for now. It will be deprecated in 2.0.0.
usernames: [],
creator: sender,
// Leave the ID field empty for now. It will be generated by the server.
id: '',
// Leave the slugified name field empty for now. It will be generated by the server.
slugifiedName: '',
},
[user.username, sender.username],
this.appId,
);

return {
id: newDMrid,
};
}
}
6 changes: 3 additions & 3 deletions src/server/bridges/MessageBridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ITypingOptions } from '../../definition/accessors/INotifier';
import type { IMessage, IDirectMessage } from '../../definition/messages';
import type { IMessage } from '../../definition/messages';
import type { IRoom } from '../../definition/rooms';
import type { IUser } from '../../definition/users';
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
Expand All @@ -12,7 +12,7 @@ export interface ITypingDescriptor extends ITypingOptions {
}

export abstract class MessageBridge extends BaseBridge {
public async doCreate(message: IMessage | IDirectMessage, appId: string): Promise<string> {
public async doCreate(message: IMessage, appId: string): Promise<string> {
if (this.hasWritePermission(appId)) {
return this.create(message, appId);
}
Expand Down Expand Up @@ -54,7 +54,7 @@ export abstract class MessageBridge extends BaseBridge {
}
}

protected abstract create(message: IMessage | IDirectMessage, appId: string): Promise<string>;
protected abstract create(message: IMessage, appId: string): Promise<string>;

protected abstract update(message: IMessage, appId: string): Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion src/server/managers/AppAccessorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class AppAccessorManager {
const persist = new PersistenceRead(this.bridges.getPersistenceBridge(), appId);
const room = new RoomRead(this.bridges.getRoomBridge(), appId);
const user = new UserRead(this.bridges.getUserBridge(), appId);
const noti = new Notifier(this.bridges.getUserBridge(), this.bridges.getMessageBridge(), this.bridges.getRoomBridge(), appId);
const noti = new Notifier(this.bridges.getUserBridge(), this.bridges.getMessageBridge(), appId);
const livechat = new LivechatRead(this.bridges.getLivechatBridge(), appId);
const upload = new UploadRead(this.bridges.getUploadBridge(), appId);
const cloud = new CloudWorkspaceRead(this.bridges.getCloudWorkspaceBridge(), appId);
Expand Down
5 changes: 1 addition & 4 deletions tests/server/accessors/Modify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Expect, SetupFixture, Test } from 'alsatian';

import { Modify } from '../../../src/server/accessors';
import type { AppBridges, MessageBridge, ModerationBridge, RoomBridge, SchedulerBridge, UiInteractionBridge, UserBridge } from '../../../src/server/bridges';
import type { AppBridges, MessageBridge, ModerationBridge, SchedulerBridge, UiInteractionBridge, UserBridge } from '../../../src/server/bridges';
import type { OAuthAppsBridge } from '../../../src/server/bridges/OAuthAppsBridge';

export class ModifyAccessorTestFixture {
Expand All @@ -16,9 +16,6 @@ export class ModifyAccessorTestFixture {
getMessageBridge(): MessageBridge {
return {} as MessageBridge;
},
getRoomBridge() {
return {} as RoomBridge;
},
getUiInteractionBridge(): UiInteractionBridge {
return {} as UiInteractionBridge;
},
Expand Down
8 changes: 3 additions & 5 deletions tests/server/accessors/Notifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import type { IMessage } from '../../../src/definition/messages';
import type { IRoom } from '../../../src/definition/rooms';
import type { IUser } from '../../../src/definition/users';
import { MessageBuilder, Notifier } from '../../../src/server/accessors';
import type { MessageBridge, RoomBridge, UserBridge } from '../../../src/server/bridges';
import type { MessageBridge, UserBridge } from '../../../src/server/bridges';
import { TestData } from '../../test-data/utilities';

export class NotifierAccessorTestFixture {
private mockUserBridge: UserBridge;

private mockMsgBridge: MessageBridge;

private mockRoomBridge: RoomBridge;

@SetupFixture
public setupFixture() {
this.mockMsgBridge = {
Expand All @@ -29,9 +27,9 @@ export class NotifierAccessorTestFixture {

@AsyncTest()
public async useNotifier() {
Expect(() => new Notifier(this.mockUserBridge, this.mockMsgBridge, this.mockRoomBridge, 'testing')).not.toThrow();
Expect(() => new Notifier(this.mockUserBridge, this.mockMsgBridge, 'testing')).not.toThrow();

const noti = new Notifier(this.mockUserBridge, this.mockMsgBridge, this.mockRoomBridge, 'testing');
const noti = new Notifier(this.mockUserBridge, this.mockMsgBridge, 'testing');
await Expect(() => noti.notifyRoom(TestData.getRoom(), TestData.getMessage())).not.toThrowAsync();
await Expect(() => noti.notifyUser(TestData.getUser(), TestData.getMessage())).not.toThrowAsync();
Expect(noti.getMessageBuilder() instanceof MessageBuilder).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion tests/server/managers/AppAccessorManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class AppAccessorManagerTestFixture {
Expect(this.bridges.getServerSettingBridge).toHaveBeenCalled().exactly(1);
Expect(this.bridges.getEnvironmentalVariableBridge).toHaveBeenCalled().exactly(1);
Expect(this.bridges.getPersistenceBridge).toHaveBeenCalled().exactly(1);
Expect(this.bridges.getRoomBridge).toHaveBeenCalled().exactly(2);
Expect(this.bridges.getRoomBridge).toHaveBeenCalled().exactly(1);
Expect(this.bridges.getUserBridge).toHaveBeenCalled().exactly(2);
Expect(this.bridges.getMessageBridge).toHaveBeenCalled().exactly(2);
}
Expand Down

0 comments on commit b2a842d

Please sign in to comment.