feat: abstracted getAllValidOpenGroupV2ConversationRoomInfos function and handled the response correctly

just need to do testing
pull/2660/head
William Grant 2 years ago
parent db00ed8827
commit 19025cd7e0

@ -13,9 +13,7 @@ import { Data } from '../../data/data';
import { deleteAllLogs } from '../../node/logs';
import { SessionRadioGroup } from '../basic/SessionRadioGroup';
import { clearInbox } from '../../session/apis/open_group_api/sogsv3/sogsV3ClearInbox';
import { OpenGroupData } from '../../data/opengroups';
import { getOpenGroupV2ConversationId } from '../../session/apis/open_group_api/utils/OpenGroupUtils';
import { getOpenGroupManager } from '../../session/apis/open_group_api/opengroupV2/OpenGroupManagerV2';
import { getAllValidOpenGroupV2ConversationRoomInfos } from '../../session/apis/open_group_api/utils/OpenGroupUtils';
const deleteDbLocally = async () => {
window?.log?.info('last message sent successfully. Deleting everything');
@ -61,40 +59,13 @@ async function deleteEverythingAndNetworkData() {
// DELETE EVERYTHING ON NETWORK, AND THEN STUFF LOCALLY STORED
// a bit of duplicate code below, but it's easier to follow every case like that (helped with returns)
// send delete for all sogs message requests
// TODO extract function from within startPollingBouncy() to replace some of the code below.
// fetch all open group conversations
const allConvos = await OpenGroupData.getAllOpenGroupV2Conversations();
let allRoomInfosMap = OpenGroupData.getAllV2OpenGroupRoomsMap();
// this is time for some cleanup!
// We consider the conversations are our source-of-truth,
// so if there is a roomInfo without an associated convo, we remove it
if (allRoomInfosMap) {
await Promise.all(
[...allRoomInfosMap.values()].map(async infos => {
try {
const roomConvoId = getOpenGroupV2ConversationId(infos.serverUrl, infos.roomId);
if (!allConvos.get(roomConvoId)) {
// remove the roomInfos locally for this open group room
await OpenGroupData.removeV2OpenGroupRoom(roomConvoId);
getOpenGroupManager().removeRoomFromPolledRooms(infos);
// no need to remove it from the ConversationController, the convo is already not there
}
} catch (e) {
window?.log?.warn('cleanup roomInfos error', e);
}
})
);
}
allRoomInfosMap = OpenGroupData.getAllV2OpenGroupRoomsMap();
// clear all sogs inboxes (includes message requests)
const allRoomInfosMap = await getAllValidOpenGroupV2ConversationRoomInfos();
if (allRoomInfosMap) {
const allRoomInfos = Object.values(allRoomInfosMap);
// clear each inbox per sogs
for (let i = 0; i < allRoomInfos.length; i++) {
// TODO handle the response to confirm this works
// TODO CONTINUE
// TODO will need to test with a dummy account with some message requests and then if we restore from seed there should be no message requests.
// TODO CONTINUE testing - use a dummy account with some message requests and then if we restore from seed there should be no message requests.
await clearInbox(allRoomInfos[i]);
}
}
@ -234,6 +205,7 @@ export const DeleteAccountModal = () => {
}
}
};
const onDeleteEverythingAndNetworkData = async () => {
if (!isLoading) {
setIsLoading(true);

@ -2,7 +2,10 @@ import { OpenGroupData, OpenGroupV2Room } from '../../../../data/opengroups';
import { ConversationModel } from '../../../../models/conversation';
import { getConversationController } from '../../../conversations';
import { allowOnlyOneAtATime } from '../../../utils/Promise';
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
import {
getAllValidOpenGroupV2ConversationRoomInfos,
getOpenGroupV2ConversationId,
} from '../utils/OpenGroupUtils';
import {
defaultServer,
defaultServerHost,
@ -124,32 +127,7 @@ export class OpenGroupManagerV2 {
if (this.isPolling) {
return;
}
const allConvos = await OpenGroupData.getAllOpenGroupV2Conversations();
let allRoomInfos = OpenGroupData.getAllV2OpenGroupRoomsMap();
// this is time for some cleanup!
// We consider the conversations are our source-of-truth,
// so if there is a roomInfo without an associated convo, we remove it
if (allRoomInfos) {
await Promise.all(
[...allRoomInfos.values()].map(async infos => {
try {
const roomConvoId = getOpenGroupV2ConversationId(infos.serverUrl, infos.roomId);
if (!allConvos.get(roomConvoId)) {
// remove the roomInfos locally for this open group room
await OpenGroupData.removeV2OpenGroupRoom(roomConvoId);
getOpenGroupManager().removeRoomFromPolledRooms(infos);
// no need to remove it from the ConversationController, the convo is already not there
}
} catch (e) {
window?.log?.warn('cleanup roomInfos error', e);
}
})
);
}
// refresh our roomInfos list
allRoomInfos = OpenGroupData.getAllV2OpenGroupRoomsMap();
const allRoomInfos = await getAllValidOpenGroupV2ConversationRoomInfos();
if (allRoomInfos) {
this.addRoomToPolledRooms([...allRoomInfos.values()]);
}

@ -14,52 +14,53 @@ type OpenGroupClearInboxResponse = {
};
export const clearInbox = async (roomInfos: OpenGroupRequestCommonType): Promise<boolean> => {
let success = false;
const converationId = getOpenGroupV2ConversationId(roomInfos.serverUrl, roomInfos.roomId);
const conversation = await Data.getConversationById(converationId);
if (!conversation) {
window.log.warn(`clear inbox Matching conversation not found in db`);
// we failed
return false;
}
const options: Array<OpenGroupBatchRow> = [
{
type: 'inbox',
inbox: {
type: 'delete',
window.log.warn(`clearInbox Matching conversation not found in db`);
} else {
const options: Array<OpenGroupBatchRow> = [
{
type: 'inbox',
inbox: {
type: 'delete',
},
},
},
];
];
const result = await sogsBatchSend(
roomInfos.serverUrl,
new Set([roomInfos.roomId]),
new AbortController().signal,
options,
'batch'
);
const result = await sogsBatchSend(
roomInfos.serverUrl,
new Set([roomInfos.roomId]),
new AbortController().signal,
options,
'batch'
);
if (!result) {
throw new Error('Could not clearInbox, res is invalid');
}
if (!result) {
throw new Error('Could not clearInbox, res is invalid');
}
const rawMessage = (result.body && (result.body[0].body as OpenGroupClearInboxResponse)) || null;
if (!rawMessage) {
throw new Error('clearInbox parsing failed');
}
const rawMessage =
(result.body && (result.body[0].body as OpenGroupClearInboxResponse)) || null;
if (!rawMessage) {
throw new Error('clearInbox parsing failed');
}
try {
if (batchGlobalIsSuccess(result) && batchFirstSubIsSuccess(result)) {
// we succeeded
return true;
} else {
// we failed
return false;
try {
if (batchGlobalIsSuccess(result) && batchFirstSubIsSuccess(result)) {
success = true;
window.log.info(`clearInbox ${rawMessage.deleted} messages deleted from ${converationId} `);
}
} catch (e) {
window?.log?.error("clearInbox Can't decode JSON body");
}
} catch (e) {
window?.log?.error("clearInbox Can't decode JSON body");
}
return false;
if (!success) {
window.log.info(`clearInbox message deletion failed for ${converationId} `);
}
return success;
};

@ -1,6 +1,7 @@
import _ from 'lodash';
import { OpenGroupV2Room } from '../../../../data/opengroups';
import { OpenGroupData, OpenGroupV2Room } from '../../../../data/opengroups';
import { OpenGroupRequestCommonType } from '../opengroupV2/ApiUtil';
import { getOpenGroupManager } from '../opengroupV2/OpenGroupManagerV2';
const protocolRegex = new RegExp('https?://');
@ -115,3 +116,38 @@ export function getOpenGroupV2FromConversationId(
export function isOpenGroupV2(conversationId: string) {
return openGroupV2ConversationIdRegex.test(conversationId);
}
/**
* Fetches all roomInfos for all of our opengroup conversations.
* We consider the conversations as our source-of-truth, so if there is a roomInfo without an associated convo, we remove it before returning.
* @returns A map of conversationIds to roomInfos for all valid open group conversations or undefined
*/
export async function getAllValidOpenGroupV2ConversationRoomInfos() {
const allConvos = await OpenGroupData.getAllOpenGroupV2Conversations();
let allRoomInfos = OpenGroupData.getAllV2OpenGroupRoomsMap();
if (allRoomInfos) {
await Promise.all(
[...allRoomInfos.values()].map(async infos => {
try {
const roomConvoId = getOpenGroupV2ConversationId(infos.serverUrl, infos.roomId);
if (!allConvos.get(roomConvoId)) {
// remove the roomInfos locally for this open group room
await OpenGroupData.removeV2OpenGroupRoom(roomConvoId);
getOpenGroupManager().removeRoomFromPolledRooms(infos);
// no need to remove it from the ConversationController, the convo is already not there
}
} catch (e) {
window?.log?.warn(
'getAllValidOpenGroupV2ConversationRoomInfos cleanup roomInfos error',
e
);
}
})
);
}
// refresh our roomInfos list
allRoomInfos = OpenGroupData.getAllV2OpenGroupRoomsMap();
return allRoomInfos;
}

Loading…
Cancel
Save