ActivityPub-federated video streaming platform using P2P directly in your web browser
Correctly sort videos feeds
Chocobozzz8d52434c334d
diff --git a/packages/tests/src/feeds/feeds.ts b/packages/tests/src/feeds/feeds.tsindex f4dfebe308..439496b396 100644--- a/packages/tests/src/feeds/feeds.ts+++ b/packages/tests/src/feeds/feeds.ts@@ -1,6 +1,6 @@/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */-import { HttpStatusCode, VideoCommentPolicy, VideoPrivacy } from '@peertube/peertube-models'+import { HttpStatusCode, VideoCommentPolicy, VideoCreateResult, VideoPrivacy } from '@peertube/peertube-models'import {PeerTubeServer,PluginsCommand,@@ -421,10 +421,27 @@ describe('Test syndication feeds', () => {const podcastUrlEl = xmlDoc.rss.channel['podcast:txt']expect(podcastUrlEl).to.not.exist})++ it('Should sort by originallyPublishedAt', async function () {+ const { uuid } = await servers[0].videos.upload({ attributes: { name: 'last video', originallyPublishedAt: '1970-01-01' } })++ const xmlDoc = parseXMLFeed(+ await servers[0].feed.getPodcastXML({ ignoreCache: false, channelId: serverHLSOnly.store.channel.id })+ )++ expect(xmlDoc.rss.channel.item.map(i => i.title)).to.deep.equal([ 'my super name for server 1', 'last video' ])++ await servers[0].videos.remove({ id: uuid })+ })})describe('Playlist podcast feed', function () {let xmlDoc: any+ let newVideo: VideoCreateResult++ before(async function () {+ newVideo = await servers[0].videos.quickUpload({ name: 'new video for playlist' })+ })it('Should contain a valid podcast enclosures', async function () {xmlDoc = parseXMLFeed(@@ -451,9 +468,7 @@ describe('Test syndication feeds', () => {const rss = await servers[0].feed.getPodcastXML({ ignoreCache: false, playlistId: podcastPlaylistId })const xmlDoc = parseXMLFeed(rss)- const items = Array.isArray(xmlDoc.rss.channel.item)- ? xmlDoc.rss.channel.item- : [ xmlDoc.rss.channel.item ]+ const items = xmlDoc.rss.channel.itemexpect(items.length).to.equal(2)expect(items.some((i: any) => i.title === 'unlisted video for playlist')).to.equal(true)@@ -505,32 +520,55 @@ describe('Test syndication feeds', () => {)})- it('Should invalidate the podcast cache if an element is added or updated', async function () {- const newVideo = await servers[0].videos.quickUpload({ name: 'new video for playlist' })+ it('Should sort by playlist position', async function () {await servers[0].playlists.addElement({ playlistId: podcastPlaylistId, attributes: { videoId: newVideo.id } })+ {+ const xmlDoc = parseXMLFeed(+ await servers[0].feed.getPodcastXML({ ignoreCache: false, playlistId: podcastPlaylistId })+ )++ expect(xmlDoc.rss.channel.item.map(i => i.title)).to.deep.equal([ 'new video for playlist', 'my super name for server 1' ])+ }++ // Last element becomes the first+ await servers[0].playlists.reorderElements({+ playlistId: podcastPlaylistId,+ attributes: { startPosition: 4, insertAfterPosition: 0 }+ })++ {+ const xmlDoc = parseXMLFeed(+ await servers[0].feed.getPodcastXML({ ignoreCache: true, playlistId: podcastPlaylistId })+ )++ expect(xmlDoc.rss.channel.item.map(i => i.title)).to.deep.equal([ 'my super name for server 1', 'new video for playlist' ])+ }+ })++ it('Should invalidate the podcast cache if an element is added or updated', async function () {const getItem = async () => {const xmlDoc = parseXMLFeed(await servers[0].feed.getPodcastXML({ ignoreCache: false, playlistId: podcastPlaylistId }))- const items = Array.isArray(xmlDoc.rss.channel.item)- ? xmlDoc.rss.channel.item- : [ xmlDoc.rss.channel.item ]+ const items = xmlDoc.rss.channel.itemexpect(items.length).to.equal(2)- return items[0]+ return items[1]}expect((await getItem()).title).to.equal('new video for playlist')await servers[0].videos.update({ id: newVideo.id, attributes: { name: 'updated name for new video' } })expect((await getItem()).title).to.equal('updated name for new video')+ })+ after(async function () {await servers[0].videos.remove({ id: newVideo.id })- await waitJobs(servers)+ await waitJobs(servers[0])})})@@ -934,29 +972,29 @@ describe('Test syndication feeds', () => {})describe('Video feed from my subscriptions', function () {- let feeduserAccountId: number- let feeduserFeedToken: string+ let feedUserAccountId: number+ let feedUserFeedToken: stringit('Should list no videos for a user with no videos and no subscriptions', async function () {const attr = { username: 'feeduser', password: 'password' }await servers[0].users.create({ username: attr.username, password: attr.password })- const feeduserAccessToken = await servers[0].login.getAccessToken(attr)+ const feedUserAccessToken = await servers[0].login.getAccessToken(attr){- const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })- feeduserAccountId = user.account.id+ const user = await servers[0].users.getMyInfo({ token: feedUserAccessToken })+ feedUserAccountId = user.account.id}{- const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })- feeduserFeedToken = token.feedToken+ const token = await servers[0].users.getMyScopedTokens({ token: feedUserAccessToken })+ feedUserFeedToken = token.feedToken}{- const body = await servers[0].videos.listMySubscriptionVideos({ token: feeduserAccessToken })+ const body = await servers[0].videos.listMySubscriptionVideos({ token: feedUserAccessToken })expect(body.total).to.equal(0)- const query = { accountId: feeduserAccountId, token: feeduserFeedToken }+ const query = { accountId: feedUserAccountId, token: feedUserFeedToken }const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })const jsonObj = JSON.parse(json)expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos@@ -964,12 +1002,12 @@ describe('Test syndication feeds', () => {})it('Should fail with an invalid token', async function () {- const query = { accountId: feeduserAccountId, token: 'toto' }+ const query = { accountId: feedUserAccountId, token: 'toto' }await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })})it('Should fail with a token of another user', async function () {- const query = { accountId: feeduserAccountId, token: userFeedToken }+ const query = { accountId: feedUserAccountId, token: userFeedToken }await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })})diff --git a/server/core/controllers/feeds/shared/video-feed-utils.ts b/server/core/controllers/feeds/shared/video-feed-utils.tsindex 19e516ea77..c4d5c2c2a0 100644--- a/server/core/controllers/feeds/shared/video-feed-utils.ts+++ b/server/core/controllers/feeds/shared/video-feed-utils.ts@@ -62,7 +62,7 @@ export function getCommonVideoFeedAttributes (video: VideoModel) {description: mdToPlainText(video.getTruncatedDescription()),content: toSafeHtml(video.description),- date: video.publishedAt,+ date: video.originallyPublishedAt || video.publishedAt,nsfw: video.nsfw,category: video.categorydiff --git a/server/core/controllers/feeds/video-feeds.ts b/server/core/controllers/feeds/video-feeds.tsindex 4e755d2ed0..9d5eb25fde 100644--- a/server/core/controllers/feeds/video-feeds.ts+++ b/server/core/controllers/feeds/video-feeds.ts@@ -13,7 +13,6 @@ import {commonVideosFiltersValidatorFactory,feedsAccountOrChannelFiltersValidator,feedsFormatValidator,- setDefaultVideosSort,setFeedFormatContentType,videosSortValidator,videoSubscriptionFeedsValidator@@ -38,7 +37,6 @@ const { middleware: cacheRouteMiddleware } = cacheRouteFactory({videoFeedsRouter.get('/videos.:format',videosSortValidator,- setDefaultVideosSort,feedsFormatValidator,setFeedFormatContentType,cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),@@ -50,7 +48,6 @@ videoFeedsRouter.get(videoFeedsRouter.get('/subscriptions.:format',videosSortValidator,- setDefaultVideosSort,feedsFormatValidator,setFeedFormatContentType,cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),@@ -97,7 +94,7 @@ async function generateVideoFeed (req: express.Request, res: express.Response) {const data = await getVideosForFeeds({...buildNSFWFilters({ req, res }),- sort: req.query.sort,+ sort: req.query.sort || '-originallyPublishedAt',isLocal: req.query.isLocal,include: req.query.include | VideoInclude.FILES,accountId: account?.id,@@ -127,7 +124,7 @@ async function generateVideoFeedForSubscriptions (req: express.Request, res: expconst data = await getVideosForFeeds({...buildNSFWFilters({ req, res }),- sort: req.query.sort,+ sort: req.query.sort || '-publishedAt',isLocal: req.query.isLocal,include: req.query.include | VideoInclude.FILES,displayOnlyForFollower: {diff --git a/server/core/controllers/feeds/video-podcast-feeds.ts b/server/core/controllers/feeds/video-podcast-feeds.tsindex 8ab3afc48a..086362e6ab 100644--- a/server/core/controllers/feeds/video-podcast-feeds.ts+++ b/server/core/controllers/feeds/video-podcast-feeds.ts@@ -113,7 +113,7 @@ async function generateChannelPodcastFeed (req: express.Request, res: express.Reconst data = await getVideosForFeeds({...nsfwOptions,- sort: '-publishedAt',+ sort: '-originallyPublishedAt',// Prevent podcast feeds from listing videos in other instances// helps prevent duplicates when they are indexed -- only the author should control them@@ -181,7 +181,7 @@ async function generatePlaylistPodcastFeed (req: express.Request, res: express.Rconst data = await getVideosForFeeds({...nsfwOptions,- sort: '-publishedAt',+ sort: '-playlistElementPosition',// Prevent podcast feeds from listing videos in other instances// helps prevent duplicates when they are indexed -- only the author should control themdiff --git a/server/core/models/video/sql/video/videos-id-list-query-builder.ts b/server/core/models/video/sql/video/videos-id-list-query-builder.tsindex 11737f8fd6..65d6442997 100644--- a/server/core/models/video/sql/video/videos-id-list-query-builder.ts+++ b/server/core/models/video/sql/video/videos-id-list-query-builder.ts@@ -446,8 +446,8 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {private joinPlaylist (playlistId: number) {this.joins.push(- 'INNER JOIN "videoPlaylistElement" ON "video"."id" = "videoPlaylistElement"."videoId" ' +- 'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'+ 'INNER JOIN "videoPlaylistElement" "VideoPlaylistElement" ON "video"."id" = "VideoPlaylistElement"."videoId" ' ++ 'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId')this.replacements.videoPlaylistId = playlistId@@ -1016,10 +1016,6 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {}private setSort (column: string, direction: 'ASC' | 'DESC') {- this.sort = this.buildOrder(column, direction)- }-- private buildOrder (column: string, direction: 'ASC' | 'DESC') {if (column.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + column)if (column === 'random') return 'ORDER BY RANDOM()'@@ -1037,13 +1033,15 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {firstSort = '"publishedAtForOrder"'} else if (column === 'localVideoFilesSize') {firstSort = '"localVideoFilesSize"'+ } else if (column === 'playlistElementPosition') {+ firstSort = '"VideoPlaylistElement"."position"'} else if (column.includes('.')) {firstSort = column} else {firstSort = `"video"."${column}"`}- return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`+ this.sort = `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`}private setLimit (countArg: number) {