addElementToSet method
Adds an element to a NIP-51 set.
If the set doesn't exist, it will be created. The updated set is then broadcast to relays and cached locally.
name name of the set (d tag identifier)
tag the tag type for the element (e.g., 'relay', 'p', 'e')
value the value to add to the set
kind kind of the set
private if true, encrypt the element in the set content
specificRelays optional specific relays to broadcast to
Returns the updated set.
Implementation
Future<Nip51Set?> addElementToSet({
required String name,
required String tag,
required String value,
required int kind,
bool private = false,
Iterable<String>? specificRelays,
}) async {
Nip51Set? set =
await getSetByName(name: name, kind: kind, forceRefresh: true);
set ??= Nip51Set(
name: name,
pubKey: _eventSigner!.getPublicKey(),
kind: Nip51List.kRelaySet,
createdAt: Helpers.now,
elements: []);
set.addElement(tag, value, private);
set.createdAt = Helpers.now;
Nip01Event event = await set.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: [kind]);
events = events.where((event) {
if (event.getDtag() != null && event.getDtag() == name) {
return true;
}
return false;
}).toList();
for (final event in events) {
_cacheManager.removeEvent(event.id);
}
await _cacheManager.saveEvent(event);
return set;
}