Skip to content

Commit f2e8fbd

Browse files
authored
Merge pull request oxen-io#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
2 parents 51e0d80 + 16d1404 commit f2e8fbd

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

ts/session/apis/open_group_api/opengroupV2/JoinOpenGroupV2.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash';
22
import { OpenGroupV2Room } from '../../../../data/opengroups';
3+
import { ConversationModel } from '../../../../models/conversation';
34
import { getConversationController } from '../../../conversations';
45
import { PromiseUtils, ToastUtils } from '../../../utils';
56

@@ -57,9 +58,12 @@ export function parseOpenGroupV2(urlWithPubkey: string): OpenGroupV2Room | undef
5758
* @param room The room id to join
5859
* @param publicKey The server publicKey. It comes from the joining link. (or is already here for the default open group server)
5960
*/
60-
async function joinOpenGroupV2(room: OpenGroupV2Room, fromConfigMessage: boolean): Promise<void> {
61+
async function joinOpenGroupV2(
62+
room: OpenGroupV2Room,
63+
fromConfigMessage: boolean
64+
): Promise<ConversationModel | undefined> {
6165
if (!room.serverUrl || !room.roomId || room.roomId.length < 2 || !room.serverPublicKey) {
62-
return;
66+
return undefined;
6367
}
6468

6569
const serverUrl = room.serverUrl;
@@ -97,6 +101,7 @@ async function joinOpenGroupV2(room: OpenGroupV2Room, fromConfigMessage: boolean
97101
if (!fromConfigMessage) {
98102
await forceSyncConfigurationNowIfNeeded();
99103
}
104+
return conversation;
100105
} catch (e) {
101106
window?.log?.error('Could not join open group v2', e.message);
102107
throw e;
@@ -154,24 +159,23 @@ export async function joinOpenGroupV2WithUIEvents(
154159

155160
uiCallback?.({ loadingState: 'started', conversationKey: conversationID });
156161

157-
await joinOpenGroupV2(parsedRoom, fromConfigMessage);
162+
const convoCreated = await joinOpenGroupV2(parsedRoom, fromConfigMessage);
158163

159-
const isConvoCreated = getConversationController().get(conversationID);
160-
if (isConvoCreated) {
164+
if (convoCreated) {
161165
if (showToasts) {
162166
ToastUtils.pushToastSuccess(
163167
'connectToServerSuccess',
164168
window.i18n('connectToServerSuccess')
165169
);
166170
}
167-
uiCallback?.({ loadingState: 'finished', conversationKey: conversationID });
171+
uiCallback?.({ loadingState: 'finished', conversationKey: convoCreated?.id });
168172

169173
return true;
170-
} else {
171-
if (showToasts) {
172-
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
173-
}
174174
}
175+
if (showToasts) {
176+
ToastUtils.pushToastError('connectToServerFail', window.i18n('connectToServerFail'));
177+
}
178+
175179
uiCallback?.({ loadingState: 'failed', conversationKey: conversationID });
176180
} catch (error) {
177181
window?.log?.warn('got error while joining open group:', error.message);

ts/session/apis/open_group_api/opengroupV2/OpenGroupManagerV2.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
66
import { OpenGroupRequestCommonType } from './ApiUtil';
77
import { OpenGroupServerPoller } from './OpenGroupServerPoller';
88

9-
import _ from 'lodash';
9+
import _, { clone, isEqual } from 'lodash';
1010
import autoBind from 'auto-bind';
1111
import { ConversationTypeEnum } from '../../../../models/conversationAttributes';
1212
import { openGroupV2GetRoomInfoViaOnionV4 } from '../sogsv3/sogsV3RoomInfos';
@@ -153,7 +153,7 @@ export class OpenGroupManagerV2 {
153153
roomId: string,
154154
serverPublicKey: string
155155
): Promise<ConversationModel | undefined> {
156-
const conversationId = getOpenGroupV2ConversationId(serverUrl, roomId);
156+
let conversationId = getOpenGroupV2ConversationId(serverUrl, roomId);
157157

158158
if (getConversationController().get(conversationId)) {
159159
// Url incorrect or server not compatible
@@ -163,47 +163,57 @@ export class OpenGroupManagerV2 {
163163
// here, the convo does not exist. Make sure the db is clean too
164164
await OpenGroupData.removeV2OpenGroupRoom(conversationId);
165165

166-
const room: OpenGroupV2Room = {
167-
serverUrl,
168-
roomId,
169-
conversationId,
170-
serverPublicKey,
171-
};
172-
173166
try {
167+
const room: OpenGroupV2Room = {
168+
serverUrl,
169+
roomId,
170+
conversationId,
171+
serverPublicKey,
172+
};
173+
const updatedRoom = clone(room);
174174
// save the pubkey to the db right now, the request for room Info
175175
// will need it and access it from the db
176176
await OpenGroupData.saveV2OpenGroupRoom(room);
177+
177178
const roomInfos = await openGroupV2GetRoomInfoViaOnionV4({
178179
serverPubkey: serverPublicKey,
179180
serverUrl,
180181
roomId,
181182
});
182-
if (!roomInfos) {
183+
184+
if (!roomInfos || !roomInfos.id) {
183185
throw new Error('Invalid open group roomInfo result');
184186
}
187+
updatedRoom.roomId = roomInfos.id;
188+
conversationId = getOpenGroupV2ConversationId(serverUrl, roomInfos.id);
189+
updatedRoom.conversationId = conversationId;
190+
if (!isEqual(room, updatedRoom)) {
191+
await OpenGroupData.removeV2OpenGroupRoom(conversationId);
192+
await OpenGroupData.saveV2OpenGroupRoom(updatedRoom);
193+
}
194+
185195
const conversation = await getConversationController().getOrCreateAndWait(
186196
conversationId,
187197
ConversationTypeEnum.GROUP
188198
);
189-
room.imageID = roomInfos.imageId || undefined;
190-
room.roomName = roomInfos.name || undefined;
191-
room.capabilities = roomInfos.capabilities;
192-
await OpenGroupData.saveV2OpenGroupRoom(room);
199+
updatedRoom.imageID = roomInfos.imageId || undefined;
200+
updatedRoom.roomName = roomInfos.name || undefined;
201+
updatedRoom.capabilities = roomInfos.capabilities;
202+
await OpenGroupData.saveV2OpenGroupRoom(updatedRoom);
193203

194204
// mark active so it's not in the contacts list but in the conversation list
195205
// mark isApproved as this is a public chat
196206
conversation.set({
197207
active_at: Date.now(),
198-
displayNameInProfile: room.roomName,
208+
displayNameInProfile: updatedRoom.roomName,
199209
isApproved: true,
200210
didApproveMe: true,
201211
isTrustedForAttachmentDownload: true, // we always trust attachments when sent to an opengroup
202212
});
203213
await conversation.commit();
204214

205215
// start polling this room
206-
this.addRoomToPolledRooms([room]);
216+
this.addRoomToPolledRooms([updatedRoom]);
207217

208218
return conversation;
209219
} catch (e) {

ts/session/apis/open_group_api/opengroupV2/OpenGroupServerPoller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ export class OpenGroupServerPoller {
143143
window?.log?.info('this is not the correct ServerPoller');
144144
return;
145145
}
146-
if (this.roomIdsToPoll.has(room.roomId)) {
146+
if (this.roomIdsToPoll.has(room.roomId) || this.roomIdsToPoll.has(room.roomId.toLowerCase())) {
147147
window?.log?.info(`Removing ${room.roomId} from polling for ${this.serverUrl}`);
148148
this.roomIdsToPoll.delete(room.roomId);
149+
this.roomIdsToPoll.delete(room.roomId.toLowerCase());
149150
} else {
150151
window?.log?.info(
151152
`Cannot remove polling of ${room.roomId} as it is not polled on ${this.serverUrl}`

0 commit comments

Comments
 (0)