-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetFiles.ts
191 lines (185 loc) · 7.49 KB
/
getFiles.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,
IHttpResponse,
IModify,
IPersistence,
IRead
} from '@rocket.chat/apps-engine/definition/accessors';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { getAccessTokenForUser } from '../storage/users';
import { Subscription } from '../sdk/webhooks.sdk';
import { storedRoomData } from '../definition';
import { botMessageChannel, botNotifyCurrentUser } from './messages';
import { TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
import { blockAction } from '../enums/enums';
import { getFilesUrl } from './const';
export async function getFiles(
modify: IModify,
context,
persistence: IPersistence,
read: IRead,
data,
room: IRoom,
user: IUser,
http: IHttp
) {
const subscriptionStorage = new Subscription(
persistence,
read.getPersistenceReader()
);
const token = await getAccessTokenForUser(read, user);
const headers: { Authorization: string } = {
Authorization: `Bearer ${token?.token}`
};
function removeDuplicates(arr: string[]) {
const unique: string[] = [];
arr.forEach((element: string) => {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique;
}
subscriptionStorage
.getAllSubscriptions()
.then(async (subscriptions) => {
if (!subscriptions) {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any files subscriptions'
);
}
// get all subscriptions then check rom_data for every subscriptions if room_data.room_id matches with the current room then send all the files inside those arrays to the user
const room_files_ids: string[] = [];
for (const subscription of subscriptions) {
const roomData: storedRoomData[] = subscription.room_data;
for (const room_data of roomData) {
console.log('room data', room_data);
if (room_data.room_Id === room.id && room_data.file_Ids) {
room_files_ids.push(...room_data.file_Ids);
}
}
}
if (room_files_ids.length === 0) {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any file subscribed in this room'
);
}
const filesDataReqUrls = removeDuplicates(room_files_ids).map(
(file_id) => getFilesUrl(file_id)
);
try {
await Promise.all(
filesDataReqUrls.map((url) =>
http.get(url, {
headers
})
)
)
.then(async (project_response) => {
const fileDetails: {
name: string;
apiUrl: string;
id: string;
}[] = [];
if (project_response.length > 0) {
project_response.map((response) => {
const apiUrl = response.url;
const name = response.data.name;
const id = apiUrl.split('/')[5];
fileDetails.push({ name, apiUrl, id });
return;
});
console.log('files data - ', fileDetails);
const block = modify.getCreator().getBlockBuilder();
block.addSectionBlock({
text: {
type: TextObjectType.PLAINTEXT,
text: 'Files in this room'
}
});
fileDetails.map((file) => {
block.addSectionBlock({
text: {
type: TextObjectType.MARKDOWN,
text: `> *${file.name}*`
}
});
block.addActionsBlock({
blockId: blockAction.FILE_ACTIONS,
elements: [
block.newButtonElement({
actionId: blockAction.COMMENT,
text: block.newPlainTextObject(
'Comment'
),
value: `${file.apiUrl}`
}),
block.newButtonElement({
actionId: blockAction.OPEN_FILE,
text: block.newPlainTextObject(
'Open file'
),
url: `https://www.figma.com/file/${file.id}`,
value: `${file.id}`
})
]
});
});
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'',
block
);
} else {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Could not find any files subscribed in this room'
);
}
})
.catch(async (e) => {
console.log('error is - ', e);
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'There was an error'
);
});
} catch (e) {
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Error in fetching files. Please Report this issue'
);
}
})
.catch(async (error) => {
console.log('error: getting all subscriptions - ', error);
return await botNotifyCurrentUser(
read,
modify,
user,
room,
'Error in fetching files. Please Report this issue'
);
});
}