Skip to content

Commit

Permalink
Add SendEventOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 23, 2024
1 parent 6305585 commit b6fe16d
Show file tree
Hide file tree
Showing 10 changed files with 447 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* Expose `UnsignedEvent` ([Yuki Kishimoto])
* Expose `EventBuilder` ([Yuki Kishimoto])
* Expose `Client::send_event_builder` ([Yuki Kishimoto])
* Add `SendEventOutput` ([Yuki Kishimoto])

### Fixed

Expand Down
1 change: 1 addition & 0 deletions lib/nostr_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export 'src/rust/api/protocol/signer.dart';
export 'src/rust/api/relay/options.dart';
export 'src/rust/api/client.dart';
export 'src/rust/api/client/options.dart';
export 'src/rust/api/client/output.dart';
5 changes: 3 additions & 2 deletions lib/src/rust/api/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import '../frb_generated.dart';
import 'client/builder.dart';
import 'client/output.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'protocol/event.dart';
import 'protocol/event/builder.dart';
Expand Down Expand Up @@ -37,12 +38,12 @@ abstract class Client implements RustOpaqueInterface {
///
/// Send `Event` to all relays with `WRITE` flag.
/// If `gossip` option is enabled, the event will be sent also to NIP65 relays (automatically discovered).
Future<String> sendEvent({required Event event});
Future<SendEventOutput> sendEvent({required Event event});

/// Send event
///
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to relays (check [`Client::send_event`] from more details).
///
/// Return an error if the [`NostrSigner`] is not set.
Future<String> sendEventBuilder({required EventBuilder builder});
Future<SendEventOutput> sendEventBuilder({required EventBuilder builder});
}
39 changes: 39 additions & 0 deletions lib/src/rust/api/client/output.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.0.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `from`

/// Output
class SendEventOutput {
/// Event ID
final String id;

/// Set of relays that success
final Set<String> success;

/// Map of relays that failed, with related errors.
final Map<String, String> failed;

const SendEventOutput({
required this.id,
required this.success,
required this.failed,
});

@override
int get hashCode => id.hashCode ^ success.hashCode ^ failed.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is SendEventOutput &&
runtimeType == other.runtimeType &&
id == other.id &&
success == other.success &&
failed == other.failed;
}
153 changes: 144 additions & 9 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import 'api/client.dart';
import 'api/client/builder.dart';
import 'api/client/options.dart';
import 'api/client/output.dart';
import 'api/protocol/event.dart';
import 'api/protocol/event/builder.dart';
import 'api/protocol/event/tag.dart';
Expand Down Expand Up @@ -92,10 +93,10 @@ abstract class NostrSdkApi extends BaseApi {

Client crateApiClientClientNew();

Future<String> crateApiClientClientSendEvent(
Future<SendEventOutput> crateApiClientClientSendEvent(
{required Client that, required Event event});

Future<String> crateApiClientClientSendEventBuilder(
Future<SendEventOutput> crateApiClientClientSendEventBuilder(
{required Client that, required EventBuilder builder});

Client crateApiClientBuilderClientBuilderBuild({required ClientBuilder that});
Expand Down Expand Up @@ -539,7 +540,7 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
);

@override
Future<String> crateApiClientClientSendEvent(
Future<SendEventOutput> crateApiClientClientSendEvent(
{required Client that, required Event event}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
Expand All @@ -552,7 +553,7 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
funcId: 6, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
decodeSuccessData: sse_decode_send_event_output,
decodeErrorData: sse_decode_AnyhowException,
),
constMeta: kCrateApiClientClientSendEventConstMeta,
Expand All @@ -568,7 +569,7 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
);

@override
Future<String> crateApiClientClientSendEventBuilder(
Future<SendEventOutput> crateApiClientClientSendEventBuilder(
{required Client that, required EventBuilder builder}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
Expand All @@ -581,7 +582,7 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
funcId: 7, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
decodeSuccessData: sse_decode_send_event_output,
decodeErrorData: sse_decode_AnyhowException,
),
constMeta: kCrateApiClientClientSendEventBuilderConstMeta,
Expand Down Expand Up @@ -3053,6 +3054,13 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
return UnsignedEventImpl.frbInternalDcoDecode(raw as List<dynamic>);
}

@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return Map.fromEntries(dco_decode_list_record_string_string(raw)
.map((e) => MapEntry(e.$1, e.$2)));
}

@protected
Client
dco_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Client(
Expand Down Expand Up @@ -3148,6 +3156,12 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
return UnsignedEventImpl.frbInternalDcoDecode(raw as List<dynamic>);
}

@protected
Set<String> dco_decode_Set_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return Set.from(dco_decode_list_String(raw));
}

@protected
String dco_decode_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
Expand Down Expand Up @@ -3226,12 +3240,44 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
return raw as Uint8List;
}

