Skip to content

Commit

Permalink
Add data types for Pubkey, NoteId, etc
Browse files Browse the repository at this point in the history
This is a huge change but makes the entire codebase faster and more
typesafe
  • Loading branch information
jb55 committed Jul 28, 2023
1 parent 76a8265 commit 3ad0d4e
Show file tree
Hide file tree
Showing 166 changed files with 2,222 additions and 1,892 deletions.
36 changes: 36 additions & 0 deletions damus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
4C285C8C28398BC7008A31F1 /* Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C285C8B28398BC6008A31F1 /* Keys.swift */; };
4C285C8E28399BFE008A31F1 /* SaveKeysView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C285C8D28399BFD008A31F1 /* SaveKeysView.swift */; };
4C28A4122A6D03D200C1A7A5 /* ReferencedId.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C28A4112A6D03D200C1A7A5 /* ReferencedId.swift */; };
4C2B7BF22A71B6540049DEE7 /* Id.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C2B7BF12A71B6540049DEE7 /* Id.swift */; };
4C2CDDF7299D4A5E00879FD5 /* Debouncer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C2CDDF6299D4A5E00879FD5 /* Debouncer.swift */; };
4C30AC7229A5677A00E2BD5A /* NotificationsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30AC7129A5677A00E2BD5A /* NotificationsView.swift */; };
4C30AC7429A5680900E2BD5A /* EventGroupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30AC7329A5680900E2BD5A /* EventGroupView.swift */; };
Expand Down Expand Up @@ -235,6 +236,10 @@
4CB9D4A72992D02B00A9A7E4 /* ProfileNameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB9D4A62992D02B00A9A7E4 /* ProfileNameView.swift */; };
4CB9D4A92992D2F400A9A7E4 /* FollowsYou.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB9D4A82992D2F400A9A7E4 /* FollowsYou.swift */; };
4CBCA930297DB57F00EC6B2F /* WebsiteLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CBCA92F297DB57F00EC6B2F /* WebsiteLink.swift */; };
4CC14FEF2A73FCCB007AEB17 /* IdType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FEE2A73FCCB007AEB17 /* IdType.swift */; };
4CC14FF12A73FCDB007AEB17 /* Pubkey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FF02A73FCDB007AEB17 /* Pubkey.swift */; };
4CC14FF52A740BB7007AEB17 /* NoteId.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FF42A740BB7007AEB17 /* NoteId.swift */; };
4CC14FF92A741939007AEB17 /* Referenced.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FF82A741939007AEB17 /* Referenced.swift */; };
4CC6193A29DC777C006A86D1 /* RelayBootstrap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC6193929DC777C006A86D1 /* RelayBootstrap.swift */; };
4CC7AAE7297EFA7B00430951 /* Zap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC7AAE6297EFA7B00430951 /* Zap.swift */; };
4CC7AAEB297F0AEC00430951 /* BuilderEventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC7AAEA297F0AEC00430951 /* BuilderEventView.swift */; };
Expand Down Expand Up @@ -529,6 +534,7 @@
4C285C8B28398BC6008A31F1 /* Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keys.swift; sourceTree = "<group>"; };
4C285C8D28399BFD008A31F1 /* SaveKeysView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveKeysView.swift; sourceTree = "<group>"; };
4C28A4112A6D03D200C1A7A5 /* ReferencedId.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReferencedId.swift; sourceTree = "<group>"; };
4C2B7BF12A71B6540049DEE7 /* Id.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Id.swift; sourceTree = "<group>"; };
4C2CDDF6299D4A5E00879FD5 /* Debouncer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Debouncer.swift; sourceTree = "<group>"; };
4C30AC7129A5677A00E2BD5A /* NotificationsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsView.swift; sourceTree = "<group>"; };
4C30AC7329A5680900E2BD5A /* EventGroupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventGroupView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -734,6 +740,10 @@
4CB9D4A62992D02B00A9A7E4 /* ProfileNameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileNameView.swift; sourceTree = "<group>"; };
4CB9D4A82992D2F400A9A7E4 /* FollowsYou.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowsYou.swift; sourceTree = "<group>"; };
4CBCA92F297DB57F00EC6B2F /* WebsiteLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebsiteLink.swift; sourceTree = "<group>"; };
4CC14FEE2A73FCCB007AEB17 /* IdType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdType.swift; sourceTree = "<group>"; };
4CC14FF02A73FCDB007AEB17 /* Pubkey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pubkey.swift; sourceTree = "<group>"; };
4CC14FF42A740BB7007AEB17 /* NoteId.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteId.swift; sourceTree = "<group>"; };
4CC14FF82A741939007AEB17 /* Referenced.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Referenced.swift; sourceTree = "<group>"; };
4CC6193929DC777C006A86D1 /* RelayBootstrap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayBootstrap.swift; sourceTree = "<group>"; };
4CC7AAE6297EFA7B00430951 /* Zap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Zap.swift; sourceTree = "<group>"; };
4CC7AAEA297F0AEC00430951 /* BuilderEventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuilderEventView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1223,6 +1233,7 @@
4C363A8F28247A1D006E126D /* NostrLink.swift */,
50088DA029E8271A008A1FDF /* WebSocket.swift */,
4C28A4112A6D03D200C1A7A5 /* ReferencedId.swift */,
4C2B7BF12A71B6540049DEE7 /* Id.swift */,
);
path = Nostr;
sourceTree = "<group>";
Expand Down Expand Up @@ -1430,6 +1441,25 @@
path = Profile;
sourceTree = "<group>";
};
4CC14FEC2A73FC9A007AEB17 /* Types */ = {
isa = PBXGroup;
children = (
4CC14FED2A73FCBB007AEB17 /* Ids */,
);
path = Types;
sourceTree = "<group>";
};
4CC14FED2A73FCBB007AEB17 /* Ids */ = {
isa = PBXGroup;
children = (
4CC14FEE2A73FCCB007AEB17 /* IdType.swift */,
4CC14FF02A73FCDB007AEB17 /* Pubkey.swift */,
4CC14FF42A740BB7007AEB17 /* NoteId.swift */,
4CC14FF82A741939007AEB17 /* Referenced.swift */,
);
path = Ids;
sourceTree = "<group>";
};
4CC7AAEE297F11B300430951 /* Events */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1525,6 +1555,7 @@
4CE6DEE527F7A08100C66700 /* damus */ = {
isa = PBXGroup;
children = (
4CC14FEC2A73FC9A007AEB17 /* Types */,
F7F0BA23297892AE009531F3 /* Modifiers */,
4C4A3A5A288A1B2200453788 /* damus.entitlements */,
4CE4F9DF285287A000C00DD9 /* Components */,
Expand Down Expand Up @@ -1924,6 +1955,7 @@
4C3EA67728FF7A9800C48A62 /* talstr.c in Sources */,
4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */,
4CEE2AF5280B29E600AB5EEF /* TimeAgo.swift in Sources */,
4CC14FF12A73FCDB007AEB17 /* Pubkey.swift in Sources */,
4CA9275D2A28FF630098A105 /* LongformView.swift in Sources */,
4C75EFAD28049CFB0006080F /* PostButton.swift in Sources */,
504323A92A3495B6006AE6DC /* RelayModelCache.swift in Sources */,
Expand Down Expand Up @@ -2008,6 +2040,7 @@
4C285C8228385570008A31F1 /* CarouselView.swift in Sources */,
3A3040F129A8FF97008A0F29 /* LocalizationUtil.swift in Sources */,
F75BA12D29A1855400E10810 /* BookmarksManager.swift in Sources */,
4CC14FEF2A73FCCB007AEB17 /* IdType.swift in Sources */,
4C3EA67F28FFC01D00C48A62 /* InvoiceView.swift in Sources */,
4CE8794829941DA700F758CC /* RelayFilters.swift in Sources */,
4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */,
Expand All @@ -2021,6 +2054,7 @@
4C3BEFD22819DB9B00B3DE84 /* ProfileModel.swift in Sources */,
4C7D09682A0AE9B200943473 /* NWCScannerView.swift in Sources */,
4C0A3F93280F66F5000448DE /* ReplyMap.swift in Sources */,
4C2B7BF22A71B6540049DEE7 /* Id.swift in Sources */,
7C95CAEE299DCEF1009DCB67 /* KFOptionSetter+.swift in Sources */,
4C7D09722A0AEF5E00943473 /* DamusGradient.swift in Sources */,
4CCEB7B029B5415A0078AA28 /* SearchingProfileView.swift in Sources */,
Expand Down Expand Up @@ -2086,6 +2120,7 @@
50088DA129E8271A008A1FDF /* WebSocket.swift in Sources */,
4C3EA64128FF553900C48A62 /* hash_u5.c in Sources */,
5C513FCC2984ACA60072348F /* QRCodeView.swift in Sources */,
4CC14FF52A740BB7007AEB17 /* NoteId.swift in Sources */,
4C19AE512A5CEF7C00C90DB7 /* NostrScript.swift in Sources */,
4C3EA64F28FF59F200C48A62 /* tal.c in Sources */,
4CB88393296F798300DC99E7 /* ReactionsModel.swift in Sources */,
Expand All @@ -2107,6 +2142,7 @@
3A4647CF2A413ADC00386AD8 /* CondensedProfilePicturesView.swift in Sources */,
D2277EEA2A089BD5006C3807 /* Router.swift in Sources */,
3A90B1812A4EA3AF00000D94 /* UserSearchCache.swift in Sources */,
4CC14FF92A741939007AEB17 /* Referenced.swift in Sources */,
4C5C7E6A284EDE2E00A22DF5 /* SearchResultsView.swift in Sources */,
4CE1399429F0669900AC6A0B /* BigButton.swift in Sources */,
7C60CAEF298471A1009C80D6 /* CoreSVG.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions damus/Components/ImageCarousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum ImageShape {
struct ImageCarousel: View {
var urls: [MediaUrl]

let evid: String
let evid: NoteId

let state: DamusState

Expand All @@ -72,7 +72,7 @@ struct ImageCarousel: View {
@State private var selectedIndex = 0
@State private var video_size: CGSize? = nil

init(state: DamusState, evid: String, urls: [MediaUrl]) {
init(state: DamusState, evid: NoteId, urls: [MediaUrl]) {
_open_sheet = State(initialValue: false)
_current_url = State(initialValue: nil)
let media_model = state.events.get_cache_data(evid).media_metadata_model
Expand Down Expand Up @@ -289,7 +289,7 @@ public struct ImageFill {
struct ImageCarousel_Previews: PreviewProvider {
static var previews: some View {
let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!)
ImageCarousel(state: test_damus_state(), evid: "evid", urls: [url, url])
ImageCarousel(state: test_damus_state(), evid: test_note.id, urls: [url, url])
}
}

4 changes: 2 additions & 2 deletions damus/Components/InvoiceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI

struct InvoiceView: View {
@Environment(\.colorScheme) var colorScheme
let our_pubkey: String
let our_pubkey: Pubkey
let invoice: Invoice
@State var showing_select_wallet: Bool = false
@State var copied = false
Expand Down Expand Up @@ -108,7 +108,7 @@ let test_invoice = Invoice(description: .description("this is a description"), a

struct InvoiceView_Previews: PreviewProvider {
static var previews: some View {
InvoiceView(our_pubkey: "", invoice: test_invoice, settings: test_damus_state().settings)
InvoiceView(our_pubkey: .empty, invoice: test_invoice, settings: test_damus_state().settings)
.frame(width: 300, height: 200)
}
}
Expand Down
4 changes: 2 additions & 2 deletions damus/Components/InvoicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct InvoicesView: View {
let our_pubkey: String
let our_pubkey: Pubkey
var invoices: [Invoice]
let settings: UserSettingsStore

Expand All @@ -29,7 +29,7 @@ struct InvoicesView: View {

struct InvoicesView_Previews: PreviewProvider {
static var previews: some View {
InvoicesView(our_pubkey: "", invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)], settings: test_damus_state().settings)
InvoicesView(our_pubkey: test_note.pubkey, invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)], settings: test_damus_state().settings)
.frame(width: 300)
}
}
8 changes: 4 additions & 4 deletions damus/Components/NIP05Badge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import SwiftUI

struct NIP05Badge: View {
let nip05: NIP05
let pubkey: String
let pubkey: Pubkey
let contacts: Contacts
let show_domain: Bool
let profiles: Profiles

@Environment(\.openURL) var openURL

init(nip05: NIP05, pubkey: String, contacts: Contacts, show_domain: Bool, profiles: Profiles) {
init(nip05: NIP05, pubkey: Pubkey, contacts: Contacts, show_domain: Bool, profiles: Profiles) {
self.nip05 = nip05
self.pubkey = pubkey
self.contacts = contacts
Expand Down Expand Up @@ -91,7 +91,7 @@ extension View {
}
}

func use_nip05_color(pubkey: String, contacts: Contacts) -> Bool {
func use_nip05_color(pubkey: Pubkey, contacts: Contacts) -> Bool {
return contacts.is_friend_or_self(pubkey) ? true : false
}

Expand All @@ -105,7 +105,7 @@ struct NIP05Badge_Previews: PreviewProvider {

NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: test_state.contacts, show_domain: true, profiles: test_state.profiles)

NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: Contacts(our_pubkey: "sdkfjsdf"), show_domain: true, profiles: test_state.profiles)
NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: Contacts(our_pubkey: test_pubkey), show_domain: true, profiles: test_state.profiles)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion damus/Components/Reposted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI

struct Reposted: View {
let damus: DamusState
let pubkey: String
let pubkey: Pubkey
let profile: Profile?

var body: some View {
Expand Down
29 changes: 16 additions & 13 deletions damus/Components/Search/SearchHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ struct SearchHeaderView: View {

func unfollow(_ hashtag: String) {
is_following = false
handle_unfollow(state: state, unfollow: .t(hashtag))
handle_unfollow(state: state, unfollow: FollowRef.hashtag(hashtag))
}

func follow(_ hashtag: String) {
is_following = true
handle_follow(state: state, follow: .t(hashtag))
handle_follow(state: state, follow: .hashtag(hashtag))
}

func FollowButton(_ ht: String) -> some View {
Expand Down Expand Up @@ -94,27 +94,30 @@ struct SearchHeaderView: View {
}
}
.onReceive(handle_notify(.followed)) { notif in
let ref = notif.object as! ReferencedId
guard hashtag_matches_search(desc: self.described, ref: ref) else { return }
let ref = notif.object as! FollowRef
guard case .hashtag(let follow_ht) = ref,
case .hashtag(let search_ht) = described,
follow_ht == search_ht else {
return
}

self.is_following = true
}
.onReceive(handle_notify(.unfollowed)) { notif in
let ref = notif.object as! ReferencedId
guard hashtag_matches_search(desc: self.described, ref: ref) else { return }
let ref = notif.object as! FollowRef
guard case .hashtag(let follow_ht) = ref,
case .hashtag(let search_ht) = described,
follow_ht == search_ht else {
return
}
self.is_following = false
}
}
}

func hashtag_matches_search(desc: DescribedSearch, ref: ReferencedId) -> Bool {
guard let ht = desc.is_hashtag, ref.key == "t" && ref.ref_id == ht
else { return false }
return true
}

func is_following_hashtag(contacts: NostrEvent?, hashtag: String) -> Bool {
guard let contacts else { return false }
return is_already_following(contacts: contacts, follow: .t(hashtag))
return is_already_following(contacts: contacts, follow: .hashtag(hashtag))
}


Expand Down
4 changes: 2 additions & 2 deletions damus/Components/TranslateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ extension View {
struct TranslateView_Previews: PreviewProvider {
static var previews: some View {
let ds = test_damus_state()
TranslateView(damus_state: ds, event: test_event, size: .normal)
TranslateView(damus_state: ds, event: test_note, size: .normal)
}
}

func translate_note(profiles: Profiles, privkey: String?, event: NostrEvent, settings: UserSettingsStore, note_lang: String) async -> TranslateStatus {
func translate_note(profiles: Profiles, privkey: Privkey?, event: NostrEvent, settings: UserSettingsStore, note_lang: String) async -> TranslateStatus {

// If the note language is different from our preferred languages, send a translation request.
let translator = Translator(settings)
Expand Down
10 changes: 5 additions & 5 deletions damus/Components/UserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import SwiftUI

struct UserViewRow: View {
let damus_state: DamusState
let pubkey: String
let pubkey: Pubkey

var body: some View {
UserView(damus_state: damus_state, pubkey: pubkey)
.contentShape(Rectangle())
Expand All @@ -20,12 +20,12 @@ struct UserViewRow: View {

struct UserView: View {
let damus_state: DamusState
let pubkey: String
let pubkey: Pubkey
let spacer: Bool

@State var about_text: Text? = nil

init(damus_state: DamusState, pubkey: String, spacer: Bool = true) {
init(damus_state: DamusState, pubkey: Pubkey, spacer: Bool = true) {
self.damus_state = damus_state
self.pubkey = pubkey
self.spacer = spacer
Expand Down Expand Up @@ -56,6 +56,6 @@ struct UserView: View {

struct UserView_Previews: PreviewProvider {
static var previews: some View {
UserView(damus_state: test_damus_state(), pubkey: "pk")
UserView(damus_state: test_damus_state(), pubkey: test_note.pubkey)
}
}
4 changes: 2 additions & 2 deletions damus/Components/ZapButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ struct ZapButton: View {

struct ZapButton_Previews: PreviewProvider {
static var previews: some View {
let pending_zap = PendingZap(amount_msat: 1000, target: ZapTarget.note(id: "noteid", author: "author"), request: .normal(test_zap_request), type: .pub, state: .external(.init(state: .fetching_invoice)))
let pending_zap = PendingZap(amount_msat: 1000, target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), request: .normal(test_zap_request), type: .pub, state: .external(.init(state: .fetching_invoice)))
let zaps = ZapsDataModel([.pending(pending_zap)])

ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_event.id, author: test_event.pubkey), lnurl: "lnurl", zaps: zaps)
ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), lnurl: "lnurl", zaps: zaps)
}
}

Expand Down
Loading

0 comments on commit 3ad0d4e

Please sign in to comment.