Skip to content
Open
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
26 changes: 12 additions & 14 deletions com.stakwork.sphinx.desktop/CoreData/Chat/Chat+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public class Chat: NSManagedObject {
if pic.isNotEmpty {
aliasesAndPics[index] = (alias, pic)
}
} else if !aliasesAndPics.contains(where: { $0.1 == pic && pic.isNotEmpty }) {
// Add new entry if alias doesn't exist and pic is unique (or empty)
} else {
// Add new entry alias is the unique key, not pic
aliasesAndPics.append((alias, pic))
}
}

/// Removes alias/pic entry by alias or pic
private func removeAliasAndPic(alias: String?, pic: String?) {
if let index = aliasesAndPics.firstIndex(where: {
(alias != nil && $0.0 == alias) || (pic != nil && pic!.isNotEmpty && $0.1 == pic)
alias != nil ? $0.0 == alias : (pic != nil && pic!.isNotEmpty && $0.1 == pic)
}) {
aliasesAndPics.remove(at: index)
}
Expand Down Expand Up @@ -485,9 +485,6 @@ public class Chat: NSManagedObject {
if !message.isIncoming(ownerId: ownerId) {
continue
}
if isDateBeforeThreeMonthsAgo(message.messageDate) {
continue
}
if let alias = message.senderAlias, alias.isNotEmpty {
if let remoteTimezoneIdentifier = message.remoteTimezoneIdentifier, remoteTimezoneIdentifier.isNotEmpty {
timezoneData[alias] = remoteTimezoneIdentifier
Expand Down Expand Up @@ -541,14 +538,6 @@ public class Chat: NSManagedObject {
persistAliasesAndPics()
}

func isDateBeforeThreeMonthsAgo(_ date: Date) -> Bool {
let calendar = Calendar.current
if let threeMonthsAgo = calendar.date(byAdding: .month, value: -3, to: Date()) {
return date < threeMonthsAgo
}
return false
}

static func getChatsWith(uuids: [String]) -> [Chat] {
let predicate = NSPredicate(format: "uuid IN %@", uuids)
let sortDescriptors = [NSSortDescriptor(key: "id", ascending: false)]
Expand Down Expand Up @@ -1120,6 +1109,15 @@ public class Chat: NSManagedObject {
}
}

public static func resetAliasesCache(context: NSManagedObjectContext) {
let chats: [Chat] = Chat.getAll(context: context)

for chat in chats {
chat.membersAliasesData = nil
chat.aliasesAndPics = []
}
}

public static func setChatsToTimezoneUpdated(context: NSManagedObjectContext) {
let predicate = NSPredicate(format: "timezoneIdentifier == nil && timezoneEnabled == true")

Expand Down
1 change: 1 addition & 0 deletions com.stakwork.sphinx.desktop/Extensions/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extension UserDefaults {
public static let routerUrl = DefaultKey<String>("router")
public static let routerPubkey = DefaultKey<String>("routerPubkey")
public static let didMigrateToTZ = DefaultKey<Bool>("didMigrateToTZ")
public static let didResetAliasesCache = DefaultKey<Bool>("didResetAliasesCache")
public static let systemTimezone = DefaultKey<String>("systemTimezone")
public static let skipAds = DefaultKey<Bool>("skipAds")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ class DashboardViewController: NSViewController {
Chat.resetTimezones(context: backgroundContext)
}

let didResetAliasesCache: Bool = UserDefaults.Keys.didResetAliasesCache.get(defaultValue: false)
if !didResetAliasesCache {
Chat.resetAliasesCache(context: backgroundContext)
UserDefaults.Keys.didResetAliasesCache.set(true)
}

if let systemTimezone: String? = UserDefaults.Keys.systemTimezone.get() {
if systemTimezone != TimeZone.current.abbreviation() {
Chat.setChatsToTimezoneUpdated(context: backgroundContext)
Expand Down