1
0
Fork 0
peertube/server/models/video/video-channel.ts

291 lines
6.5 KiB
TypeScript
Raw Normal View History

2017-10-24 17:41:09 +00:00
import * as Sequelize from 'sequelize'
2017-12-12 16:53:50 +00:00
import {
AfterDestroy,
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
HasMany,
Is,
IsUUID,
2017-12-14 09:07:57 +00:00
Model, Scopes,
2017-12-12 16:53:50 +00:00
Table,
UpdatedAt
} from 'sequelize-typescript'
import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
import { activityPubCollection } from '../../helpers'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
import { CONSTRAINTS_FIELDS } from '../../initializers'
import { getAnnounceActivityPubUrl } from '../../lib/activitypub'
import { sendDeleteVideoChannel } from '../../lib/activitypub/send'
import { AccountModel } from '../account/account'
import { ServerModel } from '../server/server'
import { getSort, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
import { VideoChannelShareModel } from './video-channel-share'
2017-12-14 09:07:57 +00:00
enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_VIDEOS = 'WITH_VIDEOS'
}
@Scopes({
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: () => AccountModel,
include: [ { model: () => ServerModel, required: false } ]
}
]
},
[ScopeNames.WITH_VIDEOS]: {
include: [
() => VideoModel
]
}
})
2017-12-12 16:53:50 +00:00
@Table({
tableName: 'videoChannel',
indexes: [
2017-10-24 17:41:09 +00:00
{
2017-12-12 16:53:50 +00:00
fields: [ 'accountId' ]
2017-10-24 17:41:09 +00:00
}
]
2017-12-12 16:53:50 +00:00
})
export class VideoChannelModel extends Model<VideoChannelModel> {
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(false)
@Default(DataType.UUIDV4)
@IsUUID(4)
@Column(DataType.UUID)
uuid: string
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(false)
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
@Column
name: string
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(true)
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
@Column
description: string
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(false)
@Column
remote: boolean
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AllowNull(false)
@Is('VideoChannelUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max))
url: string
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@CreatedAt
createdAt: Date
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@UpdatedAt
updatedAt: Date
2017-12-12 16:53:50 +00:00
@ForeignKey(() => AccountModel)
@Column
accountId: number
2017-12-12 16:53:50 +00:00
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Account: AccountModel
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@HasMany(() => VideoModel, {
2017-10-24 17:41:09 +00:00
foreignKey: {
2017-12-12 16:53:50 +00:00
name: 'channelId',
2017-10-24 17:41:09 +00:00
allowNull: false
},
onDelete: 'CASCADE'
})
2017-12-12 16:53:50 +00:00
Videos: VideoModel[]
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@HasMany(() => VideoChannelShareModel, {
2017-10-24 17:41:09 +00:00
foreignKey: {
name: 'channelId',
allowNull: false
},
onDelete: 'CASCADE'
})
2017-12-12 16:53:50 +00:00
VideoChannelShares: VideoChannelShareModel[]
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
@AfterDestroy
static sendDeleteIfOwned (instance: VideoChannelModel) {
if (instance.isOwned()) {
return sendDeleteVideoChannel(instance, undefined)
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
return undefined
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
static countByAccount (accountId: number) {
const query = {
where: {
accountId
}
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
return VideoChannelModel.count(query)
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
2017-12-14 09:07:57 +00:00
order: [ getSort(sort) ]
2017-12-12 16:53:50 +00:00
}
2017-10-24 17:41:09 +00:00
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findAndCountAll(query)
2017-12-12 16:53:50 +00:00
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
static listByAccount (accountId: number) {
const query = {
order: [ getSort('createdAt') ],
include: [
{
model: AccountModel,
where: {
id: accountId
},
required: true,
include: [ { model: ServerModel, required: false } ]
}
]
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
return VideoChannelModel.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
static loadByUrl (url: string, t?: Sequelize.Transaction) {
const query: IFindOptions<VideoChannelModel> = {
where: {
url
2017-12-14 09:07:57 +00:00
}
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
if (t !== undefined) query.transaction = t
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query)
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
static loadByUUIDOrUrl (uuid: string, url: string, t?: Sequelize.Transaction) {
const query: IFindOptions<VideoChannelModel> = {
where: {
[ Sequelize.Op.or ]: [
{ uuid },
{ url }
]
}
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
if (t !== undefined) query.transaction = t
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
return VideoChannelModel.findOne(query)
2017-11-10 13:34:45 +00:00
}
2017-12-12 16:53:50 +00:00
static loadByIdAndAccount (id: number, accountId: number) {
const options = {
where: {
id,
accountId
2017-12-14 09:07:57 +00:00
}
2017-11-10 16:27:49 +00:00
}
2017-12-12 16:53:50 +00:00
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
2017-11-10 13:34:45 +00:00
}
2017-12-12 16:53:50 +00:00
static loadAndPopulateAccount (id: number) {
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findById(id)
2017-12-12 16:53:50 +00:00
}
2017-11-10 13:34:45 +00:00
2017-12-12 16:53:50 +00:00
static loadByUUIDAndPopulateAccount (uuid: string) {
const options = {
where: {
uuid
2017-12-14 09:07:57 +00:00
}
2017-12-12 16:53:50 +00:00
}
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
static loadAndPopulateAccountAndVideos (id: number) {
const options = {
include: [
VideoModel
]
}
2017-10-24 17:41:09 +00:00
2017-12-14 09:07:57 +00:00
return VideoChannelModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]).findById(id, options)
2017-12-12 16:53:50 +00:00
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
isOwned () {
return this.remote === false
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
toFormattedJSON () {
const json = {
id: this.id,
uuid: this.uuid,
name: this.name,
description: this.description,
isLocal: this.isOwned(),
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
if (this.Account !== undefined) {
json[ 'owner' ] = {
name: this.Account.name,
uuid: this.Account.uuid
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
}
if (Array.isArray(this.Videos)) {
json[ 'videos' ] = this.Videos.map(v => v.toFormattedJSON())
}
return json
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
toActivityPubObject () {
let sharesObject
if (Array.isArray(this.VideoChannelShares)) {
const shares: string[] = []
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
for (const videoChannelShare of this.VideoChannelShares) {
const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account)
shares.push(shareUrl)
2017-10-24 17:41:09 +00:00
}
2017-12-12 16:53:50 +00:00
sharesObject = activityPubCollection(shares)
}
2017-10-24 17:41:09 +00:00
2017-12-12 16:53:50 +00:00
return {
type: 'VideoChannel' as 'VideoChannel',
id: this.url,
uuid: this.uuid,
content: this.description,
name: this.name,
published: this.createdAt.toISOString(),
updated: this.updatedAt.toISOString(),
shares: sharesObject
}
2017-10-24 17:41:09 +00:00
}
}