Add shared hex normalization for comment flows

This commit is contained in:
thePR0M3TH3AN committed 2025-12-12 13:42:15 -05:00
1 parent af8a5828dd
commit 735d67313c
6 files changed
+102 -89

No files matched your search

+4 -11
View File
@@ -1,6 +1,7 @@
// js/services/commentThreadService.js
import logger, { devLogger, userLogger } from "../utils/logger.js";
import { normalizeHexId, normalizeHexPubkey } from "../utils/hex.js";
import { buildVideoAddressPointer } from "../utils/videoPointer.js";
import { COMMENT_EVENT_KIND } from "../nostr/commentEvents.js";
import { FEATURE_IMPROVED_COMMENT_FETCHING } from "../constants.js";
@@ -24,11 +25,6 @@ function normalizeString(value) {
return typeof value === "string" ? value.trim() : "";
}
function normalizeHexId(value) {
const normalized = normalizeString(value);
return normalized ? normalized.toLowerCase() : "";
}
function normalizeRelay(value) {
if (typeof value === "string") {
return value.trim();
@@ -36,10 +32,7 @@ function normalizeRelay(value) {
return "";
}
function normalizePubkey(pubkey) {
const normalized = normalizeString(pubkey);
return normalized ? normalized.toLowerCase() : "";
}
const normalizePubkey = normalizeHexPubkey;
function normalizeKind(value) {
if (Number.isFinite(value)) {
@@ -225,11 +218,11 @@ export default class CommentThreadService {
this.videoKind = normalizeKind(video?.kind);
this.videoAuthorPubkeyRaw = rawVideoAuthorPubkey;
this.videoAuthorPubkey = normalizePubkey(rawVideoAuthorPubkey);
const pointerRootIdentifier = normalizeString(
const pointerRootIdentifier = normalizeHexId(
video?.pointerIdentifiers?.videoRootId,
);
this.videoRootIdentifier =
normalizeString(video?.videoRootId) || pointerRootIdentifier;
normalizeHexId(video?.videoRootId) || pointerRootIdentifier;
this.videoRootRelay = normalizeRelay(
video?.videoRootRelay || video?.rootIdentifierRelay,
);
+37 -43
View File
@@ -1,6 +1,7 @@
import { devLogger } from "../utils/logger.js";
import { buildVideoAddressPointer } from "../utils/videoPointer.js";
import { COMMENT_EVENT_KIND } from "../nostr/commentEvents.js";
import { normalizeHexString, normalizeHexPubkey } from "../utils/hex.js";
const DEFAULT_LIMIT = 40;
@@ -8,9 +9,7 @@ function normalizeString(value) {
return typeof value === "string" ? value.trim() : "";
}
function normalizeVideoEventId(value) {
return typeof value === "string" ? value.trim().toLowerCase() : "";
}
const normalizeHexId = normalizeHexString;
function normalizeRelay(value) {
if (typeof value === "string") {
@@ -88,7 +87,7 @@ export default class VideoModalCommentController {
normalizeHexPubkey:
typeof utils.normalizeHexPubkey === "function"
? (value) => utils.normalizeHexPubkey(value)
: () => null,
: (value) => normalizeHexPubkey(value) || null,
};
this.currentVideo = null;
@@ -156,19 +155,20 @@ export default class VideoModalCommentController {
return;
}
const videoEventId = normalizeString(video.id);
const videoEventId = normalizeHexId(video.id);
let videoDefinitionAddress = buildVideoAddressPointer(video);
if (!videoDefinitionAddress && this.modalCommentState.videoDefinitionAddress) {
videoDefinitionAddress = normalizeString(
this.modalCommentState.videoDefinitionAddress,
);
}
if (videoDefinitionAddress) {
videoDefinitionAddress = videoDefinitionAddress.trim().toLowerCase();
}
const videoKind = normalizeKind(video.kind);
const videoAuthorPubkey = normalizeString(video.pubkey);
const pointerRootId = normalizeString(
video.pointerIdentifiers?.videoRootId,
);
const videoRootId = normalizeString(video.videoRootId) || pointerRootId;
const videoAuthorPubkey = normalizeHexPubkey(video.pubkey);
const pointerRootId = normalizeHexId(video.pointerIdentifiers?.videoRootId);
const videoRootId = normalizeHexId(video.videoRootId) || pointerRootId;
const videoRootRelay = normalizeRelay(
video.videoRootRelay || video.rootIdentifierRelay,
);
@@ -356,10 +356,8 @@ export default class VideoModalCommentController {
return;
}
const snapshotVideoEventId = normalizeVideoEventId(snapshot.videoEventId);
const modalVideoEventId = normalizeVideoEventId(
this.modalCommentState.videoEventId,
);
const snapshotVideoEventId = normalizeHexId(snapshot.videoEventId);
const modalVideoEventId = normalizeHexId(this.modalCommentState.videoEventId);
if (!snapshotVideoEventId || snapshotVideoEventId !== modalVideoEventId) {
return;
@@ -368,9 +366,8 @@ export default class VideoModalCommentController {
this.modalCommentProfiles = this.createMapFromInput(snapshot.profiles, {
normalizeKey: this.utils.normalizeHexPubkey,
});
this.modalCommentState.parentCommentId = normalizeString(
snapshot.parentCommentId,
) || null;
this.modalCommentState.parentCommentId =
normalizeHexId(snapshot.parentCommentId) || null;
if (typeof snapshot.videoDefinitionAddress === "string") {
const pointer = normalizeString(snapshot.videoDefinitionAddress);
if (pointer) {
@@ -381,11 +378,11 @@ export default class VideoModalCommentController {
if (snapshotVideoKind) {
this.modalCommentState.videoKind = snapshotVideoKind;
}
const snapshotVideoAuthor = normalizeString(snapshot.videoAuthorPubkey);
const snapshotVideoAuthor = normalizeHexPubkey(snapshot.videoAuthorPubkey);
if (snapshotVideoAuthor) {
this.modalCommentState.videoAuthorPubkey = snapshotVideoAuthor;
}
const snapshotRootIdentifier = normalizeString(snapshot.rootIdentifier);
const snapshotRootIdentifier = normalizeHexId(snapshot.rootIdentifier);
if (snapshotRootIdentifier) {
this.modalCommentState.videoRootId = snapshotRootIdentifier;
}
@@ -397,7 +394,7 @@ export default class VideoModalCommentController {
if (snapshotParentKind) {
this.modalCommentState.parentCommentKind = snapshotParentKind;
}
const snapshotParentPubkey = normalizeString(snapshot.parentCommentPubkey);
const snapshotParentPubkey = normalizeHexPubkey(snapshot.parentCommentPubkey);
if (snapshotParentPubkey) {
this.modalCommentState.parentCommentPubkey = snapshotParentPubkey;
}
@@ -413,10 +410,8 @@ export default class VideoModalCommentController {
return;
}
const payloadVideoEventId = normalizeVideoEventId(payload.videoEventId);
const modalVideoEventId = normalizeVideoEventId(
this.modalCommentState.videoEventId,
);
const payloadVideoEventId = normalizeHexId(payload.videoEventId);
const modalVideoEventId = normalizeHexId(this.modalCommentState.videoEventId);
if (!payloadVideoEventId || payloadVideoEventId !== modalVideoEventId) {
return;
@@ -430,7 +425,7 @@ export default class VideoModalCommentController {
this.modalCommentProfiles.set(pubkey, profile);
});
const payloadRootIdentifier = normalizeString(payload.rootIdentifier);
const payloadRootIdentifier = normalizeHexId(payload.rootIdentifier);
if (payloadRootIdentifier) {
this.modalCommentState.videoRootId = payloadRootIdentifier;
}
@@ -460,7 +455,7 @@ export default class VideoModalCommentController {
if (eventKind) {
this.modalCommentState.parentCommentKind = eventKind;
}
const eventPubkey = normalizeString(event.pubkey);
const eventPubkey = normalizeHexPubkey(event.pubkey);
if (eventPubkey) {
this.modalCommentState.parentCommentPubkey = eventPubkey;
}
@@ -545,7 +540,7 @@ export default class VideoModalCommentController {
) {
normalizedKey = null;
} else {
normalizedKey = trimmed;
normalizedKey = normalizeHexId(trimmed) || trimmed;
}
}
const list = Array.isArray(value) ? value.filter(Boolean) : [];
@@ -592,9 +587,9 @@ export default class VideoModalCommentController {
});
return {
videoEventId: snapshot.videoEventId,
parentCommentId: snapshot.parentCommentId || null,
rootIdentifier: snapshot.rootIdentifier || null,
videoEventId: normalizeHexId(snapshot.videoEventId),
parentCommentId: normalizeHexId(snapshot.parentCommentId) || null,
rootIdentifier: normalizeHexId(snapshot.rootIdentifier) || null,
rootIdentifierRelay: snapshot.rootIdentifierRelay || null,
commentsById: sanitizedComments,
childrenByParent: sanitizedChildren,
@@ -640,7 +635,7 @@ export default class VideoModalCommentController {
return;
}
const videoEventId = normalizeString(video.id);
const videoEventId = normalizeHexId(video.id);
let videoDefinitionAddress = buildVideoAddressPointer(video);
if (!videoDefinitionAddress && this.modalCommentState.videoDefinitionAddress) {
videoDefinitionAddress = normalizeString(
@@ -648,18 +643,17 @@ export default class VideoModalCommentController {
);
}
const videoKind =
this.modalCommentState.videoKind || normalizeKind(video.kind);
normalizeKind(this.modalCommentState.videoKind) || normalizeKind(video.kind);
const videoAuthorPubkey =
this.modalCommentState.videoAuthorPubkey || normalizeString(video.pubkey);
const fallbackPointerRootId = normalizeString(
video.pointerIdentifiers?.videoRootId,
);
normalizeHexPubkey(this.modalCommentState.videoAuthorPubkey) ||
normalizeHexPubkey(video.pubkey);
const fallbackPointerRootId = normalizeHexId(video.pointerIdentifiers?.videoRootId);
const videoRootId =
this.modalCommentState.videoRootId ||
normalizeString(video.videoRootId) ||
normalizeHexId(this.modalCommentState.videoRootId) ||
normalizeHexId(video.videoRootId) ||
fallbackPointerRootId;
const videoRootRelay =
this.modalCommentState.videoRootRelay ||
normalizeRelay(this.modalCommentState.videoRootRelay) ||
normalizeRelay(video.videoRootRelay || video.rootIdentifierRelay);
if (!videoEventId) {
@@ -667,12 +661,12 @@ export default class VideoModalCommentController {
return;
}
const parentCommentId = normalizeString(detail.parentId) || null;
const parentCommentId = normalizeHexId(detail.parentId) || null;
let parentCommentKind = this.modalCommentState.parentCommentKind
? normalizeKind(this.modalCommentState.parentCommentKind)
: "";
let parentCommentPubkey = this.modalCommentState.parentCommentPubkey
? normalizeString(this.modalCommentState.parentCommentPubkey)
? normalizeHexPubkey(this.modalCommentState.parentCommentPubkey)
: "";
if (parentCommentId && typeof this.commentThreadService?.getCommentEvent === "function") {
@@ -685,7 +679,7 @@ export default class VideoModalCommentController {
parentCommentKind = eventKind;
this.modalCommentState.parentCommentKind = eventKind;
}
const eventPubkey = normalizeString(parentEvent.pubkey);
const eventPubkey = normalizeHexPubkey(parentEvent.pubkey);
if (eventPubkey) {
parentCommentPubkey = eventPubkey;
this.modalCommentState.parentCommentPubkey = eventPubkey;
@@ -874,7 +868,7 @@ export default class VideoModalCommentController {
}
async handleVideoModalCommentMute(detail = {}) {
const pubkey = normalizeString(detail?.pubkey);
const pubkey = normalizeHexPubkey(detail?.pubkey);
if (!pubkey) {
return;
}
+11
View File
@@ -0,0 +1,11 @@
export function normalizeHexString(value) {
if (typeof value !== "string") {
return "";
}
const trimmed = value.trim();
return trimmed ? trimmed.toLowerCase() : "";
}
export const normalizeHexId = normalizeHexString;
export const normalizeHexPubkey = normalizeHexString;
+2 -1
View File
@@ -2,6 +2,7 @@
import { pointerArrayToKey } from "./pointer.js";
import { devLogger } from "./logger.js";
import { normalizeHexPubkey } from "./hex.js";
const DEFAULT_VIDEO_KIND = 30078;
@@ -84,7 +85,7 @@ export function buildVideoAddressPointer(
return "";
}
const pubkey = typeof video.pubkey === "string" ? video.pubkey.trim() : "";
const pubkey = normalizeHexPubkey(video.pubkey);
if (!pubkey) {
return "";
}