Compare commits
3 Commits
42f2440067
...
0aa32bcab3
Author | SHA1 | Date |
---|---|---|
Ryan ZHAO | 0aa32bcab3 | |
Ryan ZHAO | b22fe40d6e | |
Ryan ZHAO | 7693e53183 |
|
@ -477,54 +477,34 @@ class ThreadDisappearingMessagesSettingsViewModel: SessionTableViewModel, Naviga
|
|||
_ = try updatedConfig.saved(db)
|
||||
|
||||
let userPublicKey: String = getUserHexEncodedPublicKey(db, using: dependencies)
|
||||
|
||||
if Features.useNewDisappearingMessagesConfig {
|
||||
switch threadVariant {
|
||||
case .contact:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.filter(Interaction.Columns.authorId == userPublicKey)
|
||||
.deleteAll(db)
|
||||
case .legacyGroup:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.deleteAll(db)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
let currentTimestampMs: Int64 = SnodeAPI.currentOffsetTimestampMs()
|
||||
|
||||
let interaction: Interaction = try Interaction(
|
||||
let interactionId = try DisappearingMessagesConfiguration.insertControlMessage(
|
||||
db,
|
||||
threadId: threadId,
|
||||
threadVariant: threadVariant,
|
||||
authorId: userPublicKey,
|
||||
variant: .infoDisappearingMessagesUpdate,
|
||||
body: updatedConfig.messageInfoString(
|
||||
threadVariant: threadVariant,
|
||||
senderName: nil,
|
||||
isPreviousOff: !self.config.isEnabled
|
||||
),
|
||||
timestampMs: currentTimestampMs,
|
||||
expiresInSeconds: updatedConfig.durationSeconds,
|
||||
expiresStartedAtMs: (updatedConfig.type == .disappearAfterSend ? Double(currentTimestampMs) : nil)
|
||||
serverHash: nil,
|
||||
updatedConfiguration: updatedConfig,
|
||||
isPreviousOff: !self.config.isEnabled
|
||||
)
|
||||
.inserted(db)
|
||||
|
||||
|
||||
let duration: UInt32? = {
|
||||
guard !Features.useNewDisappearingMessagesConfig else { return nil }
|
||||
return UInt32(floor(updatedConfig.isEnabled ? updatedConfig.durationSeconds : 0))
|
||||
}()
|
||||
|
||||
let expirationTimerUpdateMessage: ExpirationTimerUpdate = ExpirationTimerUpdate(
|
||||
syncTarget: nil,
|
||||
duration: duration
|
||||
)
|
||||
expirationTimerUpdateMessage.sentTimestamp = UInt64(currentTimestampMs)
|
||||
|
||||
try MessageSender.send(
|
||||
db,
|
||||
message: ExpirationTimerUpdate(
|
||||
syncTarget: nil,
|
||||
duration: duration
|
||||
),
|
||||
interactionId: interaction.id,
|
||||
message: expirationTimerUpdateMessage,
|
||||
interactionId: interactionId,
|
||||
threadId: threadId,
|
||||
threadVariant: threadVariant,
|
||||
using: dependencies
|
||||
|
|
|
@ -245,6 +245,72 @@ public extension DisappearingMessagesConfiguration {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: - Control Message
|
||||
|
||||
public extension DisappearingMessagesConfiguration {
|
||||
static func insertControlMessage(
|
||||
_ db: Database,
|
||||
threadId: String,
|
||||
threadVariant: SessionThread.Variant,
|
||||
authorId: String,
|
||||
timestampMs: Int64,
|
||||
serverHash: String?,
|
||||
updatedConfiguration: DisappearingMessagesConfiguration,
|
||||
isPreviousOff: Bool
|
||||
) throws -> Int64? {
|
||||
if Features.useNewDisappearingMessagesConfig {
|
||||
switch threadVariant {
|
||||
case .contact:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.filter(Interaction.Columns.authorId == authorId)
|
||||
.deleteAll(db)
|
||||
case .legacyGroup:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.deleteAll(db)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
let expiresStartedAtMs: Double? = {
|
||||
if updatedConfiguration.type == .disappearAfterSend ||
|
||||
SessionUtil.timestampAlreadyRead(
|
||||
threadId: threadId,
|
||||
threadVariant: threadVariant,
|
||||
timestampMs: timestampMs,
|
||||
userPublicKey: getUserHexEncodedPublicKey(db),
|
||||
openGroup: nil
|
||||
)
|
||||
{
|
||||
return Double(timestampMs)
|
||||
}
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
let interaction = try Interaction(
|
||||
serverHash: serverHash,
|
||||
threadId: threadId,
|
||||
authorId: authorId,
|
||||
variant: .infoDisappearingMessagesUpdate,
|
||||
body: updatedConfiguration.messageInfoString(
|
||||
threadVariant: threadVariant,
|
||||
senderName: (authorId != getUserHexEncodedPublicKey(db) ? Profile.displayName(db, id: authorId) : nil),
|
||||
isPreviousOff: isPreviousOff
|
||||
),
|
||||
timestampMs: timestampMs,
|
||||
expiresInSeconds: updatedConfiguration.durationSeconds,
|
||||
expiresStartedAtMs: expiresStartedAtMs
|
||||
).inserted(db)
|
||||
|
||||
return interaction.id
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UI Constraints
|
||||
|
||||
extension DisappearingMessagesConfiguration {
|
||||
|
|
|
@ -85,6 +85,8 @@ public enum GetExpirationJob: JobExecutor {
|
|||
)
|
||||
}
|
||||
|
||||
// FIXME: If currentTimestampMs - messageSentTimestampMs > expirationTimer, the message might already be expired and removed directly
|
||||
|
||||
try Interaction
|
||||
.filter(hashesToUseDefault.contains(Interaction.Columns.serverHash))
|
||||
.filter(Interaction.Columns.expiresStartedAtMs == nil)
|
||||
|
|
|
@ -198,68 +198,18 @@ extension MessageReceiver {
|
|||
}
|
||||
fallthrough
|
||||
case .contact:
|
||||
try insertExpirationUpdateControlMessage(
|
||||
_ = try DisappearingMessagesConfiguration.insertControlMessage(
|
||||
db,
|
||||
threadId: threadId,
|
||||
threadVariant: threadVariant,
|
||||
authorId: sender,
|
||||
timestampMs: timestampMs,
|
||||
timestampMs: Int64(timestampMs),
|
||||
serverHash: message.serverHash,
|
||||
localConfig: localConfig,
|
||||
remoteConfig: remoteConfig
|
||||
updatedConfiguration: remoteConfig,
|
||||
isPreviousOff: !localConfig.isEnabled
|
||||
)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
private static func insertExpirationUpdateControlMessage(
|
||||
_ db: Database,
|
||||
threadId: String,
|
||||
threadVariant: SessionThread.Variant,
|
||||
authorId: String,
|
||||
timestampMs: UInt64,
|
||||
serverHash: String?,
|
||||
localConfig: DisappearingMessagesConfiguration,
|
||||
remoteConfig: DisappearingMessagesConfiguration
|
||||
) throws {
|
||||
guard threadVariant != .contact || authorId != getUserHexEncodedPublicKey(db) else { return }
|
||||
|
||||
switch threadVariant {
|
||||
case .contact:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.filter(Interaction.Columns.authorId == authorId)
|
||||
.deleteAll(db)
|
||||
case .legacyGroup:
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.deleteAll(db)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == threadId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.filter(Interaction.Columns.authorId == authorId)
|
||||
.deleteAll(db)
|
||||
|
||||
_ = try Interaction(
|
||||
serverHash: serverHash,
|
||||
threadId: threadId,
|
||||
authorId: authorId,
|
||||
variant: .infoDisappearingMessagesUpdate,
|
||||
body: remoteConfig.messageInfoString(
|
||||
threadVariant: threadVariant,
|
||||
senderName: (authorId != getUserHexEncodedPublicKey(db) ? Profile.displayName(db, id: authorId) : nil),
|
||||
isPreviousOff: !localConfig.isEnabled
|
||||
),
|
||||
timestampMs: Int64(timestampMs),
|
||||
expiresInSeconds: remoteConfig.durationSeconds,
|
||||
expiresStartedAtMs: (remoteConfig.type == .disappearAfterSend ? Double(timestampMs) : nil)
|
||||
).inserted(db)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -180,11 +180,12 @@ internal extension SessionUtil {
|
|||
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(sessionId))
|
||||
|
||||
if data.config != localConfig {
|
||||
_ = try localConfig.with(
|
||||
isEnabled: data.config.isEnabled,
|
||||
durationSeconds: data.config.durationSeconds,
|
||||
type: data.config.type
|
||||
).save(db)
|
||||
_ = try data.config.save(db)
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == sessionId)
|
||||
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
|
||||
.filter(Interaction.Columns.authorId == getUserHexEncodedPublicKey(db))
|
||||
.deleteAll(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -277,12 +277,8 @@ internal extension SessionUtil {
|
|||
.fetchOne(db, id: group.id)
|
||||
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(group.id))
|
||||
|
||||
if let remoteConfig = group.disappearingConfig {
|
||||
_ = try localConfig.with(
|
||||
isEnabled: remoteConfig.isEnabled,
|
||||
durationSeconds: remoteConfig.durationSeconds,
|
||||
type: remoteConfig.type
|
||||
).save(db)
|
||||
if let remoteConfig = group.disappearingConfig, localConfig != remoteConfig {
|
||||
_ = try remoteConfig.save(db)
|
||||
|
||||
_ = try Interaction
|
||||
.filter(Interaction.Columns.threadId == group.id)
|
||||
|
|
Loading…
Reference in New Issue