From ba9f7c02a072e65e9fec79181f4ea0aa9a6f51b2 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 29 Apr 2021 12:55:06 +1000 Subject: [PATCH] use delete_messages for multiple delete for opengroupv2 --- .../session/SessionToastContainer.tsx | 4 +- ts/interactions/conversation.ts | 50 +++++++++++-------- ts/opengroup/opengroupV2/ApiUtil.ts | 2 +- ts/opengroup/opengroupV2/OpenGroupAPIV2.ts | 45 ++--------------- .../opengroupV2/OpenGroupAPIV2CompactPoll.ts | 2 +- 5 files changed, 38 insertions(+), 65 deletions(-) diff --git a/ts/components/session/SessionToastContainer.tsx b/ts/components/session/SessionToastContainer.tsx index 0846aa5b7..54164007c 100644 --- a/ts/components/session/SessionToastContainer.tsx +++ b/ts/components/session/SessionToastContainer.tsx @@ -6,8 +6,8 @@ const SessionToastContainerPrivate = () => { return ( { - const msgId = msg.get('serverId'); - if (msgId) { - const isRemovedOnServer = await ApiV2.deleteSingleMessage(msgId, roomInfos); - return { message: msg, isRemovedOnServer }; - } else { - window.log.warn('serverId not valid for deletePublicMessage'); - return { message: msg, isRemovedOnServer: false }; + const validServerIdsToRemove = _.compact( + messages.map(msg => { + const serverId = msg.get('serverId'); + return serverId; + }) + ); + + const validMessageModelsToRemove = _.compact( + messages.map(msg => { + const serverId = msg.get('serverId'); + if (serverId) { + return msg; } + return undefined; }) ); - if (allDeletedResults.every(m => m.isRemovedOnServer)) { - window.log.info('all those serverIds where removed'); + + let allMessagesAreDeleted: boolean = false; + if (validServerIdsToRemove.length) { + allMessagesAreDeleted = await ApiV2.deleteMessageByServerIds( + validServerIdsToRemove, + roomInfos + ); + } + // remove only the messages we managed to remove on the server + if (allMessagesAreDeleted) { + window.log.info('Removed all those serverIds messages successfully'); + return validMessageModelsToRemove; } else { - if (allDeletedResults.some(m => m.isRemovedOnServer)) { - window.log.info('some of those serverIds where not removed'); - } else { - window.log.info('failed to remove all those serverIds message'); - } + window.log.info( + 'failed to remove all those serverIds message. not removing them locally neither' + ); + return []; } - // remove only the messag we managed to remove on the server - const msgToDeleteLocally = allDeletedResults - .filter(m => m.isRemovedOnServer) - .map(m => m.message); - return msgToDeleteLocally; } else if (convo.isOpenGroupV1()) { const channelAPI = await convo.getPublicSendData(); diff --git a/ts/opengroup/opengroupV2/ApiUtil.ts b/ts/opengroup/opengroupV2/ApiUtil.ts index 8ee4ec045..c6edb32fb 100644 --- a/ts/opengroup/opengroupV2/ApiUtil.ts +++ b/ts/opengroup/opengroupV2/ApiUtil.ts @@ -23,7 +23,7 @@ export type OpenGroupV2Request = { server: string; endpoint: string; // queryParams are used for post or get, but not the same way - queryParams?: Record; + queryParams?: Record; headers?: Record; isAuthRequired: boolean; serverPublicKey?: string; // if not provided, a db called will be made to try to get it. diff --git a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts index d82eac42c..cd61f3d9a 100644 --- a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts @@ -314,42 +314,6 @@ export const deleteAuthToken = async ({ } }; -export const getMessages = async ({ - serverUrl, - roomId, -}: OpenGroupRequestCommonType): Promise | null> => { - const roomInfos = await getV2OpenGroupRoomByRoomId({ serverUrl, roomId }); - if (!roomInfos) { - window.log.warn('Could not find this room getMessages'); - return []; - } - const { lastMessageFetchedServerID } = roomInfos; - - const queryParams = {} as Record; - if (lastMessageFetchedServerID) { - queryParams.from_server_id = lastMessageFetchedServerID; - } - - const request: OpenGroupV2Request = { - method: 'GET', - room: roomId, - server: serverUrl, - isAuthRequired: true, - endpoint: 'messages', - }; - const result = await sendOpenGroupV2Request(request); - const statusCode = parseStatusCodeFromOnionRequest(result); - if (statusCode !== 200) { - return []; - } - - // we have a 200 - const rawMessages = (result as any)?.result?.messages as Array>; - const validMessages = await parseMessages(rawMessages); - console.warn('validMessages', validMessages); - return validMessages; -}; - /** * Send the specified message to the specified room. * If an error happens, this function throws it @@ -420,16 +384,17 @@ export const unbanUser = async ( return isOk; }; -export const deleteSingleMessage = async ( - messageServerId: number, +export const deleteMessageByServerIds = async ( + idsToRemove: Array, roomInfos: OpenGroupRequestCommonType ): Promise => { const request: OpenGroupV2Request = { - method: 'DELETE', + method: 'POST', room: roomInfos.roomId, server: roomInfos.serverUrl, isAuthRequired: true, - endpoint: `messages/${messageServerId}`, + endpoint: 'delete_messages', + queryParams: { ids: idsToRemove }, }; const messageDeletedResult = await sendOpenGroupV2Request(request); const isOk = parseStatusCodeFromOnionRequest(messageDeletedResult) === 200; diff --git a/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts b/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts index 0fd0039c9..8bdaf4b0a 100644 --- a/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts +++ b/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts @@ -61,7 +61,7 @@ const getCompactPollRequest = async ( roomRequestContent.from_deletion_server_id = lastMessageDeletedServerID; // } // if (lastMessageFetchedServerID) { - roomRequestContent.from_message_server_id = lastMessageFetchedServerID || 1; + roomRequestContent.from_message_server_id = lastMessageFetchedServerID; // } console.warn('compactPoll, ', roomRequestContent);