Skip to content

Commit

Permalink
Merge pull request #2598 from oxen-io/fix-use-token-from-poll-nfo-as-…
Browse files Browse the repository at this point in the history
…convo-id

fix: use token from first room info to build conversationId for sogs
  • Loading branch information
Bilb committed Nov 9, 2022
2 parents 51e0d80 + 16d1404 commit f2e8fbd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
24 changes: 14 additions & 10 deletions ts/session/apis/open_group_api/opengroupV2/JoinOpenGroupV2.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 26 additions & 16 deletions ts/session/apis/open_group_api/opengroupV2/OpenGroupManagerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -163,47 +163,57 @@ 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
});
await conversation.commit();

// start polling this room
this.addRoomToPolledRooms([room]);
this.addRoomToPolledRooms([updatedRoom]);

return conversation;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down

0 comments on commit f2e8fbd

Please sign in to comment.