forked from RocketChat/Apps.Github22
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExecuteBlockActionHandler.ts
191 lines (182 loc) · 9.16 KB
/
ExecuteBlockActionHandler.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
import {
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { IApp } from "@rocket.chat/apps-engine/definition/IApp";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { basicQueryMessage } from "../helpers/basicQueryMessage";
import { ModalsEnum } from "../enum/Modals";
import { fileCodeModal } from "../modals/fileCodeModal";
import {
IUIKitResponse,
UIKitBlockInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { AddSubscriptionModal } from "../modals/addSubscriptionsModal";
import { deleteSubsciptionsModal } from "../modals/deleteSubscriptions";
import { deleteSubscription, updateSubscription } from "../helpers/githubSDK";
import { Subscription } from "../persistance/subscriptions";
import { getAccessTokenForUser } from "../persistance/auth";
import { GithubApp } from "../GithubApp";
import { IAuthData } from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";
import { storeInteractionRoomData, getInteractionRoomData } from "../persistance/roomInteraction";
import { sendNotification } from "../lib/message";
import { subsciptionsModal } from "../modals/subscriptionsModal";
export class ExecuteBlockActionHandler {
constructor(
private readonly app: GithubApp,
private readonly read: IRead,
private readonly http: IHttp,
private readonly modify: IModify,
private readonly persistence: IPersistence
) { }
public async run(
context: UIKitBlockInteractionContext
): Promise<IUIKitResponse> {
const data = context.getInteractionData();
try {
const { actionId } = data;
switch (actionId) {
case "githubDataSelect": {
try {
const param = data.value;
let query: String = "";
let lengthOfRepoString: number = 0;
if (param && param.length) {
let i = param.length - 1;
for (
;
i >= 0 && data.value && data.value[i] != "/";
i--
) {
query = data.value[i] + query;
}
lengthOfRepoString = i;
}
const repository = param?.substring(
0,
lengthOfRepoString
) as String;
const room: IRoom = context.getInteractionData()
.room as IRoom;
await basicQueryMessage({
query,
repository,
room,
read: this.read,
persistence: this.persistence,
modify: this.modify,
http: this.http,
});
return {
success: true,
};
} catch (err) {
console.error(err);
return {
success: false,
};
}
break;
}
case ModalsEnum.VIEW_FILE_ACTION: {
const codeModal = await fileCodeModal({
data,
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
uikitcontext: context,
});
return context
.getInteractionResponder()
.openModalViewResponse(codeModal);
}
case ModalsEnum.OPEN_ADD_SUBSCRIPTIONS_MODAL: {
const addSubscriptionModal = await AddSubscriptionModal({
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
uikitcontext: context
})
return context
.getInteractionResponder()
.openModalViewResponse(addSubscriptionModal);
}
case ModalsEnum.OPEN_DELETE_SUBSCRIPTIONS_MODAL: {
const addSubscriptionModal = await deleteSubsciptionsModal({
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
uikitcontext: context
})
return context
.getInteractionResponder()
.openModalViewResponse(addSubscriptionModal);
}
case ModalsEnum.DELETE_SUBSCRIPTION_ACTION: {
let { user, room } = await context.getInteractionData();
let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config) as IAuthData;
let value: string = context.getInteractionData().value as string;
let splitted = value.split(',');
if (splitted.length == 2 && accessToken.token) {
let repoName = splitted[0];
let hookId = splitted[1];
let roomId;
if (room?.id) {
roomId = room.id;
await storeInteractionRoomData(this.persistence, user.id, roomId);
} else {
roomId = (await getInteractionRoomData(this.read.getPersistenceReader(), user.id)).roomId;
}
//delete the susbscriptions for persistance
let subscriptionStorage = new Subscription(this.persistence, this.read.getPersistenceReader());
let oldSubscriptions = await subscriptionStorage.getSubscriptionsByRepo(repoName, user.id);
await subscriptionStorage.deleteSubscriptionsByRepoUser(repoName, roomId, user.id);
//check if any subscription events of the repo is left in any other room
let eventSubscriptions = new Map<string, boolean>;
for (let subsciption of oldSubscriptions) {
eventSubscriptions.set(subsciption.event, false);
}
let updatedsubscriptions = await subscriptionStorage.getSubscriptionsByRepo(repoName, user.id);
if (updatedsubscriptions.length == 0) {
await deleteSubscription(this.http, repoName, accessToken.token, hookId);
} else {
for (let subsciption of updatedsubscriptions) {
eventSubscriptions.set(subsciption.event, true);
}
let updatedEvents: Array<string> = [];
let sameEvents = true;
for (let [event, present] of eventSubscriptions) {
sameEvents = sameEvents && present;
if (present) {
updatedEvents.push(event);
}
}
if (updatedEvents.length && !sameEvents) {
let response = await updateSubscription(this.http, repoName, accessToken.token, hookId, updatedEvents);
}
}
let userRoom = await this.read.getRoomReader().getById(roomId) as IRoom;
await sendNotification(this.read, this.modify, user, userRoom, `Unsubscribed to ${repoName} 🔕`);
}
const modal = await deleteSubsciptionsModal({ modify: this.modify, read: this.read, persistence: this.persistence, http: this.http, uikitcontext: context });
await this.modify.getUiController().updateModalView(modal, { triggerId: context.getInteractionData().triggerId }, context.getInteractionData().user);
break;
}
case ModalsEnum.SUBSCRIPTION_REFRESH_ACTION:{
const modal = await subsciptionsModal({ modify: this.modify, read: this.read, persistence: this.persistence, http: this.http, uikitcontext: context });
await this.modify.getUiController().updateModalView(modal, { triggerId: context.getInteractionData().triggerId }, context.getInteractionData().user);
break;
}
}
} catch (error) {
console.log(error);
}
return context.getInteractionResponder().successResponse();
}
}