Skip to content

Commit 2cf3cf8

Browse files
author
meow12
authored
feat(server-events): upload message files (#71)
* feat(server-events): upload message files allows to upload message files. Additionally improved work with messages. * formatting
1 parent 49eb05b commit 2cf3cf8

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

src/components/files.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,69 @@ export async function uploadRecordFile({
203203
return response;
204204
}
205205

206+
export async function uploadMessageFile({
207+
secretKey,
208+
fileUri,
209+
file,
210+
base64,
211+
fileType,
212+
fileName,
213+
channelId,
214+
}) {
215+
if (base64) {
216+
const response = await server.loadJson(
217+
`${Config.apiUrl}${Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.FILES.UPLOAD}`,
218+
{
219+
method: 'POST',
220+
headers: {
221+
'X-CM-ProjectId': Config.projectId,
222+
Authorization: `Bearer ${secretKey || Config.secretKey}`,
223+
'Content-Type': 'application/json',
224+
Accept: 'application/json',
225+
},
226+
body: JSON.stringify({
227+
channelId,
228+
base64File: {data: base64, contentType: fileType, fileName},
229+
}),
230+
}
231+
);
232+
233+
return response;
234+
}
235+
236+
const formData = new FormData();
237+
if (channelId != null && channelId !== undefined) {
238+
formData.append('channelId', channelId);
239+
}
240+
241+
if (fileUri) {
242+
const finalFilename =
243+
fileName || fileUri.substring(fileUri.lastIndexOf('/') + 1);
244+
245+
formData.append('file', {
246+
uri: fileUri,
247+
name: finalFilename,
248+
type: fileType,
249+
});
250+
} else {
251+
formData.append('file', file);
252+
}
253+
254+
const response = await server.loadJson(
255+
`${Config.apiUrl}${Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.FILES.UPLOAD}`,
256+
{
257+
method: 'POST',
258+
headers: {
259+
'X-CM-ProjectId': Config.projectId,
260+
Authorization: `Bearer ${secretKey || Config.secretKey}`,
261+
},
262+
body: formData,
263+
}
264+
);
265+
266+
return response;
267+
}
268+
206269
export function getFilePath(directory, fileName) {
207270
return `${Config.baseFilePath}/${directory}/${fileName}`;
208271
}

src/components/sse.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export async function getGroups({
5454
sort,
5555
includeChannels,
5656
includeUsers,
57+
includeNotSeenCount,
5758
}) {
5859
const request = {
5960
pageSize: pageSize || Config.tablePageSize,
@@ -62,6 +63,7 @@ export async function getGroups({
6263
sort: objectOrStringToString(sort),
6364
includeChannels,
6465
includeUsers,
66+
includeNotSeenCount,
6567
};
6668
const requestUrl = `${
6769
Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.GET_GROUPS
@@ -83,6 +85,38 @@ export async function getGroups({
8385
return response;
8486
}
8587

88+
export async function getGroup({
89+
secretKey,
90+
id,
91+
includeChannels,
92+
includeUsers,
93+
includeNotSeenCount,
94+
}) {
95+
const request = {
96+
includeChannels,
97+
includeUsers,
98+
includeNotSeenCount,
99+
};
100+
const requestUrl = `${Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.GET_GROUP(
101+
id
102+
)}?${toQueryString(request)}`;
103+
104+
const response = await server.loadJson(
105+
`${Config.eventsApiUrl}${requestUrl}`,
106+
{
107+
method: 'GET',
108+
headers: {
109+
'X-CM-ProjectId': Config.projectId,
110+
Authorization: `Bearer ${secretKey || Config.secretKey}`,
111+
Accept: 'application/json',
112+
'Content-Type': 'application/json',
113+
},
114+
body: null,
115+
}
116+
);
117+
return response;
118+
}
119+
86120
export async function createChannel({secretKey, groupId, title, meta, users}) {
87121
const response = await server.loadJson(
88122
`${
@@ -163,7 +197,13 @@ export async function getChannels({
163197
return response;
164198
}
165199

166-
export async function sendMessage({secretKey, channelId, message, meta}) {
200+
export async function sendMessage({
201+
secretKey,
202+
channelId,
203+
message,
204+
meta,
205+
fileIds,
206+
}) {
167207
const response = await server.loadJson(
168208
`${Config.eventsApiUrl}${Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.SEND_MESSAGE}`,
169209
{
@@ -178,6 +218,28 @@ export async function sendMessage({secretKey, channelId, message, meta}) {
178218
channelId,
179219
message,
180220
meta,
221+
fileIds,
222+
}),
223+
}
224+
);
225+
226+
return response;
227+
}
228+
229+
export async function readMessages({secretKey, channelId, ids}) {
230+
const response = await server.loadJson(
231+
`${Config.eventsApiUrl}${Endpoints.PROJECT.NOTIFICATIONS.SERVER_EVENTS.READ_MESSAGES}`,
232+
{
233+
method: 'POST',
234+
headers: {
235+
'X-CM-ProjectId': Config.projectId,
236+
Authorization: `Bearer ${secretKey || Config.secretKey}`,
237+
Accept: 'application/json',
238+
'Content-Type': 'application/json',
239+
},
240+
body: JSON.stringify({
241+
channelId,
242+
ids,
181243
}),
182244
}
183245
);

src/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const CONFIG = {
7777
CREATE_GROUP: '/v2/notifications/server-events/groups',
7878
DELETE_GROUP: (id) => `/v2/notifications/server-events/groups/${id}`,
7979
GET_GROUPS: '/v2/notifications/server-events/groups',
80+
GET_GROUP: (id) => `/v2/notifications/server-events/groups/${id}`,
8081
CREATE_CHANNEL: (groupId) =>
8182
`/v2/notifications/server-events/groups/${groupId}/channels`,
8283
DELETE_CHANNEL: (groupId, id) =>
@@ -85,12 +86,16 @@ export const CONFIG = {
8586
`/v2/notifications/server-events/groups/${groupId}/channels`,
8687
GET_MESSAGES: '/v2/notifications/server-events/messages',
8788
SEND_MESSAGE: '/v2/notifications/server-events/messages',
89+
READ_MESSAGES: '/v2/notifications/server-events/messages/read',
8890
AUTHORIZE_CONNECTION:
8991
'/v2/notifications/server-events/connections/initialize',
9092
OPEN_CONNECTION: '/v2/notifications/server-events/connections/open',
9193
HEARTBEAT_CONNECTION:
9294
'/v2/notifications/server-events/connections/health',
9395
CLOSE_CONNECTION: '/v2/notifications/server-events/connections/close',
96+
FILES: {
97+
UPLOAD: '/v2/notifications/server-events/messages/files',
98+
},
9499
},
95100
},
96101
MEMBERSHIP: {

0 commit comments

Comments
 (0)