forked from RocketChat/Apps.Figma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FigmaApp.ts
274 lines (259 loc) · 10 KB
/
FigmaApp.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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 {
IAuthData,
IOAuth2Client,
IOAuth2ClientOptions
} from '@rocket.chat/apps-engine/definition/oauth2/IOAuth2';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { botNotifyCurrentUser, sendDMToUser } from './src/lib/messages';
import { create as registerAuthorizedUser } from './src/storage/users';
import { createOAuth2Client } from '@rocket.chat/apps-engine/definition/oauth2/OAuth2';
import { FigmaCommand } from './src/command/FigmaCommand';
import {
UIKitBlockInteractionContext,
UIKitViewSubmitInteractionContext
} from '@rocket.chat/apps-engine/definition/uikit';
import { figmaWebHooks } from './src/endpoints/figmaEndpoints';
import {
ApiSecurity,
ApiVisibility
} from '@rocket.chat/apps-engine/definition/api';
import { ExecuteViewSubmitHandler } from './src/handlers/submit';
import { AddSubscription } from './src/subscription/addSubscription';
import { getRoom } from './src/storage/room';
import { BlockActionHandler } from './src/handlers/BlockActionHandler';
import { ExecuteReplyHandler } from './src/handlers/reply';
import { modalTitle } from './src/enums/enums';
import { CommentModalHandler } from './src/handlers/comment';
export class FigmaApp extends App {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public user: IUser;
public botName: string;
public oauth2ClientInstance: IOAuth2Client;
public oauth2Options: IOAuth2ClientOptions = {
alias: 'figma',
accessTokenUri: 'https://www.figma.com/api/oauth/token',
authUri: 'https://www.figma.com/oauth',
refreshTokenUri: 'https://www.figma.com/api/oauth/refresh',
revokeTokenUri: 'https://api.figma.com/v1/oauth/revoke_token',
defaultScopes: ['file_read'],
authorizationCallback: this.authorizationCallback.bind(this)
};
public async executeViewSubmitHandler(
context: UIKitViewSubmitInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const user: IUser = context.getInteractionData().user;
const room = await getRoom(read, user);
if (room) {
// we are using modal title to check different modals as for updated modal modal id will be same
if (
context.getInteractionData().view.title.text ===
modalTitle.NOTIFICATION_MODAL
) {
const handler = new ExecuteViewSubmitHandler(
this,
read,
http,
modify,
persistence
);
return await handler
.run(context, room)
.catch((err) =>
console.log('error: submitting Events modal', err)
);
} else if (
context.getInteractionData().view.title.text ===
modalTitle.EVENT_MODAL
) {
const handler = new AddSubscription(
this,
read,
http,
modify,
persistence
);
return await handler
.run(context, room)
.catch((err) =>
console.log('error: submitting 2nd modal', err)
);
} else if (
context.getInteractionData().view.title.text ===
modalTitle.REPLY_MODAL
) {
const handler = new ExecuteReplyHandler(
this,
read,
http,
modify,
persistence
);
return await handler.run(context, room);
} else if (
context.getInteractionData().view.title.text ===
modalTitle.CREATE_COMMENT_MODAL
) {
const handler = new CommentModalHandler(
this,
read,
http,
modify,
persistence
);
return await handler.run(context, room);
} else {
console.log('error: please check the modal title');
return context.getInteractionResponder().successResponse();
}
} else {
console.log('error: room does not exist');
}
context.getInteractionResponder().successResponse();
}
public async executeViewClosedHandler(
context: UIKitViewSubmitInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const user = context.getInteractionData().user;
const room = await getRoom(read, user);
if (room) {
botNotifyCurrentUser(
read,
modify,
user,
room,
'Modal View was closed'
);
} else {
console.log('error: room not found');
}
return context.getInteractionResponder().successResponse();
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
// handle action when the subscriptions buttons are clicked
const blockActionHandler = new BlockActionHandler(
this,
read,
http,
modify,
persistence
);
return blockActionHandler.run(context, read, http, persistence, modify);
}
private async authorizationCallback(
authData: IAuthData,
user: IUser,
read: IRead,
modify: IModify,
http: IHttp,
persistence: IPersistence
) {
if (authData) {
const userData = await http.get('https://api.figma.com/v1/me', {
headers: {
Authorization: `Bearer ${authData.token}`
}
});
await registerAuthorizedUser(
read,
persistence,
user,
authData,
userData.data
);
}
const text = `Authentication was successful! ✨
You will now be notified for all your Figma comments and notifications.
You can subscribe to your team channel with files you want to receive notifications from.
`;
await sendDMToUser(read, modify, user, text, persistence);
}
public async onEnable(): Promise<boolean> {
this.user = (await this.getAccessors()
.reader.getUserReader()
.getByUsername(this.botName)) as IUser;
this.botName = 'Figma.bot';
return true;
}
public async onInstall(
context: IAppInstallationContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<void> {
const user = context.user;
const welcomeMessage = `You’ve successfully installed Figma Rocket.Chat app! Now the admin of the server has to create an app on figma.com and add the figma client id and client secret to the app settings in order to connect the server with figma.
1. Go to https://www.figma.com/developers/apps and create a new app.
2. Get the callback url from the app settings page ( -> admin panel -> apps) in rocket.chat and add it to the figma app.
3. Copy the client id and client secret and paste it in the app settings ( don't forget to click on save button )
:tada: You are all set!
Now your Figma comments and notifications will show up in the rocket chat server.
With Figma App, you can reply to file comments directly in a rocket chat channel. You will get notified when:
\xa0\xa0 • A new comment is added to a file you are collaborating on.
\xa0\xa0 • Someone replies to a comment you made.
\xa0\xa0 • You are tagged in a file.
\xa0\xa0 • You are invited to a file.
\xa0\xa0 • A file you are collaborating on is updated.
\xa0\xa0 • Notifications on Branches ( for organizations only ).
Some tips:
\xa0\xa0 • When you reply to a Figma comment here, your reply will automatically be added to the Figma file.
\xa0\xa0 • Type \` /figma connect \` to connect your figma account to the rocket.chat server.
\xa0\xa0 • Type \` /figma help \` for all the commands command.
\xa0\xa0 • Subscribe a file, team, or project in a channel using \` /figma subscribe \` and notify all the users that they will have to authenticate their figma accounts using \` /figma connect \`. `;
await sendDMToUser(read, modify, user, welcomeMessage, persistence);
}
public getOauth2ClientInstance(): IOAuth2Client {
if (!this.oauth2ClientInstance) {
this.oauth2ClientInstance = createOAuth2Client(
this,
this.oauth2Options
);
}
return this.oauth2ClientInstance;
}
protected async extendConfiguration(
configuration: IConfigurationExtend,
environmentRead: IEnvironmentRead
): Promise<void> {
await Promise.all([
this.getOauth2ClientInstance().setup(configuration),
configuration.slashCommands.provideSlashCommand(
new FigmaCommand(this)
)
]);
configuration.api.provideApi({
visibility: ApiVisibility.PUBLIC,
security: ApiSecurity.UNSECURE,
endpoints: [new figmaWebHooks(this)]
});
}
}