snort/src/nostr/Thread.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-12-18 14:51:47 +00:00
import Event from "./Event";
export default class Thread {
constructor() {
2022-12-20 12:08:41 +00:00
/** @type {Tag} */
2022-12-18 14:51:47 +00:00
this.Root = null;
2022-12-20 12:08:41 +00:00
/** @type {Tag} */
2022-12-18 14:51:47 +00:00
this.ReplyTo = null;
2022-12-20 12:08:41 +00:00
/** @type {Array<Tag>} */
2022-12-18 14:51:47 +00:00
this.Mentions = [];
2022-12-20 12:08:41 +00:00
/** @type {Event} */
2022-12-18 14:51:47 +00:00
this.Reply = null;
2022-12-20 12:08:41 +00:00
/** @type {Array<String>} */
this.PubKeys = [];
2022-12-18 14:51:47 +00:00
}
/**
* Extract thread information from an Event
* @param {Event} ev Event to extract thread from
*/
static ExtractThread(ev) {
let isThread = ev.Tags.some(a => a.Key === "e");
if (!isThread) {
return null;
}
let ret = new Thread();
ret.Reply = ev;
let eTags = ev.Tags.filter(a => a.Key === "e");
let marked = eTags.some(a => a.Marker !== null);
if (!marked) {
ret.Root = eTags[0];
if (eTags.length > 2) {
ret.Mentions = eTags.slice(1, -1);
}
ret.ReplyTo = eTags[eTags.length - 1];
} else {
let root = eTags.find(a => a.Marker === "root");
let reply = eTags.find(a => a.Marker === "reply");
ret.Root = root;
ret.ReplyTo = reply;
2022-12-29 16:19:16 +00:00
ret.Mentions = eTags.filter(a => a.Marker === "mention");
2022-12-18 14:51:47 +00:00
}
2022-12-20 12:08:41 +00:00
ret.PubKeys = ev.Tags.filter(a => a.Key === "p").map(a => a.PubKey);
2022-12-18 14:51:47 +00:00
return ret;
}
}