snort/src/nostr/Thread.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-12-18 14:51:47 +00:00
import Event from "./Event";
2023-01-08 21:59:33 +00:00
import EventKind from "./EventKind";
2022-12-18 14:51:47 +00:00
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 {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;
}
2023-01-08 21:59:33 +00:00
let shouldWriteMarkers = ev.Kind === EventKind.TextNote;
2022-12-18 14:51:47 +00:00
let ret = new Thread();
let eTags = ev.Tags.filter(a => a.Key === "e");
let marked = eTags.some(a => a.Marker !== null);
if (!marked) {
ret.Root = eTags[0];
2023-01-08 21:59:33 +00:00
ret.Root.Marker = shouldWriteMarkers ? "root" : null;
2023-01-06 15:48:10 +00:00
if (eTags.length > 1) {
ret.ReplyTo = eTags[1];
2023-01-08 21:59:33 +00:00
ret.ReplyTo.Marker = shouldWriteMarkers ? "reply" : null;
2023-01-06 15:48:10 +00:00
}
2022-12-18 14:51:47 +00:00
if (eTags.length > 2) {
2023-01-06 15:48:10 +00:00
ret.Mentions = eTags.slice(2);
2023-01-08 21:59:33 +00:00
if (shouldWriteMarkers) {
ret.Mentions.forEach(a => a.Marker = "mention");
}
2022-12-18 14:51:47 +00:00
}
} 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
}
2023-01-06 13:00:59 +00:00
ret.PubKeys = [...new Set(ev.Tags.filter(a => a.Key === "p").map(a => a.PubKey))]
2022-12-18 14:51:47 +00:00
return ret;
}
}