Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin loadByIdOrUUID doesn't load enough data for a video listing #6759

Open
ppom0 opened this issue Dec 7, 2024 · 0 comments
Open

plugin loadByIdOrUUID doesn't load enough data for a video listing #6759

ppom0 opened this issue Dec 7, 2024 · 0 comments
Labels
Component: PeerTube Plugin 📦 Features that can be developed in a plugin, but require PeerTube plugin API development Type: Feature Request ✨

Comments

@ppom0
Copy link

ppom0 commented Dec 7, 2024

Describe the problem to be solved

First thanks for providing this awesome software!


Context: I just wrote a plugin permitting to have a random listing of the videos.

I registered my handler on this hook: filter:api.videos.list.result.

I had to recreate the listing myself, getting rid of the provided one, because shuffling a paginated list is not a good random: each section of 25 videos will be shuffled, but they'll stay in the same global order.

So here's my attempt:

	registerHook({
		target: 'filter:api.videos.list.result',
		handler: async (result, params) => {
			if (!params.random) return result;

			// We use the current day as random seed
			const now = new Date();
			const seed = "0." + (now.getFullYear() * 365 + now.getMonth() * 31 + now.getDate());

			const q = await database.query(
				"SELECT setseed(:seed);" +
				"SELECT id FROM video ORDER BY random() OFFSET :start LIMIT :count;",
				{
					type: 'SELECT',
					replacements: {
						seed,
						start: params.start,
						count: params.count,
					}
				}
			);

			// Get ids of SQL request
			const ids = Object.values(q).filter(elt => elt.id).map(elt => elt.id);

			// Fetch all videos
			const thumbnails = await Promise.all(
				ids.map(id => videos.loadByIdOrUUID(id))
			);

			return { data: thumbnails };
		},
	});

Problem is that the client app requires channel and author attributes on those objects.

So for now I hard-coded the values, which is very hacky:

			// This code is inserted before the return
			thumbnails
				.forEach(elt => {
					elt.VideoChannel = {
						toFormattedSummaryJSON: function() {
							return {
								// Hardcoded data
							}
						}
					};

					elt.VideoChannel.Account = {
						toFormattedSummaryJSON: function() {
							return {
								// Hardcoded data
							}
						}
					};
				});

Describe the solution you would like

Either having a new loadByIdOrUUIDWithChannelAndAuthor or adding this data to the existing function would permit this!

What do you think?

@Chocobozzz Chocobozzz added Type: Feature Request ✨ Component: PeerTube Plugin 📦 Features that can be developed in a plugin, but require PeerTube plugin API development labels Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: PeerTube Plugin 📦 Features that can be developed in a plugin, but require PeerTube plugin API development Type: Feature Request ✨
Projects
None yet
Development

No branches or pull requests

2 participants