Skip to content

Commit

Permalink
wip local relay model
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 29, 2024
1 parent 824d0f5 commit 1f3d846
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 84 deletions.
48 changes: 9 additions & 39 deletions damus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
4C7D09592A05BEAD00943473 /* KeyboardVisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */; };
4C7D095F2A098C5D00943473 /* ConnectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */; };
4C7D09602A098C5D00943473 /* WalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095D2A098C5D00943473 /* WalletView.swift */; };
Expand Down Expand Up @@ -590,7 +592,6 @@
D7CBD1D62B8D509800BFD889 /* DamusPurpleImpendingExpirationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CBD1D52B8D509800BFD889 /* DamusPurpleImpendingExpirationTests.swift */; };
D7CCFC072B05833200323D86 /* NdbNote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C90548A2A6AEDEE00811EEC /* NdbNote.swift */; };
D7CCFC082B05834500323D86 /* NoteId.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC14FF42A740BB7007AEB17 /* NoteId.swift */; };
D7CCFC0B2B0585EA00323D86 /* nostrdb.c in Sources */ = {isa = PBXBuildFile; fileRef = 4CE9FBB82A6B3B26007E485C /* nostrdb.c */; settings = {COMPILER_FLAGS = "-w"; }; };
D7CCFC0F2B0587F600323D86 /* Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C285C8B28398BC6008A31F1 /* Keys.swift */; };
D7CCFC102B05880F00323D86 /* Id.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C2B7BF12A71B6540049DEE7 /* Id.swift */; };
D7CCFC112B05884E00323D86 /* AsciiCharacter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C5D5C9C2A6B2CB40024563C /* AsciiCharacter.swift */; };
Expand Down Expand Up @@ -1092,6 +1093,7 @@
4C78EFD82A707C4D007E8197 /* secp256k1_ecdh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_ecdh.h; sourceTree = "<group>"; };
4C78EFD92A707C4D007E8197 /* secp256k1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1.h; sourceTree = "<group>"; };
4C78EFDA2A707C67007E8197 /* secp256k1_extrakeys.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_extrakeys.h; sourceTree = "<group>"; };
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryResult.swift; sourceTree = "<group>"; };
4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardVisible.swift; sourceTree = "<group>"; };
4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectWalletView.swift; sourceTree = "<group>"; };
4C7D095D2A098C5D00943473 /* WalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1157,6 +1159,7 @@
4CAAD8AF29888AD200060CEA /* RelayConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayConfigView.swift; sourceTree = "<group>"; };
4CACA9D4280C31E100D9BBE8 /* ReplyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyView.swift; sourceTree = "<group>"; };
4CACA9DB280C38C000D9BBE8 /* Profiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profiles.swift; sourceTree = "<group>"; };
4CB412802C80753E008DE044 /* Untitled.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Untitled.swift; sourceTree = "<group>"; };
4CB55EF4295E679D007FD187 /* UserRelaysView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRelaysView.swift; sourceTree = "<group>"; };
4CB8838529656C8B00DC99E7 /* NIP05.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NIP05.swift; sourceTree = "<group>"; };
4CB88388296AF99A00DC99E7 /* EventDetailBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDetailBar.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1769,40 +1772,6 @@
path = NIP10;
sourceTree = "<group>";
};
4C478E2A2A9935D300489948 /* bindings */ = {
isa = PBXGroup;
children = (
4C478E2B2A9935D300489948 /* swift */,
4C478E2D2A9935D300489948 /* c */,
);
path = bindings;
sourceTree = "<group>";
};
4C478E2B2A9935D300489948 /* swift */ = {
isa = PBXGroup;
children = (
4C478E2C2A9935D300489948 /* NdbProfile.swift */,
);
path = swift;
sourceTree = "<group>";
};
4C478E2D2A9935D300489948 /* c */ = {
isa = PBXGroup;
children = (
4C478E2E2A9935D300489948 /* profile_json_parser.h */,
4C478E2F2A9935D300489948 /* profile_reader.h */,
4C478E302A9935D300489948 /* meta_json_parser.h */,
4C478E312A9935D300489948 /* profile_builder.h */,
4C478E322A9935D300489948 /* meta_builder.h */,
4C478E332A9935D300489948 /* profile_verifier.h */,
4C478E352A9935D300489948 /* meta_reader.h */,
4C478E362A9935D300489948 /* flatbuffers_common_reader.h */,
4C478E372A9935D300489948 /* meta_verifier.h */,
4C478E382A9935D300489948 /* flatbuffers_common_builder.h */,
);
path = c;
sourceTree = "<group>";
};
4C47928D2A9939BD00489948 /* flatcc */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2186,6 +2155,7 @@
4C9054862A6AEB4500811EEC /* nostrdb */ = {
isa = PBXGroup;
children = (
4CB412802C80753E008DE044 /* Untitled.swift */,
4C15224A2B8D499F007CDC17 /* parser.h */,
4CF47FDC2B631C0100F2B2C0 /* src */,
4C47928D2A9939BD00489948 /* flatcc */,
Expand All @@ -2208,6 +2178,7 @@
D798D2272B085CDA00234419 /* NdbNote+.swift */,
4CF480562B633F2600F2B2C0 /* NdbBlocksIterator.swift */,
4CF480582B633F3800F2B2C0 /* NdbBlock.swift */,
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */,
);
path = nostrdb;
sourceTree = "<group>";
Expand Down Expand Up @@ -3143,7 +3114,6 @@
files = (
4C1D4FB42A7967990024F453 /* build-git-hash.txt in Resources */,
D7FB14222BE5970000398331 /* PrivacyInfo.xcprivacy in Resources */,
4CF480432B631C0100F2B2C0 /* .dir in Resources */,
3ACB685F297633BC00C46468 /* Localizable.strings in Resources */,
4CE6DEEE27F7A08200C66700 /* Preview Assets.xcassets in Resources */,
3ACB685C297633BC00C46468 /* InfoPlist.strings in Resources */,
Expand Down Expand Up @@ -3300,7 +3270,6 @@
4CB883B6297730E400DC99E7 /* LNUrls.swift in Sources */,
4C7FF7D52823313F009601DB /* Mentions.swift in Sources */,
4CF4803D2B631C0100F2B2C0 /* content_parser.c in Sources */,
BA4AB0AE2A63B9270070A32A /* AddEmojiView.swift in Sources */,
4C32B94D2A9AD44700DC3548 /* Offset.swift in Sources */,
4C633350283D40E500B1C9C3 /* HomeModel.swift in Sources */,
4C987B57283FD07F0042CE38 /* FollowersModel.swift in Sources */,
Expand Down Expand Up @@ -3482,6 +3451,7 @@
4C363A8828236948006E126D /* BlocksView.swift in Sources */,
4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */,
3A23838E2A297DD200E5AA2E /* ZapButtonModel.swift in Sources */,
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
F71694F82A6983AF001F4053 /* GrayGradient.swift in Sources */,
4C1D4FB12A7958E60024F453 /* VersionInfo.swift in Sources */,
D7FF94002AC7AC5300FD969D /* RelayURL.swift in Sources */,
Expand All @@ -3497,7 +3467,6 @@
5CC8529F2BD744F60039FFC5 /* HighlightView.swift in Sources */,
BA37598D2ABCCE500018D73B /* PhotoCaptureProcessor.swift in Sources */,
5CC8529D2BD741CD0039FFC5 /* HighlightEvent.swift in Sources */,
4C9146FD2A2A87C200DDEA40 /* wasm.c in Sources */,
4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */,
4CF480422B631C0100F2B2C0 /* NdbProfile.swift in Sources */,
4CA9276C2A2910D10098A105 /* ReplyPart.swift in Sources */,
Expand All @@ -3522,7 +3491,6 @@
4C19AE512A5CEF7C00C90DB7 /* NostrScript.swift in Sources */,
4C32B95E2A9AD44700DC3548 /* FlatBufferObject.swift in Sources */,
D783A63F2AD4E53D00658DDA /* SuggestedHashtagsView.swift in Sources */,
4CB88393296F798300DC99E7 /* ReactionsModel.swift in Sources */,
5C42E78C29DB76D90086AAC1 /* EmptyUserSearchView.swift in Sources */,
4CB88396296F7F8B00DC99E7 /* ReactionView.swift in Sources */,
4CF480552B631C4F00F2B2C0 /* wasm.c in Sources */,
Expand Down Expand Up @@ -3860,6 +3828,7 @@
D798D2222B08598A00234419 /* ReferencedId.swift in Sources */,
D7CE1B492B0BE729002EDAD4 /* DisplayName.swift in Sources */,
D7CE1B192B0BE132002EDAD4 /* builder.c in Sources */,
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
D7EDED1F2B11797D0018B19C /* LongformEvent.swift in Sources */,
D7CCFC122B05886D00323D86 /* IdType.swift in Sources */,
D7CE1B312B0BE69D002EDAD4 /* Ndb.swift in Sources */,
Expand Down Expand Up @@ -4173,6 +4142,7 @@
DEVELOPMENT_TEAM = XK7H4JAB3D;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
"HEADER_SEARCH_PATHS[arch=*]" = nostrdb/;
INFOPLIST_FILE = damus/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Damus;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
Expand Down
46 changes: 31 additions & 15 deletions damus/ContentParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,61 @@ enum NoteContent {
}
}

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

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

if let converted = Block(block, tags: tags) {
if let converted = Block(block: block, tags: tags) {
out.append(converted)
}

i += 1
}

let words = Int(bs.words)
blocks_free(&bs)

return Blocks(words: words, blocks: out)

}

