@@ -6,7 +6,7 @@ import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
6
6
import { OpenGroupRequestCommonType } from './ApiUtil' ;
7
7
import { OpenGroupServerPoller } from './OpenGroupServerPoller' ;
8
8
9
- import _ from 'lodash' ;
9
+ import _ , { clone , isEqual } from 'lodash' ;
10
10
import autoBind from 'auto-bind' ;
11
11
import { ConversationTypeEnum } from '../../../../models/conversationAttributes' ;
12
12
import { openGroupV2GetRoomInfoViaOnionV4 } from '../sogsv3/sogsV3RoomInfos' ;
@@ -153,7 +153,7 @@ export class OpenGroupManagerV2 {
153
153
roomId : string ,
154
154
serverPublicKey : string
155
155
) : Promise < ConversationModel | undefined > {
156
- const conversationId = getOpenGroupV2ConversationId ( serverUrl , roomId ) ;
156
+ let conversationId = getOpenGroupV2ConversationId ( serverUrl , roomId ) ;
157
157
158
158
if ( getConversationController ( ) . get ( conversationId ) ) {
159
159
// Url incorrect or server not compatible
@@ -163,47 +163,57 @@ export class OpenGroupManagerV2 {
163
163
// here, the convo does not exist. Make sure the db is clean too
164
164
await OpenGroupData . removeV2OpenGroupRoom ( conversationId ) ;
165
165
166
- const room : OpenGroupV2Room = {
167
- serverUrl,
168
- roomId,
169
- conversationId,
170
- serverPublicKey,
171
- } ;
172
-
173
166
try {
167
+ const room : OpenGroupV2Room = {
168
+ serverUrl,
169
+ roomId,
170
+ conversationId,
171
+ serverPublicKey,
172
+ } ;
173
+ const updatedRoom = clone ( room ) ;
174
174
// save the pubkey to the db right now, the request for room Info
175
175
// will need it and access it from the db
176
176
await OpenGroupData . saveV2OpenGroupRoom ( room ) ;
177
+
177
178
const roomInfos = await openGroupV2GetRoomInfoViaOnionV4 ( {
178
179
serverPubkey : serverPublicKey ,
179
180
serverUrl,
180
181
roomId,
181
182
} ) ;
182
- if ( ! roomInfos ) {
183
+
184
+ if ( ! roomInfos || ! roomInfos . id ) {
183
185
throw new Error ( 'Invalid open group roomInfo result' ) ;
184
186
}
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
+
185
195
const conversation = await getConversationController ( ) . getOrCreateAndWait (
186
196
conversationId ,
187
197
ConversationTypeEnum . GROUP
188
198
) ;
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 ) ;
193
203
194
204
// mark active so it's not in the contacts list but in the conversation list
195
205
// mark isApproved as this is a public chat
196
206
conversation . set ( {
197
207
active_at : Date . now ( ) ,
198
- displayNameInProfile : room . roomName ,
208
+ displayNameInProfile : updatedRoom . roomName ,
199
209
isApproved : true ,
200
210
didApproveMe : true ,
201
211
isTrustedForAttachmentDownload : true , // we always trust attachments when sent to an opengroup
202
212
} ) ;
203
213
await conversation . commit ( ) ;
204
214
205
215
// start polling this room
206
- this . addRoomToPolledRooms ( [ room ] ) ;
216
+ this . addRoomToPolledRooms ( [ updatedRoom ] ) ;
207
217
208
218
return conversation ;
209
219
} catch ( e ) {
0 commit comments