Profile editor

This commit is contained in:
2022-12-28 14:51:33 +00:00
parent aadc58a104
commit 00b0cecf6c
9 changed files with 140 additions and 26 deletions

View File

@ -45,8 +45,12 @@ export default class Connection {
console.log(e);
}
/**
* Send event on this connection
* @param {Event} e
*/
SendEvent(e) {
let req = ["EVENT", e];
let req = ["EVENT", e.ToObject()];
this._SendJson(req);
}

View File

@ -57,6 +57,9 @@ export default class Event {
let sig = await secp.schnorr.sign(this.Id, key);
this.Signature = secp.utils.bytesToHex(sig);
if(!await this.Verify()) {
throw "Signing failed";
}
}
/**
@ -135,4 +138,28 @@ export default class Event {
sig: this.Signature
};
}
/**
* Create a new event for a specific pubkey
* @param {String} pubKey
*/
static ForPubKey(pubKey) {
let ev = new Event();
ev.CreatedAt = parseInt(new Date().getTime() / 1000);
ev.PubKey = pubKey;
return ev;
}
/**
* Create new SetMetadata event
* @param {String} pubKey Pubkey of the creator of this event
* @param {any} obj Metadata content
* @returns {Event}
*/
static SetMetadata(pubKey, obj) {
let ev = Event.ForPubKey(pubKey);
ev.Kind = EventKind.SetMetadata;
ev.Content = JSON.stringify(obj);
return ev;
}
}

View File

@ -83,6 +83,14 @@ export class Subscriptions {
this.OrSubs.push(sub);
}
/**
* If all relays have responded with EOSE
* @returns {boolean}
*/
IsFinished() {
return Object.keys(this.Started).length === Object.keys(this.Finished).length;
}
static FromObject(obj) {
let ret = new Subscriptions();
ret.Ids = new Set(obj.ids);

View File

@ -8,6 +8,7 @@ export class NostrSystem {
constructor() {
this.Sockets = {};
this.Subscriptions = {};
this.PendingSubscriptions = [];
}
/**
@ -38,6 +39,10 @@ export class NostrSystem {
delete this.Subscriptions[subId];
}
/**
* Send events to writable relays
* @param {Event} ev
*/
BroadcastEvent(ev) {
for (let s of Object.values(this.Sockets)) {
s.SendEvent(ev);
@ -51,15 +56,11 @@ export class NostrSystem {
*/
RequestSubscription(sub) {
return new Promise((resolve, reject) => {
let counter = 0;
let events = [];
// force timeout returning current results
let timeout = setTimeout(() => {
for (let s of Object.values(this.Sockets)) {
s.RemoveSubscription(sub.Id);
counter++;
}
this.RemoveSubscription(sub.Id);
resolve(events);
}, 10_000);
@ -74,16 +75,13 @@ export class NostrSystem {
};
sub.OnEnd = (c) => {
c.RemoveSubscription(sub.Id);
console.debug(counter);
if (counter-- <= 0) {
if (sub.IsFinished()) {
clearInterval(timeout);
console.debug(`[${sub.Id}] Finished`);
resolve(events);
}
};
for (let s of Object.values(this.Sockets)) {
s.AddSubscription(sub);
counter++;
}
this.AddSubscription(sub);
});
}
}