Init2
This commit is contained in:
110
src/nostr/Subscriptions.js
Normal file
110
src/nostr/Subscriptions.js
Normal file
@ -0,0 +1,110 @@
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
export class Subscriptions {
|
||||
constructor() {
|
||||
/**
|
||||
* A unique id for this subscription filter
|
||||
*/
|
||||
this.Id = uuid();
|
||||
|
||||
/**
|
||||
* a list of event ids or prefixes
|
||||
*/
|
||||
this.Ids = new Set();
|
||||
|
||||
/**
|
||||
* a list of pubkeys or prefixes, the pubkey of an event must be one of these
|
||||
*/
|
||||
this.Authors = new Set();
|
||||
|
||||
/**
|
||||
* a list of a kind numbers
|
||||
*/
|
||||
this.Kinds = new Set();
|
||||
|
||||
/**
|
||||
* a list of event ids that are referenced in an "e" tag
|
||||
*/
|
||||
this.ETags = new Set();
|
||||
|
||||
/**
|
||||
* a list of pubkeys that are referenced in a "p" tag
|
||||
*/
|
||||
this.PTags = new Set();
|
||||
|
||||
/**
|
||||
* a timestamp, events must be newer than this to pass
|
||||
*/
|
||||
this.Since = NaN;
|
||||
|
||||
/**
|
||||
* a timestamp, events must be older than this to pass
|
||||
*/
|
||||
this.Until = NaN;
|
||||
|
||||
/**
|
||||
* maximum number of events to be returned in the initial query
|
||||
*/
|
||||
this.Limit = NaN;
|
||||
|
||||
/**
|
||||
* Handler function for this event
|
||||
*/
|
||||
this.OnEvent = (e) => { console.warn(`No event handler was set on subscription: ${this.Id}`) };
|
||||
|
||||
/**
|
||||
* Collection of OR sub scriptions linked to this
|
||||
*/
|
||||
this.OrSubs = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds OR filter subscriptions
|
||||
* @param {Subscriptions} sub Extra filters
|
||||
*/
|
||||
AddSubscription(sub) {
|
||||
this.OrSubs.push(sub);
|
||||
}
|
||||
|
||||
static FromObject(obj) {
|
||||
let ret = new Subscriptions();
|
||||
ret.Ids = new Set(obj.ids);
|
||||
ret.Authors = new Set(obj.authors);
|
||||
ret.Kinds = new Set(obj.kinds);
|
||||
ret.ETags = new Set(obj["#e"]);
|
||||
ret.PTags = new Set(obj["#p"]);
|
||||
ret.Since = parseInt(obj.since);
|
||||
ret.Until = parseInt(obj.until);
|
||||
ret.Limit = parseInt(obj.limit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ToObject() {
|
||||
let ret = {};
|
||||
if (this.Ids.size > 0) {
|
||||
ret.ids = Array.from(this.Ids);
|
||||
}
|
||||
if (this.Authors.size > 0) {
|
||||
ret.authors = Array.from(this.Authors);
|
||||
}
|
||||
if (this.Kinds.size > 0) {
|
||||
ret.kinds = Array.from(this.Kinds);
|
||||
}
|
||||
if (this.ETags.size > 0) {
|
||||
ret["#e"] = Array.from(this.ETags);
|
||||
}
|
||||
if (this.PTags.size > 0) {
|
||||
ret["#p"] = Array.from(this.PTags);
|
||||
}
|
||||
if (!isNaN(this.Since)) {
|
||||
ret.since = this.Since;
|
||||
}
|
||||
if (!isNaN(this.Until)) {
|
||||
ret.until = this.Until;
|
||||
}
|
||||
if (!isNaN(this.Limit)) {
|
||||
ret.limit = this.Limit;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user