Skip to content

Commit 1f3d846

Browse files
committed
wip local relay model
Signed-off-by: William Casarin <[email protected]>
1 parent 824d0f5 commit 1f3d846

File tree

10 files changed

+114
-84
lines changed

10 files changed

+114
-84
lines changed

damus.xcodeproj/project.pbxproj

+9-39
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
206206
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
207207
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
208+
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
209+
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
208210
4C7D09592A05BEAD00943473 /* KeyboardVisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */; };
209211
4C7D095F2A098C5D00943473 /* ConnectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */; };
210212
4C7D09602A098C5D00943473 /* WalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095D2A098C5D00943473 /* WalletView.swift */; };
@@ -590,7 +592,6 @@
590592
D7CBD1D62B8D509800BFD889 /* DamusPurpleImpendingExpirationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CBD1D52B8D509800BFD889 /* DamusPurpleImpendingExpirationTests.swift */; };
591593
D7CCFC072B05833200323D86 /* NdbNote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C90548A2A6AEDEE00811EEC /* NdbNote.swift */; };
592594
D7CCFC082B05834500323D86 /* NoteId.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FF42A740BB7007AEB17 /* NoteId.swift */; };
593-
D7CCFC0B2B0585EA00323D86 /* nostrdb.c in Sources */ = {isa = PBXBuildFile; fileRef = 4CE9FBB82A6B3B26007E485C /* nostrdb.c */; settings = {COMPILER_FLAGS = "-w"; }; };
594595
D7CCFC0F2B0587F600323D86 /* Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C285C8B28398BC6008A31F1 /* Keys.swift */; };
595596
D7CCFC102B05880F00323D86 /* Id.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C2B7BF12A71B6540049DEE7 /* Id.swift */; };
596597
D7CCFC112B05884E00323D86 /* AsciiCharacter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C5D5C9C2A6B2CB40024563C /* AsciiCharacter.swift */; };
@@ -1092,6 +1093,7 @@
10921093
4C78EFD82A707C4D007E8197 /* secp256k1_ecdh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_ecdh.h; sourceTree = "<group>"; };
10931094
4C78EFD92A707C4D007E8197 /* secp256k1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1.h; sourceTree = "<group>"; };
10941095
4C78EFDA2A707C67007E8197 /* secp256k1_extrakeys.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_extrakeys.h; sourceTree = "<group>"; };
1096+
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryResult.swift; sourceTree = "<group>"; };
10951097
4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardVisible.swift; sourceTree = "<group>"; };
10961098
4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectWalletView.swift; sourceTree = "<group>"; };
10971099
4C7D095D2A098C5D00943473 /* WalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletView.swift; sourceTree = "<group>"; };
@@ -1157,6 +1159,7 @@
11571159
4CAAD8AF29888AD200060CEA /* RelayConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayConfigView.swift; sourceTree = "<group>"; };
11581160
4CACA9D4280C31E100D9BBE8 /* ReplyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyView.swift; sourceTree = "<group>"; };
11591161
4CACA9DB280C38C000D9BBE8 /* Profiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profiles.swift; sourceTree = "<group>"; };
1162+
4CB412802C80753E008DE044 /* Untitled.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Untitled.swift; sourceTree = "<group>"; };
11601163
4CB55EF4295E679D007FD187 /* UserRelaysView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRelaysView.swift; sourceTree = "<group>"; };
11611164
4CB8838529656C8B00DC99E7 /* NIP05.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NIP05.swift; sourceTree = "<group>"; };
11621165
4CB88388296AF99A00DC99E7 /* EventDetailBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDetailBar.swift; sourceTree = "<group>"; };
@@ -1769,40 +1772,6 @@
17691772
path = NIP10;
17701773
sourceTree = "<group>";
17711774
};
1772-
4C478E2A2A9935D300489948 /* bindings */ = {
1773-
isa = PBXGroup;
1774-
children = (
1775-
4C478E2B2A9935D300489948 /* swift */,
1776-
4C478E2D2A9935D300489948 /* c */,
1777-
);
1778-
path = bindings;
1779-
sourceTree = "<group>";
1780-
};
1781-
4C478E2B2A9935D300489948 /* swift */ = {
1782-
isa = PBXGroup;
1783-
children = (
1784-
4C478E2C2A9935D300489948 /* NdbProfile.swift */,
1785-
);
1786-
path = swift;
1787-
sourceTree = "<group>";
1788-
};
1789-
4C478E2D2A9935D300489948 /* c */ = {
1790-
isa = PBXGroup;
1791-
children = (
1792-
4C478E2E2A9935D300489948 /* profile_json_parser.h */,
1793-
4C478E2F2A9935D300489948 /* profile_reader.h */,
1794-
4C478E302A9935D300489948 /* meta_json_parser.h */,
1795-
4C478E312A9935D300489948 /* profile_builder.h */,
1796-
4C478E322A9935D300489948 /* meta_builder.h */,
1797-
4C478E332A9935D300489948 /* profile_verifier.h */,
1798-
4C478E352A9935D300489948 /* meta_reader.h */,
1799-
4C478E362A9935D300489948 /* flatbuffers_common_reader.h */,
1800-
4C478E372A9935D300489948 /* meta_verifier.h */,
1801-
4C478E382A9935D300489948 /* flatbuffers_common_builder.h */,
1802-
);
1803-
path = c;
1804-
sourceTree = "<group>";
1805-
};
18061775
4C47928D2A9939BD00489948 /* flatcc */ = {
18071776
isa = PBXGroup;
18081777
children = (
@@ -2186,6 +2155,7 @@
21862155
4C9054862A6AEB4500811EEC /* nostrdb */ = {
21872156
isa = PBXGroup;
21882157
children = (
2158+
4CB412802C80753E008DE044 /* Untitled.swift */,
21892159
4C15224A2B8D499F007CDC17 /* parser.h */,
21902160
4CF47FDC2B631C0100F2B2C0 /* src */,
21912161
4C47928D2A9939BD00489948 /* flatcc */,
@@ -2208,6 +2178,7 @@
22082178
D798D2272B085CDA00234419 /* NdbNote+.swift */,
22092179
4CF480562B633F2600F2B2C0 /* NdbBlocksIterator.swift */,
22102180
4CF480582B633F3800F2B2C0 /* NdbBlock.swift */,
2181+
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */,
22112182
);
22122183
path = nostrdb;
22132184
sourceTree = "<group>";
@@ -3143,7 +3114,6 @@
31433114
files = (
31443115
4C1D4FB42A7967990024F453 /* build-git-hash.txt in Resources */,
31453116
D7FB14222BE5970000398331 /* PrivacyInfo.xcprivacy in Resources */,
3146-
4CF480432B631C0100F2B2C0 /* .dir in Resources */,
31473117
3ACB685F297633BC00C46468 /* Localizable.strings in Resources */,
31483118
4CE6DEEE27F7A08200C66700 /* Preview Assets.xcassets in Resources */,
31493119
3ACB685C297633BC00C46468 /* InfoPlist.strings in Resources */,
@@ -3300,7 +3270,6 @@
33003270
4CB883B6297730E400DC99E7 /* LNUrls.swift in Sources */,
33013271
4C7FF7D52823313F009601DB /* Mentions.swift in Sources */,
33023272
4CF4803D2B631C0100F2B2C0 /* content_parser.c in Sources */,
3303-
BA4AB0AE2A63B9270070A32A /* AddEmojiView.swift in Sources */,
33043273
4C32B94D2A9AD44700DC3548 /* Offset.swift in Sources */,
33053274
4C633350283D40E500B1C9C3 /* HomeModel.swift in Sources */,
33063275
4C987B57283FD07F0042CE38 /* FollowersModel.swift in Sources */,
@@ -3482,6 +3451,7 @@
34823451
4C363A8828236948006E126D /* BlocksView.swift in Sources */,
34833452
4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */,
34843453
3A23838E2A297DD200E5AA2E /* ZapButtonModel.swift in Sources */,
3454+
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
34853455
F71694F82A6983AF001F4053 /* GrayGradient.swift in Sources */,
34863456
4C1D4FB12A7958E60024F453 /* VersionInfo.swift in Sources */,
34873457
D7FF94002AC7AC5300FD969D /* RelayURL.swift in Sources */,
@@ -3497,7 +3467,6 @@
34973467
5CC8529F2BD744F60039FFC5 /* HighlightView.swift in Sources */,
34983468
BA37598D2ABCCE500018D73B /* PhotoCaptureProcessor.swift in Sources */,
34993469
5CC8529D2BD741CD0039FFC5 /* HighlightEvent.swift in Sources */,
3500-
4C9146FD2A2A87C200DDEA40 /* wasm.c in Sources */,
35013470
4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */,
35023471
4CF480422B631C0100F2B2C0 /* NdbProfile.swift in Sources */,
35033472
4CA9276C2A2910D10098A105 /* ReplyPart.swift in Sources */,
@@ -3522,7 +3491,6 @@
35223491
4C19AE512A5CEF7C00C90DB7 /* NostrScript.swift in Sources */,
35233492
4C32B95E2A9AD44700DC3548 /* FlatBufferObject.swift in Sources */,
35243493
D783A63F2AD4E53D00658DDA /* SuggestedHashtagsView.swift in Sources */,
3525-
4CB88393296F798300DC99E7 /* ReactionsModel.swift in Sources */,
35263494
5C42E78C29DB76D90086AAC1 /* EmptyUserSearchView.swift in Sources */,
35273495
4CB88396296F7F8B00DC99E7 /* ReactionView.swift in Sources */,
35283496
4CF480552B631C4F00F2B2C0 /* wasm.c in Sources */,
@@ -3860,6 +3828,7 @@
38603828
D798D2222B08598A00234419 /* ReferencedId.swift in Sources */,
38613829
D7CE1B492B0BE729002EDAD4 /* DisplayName.swift in Sources */,
38623830
D7CE1B192B0BE132002EDAD4 /* builder.c in Sources */,
3831+
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
38633832
D7EDED1F2B11797D0018B19C /* LongformEvent.swift in Sources */,
38643833
D7CCFC122B05886D00323D86 /* IdType.swift in Sources */,
38653834
D7CE1B312B0BE69D002EDAD4 /* Ndb.swift in Sources */,
@@ -4173,6 +4142,7 @@
41734142
DEVELOPMENT_TEAM = XK7H4JAB3D;
41744143
ENABLE_PREVIEWS = YES;
41754144
GENERATE_INFOPLIST_FILE = YES;
4145+
"HEADER_SEARCH_PATHS[arch=*]" = nostrdb/;
41764146
INFOPLIST_FILE = damus/Info.plist;
41774147
INFOPLIST_KEY_CFBundleDisplayName = Damus;
41784148
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";

damus/ContentParsing.swift

+31-15
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,61 @@ enum NoteContent {
2020
}
2121
}
2222

23-
func parsed_blocks_finish(bs: inout note_blocks, tags: TagsSequence?) -> Blocks {
23+
// TODO: just make a blocks iterator over the compact data instead of using Blocks
24+
func parsed_blocks_finish(bs: inout ndb_blocks, tags: TagsSequence?) -> Blocks {
2425
var out: [Block] = []
2526

2627
var i = 0
2728
while (i < bs.num_blocks) {
2829
let block = bs.blocks[i]
2930

30-
if let converted = Block(block, tags: tags) {
31+
if let converted = Block(block: block, tags: tags) {
3132
out.append(converted)
3233
}
3334

3435
i += 1
3536
}
3637

3738
let words = Int(bs.words)
38-
blocks_free(&bs)
3939

4040
return Blocks(words: words, blocks: out)
4141

4242
}
4343

44-
func parse_note_content(content: NoteContent) -> Blocks {
45-
var bs = note_blocks()
46-
bs.num_blocks = 0;
47-
48-
blocks_init(&bs)
44+
45+
func parse_note_content(content: NoteContent) -> Blocks? {
46+
// Step 1: Prepare the data you need to pass to the C function.
47+
var buffer = [UInt8](repeating: 0, count: 1024*1024) // Example buffer, replace size with what you need
48+
let buf_size = Int32(buffer.count)
49+
var ptr: OpaquePointer? = nil // Pointer for the result
4950

5051
switch content {
51-
case .content(let s, let tags):
52-
return s.withCString { cptr in
53-
damus_parse_content(&bs, cptr)
54-
return parsed_blocks_finish(bs: &bs, tags: tags)
52+
case .note(let nostrEvent):
53+
let len = Int32(nostrEvent.content_len)
54+
let r = ndb_parse_content(&buffer, buf_size, nostrEvent.content_raw, len, &ptr)
55+
56+
if r != 0 {
57+
let nil_tags: TagsSequence? = nil
58+
let size = ndb_blocks_total_size(ptr)
59+
let resized = buffer[0:size]
60+
return Blocks.init(buffer: buffer[0:], blocks: <#T##NdbBlocks#>)
61+
}
62+
63+
case .content(let s, let tagsSequence):
64+
let content_len = Int32(s.utf8.count)
65+
let res = s.withCString { cptr in
66+
ndb_parse_content(&buffer, buf_size, cptr, content_len, &blocks)
67+
}
68+
69+
if res != 0 {
70+
return parsed_blocks_finish(bs: blocks, tags: tagsSequence)
71+
} else {
72+
return nil
5573
}
56-
case .note(let note):
57-
damus_parse_content(&bs, note.content_raw)
58-
return parsed_blocks_finish(bs: &bs, tags: note.tags)
5974
}
6075
}
6176

77+
6278
func interpret_event_refs(tags: TagsSequence) -> ThreadReply? {
6379
// migration is long over, lets just do this to fix tests
6480
return interpret_event_refs_ndb(tags: tags)

damus/ContentView.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -606,22 +606,25 @@ struct ContentView: View {
606606
self.selected_timeline = timeline
607607
}
608608

609+
func on_local_sub(subid: UInt64) {
610+
}
611+
609612
func connect() {
610613
// nostrdb
611-
var mndb = Ndb()
612-
if mndb == nil {
614+
var ndb: Ndb? = Ndb()
615+
if ndb?.open() == nil {
613616
// try recovery
614617
print("DB ISSUE! RECOVERING")
615-
mndb = Ndb.safemode()
618+
ndb = Ndb.safemode()
616619

617620
// out of space or something?? maybe we need a in-memory fallback
618-
if mndb == nil {
621+
if ndb == nil {
619622
logout(nil)
620623
return
621624
}
622625
}
623626

624-
guard let ndb = mndb else { return }
627+
guard let ndb else { return }
625628

626629
let pool = RelayPool(ndb: ndb, keypair: keypair)
627630
let model_cache = RelayModelCache()

damus/TestData.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ var test_damus_state: DamusState = ({
7979
}
8080

8181
print("opening \(tempDir!)")
82-
let ndb = Ndb(path: tempDir)!
82+
let ndb = Ndb(path: tempDir)
83+
let _ = ndb.open()!
8384
let our_pubkey = test_pubkey
8485
let pool = RelayPool(ndb: ndb)
8586
let settings = UserSettingsStore()

damus/Types/Block.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ enum Block: Equatable {
3535
case relay(String)
3636
}
3737

38-
struct Blocks: Equatable {
39-
let words: Int
40-
let blocks: [Block]
38+
struct Blocks {
39+
let buffer: [UInt8]
40+
let blocks: NdbBlocks
4141
}
4242

4343
extension ndb_str_block {

damus/Util/Router.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ enum Route: Hashable {
112112
case .CreateAccount:
113113
CreateAccountView(nav: navigationCoordinator)
114114
case .SaveKeys(let account):
115-
SaveKeysView(account: account)
115+
SaveKeysView(account: account, pool: damusState.pool)
116116
case .Wallet(let walletModel):
117117
WalletView(damus_state: damusState, model: walletModel)
118118
case .WalletScanner(let walletScanResult):

damus/Views/SaveKeysView.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import Security
1111
struct SaveKeysView: View {
1212
let account: CreateAccountModel
1313
let pool: RelayPool = RelayPool(ndb: Ndb()!)
14+
15+
@State var pub_copied: Bool = false
16+
@State var priv_copied: Bool = false
1417
@State var loading: Bool = false
1518
@State var error: String? = nil
1619

@@ -21,7 +24,8 @@ struct SaveKeysView: View {
2124

2225
let first_contact_event: NdbNote?
2326

24-
init(account: CreateAccountModel) {
27+
init(account: CreateAccountModel, pool: RelayPool) {
28+
self.pool = pool
2529
self.account = account
2630
self.first_contact_event = make_first_contact_event(keypair: account.keypair)
2731
}
@@ -203,8 +207,8 @@ struct SaveKeysView: View {
203207

204208
struct SaveKeysView_Previews: PreviewProvider {
205209
static var previews: some View {
206-
let model = CreateAccountModel(display_name: "William", name: "jb55", about: "I'm me")
207-
SaveKeysView(account: model)
210+
let model = CreateAccountModel(display_name: "William", nick: "jb55", about: "I'm me")
211+
SaveKeysView(account: model, pool: RelayPool(ndb: test_damus_state.ndb))
208212
}
209213
}
210214

0 commit comments

Comments
 (0)