Skip to content

Commit

Permalink
made changes in the pr as per the comments removed unused code for th…
Browse files Browse the repository at this point in the history
…e best practice
  • Loading branch information
not-meet committed Nov 30, 2024
1 parent f7095e6 commit 7d8d7b0
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 63 deletions.
3 changes: 3 additions & 0 deletions QuickRepliesApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { settings } from './src/config/settings';
export class QuickRepliesApp extends App {
private elementBuilder: ElementBuilder;
private blockBuilder: BlockBuilder;
public params: Array<string>;
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
Expand Down Expand Up @@ -141,6 +142,7 @@ export class QuickRepliesApp extends App {
http,
persistence,
modify,
this.params,
context,
);

Expand All @@ -160,6 +162,7 @@ export class QuickRepliesApp extends App {
http,
persistence,
modify,
this.params,
context,
);

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reply name>`**: Quickly search and send a reply by name
- **`/quick create <name> <message>`**: Create a quick reply directly from the input box with a name and message
### Using Placeholders:
Expand Down
6 changes: 6 additions & 0 deletions src/commands/CommandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class CommandUtility implements ICommandUtility {
triggerId: this.triggerId,
threadId: this.threadId,
language,
args: this.params,
});

switch (this.params.length) {
Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/definition/handlers/IHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IHandler extends Omit<ICommandUtilityParams, 'params'> {

export type IHanderParams = Omit<ICommandUtilityParams, 'params'> & {
language: Language;
args : string[];
};
2 changes: 2 additions & 0 deletions src/handlers/ExecuteActionButtonHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,7 @@ export class ExecuteActionButtonHandler {
persis: this.persistence,
triggerId,
language,
args: this.params,
});

switch (actionId) {
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +103,7 @@ export class ExecuteBlockActionHandler {
persis: this.persistence,
triggerId,
language,
args: this.params,
});

switch (actionId) {
Expand Down
10 changes: 10 additions & 0 deletions src/handlers/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -62,15 +65,22 @@ export class Handler implements IHandler {
this.read,
this.persis,
this.modify,
this.sender,
this.room,
this.language,
this.args ?? [],
);

if (modal instanceof Error) {
this.app.getLogger().error(modal.message);
return;
}

if (!modal) {
this.app.getLogger().error('Modal is undefined. Cannot open surface view.');
return;
}

const triggerId = this.triggerId;

if (triggerId) {
Expand Down
157 changes: 94 additions & 63 deletions src/modal/createModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IUIKitSurfaceViewParam | Error> {
args: string[],
): Promise<IUIKitSurfaceViewParam | Error | void> {
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,
};
}
}

0 comments on commit 7d8d7b0

Please sign in to comment.