WIP: sync disappearing messages config through user config

pull/941/head
ryanzhao 2 years ago
parent ef1db5a1e2
commit e711c978c9

@ -52,7 +52,7 @@ public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatabl
init(sessionUtilType: CONVO_EXPIRATION_MODE) { init(sessionUtilType: CONVO_EXPIRATION_MODE) {
switch sessionUtilType { switch sessionUtilType {
case CONVO_EXPIRATION_AFTER_SEND: self = .disappearAfterRead case CONVO_EXPIRATION_AFTER_READ: self = .disappearAfterRead
case CONVO_EXPIRATION_AFTER_SEND: self = .disappearAfterSend case CONVO_EXPIRATION_AFTER_SEND: self = .disappearAfterSend
default: self = .unknown default: self = .unknown
} }
@ -65,6 +65,14 @@ public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatabl
case .disappearAfterSend: return .deleteAfterSend case .disappearAfterSend: return .deleteAfterSend
} }
} }
func toSessionUtilType() -> CONVO_EXPIRATION_MODE {
switch self {
case .unknown: return CONVO_EXPIRATION_NONE
case .disappearAfterRead: return CONVO_EXPIRATION_AFTER_READ
case .disappearAfterSend: return CONVO_EXPIRATION_AFTER_SEND
}
}
} }
public var id: String { threadId } // Identifiable public var id: String { threadId } // Identifiable

@ -23,7 +23,10 @@ internal extension SessionUtil {
Profile.Columns.name, Profile.Columns.name,
Profile.Columns.nickname, Profile.Columns.nickname,
Profile.Columns.profilePictureUrl, Profile.Columns.profilePictureUrl,
Profile.Columns.profileEncryptionKey Profile.Columns.profileEncryptionKey,
DisappearingMessagesConfiguration.Columns.isEnabled,
DisappearingMessagesConfiguration.Columns.type,
DisappearingMessagesConfiguration.Columns.durationSeconds
] ]
// MARK: - Incoming Changes // MARK: - Incoming Changes
@ -38,7 +41,7 @@ internal extension SessionUtil {
String: ( String: (
contact: Contact, contact: Contact,
profile: Profile, profile: Profile,
disappearingMessagesConfiguration: DisappearingMessagesConfiguration, disappearingMessagesConfig: DisappearingMessagesConfiguration,
priority: Int32, priority: Int32,
created: TimeInterval created: TimeInterval
) )
@ -174,15 +177,15 @@ internal extension SessionUtil {
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(sessionId)) .defaulting(to: DisappearingMessagesConfiguration.defaultWith(sessionId))
if if
let remoteLastChangeTimestampMs = data.disappearingMessagesConfiguration.lastChangeTimestampMs, let remoteLastChangeTimestampMs = data.disappearingMessagesConfig.lastChangeTimestampMs,
let localLastChangeTimestampMs = localDisappearingMessagesConfig.lastChangeTimestampMs, let localLastChangeTimestampMs = localDisappearingMessagesConfig.lastChangeTimestampMs,
remoteLastChangeTimestampMs > localLastChangeTimestampMs remoteLastChangeTimestampMs > localLastChangeTimestampMs
{ {
_ = try localDisappearingMessagesConfig.with( _ = try localDisappearingMessagesConfig.with(
isEnabled: data.disappearingMessagesConfiguration.isEnabled, isEnabled: data.disappearingMessagesConfig.isEnabled,
durationSeconds: data.disappearingMessagesConfiguration.durationSeconds, durationSeconds: data.disappearingMessagesConfig.durationSeconds,
type: data.disappearingMessagesConfiguration.type, type: data.disappearingMessagesConfig.type,
lastChangeTimestampMs: data.disappearingMessagesConfiguration.lastChangeTimestampMs lastChangeTimestampMs: data.disappearingMessagesConfig.lastChangeTimestampMs
).save(db) ).save(db)
} }
@ -379,6 +382,15 @@ internal extension SessionUtil {
contacts_set(conf, &contact) contacts_set(conf, &contact)
} }
// Assign all properties to match the updated disappearing messages configuration (if there is one)
if
let updatedDisappearingMessageConfig: DisappearingMessagesConfiguration = info.disappearingMessagesConfig,
let exp_mode: CONVO_EXPIRATION_MODE = updatedDisappearingMessageConfig.type?.toSessionUtilType()
{
contact.exp_mode = exp_mode
contact.exp_seconds = Int32(updatedDisappearingMessageConfig.durationSeconds)
}
// Store the updated contact (can't be sure if we made any changes above) // Store the updated contact (can't be sure if we made any changes above)
contact.priority = (info.priority ?? contact.priority) contact.priority = (info.priority ?? contact.priority)
contacts_set(conf, &contact) contacts_set(conf, &contact)
@ -463,7 +475,7 @@ internal extension SessionUtil {
// to do a config sync) // to do a config sync)
guard !existingContactIds.isEmpty else { return updated } guard !existingContactIds.isEmpty else { return updated }
// Get the user public key (updating their profile is handled separately // Get the user public key (updating their profile is handled separately)
let userPublicKey: String = getUserHexEncodedPublicKey(db) let userPublicKey: String = getUserHexEncodedPublicKey(db)
let targetProfiles: [Profile] = updatedProfiles let targetProfiles: [Profile] = updatedProfiles
.filter { .filter {
@ -501,6 +513,28 @@ internal extension SessionUtil {
return updated return updated
} }
static func updatingDisappearingMessagesConfigs<T>(_ db: Database, _ updated: [T]) throws -> [T] {
guard let updatedDisappearingMessagesConfigs: [DisappearingMessagesConfiguration] = updated as? [DisappearingMessagesConfiguration] else { throw StorageError.generic }
let userPublicKey: String = getUserHexEncodedPublicKey(db)
guard !updatedDisappearingMessagesConfigs.isEmpty else { return updated }
try SessionUtil.performAndPushChange(
db,
for: .contacts,
publicKey: userPublicKey
) { conf in
try SessionUtil
.upsert(
contactData: updatedDisappearingMessagesConfigs
.map { SyncedContactInfo(id: $0.id, disappearingMessagesConfig: $0) },
in: conf
)
}
return updated
}
} }
// MARK: - External Outgoing Changes // MARK: - External Outgoing Changes
@ -551,17 +585,20 @@ extension SessionUtil {
let id: String let id: String
let contact: Contact? let contact: Contact?
let profile: Profile? let profile: Profile?
let disappearingMessagesConfig: DisappearingMessagesConfiguration?
let priority: Int32? let priority: Int32?
init( init(
id: String, id: String,
contact: Contact? = nil, contact: Contact? = nil,
profile: Profile? = nil, profile: Profile? = nil,
disappearingMessagesConfig: DisappearingMessagesConfiguration? = nil,
priority: Int32? = nil priority: Int32? = nil
) { ) {
self.id = id self.id = id
self.contact = contact self.contact = contact
self.profile = profile self.profile = profile
self.disappearingMessagesConfig = disappearingMessagesConfig
self.priority = priority self.priority = priority
} }
} }

Loading…
Cancel
Save