Skip to content

Commit

Permalink
Add channelUpdatedAt list subscriptions sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 12, 2025
1 parent 56419fd commit e002b6f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
12 changes: 11 additions & 1 deletion packages/tests/src/api/users/user-subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('Test users subscriptions', function () {
command = servers[0].subscriptions
})

describe('Destinction between server videos and user videos', function () {
describe('Distinction between server videos and user videos', function () {

it('Should display videos of server 2 on server 1', async function () {
const { total } = await servers[0].videos.list()

Expand Down Expand Up @@ -161,6 +162,15 @@ describe('Test users subscriptions', function () {
expect(body.data).to.have.lengthOf(0)
}
})

it('Should sort subscriptions by channelUpdatedAt', async function () {
const body = await command.list({ token: users[0].accessToken, sort: '-channelUpdatedAt' })
expect(body.total).to.equal(2)

const subscriptions = body.data
expect(subscriptions[0].name).to.equal('user3_channel')
expect(subscriptions[1].name).to.equal('root_channel')
})
})

describe('Subscription videos', function () {
Expand Down
4 changes: 2 additions & 2 deletions server/core/controllers/api/users/my-subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mySubscriptionsRouter.get('/me/subscriptions',
setDefaultSort,
setDefaultPagination,
userSubscriptionListValidator,
asyncMiddleware(getUserSubscriptions)
asyncMiddleware(listUserSubscriptions)
)

mySubscriptionsRouter.post('/me/subscriptions',
Expand Down Expand Up @@ -151,7 +151,7 @@ async function deleteUserSubscription (req: express.Request, res: express.Respon
.end()
}

async function getUserSubscriptions (req: express.Request, res: express.Response) {
async function listUserSubscriptions (req: express.Request, res: express.Response) {
const user = res.locals.oauth.token.User
const actorId = user.Account.Actor.id

Expand Down
2 changes: 1 addition & 1 deletion server/core/initializers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const WEBSERVER = {
// Sortable columns per schema
export const SORTABLE_COLUMNS = {
ADMIN_USERS: [ 'id', 'username', 'videoQuotaUsed', 'createdAt', 'lastLoginDate', 'role' ],
USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
USER_SUBSCRIPTIONS: [ 'id', 'createdAt', 'channelUpdatedAt' ],
ACCOUNTS: [ 'createdAt' ],
JOBS: [ 'createdAt' ],
VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
Expand Down
6 changes: 3 additions & 3 deletions server/core/models/actor/actor-follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from '../../initializers/constants.js'
import { AccountModel } from '../account/account.js'
import { ServerModel } from '../server/server.js'
import { SequelizeModel, buildSQLAttributes, createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../shared/index.js'
import { SequelizeModel, buildSQLAttributes, createSafeIn, getSubscriptionSort, searchAttribute, throwIfNotValid } from '../shared/index.js'
import { doesExist } from '../shared/query.js'
import { VideoChannelModel } from '../video/video-channel.js'
import { ActorModel, unusedActorAttributesForAPI } from './actor.js'
Expand Down Expand Up @@ -428,11 +428,11 @@ export class ActorFollowModel extends SequelizeModel<ActorFollowModel> {
return {
attributes: forCount === true
? []
: SORTABLE_COLUMNS.USER_SUBSCRIPTIONS,
: SORTABLE_COLUMNS.USER_SUBSCRIPTIONS.filter(s => s !== 'channelUpdatedAt'),
distinct: true,
offset: start,
limit: count,
order: getSort(sort),
order: getSubscriptionSort(sort),
where,
include: [
{
Expand Down
38 changes: 19 additions & 19 deletions server/core/models/shared/sort.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { literal, OrderItem, Sequelize } from 'sequelize'

// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
export function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

let finalField: string | ReturnType<typeof Sequelize.col>
Expand All @@ -15,7 +15,7 @@ function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderIt
return [ [ finalField, direction ], lastSort ]
}

function getAdminUsersSort (value: string): OrderItem[] {
export function getAdminUsersSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

let finalField: string | ReturnType<typeof Sequelize.col>
Expand All @@ -34,7 +34,7 @@ function getAdminUsersSort (value: string): OrderItem[] {
return [ [ finalField as any, direction, nullPolicy ], [ 'id', 'ASC' ] ]
}

function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
export function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

if (field.toLowerCase() === 'name') {
Expand All @@ -44,7 +44,7 @@ function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]):
return getSort(value, lastSort)
}

function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
export function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

if (field.toLowerCase() === 'trending') { // Sort by aggregation
Expand Down Expand Up @@ -81,7 +81,7 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or
return [ firstSort, lastSort ]
}

function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
export function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

const videoFields = new Set([ 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid' ])
Expand All @@ -96,7 +96,7 @@ function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ])
return getSort(value, lastSort)
}

function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
export function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

if (field === 'redundancyAllowed') {
Expand All @@ -109,7 +109,7 @@ function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'A
return getSort(value, lastSort)
}

function getChannelSyncSort (value: string): OrderItem[] {
export function getChannelSyncSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'videochannel') {
return [
Expand All @@ -119,7 +119,18 @@ function getChannelSyncSort (value: string): OrderItem[] {
return [ [ field, direction ] ]
}

function buildSortDirectionAndField (value: string) {
export function getSubscriptionSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)

if (field === 'channelUpdatedAt') {
return [
[ literal('"ActorFollowing.VideoChannel.updatedAt"'), direction ]
]
}
return [ [ field, direction ] ]
}

export function buildSortDirectionAndField (value: string) {
let field: string
let direction: 'ASC' | 'DESC'

Expand All @@ -133,14 +144,3 @@ function buildSortDirectionAndField (value: string) {

return { direction, field }
}

export {
buildSortDirectionAndField,
getPlaylistSort,
getSort,
getAdminUsersSort,
getVideoSort,
getBlacklistSort,
getChannelSyncSort,
getInstanceFollowsSort
}

0 comments on commit e002b6f

Please sign in to comment.