damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

MutedThreadsManager.swift (1714B)


      1 //
      2 //  MutedThreadsManager.swift
      3 //  damus
      4 //
      5 //  Created by Terry Yiu on 4/6/23.
      6 //
      7 
      8 import Foundation
      9 
     10 fileprivate func getMutedThreadsKey(pubkey: Pubkey) -> String {
     11     pk_setting_key(pubkey, key: "muted_threads")
     12 }
     13 
     14 func loadOldMutedThreads(pubkey: Pubkey) -> [NoteId] {
     15     let key = getMutedThreadsKey(pubkey: pubkey)
     16     let xs = UserDefaults.standard.stringArray(forKey: key) ?? []
     17     return xs.reduce(into: [NoteId]()) { ids, k in
     18         guard let note_id = hex_decode(k) else { return }
     19         ids.append(NoteId(Data(note_id)))
     20     }
     21 }
     22 
     23 // We need to still use it since existing users might have their muted threads stored in UserDefaults
     24 // So now all it's doing is moving a users muted threads to the new kind:10000 system
     25 // It should not be used for any purpose beyond that
     26 func migrate_old_muted_threads_to_new_mutelist(keypair: Keypair, damus_state: DamusState) {
     27     // Ensure that keypair is fullkeypair
     28     guard let fullKeypair = keypair.to_full() else { return }
     29     // Load existing muted threads
     30     let mutedThreads = loadOldMutedThreads(pubkey: fullKeypair.pubkey)
     31     guard !mutedThreads.isEmpty else { return }
     32     // Set new muted system for those existing threads
     33     let previous_mute_list_event = damus_state.mutelist_manager.event
     34     guard let new_mutelist_event = create_or_update_mutelist(keypair: fullKeypair, mprev: previous_mute_list_event, to_add: Set(mutedThreads.map { MuteItem.thread($0, nil) })) else { return }
     35     damus_state.mutelist_manager.set_mutelist(new_mutelist_event)
     36     damus_state.postbox.send(new_mutelist_event)
     37     // Set existing muted threads to an empty array
     38     UserDefaults.standard.set([], forKey: getMutedThreadsKey(pubkey: keypair.pubkey))
     39 }