committed by
William Casarin
parent
0da10eb716
commit
9847f12c95
@@ -32,7 +32,6 @@ func save_bookmarks(pubkey: String, current_value: [NostrEvent], value: [NostrEv
|
||||
|
||||
class BookmarksManager: ObservableObject {
|
||||
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private let pubkey: String
|
||||
|
||||
private var _bookmarks: [NostrEvent]
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
//
|
||||
// ChatroomMetadata.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2022-09-07.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
struct ChatroomMetadata: Decodable {
|
||||
let name: String?
|
||||
let about: String?
|
||||
let picture: String?
|
||||
}
|
||||
@@ -56,12 +56,6 @@ class Contacts {
|
||||
}
|
||||
}
|
||||
|
||||
func get_friendosphere() -> [String] {
|
||||
var fs = get_friend_list()
|
||||
fs.append(contentsOf: get_friend_of_friend_list())
|
||||
return fs
|
||||
}
|
||||
|
||||
func remove_friend(_ pubkey: String) {
|
||||
friends.remove(pubkey)
|
||||
}
|
||||
@@ -70,10 +64,6 @@ class Contacts {
|
||||
return Array(friends)
|
||||
}
|
||||
|
||||
func get_friend_of_friend_list() -> [String] {
|
||||
return Array(friend_of_friends)
|
||||
}
|
||||
|
||||
func add_friend_pubkey(_ pubkey: String) {
|
||||
friends.insert(pubkey)
|
||||
}
|
||||
@@ -108,24 +98,6 @@ class Contacts {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func create_contacts(relays: [RelayDescriptor], our_pubkey: String, follow: ReferencedId) -> NostrEvent {
|
||||
let kind = NostrKind.contacts.rawValue
|
||||
let content = create_contacts_content(relays) ?? "{}"
|
||||
let tags = [refid_to_tag(follow)]
|
||||
return NostrEvent(content: content, pubkey: our_pubkey, kind: kind, tags: tags)
|
||||
}
|
||||
|
||||
func create_contacts_content(_ relays: [RelayDescriptor]) -> String? {
|
||||
// TODO: just create a new one of this is corrupted?
|
||||
let crelays = make_contact_relays(relays)
|
||||
guard let encoded = encode_json(crelays) else {
|
||||
return nil
|
||||
}
|
||||
return encoded
|
||||
}
|
||||
|
||||
|
||||
func follow_user(pool: RelayPool, our_contacts: NostrEvent?, pubkey: String, privkey: String, follow: ReferencedId) -> NostrEvent? {
|
||||
guard let ev = follow_user_event(our_contacts: our_contacts, our_pubkey: pubkey, follow: follow) else {
|
||||
return nil
|
||||
@@ -245,37 +217,3 @@ func make_contact_relays(_ relays: [RelayDescriptor]) -> [String: RelayInfo] {
|
||||
acc[relay.url.url.absoluteString] = relay.info
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: tests for this
|
||||
func is_friend_event(_ ev: NostrEvent, keypair: Keypair, contacts: Contacts) -> Bool
|
||||
{
|
||||
if !contacts.is_friend(ev.pubkey) {
|
||||
return false
|
||||
}
|
||||
|
||||
if ev.is_reply(keypair.privkey) {
|
||||
return true
|
||||
}
|
||||
|
||||
let pks = get_referenced_id_set(tags: ev.tags, key: "p")
|
||||
|
||||
// reply to self
|
||||
if pks.count == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// allow reply-to-self-or-friend case
|
||||
if pks.count == 1 && contacts.is_friend(pks.first!.ref_id) {
|
||||
return true
|
||||
}
|
||||
|
||||
// show our replies?
|
||||
for pk in pks {
|
||||
// don't count self mentions here
|
||||
if pk.ref_id != ev.pubkey && contacts.is_friend(pk.ref_id) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ struct DamusState {
|
||||
let likes: EventCounter
|
||||
let boosts: EventCounter
|
||||
let contacts: Contacts
|
||||
let tips: TipCounter
|
||||
let profiles: Profiles
|
||||
let dms: DirectMessagesModel
|
||||
let previews: PreviewCache
|
||||
@@ -47,8 +46,6 @@ struct DamusState {
|
||||
keypair.privkey != nil
|
||||
}
|
||||
|
||||
static var settings_pubkey: String? = nil
|
||||
|
||||
static var empty: DamusState {
|
||||
return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), tips: TipCounter(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: ""), previews: PreviewCache(), zaps: Zaps(our_pubkey: ""), lnurls: LNUrls(), settings: UserSettingsStore(), relay_filters: RelayFilters(our_pubkey: ""), relay_metadata: RelayMetadatas(), drafts: Drafts(), events: EventCache(), bookmarks: BookmarksManager(pubkey: ""), postbox: PostBox(pool: RelayPool()), bootstrap_relays: [], replies: ReplyCounter(our_pubkey: ""), muted_threads: MutedThreadsManager(keypair: Keypair(pubkey: "", privkey: nil))) }
|
||||
return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: ""), previews: PreviewCache(), zaps: Zaps(our_pubkey: ""), lnurls: LNUrls(), settings: UserSettingsStore(), relay_filters: RelayFilters(our_pubkey: ""), relay_metadata: RelayMetadatas(), drafts: Drafts(), events: EventCache(), bookmarks: BookmarksManager(pubkey: ""), postbox: PostBox(pool: RelayPool()), bootstrap_relays: [], replies: ReplyCounter(our_pubkey: ""), muted_threads: MutedThreadsManager(keypair: Keypair(pubkey: "", privkey: nil))) }
|
||||
}
|
||||
|
||||
@@ -40,8 +40,4 @@ enum DeepLPlan: String, CaseIterable, Identifiable, StringCodable {
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("Pro", comment: "Dropdown option for selecting Pro plan for DeepL translation service."), url: "https://api.deepl.com")
|
||||
}
|
||||
}
|
||||
|
||||
static var allModels: [Model] {
|
||||
return Self.allCases.map { $0.model }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,15 +60,6 @@ enum EventRef {
|
||||
}
|
||||
}
|
||||
|
||||
func has_any_e_refs(_ tags: [[String]]) -> Bool {
|
||||
for tag in tags {
|
||||
if tag.count >= 2 && tag[0] == "e" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func build_mention_indices(_ blocks: [Block], type: MentionType) -> Set<Int> {
|
||||
return blocks.reduce(into: []) { acc, block in
|
||||
switch block {
|
||||
|
||||
@@ -10,7 +10,6 @@ import Foundation
|
||||
class FollowersModel: ObservableObject {
|
||||
let damus_state: DamusState
|
||||
let target: String
|
||||
var needs_sub: Bool = true
|
||||
|
||||
@Published var contacts: [String]? = nil
|
||||
var has_contact: Set<String> = Set()
|
||||
|
||||
@@ -11,7 +11,6 @@ class FollowingModel {
|
||||
let damus_state: DamusState
|
||||
var needs_sub: Bool = true
|
||||
|
||||
var has_contact: Set<String> = Set()
|
||||
let contacts: [String]
|
||||
|
||||
let sub_id: String = UUID().description
|
||||
|
||||
@@ -121,7 +121,7 @@ class HomeModel: ObservableObject {
|
||||
case .channel_create:
|
||||
handle_channel_create(ev)
|
||||
case .channel_meta:
|
||||
handle_channel_meta(ev)
|
||||
break
|
||||
case .zap:
|
||||
handle_zap_event(ev)
|
||||
case .zap_request:
|
||||
@@ -140,7 +140,7 @@ class HomeModel: ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
if !notifications.insert_zap(zap, damus_state: damus_state) {
|
||||
if !notifications.insert_zap(zap) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -195,9 +195,6 @@ class HomeModel: ObservableObject {
|
||||
self.channels[ev.id] = ev
|
||||
}
|
||||
|
||||
func handle_channel_meta(_ ev: NostrEvent) {
|
||||
}
|
||||
|
||||
func filter_events() {
|
||||
events.filter { ev in
|
||||
!damus_state.contacts.is_muted(ev.pubkey)
|
||||
@@ -327,7 +324,6 @@ class HomeModel: ObservableObject {
|
||||
|
||||
self.process_event(sub_id: sub_id, relay_id: relay_id, ev: ev)
|
||||
case .notice(let msg):
|
||||
//self.events.insert(NostrEvent(content: "NOTICE from \(relay_id): \(msg)", pubkey: "system"), at: 0)
|
||||
print(msg)
|
||||
|
||||
case .eose(let sub_id):
|
||||
@@ -600,7 +596,7 @@ func add_contact_if_friend(contacts: Contacts, ev: NostrEvent) {
|
||||
contacts.add_friend_contact(ev)
|
||||
}
|
||||
|
||||
func load_our_contacts(contacts: Contacts, our_pubkey: String, m_old_ev: NostrEvent?, ev: NostrEvent) {
|
||||
func load_our_contacts(contacts: Contacts, m_old_ev: NostrEvent?, ev: NostrEvent) {
|
||||
var new_pks = Set<String>()
|
||||
// our contacts
|
||||
for tag in ev.tags {
|
||||
@@ -798,7 +794,7 @@ func load_our_stuff(state: DamusState, ev: NostrEvent) {
|
||||
let m_old_ev = state.contacts.event
|
||||
state.contacts.event = ev
|
||||
|
||||
load_our_contacts(contacts: state.contacts, our_pubkey: state.pubkey, m_old_ev: m_old_ev, ev: ev)
|
||||
load_our_contacts(contacts: state.contacts, m_old_ev: m_old_ev, ev: ev)
|
||||
load_our_relays(state: state, m_old_ev: m_old_ev, ev: ev)
|
||||
}
|
||||
|
||||
@@ -896,9 +892,6 @@ func fetch_relay_metadata(relay_id: String) async throws -> RelayMetadata? {
|
||||
return nip11
|
||||
}
|
||||
|
||||
func process_relay_metadata() {
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func handle_incoming_dm(ev: NostrEvent, our_pubkey: String, dms: DirectMessagesModel, prev_events: NewEventsBits) -> (Bool, NewEventsBits?) {
|
||||
var inserted = false
|
||||
|
||||
@@ -42,8 +42,4 @@ enum LibreTranslateServer: String, CaseIterable, Identifiable, StringCodable {
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("Custom", comment: "Dropdown option for selecting a custom translation server."), url: nil)
|
||||
}
|
||||
}
|
||||
|
||||
static var allModels: [Model] {
|
||||
return Self.allCases.map { $0.model }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,3 @@ struct Counted {
|
||||
let id: String
|
||||
let total: Int
|
||||
}
|
||||
|
||||
struct LikeRefs {
|
||||
let thread_id: String?
|
||||
let like_id: String
|
||||
}
|
||||
|
||||
@@ -27,11 +27,6 @@ struct Mention: Equatable {
|
||||
let ref: ReferencedId
|
||||
}
|
||||
|
||||
struct IdBlock: Identifiable {
|
||||
let id: String = UUID().description
|
||||
let block: Block
|
||||
}
|
||||
|
||||
typealias Invoice = LightningInvoice<Amount>
|
||||
typealias ZapInvoice = LightningInvoice<Int64>
|
||||
|
||||
@@ -155,10 +150,6 @@ func render_blocks(blocks: [Block]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
func parse_textblock(str: String, from: Int, to: Int) -> Block {
|
||||
return .text(String(substring(str, start: from, end: to)))
|
||||
}
|
||||
|
||||
func parse_mentions(content: String, tags: [[String]]) -> [Block] {
|
||||
var out: [Block] = []
|
||||
|
||||
@@ -249,32 +240,6 @@ enum Amount: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
func format_actions_abbrev(_ actions: Int) -> String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .decimal
|
||||
formatter.positiveSuffix = "m"
|
||||
formatter.positivePrefix = ""
|
||||
formatter.minimumFractionDigits = 0
|
||||
formatter.maximumFractionDigits = 3
|
||||
formatter.roundingMode = .down
|
||||
formatter.roundingIncrement = 0.1
|
||||
formatter.multiplier = 1
|
||||
|
||||
if actions >= 1_000_000 {
|
||||
formatter.positiveSuffix = "m"
|
||||
formatter.multiplier = 0.000001
|
||||
} else if actions >= 1000 {
|
||||
formatter.positiveSuffix = "k"
|
||||
formatter.multiplier = 0.001
|
||||
} else {
|
||||
return "\(actions)"
|
||||
}
|
||||
|
||||
let actions = NSNumber(value: actions)
|
||||
|
||||
return formatter.string(from: actions) ?? "\(actions)"
|
||||
}
|
||||
|
||||
func format_msats_abbrev(_ msats: Int64) -> String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .decimal
|
||||
@@ -423,59 +388,6 @@ func convert_mention_index_block(ind: Int32, tags: [[String]]) -> Block?
|
||||
return .mention(Mention(index: ind, type: mention_type, ref: ref))
|
||||
}
|
||||
|
||||
func parse_mentions_old(content: String, tags: [[String]]) -> [Block] {
|
||||
let p = Parser(pos: 0, str: content)
|
||||
var blocks: [Block] = []
|
||||
var starting_from: Int = 0
|
||||
|
||||
while p.pos < content.count {
|
||||
if !consume_until(p, match: { !$0.isWhitespace}) {
|
||||
break
|
||||
}
|
||||
|
||||
let pre_mention = p.pos
|
||||
|
||||
let c = peek_char(p, 0)
|
||||
let pr = peek_char(p, -1)
|
||||
|
||||
if c == "#" {
|
||||
if let mention = parse_mention(p, tags: tags) {
|
||||
blocks.append(parse_textblock(str: p.str, from: starting_from, to: pre_mention))
|
||||
blocks.append(.mention(mention))
|
||||
starting_from = p.pos
|
||||
} else if let hashtag = parse_hashtag(p) {
|
||||
blocks.append(parse_textblock(str: p.str, from: starting_from, to: pre_mention))
|
||||
blocks.append(.hashtag(hashtag))
|
||||
starting_from = p.pos
|
||||
} else {
|
||||
if !consume_until(p, match: { $0.isWhitespace }) {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if c == "h" && (pr == nil || pr!.isWhitespace) {
|
||||
if let url = parse_url(p) {
|
||||
blocks.append(parse_textblock(str: p.str, from: starting_from, to: pre_mention))
|
||||
blocks.append(.url(url))
|
||||
starting_from = p.pos
|
||||
} else {
|
||||
if !consume_until(p, match: { $0.isWhitespace }) {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !consume_until(p, match: { $0.isWhitespace }) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if p.str.count - starting_from > 0 {
|
||||
blocks.append(parse_textblock(str: p.str, from: starting_from, to: p.str.count))
|
||||
}
|
||||
|
||||
return blocks
|
||||
}
|
||||
|
||||
func parse_while(_ p: Parser, match: (Character) -> Bool) -> String? {
|
||||
var i: Int = 0
|
||||
let sub = substring(p.str, start: p.pos, end: p.str.count)
|
||||
@@ -513,37 +425,6 @@ func is_punctuation(_ c: Character) -> Bool {
|
||||
return c.isWhitespace || c.isPunctuation
|
||||
}
|
||||
|
||||
func parse_url(_ p: Parser) -> URL? {
|
||||
let start = p.pos
|
||||
|
||||
if !parse_str(p, "http") {
|
||||
return nil
|
||||
}
|
||||
|
||||
if parse_char(p, "s") {
|
||||
if !parse_str(p, "://") {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
if !parse_str(p, "://") {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if !consume_until(p, match: { c in c.isWhitespace }, end_ok: true) {
|
||||
p.pos = start
|
||||
return nil
|
||||
}
|
||||
|
||||
let url_str = String(substring(p.str, start: start, end: p.pos))
|
||||
guard let url = URL(string: url_str) else {
|
||||
p.pos = start
|
||||
return nil
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func parse_hashtag(_ p: Parser) -> String? {
|
||||
let start = p.pos
|
||||
|
||||
@@ -566,51 +447,6 @@ func parse_hashtag(_ p: Parser) -> String? {
|
||||
return str
|
||||
}
|
||||
|
||||
func parse_mention(_ p: Parser, tags: [[String]]) -> Mention? {
|
||||
let start = p.pos
|
||||
|
||||
if !parse_str(p, "#[") {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let digit = parse_digit(p) else {
|
||||
p.pos = start
|
||||
return nil
|
||||
}
|
||||
|
||||
var ind = digit
|
||||
|
||||
if let d2 = parse_digit(p) {
|
||||
ind = digit * 10
|
||||
ind += d2
|
||||
}
|
||||
|
||||
if !parse_char(p, "]") {
|
||||
return nil
|
||||
}
|
||||
|
||||
var kind: MentionType = .pubkey
|
||||
if ind > tags.count - 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if tags[ind].count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tags[ind][0] {
|
||||
case "e": kind = .event
|
||||
case "p": kind = .pubkey
|
||||
default: return nil
|
||||
}
|
||||
|
||||
guard let ref = tag_to_refid(tags[ind]) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return Mention(index: ind, type: kind, ref: ref)
|
||||
}
|
||||
|
||||
func find_tag_ref(type: String, id: String, tags: [[String]]) -> Int? {
|
||||
var i: Int = 0
|
||||
for tag in tags {
|
||||
|
||||
@@ -29,7 +29,6 @@ func saveMutedThreads(pubkey: String, currentValue: [String], value: [String]) -
|
||||
|
||||
class MutedThreadsManager: ObservableObject {
|
||||
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private let keypair: Keypair
|
||||
|
||||
private var _mutedThreadsSet: Set<String>
|
||||
@@ -68,9 +67,4 @@ class MutedThreadsManager: ObservableObject {
|
||||
notify(.mute_thread, ev)
|
||||
}
|
||||
}
|
||||
|
||||
func clearAll() {
|
||||
mutedThreads = []
|
||||
_mutedThreadsSet.removeAll()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
//
|
||||
// ListModel.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-01-25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/*
|
||||
class MutelistModel: ObservableObject {
|
||||
let contacts: Contacts
|
||||
|
||||
@Published var users: [String]
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -285,7 +285,7 @@ class NotificationsModel: ObservableObject, ScrollQueue {
|
||||
return false
|
||||
}
|
||||
|
||||
func insert_zap(_ zap: Zap, damus_state: DamusState) -> Bool {
|
||||
func insert_zap(_ zap: Zap) -> Bool {
|
||||
if should_queue {
|
||||
return insert_uniq_sorted_zap_by_created(zaps: &incoming_zaps, new_zap: zap)
|
||||
}
|
||||
|
||||
@@ -19,10 +19,6 @@ struct NostrPost {
|
||||
self.kind = kind
|
||||
self.tags = tags
|
||||
}
|
||||
|
||||
func to_event(keypair: FullKeypair) -> NostrEvent {
|
||||
return post_to_event(post: self, privkey: keypair.privkey, pubkey: keypair.pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: parse nostr:{e,p}:pubkey uris as well
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
//
|
||||
// SearchResultsModel.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-03-03.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
class SearchResultsModel: ObservableObject {
|
||||
}
|
||||
@@ -12,7 +12,6 @@ class SearchModel: ObservableObject {
|
||||
let state: DamusState
|
||||
var events: EventHolder
|
||||
@Published var loading: Bool = false
|
||||
@Published var channel_name: String? = nil
|
||||
|
||||
var search: NostrFilter
|
||||
let sub_id = UUID().description
|
||||
@@ -65,24 +64,14 @@ class SearchModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func handle_channel_create(_ ev: NostrEvent) {
|
||||
self.channel_name = ev.content
|
||||
return
|
||||
}
|
||||
|
||||
func handle_channel_meta(_ ev: NostrEvent) {
|
||||
self.channel_name = ev.content
|
||||
return
|
||||
}
|
||||
|
||||
func handle_event(relay_id: String, ev: NostrConnectionEvent) {
|
||||
let (sub_id, done) = handle_subid_event(pool: state.pool, relay_id: relay_id, ev: ev) { sub_id, ev in
|
||||
if ev.is_textlike && ev.should_show_event {
|
||||
self.add_event(ev)
|
||||
} else if ev.known_kind == .channel_create {
|
||||
handle_channel_create(ev)
|
||||
// unimplemented
|
||||
} else if ev.known_kind == .channel_meta {
|
||||
handle_channel_meta(ev)
|
||||
// unimplemented
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,16 +101,6 @@ func tag_is_hashtag(_ tag: [String]) -> Bool {
|
||||
return tag.count >= 2 && (tag[0] == "hashtag" || tag[0] == "t")
|
||||
}
|
||||
|
||||
func has_hashtag(_ tags: [[String]], hashtag: String) -> Bool {
|
||||
for tag in tags {
|
||||
if tag_is_hashtag(tag) && tag[1] == hashtag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func event_matches_filter(_ ev: NostrEvent, filter: NostrFilter) -> Bool {
|
||||
if let hashtags = filter.hashtag {
|
||||
return event_matches_hashtag(ev, hashtags: hashtags)
|
||||
|
||||
@@ -12,14 +12,6 @@ class SignalModel: ObservableObject {
|
||||
@Published var signal: Int
|
||||
@Published var max_signal: Int
|
||||
|
||||
var percentage: Double {
|
||||
if max_signal == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return Double(signal) / Double(max_signal)
|
||||
}
|
||||
|
||||
init() {
|
||||
self.signal = 0
|
||||
self.max_signal = 0
|
||||
|
||||
@@ -7,41 +7,16 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
enum InitialEvent {
|
||||
case event(NostrEvent)
|
||||
case event_id(String)
|
||||
|
||||
var is_event_id: String? {
|
||||
if case .event_id(let evid) = self {
|
||||
return evid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var id: String {
|
||||
switch self {
|
||||
case .event(let ev):
|
||||
return ev.id
|
||||
case .event_id(let evid):
|
||||
return evid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// manages the lifetime of a thread
|
||||
class ThreadModel: ObservableObject {
|
||||
@Published var event: NostrEvent
|
||||
var event_map: Set<NostrEvent>
|
||||
|
||||
@Published var loading: Bool = false
|
||||
|
||||
var replies: ReplyMap = ReplyMap()
|
||||
|
||||
init(event: NostrEvent, damus_state: DamusState) {
|
||||
self.damus_state = damus_state
|
||||
self.event_map = Set()
|
||||
self.event = event
|
||||
add_event(event, privkey: nil)
|
||||
add_event(event)
|
||||
}
|
||||
|
||||
let damus_state: DamusState
|
||||
@@ -65,9 +40,9 @@ class ThreadModel: ObservableObject {
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func set_active_event(_ ev: NostrEvent, privkey: String?) -> Bool {
|
||||
func set_active_event(_ ev: NostrEvent) -> Bool {
|
||||
self.event = ev
|
||||
add_event(ev, privkey: privkey)
|
||||
add_event(ev)
|
||||
|
||||
//self.objectWillChange.send()
|
||||
return false
|
||||
@@ -96,24 +71,15 @@ class ThreadModel: ObservableObject {
|
||||
|
||||
meta_events.limit = 1000
|
||||
|
||||
/*
|
||||
if let last_ev = self.events.last {
|
||||
if last_ev.created_at <= Int64(Date().timeIntervalSince1970) {
|
||||
ref_events.since = last_ev.created_at
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
let base_filters = [event_filter, ref_events]
|
||||
let meta_filters = [meta_events]
|
||||
|
||||
print("subscribing to thread \(event.id) with sub_id \(base_subid)")
|
||||
loading = true
|
||||
damus_state.pool.subscribe(sub_id: base_subid, filters: base_filters, handler: handle_event)
|
||||
damus_state.pool.subscribe(sub_id: meta_subid, filters: meta_filters, handler: handle_event)
|
||||
}
|
||||
|
||||
func add_event(_ ev: NostrEvent, privkey: String?) {
|
||||
func add_event(_ ev: NostrEvent) {
|
||||
if event_map.contains(ev) {
|
||||
return
|
||||
}
|
||||
@@ -136,7 +102,7 @@ class ThreadModel: ObservableObject {
|
||||
if ev.known_kind == .metadata {
|
||||
process_metadata_event(events: damus_state.events, our_pubkey: damus_state.pubkey, profiles: damus_state.profiles, ev: ev)
|
||||
} else if ev.is_textlike {
|
||||
self.add_event(ev, privkey: self.damus_state.keypair.privkey)
|
||||
self.add_event(ev)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,10 +110,6 @@ class ThreadModel: ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
if event_map.contains(event) {
|
||||
loading = false
|
||||
}
|
||||
|
||||
if sub_id == self.base_subid {
|
||||
load_profiles(profiles_subid: self.profiles_subid, relay_id: relay_id, load: .from_events(Array(event_map)), damus_state: damus_state)
|
||||
}
|
||||
|
||||
@@ -45,8 +45,4 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("NoKYCTranslate.com (Prepay with BTC)", comment: "Dropdown option for selecting NoKYCTranslate.com as the translation service."))
|
||||
}
|
||||
}
|
||||
|
||||
static var allModels: [Model] {
|
||||
return Self.allCases.map { $0.model }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user