-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathNotionApp.ts
198 lines (180 loc) · 6.31 KB
/
NotionApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
IAppAccessors,
IAppInstallationContext,
IConfigurationExtend,
IEnvironmentRead,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { App } from "@rocket.chat/apps-engine/definition/App";
import { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
import { settings } from "./config/settings";
import { OAuth2Client } from "./src/authorization/OAuth2Client";
import { NotionCommand } from "./src/commands/NotionCommand";
import { NotionSDK } from "./src/lib/NotionSDK";
import {
ApiSecurity,
ApiVisibility,
} from "@rocket.chat/apps-engine/definition/api";
import { WebHookEndpoint } from "./src/endpoints/webhook";
import { ElementBuilder } from "./src/lib/ElementBuilder";
import { BlockBuilder } from "./src/lib/BlockBuilder";
import {
IUIKitResponse,
UIKitActionButtonInteractionContext,
UIKitBlockInteractionContext,
UIKitViewCloseInteractionContext,
UIKitViewSubmitInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { IAppUtils } from "./definition/lib/IAppUtils";
import { ExecuteViewClosedHandler } from "./src/handlers/ExecuteViewClosedHandler";
import { ExecuteViewSubmitHandler } from "./src/handlers/ExecuteViewSubmitHandler";
import { ExecuteBlockActionHandler } from "./src/handlers/ExecuteBlockActionHandler";
import { sendHelperMessageOnInstall } from "./src/helper/message";
import {
IUIActionButtonDescriptor,
UIActionButtonContext,
} from "@rocket.chat/apps-engine/definition/ui";
import { ActionButton } from "./enum/modals/common/ActionButtons";
import { ExecuteActionButtonHandler } from "./src/handlers/ExecuteActionButtonHandler";
export class NotionApp extends App {
private oAuth2Client: OAuth2Client;
private NotionSdk: NotionSDK;
private elementBuilder: ElementBuilder;
private blockBuilder: BlockBuilder;
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public async initialize(
configurationExtend: IConfigurationExtend,
environmentRead: IEnvironmentRead
): Promise<void> {
await configurationExtend.slashCommands.provideSlashCommand(
new NotionCommand(this)
);
await Promise.all(
settings.map((setting) => {
configurationExtend.settings.provideSetting(setting);
})
);
await configurationExtend.api.provideApi({
visibility: ApiVisibility.PUBLIC,
security: ApiSecurity.UNSECURE,
endpoints: [new WebHookEndpoint(this)],
});
this.oAuth2Client = new OAuth2Client(this);
this.NotionSdk = new NotionSDK(this.getAccessors().http);
this.elementBuilder = new ElementBuilder(this.getID());
this.blockBuilder = new BlockBuilder(this.getID());
const commentOnPagesButton: IUIActionButtonDescriptor = {
actionId: ActionButton.COMMENT_ON_PAGES_MESSAGE_BOX_ACTION,
labelI18n: ActionButton.COMMENT_ON_PAGES_MESSAGE_BOX_ACTION_LABEL,
context: UIActionButtonContext.MESSAGE_BOX_ACTION,
};
const sendToPageButton: IUIActionButtonDescriptor = {
actionId: ActionButton.SEND_TO_PAGE_MESSAGE_ACTION,
labelI18n: ActionButton.SEND_TO_PAGE_MESSAGE_ACTION_LABEL,
context: UIActionButtonContext.MESSAGE_ACTION,
};
const sendToNewPageButton: IUIActionButtonDescriptor = {
actionId: ActionButton.SEND_TO_NEW_PAGE_MESSAGE_ACTION,
labelI18n: ActionButton.SEND_TO_NEW_PAGE_MESSAGE_ACTION_LABEL,
context: UIActionButtonContext.MESSAGE_ACTION,
};
configurationExtend.ui.registerButton(commentOnPagesButton);
configurationExtend.ui.registerButton(sendToPageButton);
configurationExtend.ui.registerButton(sendToNewPageButton);
}
public getOAuth2Client(): OAuth2Client {
return this.oAuth2Client;
}
public getUtils(): IAppUtils {
return {
NotionSdk: this.NotionSdk,
elementBuilder: this.elementBuilder,
blockBuilder: this.blockBuilder,
};
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const handler = new ExecuteBlockActionHandler(
this,
read,
http,
persistence,
modify,
context
);
return await handler.handleActions();
}
public async executeViewSubmitHandler(
context: UIKitViewSubmitInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const handler = new ExecuteViewSubmitHandler(
this,
read,
http,
persistence,
modify,
context
);
return await handler.handleActions();
}
public async executeViewClosedHandler(
context: UIKitViewCloseInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const handler = new ExecuteViewClosedHandler(
this,
read,
http,
persistence,
modify,
context
);
return await handler.handleActions();
}
public async onInstall(
context: IAppInstallationContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<void> {
const { user } = context;
await sendHelperMessageOnInstall(this.getID(), user, read, modify);
return;
}
public async executeActionButtonHandler(
context: UIKitActionButtonInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const handler = new ExecuteActionButtonHandler(
this,
read,
http,
persistence,
modify,
context
);
return await handler.handleActions();
}
}