Use kind 1111 comments with address-first tags

This commit is contained in:
thePR0M3TH3AN committed 2025-10-29 07:45:34 -04:00
1 parent a42dbb45e2
commit d1d4fa150c
5 files changed
+128 -68

No files matched your search

+63 -23
View File
@@ -11,7 +11,7 @@ import { devLogger, userLogger } from "../utils/logger.js";
const COMMENT_EVENT_SCHEMA = getNostrEventSchema(NOTE_TYPES.VIDEO_COMMENT);
export const COMMENT_EVENT_KIND = Number.isFinite(COMMENT_EVENT_SCHEMA?.kind)
? COMMENT_EVENT_SCHEMA.kind
: 1;
: 1111;
function sanitizeRelayList(primary, fallback) {
if (Array.isArray(primary) && primary.length) {
@@ -239,41 +239,77 @@ function normalizeCommentTarget(targetInput = {}, overrides = {}) {
};
}
function createVideoCommentFilters(targetInput, options = {}) {
const descriptor = normalizeCommentTarget(targetInput, options);
if (!descriptor) {
throw new Error("Invalid video comment target supplied.");
function applyFilterOptions(filter, options = {}) {
if (!filter || typeof filter !== "object") {
return filter;
}
const filter = {
kinds: [COMMENT_EVENT_KIND],
"#e": [descriptor.videoEventId],
"#a": [descriptor.videoDefinitionAddress],
};
if (descriptor.parentCommentId) {
if (!filter["#e"].includes(descriptor.parentCommentId)) {
filter["#e"].push(descriptor.parentCommentId);
}
}
const result = { ...filter };
if (
typeof options.limit === "number" &&
Number.isFinite(options.limit) &&
options.limit > 0
) {
filter.limit = Math.floor(options.limit);
result.limit = Math.floor(options.limit);
}
if (typeof options.since === "number" && Number.isFinite(options.since)) {
filter.since = Math.floor(options.since);
result.since = Math.floor(options.since);
}
if (typeof options.until === "number" && Number.isFinite(options.until)) {
filter.until = Math.floor(options.until);
result.until = Math.floor(options.until);
}
return { descriptor, filters: [filter] };
return result;
}
function createVideoCommentFilters(targetInput, options = {}) {
const descriptor = normalizeCommentTarget(targetInput, options);
if (!descriptor) {
throw new Error("Invalid video comment target supplied.");
}
const filters = [];
const baseFilter = { kinds: [COMMENT_EVENT_KIND] };
if (descriptor.videoDefinitionAddress) {
baseFilter["#a"] = [descriptor.videoDefinitionAddress];
}
if (descriptor.parentCommentId) {
baseFilter["#e"] = [descriptor.parentCommentId];
}
if (!descriptor.videoDefinitionAddress && descriptor.videoEventId) {
baseFilter["#e"] = baseFilter["#e"] || [];
if (!baseFilter["#e"].includes(descriptor.videoEventId)) {
baseFilter["#e"].push(descriptor.videoEventId);
}
}
filters.push(applyFilterOptions(baseFilter, options));
const shouldIncludeLegacyFilter =
Boolean(descriptor.videoDefinitionAddress) && Boolean(descriptor.videoEventId);
if (shouldIncludeLegacyFilter) {
const legacyFilter = {
kinds: [COMMENT_EVENT_KIND],
"#e": [descriptor.videoEventId],
};
if (descriptor.parentCommentId) {
if (!legacyFilter["#e"].includes(descriptor.parentCommentId)) {
legacyFilter["#e"].push(descriptor.parentCommentId);
}
}
filters.push(applyFilterOptions(legacyFilter, options));
}
return { descriptor, filters };
}
function flattenListResults(input) {
@@ -306,9 +342,13 @@ function isVideoCommentEvent(event, descriptor) {
}
const tags = Array.isArray(event.tags) ? event.tags : [];
let hasEventTag = false;
let hasDefinitionTag = false;
let hasParentTag = !descriptor.parentCommentId;
const requiresParentTag = Boolean(descriptor.parentCommentId);
const requiresAddressMatch = Boolean(descriptor.videoDefinitionAddress);
const requiresEventMatch = !requiresAddressMatch && Boolean(descriptor.videoEventId);
let hasEventTag = !requiresEventMatch;
let hasDefinitionTag = !requiresAddressMatch;
let hasParentTag = !requiresParentTag;
for (const tag of tags) {
if (!Array.isArray(tag) || tag.length < 2) {
+16 -11
View File
@@ -238,7 +238,7 @@ const BASE_SCHEMAS = {
[NOTE_TYPES.VIDEO_COMMENT]: {
type: NOTE_TYPES.VIDEO_COMMENT,
label: "Video comment",
kind: 1,
kind: 1111,
videoEventTagName: "e",
videoDefinitionTagName: "a",
parentCommentTagName: "e",
@@ -889,19 +889,10 @@ export function buildCommentEvent({
const schema = getNostrEventSchema(NOTE_TYPES.VIDEO_COMMENT);
const tags = [];
const videoEventTagName = schema?.videoEventTagName || "e";
const normalizedVideoEventId =
typeof videoEventId === "string" ? videoEventId.trim() : "";
const normalizedVideoEventRelay =
typeof videoEventRelay === "string" ? videoEventRelay.trim() : "";
if (videoEventTagName && normalizedVideoEventId) {
if (normalizedVideoEventRelay) {
tags.push([videoEventTagName, normalizedVideoEventId, normalizedVideoEventRelay]);
} else {
tags.push([videoEventTagName, normalizedVideoEventId]);
}
}
const videoDefinitionTagName = schema?.videoDefinitionTagName || "a";
const normalizedVideoDefinitionAddress =
typeof videoDefinitionAddress === "string"
@@ -909,6 +900,20 @@ export function buildCommentEvent({
: "";
const normalizedVideoDefinitionRelay =
typeof videoDefinitionRelay === "string" ? videoDefinitionRelay.trim() : "";
const includeVideoEventTag =
!normalizedVideoDefinitionAddress &&
Boolean(schema?.videoEventTagName || "e") &&
Boolean(normalizedVideoEventId);
if (includeVideoEventTag) {
const videoEventTagName = schema?.videoEventTagName || "e";
if (normalizedVideoEventRelay) {
tags.push([videoEventTagName, normalizedVideoEventId, normalizedVideoEventRelay]);
} else {
tags.push([videoEventTagName, normalizedVideoEventId]);
}
}
if (videoDefinitionTagName && normalizedVideoDefinitionAddress) {
if (normalizedVideoDefinitionRelay) {
tags.push([
@@ -972,7 +977,7 @@ export function buildCommentEvent({
const resolvedContent = ensureValidUtf8Content(content);
return {
kind: schema?.kind ?? 1,
kind: schema?.kind ?? 1111,
pubkey,
created_at,
tags,
+3 -2
View File
@@ -1,5 +1,6 @@
import { devLogger } from "../utils/logger.js";
import { buildVideoAddressPointer } from "../utils/videoPointer.js";
import { COMMENT_EVENT_KIND } from "../nostr/commentEvents.js";
const DEFAULT_MAX_DISCUSSION_COUNT_VIDEOS = 24;
const COUNT_UNSUPPORTED_TITLE = "Relay does not support NIP-45 COUNT queries.";
@@ -130,12 +131,12 @@ export default class DiscussionCountService {
const filters = [];
const eventId = typeof video.id === "string" ? video.id.trim() : "";
if (eventId) {
filters.push({ kinds: [1], "#e": [eventId] });
filters.push({ kinds: [COMMENT_EVENT_KIND], "#e": [eventId] });
}
const address = buildVideoAddressPointer(video);
if (address) {
filters.push({ kinds: [1], "#a": [address] });
filters.push({ kinds: [COMMENT_EVENT_KIND], "#a": [address] });
}
return filters;