Huge refactor to add better structure to the project. Separating features with their associated view and model structure. This should be better organization and will allow us to improve the overall architecture in the future. I forsee many more improvements that can follow this change. e.g. MVVM Arch As well as cleaning up duplicate, unused, functionality. Many files have global functions that can also be moved or be renamed. damus/ ├── Features/ │ ├── <Feature>/ │ │ ├── Views/ │ │ └── Models/ ├── Shared/ │ ├── Components/ │ ├── Media/ │ ├── Buttons/ │ ├── Extensions/ │ ├── Empty Views/ │ ├── ErrorHandling/ │ ├── Modifiers/ │ └── Utilities/ ├── Core/ │ ├── Nostr/ │ ├── NIPs/ │ ├── DIPs/ │ ├── Types/ │ ├── Networking/ │ └── Storage/ Signed-off-by: ericholguin <ericholguin@apache.org>
53 lines
1.1 KiB
Swift
53 lines
1.1 KiB
Swift
//
|
|
// LikeCounter.swift
|
|
// damus
|
|
//
|
|
// Created by William Casarin on 2022-04-30.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum CountResult {
|
|
case already_counted
|
|
case success(Int)
|
|
}
|
|
|
|
class EventCounter {
|
|
var counts: [NoteId: Int] = [:]
|
|
var user_events: [Pubkey: Set<NoteId>] = [:]
|
|
var our_events: [NoteId: NostrEvent] = [:]
|
|
var our_pubkey: Pubkey
|
|
|
|
init(our_pubkey: Pubkey) {
|
|
self.our_pubkey = our_pubkey
|
|
}
|
|
|
|
func add_event(_ ev: NostrEvent, target: NoteId) -> CountResult {
|
|
let pubkey = ev.pubkey
|
|
|
|
if self.user_events[pubkey] == nil {
|
|
self.user_events[pubkey] = Set()
|
|
}
|
|
|
|
if user_events[pubkey]!.contains(target) {
|
|
// don't double count
|
|
return .already_counted
|
|
}
|
|
|
|
user_events[pubkey]!.insert(target)
|
|
|
|
if ev.pubkey == self.our_pubkey {
|
|
our_events[target] = ev
|
|
}
|
|
|
|
if counts[target] == nil {
|
|
counts[target] = 1
|
|
return .success(1)
|
|
}
|
|
|
|
counts[target]! += 1
|
|
|
|
return .success(counts[target]!)
|
|
}
|
|
}
|