@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return (raw as List<dynamic>).map(dco_decode_record_string_string).toList();
}

@protected
String? dco_decode_opt_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return raw == null ? null : dco_decode_String(raw);
}

@protected
(String, String) dco_decode_record_string_string(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
final arr = raw as List<dynamic>;
if (arr.length != 2) {
throw Exception('Expected 2 elements, got ${arr.length}');
}
return (
dco_decode_String(arr[0]),
dco_decode_String(arr[1]),
);
}

@protected
SendEventOutput dco_decode_send_event_output(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
final arr = raw as List<dynamic>;
if (arr.length != 3)
throw Exception('unexpected arr length: expect 3 but see ${arr.length}');
return SendEventOutput(
id: dco_decode_String(arr[0]),
success: dco_decode_Set_String(arr[1]),
failed: dco_decode_Map_String_String(arr[2]),
);
}

@protected
SignerBackend dco_decode_signer_backend(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
Expand Down Expand Up @@ -3502,6 +3548,14 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
sse_decode_usize(deserializer), sse_decode_i_32(deserializer));
}

@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_list_record_string_string(deserializer);
return Map.fromEntries(inner.map((e) => MapEntry(e.$1, e.$2)));
}

@protected
Client
sse_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Client(
Expand Down Expand Up @@ -3609,6 +3663,13 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
sse_decode_usize(deserializer), sse_decode_i_32(deserializer));
}

@protected
Set<String> sse_decode_Set_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_list_String(deserializer);
return Set.from(inner);
}

@protected
String sse_decode_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -3703,6 +3764,19 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
return deserializer.buffer.getUint8List(len_);
}

@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs

var len_ = sse_decode_i_32(deserializer);
var ans_ = <(String, String)>[];
for (var idx_ = 0; idx_ < len_; ++idx_) {
ans_.add(sse_decode_record_string_string(deserializer));
}
return ans_;
}

@protected
String? sse_decode_opt_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand All @@ -3714,6 +3788,25 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
}
}

@protected
(String, String) sse_decode_record_string_string(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var var_field0 = sse_decode_String(deserializer);
var var_field1 = sse_decode_String(deserializer);
return (var_field0, var_field1);
}

@protected
SendEventOutput sse_decode_send_event_output(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var var_id = sse_decode_String(deserializer);
var var_success = sse_decode_Set_String(deserializer);
var var_failed = sse_decode_Map_String_String(deserializer);
return SendEventOutput(
id: var_id, success: var_success, failed: var_failed);
}

@protected
SignerBackend sse_decode_signer_backend(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -3995,6 +4088,14 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
serializer);
}

@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_list_record_string_string(
self.entries.map((e) => (e.key, e.value)).toList(), serializer);
}

@protected
void
sse_encode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Client(
Expand Down Expand Up @@ -4107,6 +4208,12 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
serializer);
}

@protected
void sse_encode_Set_String(Set<String> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_list_String(self.toList(), serializer);
}

@protected
void sse_encode_String(String self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -4195,6 +4302,16 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
serializer.buffer.putUint8List(self);
}

@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_i_32(self.length, serializer);
for (final item in self) {
sse_encode_record_string_string(item, serializer);
}
}

@protected
void sse_encode_opt_String(String? self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand All @@ -4205,6 +4322,23 @@ class NostrSdkApiImpl extends NostrSdkApiImplPlatform implements NostrSdkApi {
}
}

@protected
void sse_encode_record_string_string(
(String, String) self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_String(self.$1, serializer);
sse_encode_String(self.$2, serializer);
}

@protected
void sse_encode_send_event_output(
SendEventOutput self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_String(self.id, serializer);
sse_encode_Set_String(self.success, serializer);
sse_encode_Map_String_String(self.failed, serializer);
}

@protected
void sse_encode_signer_backend(SignerBackend self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -4327,15 +4461,16 @@ class ClientImpl extends RustOpaque implements Client {
///
/// Send `Event` to all relays with `WRITE` flag.
/// If `gossip` option is enabled, the event will be sent also to NIP65 relays (automatically discovered).
Future<String> sendEvent({required Event event}) => NostrSdk.instance.api
.crateApiClientClientSendEvent(that: this, event: event);
Future<SendEventOutput> sendEvent({required Event event}) =>
NostrSdk.instance.api
.crateApiClientClientSendEvent(that: this, event: event);

/// Send event
///
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to relays (check [`Client::send_event`] from more details).
///
/// Return an error if the [`NostrSigner`] is not set.
Future<String> sendEventBuilder({required EventBuilder builder}) =>
Future<SendEventOutput> sendEventBuilder({required EventBuilder builder}) =>
NostrSdk.instance.api
.crateApiClientClientSendEventBuilder(that: this, builder: builder);
}
Expand Down
Loading

0 comments on commit b6fe16d

Please sign in to comment.