func parse_note_content(content: NoteContent) -> Blocks {
var bs = note_blocks()
bs.num_blocks = 0;

blocks_init(&bs)

func parse_note_content(content: NoteContent) -> Blocks? {
// Step 1: Prepare the data you need to pass to the C function.
var buffer = [UInt8](repeating: 0, count: 1024*1024) // Example buffer, replace size with what you need
let buf_size = Int32(buffer.count)
var ptr: OpaquePointer? = nil // Pointer for the result

switch content {
case .content(let s, let tags):
return s.withCString { cptr in
damus_parse_content(&bs, cptr)
return parsed_blocks_finish(bs: &bs, tags: tags)
case .note(let nostrEvent):
let len = Int32(nostrEvent.content_len)
let r = ndb_parse_content(&buffer, buf_size, nostrEvent.content_raw, len, &ptr)

if r != 0 {
let nil_tags: TagsSequence? = nil
let size = ndb_blocks_total_size(ptr)
let resized = buffer[0:size]
return Blocks.init(buffer: buffer[0:], blocks: <#T##NdbBlocks#>)
}

case .content(let s, let tagsSequence):
let content_len = Int32(s.utf8.count)
let res = s.withCString { cptr in
ndb_parse_content(&buffer, buf_size, cptr, content_len, &blocks)
}

if res != 0 {
return parsed_blocks_finish(bs: blocks, tags: tagsSequence)
} else {
return nil
}
case .note(let note):
damus_parse_content(&bs, note.content_raw)
return parsed_blocks_finish(bs: &bs, tags: note.tags)
}
}


func interpret_event_refs(tags: TagsSequence) -> ThreadReply? {
// migration is long over, lets just do this to fix tests
return interpret_event_refs_ndb(tags: tags)
Expand Down
13 changes: 8 additions & 5 deletions damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,22 +606,25 @@ struct ContentView: View {
self.selected_timeline = timeline
}

func on_local_sub(subid: UInt64) {
}

func connect() {
// nostrdb
var mndb = Ndb()
if mndb == nil {
var ndb: Ndb? = Ndb()
if ndb?.open() == nil {
// try recovery
print("DB ISSUE! RECOVERING")
mndb = Ndb.safemode()
ndb = Ndb.safemode()

// out of space or something?? maybe we need a in-memory fallback
if mndb == nil {
if ndb == nil {
logout(nil)
return
}
}

guard let ndb = mndb else { return }
guard let ndb else { return }

let pool = RelayPool(ndb: ndb, keypair: keypair)
let model_cache = RelayModelCache()
Expand Down
3 changes: 2 additions & 1 deletion damus/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ var test_damus_state: DamusState = ({
}

print("opening \(tempDir!)")
let ndb = Ndb(path: tempDir)!
let ndb = Ndb(path: tempDir)
let _ = ndb.open()!
let our_pubkey = test_pubkey
let pool = RelayPool(ndb: ndb)
let settings = UserSettingsStore()
Expand Down
6 changes: 3 additions & 3 deletions damus/Types/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ enum Block: Equatable {
case relay(String)
}

struct Blocks: Equatable {
let words: Int
let blocks: [Block]
struct Blocks {
let buffer: [UInt8]
let blocks: NdbBlocks
}

extension ndb_str_block {
Expand Down
2 changes: 1 addition & 1 deletion damus/Util/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ enum Route: Hashable {
case .CreateAccount:
CreateAccountView(nav: navigationCoordinator)
case .SaveKeys(let account):
SaveKeysView(account: account)
SaveKeysView(account: account, pool: damusState.pool)
case .Wallet(let walletModel):
WalletView(damus_state: damusState, model: walletModel)
case .WalletScanner(let walletScanResult):
Expand Down
10 changes: 7 additions & 3 deletions damus/Views/SaveKeysView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import Security
struct SaveKeysView: View {
let account: CreateAccountModel
let pool: RelayPool = RelayPool(ndb: Ndb()!)

@State var pub_copied: Bool = false
@State var priv_copied: Bool = false
@State var loading: Bool = false
@State var error: String? = nil

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

let first_contact_event: NdbNote?

init(account: CreateAccountModel) {
init(account: CreateAccountModel, pool: RelayPool) {
self.pool = pool
self.account = account
self.first_contact_event = make_first_contact_event(keypair: account.keypair)
}
Expand Down Expand Up @@ -203,8 +207,8 @@ struct SaveKeysView: View {

struct SaveKeysView_Previews: PreviewProvider {
static var previews: some View {
let model = CreateAccountModel(display_name: "William", name: "jb55", about: "I'm me")
SaveKeysView(account: model)
let model = CreateAccountModel(display_name: "William", nick: "jb55", about: "I'm me")
SaveKeysView(account: model, pool: RelayPool(ndb: test_damus_state.ndb))
}
}

Expand Down
Loading

0 comments on commit 1f3d846

Please sign in to comment.