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

370 lines
8.6 KiB
TypeScript
Raw Normal View History

2017-10-24 20:41:09 +03:00
import * as Sequelize from 'sequelize'
import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
import { addMethodsToModel, getSort } from '../utils'
import {
VideoChannelInstance,
VideoChannelAttributes,
VideoChannelMethods
} from './video-channel-interface'
2017-11-13 19:39:41 +03:00
import { sendDeleteVideoChannel } from '../../lib/activitypub/send-request'
2017-10-24 20:41:09 +03:00
let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
2017-11-09 19:51:58 +03:00
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
2017-10-24 20:41:09 +03:00
let isOwned: VideoChannelMethods.IsOwned
2017-11-09 19:51:58 +03:00
let countByAccount: VideoChannelMethods.CountByAccount
2017-10-24 20:41:09 +03:00
let listOwned: VideoChannelMethods.ListOwned
let listForApi: VideoChannelMethods.ListForApi
2017-11-09 19:51:58 +03:00
let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
2017-10-24 20:41:09 +03:00
let loadByUUID: VideoChannelMethods.LoadByUUID
2017-11-09 19:51:58 +03:00
let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
2017-10-24 20:41:09 +03:00
let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
2017-11-09 19:51:58 +03:00
let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
2017-11-10 16:34:45 +03:00
let loadByUrl: VideoChannelMethods.LoadByUrl
let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
2017-10-24 20:41:09 +03: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 19:51:58 +03:00
},
url: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isUrl: true
}
2017-10-24 20:41:09 +03:00
}
},
{
indexes: [
{
2017-11-09 19:51:58 +03:00
fields: [ 'accountId' ]
2017-10-24 20:41:09 +03:00
}
],
hooks: {
afterDestroy
}
}
)
const classMethods = [
associate,
listForApi,
2017-11-09 19:51:58 +03:00
listByAccount,
2017-10-24 20:41:09 +03:00
listOwned,
2017-11-09 19:51:58 +03:00
loadByIdAndAccount,
loadAndPopulateAccount,
loadByUUIDAndPopulateAccount,
2017-10-24 20:41:09 +03:00
loadByUUID,
loadByHostAndUUID,
2017-11-09 19:51:58 +03:00
loadAndPopulateAccountAndVideos,
2017-11-10 16:34:45 +03:00
countByAccount,
loadByUrl,
loadByUUIDOrUrl
2017-10-24 20:41:09 +03:00
]
const instanceMethods = [
isOwned,
toFormattedJSON,
2017-11-10 16:34:45 +03:00
toActivityPubObject
2017-10-24 20:41:09 +03: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 19:51:58 +03:00
if (this.Account !== undefined) {
2017-10-24 20:41:09 +03:00
json['owner'] = {
2017-11-09 19:51:58 +03:00
name: this.Account.name,
uuid: this.Account.uuid
2017-10-24 20:41:09 +03:00
}
}
if (Array.isArray(this.Videos)) {
json['videos'] = this.Videos.map(v => v.toFormattedJSON())
}
return json
}
2017-11-09 19:51:58 +03:00
toActivityPubObject = function (this: VideoChannelInstance) {
2017-10-24 20:41:09 +03:00
const json = {
2017-11-10 19:27:49 +03:00
type: 'VideoChannel' as 'VideoChannel',
id: this.url,
2017-10-24 20:41:09 +03:00
uuid: this.uuid,
2017-11-10 19:27:49 +03:00
content: this.description,
2017-10-24 20:41:09 +03:00
name: this.name,
2017-11-10 19:27:49 +03:00
published: this.createdAt,
updated: this.updatedAt
2017-10-24 20:41:09 +03:00
}
return json
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-11-09 19:51:58 +03:00
VideoChannel.belongsTo(models.Account, {
2017-10-24 20:41:09 +03:00
foreignKey: {
2017-11-09 19:51:58 +03:00
name: 'accountId',
2017-10-24 20:41:09 +03:00
allowNull: false
},
onDelete: 'CASCADE'
})
VideoChannel.hasMany(models.Video, {
foreignKey: {
name: 'channelId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
function afterDestroy (videoChannel: VideoChannelInstance) {
2017-10-24 20:41:09 +03:00
if (videoChannel.isOwned()) {
2017-11-13 19:39:41 +03:00
return sendDeleteVideoChannel(videoChannel, undefined)
2017-10-24 20:41:09 +03:00
}
return undefined
}
2017-11-09 19:51:58 +03:00
countByAccount = function (accountId: number) {
2017-10-24 20:41:09 +03:00
const query = {
where: {
2017-11-09 19:51:58 +03:00
accountId
2017-10-24 20:41:09 +03:00
}
}
return VideoChannel.count(query)
}
listOwned = function () {
const query = {
where: {
remote: false
},
2017-11-09 19:51:58 +03:00
include: [ VideoChannel['sequelize'].models.Account ]
2017-10-24 20:41:09 +03:00
}
return VideoChannel.findAll(query)
}
listForApi = function (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: [ getSort(sort) ],
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
required: true,
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
}
]
}
return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2017-11-09 19:51:58 +03:00
listByAccount = function (accountId: number) {
2017-10-24 20:41:09 +03:00
const query = {
order: [ getSort('createdAt') ],
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
where: {
2017-11-09 19:51:58 +03:00
id: accountId
2017-10-24 20:41:09 +03:00
},
required: true,
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
}
]
}
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 16:34:45 +03:00
loadByUrl = function (url: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
url
}
}
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 19:27:49 +03:00
}
2017-11-10 16:34:45 +03:00
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-10-24 20:41:09 +03:00
loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
uuid
},
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
include: [
{
model: VideoChannel['sequelize'].models.Pod,
required: true,
where: {
host: fromHost
}
}
]
}
]
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-11-09 19:51:58 +03:00
loadByIdAndAccount = function (id: number, accountId: number) {
2017-10-24 20:41:09 +03:00
const options = {
where: {
id,
2017-11-09 19:51:58 +03:00
accountId
2017-10-24 20:41:09 +03:00
},
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 19:51:58 +03:00
loadAndPopulateAccount = function (id: number) {
2017-10-24 20:41:09 +03:00
const options = {
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
}
]
}
return VideoChannel.findById(id, options)
}
2017-11-09 19:51:58 +03:00
loadByUUIDAndPopulateAccount = function (uuid: string) {
2017-10-24 20:41:09 +03:00
const options = {
where: {
uuid
},
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 19:51:58 +03:00
loadAndPopulateAccountAndVideos = function (id: number) {
2017-10-24 20:41:09 +03:00
const options = {
include: [
{
2017-11-09 19:51:58 +03:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 20:41:09 +03:00
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
},
VideoChannel['sequelize'].models.Video
]
}
return VideoChannel.findById(id, options)
}