mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-09 06:12:56 +00:00
Preserve video metadata in watch history payloads
This commit is contained in:
1 parent
4e42db2667
commit
507f09dff5
6 files changed
+435
-30
No files matched your search
@@ -260,6 +260,23 @@ function sanitizeVideoForStorage(video) {
|
||||
};
|
||||
}
|
||||
|
||||
function sanitizeVideoForHistory(video) {
|
||||
const sanitized = sanitizeVideoForStorage(video);
|
||||
if (!sanitized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
id: sanitized.id,
|
||||
title: sanitized.title,
|
||||
thumbnail: sanitized.thumbnail,
|
||||
pubkey: sanitized.pubkey,
|
||||
created_at: sanitized.created_at,
|
||||
url: sanitized.url,
|
||||
magnet: sanitized.magnet,
|
||||
};
|
||||
}
|
||||
|
||||
function sanitizeProfileForStorage(profile) {
|
||||
if (!profile || typeof profile !== "object") {
|
||||
return null;
|
||||
@@ -357,6 +374,41 @@ function restoreMetadataCache() {
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizePointerMetadata(metadata) {
|
||||
if (!metadata || typeof metadata !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pointerMetadata = {};
|
||||
const video = sanitizeVideoForHistory(metadata.video);
|
||||
if (video) {
|
||||
pointerMetadata.video = video;
|
||||
}
|
||||
|
||||
const profile = sanitizeProfileForStorage(metadata.profile);
|
||||
if (profile) {
|
||||
pointerMetadata.profile = profile;
|
||||
}
|
||||
|
||||
const resumeCandidates = [
|
||||
metadata.resumeAt,
|
||||
metadata.resume,
|
||||
metadata.resumeSeconds,
|
||||
];
|
||||
for (const candidate of resumeCandidates) {
|
||||
if (Number.isFinite(candidate)) {
|
||||
pointerMetadata.resumeAt = Math.max(0, Math.floor(candidate));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata.completed === true) {
|
||||
pointerMetadata.completed = true;
|
||||
}
|
||||
|
||||
return Object.keys(pointerMetadata).length ? pointerMetadata : null;
|
||||
}
|
||||
|
||||
function persistMetadataCache() {
|
||||
const storage = getLocalStorage();
|
||||
if (!storage) {
|
||||
@@ -975,6 +1027,33 @@ async function publishView(pointerInput, createdAt, metadata = {}) {
|
||||
viewResult?.event?.created_at
|
||||
);
|
||||
|
||||
const pointerMetadata = sanitizePointerMetadata(metadata);
|
||||
if (pointerMetadata) {
|
||||
const existingMetadata =
|
||||
typeof normalizedPointer.metadata === "object"
|
||||
? normalizedPointer.metadata
|
||||
: {};
|
||||
normalizedPointer.metadata = { ...existingMetadata, ...pointerMetadata };
|
||||
if (pointerMetadata.video) {
|
||||
normalizedPointer.video = pointerMetadata.video;
|
||||
}
|
||||
if (pointerMetadata.profile) {
|
||||
normalizedPointer.profile = pointerMetadata.profile;
|
||||
}
|
||||
if (
|
||||
Number.isFinite(pointerMetadata.resumeAt) &&
|
||||
!Number.isFinite(normalizedPointer.resumeAt)
|
||||
) {
|
||||
normalizedPointer.resumeAt = Math.max(
|
||||
0,
|
||||
Math.floor(pointerMetadata.resumeAt),
|
||||
);
|
||||
}
|
||||
if (pointerMetadata.completed === true) {
|
||||
normalizedPointer.completed = true;
|
||||
}
|
||||
}
|
||||
|
||||
const key = pointerKey(normalizedPointer);
|
||||
console.info(
|
||||
"[watchHistoryService] Watch list process triggered by video view.",
|
||||
|
||||
Reference in new issue
Block a user