session-ios/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift
Morgan Pretty 53a5db0ea5 Fixed a number of issues found during internal testing
Added copy for an unrecoverable startup case
Added some additional logs to better debug ValueObservation query errors
Increased the pageSize to 20 on iPad devices (to prevent it immediately loading a second page)
Cleaned up a bunch of threading logic (try to avoid overriding subscribe/receive threads specified at subscription)
Consolidated the 'sendMessage' and 'sendAttachments' functions
Updated the various frameworks to use 'DAWRF with DSYM' to allow for better debugging during debug mode (at the cost of a longer build time)
Updated the logic to optimistically insert messages when sending to avoid any database write delays
Updated the logic to avoid sending notifications for messages which are already marked as read by the config
Fixed an issue where multiple paths could incorrectly get built at the same time in some cases
Fixed an issue where other job queues could be started before the blockingQueue finishes
Fixed a potential bug with the snode version comparison (was just a string comparison which would fail when getting to double-digit values)
Fixed a bug where you couldn't remove the last reaction on a message
Fixed the broken media message zoom animations
Fixed a bug where the last message read in a conversation wouldn't be correctly detected as already read
Fixed a bug where the QuoteView had no line limits (resulting in the '@You' mention background highlight being incorrectly positioned in the quote preview)
Fixed a bug where a large number of configSyncJobs could be scheduled (only one would run at a time but this could result in performance impacts)
2023-06-23 17:54:29 +10:00

104 lines
4.1 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
extension MessageReceiver {
internal static func handleExpirationTimerUpdate(
_ db: Database,
threadId: String,
threadVariant: SessionThread.Variant,
message: ExpirationTimerUpdate
) throws {
guard
// Only process these for contact and legacy groups (new groups handle it separately)
(threadVariant == .contact || threadVariant == .legacyGroup),
let sender: String = message.sender
else { throw MessageReceiverError.invalidMessage }
// Generate an updated configuration
//
// Note: Messages which had been sent during the previous configuration will still
// use it's settings (so if you enable, send a message and then disable disappearing
// message then the message you had sent will still disappear)
let config: DisappearingMessagesConfiguration = try DisappearingMessagesConfiguration
.filter(id: threadId)
.fetchOne(db)
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(threadId))
.with(
// If there is no duration then we should disable the expiration timer
isEnabled: ((message.duration ?? 0) > 0),
durationSeconds: (
message.duration.map { TimeInterval($0) } ??
DisappearingMessagesConfiguration.defaultDuration
)
)
let timestampMs: Int64 = Int64(message.sentTimestamp ?? 0) // Default to `0` if not set
// Only actually make the change if SessionUtil says we can (we always want to insert the info
// message though)
let canPerformChange: Bool = SessionUtil.canPerformChange(
db,
threadId: threadId,
targetConfig: {
switch threadVariant {
case .contact:
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db)
return (threadId == currentUserPublicKey ? .userProfile : .contacts)
default: return .userGroups
}
}(),
changeTimestampMs: timestampMs
)
// Only update libSession if we can perform the change
if canPerformChange {
// Legacy closed groups need to update the SessionUtil
switch threadVariant {
case .legacyGroup:
try SessionUtil
.update(
db,
groupPublicKey: threadId,
disappearingConfig: config
)
default: break
}
}
// Add an info message for the user
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db)
_ = try Interaction(
serverHash: nil, // Intentionally null so sync messages are seen as duplicates
threadId: threadId,
authorId: sender,
variant: .infoDisappearingMessagesUpdate,
body: config.messageInfoString(
with: (sender != currentUserPublicKey ?
Profile.displayName(db, id: sender) :
nil
)
),
timestampMs: timestampMs,
wasRead: SessionUtil.timestampAlreadyRead(
threadId: threadId,
threadVariant: threadVariant,
timestampMs: (timestampMs * 1000),
userPublicKey: currentUserPublicKey,
openGroup: nil
)
).inserted(db)
// Only save the updated config if we can perform the change
if canPerformChange {
// Finally save the changes to the DisappearingMessagesConfiguration (If it's a duplicate
// then the interaction unique constraint will prevent the code from getting here)
try config.save(db)
}
}
}