Skip to content

Commit

Permalink
instant streaming of metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Nov 19, 2024
1 parent 925ab41 commit 5358d11
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/data_layer/repositories/metadata_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,42 @@ class MetadataRepositoryImpl implements MetadataRepository {

/// nip05 is automatically verified if present and removed if invalid
@override
Stream<UserMetadata> getMetadataByPubkey(String pubkey) {
Stream<UserMetadata> getMetadataByPubkey(String pubkey) async* {
final myMetadata = dartNdkSource.dartNdk.metadata.loadMetadata(pubkey);

final Stream<ndk_entities.Metadata?> myMetadataStream =
myMetadata.asStream();

return myMetadataStream.where((event) => event != null).asyncMap(
(event) async {
final metadata = UserMetadataModel.fromNDKMetadata(event!);
await for (final event in myMetadataStream) {
if (event != null) {
final metadata = UserMetadataModel.fromNDKMetadata(event);

// Check if there's a NIP-05 identifier
if (metadata.nip05 != null && metadata.nip05!.isNotEmpty) {
// Store the original NIP-05 value
final originalNip05 = metadata.nip05;

// Set NIP-05 to null for the first event
metadata.nip05 = null;

// Emit the first event immediately with NIP-05 set to null
yield metadata;

// If there was originally a NIP-05 identifier, perform verification
if (originalNip05 != null && originalNip05.isNotEmpty) {
// Perform NIP-05 verification
final nip05Result = await dartNdkSource.dartNdk.nip05
.check(nip05: metadata.nip05!, pubkey: pubkey);
.check(nip05: originalNip05, pubkey: pubkey);

// If verification fails, set NIP-05 to null
if (!nip05Result.valid) {
metadata.nip05 = null;
return metadata;
// If verification succeeds, set NIP-05 to the original value
if (nip05Result.valid) {
metadata.nip05 = originalNip05;
// Emit the second event with verified NIP-05
yield metadata;
}
// If verification fails, we don't emit another event
// as the NIP-05 is already null from the first event
}

return metadata;
},
);
}
}
}

@override
Expand Down

0 comments on commit 5358d11

Please sign in to comment.