removeElementFromList method
Removes an element from a NIP-51 list.
Updates the list by removing the specified element, then broadcasts the updated list to relays and updates the cache.
kind the kind of NIP-51 list
tag the tag type of the element to remove
value the value to remove from the list
broadcastRelays optional specific relays to broadcast to
Returns the updated list, or null if the list doesn't exist.
Throws an exception if no event signer is available.
Implementation
Future<Nip51List?> removeElementFromList({
required int kind,
required String tag,
required String value,
Iterable<String>? broadcastRelays,
}) async {
if (_eventSigner == null) {
throw Exception(
"cannot broadcast private nip51 list without a signer that can sign");
}
Nip51List? list = await getSingleNip51List(
kind,
forceRefresh: true,
);
if (list == null || list.elements.isEmpty) {
list = Nip51List(
kind: kind,
pubKey: _eventSigner!.getPublicKey(),
createdAt: Helpers.now,
elements: []);
}
if (list.elements.isNotEmpty) {
list.removeElement(tag, value);
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;
}