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

feat: Reactions bridge for apps #792

Merged
merged 3 commits into from
Aug 16, 2024
Merged
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
21 changes: 21 additions & 0 deletions src/definition/accessors/IMessageUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Reaction } from '../messages';

export interface IMessageUpdater {
/**
* Add a reaction to a message
*
* @param messageId the id of the message
* @param userId the id of the user
* @param reaction the reaction
*/
addReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;

/**
* Remove a reaction from a message
*
* @param messageId the id of the message
* @param userId the id of the user
* @param reaction the reaction
*/
removeReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;
}
7 changes: 7 additions & 0 deletions src/definition/accessors/IModifyUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ILivechatUpdater } from './ILivechatUpdater';
import type { IMessageBuilder } from './IMessageBuilder';
import type { IRoomBuilder } from './IRoomBuilder';
import type { IUserUpdater } from './IUserUpdater';
import type { IMessageUpdater } from './IMessageUpdater';

export interface IModifyUpdater {
/**
Expand All @@ -17,6 +18,12 @@ export interface IModifyUpdater {
*/
getUserUpdater(): IUserUpdater;

/**
* Get the updater object responsible for
* methods that update messages
*/
getMessageUpdater(): IMessageUpdater;

/**
* Modifies an existing message.
* Raises an exception if a non-existent messageId is supplied
Expand Down
1 change: 1 addition & 0 deletions src/definition/accessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './ILogger';
export * from './IMessageBuilder';
export * from './IMessageExtender';
export * from './IMessageRead';
export * from './IMessageUpdater';
export * from './IModify';
export * from './IModifyCreator';
export * from './IModifyDeleter';
Expand Down
3 changes: 3 additions & 0 deletions src/definition/messages/IMessageReaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type Reaction = `:${string}:`;

/**
* Interface which represents a reaction which can be added to a message.
*/
// Note: keeping it as string for compatibility
export interface IMessageReactions {
[emoji: string]: Array<IMessageReaction>;
}
Expand Down
3 changes: 2 additions & 1 deletion src/definition/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IMessageFile } from './IMessageFile';
import { IMessageFollowContext } from './IMessageFollowContext';
import { IMessagePinContext } from './IMessagePinContext';
import { IMessageRaw } from './IMessageRaw';
import { IMessageReaction, IMessageReactions } from './IMessageReaction';
import { IMessageReaction, IMessageReactions, Reaction } from './IMessageReaction';
import { IMessageReactionContext } from './IMessageReactionContext';
import { IMessageReportContext } from './IMessageReportContext';
import { IMessageStarContext } from './IMessageStarContext';
Expand Down Expand Up @@ -67,4 +67,5 @@ export {
MessageActionType,
MessageProcessingType,
IMessageDeleteContext,
Reaction,
};
8 changes: 7 additions & 1 deletion src/server/accessors/ModifyUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ILivechatUpdater, IMessageBuilder, IModifyUpdater, IRoomBuilder } from '../../definition/accessors';
import type { ILivechatUpdater, IMessageBuilder, IMessageUpdater, IModifyUpdater, IRoomBuilder } from '../../definition/accessors';
import type { IUserUpdater } from '../../definition/accessors/IUserUpdater';
import { RocketChatAssociationModel } from '../../definition/metadata';
import { RoomType } from '../../definition/rooms';
Expand All @@ -15,6 +15,8 @@ export class ModifyUpdater implements IModifyUpdater {

private userUpdater: IUserUpdater;

private messageUpdater: IMessageUpdater;

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);
Expand All @@ -28,6 +30,10 @@ export class ModifyUpdater implements IModifyUpdater {
return this.userUpdater;
}

public getMessageUpdater(): IMessageUpdater {
return this.messageUpdater;
}

public async message(messageId: string, updater: IUser): Promise<IMessageBuilder> {
const msg = await this.bridges.getMessageBridge().doGetById(messageId, this.appId);

Expand Down
18 changes: 17 additions & 1 deletion 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 } from '../../definition/messages';
import type { IMessage, Reaction } from '../../definition/messages';
import type { IRoom } from '../../definition/rooms';
import type { IUser } from '../../definition/users';
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
Expand Down Expand Up @@ -54,6 +54,18 @@ export abstract class MessageBridge extends BaseBridge {
}
}

public async doAddReaction(messageId: string, userId: string, reaction: Reaction, appId: string): Promise<void> {
if (this.hasWritePermission(appId)) {
return this.addReaction(messageId, userId, reaction);
}
}

public async doRemoveReaction(messageId: string, userId: string, reaction: Reaction, appId: string): Promise<void> {
if (this.hasWritePermission(appId)) {
return this.removeReaction(messageId, userId, reaction);
}
}

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

protected abstract update(message: IMessage, appId: string): Promise<void>;
Expand All @@ -68,6 +80,10 @@ export abstract class MessageBridge extends BaseBridge {

protected abstract delete(message: IMessage, user: IUser, appId: string): Promise<void>;

protected abstract addReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;

protected abstract removeReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;

private hasReadPermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions.message.read)) {
return true;
Expand Down
10 changes: 9 additions & 1 deletion tests/test-data/bridges/messageBridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IMessage } from '../../../src/definition/messages';
import type { IMessage, Reaction } from '../../../src/definition/messages';
import type { IRoom } from '../../../src/definition/rooms';
import type { IUser } from '../../../src/definition/users';
import { MessageBridge } from '../../../src/server/bridges';
Expand Down Expand Up @@ -32,4 +32,12 @@ export class TestsMessageBridge extends MessageBridge {
public typing(options: ITypingDescriptor): Promise<void> {
throw new Error('Method not implemented.');
}

public addReaction(_messageId: string, _userId: string, _reaction: Reaction): Promise<void> {
throw new Error('Method not implemented.');
}

public removeReaction(_messageId: string, _userId: string, _reaction: Reaction): Promise<void> {
throw new Error('Method not implemented.');
}
}
Loading