Merge pull request #2598 from oxen-io/fix-use-token-from-poll-nfo-as-convo-id

fix: use token from first room info to build conversationId for sogs
pull/2607/head
Audric Ackermann 2 years ago committed by GitHub
commit f2e8fbdeb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
import _ from 'lodash';
import { OpenGroupV2Room } from '../../../../data/opengroups';
import { ConversationModel } from '../../../../models/conversation';
import { getConversationController } from '../../../conversations';
import { PromiseUtils, ToastUtils } from '../../../utils';
@ -57,9 +58,12 @@ export function parseOpenGroupV2(urlWithPubkey: string): OpenGroupV2Room | undef
* @param room The room id to join
* @param publicKey The server publicKey. It comes from the joining link. (or is already here for the default open group server)
*/
async function joinOpenGroupV2(room: OpenGroupV2Room, fromConfigMessage: boolean): Promise<void> {
async function joinOpenGroupV2(
room: OpenGroupV2Room,
fromConfigMessage: boolean
): Promise<ConversationModel | undefined> {
if (!room.serverUrl || !room.roomId || room.roomId.length < 2 || !room.serverPublicKey) {
return;
return undefined;
}
const serverUrl = room.serverUrl;
@ -97,6 +101,7 @@ async function joinOpenGroupV2(room: OpenGroupV2Room, fromConfigMessage: boolean
if (!fromConfigMessage) {
await forceSyncConfigurationNowIfNeeded();
}
return conversation;
} catch (e) {
window?.log?.error('Could not join open group v2', e.message);
throw e;
@ -154,24 +159,23 @@ export async function joinOpenGroupV2WithUIEvents(
uiCallback?.({ loadingState: 'started', conversationKey: conversationID });
await joinOpenGroupV2(parsedRoom, fromConfigMessage);
const convoCreated = await joinOpenGroupV2(parsedRoom, fromConfigMessage);
const isConvoCreated = getConversationController().get(conversationID);
if (isConvoCreated) {
if (convoCreated) {
if (showToasts) {
ToastUtils.pushToastSuccess(
'connectToServerSuccess',
window.i18n('connectToServerSuccess')
);
}
uiCallback?.({ loadingState: 'finished', conversationKey: conversationID });
uiCallback?.({ loadingState: 'finished', conversationKey: convoCreated?.id });
return true;
} else {
if (showToasts) {
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
}
}
if (showToasts) {
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
}
uiCallback?.({ loadingState: 'failed', conversationKey: conversationID });
} catch (error) {
window?.log?.warn('got error while joining open group:', error.message);

@ -6,7 +6,7 @@ import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
import { OpenGroupRequestCommonType } from './ApiUtil';
import { OpenGroupServerPoller } from './OpenGroupServerPoller';
import _ from 'lodash';
import _, { clone, isEqual } from 'lodash';
import autoBind from 'auto-bind';
import { ConversationTypeEnum } from '../../../../models/conversationAttributes';
import { openGroupV2GetRoomInfoViaOnionV4 } from '../sogsv3/sogsV3RoomInfos';
@ -153,7 +153,7 @@ export class OpenGroupManagerV2 {
roomId: string,
serverPublicKey: string
): Promise<ConversationModel | undefined> {
const conversationId = getOpenGroupV2ConversationId(serverUrl, roomId);
let conversationId = getOpenGroupV2ConversationId(serverUrl, roomId);
if (getConversationController().get(conversationId)) {
// Url incorrect or server not compatible
@ -163,39 +163,49 @@ export class OpenGroupManagerV2 {
// here, the convo does not exist. Make sure the db is clean too
await OpenGroupData.removeV2OpenGroupRoom(conversationId);
const room: OpenGroupV2Room = {
serverUrl,
roomId,
conversationId,
serverPublicKey,
};
try {
const room: OpenGroupV2Room = {
serverUrl,
roomId,
conversationId,
serverPublicKey,
};
const updatedRoom = clone(room);
// save the pubkey to the db right now, the request for room Info
// will need it and access it from the db
await OpenGroupData.saveV2OpenGroupRoom(room);
const roomInfos = await openGroupV2GetRoomInfoViaOnionV4({
serverPubkey: serverPublicKey,
serverUrl,
roomId,
});
if (!roomInfos) {
if (!roomInfos || !roomInfos.id) {
throw new Error('Invalid open group roomInfo result');
}
updatedRoom.roomId = roomInfos.id;
conversationId = getOpenGroupV2ConversationId(serverUrl, roomInfos.id);
updatedRoom.conversationId = conversationId;
if (!isEqual(room, updatedRoom)) {
await OpenGroupData.removeV2OpenGroupRoom(conversationId);
await OpenGroupData.saveV2OpenGroupRoom(updatedRoom);
}
const conversation = await getConversationController().getOrCreateAndWait(
conversationId,
ConversationTypeEnum.GROUP
);
room.imageID = roomInfos.imageId || undefined;
room.roomName = roomInfos.name || undefined;
room.capabilities = roomInfos.capabilities;
await OpenGroupData.saveV2OpenGroupRoom(room);
updatedRoom.imageID = roomInfos.imageId || undefined;
updatedRoom.roomName = roomInfos.name || undefined;
updatedRoom.capabilities = roomInfos.capabilities;
await OpenGroupData.saveV2OpenGroupRoom(updatedRoom);
// mark active so it's not in the contacts list but in the conversation list
// mark isApproved as this is a public chat
conversation.set({
active_at: Date.now(),
displayNameInProfile: room.roomName,
displayNameInProfile: updatedRoom.roomName,
isApproved: true,
didApproveMe: true,
isTrustedForAttachmentDownload: true, // we always trust attachments when sent to an opengroup
@ -203,7 +213,7 @@ export class OpenGroupManagerV2 {
await conversation.commit();
// start polling this room
this.addRoomToPolledRooms([room]);
this.addRoomToPolledRooms([updatedRoom]);
return conversation;
} catch (e) {

@ -143,9 +143,10 @@ export class OpenGroupServerPoller {
window?.log?.info('this is not the correct ServerPoller');
return;
}
if (this.roomIdsToPoll.has(room.roomId)) {
if (this.roomIdsToPoll.has(room.roomId) || this.roomIdsToPoll.has(room.roomId.toLowerCase())) {
window?.log?.info(`Removing ${room.roomId} from polling for ${this.serverUrl}`);
this.roomIdsToPoll.delete(room.roomId);
this.roomIdsToPoll.delete(room.roomId.toLowerCase());
} else {
window?.log?.info(
`Cannot remove polling of ${room.roomId} as it is not polled on ${this.serverUrl}`

Loading…
Cancel
Save