Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Sort live session by startDate desc

Chocobozzz4d98273706ff
diff --git a/client/src/app/+videos-publish-manage/shared-manage/live-settings/live-stream-information.component.ts b/client/src/app/+videos-publish-manage/shared-manage/live-settings/live-stream-information.component.ts
index 6713ee8a92..8de99dc9fc 100644
--- a/client/src/app/+videos-publish-manage/shared-manage/live-settings/live-stream-information.component.ts
+++ b/client/src/app/+videos-publish-manage/shared-manage/live-settings/live-stream-information.component.ts
@@ -1,4 +1,3 @@
-
import { Component, inject, input, OnInit } from '@angular/core'
import { RouterLink } from '@angular/router'
import { Video } from '@app/shared/shared-main/video/video.model'
@@ -13,7 +12,7 @@ import { LiveVideoService } from '../../../shared/shared-video-live/live-video.s
imports: [
RouterLink,
PTDatePipe
-],
+ ],
providers: [ LiveVideoService ]
})
export class LiveStreamInformationComponent implements OnInit {
@@ -24,7 +23,7 @@ export class LiveStreamInformationComponent implements OnInit {
ngOnInit () {
this.liveVideoService.listSessions(this.video().id)
- .subscribe(({ data }) => this.latestLiveSessions = data.reverse())
+ .subscribe(({ data }) => this.latestLiveSessions = data)
}
getVideoUrl (video: { shortUUID: string }) {
diff --git a/client/src/app/shared/shared-video-live/live-video.service.ts b/client/src/app/shared/shared-video-live/live-video.service.ts
index 49909cf762..a7a009186e 100644
--- a/client/src/app/shared/shared-video-live/live-video.service.ts
+++ b/client/src/app/shared/shared-video-live/live-video.service.ts
@@ -1,5 +1,5 @@
import { catchError } from 'rxjs/operators'
-import { HttpClient } from '@angular/common/http'
+import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
import { RestExtractor } from '@app/core'
import { LiveVideo, LiveVideoCreate, LiveVideoSession, LiveVideoUpdate, ResultList, VideoCreateResult } from '@peertube/peertube-models'
@@ -26,8 +26,10 @@ export class LiveVideoService {
}
listSessions (videoId: number | string) {
+ const params = new HttpParams().set('sort', '-startDate')
+
return this.authHttp
- .get<ResultList<LiveVideoSession>>(LiveVideoService.BASE_VIDEO_LIVE_URL + videoId + '/sessions')
+ .get<ResultList<LiveVideoSession>>(LiveVideoService.BASE_VIDEO_LIVE_URL + videoId + '/sessions', { params })
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
diff --git a/packages/tests/src/api/check-params/live.ts b/packages/tests/src/api/check-params/live.ts
index 2fd3119066..4b093138b2 100644
--- a/packages/tests/src/api/check-params/live.ts
+++ b/packages/tests/src/api/check-params/live.ts
@@ -22,6 +22,7 @@ import {
setAccessTokensToServers,
stopFfmpeg
} from '@peertube/peertube-server-commands'
+import { checkBadSortPagination } from '@tests/shared/checks.js'
import { expect } from 'chai'
describe('Test video lives API validator', function () {
@@ -447,6 +448,10 @@ describe('Test video lives API validator', function () {
})
describe('When getting live sessions', function () {
+ it('Should fail with an incorrect sort', async function () {
+ await checkBadSortPagination(server.url, path, server.accessToken)
+ })
+
it('Should fail with a bad access token', async function () {
await command.listSessions({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
})
diff --git a/server/core/controllers/api/videos/live.ts b/server/core/controllers/api/videos/live.ts
index d2dfcecfe2..7bc65d7493 100644
--- a/server/core/controllers/api/videos/live.ts
+++ b/server/core/controllers/api/videos/live.ts
@@ -26,6 +26,7 @@ import {
videoLiveListSessionsValidator,
videoLiveUpdateValidator
} from '@server/middlewares/validators/videos/video-live.js'
+import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'
import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting.js'
import { VideoLiveScheduleModel } from '@server/models/video/video-live-schedule.js'
import { VideoLiveSessionModel } from '@server/models/video/video-live-session.js'
@@ -33,8 +34,14 @@ import { MVideoLive } from '@server/types/models/index.js'
import express from 'express'
import { Transaction } from 'sequelize'
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
-import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares/index.js'
-import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'
+import {
+ asyncMiddleware,
+ asyncRetryTransactionMiddleware,
+ authenticate,
+ liveSessionsSortValidator,
+ optionalAuthenticate,
+ setLiveSessionsSort
+} from '../../../middlewares/index.js'
const lTags = loggerTagsFactory('api', 'live')
@@ -53,9 +60,11 @@ liveRouter.post(
liveRouter.get(
'/live/:videoId/sessions',
authenticate,
+ liveSessionsSortValidator,
+ setLiveSessionsSort,
asyncMiddleware(videoLiveGetValidator),
asyncMiddleware(videoLiveListSessionsValidator),
- asyncMiddleware(getLiveVideoSessions)
+ asyncMiddleware(listLiveVideoSessions)
)
liveRouter.get(
@@ -99,10 +108,14 @@ function getLiveReplaySession (req: express.Request, res: express.Response) {
return res.json(session.toFormattedJSON())
}
-async function getLiveVideoSessions (req: express.Request, res: express.Response) {
+async function listLiveVideoSessions (req: express.Request, res: express.Response) {
const videoLive = res.locals.videoLive
- const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId, count: 100 })
+ const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({
+ videoId: videoLive.videoId,
+ count: 100,
+ sort: req.query.sort
+ })
return res.json(getFormattedObjects(data, data.length))
}
diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.ts
index ecd0f702ef..4c5fc8004a 100644
--- a/server/core/initializers/constants.ts
+++ b/server/core/initializers/constants.ts
@@ -163,7 +163,9 @@ export const SORTABLE_COLUMNS = {
VIDEO_REDUNDANCIES: [ 'name' ],
- VIDEO_CHANNEL_ACTIVITIES: [ 'createdAt' ]
+ VIDEO_CHANNEL_ACTIVITIES: [ 'createdAt' ],
+
+ LIVE_SESSIONS: [ 'startDate' ]
}
export const ROUTE_CACHE_LIFETIME = {
diff --git a/server/core/middlewares/sort.ts b/server/core/middlewares/sort.ts
index 77a532276d..408a8ebc68 100644
--- a/server/core/middlewares/sort.ts
+++ b/server/core/middlewares/sort.ts
@@ -1,23 +1,17 @@
import express from 'express'
-const setDefaultSort = setDefaultSortFactory('-createdAt')
-const setDefaultVideosSort = setDefaultSortFactory('-publishedAt')
+export const setDefaultSort = setDefaultSortFactory('-createdAt')
+export const setDefaultVideosSort = setDefaultSortFactory('-publishedAt')
-const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')
+export const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')
-const setDefaultSearchSort = setDefaultSortFactory('-match')
-const setBlacklistSort = setDefaultSortFactory('-createdAt')
+export const setDefaultSearchSort = setDefaultSortFactory('-match')
+export const setBlacklistSort = setDefaultSortFactory('-createdAt')
-// ---------------------------------------------------------------------------
-
-export {
- setDefaultSort,
- setDefaultSearchSort,
- setDefaultVideosSort,
- setDefaultVideoRedundanciesSort,
- setBlacklistSort
-}
+export const setLiveSessionsSort = setDefaultSortFactory('startDate')
+// ---------------------------------------------------------------------------
+// Private
// ---------------------------------------------------------------------------
function setDefaultSortFactory (sort: string) {
diff --git a/server/core/middlewares/validators/sort.ts b/server/core/middlewares/validators/sort.ts
index 915740f548..1416d05bd5 100644
--- a/server/core/middlewares/validators/sort.ts
+++ b/server/core/middlewares/validators/sort.ts
@@ -44,6 +44,8 @@ export const runnersSortValidator = checkSortFactory(SORTABLE_COLUMNS.RUNNERS)
export const runnerRegistrationTokensSortValidator = checkSortFactory(SORTABLE_COLUMNS.RUNNER_REGISTRATION_TOKENS)
export const runnerJobsSortValidator = checkSortFactory(SORTABLE_COLUMNS.RUNNER_JOBS)
+export const liveSessionsSortValidator = checkSortFactory(SORTABLE_COLUMNS.LIVE_SESSIONS)
+
// ---------------------------------------------------------------------------
function checkSortFactory (columns: string[], tags: string[] = []) {
diff --git a/server/core/models/video/video-live-session.ts b/server/core/models/video/video-live-session.ts
index ffd09eb4a1..a2be67832f 100644
--- a/server/core/models/video/video-live-session.ts
+++ b/server/core/models/video/video-live-session.ts
@@ -15,9 +15,9 @@ import {
Table,
UpdatedAt
} from 'sequelize-typescript'
+import { getSort, SequelizeModel } from '../shared/index.js'
import { VideoLiveReplaySettingModel } from './video-live-replay-setting.js'
import { VideoModel } from './video.js'
-import { SequelizeModel } from '../shared/index.js'
export enum ScopeNames {
WITH_REPLAY = 'WITH_REPLAY'
@@ -177,15 +177,16 @@ export class VideoLiveSessionModel extends SequelizeModel<VideoLiveSessionModel>
static listSessionsOfLiveForAPI (options: {
videoId: number
count: number
+ sort: string
}) {
- const { videoId, count } = options
+ const { videoId, count, sort } = options
const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
where: {
liveVideoId: videoId
},
limit: count,
- order: [ [ 'startDate', 'ASC' ] ]
+ order: getSort(sort)
}
return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yaml
index 04363c37a7..dcf6249d6f 100644
--- a/support/doc/api/openapi.yaml
+++ b/support/doc/api/openapi.yaml
@@ -3831,6 +3831,7 @@ paths:
- Live Videos
parameters:
- $ref: '#/components/parameters/idOrUUID'
+ - $ref: '#/components/parameters/sort'
responses:
'200':
description: successful operation