+import { join } from 'path'
+import { FindOptions, literal, Op, ScopeOptions, 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 { MAccountId, MChannelId } from '@server/types/models'
+import { AttributesOnly } 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'
-import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils'
+import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model'
+import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model'
+import { activityPubCollectionPagination } from '../../helpers/activitypub'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import {
isVideoPlaylistDescriptionValid,
isVideoPlaylistNameValid,
isVideoPlaylistPrivacyValid
} from '../../helpers/custom-validators/video-playlists'
-import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import {
ACTIVITY_PUB,
CONSTRAINTS_FIELDS,
VIDEO_PLAYLIST_TYPES,
WEBSERVER
} from '../../initializers/constants'
-import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model'
-import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account'
-import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
-import { join } from 'path'
-import { VideoPlaylistElementModel } from './video-playlist-element'
-import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object'
-import { activityPubCollectionPagination } from '../../helpers/activitypub'
-import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model'
-import { ThumbnailModel } from './thumbnail'
-import { ActivityIconObject } from '../../../shared/models/activitypub/objects'
-import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
-import * as Bluebird from 'bluebird'
+import { MThumbnail } from '../../types/models/video/thumbnail'
import {
MVideoPlaylistAccountThumbnail,
MVideoPlaylistAP,
MVideoPlaylistFullSummary,
MVideoPlaylistIdWithElements
} from '../../types/models/video/video-playlist'
-import { MThumbnail } from '../../types/models/video/thumbnail'
-import { MAccountId, MChannelId } from '@server/types/models'
+import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account'
+import { ActorModel } from '../actor/actor'
+import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getPlaylistSort, isOutdated, throwIfNotValid } from '../utils'
+import { ThumbnailModel } from './thumbnail'
+import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
+import { VideoPlaylistElementModel } from './video-playlist-element'
enum ScopeNames {
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
}
type AvailableForListOptions = {
- followerActorId: number
+ followerActorId?: number
type?: VideoPlaylistType
accountId?: number
videoChannelId?: number
]
},
[ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
-
let whereActor: WhereOptions = {}
const whereAnd: WhereOptions[] = []
privacy: VideoPlaylistPrivacy.PUBLIC
})
- // Only list local playlists OR playlists that are on an instance followed by actorId
- const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
+ // Only list local playlists
+ const whereActorOr: WhereOptions[] = [
+ {
+ serverId: null
+ }
+ ]
- whereActor = {
- [Op.or]: [
- {
- serverId: null
- },
- {
- serverId: {
- [Op.in]: literal(inQueryInstanceFollow)
- }
+ // … OR playlists that are on an instance followed by actorId
+ if (options.followerActorId) {
+ const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
+
+ whereActorOr.push({
+ serverId: {
+ [Op.in]: literal(inQueryInstanceFollow)
}
- ]
+ })
+ }
+
+ whereActor = {
+ [Op.or]: whereActorOr
}
}
[Op.and]: whereAnd
}
- const accountScope = {
- method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ]
- }
-
return {
where,
include: [
{
- model: AccountModel.scope(accountScope),
+ model: AccountModel.scope({
+ method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ]
+ }),
required: true
},
{
}
]
})
-export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
+export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlaylistModel>>> {
@CreatedAt
createdAt: Date
const query = {
offset: options.start,
limit: options.count,
- order: getSort(options.sort)
+ order: getPlaylistSort(options.sort)
}
const scopes: (string | ScopeOptions)[] = [
})
}
- static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> {
+ static listPlaylistIdsOf (accountId: number, videoIds: number[]): Promise<MVideoPlaylistIdWithElements[]> {
const query = {
attributes: [ 'id' ],
where: {
static doesPlaylistExist (url: string) {
const query = {
- attributes: [],
+ attributes: [ 'id' ],
where: {
url
}
.then(e => !!e)
}
- static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> {
+ static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFullSummary> {
const where = buildWhereIdOrUUID(id)
const query = {
.findOne(query)
}
- static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> {
+ static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFull> {
const where = buildWhereIdOrUUID(id)
const query = {
.findOne(query)
}
- static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> {
+ static loadByUrlAndPopulateAccount (url: string): Promise<MVideoPlaylistAccountThumbnail> {
const query = {
where: {
url
generateThumbnailName () {
const extension = '.jpg'
- return 'playlist-' + this.uuid + extension
+ return 'playlist-' + uuidv4() + extension
}
getThumbnailUrl () {
}
getWatchUrl () {
- return WEBSERVER.URL + '/videos/watch/playlist/' + this.uuid
+ return WEBSERVER.URL + '/w/p/' + this.uuid
}
getEmbedStaticPath () {
return '/video-playlists/embed/' + this.uuid
}
- setAsRefreshed () {
- this.changed('updatedAt', true)
+ static async getStats () {
+ const totalLocalPlaylists = await VideoPlaylistModel.count({
+ include: [
+ {
+ model: AccountModel,
+ required: true,
+ include: [
+ {
+ model: ActorModel,
+ required: true,
+ where: {
+ serverId: null
+ }
+ }
+ ]
+ }
+ ],
+ where: {
+ privacy: VideoPlaylistPrivacy.PUBLIC
+ }
+ })
- return this.save()
+ return {
+ totalLocalPlaylists
+ }
+ }
+
+ setAsRefreshed () {
+ return setAsUpdated('videoPlaylist', this.id)
}
isOwned () {