You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift

654 lines
28 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import GRDB
import SessionUIKit
import SessionSnodeKit
import SessionUtil
import SessionUtilitiesKit
// MARK: - Convenience
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # LibSession-Util # Podfile # Podfile.lock # Session.xcodeproj/project.pbxproj # Session.xcodeproj/xcshareddata/xcschemes/SessionSnodeKit.xcscheme # Session/Calls/Call Management/SessionCallManager.swift # Session/Closed Groups/EditClosedGroupVC.swift # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewModel.swift # Session/Conversations/Settings/ThreadDisappearingMessagesSettingsViewModel.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Meta/AppDelegate.swift # Session/Meta/SessionApp.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Onboarding/Onboarding.swift # Session/Path/PathStatusView.swift # Session/Path/PathVC.swift # Session/Settings/NukeDataModal.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/IP2Country.swift # SessionMessagingKit/Database/Migrations/_014_GenerateInitialUserConfigDumps.swift # SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageRequests.swift # SessionMessagingKit/Database/Migrations/_018_DisappearingMessagesConfiguration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/ConfigDump.swift # SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Jobs/AttachmentDownloadJob.swift # SessionMessagingKit/Jobs/ConfigMessageReceiveJob.swift # SessionMessagingKit/Jobs/ConfigurationSyncJob.swift # SessionMessagingKit/Jobs/ExpirationUpdateJob.swift # SessionMessagingKit/Jobs/GetExpirationJob.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+ConvoInfoVolatile.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupInfo.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupKeys.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupMembers.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+SharedGroup.swift # SessionMessagingKit/LibSession/Database/QueryInterfaceRequest+Utilities.swift # SessionMessagingKit/LibSession/Database/Setting+Utilities.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message+Origin.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/Models/SOGSMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Open Groups/OpenGroupManager.swift # SessionMessagingKit/Open Groups/OpenGroupServerIdLookup.swift # SessionMessagingKit/Open Groups/Types/Request+OpenGroupAPI.swift # SessionMessagingKit/Open Groups/Types/SOGSEndpoint.swift # SessionMessagingKit/Protos/Generated/SNProto.swift # SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift # SessionMessagingKit/Protos/SessionProtos.proto # SessionMessagingKit/Sending & Receiving/Errors/MessageSenderError.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/PushNotificationAPIRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Notifications/Types/Request+PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/GroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupAPI+Poller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/SessionUtil/SessionUtilError.swift # SessionMessagingKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionMessagingKit/Shared Models/MessageViewModel.swift # SessionMessagingKit/Shared Models/SessionThreadViewModel.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/LibSession/LibSessionSpec.swift # SessionMessagingKitTests/LibSession/LibSessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NotificationServiceExtension.swift # SessionShareExtension/ShareNavController.swift # SessionShareExtension/ThreadPickerVC.swift # SessionSnodeKit/Configuration.swift # SessionSnodeKit/Database/Migrations/_001_InitialSetupMigration.swift # SessionSnodeKit/Database/Models/Snode.swift # SessionSnodeKit/Database/Models/SnodeReceivedMessageInfo.swift # SessionSnodeKit/Database/Models/SnodeSet.swift # SessionSnodeKit/Jobs/GetSnodePoolJob.swift # SessionSnodeKit/Models/DeleteAllBeforeRequest.swift # SessionSnodeKit/Models/DeleteAllMessagesRequest.swift # SessionSnodeKit/Models/DeleteMessagesRequest.swift # SessionSnodeKit/Models/GetExpiriesRequest.swift # SessionSnodeKit/Models/GetMessagesRequest.swift # SessionSnodeKit/Models/ONSResolveResponse.swift # SessionSnodeKit/Models/RevokeSubkeyRequest.swift # SessionSnodeKit/Models/SendMessageRequest.swift # SessionSnodeKit/Models/SnodeAuthenticatedRequestBody.swift # SessionSnodeKit/Models/SnodeRequest.swift # SessionSnodeKit/Models/SwarmSnode.swift # SessionSnodeKit/Models/UpdateExpiryAllRequest.swift # SessionSnodeKit/Models/UpdateExpiryRequest.swift # SessionSnodeKit/Networking/OnionRequestAPI.swift # SessionSnodeKit/Networking/PreparedRequest+OnionRequest.swift # SessionSnodeKit/Networking/Request+SnodeAPI.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/OnionRequestAPIError.swift # SessionSnodeKit/Types/SnodeAPIEndpoint.swift # SessionSnodeKit/Types/SnodeAPIError.swift # SessionSnodeKit/Types/SnodeAPINamespace.swift # SessionSnodeKit/Types/SwarmDrainBehaviour.swift # SessionSnodeKitTests/Models/SnodeRequestSpec.swift # SessionTests/Database/DatabaseSpec.swift # SessionUIKit/Style Guide/Values.swift # SessionUtilitiesKit/Database/Migrations/_005_AddJobUniqueHash.swift # SessionUtilitiesKit/Database/Models/Job.swift # SessionUtilitiesKit/Database/Types/Migration.swift # SessionUtilitiesKit/General/Data+Utilities.swift # SessionUtilitiesKit/General/Dependencies.swift # SessionUtilitiesKit/General/Features.swift # SessionUtilitiesKit/General/Logging.swift # SessionUtilitiesKit/JobRunner/JobRunner.swift # SessionUtilitiesKit/LibSession/Utilities/Crypto+SessionUtil.swift # SessionUtilitiesKit/LibSession/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Networking/BatchRequest.swift # SessionUtilitiesKit/Networking/BatchResponse.swift # SessionUtilitiesKit/Networking/HTTP.swift # SessionUtilitiesKit/Networking/HTTPError.swift # SessionUtilitiesKit/Networking/PreparedRequest.swift # SessionUtilitiesKit/Networking/Request.swift # SessionUtilitiesKit/Networking/RequestTarget.swift # SessionUtilitiesKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Utilities/Bencode.swift # SessionUtilitiesKit/Utilities/JSONEncoder+Utilities.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # SessionUtilitiesKitTests/Networking/BatchRequestSpec.swift # SessionUtilitiesKitTests/Networking/BatchResponseSpec.swift # SessionUtilitiesKitTests/Networking/PreparedRequestSpec.swift # SessionUtilitiesKitTests/Networking/RequestSpec.swift # SessionUtilitiesKitTests/Utilities/BencodeResponseSpec.swift # SignalUtilitiesKit/Configuration.swift # SignalUtilitiesKit/Utilities/AppSetup.swift # SignalUtilitiesKit/Utilities/Bench.swift # SignalUtilitiesKit/Utilities/UIGestureRecognizer+OWS.swift # _SharedTestUtilities/CommonMockedExtensions.swift # _SharedTestUtilities/MockJobRunner.swift # _SharedTestUtilities/Mocked.swift
11 months ago
public extension LibSession {
enum Crypto {
public typealias Domain = String
}
}
internal extension LibSession {
/// This is a buffer period within which we will process messages which would result in a config change, any message which would normally
/// result in a config change which was sent before `lastConfigMessage.timestamp - configChangeBufferPeriod` will not
/// actually have it's changes applied (info messages would still be inserted though)
static let configChangeBufferPeriod: TimeInterval = (2 * 60)
static let columnsRelatedToThreads: [ColumnExpression] = [
SessionThread.Columns.pinnedPriority,
SessionThread.Columns.shouldBeVisible
]
static func assignmentsRequireConfigUpdate(_ assignments: [ConfigColumnAssignment]) -> Bool {
let targetColumns: Set<ColumnKey> = Set(assignments.map { ColumnKey($0.column) })
let allColumnsThatTriggerConfigUpdate: Set<ColumnKey> = []
.appending(contentsOf: columnsRelatedToUserProfile)
.appending(contentsOf: columnsRelatedToContacts)
.appending(contentsOf: columnsRelatedToConvoInfoVolatile)
.appending(contentsOf: columnsRelatedToUserGroups)
.appending(contentsOf: columnsRelatedToThreads)
.appending(contentsOf: columnsRelatedToGroupInfo)
.appending(contentsOf: columnsRelatedToGroupMembers)
.appending(contentsOf: columnsRelatedToGroupKeys)
.map { ColumnKey($0) }
.asSet()
return !allColumnsThatTriggerConfigUpdate.isDisjoint(with: targetColumns)
}
/// A `0` `priority` value indicates visible, but not pinned
static let visiblePriority: Int32 = 0
/// A negative `priority` value indicates hidden
static let hiddenPriority: Int32 = -1
static func shouldBeVisible(priority: Int32) -> Bool {
return (priority >= LibSession.visiblePriority)
}
static func pushChangesIfNeeded(
_ db: Database,
for variant: ConfigDump.Variant,
sessionId: SessionId,
using dependencies: Dependencies
) throws {
try performAndPushChange(db, for: variant, sessionId: sessionId, using: dependencies) { _ in }
}
static func performAndPushChange(
_ db: Database,
for variant: ConfigDump.Variant,
sessionId: SessionId,
using dependencies: Dependencies,
change: (Config?) throws -> ()
) throws {
// Since we are doing direct memory manipulation we are using an `Atomic`
// type which has blocking access in it's `mutate` closure
let needsPush: Bool
do {
needsPush = try dependencies[cache: .sessionUtil]
.config(for: variant, sessionId: sessionId)
.mutate { config in
// Peform the change
try change(config)
// If an error occurred during the change then actually throw it to prevent
// any database change from completing
if let lastError: SessionUtilError = config?.lastError { throw lastError }
// If we don't need to dump the data the we can finish early
guard config.needsDump(using: dependencies) else { return config.needsPush }
try LibSession.createDump(
config: config,
for: variant,
sessionId: sessionId,
timestampMs: SnodeAPI.currentOffsetTimestampMs(using: dependencies),
using: dependencies
)?.upsert(db)
return config.needsPush
}
}
catch {
SNLog("[LibSession] Failed to update/dump updated \(variant) config data due to error: \(error)")
throw error
}
// Make sure we need a push before scheduling one
guard needsPush else { return }
db.afterNextTransactionNestedOnce(dedupeId: SessionUtil.syncDedupeId(sessionId.hexString)) { db in
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # LibSession-Util # Podfile # Podfile.lock # Session.xcodeproj/project.pbxproj # Session.xcodeproj/xcshareddata/xcschemes/SessionSnodeKit.xcscheme # Session/Calls/Call Management/SessionCallManager.swift # Session/Closed Groups/EditClosedGroupVC.swift # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewModel.swift # Session/Conversations/Settings/ThreadDisappearingMessagesSettingsViewModel.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Meta/AppDelegate.swift # Session/Meta/SessionApp.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Onboarding/Onboarding.swift # Session/Path/PathStatusView.swift # Session/Path/PathVC.swift # Session/Settings/NukeDataModal.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/IP2Country.swift # SessionMessagingKit/Database/Migrations/_014_GenerateInitialUserConfigDumps.swift # SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageRequests.swift # SessionMessagingKit/Database/Migrations/_018_DisappearingMessagesConfiguration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/ConfigDump.swift # SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Jobs/AttachmentDownloadJob.swift # SessionMessagingKit/Jobs/ConfigMessageReceiveJob.swift # SessionMessagingKit/Jobs/ConfigurationSyncJob.swift # SessionMessagingKit/Jobs/ExpirationUpdateJob.swift # SessionMessagingKit/Jobs/GetExpirationJob.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+ConvoInfoVolatile.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupInfo.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupKeys.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupMembers.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+SharedGroup.swift # SessionMessagingKit/LibSession/Database/QueryInterfaceRequest+Utilities.swift # SessionMessagingKit/LibSession/Database/Setting+Utilities.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message+Origin.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/Models/SOGSMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Open Groups/OpenGroupManager.swift # SessionMessagingKit/Open Groups/OpenGroupServerIdLookup.swift # SessionMessagingKit/Open Groups/Types/Request+OpenGroupAPI.swift # SessionMessagingKit/Open Groups/Types/SOGSEndpoint.swift # SessionMessagingKit/Protos/Generated/SNProto.swift # SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift # SessionMessagingKit/Protos/SessionProtos.proto # SessionMessagingKit/Sending & Receiving/Errors/MessageSenderError.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/PushNotificationAPIRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Notifications/Types/Request+PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/GroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupAPI+Poller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/SessionUtil/SessionUtilError.swift # SessionMessagingKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionMessagingKit/Shared Models/MessageViewModel.swift # SessionMessagingKit/Shared Models/SessionThreadViewModel.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/LibSession/LibSessionSpec.swift # SessionMessagingKitTests/LibSession/LibSessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NotificationServiceExtension.swift # SessionShareExtension/ShareNavController.swift # SessionShareExtension/ThreadPickerVC.swift # SessionSnodeKit/Configuration.swift # SessionSnodeKit/Database/Migrations/_001_InitialSetupMigration.swift # SessionSnodeKit/Database/Models/Snode.swift # SessionSnodeKit/Database/Models/SnodeReceivedMessageInfo.swift # SessionSnodeKit/Database/Models/SnodeSet.swift # SessionSnodeKit/Jobs/GetSnodePoolJob.swift # SessionSnodeKit/Models/DeleteAllBeforeRequest.swift # SessionSnodeKit/Models/DeleteAllMessagesRequest.swift # SessionSnodeKit/Models/DeleteMessagesRequest.swift # SessionSnodeKit/Models/GetExpiriesRequest.swift # SessionSnodeKit/Models/GetMessagesRequest.swift # SessionSnodeKit/Models/ONSResolveResponse.swift # SessionSnodeKit/Models/RevokeSubkeyRequest.swift # SessionSnodeKit/Models/SendMessageRequest.swift # SessionSnodeKit/Models/SnodeAuthenticatedRequestBody.swift # SessionSnodeKit/Models/SnodeRequest.swift # SessionSnodeKit/Models/SwarmSnode.swift # SessionSnodeKit/Models/UpdateExpiryAllRequest.swift # SessionSnodeKit/Models/UpdateExpiryRequest.swift # SessionSnodeKit/Networking/OnionRequestAPI.swift # SessionSnodeKit/Networking/PreparedRequest+OnionRequest.swift # SessionSnodeKit/Networking/Request+SnodeAPI.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/OnionRequestAPIError.swift # SessionSnodeKit/Types/SnodeAPIEndpoint.swift # SessionSnodeKit/Types/SnodeAPIError.swift # SessionSnodeKit/Types/SnodeAPINamespace.swift # SessionSnodeKit/Types/SwarmDrainBehaviour.swift # SessionSnodeKitTests/Models/SnodeRequestSpec.swift # SessionTests/Database/DatabaseSpec.swift # SessionUIKit/Style Guide/Values.swift # SessionUtilitiesKit/Database/Migrations/_005_AddJobUniqueHash.swift # SessionUtilitiesKit/Database/Models/Job.swift # SessionUtilitiesKit/Database/Types/Migration.swift # SessionUtilitiesKit/General/Data+Utilities.swift # SessionUtilitiesKit/General/Dependencies.swift # SessionUtilitiesKit/General/Features.swift # SessionUtilitiesKit/General/Logging.swift # SessionUtilitiesKit/JobRunner/JobRunner.swift # SessionUtilitiesKit/LibSession/Utilities/Crypto+SessionUtil.swift # SessionUtilitiesKit/LibSession/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Networking/BatchRequest.swift # SessionUtilitiesKit/Networking/BatchResponse.swift # SessionUtilitiesKit/Networking/HTTP.swift # SessionUtilitiesKit/Networking/HTTPError.swift # SessionUtilitiesKit/Networking/PreparedRequest.swift # SessionUtilitiesKit/Networking/Request.swift # SessionUtilitiesKit/Networking/RequestTarget.swift # SessionUtilitiesKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Utilities/Bencode.swift # SessionUtilitiesKit/Utilities/JSONEncoder+Utilities.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # SessionUtilitiesKitTests/Networking/BatchRequestSpec.swift # SessionUtilitiesKitTests/Networking/BatchResponseSpec.swift # SessionUtilitiesKitTests/Networking/PreparedRequestSpec.swift # SessionUtilitiesKitTests/Networking/RequestSpec.swift # SessionUtilitiesKitTests/Utilities/BencodeResponseSpec.swift # SignalUtilitiesKit/Configuration.swift # SignalUtilitiesKit/Utilities/AppSetup.swift # SignalUtilitiesKit/Utilities/Bench.swift # SignalUtilitiesKit/Utilities/UIGestureRecognizer+OWS.swift # _SharedTestUtilities/CommonMockedExtensions.swift # _SharedTestUtilities/MockJobRunner.swift # _SharedTestUtilities/Mocked.swift
11 months ago
ConfigurationSyncJob.enqueue(db, swarmPublicKey: sessionId.hexString, using: dependencies)
}
}
@discardableResult static func updatingThreads<T>(
_ db: Database,
_ updated: [T],
using dependencies: Dependencies
) throws -> [T] {
guard let updatedThreads: [SessionThread] = updated as? [SessionThread] else {
throw StorageError.generic
}
// If we have no updated threads then no need to continue
guard !updatedThreads.isEmpty else { return updated }
let userSessionId: SessionId = getUserSessionId(db, using: dependencies)
let groupedThreads: [SessionThread.Variant: [SessionThread]] = updatedThreads
.grouped(by: \.variant)
let urlInfo: [String: OpenGroupUrlInfo] = try OpenGroupUrlInfo
.fetchAll(db, ids: updatedThreads.map { $0.id })
.reduce(into: [:]) { result, next in result[next.threadId] = next }
// Update the unread state for the threads first (just in case that's what changed)
try SessionUtil.updateMarkedAsUnreadState(db, threads: updatedThreads, using: dependencies)
// Then update the `hidden` and `priority` values
try groupedThreads.forEach { variant, threads in
switch variant {
case .contact:
// If the 'Note to Self' conversation is pinned then we need to custom handle it
// first as it's part of the UserProfile config
if let noteToSelf: SessionThread = threads.first(where: { $0.id == userSessionId.hexString }) {
try LibSession.performAndPushChange(
db,
for: .userProfile,
sessionId: userSessionId,
using: dependencies
) { config in
try LibSession.updateNoteToSelf(
priority: {
guard noteToSelf.shouldBeVisible else { return LibSession.hiddenPriority }
return noteToSelf.pinnedPriority
.map { Int32($0 == 0 ? LibSession.visiblePriority : max($0, 1)) }
.defaulting(to: LibSession.visiblePriority)
}(),
in: config
)
}
}
// Remove the 'Note to Self' convo from the list for updating contact priorities
let remainingThreads: [SessionThread] = threads.filter { $0.id != userSessionId.hexString }
guard !remainingThreads.isEmpty else { return }
try LibSession.performAndPushChange(
db,
for: .contacts,
sessionId: userSessionId,
using: dependencies
) { config in
try LibSession.upsert(
contactData: remainingThreads
.map { thread in
SyncedContactInfo(
id: thread.id,
priority: {
guard thread.shouldBeVisible else { return LibSession.hiddenPriority }
return thread.pinnedPriority
.map { Int32($0 == 0 ? LibSession.visiblePriority : max($0, 1)) }
.defaulting(to: LibSession.visiblePriority)
}()
)
},
in: config,
using: dependencies
)
}
case .community:
try LibSession.performAndPushChange(
db,
for: .userGroups,
sessionId: userSessionId,
using: dependencies
) { config in
try LibSession.upsert(
communities: threads
.compactMap { thread -> CommunityInfo? in
urlInfo[thread.id].map { urlInfo in
CommunityInfo(
urlInfo: urlInfo,
priority: thread.pinnedPriority
.map { Int32($0 == 0 ? LibSession.visiblePriority : max($0, 1)) }
.defaulting(to: LibSession.visiblePriority)
)
}
},
in: config,
using: dependencies
)
}
case .legacyGroup:
try LibSession.performAndPushChange(
db,
for: .userGroups,
sessionId: userSessionId,
using: dependencies
) { config in
try LibSession.upsert(
legacyGroups: threads
.map { thread in
LegacyGroupInfo(
id: thread.id,
priority: thread.pinnedPriority
.map { Int32($0 == 0 ? LibSession.visiblePriority : max($0, 1)) }
.defaulting(to: LibSession.visiblePriority)
)
},
in: config,
using: dependencies
)
}
case .group:
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
sessionId: userSessionId,
using: dependencies
) { config in
try SessionUtil.upsert(
groups: threads
.map { thread in
GroupInfo(
groupSessionId: thread.id,
priority: thread.pinnedPriority
.map { Int32($0 == 0 ? SessionUtil.visiblePriority : max($0, 1)) }
.defaulting(to: SessionUtil.visiblePriority)
)
},
in: config,
using: dependencies
)
}
}
}
return updated
}
static func hasSetting(
_ db: Database,
forKey key: String,
using dependencies: Dependencies
) throws -> Bool {
let userSessionId: SessionId = getUserSessionId(db, using: dependencies)
// Currently the only synced setting is 'checkForCommunityMessageRequests'
switch key {
case Setting.BoolKey.checkForCommunityMessageRequests.rawValue:
return try dependencies[cache: .sessionUtil]
.config(for: .userProfile, sessionId: userSessionId)
.wrappedValue
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # LibSession-Util # Podfile # Podfile.lock # Session.xcodeproj/project.pbxproj # Session.xcodeproj/xcshareddata/xcschemes/SessionSnodeKit.xcscheme # Session/Calls/Call Management/SessionCallManager.swift # Session/Closed Groups/EditClosedGroupVC.swift # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewModel.swift # Session/Conversations/Settings/ThreadDisappearingMessagesSettingsViewModel.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Meta/AppDelegate.swift # Session/Meta/SessionApp.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Onboarding/Onboarding.swift # Session/Path/PathStatusView.swift # Session/Path/PathVC.swift # Session/Settings/NukeDataModal.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/IP2Country.swift # SessionMessagingKit/Database/Migrations/_014_GenerateInitialUserConfigDumps.swift # SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageRequests.swift # SessionMessagingKit/Database/Migrations/_018_DisappearingMessagesConfiguration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/ConfigDump.swift # SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Jobs/AttachmentDownloadJob.swift # SessionMessagingKit/Jobs/ConfigMessageReceiveJob.swift # SessionMessagingKit/Jobs/ConfigurationSyncJob.swift # SessionMessagingKit/Jobs/ExpirationUpdateJob.swift # SessionMessagingKit/Jobs/GetExpirationJob.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+ConvoInfoVolatile.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupInfo.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupKeys.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupMembers.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+SharedGroup.swift # SessionMessagingKit/LibSession/Database/QueryInterfaceRequest+Utilities.swift # SessionMessagingKit/LibSession/Database/Setting+Utilities.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message+Origin.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/Models/SOGSMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Open Groups/OpenGroupManager.swift # SessionMessagingKit/Open Groups/OpenGroupServerIdLookup.swift # SessionMessagingKit/Open Groups/Types/Request+OpenGroupAPI.swift # SessionMessagingKit/Open Groups/Types/SOGSEndpoint.swift # SessionMessagingKit/Protos/Generated/SNProto.swift # SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift # SessionMessagingKit/Protos/SessionProtos.proto # SessionMessagingKit/Sending & Receiving/Errors/MessageSenderError.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/PushNotificationAPIRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Notifications/Types/Request+PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/GroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupAPI+Poller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/SessionUtil/SessionUtilError.swift # SessionMessagingKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionMessagingKit/Shared Models/MessageViewModel.swift # SessionMessagingKit/Shared Models/SessionThreadViewModel.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/LibSession/LibSessionSpec.swift # SessionMessagingKitTests/LibSession/LibSessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NotificationServiceExtension.swift # SessionShareExtension/ShareNavController.swift # SessionShareExtension/ThreadPickerVC.swift # SessionSnodeKit/Configuration.swift # SessionSnodeKit/Database/Migrations/_001_InitialSetupMigration.swift # SessionSnodeKit/Database/Models/Snode.swift # SessionSnodeKit/Database/Models/SnodeReceivedMessageInfo.swift # SessionSnodeKit/Database/Models/SnodeSet.swift # SessionSnodeKit/Jobs/GetSnodePoolJob.swift # SessionSnodeKit/Models/DeleteAllBeforeRequest.swift # SessionSnodeKit/Models/DeleteAllMessagesRequest.swift # SessionSnodeKit/Models/DeleteMessagesRequest.swift # SessionSnodeKit/Models/GetExpiriesRequest.swift # SessionSnodeKit/Models/GetMessagesRequest.swift # SessionSnodeKit/Models/ONSResolveResponse.swift # SessionSnodeKit/Models/RevokeSubkeyRequest.swift # SessionSnodeKit/Models/SendMessageRequest.swift # SessionSnodeKit/Models/SnodeAuthenticatedRequestBody.swift # SessionSnodeKit/Models/SnodeRequest.swift # SessionSnodeKit/Models/SwarmSnode.swift # SessionSnodeKit/Models/UpdateExpiryAllRequest.swift # SessionSnodeKit/Models/UpdateExpiryRequest.swift # SessionSnodeKit/Networking/OnionRequestAPI.swift # SessionSnodeKit/Networking/PreparedRequest+OnionRequest.swift # SessionSnodeKit/Networking/Request+SnodeAPI.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/OnionRequestAPIError.swift # SessionSnodeKit/Types/SnodeAPIEndpoint.swift # SessionSnodeKit/Types/SnodeAPIError.swift # SessionSnodeKit/Types/SnodeAPINamespace.swift # SessionSnodeKit/Types/SwarmDrainBehaviour.swift # SessionSnodeKitTests/Models/SnodeRequestSpec.swift # SessionTests/Database/DatabaseSpec.swift # SessionUIKit/Style Guide/Values.swift # SessionUtilitiesKit/Database/Migrations/_005_AddJobUniqueHash.swift # SessionUtilitiesKit/Database/Models/Job.swift # SessionUtilitiesKit/Database/Types/Migration.swift # SessionUtilitiesKit/General/Data+Utilities.swift # SessionUtilitiesKit/General/Dependencies.swift # SessionUtilitiesKit/General/Features.swift # SessionUtilitiesKit/General/Logging.swift # SessionUtilitiesKit/JobRunner/JobRunner.swift # SessionUtilitiesKit/LibSession/Utilities/Crypto+SessionUtil.swift # SessionUtilitiesKit/LibSession/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Networking/BatchRequest.swift # SessionUtilitiesKit/Networking/BatchResponse.swift # SessionUtilitiesKit/Networking/HTTP.swift # SessionUtilitiesKit/Networking/HTTPError.swift # SessionUtilitiesKit/Networking/PreparedRequest.swift # SessionUtilitiesKit/Networking/Request.swift # SessionUtilitiesKit/Networking/RequestTarget.swift # SessionUtilitiesKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Utilities/Bencode.swift # SessionUtilitiesKit/Utilities/JSONEncoder+Utilities.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # SessionUtilitiesKitTests/Networking/BatchRequestSpec.swift # SessionUtilitiesKitTests/Networking/BatchResponseSpec.swift # SessionUtilitiesKitTests/Networking/PreparedRequestSpec.swift # SessionUtilitiesKitTests/Networking/RequestSpec.swift # SessionUtilitiesKitTests/Utilities/BencodeResponseSpec.swift # SignalUtilitiesKit/Configuration.swift # SignalUtilitiesKit/Utilities/AppSetup.swift # SignalUtilitiesKit/Utilities/Bench.swift # SignalUtilitiesKit/Utilities/UIGestureRecognizer+OWS.swift # _SharedTestUtilities/CommonMockedExtensions.swift # _SharedTestUtilities/MockJobRunner.swift # _SharedTestUtilities/Mocked.swift
11 months ago
.map { config -> Bool in (try LibSession.rawBlindedMessageRequestValue(in: config) >= 0) }
.defaulting(to: false)
default: return false
}
}
static func updatingSetting(
_ db: Database,
_ updated: Setting?,
using dependencies: Dependencies
) throws {
// Don't current support any nullable settings
guard let updatedSetting: Setting = updated else { return }
let userSessionId: SessionId = getUserSessionId(db, using: dependencies)
// Currently the only synced setting is 'checkForCommunityMessageRequests'
switch updatedSetting.id {
case Setting.BoolKey.checkForCommunityMessageRequests.rawValue:
try LibSession.performAndPushChange(
db,
for: .userProfile,
sessionId: userSessionId,
using: dependencies
) { config in
try LibSession.updateSettings(
checkForCommunityMessageRequests: updatedSetting.unsafeValue(as: Bool.self),
in: config
)
}
default: break
}
}
static func kickFromConversationUIIfNeeded(removedThreadIds: [String], using dependencies: Dependencies) {
guard !removedThreadIds.isEmpty else { return }
// If the user is currently navigating somewhere within the view hierarchy of a conversation
// we just deleted then return to the home screen
DispatchQueue.main.async {
guard
dependencies.hasInitialised(singleton: .appContext),
let rootViewController: UIViewController = dependencies[singleton: .appContext].mainWindow?.rootViewController,
let topBannerController: TopBannerController = (rootViewController as? TopBannerController),
!topBannerController.children.isEmpty,
let navController: UINavigationController = topBannerController.children[0] as? UINavigationController
else { return }
// Extract the ones which will respond to LibSession changes
let targetViewControllers: [any LibSessionRespondingViewController] = navController
.viewControllers
.compactMap { $0 as? LibSessionRespondingViewController }
let presentedNavController: UINavigationController? = (navController.presentedViewController as? UINavigationController)
let presentedTargetViewControllers: [any LibSessionRespondingViewController] = (presentedNavController?
.viewControllers
.compactMap { $0 as? LibSessionRespondingViewController })
.defaulting(to: [])
// Make sure we have a conversation list and that one of the removed conversations are
// in the nav hierarchy
let rootNavControllerNeedsPop: Bool = (
targetViewControllers.count > 1 &&
targetViewControllers.contains(where: { $0.isConversationList }) &&
targetViewControllers.contains(where: { $0.isConversation(in: removedThreadIds) })
)
let presentedNavControllerNeedsPop: Bool = (
presentedTargetViewControllers.count > 1 &&
presentedTargetViewControllers.contains(where: { $0.isConversationList }) &&
presentedTargetViewControllers.contains(where: { $0.isConversation(in: removedThreadIds) })
)
// Force the UI to refresh if needed (most screens should do this automatically via database
// observation, but a couple of screens don't so need to be done manually)
targetViewControllers
.appending(contentsOf: presentedTargetViewControllers)
.filter { $0.isConversationList }
.forEach { $0.forceRefreshIfNeeded() }
switch (rootNavControllerNeedsPop, presentedNavControllerNeedsPop) {
case (true, false):
// Return to the conversation list as the removed conversation will be invalid
guard
let targetViewController: UIViewController = navController.viewControllers
.last(where: { viewController in
((viewController as? LibSessionRespondingViewController)?.isConversationList)
.defaulting(to: false)
})
else { return }
if navController.presentedViewController != nil {
navController.dismiss(animated: false) {
navController.popToViewController(targetViewController, animated: true)
}
}
else {
navController.popToViewController(targetViewController, animated: true)
}
case (false, true):
// Return to the conversation list as the removed conversation will be invalid
guard
let targetViewController: UIViewController = presentedNavController?
.viewControllers
.last(where: { viewController in
((viewController as? LibSessionRespondingViewController)?.isConversationList)
.defaulting(to: false)
})
else { return }
if presentedNavController?.presentedViewController != nil {
presentedNavController?.dismiss(animated: false) {
presentedNavController?.popToViewController(targetViewController, animated: true)
}
}
else {
presentedNavController?.popToViewController(targetViewController, animated: true)
}
default: break
}
}
}
static func canPerformChange(
_ db: Database,
threadId: String,
targetConfig: ConfigDump.Variant,
changeTimestampMs: Int64,
using dependencies: Dependencies = Dependencies()
) -> Bool {
let targetSessionId: String = {
switch targetConfig {
case .userProfile, .contacts, .convoInfoVolatile, .userGroups:
return getUserSessionId(db, using: dependencies).hexString
case .groupInfo, .groupMembers, .groupKeys: return threadId
case .invalid: return ""
}
}()
let configDumpTimestampMs: Int64 = (try? ConfigDump
.filter(
ConfigDump.Columns.variant == targetConfig &&
ConfigDump.Columns.sessionId == targetSessionId
)
.select(.timestampMs)
.asRequest(of: Int64.self)
.fetchOne(db))
.defaulting(to: 0)
// Ensure the change occurred after the last config message was handled (minus the buffer period)
return (changeTimestampMs >= (configDumpTimestampMs - Int64(LibSession.configChangeBufferPeriod * 1000)))
}
static func checkLoopLimitReached(_ loopCounter: inout Int, for variant: ConfigDump.Variant, maxLoopCount: Int = 50000) throws {
loopCounter += 1
guard loopCounter < maxLoopCount else {
SNLog("[LibSession] Got stuck in infinite loop processing '\(variant)' data")
throw LibSessionError.processingLoopLimitReached
}
}
}
// MARK: - Encryption
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # LibSession-Util # Podfile # Podfile.lock # Session.xcodeproj/project.pbxproj # Session.xcodeproj/xcshareddata/xcschemes/SessionSnodeKit.xcscheme # Session/Calls/Call Management/SessionCallManager.swift # Session/Closed Groups/EditClosedGroupVC.swift # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewModel.swift # Session/Conversations/Settings/ThreadDisappearingMessagesSettingsViewModel.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Meta/AppDelegate.swift # Session/Meta/SessionApp.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Onboarding/Onboarding.swift # Session/Path/PathStatusView.swift # Session/Path/PathVC.swift # Session/Settings/NukeDataModal.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/IP2Country.swift # SessionMessagingKit/Database/Migrations/_014_GenerateInitialUserConfigDumps.swift # SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageRequests.swift # SessionMessagingKit/Database/Migrations/_018_DisappearingMessagesConfiguration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/ConfigDump.swift # SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Jobs/AttachmentDownloadJob.swift # SessionMessagingKit/Jobs/ConfigMessageReceiveJob.swift # SessionMessagingKit/Jobs/ConfigurationSyncJob.swift # SessionMessagingKit/Jobs/ExpirationUpdateJob.swift # SessionMessagingKit/Jobs/GetExpirationJob.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+ConvoInfoVolatile.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupInfo.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupKeys.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupMembers.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+SharedGroup.swift # SessionMessagingKit/LibSession/Database/QueryInterfaceRequest+Utilities.swift # SessionMessagingKit/LibSession/Database/Setting+Utilities.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message+Origin.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/Models/SOGSMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Open Groups/OpenGroupManager.swift # SessionMessagingKit/Open Groups/OpenGroupServerIdLookup.swift # SessionMessagingKit/Open Groups/Types/Request+OpenGroupAPI.swift # SessionMessagingKit/Open Groups/Types/SOGSEndpoint.swift # SessionMessagingKit/Protos/Generated/SNProto.swift # SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift # SessionMessagingKit/Protos/SessionProtos.proto # SessionMessagingKit/Sending & Receiving/Errors/MessageSenderError.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/PushNotificationAPIRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Notifications/Types/Request+PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/GroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupAPI+Poller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/SessionUtil/SessionUtilError.swift # SessionMessagingKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionMessagingKit/Shared Models/MessageViewModel.swift # SessionMessagingKit/Shared Models/SessionThreadViewModel.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/LibSession/LibSessionSpec.swift # SessionMessagingKitTests/LibSession/LibSessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NotificationServiceExtension.swift # SessionShareExtension/ShareNavController.swift # SessionShareExtension/ThreadPickerVC.swift # SessionSnodeKit/Configuration.swift # SessionSnodeKit/Database/Migrations/_001_InitialSetupMigration.swift # SessionSnodeKit/Database/Models/Snode.swift # SessionSnodeKit/Database/Models/SnodeReceivedMessageInfo.swift # SessionSnodeKit/Database/Models/SnodeSet.swift # SessionSnodeKit/Jobs/GetSnodePoolJob.swift # SessionSnodeKit/Models/DeleteAllBeforeRequest.swift # SessionSnodeKit/Models/DeleteAllMessagesRequest.swift # SessionSnodeKit/Models/DeleteMessagesRequest.swift # SessionSnodeKit/Models/GetExpiriesRequest.swift # SessionSnodeKit/Models/GetMessagesRequest.swift # SessionSnodeKit/Models/ONSResolveResponse.swift # SessionSnodeKit/Models/RevokeSubkeyRequest.swift # SessionSnodeKit/Models/SendMessageRequest.swift # SessionSnodeKit/Models/SnodeAuthenticatedRequestBody.swift # SessionSnodeKit/Models/SnodeRequest.swift # SessionSnodeKit/Models/SwarmSnode.swift # SessionSnodeKit/Models/UpdateExpiryAllRequest.swift # SessionSnodeKit/Models/UpdateExpiryRequest.swift # SessionSnodeKit/Networking/OnionRequestAPI.swift # SessionSnodeKit/Networking/PreparedRequest+OnionRequest.swift # SessionSnodeKit/Networking/Request+SnodeAPI.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/OnionRequestAPIError.swift # SessionSnodeKit/Types/SnodeAPIEndpoint.swift # SessionSnodeKit/Types/SnodeAPIError.swift # SessionSnodeKit/Types/SnodeAPINamespace.swift # SessionSnodeKit/Types/SwarmDrainBehaviour.swift # SessionSnodeKitTests/Models/SnodeRequestSpec.swift # SessionTests/Database/DatabaseSpec.swift # SessionUIKit/Style Guide/Values.swift # SessionUtilitiesKit/Database/Migrations/_005_AddJobUniqueHash.swift # SessionUtilitiesKit/Database/Models/Job.swift # SessionUtilitiesKit/Database/Types/Migration.swift # SessionUtilitiesKit/General/Data+Utilities.swift # SessionUtilitiesKit/General/Dependencies.swift # SessionUtilitiesKit/General/Features.swift # SessionUtilitiesKit/General/Logging.swift # SessionUtilitiesKit/JobRunner/JobRunner.swift # SessionUtilitiesKit/LibSession/Utilities/Crypto+SessionUtil.swift # SessionUtilitiesKit/LibSession/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Networking/BatchRequest.swift # SessionUtilitiesKit/Networking/BatchResponse.swift # SessionUtilitiesKit/Networking/HTTP.swift # SessionUtilitiesKit/Networking/HTTPError.swift # SessionUtilitiesKit/Networking/PreparedRequest.swift # SessionUtilitiesKit/Networking/Request.swift # SessionUtilitiesKit/Networking/RequestTarget.swift # SessionUtilitiesKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Utilities/Bencode.swift # SessionUtilitiesKit/Utilities/JSONEncoder+Utilities.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # SessionUtilitiesKitTests/Networking/BatchRequestSpec.swift # SessionUtilitiesKitTests/Networking/BatchResponseSpec.swift # SessionUtilitiesKitTests/Networking/PreparedRequestSpec.swift # SessionUtilitiesKitTests/Networking/RequestSpec.swift # SessionUtilitiesKitTests/Utilities/BencodeResponseSpec.swift # SignalUtilitiesKit/Configuration.swift # SignalUtilitiesKit/Utilities/AppSetup.swift # SignalUtilitiesKit/Utilities/Bench.swift # SignalUtilitiesKit/Utilities/UIGestureRecognizer+OWS.swift # _SharedTestUtilities/CommonMockedExtensions.swift # _SharedTestUtilities/MockJobRunner.swift # _SharedTestUtilities/Mocked.swift
11 months ago
public extension LibSession {
static func encrypt(
messages: [Data],
toRecipients recipients: [SessionId],
ed25519PrivateKey: [UInt8],
domain: SessionUtil.Crypto.Domain,
using dependencies: Dependencies
) throws -> Data {
var outLen: Int = 0
var cMessages: [UnsafePointer<UInt8>?] = messages
.map { message in message.cArray }
.unsafeCopy()
var messageSizes: [Int] = messages.map { $0.count }
var cRecipients: [UnsafePointer<UInt8>?] = recipients
.map { recipient in recipient.publicKey }
.unsafeCopy()
var secretKey: [UInt8] = ed25519PrivateKey
var cDomain: [CChar] = domain.cArray
var cEncryptedDataPtr: UnsafeMutablePointer<UInt8>?
try CExceptionHelper.performSafely {
cEncryptedDataPtr = session_encrypt_for_multiple_simple_ed25519(
&outLen,
&cMessages,
&messageSizes,
messages.count,
&cRecipients,
recipients.count,
&secretKey,
&cDomain,
nil,
0
)
}
let encryptedData: Data? = cEncryptedDataPtr.map { Data(bytes: $0, count: outLen) }
cMessages.forEach { $0?.deallocate() }
cRecipients.forEach { $0?.deallocate() }
cEncryptedDataPtr?.deallocate()
return try encryptedData ?? { throw MessageSenderError.encryptionFailed }()
}
static func decrypt(
ciphertext: Data,
senderSessionId: SessionId,
ed25519KeyPair: KeyPair,
domain: SessionUtil.Crypto.Domain,
using dependencies: Dependencies
) throws -> Data {
var outLen: Int = 0
var cEncryptedData: [UInt8] = Array(ciphertext)
var secretKey: [UInt8] = ed25519KeyPair.secretKey
var cSenderPubkey: [UInt8] = senderSessionId.publicKey
var cDomain: [CChar] = domain.cArray
var cDecryptedDataPtr: UnsafeMutablePointer<UInt8>?
try CExceptionHelper.performSafely {
cDecryptedDataPtr = session_decrypt_for_multiple_simple_ed25519(
&outLen,
&cEncryptedData,
cEncryptedData.count,
&secretKey,
&cSenderPubkey,
&cDomain
)
}
let decryptedData: Data? = cDecryptedDataPtr.map { Data(bytes: $0, count: outLen) }
cDecryptedDataPtr?.deallocate()
return try decryptedData ?? { throw MessageReceiverError.decryptionFailed }()
}
}
// MARK: - External Outgoing Changes
public extension LibSession {
static func conversationInConfig(
_ db: Database? = nil,
threadId: String,
threadVariant: SessionThread.Variant,
visibleOnly: Bool,
using dependencies: Dependencies
) -> Bool {
let userSessionId: SessionId = getUserSessionId(db, using: dependencies)
let configVariant: ConfigDump.Variant = {
switch threadVariant {
case .contact: return (threadId == userSessionId.hexString ? .userProfile : .contacts)
case .legacyGroup, .group, .community: return .userGroups
}
}()
return dependencies[cache: .sessionUtil]
.config(for: configVariant, sessionId: userSessionId)
.wrappedValue
.map { config in
guard case .object(let conf) = config else { return false }
var cThreadId: [CChar] = threadId.cArray.nullTerminated()
switch threadVariant {
case .contact:
// The 'Note to Self' conversation is stored in the 'userProfile' config
guard threadId != userSessionId.hexString else {
return (
!visibleOnly ||
LibSession.shouldBeVisible(priority: user_profile_get_nts_priority(conf))
)
}
var contact: contacts_contact = contacts_contact()
guard contacts_get(conf, &contact, &cThreadId) else { return false }
/// If the user opens a conversation with an existing contact but doesn't send them a message
/// then the one-to-one conversation should remain hidden so we want to delete the `SessionThread`
/// when leaving the conversation
return (!visibleOnly || LibSession.shouldBeVisible(priority: contact.priority))
case .community:
let maybeUrlInfo: OpenGroupUrlInfo? = dependencies[singleton: .storage]
.read { db in try OpenGroupUrlInfo.fetchAll(db, ids: [threadId]) }?
.first
guard let urlInfo: OpenGroupUrlInfo = maybeUrlInfo else { return false }
var cBaseUrl: [CChar] = urlInfo.server.cArray.nullTerminated()
var cRoom: [CChar] = urlInfo.roomToken.cArray.nullTerminated()
var community: ugroups_community_info = ugroups_community_info()
/// Not handling the `hidden` behaviour for communities so just indicate the existence
return user_groups_get_community(conf, &community, &cBaseUrl, &cRoom)
case .legacyGroup:
let groupInfo: UnsafeMutablePointer<ugroups_legacy_group_info>? = user_groups_get_legacy_group(conf, &cThreadId)
/// Not handling the `hidden` behaviour for legacy groups so just indicate the existence
if groupInfo != nil {
ugroups_legacy_group_free(groupInfo)
return true
}
return false
case .group:
var group: ugroups_group_info = ugroups_group_info()
/// Not handling the `hidden` behaviour for legacy groups so just indicate the existence
return user_groups_get_group(conf, &group, &cThreadId)
}
}
.defaulting(to: false)
}
}
// MARK: - ColumnKey
internal extension LibSession {
struct ColumnKey: Equatable, Hashable {
let sourceType: Any.Type
let columnName: String
init(_ column: ColumnExpression) {
self.sourceType = type(of: column)
self.columnName = column.name
}
func hash(into hasher: inout Hasher) {
ObjectIdentifier(sourceType).hash(into: &hasher)
columnName.hash(into: &hasher)
}
static func == (lhs: ColumnKey, rhs: ColumnKey) -> Bool {
return (
lhs.sourceType == rhs.sourceType &&
lhs.columnName == rhs.columnName
)
}
}
}
// MARK: - PriorityVisibilityInfo
extension LibSession {
struct PriorityVisibilityInfo: Codable, FetchableRecord, Identifiable {
let id: String
let variant: SessionThread.Variant
let pinnedPriority: Int32?
let shouldBeVisible: Bool
}
}
// MARK: - LibSessionRespondingViewController
public protocol LibSessionRespondingViewController {
var isConversationList: Bool { get }
func isConversation(in threadIds: [String]) -> Bool
func forceRefreshIfNeeded()
}
public extension LibSessionRespondingViewController {
var isConversationList: Bool { false }
func isConversation(in threadIds: [String]) -> Bool { return false }
func forceRefreshIfNeeded() {}
}