Skip to content

Commit 4b89f8a

Browse files
committed
add reactions updater to apps engine
1 parent bf590b6 commit 4b89f8a

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Reaction } from '../messages';
2+
3+
export interface IMessageUpdater {
4+
/**
5+
* Add a reaction to a message
6+
*
7+
* @param messageId the id of the message
8+
* @param userId the id of the user
9+
* @param reaction the reaction
10+
*/
11+
addReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;
12+
13+
/**
14+
* Remove a reaction from a message
15+
*
16+
* @param messageId the id of the message
17+
* @param userId the id of the user
18+
* @param reaction the reaction
19+
*/
20+
removeReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;
21+
}

src/definition/accessors/IModifyUpdater.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ILivechatUpdater } from './ILivechatUpdater';
33
import type { IMessageBuilder } from './IMessageBuilder';
44
import type { IRoomBuilder } from './IRoomBuilder';
55
import type { IUserUpdater } from './IUserUpdater';
6+
import type { IMessageUpdater } from './IMessageUpdater';
67

78
export interface IModifyUpdater {
89
/**
@@ -17,6 +18,12 @@ export interface IModifyUpdater {
1718
*/
1819
getUserUpdater(): IUserUpdater;
1920

21+
/**
22+
* Get the updater object responsible for
23+
* methods that update messages
24+
*/
25+
getMessageUpdater(): IMessageUpdater;
26+
2027
/**
2128
* Modifies an existing message.
2229
* Raises an exception if a non-existent messageId is supplied

src/definition/accessors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './ILogger';
2121
export * from './IMessageBuilder';
2222
export * from './IMessageExtender';
2323
export * from './IMessageRead';
24+
export * from './IMessageUpdater';
2425
export * from './IModify';
2526
export * from './IModifyCreator';
2627
export * from './IModifyDeleter';

src/definition/messages/IMessageReaction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
export type Reaction = `:${string}:`;
2+
13
/**
24
* Interface which represents a reaction which can be added to a message.
35
*/
6+
// Note: keeping it as string for compatibility
47
export interface IMessageReactions {
58
[emoji: string]: Array<IMessageReaction>;
69
}

src/definition/messages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IMessageFile } from './IMessageFile';
99
import { IMessageFollowContext } from './IMessageFollowContext';
1010
import { IMessagePinContext } from './IMessagePinContext';
1111
import { IMessageRaw } from './IMessageRaw';
12-
import { IMessageReaction, IMessageReactions } from './IMessageReaction';
12+
import { IMessageReaction, IMessageReactions, Reaction } from './IMessageReaction';
1313
import { IMessageReactionContext } from './IMessageReactionContext';
1414
import { IMessageReportContext } from './IMessageReportContext';
1515
import { IMessageStarContext } from './IMessageStarContext';
@@ -67,4 +67,5 @@ export {
6767
MessageActionType,
6868
MessageProcessingType,
6969
IMessageDeleteContext,
70+
Reaction,
7071
};

src/server/accessors/ModifyUpdater.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ILivechatUpdater, IMessageBuilder, IModifyUpdater, IRoomBuilder } from '../../definition/accessors';
1+
import type { ILivechatUpdater, IMessageBuilder, IMessageUpdater, IModifyUpdater, IRoomBuilder } from '../../definition/accessors';
22
import type { IUserUpdater } from '../../definition/accessors/IUserUpdater';
33
import { RocketChatAssociationModel } from '../../definition/metadata';
44
import { RoomType } from '../../definition/rooms';
@@ -15,6 +15,8 @@ export class ModifyUpdater implements IModifyUpdater {
1515

1616
private userUpdater: IUserUpdater;
1717

18+
private messageUpdater: IMessageUpdater;
19+
1820
constructor(private readonly bridges: AppBridges, private readonly appId: string) {
1921
this.livechatUpdater = new LivechatUpdater(this.bridges, this.appId);
2022
this.userUpdater = new UserUpdater(this.bridges, this.appId);
@@ -28,6 +30,10 @@ export class ModifyUpdater implements IModifyUpdater {
2830
return this.userUpdater;
2931
}
3032

33+
public getMessageUpdater(): IMessageUpdater {
34+
return this.messageUpdater;
35+
}
36+
3137
public async message(messageId: string, updater: IUser): Promise<IMessageBuilder> {
3238
const msg = await this.bridges.getMessageBridge().doGetById(messageId, this.appId);
3339

src/server/bridges/MessageBridge.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ITypingOptions } from '../../definition/accessors/INotifier';
2-
import type { IMessage } from '../../definition/messages';
2+
import type { IMessage, Reaction } from '../../definition/messages';
33
import type { IRoom } from '../../definition/rooms';
44
import type { IUser } from '../../definition/users';
55
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
@@ -54,6 +54,18 @@ export abstract class MessageBridge extends BaseBridge {
5454
}
5555
}
5656

57+
public async doAddReaction(messageId: string, userId: string, reaction: Reaction, appId: string): Promise<void> {
58+
if (this.hasWritePermission(appId)) {
59+
return this.addReaction(messageId, userId, reaction);
60+
}
61+
}
62+
63+
public async doRemoveReaction(messageId: string, userId: string, reaction: Reaction, appId: string): Promise<void> {
64+
if (this.hasWritePermission(appId)) {
65+
return this.removeReaction(messageId, userId, reaction);
66+
}
67+
}
68+
5769
protected abstract create(message: IMessage, appId: string): Promise<string>;
5870

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

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

83+
protected abstract addReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;
84+
85+
protected abstract removeReaction(messageId: string, userId: string, reaction: Reaction): Promise<void>;
86+
7187
private hasReadPermission(appId: string): boolean {
7288
if (AppPermissionManager.hasPermission(appId, AppPermissions.message.read)) {
7389
return true;

0 commit comments

Comments
 (0)