removeElementFromSet method
Removes an element from a NIP-51 set.
Updates the set by removing the specified element, then broadcasts the updated set to relays and updates the cache.
name name of the set (d tag identifier)
value the value to remove from the set
tag the tag type of the element to remove
kind kind of the set
private if true, the element was encrypted
specificRelays optional specific relays to broadcast to
Returns the updated set. Throws an exception if no event signer is available.
Implementation
Future<Nip51Set?> removeElementFromSet({
required String name,
required String value,
required String tag,
required int kind,
bool private = false,
Iterable<String>? specificRelays,
}) async {
if (_eventSigner == null) {
throw Exception(
"cannot broadcast private nip51 list without a signer that can sign");
}
Nip51Set? mySet = await getSetByName(
name: name,
kind: kind,
forceRefresh: true,
);
if ((mySet == null || mySet.allRelays.isEmpty)) {
mySet = Nip51Set(
name: name,
kind: Nip51List.kRelaySet,
pubKey: _eventSigner!.getPublicKey(),
createdAt: Helpers.now,
elements: []);
}
mySet.removeElement(tag, value);
mySet.createdAt = Helpers.now;
Nip01Event event = await mySet.toEvent(_eventSigner);
final broadcastResponse = _broadcast.broadcast(
nostrEvent: event,
specificRelays: specificRelays,
customSigner: _eventSigner,
);
await broadcastResponse.broadcastDoneFuture;
List<Nip01Event>? events = await _cacheManager.loadEvents(
pubKeys: [_eventSigner!.getPublicKey()], kinds: [Nip51List.kRelaySet]);
events = events.where((event) {
if (event.getDtag() != null && event.getDtag() == name) {
return true;
}
return false;
}).toList();
for (var event in events) {
_cacheManager.removeEvent(event.id);
}
await _cacheManager.saveEvent(event);
return mySet;
}