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,
Table,
UpdatedAt
} from 'sequelize-typescript'
-import { v4 as uuidv4 } from 'uuid'
+import { setAsUpdated } from '@server/helpers/database-utils'
+import { buildUUID, uuidToShort } from '@server/helpers/uuid'
import { MAccountId, MChannelId } from '@server/types/models'
+import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistLink, buildPlaylistWatchPath } 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'
MVideoPlaylistIdWithElements
} from '../../types/models/video/video-playlist'
import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account'
-import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getPlaylistSort, isOutdated, throwIfNotValid } from '../utils'
+import { ActorModel } from '../actor/actor'
+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'
-import { ActorModel } from '../activitypub/actor'
enum ScopeNames {
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
videoChannelId?: number
listMyPlaylists?: boolean
search?: string
+ withVideos?: boolean
+}
+
+function getVideoLengthSelect () {
+ return 'SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"'
}
@Scopes(() => ({
attributes: {
include: [
[
- literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
+ literal(`(${getVideoLengthSelect()})`),
'videosLength'
]
]
})
}
+ if (options.withVideos === true) {
+ whereAnd.push(
+ literal(`(${getVideoLengthSelect()}) != 0`)
+ )
+ }
+
+ const attributesInclude = []
+
if (options.search) {
+ const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search)
+ const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%')
+ attributesInclude.push(createSimilarityAttribute('VideoPlaylistModel.name', options.search))
+
whereAnd.push({
- name: {
- [Op.iLike]: '%' + options.search + '%'
- }
+ [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 + '))'
+ )
+ ]
})
}
}
return {
+ attributes: {
+ include: attributesInclude
+ },
where,
include: [
{
@Table({
tableName: 'videoPlaylist',
indexes: [
+ buildTrigramSearchIndex('video_playlist_name_trigram', 'name'),
+
{
fields: [ 'ownerAccountId' ]
},
}
]
})
-export class VideoPlaylistModel extends Model {
+export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlaylistModel>>> {
@CreatedAt
createdAt: Date
videoChannelId?: number
listMyPlaylists?: boolean
search?: string
+ withVideos?: boolean // false by default
}) {
const query = {
offset: options.start,
accountId: options.accountId,
videoChannelId: options.videoChannelId,
listMyPlaylists: options.listMyPlaylists,
- search: options.search
+ search: options.search,
+ withVideos: options.withVideos || false
} as AvailableForListOptions
]
},
})
}
+ static searchForApi (options: {
+ followerActorId: number
+ start: number
+ count: number
+ sort: string
+ search?: 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
return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
}
+ static loadByUrlWithAccountAndChannelSummary (url: string): Promise<MVideoPlaylistFullSummary> {
+ 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'
}
generateThumbnailName () {
const extension = '.jpg'
- return 'playlist-' + uuidv4() + extension
+ return 'playlist-' + buildUUID() + extension
}
getThumbnailUrl () {
return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
}
- getWatchUrl () {
- return WEBSERVER.URL + '/videos/watch/playlist/' + this.uuid
+ getWatchStaticPath () {
+ return buildPlaylistWatchPath({ shortUUID: uuidToShort(this.uuid) })
}
getEmbedStaticPath () {
- return '/video-playlists/embed/' + this.uuid
+ return buildPlaylistEmbedPath(this)
}
static async getStats () {
}
setAsRefreshed () {
- this.changed('updatedAt', true)
+ return setAsUpdated('videoPlaylist', this.id)
+ }
- return this.save()
+ setVideosLength (videosLength: number) {
+ this.set('videosLength' as any, videosLength, { raw: true })
}
isOwned () {
return {
id: this.id,
uuid: this.uuid,
+ shortUUID: uuidToShort(this.uuid),
+
isLocal: this.isOwned(),
+ url: this.url,
+
displayName: this.name,
description: this.description,
privacy: {