session-ios/SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+UserGroups.swift

1234 lines
52 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import Sodium
import SessionUtil
import SessionUtilitiesKit
import SessionSnodeKit
// MARK: - Size Restrictions
public extension SessionUtil {
static var libSessionMaxGroupNameByteLength: Int { GROUP_NAME_MAX_LENGTH }
static var libSessionMaxGroupBaseUrlByteLength: Int { COMMUNITY_BASE_URL_MAX_LENGTH }
static var libSessionMaxGroupFullUrlByteLength: Int { COMMUNITY_FULL_URL_MAX_LENGTH }
static var libSessionMaxCommunityRoomByteLength: Int { COMMUNITY_ROOM_MAX_LENGTH }
}
// MARK: - UserGroups Handling
internal extension SessionUtil {
static let columnsRelatedToUserGroups: [ColumnExpression] = [
ClosedGroup.Columns.name
]
// MARK: - Incoming Changes
static func handleUserGroupsUpdate(
_ db: Database,
in config: Config?,
latestConfigSentTimestampMs: Int64,
using dependencies: Dependencies
) throws {
guard config.needsDump else { return }
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
var infiniteLoopGuard: Int = 0
var communities: [PrioritisedData<OpenGroupUrlInfo>] = []
var legacyGroups: [LegacyGroupInfo] = []
var groups: [GroupInfo] = []
var community: ugroups_community_info = ugroups_community_info()
var legacyGroup: ugroups_legacy_group_info = ugroups_legacy_group_info()
var group: ugroups_group_info = ugroups_group_info()
let groupsIterator: OpaquePointer = user_groups_iterator_new(conf)
while !user_groups_iterator_done(groupsIterator) {
try SessionUtil.checkLoopLimitReached(&infiniteLoopGuard, for: .userGroups)
if user_groups_it_is_community(groupsIterator, &community) {
let server: String = String(libSessionVal: community.base_url)
let roomToken: String = String(libSessionVal: community.room)
communities.append(
PrioritisedData(
data: OpenGroupUrlInfo(
threadId: OpenGroup.idFor(roomToken: roomToken, server: server),
server: server,
roomToken: roomToken,
publicKey: Data(
libSessionVal: community.pubkey,
count: OpenGroup.pubkeyByteLength
).toHexString()
),
priority: community.priority
)
)
}
else if user_groups_it_is_legacy_group(groupsIterator, &legacyGroup) {
let groupId: String = String(libSessionVal: legacyGroup.session_id)
let members: [String: Bool] = SessionUtil.memberInfo(in: &legacyGroup)
legacyGroups.append(
LegacyGroupInfo(
id: groupId,
name: String(libSessionVal: legacyGroup.name),
lastKeyPair: ClosedGroupKeyPair(
threadId: groupId,
publicKey: Data(
libSessionVal: legacyGroup.enc_pubkey,
count: ClosedGroup.pubKeyByteLength(for: .legacyGroup)
),
secretKey: Data(
libSessionVal: legacyGroup.enc_seckey,
count: ClosedGroup.secretKeyByteLength(for: .legacyGroup)
),
receivedTimestamp: (TimeInterval(SnodeAPI.currentOffsetTimestampMs(using: dependencies)) / 1000)
),
disappearingConfig: DisappearingMessagesConfiguration
.defaultWith(groupId)
.with(
isEnabled: (legacyGroup.disappearing_timer > 0),
durationSeconds: TimeInterval(legacyGroup.disappearing_timer),
lastChangeTimestampMs: latestConfigSentTimestampMs
),
groupMembers: members
.filter { _, isAdmin in !isAdmin }
.map { memberId, _ in
GroupMember(
groupId: groupId,
profileId: memberId,
role: .standard,
isHidden: false
)
},
groupAdmins: members
.filter { _, isAdmin in isAdmin }
.map { memberId, _ in
GroupMember(
groupId: groupId,
profileId: memberId,
role: .admin,
isHidden: false
)
},
priority: legacyGroup.priority,
joinedAt: legacyGroup.joined_at
)
)
}
else if user_groups_it_is_group(groupsIterator, &group) {
let groupId: String = String(libSessionVal: group.id)
groups.append(
GroupInfo(
groupIdentityPublicKey: groupId,
groupIdentityPrivateKey: (!group.have_secretkey ? nil :
Data(
libSessionVal: group.secretkey,
count: ClosedGroup.secretKeyByteLength(for: .group),
nullIfEmpty: true
)
),
authData: (!group.have_auth_data ? nil :
Data(
libSessionVal: group.auth_data,
count: ClosedGroup.authDataByteLength(for: .group),
nullIfEmpty: true
)
),
priority: group.priority,
joinedAt: group.joined_at
)
)
}
else {
SNLog("Ignoring unknown conversation type when iterating through volatile conversation info update")
}
user_groups_iterator_advance(groupsIterator)
}
user_groups_iterator_free(groupsIterator) // Need to free the iterator
// Extract all community/legacyGroup/group thread priorities
let existingThreadInfo: [String: PriorityVisibilityInfo] = (try? SessionThread
.select(.id, .variant, .pinnedPriority, .shouldBeVisible)
.filter(
[
SessionThread.Variant.community,
SessionThread.Variant.legacyGroup,
SessionThread.Variant.group
].contains(SessionThread.Columns.variant)
)
.asRequest(of: PriorityVisibilityInfo.self)
.fetchAll(db))
.defaulting(to: [])
.reduce(into: [:]) { result, next in result[next.id] = next }
// MARK: -- Handle Community Changes
// Add any new communities (via the OpenGroupManager)
communities.forEach { community in
let successfullyAddedGroup: Bool = OpenGroupManager.shared
.add(
db,
roomToken: community.data.roomToken,
server: community.data.server,
publicKey: community.data.publicKey,
calledFromConfigHandling: true
)
if successfullyAddedGroup {
db.afterNextTransactionNested { _ in
OpenGroupManager.shared.performInitialRequestsAfterAdd(
successfullyAddedGroup: successfullyAddedGroup,
roomToken: community.data.roomToken,
server: community.data.server,
publicKey: community.data.publicKey,
calledFromConfigHandling: false
)
.subscribe(on: OpenGroupAPI.workQueue)
.sinkUntilComplete()
}
}
// Set the priority if it's changed (new communities will have already been inserted at
// this stage)
if existingThreadInfo[community.data.threadId]?.pinnedPriority != community.priority {
_ = try? SessionThread
.filter(id: community.data.threadId)
.updateAll( // Handling a config update so don't use `updateAllAndConfig`
db,
SessionThread.Columns.pinnedPriority.set(to: community.priority)
)
}
}
// Remove any communities which are no longer in the config
let communityIdsToRemove: Set<String> = Set(existingThreadInfo
.filter { $0.value.variant == .community }
.keys)
.subtracting(communities.map { $0.data.threadId })
if !communityIdsToRemove.isEmpty {
SessionUtil.kickFromConversationUIIfNeeded(removedThreadIds: Array(communityIdsToRemove))
try SessionThread
.deleteOrLeave(
db,
threadIds: Array(communityIdsToRemove),
threadVariant: .community,
groupLeaveType: .forced,
calledFromConfigHandling: true,
using: dependencies
)
}
// MARK: -- Handle Legacy Group Changes
let existingLegacyGroupIds: Set<String> = Set(existingThreadInfo
.filter { $0.value.variant == .legacyGroup }
.keys)
let existingLegacyGroups: [String: ClosedGroup] = (try? ClosedGroup
.fetchAll(db, ids: existingLegacyGroupIds))
.defaulting(to: [])
.reduce(into: [:]) { result, next in result[next.id] = next }
let existingLegacyGroupMembers: [String: [GroupMember]] = (try? GroupMember
.filter(existingLegacyGroupIds.contains(GroupMember.Columns.groupId))
.fetchAll(db))
.defaulting(to: [])
.grouped(by: \.groupId)
try legacyGroups.forEach { group in
guard
let name: String = group.name,
let lastKeyPair: ClosedGroupKeyPair = group.lastKeyPair,
let members: [GroupMember] = group.groupMembers,
let updatedAdmins: Set<GroupMember> = group.groupAdmins?.asSet(),
let joinedAt: Int64 = group.joinedAt
else { return }
if !existingLegacyGroupIds.contains(group.id) {
// Add a new group if it doesn't already exist
try MessageReceiver.handleNewLegacyClosedGroup(
db,
groupPublicKey: group.id,
name: name,
encryptionKeyPair: KeyPair(
publicKey: lastKeyPair.publicKey.bytes,
secretKey: lastKeyPair.secretKey.bytes
),
members: members
.asSet()
.inserting(contentsOf: updatedAdmins) // Admins should also have 'standard' member entries
.map { $0.profileId },
admins: updatedAdmins.map { $0.profileId },
expirationTimer: UInt32(group.disappearingConfig?.durationSeconds ?? 0),
formationTimestampMs: UInt64((group.joinedAt.map { $0 * 1000 } ?? latestConfigSentTimestampMs)),
calledFromConfigHandling: true,
using: dependencies
)
}
else {
// Otherwise update the existing group
let groupChanges: [ConfigColumnAssignment] = [
(existingLegacyGroups[group.id]?.name == name ? nil :
ClosedGroup.Columns.name.set(to: name)
),
(existingLegacyGroups[group.id]?.formationTimestamp == TimeInterval(joinedAt) ? nil :
ClosedGroup.Columns.formationTimestamp.set(to: TimeInterval(joinedAt))
)
].compactMap { $0 }
// Apply any group changes
if !groupChanges.isEmpty {
_ = try? ClosedGroup
.filter(id: group.id)
.updateAll( // Handling a config update so don't use `updateAllAndConfig`
db,
groupChanges
)
}
// Add the lastKey if it doesn't already exist
let keyPairExists: Bool = ClosedGroupKeyPair
.filter(ClosedGroupKeyPair.Columns.threadKeyPairHash == lastKeyPair.threadKeyPairHash)
.isNotEmpty(db)
if !keyPairExists {
try lastKeyPair.insert(db)
}
// Update the disappearing messages timer
let localConfig: DisappearingMessagesConfiguration = try DisappearingMessagesConfiguration
.fetchOne(db, id: group.id)
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(group.id))
if
let remoteConfig = group.disappearingConfig,
let remoteLastChangeTimestampMs = remoteConfig.lastChangeTimestampMs,
let localLastChangeTimestampMs = localConfig.lastChangeTimestampMs,
remoteLastChangeTimestampMs > localLastChangeTimestampMs
{
_ = try localConfig.with(
isEnabled: remoteConfig.isEnabled,
durationSeconds: remoteConfig.durationSeconds,
type: remoteConfig.type,
lastChangeTimestampMs: remoteConfig.lastChangeTimestampMs
).save(db)
_ = try Interaction
.filter(Interaction.Columns.threadId == group.id)
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
.filter(Interaction.Columns.timestampMs <= (remoteLastChangeTimestampMs - Int64(remoteConfig.durationSeconds * 1000)))
.deleteAll(db)
}
// Update the members
let updatedMembers: Set<GroupMember> = members
.appending(
contentsOf: updatedAdmins.map { admin in
GroupMember(
groupId: admin.groupId,
profileId: admin.profileId,
role: .standard,
isHidden: false
)
}
)
.asSet()
if
let existingMembers: Set<GroupMember> = existingLegacyGroupMembers[group.id]?
.filter({ $0.role == .standard || $0.role == .zombie })
.asSet(),
existingMembers != updatedMembers
{
// Add in any new members and remove any removed members
try updatedMembers.forEach { try $0.save(db) }
try existingMembers
.filter { !updatedMembers.contains($0) }
.forEach { member in
try GroupMember
.filter(
GroupMember.Columns.groupId == group.id &&
GroupMember.Columns.profileId == member.profileId && (
GroupMember.Columns.role == GroupMember.Role.standard ||
GroupMember.Columns.role == GroupMember.Role.zombie
)
)
.deleteAll(db)
}
}
if
let existingAdmins: Set<GroupMember> = existingLegacyGroupMembers[group.id]?
.filter({ $0.role == .admin })
.asSet(),
existingAdmins != updatedAdmins
{
// Add in any new admins and remove any removed admins
try updatedAdmins.forEach { try $0.save(db) }
try existingAdmins
.filter { !updatedAdmins.contains($0) }
.forEach { member in
try GroupMember
.filter(
GroupMember.Columns.groupId == group.id &&
GroupMember.Columns.profileId == member.profileId &&
GroupMember.Columns.role == GroupMember.Role.admin
)
.deleteAll(db)
}
}
}
// Make any thread-specific changes if needed
if existingThreadInfo[group.id]?.pinnedPriority != group.priority {
_ = try? SessionThread
.filter(id: group.id)
.updateAll( // Handling a config update so don't use `updateAllAndConfig`
db,
SessionThread.Columns.pinnedPriority.set(to: group.priority)
)
}
}
// Remove any legacy groups which are no longer in the config
let legacyGroupIdsToRemove: Set<String> = existingLegacyGroupIds
.subtracting(legacyGroups.map { $0.id })
if !legacyGroupIdsToRemove.isEmpty {
SessionUtil.kickFromConversationUIIfNeeded(removedThreadIds: Array(legacyGroupIdsToRemove))
try SessionThread
.deleteOrLeave(
db,
threadIds: Array(legacyGroupIdsToRemove),
threadVariant: .legacyGroup,
groupLeaveType: .forced,
calledFromConfigHandling: true,
using: dependencies
)
}
// MARK: -- Handle Group Changes
let existingGroupIds: Set<String> = Set(existingThreadInfo
.filter { $0.value.variant == .group }
.keys)
let existingGroups: [String: ClosedGroup] = (try? ClosedGroup
.fetchAll(db, ids: existingGroupIds))
.defaulting(to: [])
.reduce(into: [:]) { result, next in result[next.id] = next }
try groups.forEach { group in
guard
let joinedAt: Int64 = group.joinedAt
else { return }
if !existingGroupIds.contains(group.groupIdentityPublicKey) {
// Add a new group if it doesn't already exist
try MessageReceiver.handleNewGroup(
db,
groupIdentityPublicKey: group.groupIdentityPublicKey,
groupIdentityPrivateKey: group.groupIdentityPrivateKey,
name: group.name,
authData: group.authData,
created: Int64((group.joinedAt ?? (latestConfigSentTimestampMs / 1000))),
approved: (group.approved == true),
calledFromConfigHandling: true,
using: dependencies
)
}
else {
/// Otherwise update the existing group
///
/// **Note:** We ignore the `name` value here as if it's an existing group then assume we will get the
/// proper name by polling for the `GROUP_INFO` instead of via syncing the `USER_GROUPS` data
let groupChanges: [ConfigColumnAssignment] = [
(existingGroups[group.groupIdentityPublicKey]?.formationTimestamp == TimeInterval(joinedAt) ? nil :
ClosedGroup.Columns.formationTimestamp.set(to: TimeInterval(joinedAt))
),
(existingGroups[group.groupIdentityPublicKey]?.authData == group.authData ? nil :
ClosedGroup.Columns.authData.set(to: group.authData)
),
(existingGroups[group.groupIdentityPublicKey]?.groupIdentityPrivateKey == group.groupIdentityPrivateKey ? nil :
ClosedGroup.Columns.groupIdentityPrivateKey.set(to: group.groupIdentityPrivateKey)
),
(existingGroups[group.groupIdentityPublicKey]?.approved == group.approved ? nil :
ClosedGroup.Columns.approved.set(to: (group.approved ?? false))
)
].compactMap { $0 }
// Apply any group changes
if !groupChanges.isEmpty {
_ = try? ClosedGroup
.filter(id: group.groupIdentityPublicKey)
.updateAll( // Handling a config update so don't use `updateAllAndConfig`
db,
groupChanges
)
}
}
// Make any thread-specific changes if needed
if existingThreadInfo[group.groupIdentityPublicKey]?.pinnedPriority != group.priority {
_ = try? SessionThread
.filter(id: group.groupIdentityPublicKey)
.updateAll( // Handling a config update so don't use `updateAllAndConfig`
db,
SessionThread.Columns.pinnedPriority.set(to: group.priority)
)
}
}
// Remove any groups which are no longer in the config
let groupIdsToRemove: Set<String> = existingGroupIds
.subtracting(groups.map { $0.groupIdentityPublicKey })
if !groupIdsToRemove.isEmpty {
SessionUtil.kickFromConversationUIIfNeeded(removedThreadIds: Array(groupIdsToRemove))
try SessionThread
.deleteOrLeave(
db,
threadIds: Array(groupIdsToRemove),
threadVariant: .group,
groupLeaveType: .forced,
calledFromConfigHandling: true
)
groupIdsToRemove.forEach { groupId in
SessionUtil.removeGroupStateIfNeeded(
db,
groupIdentityPublicKey: groupId,
using: dependencies
)
}
}
}
fileprivate static func memberInfo(in legacyGroup: UnsafeMutablePointer<ugroups_legacy_group_info>) -> [String: Bool] {
let membersIt: OpaquePointer = ugroups_legacy_members_begin(legacyGroup)
var members: [String: Bool] = [:]
var maybeMemberSessionId: UnsafePointer<CChar>? = nil
var memberAdmin: Bool = false
while ugroups_legacy_members_next(membersIt, &maybeMemberSessionId, &memberAdmin) {
guard let memberSessionId: UnsafePointer<CChar> = maybeMemberSessionId else {
continue
}
members[String(cString: memberSessionId)] = memberAdmin
}
return members
}
// MARK: - Outgoing Changes
static func upsert(
legacyGroups: [LegacyGroupInfo],
in config: Config?
) throws {
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
guard !legacyGroups.isEmpty else { return }
try legacyGroups
.forEach { legacyGroup in
var cGroupId: [CChar] = legacyGroup.id.cArray.nullTerminated()
guard let userGroup: UnsafeMutablePointer<ugroups_legacy_group_info> = user_groups_get_or_construct_legacy_group(conf, &cGroupId) else {
/// It looks like there are some situations where this object might not get created correctly (and
/// will throw due to the implicit unwrapping) as a result we put it in a guard and throw instead
SNLog("Unable to upsert legacy group conversation to SessionUtil: \(config.lastError)")
throw SessionUtilError.getOrConstructFailedUnexpectedly
}
// Assign all properties to match the updated group (if there is one)
if let updatedName: String = legacyGroup.name {
userGroup.pointee.name = updatedName.toLibSession()
// Store the updated group (needs to happen before variables go out of scope)
user_groups_set_legacy_group(conf, userGroup)
}
if let lastKeyPair: ClosedGroupKeyPair = legacyGroup.lastKeyPair {
userGroup.pointee.enc_pubkey = lastKeyPair.publicKey.toLibSession()
userGroup.pointee.enc_seckey = lastKeyPair.secretKey.toLibSession()
userGroup.pointee.have_enc_keys = true
// Store the updated group (needs to happen before variables go out of scope)
user_groups_set_legacy_group(conf, userGroup)
}
// Assign all properties to match the updated disappearing messages config (if there is one)
if let updatedConfig: DisappearingMessagesConfiguration = legacyGroup.disappearingConfig {
userGroup.pointee.disappearing_timer = (!updatedConfig.isEnabled ? 0 :
Int64(floor(updatedConfig.durationSeconds))
)
user_groups_set_legacy_group(conf, userGroup)
}
// Add/Remove the group members and admins
let existingMembers: [String: Bool] = {
guard legacyGroup.groupMembers != nil || legacyGroup.groupAdmins != nil else { return [:] }
return SessionUtil.memberInfo(in: userGroup)
}()
if let groupMembers: [GroupMember] = legacyGroup.groupMembers {
// Need to make sure we remove any admins before adding them here otherwise we will
// overwrite the admin permission to be a standard user permission
let memberIds: Set<String> = groupMembers
.map { $0.profileId }
.asSet()
.subtracting(legacyGroup.groupAdmins.defaulting(to: []).map { $0.profileId }.asSet())
let existingMemberIds: Set<String> = Array(existingMembers
.filter { _, isAdmin in !isAdmin }
.keys)
.asSet()
let membersIdsToAdd: Set<String> = memberIds.subtracting(existingMemberIds)
let membersIdsToRemove: Set<String> = existingMemberIds.subtracting(memberIds)
membersIdsToAdd.forEach { memberId in
var cProfileId: [CChar] = memberId.cArray.nullTerminated()
ugroups_legacy_member_add(userGroup, &cProfileId, false)
}
membersIdsToRemove.forEach { memberId in
var cProfileId: [CChar] = memberId.cArray.nullTerminated()
ugroups_legacy_member_remove(userGroup, &cProfileId)
}
}
if let groupAdmins: [GroupMember] = legacyGroup.groupAdmins {
let adminIds: Set<String> = groupAdmins.map { $0.profileId }.asSet()
let existingAdminIds: Set<String> = Array(existingMembers
.filter { _, isAdmin in isAdmin }
.keys)
.asSet()
let adminIdsToAdd: Set<String> = adminIds.subtracting(existingAdminIds)
let adminIdsToRemove: Set<String> = existingAdminIds.subtracting(adminIds)
adminIdsToAdd.forEach { adminId in
var cProfileId: [CChar] = adminId.cArray.nullTerminated()
ugroups_legacy_member_add(userGroup, &cProfileId, true)
}
adminIdsToRemove.forEach { adminId in
var cProfileId: [CChar] = adminId.cArray.nullTerminated()
ugroups_legacy_member_remove(userGroup, &cProfileId)
}
}
if let joinedAt: Int64 = legacyGroup.joinedAt {
userGroup.pointee.joined_at = joinedAt
}
// Store the updated group (can't be sure if we made any changes above)
userGroup.pointee.priority = (legacyGroup.priority ?? userGroup.pointee.priority)
// Note: Need to free the legacy group pointer
user_groups_set_free_legacy_group(conf, userGroup)
}
}
static func upsert(
groups: [GroupInfo],
in config: Config?
) throws {
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
guard !groups.isEmpty else { return }
try groups
.forEach { group in
var cGroupId: [CChar] = group.groupIdentityPublicKey.cArray.nullTerminated()
var userGroup: ugroups_group_info = ugroups_group_info()
guard user_groups_get_or_construct_group(conf, &userGroup, &cGroupId) else {
/// It looks like there are some situations where this object might not get created correctly (and
/// will throw due to the implicit unwrapping) as a result we put it in a guard and throw instead
SNLog("Unable to upsert group conversation to SessionUtil: \(config.lastError)")
throw SessionUtilError.getOrConstructFailedUnexpectedly
}
/// Assign the non-admin auth data (if it exists)
if let authData: Data = group.authData {
userGroup.auth_data = authData.toLibSession()
userGroup.have_auth_data = true
}
/// Assign the admin key (if it exists)
///
/// **Note:** We do this after assigning the `auth_data` as generally the values are mutually
/// exclusive and if we have a `groupIdentityPrivateKey` we want that to take priority
if let privateKey: Data = group.groupIdentityPrivateKey {
userGroup.secretkey = privateKey.toLibSession()
userGroup.have_secretkey = true
// Store the updated group (needs to happen before variables go out of scope)
user_groups_set_group(conf, &userGroup)
}
// Store the updated group (can't be sure if we made any changes above)
userGroup.joined_at = (group.joinedAt ?? userGroup.joined_at)
userGroup.priority = (group.priority ?? userGroup.priority)
user_groups_set_group(conf, &userGroup)
}
}
static func upsert(
communities: [CommunityInfo],
in config: Config?
) throws {
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
guard !communities.isEmpty else { return }
try communities
.forEach { community in
var cBaseUrl: [CChar] = community.urlInfo.server.cArray.nullTerminated()
var cRoom: [CChar] = community.urlInfo.roomToken.cArray.nullTerminated()
var cPubkey: [UInt8] = Data(hex: community.urlInfo.publicKey).cArray
var userCommunity: ugroups_community_info = ugroups_community_info()
guard user_groups_get_or_construct_community(conf, &userCommunity, &cBaseUrl, &cRoom, &cPubkey) else {
/// It looks like there are some situations where this object might not get created correctly (and
/// will throw due to the implicit unwrapping) as a result we put it in a guard and throw instead
SNLog("Unable to upsert community conversation to SessionUtil: \(config.lastError)")
throw SessionUtilError.getOrConstructFailedUnexpectedly
}
userCommunity.priority = (community.priority ?? userCommunity.priority)
user_groups_set_community(conf, &userCommunity)
}
}
}
// MARK: - External Outgoing Changes
public extension SessionUtil {
// MARK: -- Communities
static func add(
_ db: Database,
server: String,
rootToken: String,
publicKey: String,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
try SessionUtil.upsert(
communities: [
CommunityInfo(
urlInfo: OpenGroupUrlInfo(
threadId: OpenGroup.idFor(roomToken: rootToken, server: server),
server: server,
roomToken: rootToken,
publicKey: publicKey
)
)
],
in: config
)
}
}
static func remove(
_ db: Database,
server: String,
roomToken: String,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
var cBaseUrl: [CChar] = server.cArray.nullTerminated()
var cRoom: [CChar] = roomToken.cArray.nullTerminated()
// Don't care if the community doesn't exist
user_groups_erase_community(conf, &cBaseUrl, &cRoom)
}
// Remove the volatile info as well
try SessionUtil.remove(
db,
volatileCommunityInfo: [
OpenGroupUrlInfo(
threadId: OpenGroup.idFor(roomToken: roomToken, server: server),
server: server,
roomToken: roomToken,
publicKey: ""
)
],
using: dependencies
)
}
// MARK: -- Legacy Group Changes
static func add(
_ db: Database,
legacyGroupPublicKey: String,
name: String,
latestKeyPairPublicKey: Data,
latestKeyPairSecretKey: Data,
latestKeyPairReceivedTimestamp: TimeInterval,
disappearingConfig: DisappearingMessagesConfiguration,
members: Set<String>,
admins: Set<String>,
formationTimestamp: TimeInterval,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
var cGroupId: [CChar] = legacyGroupPublicKey.cArray.nullTerminated()
let userGroup: UnsafeMutablePointer<ugroups_legacy_group_info>? = user_groups_get_legacy_group(conf, &cGroupId)
// Need to make sure the group doesn't already exist (otherwise we will end up overriding the
// content which could revert newer changes since this can be triggered from other 'NEW' messages
// coming in from the legacy group swarm)
guard userGroup == nil else {
ugroups_legacy_group_free(userGroup)
return
}
try SessionUtil.upsert(
legacyGroups: [
LegacyGroupInfo(
id: legacyGroupPublicKey,
name: name,
lastKeyPair: ClosedGroupKeyPair(
threadId: legacyGroupPublicKey,
publicKey: latestKeyPairPublicKey,
secretKey: latestKeyPairSecretKey,
receivedTimestamp: latestKeyPairReceivedTimestamp
),
disappearingConfig: disappearingConfig,
groupMembers: members
.map { memberId in
GroupMember(
groupId: legacyGroupPublicKey,
profileId: memberId,
role: .standard,
isHidden: false
)
},
groupAdmins: admins
.map { memberId in
GroupMember(
groupId: legacyGroupPublicKey,
profileId: memberId,
role: .admin,
isHidden: false
)
},
joinedAt: Int64(floor(formationTimestamp))
)
],
in: config
)
}
}
static func update(
_ db: Database,
legacyGroupPublicKey: String,
name: String? = nil,
latestKeyPair: ClosedGroupKeyPair? = nil,
disappearingConfig: DisappearingMessagesConfiguration? = nil,
members: Set<String>? = nil,
admins: Set<String>? = nil,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
try SessionUtil.upsert(
legacyGroups: [
LegacyGroupInfo(
id: legacyGroupPublicKey,
name: name,
lastKeyPair: latestKeyPair,
disappearingConfig: disappearingConfig,
groupMembers: members?
.map { memberId in
GroupMember(
groupId: legacyGroupPublicKey,
profileId: memberId,
role: .standard,
isHidden: false
)
},
groupAdmins: admins?
.map { memberId in
GroupMember(
groupId: legacyGroupPublicKey,
profileId: memberId,
role: .admin,
isHidden: false
)
}
)
],
in: config
)
}
}
static func batchUpdate(
_ db: Database,
disappearingConfigs: [DisappearingMessagesConfiguration],
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
try SessionUtil.upsert(
legacyGroups: disappearingConfigs.map {
LegacyGroupInfo(
id: $0.id,
disappearingConfig: $0
)
},
in: config
)
}
}
static func remove(
_ db: Database,
legacyGroupIds: [String],
using dependencies: Dependencies
) throws {
guard !legacyGroupIds.isEmpty else { return }
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
legacyGroupIds.forEach { threadId in
var cGroupId: [CChar] = threadId.cArray.nullTerminated()
// Don't care if the group doesn't exist
user_groups_erase_legacy_group(conf, &cGroupId)
}
}
// Remove the volatile info as well
try SessionUtil.remove(db, volatileLegacyGroupIds: legacyGroupIds, using: dependencies)
}
// MARK: -- Group Changes
static func add(
_ db: Database,
groupIdentityPublicKey: String,
groupIdentityPrivateKey: Data?,
name: String?,
authData: Data?,
joinedAt: Int64,
approved: Bool,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
try SessionUtil.upsert(
groups: [
GroupInfo(
groupIdentityPublicKey: groupIdentityPublicKey,
groupIdentityPrivateKey: groupIdentityPrivateKey,
name: name,
authData: authData,
joinedAt: joinedAt,
approved: approved
)
],
in: config
)
}
}
static func update(
_ db: Database,
groupIdentityPublicKey: String,
groupIdentityPrivateKey: Data? = nil,
name: String? = nil,
authData: Data? = nil,
approved: Bool? = nil,
using dependencies: Dependencies
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
try SessionUtil.upsert(
groups: [
GroupInfo(
groupIdentityPublicKey: groupIdentityPublicKey,
groupIdentityPrivateKey: groupIdentityPrivateKey,
name: name,
authData: authData,
approved: approved
)
],
in: config
)
}
}
static func remove(
_ db: Database,
groupIds: [String],
using dependencies: Dependencies
) throws {
guard !groupIds.isEmpty else { return }
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db, using: dependencies),
using: dependencies
) { config in
guard case .object(let conf) = config else { throw SessionUtilError.invalidConfigObject }
groupIds.forEach { threadId in
var cGroupId: [CChar] = threadId.cArray.nullTerminated()
// Don't care if the group doesn't exist
user_groups_erase_group(conf, &cGroupId)
}
}
// Remove the volatile info as well
try SessionUtil.remove(db, volatileGroupIds: groupIds, using: dependencies)
}
}
// MARK: - LegacyGroupInfo
extension SessionUtil {
struct LegacyGroupInfo: Decodable, FetchableRecord, ColumnExpressible {
typealias Columns = CodingKeys
enum CodingKeys: String, CodingKey, ColumnExpression, CaseIterable {
case threadId
case name
case lastKeyPair
case disappearingConfig
case groupMembers
case groupAdmins
case priority
case joinedAt = "formationTimestamp"
}
var id: String { threadId }
let threadId: String
let name: String?
let lastKeyPair: ClosedGroupKeyPair?
let disappearingConfig: DisappearingMessagesConfiguration?
let groupMembers: [GroupMember]?
let groupAdmins: [GroupMember]?
let priority: Int32?
let joinedAt: Int64?
init(
id: String,
name: String? = nil,
lastKeyPair: ClosedGroupKeyPair? = nil,
disappearingConfig: DisappearingMessagesConfiguration? = nil,
groupMembers: [GroupMember]? = nil,
groupAdmins: [GroupMember]? = nil,
priority: Int32? = nil,
joinedAt: Int64? = nil
) {
self.threadId = id
self.name = name
self.lastKeyPair = lastKeyPair
self.disappearingConfig = disappearingConfig
self.groupMembers = groupMembers
self.groupAdmins = groupAdmins
self.priority = priority
self.joinedAt = joinedAt
}
static func fetchAll(_ db: Database) throws -> [LegacyGroupInfo] {
let closedGroup: TypedTableAlias<ClosedGroup> = TypedTableAlias()
let thread: TypedTableAlias<SessionThread> = TypedTableAlias()
let lastKeyPair: TypedTableAlias<ClosedGroupKeyPair> = TypedTableAlias(LegacyGroupInfo.self, column: .lastKeyPair)
let disappearingConfig: TypedTableAlias<DisappearingMessagesConfiguration> = TypedTableAlias(LegacyGroupInfo.self, column: .disappearingConfig)
/// **Note:** The `numColumnsBeforeTypes` value **MUST** match the number of fields before
/// the `lastKeyPair` entry below otherwise the query will fail to
/// parse and might throw
///
/// Explicitly set default values for the fields ignored for search results
let numColumnsBeforeTypes: Int = 4
let request: SQLRequest<LegacyGroupInfo> = """
SELECT
\(closedGroup[.threadId]) AS \(LegacyGroupInfo.Columns.threadId),
\(closedGroup[.name]) AS \(LegacyGroupInfo.Columns.name),
\(closedGroup[.formationTimestamp]) AS \(LegacyGroupInfo.Columns.joinedAt),
\(thread[.pinnedPriority]) AS \(LegacyGroupInfo.Columns.priority),
\(lastKeyPair.allColumns),
\(disappearingConfig.allColumns)
FROM \(ClosedGroup.self)
JOIN \(SessionThread.self) ON \(thread[.id]) = \(closedGroup[.threadId])
LEFT JOIN (
SELECT
\(lastKeyPair[.threadId]),
\(lastKeyPair[.publicKey]),
\(lastKeyPair[.secretKey]),
MAX(\(lastKeyPair[.receivedTimestamp])) AS \(ClosedGroupKeyPair.Columns.receivedTimestamp),
\(lastKeyPair[.threadKeyPairHash])
FROM \(ClosedGroupKeyPair.self)
GROUP BY \(lastKeyPair[.threadId])
) AS \(lastKeyPair) ON \(lastKeyPair[.threadId]) = \(closedGroup[.threadId])
LEFT JOIN \(disappearingConfig) ON \(disappearingConfig[.threadId]) = \(closedGroup[.threadId])
WHERE \(SQL("\(closedGroup[.threadId]) LIKE '\(SessionId.Prefix.standard)%'"))
"""
let legacyGroupInfoNoMembers: [LegacyGroupInfo] = try request
.adapted { db in
let adapters = try splittingRowAdapters(columnCounts: [
numColumnsBeforeTypes,
ClosedGroupKeyPair.numberOfSelectedColumns(db),
DisappearingMessagesConfiguration.numberOfSelectedColumns(db)
])
return ScopeAdapter.with(LegacyGroupInfo.self, [
.lastKeyPair: adapters[1],
.disappearingConfig: adapters[2]
])
}
.fetchAll(db)
let legacyGroupIds: [String] = legacyGroupInfoNoMembers.map { $0.threadId }
let allLegacyGroupMembers: [String: [GroupMember]] = try GroupMember
.filter(legacyGroupIds.contains(GroupMember.Columns.groupId))
.fetchAll(db)
.grouped(by: \.groupId)
return legacyGroupInfoNoMembers
.map { nonMemberGroup in
LegacyGroupInfo(
id: nonMemberGroup.id,
name: nonMemberGroup.name,
lastKeyPair: nonMemberGroup.lastKeyPair,
disappearingConfig: nonMemberGroup.disappearingConfig,
groupMembers: allLegacyGroupMembers[nonMemberGroup.id]?
.filter { $0.role == .standard || $0.role == .zombie },
groupAdmins: allLegacyGroupMembers[nonMemberGroup.id]?
.filter { $0.role == .admin },
priority: nonMemberGroup.priority,
joinedAt: nonMemberGroup.joinedAt
)
}
}
}
}
// MARK: - GroupInfo
extension SessionUtil {
struct GroupInfo {
let groupIdentityPublicKey: String
let groupIdentityPrivateKey: Data?
let name: String?
let authData: Data?
let priority: Int32?
let joinedAt: Int64?
let approved: Bool?
init(
groupIdentityPublicKey: String,
groupIdentityPrivateKey: Data? = nil,
name: String? = nil,
authData: Data? = nil,
priority: Int32? = nil,
joinedAt: Int64? = nil,
approved: Bool? = nil
) {
self.groupIdentityPublicKey = groupIdentityPublicKey
self.groupIdentityPrivateKey = groupIdentityPrivateKey
self.name = name
self.authData = authData
self.priority = priority
self.joinedAt = joinedAt
self.approved = approved
}
}
}
// MARK: - CommunityInfo
extension SessionUtil {
struct CommunityInfo {
let urlInfo: OpenGroupUrlInfo
let priority: Int32?
init(
urlInfo: OpenGroupUrlInfo,
priority: Int32? = nil
) {
self.urlInfo = urlInfo
self.priority = priority
}
}
}
// MARK: - GroupThreadData
extension SessionUtil {
fileprivate struct GroupThreadData {
let communities: [PrioritisedData<SessionUtil.OpenGroupUrlInfo>]
let legacyGroups: [PrioritisedData<LegacyGroupInfo>]
let groups: [PrioritisedData<GroupInfo>]
}
}
// MARK: - PrioritisedData
extension SessionUtil {
fileprivate struct PrioritisedData<T> {
let data: T
let priority: Int32
}
}