X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-playlist.ts;h=630684a88350d04f5ffe9d20026c5ecdf5622144;hb=9ea02c48a7a47b5bbed261e847d7d671e266a073;hp=98cea1b64f21fabfeadd580b44062f6b33b04690;hpb=4d7ce9218a3f695bf3d013cbdce1c5c6a5221927;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 98cea1b64..630684a88 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -1,5 +1,5 @@ import { join } from 'path' -import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, literal, Op, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -17,9 +17,9 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { v4 as uuidv4 } from 'uuid' +import { buildUUID, uuidToShort } from '@server/helpers/uuid' import { MAccountId, MChannelId } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' +import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistWatchPath, pick } from '@shared/core-utils' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' @@ -52,7 +52,16 @@ import { } from '../../types/models/video/video-playlist' import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' import { ActorModel } from '../actor/actor' -import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getPlaylistSort, isOutdated, throwIfNotValid } from '../utils' +import { setAsUpdated } from '../shared' +import { + buildServerIdsFollowedBy, + buildTrigramSearchIndex, + buildWhereIdOrUUID, + createSimilarityAttribute, + getPlaylistSort, + isOutdated, + throwIfNotValid +} from '../utils' import { ThumbnailModel } from './thumbnail' import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' import { VideoPlaylistElementModel } from './video-playlist-element' @@ -73,6 +82,13 @@ type AvailableForListOptions = { videoChannelId?: number listMyPlaylists?: boolean search?: string + host?: string + uuids?: string[] + withVideos?: boolean +} + +function getVideoLengthSelect () { + return 'SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"' } @Scopes(() => ({ @@ -88,7 +104,7 @@ type AvailableForListOptions = { attributes: { include: [ [ - literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), + literal(`(${getVideoLengthSelect()})`), 'videosLength' ] ] @@ -127,9 +143,19 @@ type AvailableForListOptions = { ] }, [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { + const whereAnd: WhereOptions[] = [] + + const whereServer = options.host && options.host !== WEBSERVER.HOST + ? { host: options.host } + : undefined + let whereActor: WhereOptions = {} - const whereAnd: WhereOptions[] = [] + if (options.host === WEBSERVER.HOST) { + whereActor = { + [Op.and]: [ { serverId: null } ] + } + } if (options.listMyPlaylists !== true) { whereAnd.push({ @@ -154,9 +180,7 @@ type AvailableForListOptions = { }) } - whereActor = { - [Op.or]: whereActorOr - } + Object.assign(whereActor, { [Op.or]: whereActorOr }) } if (options.accountId) { @@ -177,24 +201,52 @@ type AvailableForListOptions = { }) } - if (options.search) { + if (options.uuids) { whereAnd.push({ - name: { - [Op.iLike]: '%' + options.search + '%' + uuid: { + [Op.in]: options.uuids } }) } + if (options.withVideos === true) { + whereAnd.push( + literal(`(${getVideoLengthSelect()}) != 0`) + ) + } + + let attributesInclude: any[] = [ literal('0 as similarity') ] + + if (options.search) { + const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search) + const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%') + attributesInclude = [ createSimilarityAttribute('VideoPlaylistModel.name', options.search) ] + + whereAnd.push({ + [Op.or]: [ + Sequelize.literal( + 'lower(immutable_unaccent("VideoPlaylistModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' + ), + Sequelize.literal( + 'lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + ) + ] + }) + } + const where = { [Op.and]: whereAnd } return { + attributes: { + include: attributesInclude + }, where, include: [ { model: AccountModel.scope({ - method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] + method: [ AccountScopeNames.SUMMARY, { whereActor, whereServer } as SummaryOptions ] }), required: true }, @@ -210,6 +262,8 @@ type AvailableForListOptions = { @Table({ tableName: 'videoPlaylist', indexes: [ + buildTrigramSearchIndex('video_playlist_name_trigram', 'name'), + { fields: [ 'ownerAccountId' ] }, @@ -303,16 +357,10 @@ export class VideoPlaylistModel extends Model & { + start: number + count: number + sort: string + }) { + return VideoPlaylistModel.listForApi({ + ...options, + + type: VideoPlaylistType.REGULAR, + listMyPlaylists: false, + withVideos: true + }) + } + static listPublicUrlsOfForAP (options: { account?: MAccountId, channel?: MChannelId }, start: number, count: number) { const where = { privacy: VideoPlaylistPrivacy.PUBLIC @@ -444,6 +503,18 @@ export class VideoPlaylistModel extends Model { + const query = { + where: { + url + } + } + + return VideoPlaylistModel + .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) + .findOne(query) + } + static getPrivacyLabel (privacy: VideoPlaylistPrivacy) { return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown' } @@ -480,7 +551,7 @@ export class VideoPlaylistModel extends Model