+import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Table,
UpdatedAt
} from 'sequelize-typescript'
-import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
-import { VideoPlaylistModel } from './video-playlist'
-import { getSort, throwIfNotValid } from '../utils'
-import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
+import validator from 'validator'
+import { MUserAccountId } from '@server/types/models'
+import {
+ MVideoPlaylistElement,
+ MVideoPlaylistElementAP,
+ MVideoPlaylistElementFormattable,
+ MVideoPlaylistElementVideoUrlPlaylistPrivacy,
+ MVideoPlaylistVideoThumbnail
+} from '@server/types/models/video/video-playlist-element'
import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
-import * as validator from 'validator'
-import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
-import { UserModel } from '../account/user'
+import { VideoPrivacy } from '../../../shared/models/videos'
import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
+import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
import { AccountModel } from '../account/account'
-import { VideoPrivacy } from '../../../shared/models/videos'
+import { getSort, throwIfNotValid } from '../utils'
+import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
+import { VideoPlaylistModel } from './video-playlist'
@Table({
tableName: 'videoPlaylistElement',
{
fields: [ 'videoId' ]
},
- {
- fields: [ 'videoPlaylistId', 'videoId' ],
- unique: true
- },
{
fields: [ 'url' ],
unique: true
}
]
})
-export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
+export class VideoPlaylistElementModel extends Model {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
- @AllowNull(false)
- @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
+ @AllowNull(true)
+ @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
url: string
}
static listForApi (options: {
- start: number,
- count: number,
- videoPlaylistId: number,
- serverAccount: AccountModel,
- user?: UserModel
+ start: number
+ count: number
+ videoPlaylistId: number
+ serverAccount: AccountModel
+ user?: MUserAccountId
}) {
const accountIds = [ options.serverAccount.id ]
const videoScope: (ScopeOptions | string)[] = [
]).then(([ total, data ]) => ({ total, data }))
}
- static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) {
+ static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
const query = {
where: {
videoPlaylistId,
return VideoPlaylistElementModel.findOne(query)
}
- static loadById (playlistElementId: number) {
+ static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
return VideoPlaylistElementModel.findByPk(playlistElementId)
}
- static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) {
+ static loadByPlaylistAndElementIdForAP (
+ playlistId: number | string,
+ playlistElementId: number
+ ): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
- const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
const query = {
include: [
},
{
attributes: [ 'url' ],
- model: VideoModel.unscoped(),
- where: videoWhere
+ model: VideoModel.unscoped()
}
- ]
+ ],
+ where: {
+ id: playlistElementId
+ }
}
return VideoPlaylistElementModel.findOne(query)
})
}
- static loadFirstElementWithVideoThumbnail (videoPlaylistId: number) {
+ static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistVideoThumbnail> {
const query = {
order: getSort('position'),
where: {
return VideoPlaylistElementModel.increment({ position: by }, query)
}
- getType (displayNSFW?: boolean, accountId?: number) {
+ getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
const video = this.Video
if (!video) return VideoPlaylistElementType.DELETED
// Owned video, don't filter it
if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
- if (video.privacy === VideoPrivacy.PRIVATE) return VideoPlaylistElementType.PRIVATE
+ // Internal video?
+ if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
+
+ if (video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL) return VideoPlaylistElementType.PRIVATE
if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
return VideoPlaylistElementType.REGULAR
}
- getVideoElement (displayNSFW?: boolean, accountId?: number) {
+ getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
if (!this.Video) return null
if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
return this.Video.toFormattedJSON()
}
- toFormattedJSON (options: { displayNSFW?: boolean, accountId?: number } = {}): VideoPlaylistElement {
+ toFormattedJSON (
+ this: MVideoPlaylistElementFormattable,
+ options: { displayNSFW?: boolean, accountId?: number } = {}
+ ): VideoPlaylistElement {
return {
id: this.id,
position: this.position,
}
}
- toActivityPubObject (): PlaylistElementObject {
+ toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
const base: PlaylistElementObject = {
id: this.url,
type: 'PlaylistElement',