damus

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

Contacts.swift (2541B)


      1 //
      2 //  Contacts.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2022-05-14.
      6 //
      7 
      8 import Foundation
      9 
     10 
     11 class Contacts {
     12     private var friends: Set<Pubkey> = Set()
     13     private var friend_of_friends: Set<Pubkey> = Set()
     14     /// Tracks which friends are friends of a given pubkey.
     15     private var pubkey_to_our_friends = [Pubkey : Set<Pubkey>]()
     16 
     17     let our_pubkey: Pubkey
     18     var event: NostrEvent?
     19 
     20     init(our_pubkey: Pubkey) {
     21         self.our_pubkey = our_pubkey
     22     }
     23 
     24     func remove_friend(_ pubkey: Pubkey) {
     25         friends.remove(pubkey)
     26 
     27         pubkey_to_our_friends.forEach {
     28             pubkey_to_our_friends[$0.key]?.remove(pubkey)
     29         }
     30     }
     31     
     32     func get_friend_list() -> Set<Pubkey> {
     33         return friends
     34     }
     35 
     36     func get_followed_hashtags() -> Set<String> {
     37         guard let ev = self.event else { return Set() }
     38         return Set(ev.referenced_hashtags.map({ $0.hashtag }))
     39     }
     40     
     41     func follows(hashtag: Hashtag) -> Bool {
     42         guard let ev = self.event else { return false }
     43         return ev.referenced_hashtags.first(where: { $0 == hashtag }) != nil
     44     }
     45 
     46     func add_friend_pubkey(_ pubkey: Pubkey) {
     47         friends.insert(pubkey)
     48     }
     49     
     50     func add_friend_contact(_ contact: NostrEvent) {
     51         friends.insert(contact.pubkey)
     52         for pk in contact.referenced_pubkeys {
     53             friend_of_friends.insert(pk)
     54 
     55             // Exclude themself and us.
     56             if contact.pubkey != our_pubkey && contact.pubkey != pk {
     57                 if pubkey_to_our_friends[pk] == nil {
     58                     pubkey_to_our_friends[pk] = Set<Pubkey>()
     59                 }
     60 
     61                 pubkey_to_our_friends[pk]?.insert(contact.pubkey)
     62             }
     63         }
     64     }
     65     
     66     func is_friend_of_friend(_ pubkey: Pubkey) -> Bool {
     67         return friend_of_friends.contains(pubkey)
     68     }
     69     
     70     func is_in_friendosphere(_ pubkey: Pubkey) -> Bool {
     71         return friends.contains(pubkey) || friend_of_friends.contains(pubkey)
     72     }
     73 
     74     func is_friend(_ pubkey: Pubkey) -> Bool {
     75         return friends.contains(pubkey)
     76     }
     77     
     78     func is_friend_or_self(_ pubkey: Pubkey) -> Bool {
     79         return pubkey == our_pubkey || is_friend(pubkey)
     80     }
     81     
     82     func follow_state(_ pubkey: Pubkey) -> FollowState {
     83         return is_friend(pubkey) ? .follows : .unfollows
     84     }
     85 
     86     /// Gets the list of pubkeys of our friends who follow the given pubkey.
     87     func get_friended_followers(_ pubkey: Pubkey) -> [Pubkey] {
     88         return Array((pubkey_to_our_friends[pubkey] ?? Set()))
     89     }
     90 }