mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-01 02:12:26 +00:00
Sort video grid by original posted time
This commit is contained in:
1 parent
b744524521
commit
57a1587ab5
2 files changed
+101
-4
No files matched your search
@@ -455,7 +455,10 @@ export class VideoListView {
|
||||
return displayVideos;
|
||||
}
|
||||
|
||||
displayVideos.sort((a, b) => (b?.created_at || 0) - (a?.created_at || 0));
|
||||
displayVideos.sort(
|
||||
(a, b) =>
|
||||
this.getVideoPostedAtForSort(b) - this.getVideoPostedAtForSort(a),
|
||||
);
|
||||
this.currentVideos = displayVideos.slice();
|
||||
|
||||
const signaturePayload = displayVideos.map((video) => ({
|
||||
@@ -463,6 +466,10 @@ export class VideoListView {
|
||||
createdAt: Number.isFinite(video?.created_at)
|
||||
? video.created_at
|
||||
: Number(video?.created_at) || 0,
|
||||
postedAt: (() => {
|
||||
const timestamp = this.getVideoPostedAtForSort(video);
|
||||
return Number.isFinite(timestamp) ? timestamp : 0;
|
||||
})(),
|
||||
deleted: Boolean(video?.deleted),
|
||||
isPrivate: Boolean(video?.isPrivate),
|
||||
isNsfw: Boolean(video?.isNsfw),
|
||||
@@ -530,9 +537,10 @@ export class VideoListView {
|
||||
const fallbackTimestamp =
|
||||
normalizedPostedAt !== null
|
||||
? normalizedPostedAt
|
||||
: Number.isFinite(video?.created_at)
|
||||
? Math.floor(video.created_at)
|
||||
: null;
|
||||
: (() => {
|
||||
const sortTimestamp = this.getVideoPostedAtForSort(video);
|
||||
return Number.isFinite(sortTimestamp) ? sortTimestamp : null;
|
||||
})();
|
||||
const timeAgo =
|
||||
fallbackTimestamp !== null
|
||||
? this.formatters.formatTimeAgo(fallbackTimestamp)
|
||||
@@ -928,6 +936,31 @@ export class VideoListView {
|
||||
return next;
|
||||
}
|
||||
|
||||
getVideoPostedAtForSort(video) {
|
||||
if (!video || typeof video !== "object") {
|
||||
return Number.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
const known = this.utils.getKnownVideoPostedAt(video);
|
||||
if (Number.isFinite(known)) {
|
||||
return Math.floor(known);
|
||||
}
|
||||
|
||||
if (Number.isFinite(video?.rootCreatedAt)) {
|
||||
return Math.floor(video.rootCreatedAt);
|
||||
}
|
||||
|
||||
if (Number.isFinite(video?.nip71Source?.created_at)) {
|
||||
return Math.floor(video.nip71Source.created_at);
|
||||
}
|
||||
|
||||
if (Number.isFinite(video?.created_at)) {
|
||||
return Math.floor(video.created_at);
|
||||
}
|
||||
|
||||
return Number.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
computeShareBase() {
|
||||
const explicit = this.utils.getShareUrlBase();
|
||||
if (explicit) {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
import { VideoListView } from "../../js/ui/views/VideoListView.js";
|
||||
|
||||
function createViewDom() {
|
||||
const dom = new JSDOM(
|
||||
"<!DOCTYPE html><body><section><div id=\"videoList\"></div></section></body>",
|
||||
);
|
||||
const { document } = dom.window;
|
||||
const listRoot = document.getElementById("videoList");
|
||||
return { dom, document, listRoot };
|
||||
}
|
||||
|
||||
test("VideoListView sorts cards by original posted timestamp", () => {
|
||||
const { document, listRoot } = createViewDom();
|
||||
const view = new VideoListView({
|
||||
document,
|
||||
utils: {
|
||||
dedupeVideos: (videos) => (Array.isArray(videos) ? [...videos] : []),
|
||||
getKnownVideoPostedAt: (video) =>
|
||||
Number.isFinite(video?.rootCreatedAt) ? Math.floor(video.rootCreatedAt) : null,
|
||||
},
|
||||
});
|
||||
|
||||
view.mount(listRoot);
|
||||
|
||||
const videos = [
|
||||
{
|
||||
id: "video-a",
|
||||
title: "Edited Upload",
|
||||
created_at: 200,
|
||||
rootCreatedAt: 100,
|
||||
},
|
||||
{
|
||||
id: "video-b",
|
||||
title: "Recent Upload",
|
||||
created_at: 150,
|
||||
rootCreatedAt: 150,
|
||||
},
|
||||
{
|
||||
id: "video-c",
|
||||
title: "Vintage Upload",
|
||||
created_at: 140,
|
||||
rootCreatedAt: 90,
|
||||
},
|
||||
];
|
||||
|
||||
view.render(videos);
|
||||
|
||||
assert.deepEqual(
|
||||
view.currentVideos.map((video) => video.id),
|
||||
["video-b", "video-a", "video-c"],
|
||||
);
|
||||
|
||||
const nodes = listRoot.querySelectorAll('[data-component="video-card"]');
|
||||
assert.equal(nodes.length, 3);
|
||||
assert.deepEqual(
|
||||
Array.from(nodes, (node) => node.dataset.videoId),
|
||||
["video-b", "video-a", "video-c"],
|
||||
);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user