addElementToList method
Adds an element to a NIP-51 list.
If the list doesn't exist, it will be created. The updated list is then broadcast to relays and cached locally.
kind the kind of NIP-51 list
tag the tag type for the element (e.g., 'p' for pubkey, 'e' for event)
value the value to add to the list
broadcastRelays optional specific relays to broadcast to
private if true, encrypt the element in the list content
Returns the updated list.
Throws an exception if no event signer is available.
Implementation
Future<Nip51List> addElementToList({
required int kind,
required String tag,
required String value,
Iterable<String>? broadcastRelays,
bool private = false,
}) async {
if (_eventSigner == null) {
throw Exception(
"cannot broadcast private nip51 list without a signer that can sign");
}
Nip51List? list = await getSingleNip51List(
kind,
forceRefresh: true,
);
list ??= Nip51List(
kind: kind,
pubKey: _eventSigner!.getPublicKey(),
createdAt: Helpers.now,
elements: []);
list.addElement(tag, value, private);
list.createdAt = Helpers.now;
Nip01Event event = await list.toEvent(_eventSigner);
final broadcastResponse = _broadcast.broadcast(
nostrEvent: event,
specificRelays: broadcastRelays,
customSigner: _eventSigner,
);
await broadcastResponse.broadcastDoneFuture;
List<Nip01Event>? events = await _cacheManager
.loadEvents(pubKeys: [_eventSigner!.getPublicKey()], kinds: [kind]);
for (var event in events) {
_cacheManager.removeEvent(event.id);
}
await _cacheManager.saveEvent(event);
return list;
}