PeerTube/server/models/video/video-channel.ts

357 lines
8.8 KiB
TypeScript
Raw Normal View History

2017-10-24 19:41:09 +02:00
import * as Sequelize from 'sequelize'
2017-11-20 09:43:39 +01:00
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers'
2017-11-14 10:57:56 +01:00
import { isVideoChannelUrlValid } from '../../helpers/custom-validators/video-channels'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
2017-11-20 09:43:39 +01:00
import { sendDeleteVideoChannel } from '../../lib/activitypub/send/send-delete'
import { addMethodsToModel, getSort } from '../utils'
import { VideoChannelAttributes, VideoChannelInstance, VideoChannelMethods } from './video-channel-interface'
2017-10-24 19:41:09 +02:00
let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
2017-11-09 17:51:58 +01:00
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
2017-10-24 19:41:09 +02:00
let isOwned: VideoChannelMethods.IsOwned
2017-11-09 17:51:58 +01:00
let countByAccount: VideoChannelMethods.CountByAccount
2017-10-24 19:41:09 +02:00
let listForApi: VideoChannelMethods.ListForApi
2017-11-09 17:51:58 +01:00
let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
2017-10-24 19:41:09 +02:00
let loadByUUID: VideoChannelMethods.LoadByUUID
2017-11-09 17:51:58 +01:00
let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
2017-10-24 19:41:09 +02:00
let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
2017-11-09 17:51:58 +01:00
let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
2017-11-10 14:34:45 +01:00
let loadByUrl: VideoChannelMethods.LoadByUrl
let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
2017-10-24 19:41:09 +02:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoChannel = sequelize.define<VideoChannelInstance, VideoChannelAttributes>('VideoChannel',
{
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
validate: {
isUUID: 4
}
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
nameValid: value => {
const res = isVideoChannelNameValid(value)
if (res === false) throw new Error('Video channel name is not valid.')
}
}
},
description: {
type: DataTypes.STRING,
allowNull: true,
validate: {
descriptionValid: value => {
const res = isVideoChannelDescriptionValid(value)
if (res === false) throw new Error('Video channel description is not valid.')
}
}
},
remote: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
2017-11-09 17:51:58 +01:00
},
url: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
2017-11-14 10:57:56 +01:00
urlValid: value => {
const res = isVideoChannelUrlValid(value)
if (res === false) throw new Error('Video channel URL is not valid.')
}
2017-11-09 17:51:58 +01:00
}
2017-10-24 19:41:09 +02:00
}
},
{
indexes: [
{
2017-11-09 17:51:58 +01:00
fields: [ 'accountId' ]
2017-10-24 19:41:09 +02:00
}
],
hooks: {
afterDestroy
}
}
)
const classMethods = [
associate,
listForApi,
2017-11-09 17:51:58 +01:00
listByAccount,
loadByIdAndAccount,
loadAndPopulateAccount,
loadByUUIDAndPopulateAccount,
2017-10-24 19:41:09 +02:00
loadByUUID,
loadByHostAndUUID,
2017-11-09 17:51:58 +01:00
loadAndPopulateAccountAndVideos,
2017-11-10 14:34:45 +01:00
countByAccount,
loadByUrl,
loadByUUIDOrUrl
2017-10-24 19:41:09 +02:00
]
const instanceMethods = [
isOwned,
toFormattedJSON,
2017-11-10 14:34:45 +01:00
toActivityPubObject
2017-10-24 19:41:09 +02:00
]
addMethodsToModel(VideoChannel, classMethods, instanceMethods)
return VideoChannel
}
// ------------------------------ METHODS ------------------------------
isOwned = function (this: VideoChannelInstance) {
return this.remote === false
}
toFormattedJSON = function (this: VideoChannelInstance) {
const json = {
id: this.id,
uuid: this.uuid,
name: this.name,
description: this.description,
isLocal: this.isOwned(),
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
2017-11-09 17:51:58 +01:00
if (this.Account !== undefined) {
2017-10-24 19:41:09 +02:00
json['owner'] = {
2017-11-09 17:51:58 +01:00
name: this.Account.name,
uuid: this.Account.uuid
2017-10-24 19:41:09 +02:00
}
}
if (Array.isArray(this.Videos)) {
json['videos'] = this.Videos.map(v => v.toFormattedJSON())
}
return json
}
2017-11-09 17:51:58 +01:00
toActivityPubObject = function (this: VideoChannelInstance) {
2017-10-24 19:41:09 +02:00
const json = {
2017-11-10 17:27:49 +01:00
type: 'VideoChannel' as 'VideoChannel',
id: this.url,
2017-10-24 19:41:09 +02:00
uuid: this.uuid,
2017-11-10 17:27:49 +01:00
content: this.description,
2017-10-24 19:41:09 +02:00
name: this.name,
published: this.createdAt.toISOString(),
updated: this.updatedAt.toISOString()
2017-10-24 19:41:09 +02:00
}
return json
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-11-09 17:51:58 +01:00
VideoChannel.belongsTo(models.Account, {
2017-10-24 19:41:09 +02:00
foreignKey: {
2017-11-09 17:51:58 +01:00
name: 'accountId',
2017-10-24 19:41:09 +02:00
allowNull: false
},
onDelete: 'CASCADE'
})
VideoChannel.hasMany(models.Video, {
foreignKey: {
name: 'channelId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
function afterDestroy (videoChannel: VideoChannelInstance) {
2017-10-24 19:41:09 +02:00
if (videoChannel.isOwned()) {
2017-11-13 17:39:41 +01:00
return sendDeleteVideoChannel(videoChannel, undefined)
2017-10-24 19:41:09 +02:00
}
return undefined
}
2017-11-09 17:51:58 +01:00
countByAccount = function (accountId: number) {
2017-10-24 19:41:09 +02:00
const query = {
where: {
2017-11-09 17:51:58 +01:00
accountId
2017-10-24 19:41:09 +02:00
}
}
return VideoChannel.count(query)
}
listForApi = function (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: [ getSort(sort) ],
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 19:41:09 +02:00
required: true,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
}
]
}
return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2017-11-09 17:51:58 +01:00
listByAccount = function (accountId: number) {
2017-10-24 19:41:09 +02:00
const query = {
order: [ getSort('createdAt') ],
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 19:41:09 +02:00
where: {
2017-11-09 17:51:58 +01:00
id: accountId
2017-10-24 19:41:09 +02:00
},
required: true,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
}
]
}
return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
})
}
loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
uuid
}
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-11-10 14:34:45 +01:00
loadByUrl = function (url: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
url
2017-11-16 15:22:39 +01:00
},
include: [ VideoChannel['sequelize'].models.Account ]
2017-11-10 14:34:45 +01:00
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
loadByUUIDOrUrl = function (uuid: string, url: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
[Sequelize.Op.or]: [
{ uuid },
{ url }
]
2017-11-10 17:27:49 +01:00
}
2017-11-10 14:34:45 +01:00
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-10-24 19:41:09 +02:00
loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
uuid
},
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 19:41:09 +02:00
include: [
{
2017-11-15 11:00:25 +01:00
model: VideoChannel['sequelize'].models.Server,
2017-10-24 19:41:09 +02:00
required: true,
where: {
host: fromHost
}
}
]
}
]
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-11-09 17:51:58 +01:00
loadByIdAndAccount = function (id: number, accountId: number) {
2017-10-24 19:41:09 +02:00
const options = {
where: {
id,
2017-11-09 17:51:58 +01:00
accountId
2017-10-24 19:41:09 +02:00
},
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 17:51:58 +01:00
loadAndPopulateAccount = function (id: number) {
2017-10-24 19:41:09 +02:00
const options = {
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
}
]
}
return VideoChannel.findById(id, options)
}
2017-11-09 17:51:58 +01:00
loadByUUIDAndPopulateAccount = function (uuid: string) {
2017-10-24 19:41:09 +02:00
const options = {
where: {
uuid
},
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 17:51:58 +01:00
loadAndPopulateAccountAndVideos = function (id: number) {
2017-10-24 19:41:09 +02:00
const options = {
include: [
{
2017-11-09 17:51:58 +01:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 11:00:25 +01:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 19:41:09 +02:00
},
VideoChannel['sequelize'].models.Video
]
}
return VideoChannel.findById(id, options)
}