fix: use token from first room info to build conversationId for sogs

pull/2598/head
Audric Ackermann 2 years ago
parent 51e0d80c21
commit 16d14043b8

@ -1,5 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import { OpenGroupV2Room } from '../../../../data/opengroups'; import { OpenGroupV2Room } from '../../../../data/opengroups';
import { ConversationModel } from '../../../../models/conversation';
import { getConversationController } from '../../../conversations'; import { getConversationController } from '../../../conversations';
import { PromiseUtils, ToastUtils } from '../../../utils'; import { PromiseUtils, ToastUtils } from '../../../utils';
@ -57,9 +58,12 @@ export function parseOpenGroupV2(urlWithPubkey: string): OpenGroupV2Room | undef
* @param room The room id to join * @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) * @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) { if (!room.serverUrl || !room.roomId || room.roomId.length < 2 || !room.serverPublicKey) {
return; return undefined;
} }
const serverUrl = room.serverUrl; const serverUrl = room.serverUrl;
@ -97,6 +101,7 @@ async function joinOpenGroupV2(room: OpenGroupV2Room, fromConfigMessage: boolean
if (!fromConfigMessage) { if (!fromConfigMessage) {
await forceSyncConfigurationNowIfNeeded(); await forceSyncConfigurationNowIfNeeded();
} }
return conversation;
} catch (e) { } catch (e) {
window?.log?.error('Could not join open group v2', e.message); window?.log?.error('Could not join open group v2', e.message);
throw e; throw e;
@ -154,24 +159,23 @@ export async function joinOpenGroupV2WithUIEvents(
uiCallback?.({ loadingState: 'started', conversationKey: conversationID }); uiCallback?.({ loadingState: 'started', conversationKey: conversationID });
await joinOpenGroupV2(parsedRoom, fromConfigMessage); const convoCreated = await joinOpenGroupV2(parsedRoom, fromConfigMessage);
const isConvoCreated = getConversationController().get(conversationID); if (convoCreated) {
if (isConvoCreated) {
if (showToasts) { if (showToasts) {
ToastUtils.pushToastSuccess( ToastUtils.pushToastSuccess(
'connectToServerSuccess', 'connectToServerSuccess',
window.i18n('connectToServerSuccess') window.i18n('connectToServerSuccess')
); );
} }
uiCallback?.({ loadingState: 'finished', conversationKey: conversationID }); uiCallback?.({ loadingState: 'finished', conversationKey: convoCreated?.id });
return true; return true;
} else {
if (showToasts) {
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
}
} }
if (showToasts) {
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
}
uiCallback?.({ loadingState: 'failed', conversationKey: conversationID }); uiCallback?.({ loadingState: 'failed', conversationKey: conversationID });
} catch (error) { } catch (error) {
window?.log?.warn('got error while joining open group:', error.message); 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 { OpenGroupRequestCommonType } from './ApiUtil';
import { OpenGroupServerPoller } from './OpenGroupServerPoller'; import { OpenGroupServerPoller } from './OpenGroupServerPoller';
import _ from 'lodash'; import _, { clone, isEqual } from 'lodash';
import autoBind from 'auto-bind'; import autoBind from 'auto-bind';
import { ConversationTypeEnum } from '../../../../models/conversationAttributes'; import { ConversationTypeEnum } from '../../../../models/conversationAttributes';
import { openGroupV2GetRoomInfoViaOnionV4 } from '../sogsv3/sogsV3RoomInfos'; import { openGroupV2GetRoomInfoViaOnionV4 } from '../sogsv3/sogsV3RoomInfos';
@ -153,7 +153,7 @@ export class OpenGroupManagerV2 {
roomId: string, roomId: string,
serverPublicKey: string serverPublicKey: string
): Promise<ConversationModel | undefined> { ): Promise<ConversationModel | undefined> {
const conversationId = getOpenGroupV2ConversationId(serverUrl, roomId); let conversationId = getOpenGroupV2ConversationId(serverUrl, roomId);
if (getConversationController().get(conversationId)) { if (getConversationController().get(conversationId)) {
// Url incorrect or server not compatible // 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 // here, the convo does not exist. Make sure the db is clean too
await OpenGroupData.removeV2OpenGroupRoom(conversationId); await OpenGroupData.removeV2OpenGroupRoom(conversationId);
const room: OpenGroupV2Room = {
serverUrl,
roomId,
conversationId,
serverPublicKey,
};
try { 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 // save the pubkey to the db right now, the request for room Info
// will need it and access it from the db // will need it and access it from the db
await OpenGroupData.saveV2OpenGroupRoom(room); await OpenGroupData.saveV2OpenGroupRoom(room);
const roomInfos = await openGroupV2GetRoomInfoViaOnionV4({ const roomInfos = await openGroupV2GetRoomInfoViaOnionV4({
serverPubkey: serverPublicKey, serverPubkey: serverPublicKey,
serverUrl, serverUrl,
roomId, roomId,
}); });
if (!roomInfos) {
if (!roomInfos || !roomInfos.id) {
throw new Error('Invalid open group roomInfo result'); 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( const conversation = await getConversationController().getOrCreateAndWait(
conversationId, conversationId,
ConversationTypeEnum.GROUP ConversationTypeEnum.GROUP
); );
room.imageID = roomInfos.imageId || undefined; updatedRoom.imageID = roomInfos.imageId || undefined;
room.roomName = roomInfos.name || undefined; updatedRoom.roomName = roomInfos.name || undefined;
room.capabilities = roomInfos.capabilities; updatedRoom.capabilities = roomInfos.capabilities;
await OpenGroupData.saveV2OpenGroupRoom(room); await OpenGroupData.saveV2OpenGroupRoom(updatedRoom);
// mark active so it's not in the contacts list but in the conversation list // mark active so it's not in the contacts list but in the conversation list
// mark isApproved as this is a public chat // mark isApproved as this is a public chat
conversation.set({ conversation.set({
active_at: Date.now(), active_at: Date.now(),
displayNameInProfile: room.roomName, displayNameInProfile: updatedRoom.roomName,
isApproved: true, isApproved: true,
didApproveMe: true, didApproveMe: true,
isTrustedForAttachmentDownload: true, // we always trust attachments when sent to an opengroup isTrustedForAttachmentDownload: true, // we always trust attachments when sent to an opengroup
@ -203,7 +213,7 @@ export class OpenGroupManagerV2 {
await conversation.commit(); await conversation.commit();
// start polling this room // start polling this room
this.addRoomToPolledRooms([room]); this.addRoomToPolledRooms([updatedRoom]);
return conversation; return conversation;
} catch (e) { } catch (e) {

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

Loading…
Cancel
Save