From 08a52f94976f5e626726a3d4432167a902af15a3 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Mon, 29 Aug 2022 13:11:45 +1000 Subject: [PATCH] Fixed an issue which could cause the DB migration to fail for users with closed groups --- .../Database/Models/Interaction.swift | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/SessionMessagingKit/Database/Models/Interaction.swift b/SessionMessagingKit/Database/Models/Interaction.swift index 641d33d75..0b9c64e96 100644 --- a/SessionMessagingKit/Database/Models/Interaction.swift +++ b/SessionMessagingKit/Database/Models/Interaction.swift @@ -348,10 +348,14 @@ public struct Interaction: Codable, Identifiable, Equatable, FetchableRecord, Mu ).insert(db) case .closedGroup: - guard - let closedGroup: ClosedGroup = try? thread.closedGroup.fetchOne(db), - let members: [GroupMember] = try? closedGroup.members.fetchAll(db) - else { + let closedGroupMemberIds: Set = (try? GroupMember + .select(.profileId) + .filter(GroupMember.Columns.groupId == thread.id) + .asRequest(of: String.self) + .fetchSet(db)) + .defaulting(to: []) + + guard !closedGroupMemberIds.isEmpty else { SNLog("Inserted an interaction but couldn't find it's associated thread members") return } @@ -359,12 +363,12 @@ public struct Interaction: Codable, Identifiable, Equatable, FetchableRecord, Mu // Exclude the current user when creating recipient states (as they will never // receive the message resulting in the message getting flagged as failed) let userPublicKey: String = getUserHexEncodedPublicKey(db) - try members - .filter { member -> Bool in member.profileId != userPublicKey } - .forEach { member in + try closedGroupMemberIds + .filter { memberId -> Bool in memberId != userPublicKey } + .forEach { memberId in try RecipientState( interactionId: interactionId, - recipientId: member.profileId, + recipientId: memberId, state: .sending ).insert(db) }