Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked 'deleteOrLeave' function to remove ambiguity, fixed some strings #1045

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Session/Conversations/Settings/ThreadSettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,8 @@ class ThreadSettingsViewModel: SessionTableViewModel, NavigationItemSource, Navi
dependencies.storage.write { db in
try SessionThread.deleteOrLeave(
db,
type: .leaveGroupAsync,
threadId: threadViewModel.threadId,
threadVariant: threadViewModel.threadVariant,
groupLeaveType: .standard,
calledFromConfigHandling: false
)
}
Expand Down
6 changes: 2 additions & 4 deletions Session/Home/Message Requests/MessageRequestsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,20 @@ class MessageRequestsViewModel: SessionTableViewModel, NavigatableStateHolder, O
// Remove the one-to-one requests
try SessionThread.deleteOrLeave(
db,
type: .hideContactConversationAndDeleteContent,
threadIds: threadInfo
.filter { _, variant in variant == .contact }
.map { id, _ in id },
threadVariant: .contact,
groupLeaveType: .silent,
calledFromConfigHandling: false
)

// Remove the group requests
try SessionThread.deleteOrLeave(
db,
type: .deleteGroupAndContent,
threadIds: threadInfo
.filter { _, variant in variant == .legacyGroup || variant == .group }
.map { id, _ in id },
threadVariant: .group,
groupLeaveType: .silent,
calledFromConfigHandling: false
)
}
Expand Down
2 changes: 1 addition & 1 deletion Session/Home/New Conversation/NewMessageScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct EnterAccountIdScreen: View {
)
) {
ZStack {
Text("\("messageNewDescriptionMobile".localized())\(Image(systemName: "questionmark.circle"))")
(Text("messageNewDescriptionMobile".localized()) + Text(Image(systemName: "questionmark.circle")))
.font(.system(size: Values.verySmallFontSize))
.foregroundColor(themeColor: .textSecondary)
.multilineTextAlignment(.center)
Expand Down
2 changes: 1 addition & 1 deletion Session/Meta/Translations/InfoPlist.xcstrings

Large diffs are not rendered by default.

34 changes: 24 additions & 10 deletions Session/Utilities/UIContextualAction+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ public extension UIContextualAction {
Storage.shared.writeAsync { db in
try SessionThread.deleteOrLeave(
db,
type: .hideContactConversationAndDeleteContent,
threadId: threadViewModel.threadId,
threadVariant: threadViewModel.threadVariant,
groupLeaveType: .silent,
calledFromConfigHandling: false
)
}
Expand Down Expand Up @@ -187,9 +186,8 @@ public extension UIContextualAction {
Storage.shared.writeAsync { db in
try SessionThread.deleteOrLeave(
db,
type: .hideContactConversationAndDeleteContent,
threadId: threadViewModel.threadId,
threadVariant: threadViewModel.threadVariant,
groupLeaveType: .silent,
calledFromConfigHandling: false
)
}
Expand Down Expand Up @@ -345,9 +343,8 @@ public extension UIContextualAction {
if threadIsMessageRequest {
try SessionThread.deleteOrLeave(
db,
type: .hideContactConversationAndDeleteContent,
threadId: threadViewModel.threadId,
threadVariant: .contact,
groupLeaveType: .silent,
calledFromConfigHandling: false
)
}
Expand Down Expand Up @@ -429,13 +426,19 @@ public extension UIContextualAction {
cancelStyle: .alert_text,
dismissOnConfirm: true,
onConfirm: { _ in
let deletionType: SessionThread.DeletionType = {
switch threadViewModel.threadVariant {
case .legacyGroup, .group: return .leaveGroupAsync
default: return .deleteCommunityAndContent
}
}()

Storage.shared.writeAsync { db in
do {
try SessionThread.deleteOrLeave(
db,
type: deletionType,
threadId: threadViewModel.threadId,
threadVariant: threadViewModel.threadVariant,
groupLeaveType: .standard,
calledFromConfigHandling: false
)
} catch {
Expand Down Expand Up @@ -532,12 +535,23 @@ public extension UIContextualAction {
cancelStyle: .alert_text,
dismissOnConfirm: true,
onConfirm: { _ in
let deletionType: SessionThread.DeletionType = {
switch (threadViewModel.threadVariant, isMessageRequest) {
case (.community, _): return .deleteCommunityAndContent
case (.group, true): return .deleteGroupAndContent
case (.group, _), (.legacyGroup, _):
return .leaveGroupAsync

case (.contact, _):
return .hideContactConversationAndDeleteContent
}
}()

Storage.shared.writeAsync { db in
try SessionThread.deleteOrLeave(
db,
type: deletionType,
threadId: threadViewModel.threadId,
threadVariant: threadViewModel.threadVariant,
groupLeaveType: (isMessageRequest ? .silent : .forced),
calledFromConfigHandling: false
)
}
Expand Down
6 changes: 0 additions & 6 deletions SessionMessagingKit/Database/Models/ClosedGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ public extension ClosedGroup {
// MARK: - Convenience

public extension ClosedGroup {
enum LeaveType {
case standard
case silent
case forced
}

static func removeKeysAndUnsubscribe(
_ db: Database? = nil,
threadId: String,
Expand Down
51 changes: 29 additions & 22 deletions SessionMessagingKit/Database/Models/SessionThread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,35 +276,44 @@ public extension SessionThread {
)
""")
}
}

// MARK: - Deletion

public extension SessionThread {
enum DeletionType {
case hideContactConversationAndDeleteContent
case deleteContactConversationAndContact
case leaveGroupAsync
case deleteGroupAndContent
case deleteCommunityAndContent
}

static func deleteOrLeave(
_ db: Database,
type: SessionThread.DeletionType,
threadId: String,
threadVariant: Variant,
groupLeaveType: ClosedGroup.LeaveType,
calledFromConfigHandling: Bool
) throws {
try deleteOrLeave(
db,
type: type,
threadIds: [threadId],
threadVariant: threadVariant,
groupLeaveType: groupLeaveType,
calledFromConfigHandling: calledFromConfigHandling
)
}

static func deleteOrLeave(
_ db: Database,
type: SessionThread.DeletionType,
threadIds: [String],
threadVariant: Variant,
groupLeaveType: ClosedGroup.LeaveType,
calledFromConfigHandling: Bool
) throws {
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db)
let remainingThreadIds: Set<String> = threadIds.asSet().removing(currentUserPublicKey)

switch (threadVariant, groupLeaveType) {
case (.contact, .standard), (.contact, .silent):
switch type {
case .hideContactConversationAndDeleteContent:
// Clear any interactions for the deleted thread
_ = try Interaction
.filter(threadIds.contains(Interaction.Columns.threadId))
Expand Down Expand Up @@ -334,36 +343,34 @@ public extension SessionThread {
SessionThread.Columns.shouldBeVisible.set(to: false)
)

case (.contact, .forced):
case .deleteContactConversationAndContact:
// If this wasn't called from config handling then we need to hide the conversation
if !calledFromConfigHandling {
try LibSession
.remove(db, contactIds: Array(remainingThreadIds))
try LibSession.remove(db, contactIds: Array(remainingThreadIds))
}

_ = try SessionThread
.filter(ids: remainingThreadIds)
.deleteAll(db)

case (.legacyGroup, .standard), (.group, .standard):
case .leaveGroupAsync:
try threadIds.forEach { threadId in
try MessageSender
.leave(
db,
groupPublicKey: threadId,
deleteThread: true
)
try MessageSender.leave(
db,
groupPublicKey: threadId,
deleteThread: true
)
}

case (.legacyGroup, .silent), (.legacyGroup, .forced), (.group, .forced), (.group, .silent):
case .deleteGroupAndContent:
try ClosedGroup.removeKeysAndUnsubscribe(
db,
threadIds: threadIds,
removeGroupData: true,
calledFromConfigHandling: calledFromConfigHandling
)

case (.community, _):
case .deleteCommunityAndContent:
threadIds.forEach { threadId in
OpenGroupManager.shared.delete(
db,
Expand Down Expand Up @@ -530,8 +537,8 @@ public extension SessionThread {
profile: Profile? = nil
) -> String {
switch variant {
case .legacyGroup, .group: return (closedGroupName ?? "Unknown Group")
case .community: return (openGroupName ?? "Unknown Community")
case .legacyGroup, .group: return (closedGroupName ?? "groupUnknown".localized())
case .community: return (openGroupName ?? "communityUnknown".localized())
case .contact:
guard !isNoteToSelf else { return "noteToSelf".localized() }
guard let profile: Profile = profile else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ internal extension LibSession {
try SessionThread
.deleteOrLeave(
db,
type: .deleteContactConversationAndContact,
threadIds: combinedIds,
threadVariant: .contact,
groupLeaveType: .forced,
calledFromConfigHandling: true
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,12 @@ internal extension LibSession {
if !communityIdsToRemove.isEmpty {
LibSession.kickFromConversationUIIfNeeded(removedThreadIds: Array(communityIdsToRemove))

try SessionThread
.deleteOrLeave(
db,
threadIds: Array(communityIdsToRemove),
threadVariant: .community,
groupLeaveType: .forced,
calledFromConfigHandling: true
)
try SessionThread.deleteOrLeave(
db,
type: .deleteCommunityAndContent,
threadIds: Array(communityIdsToRemove),
calledFromConfigHandling: true
)
}

// MARK: -- Handle Legacy Group Changes
Expand Down Expand Up @@ -370,9 +368,8 @@ internal extension LibSession {
try SessionThread
.deleteOrLeave(
db,
type: .deleteGroupAndContent,
threadIds: Array(legacyGroupIdsToRemove),
threadVariant: .legacyGroup,
groupLeaveType: .forced,
calledFromConfigHandling: true
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ internal extension LibSession {
// `deleteOrLeave` behaviour (for 'Note to Self' this will leave the conversation
// but remove the associated interactions)
if !LibSession.shouldBeVisible(priority: targetPriority) {
try SessionThread
.deleteOrLeave(
db,
threadId: userPublicKey,
threadVariant: .contact,
groupLeaveType: .silent,
calledFromConfigHandling: true
)
try SessionThread.deleteOrLeave(
db,
type: .hideContactConversationAndDeleteContent,
threadId: userPublicKey,
calledFromConfigHandling: true
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ extension MessageReceiver {
_ = try SessionThread
.deleteOrLeave(
db,
type: .deleteContactConversationAndContact, // Blinded contact isn't synced anyway
threadId: blindedIdLookup.blindedId,
threadVariant: .contact,
groupLeaveType: .forced,
calledFromConfigHandling: false
)
}
Expand Down