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: create a handler for each uikit interaction #640

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/definition/accessors/ILogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ILogEntry } from './ILogEntry';
* allow people to easily see what happened (users) or debug what went wrong.
*/
export interface ILogger {
method: AppMethod;
method: `${AppMethod}`;

debug(...items: Array<any>): void;
info(...items: Array<any>): void;
Expand All @@ -19,7 +19,7 @@ export interface ILogger {
/** Gets the entries logged. */
getEntries(): Array<ILogEntry>;
/** Gets the method which this logger is for. */
getMethod(): AppMethod;
getMethod(): `${AppMethod}`;
/** Gets when this logger was constructed. */
getStartTime(): Date;
/** Gets the end time, usually Date.now(). */
Expand Down
7 changes: 7 additions & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export enum AppMethod {
UIKIT_VIEW_SUBMIT = 'executeViewSubmitHandler',
UIKIT_VIEW_CLOSE = 'executeViewClosedHandler',
UIKIT_ACTION_BUTTON = 'executeActionButtonHandler',

UIKIT_ACTION_BUTTON_MESSAGE = 'executeActionButtonMessageHandler',
UIKIT_ACTION_BUTTON_MESSAGE_BOX = 'executeActionButtonMessageBoxHandler',
UIKIT_ACTION_BUTTON_ROOM = 'executeActionButtonRoomHandler',
UIKIT_ACTION_BUTTON_USER_DROPDOWN = 'executeActionButtonUserDropdownHandler',
UIKIT_ACTION_BUTTON_SIDEBAR_ROOM = 'executeActionButtonSidebarRoomHandler',

UIKIT_LIVECHAT_BLOCK_ACTION = 'executeLivechatBlockActionHandler',
// Livechat
EXECUTE_POST_LIVECHAT_ROOM_STARTED = 'executePostLivechatRoomStarted',
Expand Down
2 changes: 1 addition & 1 deletion src/definition/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rocket.chat/apps-ts-definition",
"version": "1.39.0-alpha",
"version": "1.40.0-alpha",
"description": "Contains the TypeScript definitions for the Rocket.Chat Applications.",
"main": "index.js",
"typings": "index",
Expand Down
124 changes: 116 additions & 8 deletions src/definition/uikit/IUIKitActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ export interface IUIKitInteractionHandler {
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
*/
[AppMethod.UIKIT_BLOCK_ACTION]?(context: UIKitBlockInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify):
Promise<IUIKitResponse>;
[AppMethod.UIKIT_BLOCK_ACTION]?(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when a modal is submitted.
Expand All @@ -29,8 +34,13 @@ export interface IUIKitInteractionHandler {
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
*/
[AppMethod.UIKIT_VIEW_SUBMIT]?(context: UIKitViewSubmitInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify):
Promise<IUIKitResponse>;
[AppMethod.UIKIT_VIEW_SUBMIT]?(
context: UIKitViewSubmitInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when a modal is closed.
Expand All @@ -40,8 +50,13 @@ export interface IUIKitInteractionHandler {
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
*/
[AppMethod.UIKIT_VIEW_CLOSE]?(context: UIKitViewCloseInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify):
Promise<IUIKitResponse>;
[AppMethod.UIKIT_VIEW_CLOSE]?(
context: UIKitViewCloseInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button is clicked.
Expand All @@ -52,6 +67,99 @@ export interface IUIKitInteractionHandler {
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/
[AppMethod.UIKIT_ACTION_BUTTON]?(context: UIKitActionButtonInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify):
Promise<IUIKitResponse>;
[AppMethod.UIKIT_ACTION_BUTTON]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button on the message is clicked.
*
* @param context
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/
[AppMethod.UIKIT_ACTION_BUTTON_MESSAGE]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button on the message box is clicked.
*
* @param context
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/

[AppMethod.UIKIT_ACTION_BUTTON_MESSAGE_BOX]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button on the room is clicked.
*
* @param context
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/

[AppMethod.UIKIT_ACTION_BUTTON_ROOM]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button on the users dropdown is clicked.
*
* @param context
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/
[AppMethod.UIKIT_ACTION_BUTTON_USER_DROPDOWN]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;

/**
* Method called when an action button on the sidebar is clicked.
*
* @param context
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
* @param modify An accessor to the App's persistence
*/

[AppMethod.UIKIT_ACTION_BUTTON_SIDEBAR_ROOM]?(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify,
): Promise<IUIKitResponse>;
}
12 changes: 7 additions & 5 deletions src/definition/uikit/IUIKitIncomingInteraction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { IMessage } from '../messages';
import { IRoom } from '../rooms';
import { IUser } from '../users';
import {
IUIKitIncomingInteractionMessageContainer,
IUIKitIncomingInteractionModalContainer,
} from './UIKitIncomingInteractionContainer';
import { IUIKitIncomingInteractionActionButton } from './IUIKitIncomingInteractionActionButton';
import { IUIKitIncomingInteractionMessageContainer, IUIKitIncomingInteractionModalContainer } from './UIKitIncomingInteractionContainer';

export enum UIKitIncomingInteractionType {
BLOCK = 'blockAction',
Expand All @@ -13,8 +11,10 @@ export enum UIKitIncomingInteractionType {
ACTION_BUTTON = 'actionButton',
}

/** @deprecated use UIKitIncomingInteraction instead */

export interface IUIKitIncomingInteraction {
type: UIKitIncomingInteractionType;
type: 'blockAction' | 'viewSubmit' | 'viewClosed';
container: IUIKitIncomingInteractionModalContainer | IUIKitIncomingInteractionMessageContainer;
user: IUser;
appId: string;
Expand All @@ -24,3 +24,5 @@ export interface IUIKitIncomingInteraction {
room?: IRoom;
message?: IMessage;
}

export type UIKitIncomingInteraction = IUIKitIncomingInteraction | IUIKitIncomingInteractionActionButton;
78 changes: 78 additions & 0 deletions src/definition/uikit/IUIKitIncomingInteractionActionButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { IMessage } from '../messages';
import { IRoom } from '../rooms';
import { IUser } from '../users';

interface IUIKitIncomingInteractionActionButtonBase {
triggerId: string;
type: 'actionButton';

user: IUser;
actionId: string;
appId: string;

payload: Record<string, any>;
}

interface IUIKitIncomingInteractionActionButtonMessageBox {
triggerId: string;

type: 'actionButton';
room: IRoom;
tmid?: string;

user: IUser;

actionId: string;
appId: string;

payload: {
context: 'messageBoxAction';
message?: string;
};
}

export const isUIKitIncomingInteractionActionButtonMessageBox = (
interaction: IUIKitIncomingInteractionActionButtonBase,
): interaction is IUIKitIncomingInteractionActionButtonMessageBox => {
return interaction.payload.context === 'messageBoxAction';
};

interface IUIKitIncomingInteractionActionButtonMessage extends IUIKitIncomingInteractionActionButtonBase {
room: IRoom;
tmid?: string;

message: IMessage;

payload: {
context: 'messageAction';
};
}

interface IUIKitIncomingInteractionActionButtonRoomSidebar extends IUIKitIncomingInteractionActionButtonBase {
room: IRoom;

payload: {
context: 'roomSideBarAction';
};
}

interface IUIKitIncomingInteractionActionButtonRoom extends IUIKitIncomingInteractionActionButtonBase {
room: IRoom;

payload: {
context: 'roomAction';
};
}

interface IUIKitIncomingInteractionActionButtonUserDropdown extends IUIKitIncomingInteractionActionButtonBase {
payload: {
context: 'userDropdownAction';
};
}

export type IUIKitIncomingInteractionActionButton =
| IUIKitIncomingInteractionActionButtonMessageBox
| IUIKitIncomingInteractionActionButtonMessage
| IUIKitIncomingInteractionActionButtonRoomSidebar
| IUIKitIncomingInteractionActionButtonRoom
| IUIKitIncomingInteractionActionButtonUserDropdown;
15 changes: 14 additions & 1 deletion src/definition/uikit/UIKitIncomingInteractionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,23 @@ export interface IUIKitViewCloseIncomingInteraction extends IUIKitBaseIncomingIn
isCleared: boolean;
}

/** @deprecated */
export interface IUIKitActionButtonIncomingInteraction extends IUIKitBaseIncomingInteraction {
buttonContext: UIActionButtonContext;
buttonContext: `${UIActionButtonContext}`;
actionId: string;
triggerId: string;
room: IRoom;
message?: IMessage;
}

export interface IUIKitActionButtonMessageBoxIncomingInteraction extends IUIKitBaseIncomingInteraction {
buttonContext: `messageBoxAction`;
actionId: string;
triggerId: string;
room: IRoom;
message?: string;

threadId?: string;
}

export type UIKitActionButtonIncomingInteraction = IUIKitActionButtonIncomingInteraction | IUIKitActionButtonMessageBoxIncomingInteraction;
12 changes: 9 additions & 3 deletions src/definition/uikit/UIKitInteractionContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// tslint:disable:max-classes-per-file
import { IUIKitActionButtonIncomingInteraction, IUIKitBaseIncomingInteraction, IUIKitBlockIncomingInteraction, IUIKitViewCloseIncomingInteraction, IUIKitViewSubmitIncomingInteraction } from './UIKitIncomingInteractionTypes';
import {
IUIKitBaseIncomingInteraction,
IUIKitBlockIncomingInteraction,
IUIKitViewCloseIncomingInteraction,
IUIKitViewSubmitIncomingInteraction,
UIKitActionButtonIncomingInteraction,
} from './UIKitIncomingInteractionTypes';
import { UIKitInteractionResponder } from './UIKitInteractionResponder';

export abstract class UIKitInteractionContext {
Expand Down Expand Up @@ -51,11 +57,11 @@ export class UIKitViewCloseInteractionContext extends UIKitInteractionContext {
}

export class UIKitActionButtonInteractionContext extends UIKitInteractionContext {
constructor(private readonly interactionData: IUIKitActionButtonIncomingInteraction) {
constructor(private readonly interactionData: UIKitActionButtonIncomingInteraction) {
super(interactionData);
}

public getInteractionData(): IUIKitActionButtonIncomingInteraction {
public getInteractionData(): UIKitActionButtonIncomingInteraction {
return this.interactionData;
}
}
6 changes: 3 additions & 3 deletions src/server/ProxiedApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ export class ProxiedApp implements IApp {
return this.storageItem.implemented;
}

public hasMethod(method: AppMethod): boolean {
public hasMethod(method: `${AppMethod}`): boolean {
return typeof (this.app as any)[method] === 'function';
}

public setupLogger(method: AppMethod): AppConsole {
public setupLogger(method: `${AppMethod}`): AppConsole {
const logger = new AppConsole(method);
// Set the logger to our new one
(this.app as any).logger = logger;

return logger;
}

public async call(method: AppMethod, ...args: Array<any>): Promise<any> {
public async call(method: `${AppMethod}`, ...args: Array<any>): Promise<any> {
if (typeof (this.app as any)[method] !== 'function') {
throw new Error(`The App ${this.app.getName()} (${this.app.getID()}` + ` does not have the method: "${method}"`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/bridges/IListenerBridge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IMessage } from '../../definition/messages';
import { AppInterface } from '../../definition/metadata';
import { IRoom } from '../../definition/rooms';
import { IUIKitIncomingInteraction } from '../../definition/uikit';
import { UIKitIncomingInteraction } from '../../definition/uikit';

export interface IListenerBridge {
messageEvent(int: AppInterface, message: IMessage): Promise<void | boolean | IMessage>;
roomEvent(int: AppInterface, room: IRoom): Promise<void | boolean | IRoom>;
uiKitInteractionEvent(int: AppInterface, action: IUIKitIncomingInteraction): Promise<void | boolean>;
uiKitInteractionEvent(int: AppInterface, action: UIKitIncomingInteraction): Promise<void | boolean>;
}
Loading