From 111579d64570b82583fdb4a8d85a1929d1795126 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Thu, 8 Aug 2019 19:39:54 +0800 Subject: [PATCH 1/6] update IWebHooks interface --- src/definition/externalComponent/IWebHooks.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/definition/externalComponent/IWebHooks.ts b/src/definition/externalComponent/IWebHooks.ts index 2911eac57..6f967106a 100644 --- a/src/definition/externalComponent/IWebHooks.ts +++ b/src/definition/externalComponent/IWebHooks.ts @@ -5,5 +5,6 @@ export interface IWebHooks { // When a game is closed, Rocket.Chat will post all // relevant information to that URL. + sessionStarts?: string; sessionEnds?: string; } From f19f0a8b2386a474d970e2183d49bdf16d869384 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Thu, 8 Aug 2019 19:40:44 +0800 Subject: [PATCH 2/6] Add util randomString --- src/client/utils/index.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/client/utils/index.ts diff --git a/src/client/utils/index.ts b/src/client/utils/index.ts new file mode 100644 index 000000000..c2ba994aa --- /dev/null +++ b/src/client/utils/index.ts @@ -0,0 +1,18 @@ +/** + * Generate a random string with the specified length. + * @param length the length for the generated random string. + */ +export function randomString(length: number): string { + const buffer: Array = []; + const chars: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + + for (let i = 0; i < length; i++) { + buffer.push(chars[getRandomInt(chars.length)]); + } + + return buffer.join(''); +} + +function getRandomInt(max: number): number { + return Math.floor(Math.random() * Math.floor(max)); +} From b9e3eba8ea6fd6523524d881308b4e84aa15bfe9 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Thu, 8 Aug 2019 19:41:30 +0800 Subject: [PATCH 3/6] add AppEmbeddedSDK --- src/client/AppEmbeddedSDK.ts | 63 ++++++++++++++++++++++++++++++++++++ src/definition/package.json | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/client/AppEmbeddedSDK.ts diff --git a/src/client/AppEmbeddedSDK.ts b/src/client/AppEmbeddedSDK.ts new file mode 100644 index 000000000..f94e40aaa --- /dev/null +++ b/src/client/AppEmbeddedSDK.ts @@ -0,0 +1,63 @@ +import { randomString } from './utils'; + +const ACTION_ID_LENFTH = 80; + +export class AppEmbeddedSDK { + private listener: (this: Window, ev: MessageEvent) => any; + private callbacks: Map any>; + + constructor() { + this.listener = () => console.log('init'); + this.callbacks = new Map(); + } + + public getUserInfo(): Promise { + return this.call(AppEmbeddedSDKActions.GET_USER_INFO); + } + + public getRoomInfo(): Promise { + return this.call(AppEmbeddedSDKActions.GET_ROOM_INFO); + } + + /** + * Create a message listener with id and add them to callbacks map set. + * @param id the id of the message event listener + */ + private initListener(): void { + this.listener = ({ data }) => { + if (!data.hasOwnProperty('rcEmbeddedSDK')) { + return; + } + + const { rcEmbeddedSDK: { id, payload } } = data; + + if (this.callbacks.has(id)) { + const resolve = this.callbacks.get(id); + + if (typeof resolve === 'function') { + resolve(payload); + } + this.callbacks.delete(id); + } + }; + window.addEventListener('message', this.listener); + } + private call(action: string, payload?: any ): Promise { + return new Promise((resolve) => { + const id = randomString(ACTION_ID_LENFTH); + + this.initListener(); + + if (window.parent) { + window.parent.postMessage({ rcEmbeddedSDK: { action, payload, id } }, '*'); + } + + this.callbacks.set(id, resolve); + }); + } +} + +export enum AppEmbeddedSDKActions { + GET_USER_INFO = 'getUserInfo', + GET_ROOM_INFO = 'getRoomInfo', +} diff --git a/src/definition/package.json b/src/definition/package.json index 8cd151611..ed55a2a9d 100644 --- a/src/definition/package.json +++ b/src/definition/package.json @@ -1,6 +1,6 @@ { "name": "@rocket.chat/apps-ts-definition", - "version": "1.4.2", + "version": "1.5.2", "description": "Contains the TypeScript definitions for the Rocket.Chat Applications.", "main": "index.js", "typings": "index", From 6eff2cbe547dffc6ae9f03b4aef1eccd98602c55 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Fri, 9 Aug 2019 20:26:41 +0800 Subject: [PATCH 4/6] move constants to src/client/constants directory --- src/client/AppEmbeddedSDK.ts | 5 ++--- src/client/constants/index.ts | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 src/client/constants/index.ts diff --git a/src/client/AppEmbeddedSDK.ts b/src/client/AppEmbeddedSDK.ts index f94e40aaa..b111abcc2 100644 --- a/src/client/AppEmbeddedSDK.ts +++ b/src/client/AppEmbeddedSDK.ts @@ -1,7 +1,6 @@ +import { ACTION_ID_LENGTH } from './constants'; import { randomString } from './utils'; -const ACTION_ID_LENFTH = 80; - export class AppEmbeddedSDK { private listener: (this: Window, ev: MessageEvent) => any; private callbacks: Map any>; @@ -44,7 +43,7 @@ export class AppEmbeddedSDK { } private call(action: string, payload?: any ): Promise { return new Promise((resolve) => { - const id = randomString(ACTION_ID_LENFTH); + const id = randomString(ACTION_ID_LENGTH); this.initListener(); diff --git a/src/client/constants/index.ts b/src/client/constants/index.ts new file mode 100644 index 000000000..c6e0ff613 --- /dev/null +++ b/src/client/constants/index.ts @@ -0,0 +1,4 @@ +/** + * The id length of each action. + */ +export const ACTION_ID_LENGTH = 80; From a89dbfadd1572690ddb69ebd07d9370fb2b82c82 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Fri, 9 Aug 2019 21:09:55 +0800 Subject: [PATCH 5/6] separate initListener from call method * rename initListener to init --- src/client/AppEmbeddedSDK.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/client/AppEmbeddedSDK.ts b/src/client/AppEmbeddedSDK.ts index b111abcc2..313efada6 100644 --- a/src/client/AppEmbeddedSDK.ts +++ b/src/client/AppEmbeddedSDK.ts @@ -19,10 +19,9 @@ export class AppEmbeddedSDK { } /** - * Create a message listener with id and add them to callbacks map set. - * @param id the id of the message event listener + * Initialize the App Embedded SDK for communicating with Rocket.Chat */ - private initListener(): void { + public init(): void { this.listener = ({ data }) => { if (!data.hasOwnProperty('rcEmbeddedSDK')) { return; @@ -41,16 +40,12 @@ export class AppEmbeddedSDK { }; window.addEventListener('message', this.listener); } + private call(action: string, payload?: any ): Promise { return new Promise((resolve) => { const id = randomString(ACTION_ID_LENGTH); - this.initListener(); - - if (window.parent) { - window.parent.postMessage({ rcEmbeddedSDK: { action, payload, id } }, '*'); - } - + window.parent.postMessage({ rcEmbeddedSDK: { action, payload, id } }, '*'); this.callbacks.set(id, resolve); }); } From 832a2545d4e9be5bbcb828b2f9e3986ac91a2068 Mon Sep 17 00:00:00 2001 From: lolimay <1404363070@qq.com> Date: Wed, 21 Aug 2019 01:25:55 +0800 Subject: [PATCH 6/6] add appId to IExternalComponent interface --- src/definition/externalComponent/IExternalComponent.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/definition/externalComponent/IExternalComponent.ts b/src/definition/externalComponent/IExternalComponent.ts index 1e58fa836..00e7198ba 100644 --- a/src/definition/externalComponent/IExternalComponent.ts +++ b/src/definition/externalComponent/IExternalComponent.ts @@ -3,6 +3,10 @@ import { IExternalComponentOptions } from './IExternalComponentOptions'; * Represents an external component that is being provided. */ export interface IExternalComponent { + /** + * Provides the appId of the app which the external component belongs to. + */ + appId: string; /** * Provides the name of the external component. */