From 7d8d7b0fbc26b99b750d493bbd8bfb8b2d1ec208 Mon Sep 17 00:00:00 2001 From: not-meet Date: Sat, 30 Nov 2024 20:33:35 +0530 Subject: [PATCH] made changes in the pr as per the comments removed unused code for the best practice --- QuickRepliesApp.ts | 3 + README.md | 1 + src/commands/CommandUtility.ts | 6 + src/definition/handlers/IHandler.ts | 1 + src/handlers/ExecuteActionButtonHandler.ts | 2 + src/handlers/ExecuteBlockActionHandler.ts | 2 + src/handlers/Handler.ts | 10 ++ src/modal/createModal.ts | 157 ++++++++++++--------- 8 files changed, 119 insertions(+), 63 deletions(-) diff --git a/QuickRepliesApp.ts b/QuickRepliesApp.ts index 48a0d53..8eab81b 100644 --- a/QuickRepliesApp.ts +++ b/QuickRepliesApp.ts @@ -36,6 +36,7 @@ import { settings } from './src/config/settings'; export class QuickRepliesApp extends App { private elementBuilder: ElementBuilder; private blockBuilder: BlockBuilder; + public params: Array; constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { super(info, logger, accessors); } @@ -141,6 +142,7 @@ export class QuickRepliesApp extends App { http, persistence, modify, + this.params, context, ); @@ -160,6 +162,7 @@ export class QuickRepliesApp extends App { http, persistence, modify, + this.params, context, ); diff --git a/README.md b/README.md index c495576..7a5a8cc 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ By selecting quick replies instead of typing manually, agents/users can respond - **`/quick ai`**: Use AI to generate replies - **`/quick help`**: Get help with Quick Reply - **`/qs `**: Quickly search and send a reply by name +- **`/quick create `**: Create a quick reply directly from the input box with a name and message ### Using Placeholders: diff --git a/src/commands/CommandUtility.ts b/src/commands/CommandUtility.ts index 3c07bdd..16846a8 100644 --- a/src/commands/CommandUtility.ts +++ b/src/commands/CommandUtility.ts @@ -65,6 +65,7 @@ export class CommandUtility implements ICommandUtility { triggerId: this.triggerId, threadId: this.threadId, language, + args: this.params, }); switch (this.params.length) { @@ -77,6 +78,11 @@ export class CommandUtility implements ICommandUtility { break; } default: { + const subCommand = this.params[0].toLowerCase(); + if (subCommand === CommandParam.CREATE){ + await this.handleSingleParam(handler) + break; + } await handler.sendDefault(); } } diff --git a/src/definition/handlers/IHandler.ts b/src/definition/handlers/IHandler.ts index d589bf0..3bccae5 100644 --- a/src/definition/handlers/IHandler.ts +++ b/src/definition/handlers/IHandler.ts @@ -10,4 +10,5 @@ export interface IHandler extends Omit { export type IHanderParams = Omit & { language: Language; + args : string[]; }; diff --git a/src/handlers/ExecuteActionButtonHandler.ts b/src/handlers/ExecuteActionButtonHandler.ts index 58fbca6..c418ea3 100644 --- a/src/handlers/ExecuteActionButtonHandler.ts +++ b/src/handlers/ExecuteActionButtonHandler.ts @@ -24,6 +24,7 @@ export class ExecuteActionButtonHandler { protected readonly http: IHttp, protected readonly persistence: IPersistence, protected readonly modify: IModify, + protected readonly params: string[] = [], context: UIKitActionButtonInteractionContext, ) { this.context = context; @@ -56,6 +57,7 @@ export class ExecuteActionButtonHandler { persis: this.persistence, triggerId, language, + args: this.params, }); switch (actionId) { diff --git a/src/handlers/ExecuteBlockActionHandler.ts b/src/handlers/ExecuteBlockActionHandler.ts index 5912997..3d34548 100644 --- a/src/handlers/ExecuteBlockActionHandler.ts +++ b/src/handlers/ExecuteBlockActionHandler.ts @@ -39,6 +39,7 @@ export class ExecuteBlockActionHandler { protected readonly http: IHttp, protected readonly persistence: IPersistence, protected readonly modify: IModify, + protected readonly params: string[] = [], context: UIKitBlockInteractionContext, ) { this.context = context; @@ -102,6 +103,7 @@ export class ExecuteBlockActionHandler { persis: this.persistence, triggerId, language, + args: this.params, }); switch (actionId) { diff --git a/src/handlers/Handler.ts b/src/handlers/Handler.ts index 5f4c0ca..033f452 100644 --- a/src/handlers/Handler.ts +++ b/src/handlers/Handler.ts @@ -35,6 +35,7 @@ export class Handler implements IHandler { public triggerId?: string; public threadId?: string; public language: Language; + public args? : string[]; constructor(params: IHanderParams) { this.app = params.app; @@ -47,6 +48,8 @@ export class Handler implements IHandler { this.triggerId = params.triggerId; this.threadId = params.threadId; this.language = params.language; + this.args = params.args; + const persistenceRead = params.read.getPersistenceReader(); this.roomInteractionStorage = new RoomInteractionStorage( params.persis, @@ -62,8 +65,10 @@ export class Handler implements IHandler { this.read, this.persis, this.modify, + this.sender, this.room, this.language, + this.args ?? [], ); if (modal instanceof Error) { @@ -71,6 +76,11 @@ export class Handler implements IHandler { return; } + if (!modal) { + this.app.getLogger().error('Modal is undefined. Cannot open surface view.'); + return; + } + const triggerId = this.triggerId; if (triggerId) { diff --git a/src/modal/createModal.ts b/src/modal/createModal.ts index 7815e98..858f241 100644 --- a/src/modal/createModal.ts +++ b/src/modal/createModal.ts @@ -3,92 +3,123 @@ import { IPersistence, IRead, IUIKitSurfaceViewParam, -} from '@rocket.chat/apps-engine/definition/accessors'; -import { TextObjectType, InputBlock } from '@rocket.chat/ui-kit'; - -import { QuickRepliesApp } from '../../QuickRepliesApp'; -import { IUser } from '@rocket.chat/apps-engine/definition/users'; -import { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; -import { inputElementComponent } from './common/inputElementComponent'; -import { + } from '@rocket.chat/apps-engine/definition/accessors'; + import { TextObjectType, InputBlock } from '@rocket.chat/ui-kit'; + + import { QuickRepliesApp } from '../../QuickRepliesApp'; + import { IUser } from '@rocket.chat/apps-engine/definition/users'; + import { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; + import { inputElementComponent } from './common/inputElementComponent'; + import { ButtonStyle, UIKitSurfaceType, -} from '@rocket.chat/apps-engine/definition/uikit'; -import { CreateModalEnum } from '../enum/modals/createModal'; -import { Language, t } from '../lib/Translation/translation'; - -export async function CreateReplyModal( + } from '@rocket.chat/apps-engine/definition/uikit'; + import { CreateModalEnum } from '../enum/modals/createModal'; + import { Language, t } from '../lib/Translation/translation'; + import { ReplyStorage } from '../storage/ReplyStorage'; + import { sendNotification } from '../helper/notification'; + + export async function CreateReplyModal( app: QuickRepliesApp, user: IUser, read: IRead, persistence: IPersistence, modify: IModify, + sender: IUser, room: IRoom, language: Language, -): Promise { + args: string[], + ): Promise { + if (args.length > 1) { + const replyName = args[1]; + const replyBody = args.slice(2).join(' '); + + const replyStorage = new ReplyStorage(persistence, read.getPersistenceReader()); + + const result = await replyStorage.createReply(sender, replyName, replyBody, language); + + if (!result.success) { + const errorMessage = `${t('Fail_Create_Reply', language, { + name: sender.name, + })} \n\n ${result.error}`; + await sendNotification(read, modify, sender, room, { message: errorMessage }); + return; + } + + const successMessage = `${t('Success_Create_Reply', language, { + name: sender.name, + replyname: replyName, + })}`; + await sendNotification(read, modify, sender, room, { message: successMessage }); + return; + + + } + const { elementBuilder, blockBuilder } = app.getUtils(); - + const blocks: InputBlock[] = []; - + const labelReplyName = t('Reply_Name_Label', language); const placeholderReplyName = t('Reply_Name_Placeholder', language); - + const inputReplyName = inputElementComponent( - { - app, - placeholder: placeholderReplyName, - label: labelReplyName, - optional: false, - }, - { - blockId: CreateModalEnum.REPLY_NAME_BLOCK_ID, - actionId: CreateModalEnum.REPLY_NAME_ACTION_ID, - }, + { + app, + placeholder: placeholderReplyName, + label: labelReplyName, + optional: false, + }, + { + blockId: CreateModalEnum.REPLY_NAME_BLOCK_ID, + actionId: CreateModalEnum.REPLY_NAME_ACTION_ID, + }, ); - + const labelReplyBody = t('Reply_Body_Label', language); const placeholderReplyBody = t('Reply_Body_Placeholder', language); - + const inputReplyBody = inputElementComponent( - { - app, - placeholder: placeholderReplyBody, - label: labelReplyBody, - optional: false, - multiline: true, - }, - { - blockId: CreateModalEnum.REPLY_BODY_BLOCK_ID, - actionId: CreateModalEnum.REPLY_BODY_ACTION_ID, - }, + { + app, + placeholder: placeholderReplyBody, + label: labelReplyBody, + optional: false, + multiline: true, + }, + { + blockId: CreateModalEnum.REPLY_BODY_BLOCK_ID, + actionId: CreateModalEnum.REPLY_BODY_ACTION_ID, + }, ); - + blocks.push(inputReplyName, inputReplyBody); - + const submit = elementBuilder.addButton( - { text: t('Create_Button', language), style: ButtonStyle.PRIMARY }, - { - actionId: CreateModalEnum.SUBMIT_ACTION_ID, - blockId: CreateModalEnum.SUBMIT_BLOCK_ID, - }, + { text: t('Create_Button', language), style: ButtonStyle.PRIMARY }, + { + actionId: CreateModalEnum.SUBMIT_ACTION_ID, + blockId: CreateModalEnum.SUBMIT_BLOCK_ID, + }, ); - + const close = elementBuilder.addButton( - { text: t('Close_Button', language), style: ButtonStyle.DANGER }, - { - actionId: CreateModalEnum.CLOSE_ACTION_ID, - blockId: CreateModalEnum.CLOSE_BLOCK_ID, - }, + { text: t('Close_Button', language), style: ButtonStyle.DANGER }, + { + actionId: CreateModalEnum.CLOSE_ACTION_ID, + blockId: CreateModalEnum.CLOSE_BLOCK_ID, + }, ); return { - id: `${CreateModalEnum.VIEW_ID}`, - type: UIKitSurfaceType.MODAL, - title: { - type: TextObjectType.MRKDWN, - text: t('Create_Modal_Title', language), - }, - blocks, - close, - submit, + id: `${CreateModalEnum.VIEW_ID}`, + type: UIKitSurfaceType.MODAL, + title: { + type: TextObjectType.MRKDWN, + text: t('Create_Modal_Title', language), + }, + blocks, + close, + submit, }; -} + } + \ No newline at end of file