mirror of
https://github.com/lux-tel/session-ios.git
synced 2026-10-30 20:38:32 +00:00
The `SessionThreadViewModel` has been renamed to `ConversationInfoViewModel` and is now a simple struct (as opposed to a FetchableRecord with a handful of behemoth database queries) The `ConversationInfoViewModel` and `MessageViewModel` now get initialised with a `ConversationDataCache` which is an object that holds a bunch of [ID -> Data] stores for various types that are needed to construct the aforementioned view models. The `ConversationDataCache` will generally be populated via the `ConversationDataHelper` which, when combined with our `ObservableEvent` system, is where the majority of the work for this refactoring has gone. This helper essentially has 4 parts: - `determineFetchRequirements`: Reviews any events which have come through and determines whether anything needs to be fetched from the database - `applyNonDatabaseEvents`: Updates the cached data directly based on delta information included in the `ObservableEvent` instances - `fetchFromDatabase`: Fetches any data from the database based on the info returned from `determineFetchRequirements`, this function will also resolve any dependant data that newly fetched objects need if they aren't already in the cache - `fetchFromLibSession`: Retrieves requested data from libSession Using the above setup all screens which contain conversation or message data now use these two view models, and can share the state of the view models between each other to reduce the need to refetch data when entering a new screen Some additional changes included due to the above are: • No longer need to pass `displayNameRetriever` throughout • Removed deprecated display name retrieval functions • Removed some duplicate code • Removed some synchronous database query hacks we needed until refactoring had been completed • Refactored mentions to be handled by tags • Refactored and optimised the searching logic (also added some caching) • Refactored the ThreadSettingsViewModel to use the new ObservationBuilder system • Refactored the ThreadPickerViewModel to use the new ObservationBuilder system • Refactored the HomeViewModel to retrieve data using the `ConversationDataHelper` • Refactored the ConversationViewModel to retrieve data using the `ConversationDataHelper` • Fixed an issue where communities weren't getting the correct "joined" timestamp
106 lines
4.7 KiB
Swift
106 lines
4.7 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
import GRDB
|
|
import SessionNetworkingKit
|
|
import SessionUtilitiesKit
|
|
|
|
extension MessageReceiver {
|
|
public static func handleUnsendRequest(
|
|
_ db: ObservingDatabase,
|
|
threadId: String,
|
|
threadVariant: SessionThread.Variant,
|
|
message: UnsendRequest,
|
|
using dependencies: Dependencies
|
|
) throws {
|
|
let userSessionId: SessionId = dependencies[cache: .general].sessionId
|
|
let senderIsLegacyGroupAdmin: Bool = {
|
|
switch (message.sender, threadVariant) {
|
|
case (.some(let sender), .legacyGroup):
|
|
return GroupMember
|
|
.filter(GroupMember.Columns.groupId == threadId)
|
|
.filter(GroupMember.Columns.profileId == sender)
|
|
.filter(GroupMember.Columns.role == GroupMember.Role.admin)
|
|
.isNotEmpty(db)
|
|
|
|
default: return false
|
|
}
|
|
}()
|
|
|
|
guard
|
|
senderIsLegacyGroupAdmin ||
|
|
message.sender == message.author ||
|
|
userSessionId.hexString == message.sender
|
|
else { return }
|
|
guard
|
|
let author: String = message.author,
|
|
let timestampMs: UInt64 = message.timestamp,
|
|
let interactionInfo: Interaction.ThreadInfo = try Interaction
|
|
.select(.id, .threadId)
|
|
.filter(Interaction.Columns.timestampMs == Int64(timestampMs))
|
|
.filter(Interaction.Columns.authorId == author)
|
|
.asRequest(of: Interaction.ThreadInfo.self)
|
|
.fetchOne(db)
|
|
else { return }
|
|
|
|
/// Retrieve the hashes which should be deleted first (these will be removed from the local
|
|
/// device in the `markAsDeleted` function) then call `markAsDeleted` to remove
|
|
/// message content
|
|
let hashes: Set<String> = try Interaction.serverHashesForDeletion(
|
|
db,
|
|
interactionIds: [interactionInfo.id]
|
|
)
|
|
try Interaction.markAsDeleted(
|
|
db,
|
|
threadId: interactionInfo.threadId, /// Can't use `threadId` as that may be the current users
|
|
threadVariant: threadVariant,
|
|
interactionIds: [interactionInfo.id],
|
|
options: [.local, .network],
|
|
using: dependencies
|
|
)
|
|
|
|
/// If it's the `Note to Self` conversation then we want to just delete the interaction
|
|
if userSessionId.hexString == interactionInfo.threadId {
|
|
try Interaction.deleteWhere(db, .filter(Interaction.Columns.id == interactionInfo.id))
|
|
}
|
|
|
|
/// Can't delete from the legacy group swarm so only bother for contact conversations
|
|
switch threadVariant {
|
|
case .legacyGroup, .group, .community: break
|
|
case .contact:
|
|
AnyPublisher
|
|
.lazy {
|
|
try Network.SnodeAPI.preparedDeleteMessages(
|
|
serverHashes: Array(hashes),
|
|
requireSuccessfulDeletion: false,
|
|
authMethod: try Authentication.with(
|
|
swarmPublicKey: dependencies[cache: .general].sessionId.hexString,
|
|
using: dependencies
|
|
),
|
|
using: dependencies
|
|
)
|
|
}
|
|
.flatMap { $0.send(using: dependencies) }
|
|
.subscribe(on: DispatchQueue.global(qos: .background), using: dependencies)
|
|
.sinkUntilComplete(
|
|
receiveCompletion: { result in
|
|
switch result {
|
|
case .failure: break
|
|
case .finished:
|
|
/// Since the server deletion was successful we should also flag the `SnodeReceivedMessageInfo`
|
|
/// entries for the hashes as invalud (otherwise we might try to poll for a hash which no longer exists,
|
|
/// resulting in fetching the last 14 days of messages)
|
|
dependencies[singleton: .storage].writeAsync { db in
|
|
try SnodeReceivedMessageInfo.handlePotentialDeletedOrInvalidHash(
|
|
db,
|
|
potentiallyInvalidHashes: Array(hashes)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|