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

372 lines
9.3 KiB
TypeScript
Raw Normal View History

2017-10-24 12:41:09 -05:00
import * as Sequelize from 'sequelize'
2017-11-20 02:43:39 -06:00
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers'
2017-11-14 03:57:56 -06:00
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
2017-11-20 02:43:39 -06:00
import { sendDeleteVideoChannel } from '../../lib/activitypub/send/send-delete'
import { addMethodsToModel, getSort } from '../utils'
import { VideoChannelAttributes, VideoChannelInstance, VideoChannelMethods } from './video-channel-interface'
import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
import { activityPubCollection } from '../../helpers/activitypub'
2017-11-27 10:30:46 -06:00
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
2017-10-24 12:41:09 -05:00
let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
2017-11-09 10:51:58 -06:00
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
2017-10-24 12:41:09 -05:00
let isOwned: VideoChannelMethods.IsOwned
2017-11-09 10:51:58 -06:00
let countByAccount: VideoChannelMethods.CountByAccount
2017-10-24 12:41:09 -05:00
let listForApi: VideoChannelMethods.ListForApi
2017-11-09 10:51:58 -06:00
let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
2017-10-24 12:41:09 -05:00
let loadByUUID: VideoChannelMethods.LoadByUUID
2017-11-09 10:51:58 -06:00
let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
2017-10-24 12:41:09 -05:00
let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
2017-11-09 10:51:58 -06:00
let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
2017-11-10 07:34:45 -06:00
let loadByUrl: VideoChannelMethods.LoadByUrl
let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
2017-10-24 12:41:09 -05: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 10:51:58 -06:00
},
url: {
2017-11-14 03:57:56 -06:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max),
2017-11-09 10:51:58 -06:00
allowNull: false,
validate: {
2017-11-14 03:57:56 -06:00
urlValid: value => {
2017-11-27 10:30:46 -06:00
const res = isActivityPubUrlValid(value)
2017-11-14 03:57:56 -06:00
if (res === false) throw new Error('Video channel URL is not valid.')
}
2017-11-09 10:51:58 -06:00
}
2017-10-24 12:41:09 -05:00
}
},
{
indexes: [
{
2017-11-09 10:51:58 -06:00
fields: [ 'accountId' ]
2017-10-24 12:41:09 -05:00
}
],
hooks: {
afterDestroy
}
}
)
const classMethods = [
associate,
listForApi,
2017-11-09 10:51:58 -06:00
listByAccount,
loadByIdAndAccount,
loadAndPopulateAccount,
loadByUUIDAndPopulateAccount,
2017-10-24 12:41:09 -05:00
loadByUUID,
loadByHostAndUUID,
2017-11-09 10:51:58 -06:00
loadAndPopulateAccountAndVideos,
2017-11-10 07:34:45 -06:00
countByAccount,
loadByUrl,
loadByUUIDOrUrl
2017-10-24 12:41:09 -05:00
]
const instanceMethods = [
isOwned,
toFormattedJSON,
2017-11-10 07:34:45 -06:00
toActivityPubObject
2017-10-24 12:41:09 -05: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 10:51:58 -06:00
if (this.Account !== undefined) {
2017-10-24 12:41:09 -05:00
json['owner'] = {
2017-11-09 10:51:58 -06:00
name: this.Account.name,
uuid: this.Account.uuid
2017-10-24 12:41:09 -05:00
}
}
if (Array.isArray(this.Videos)) {
json['videos'] = this.Videos.map(v => v.toFormattedJSON())
}
return json
}
2017-11-09 10:51:58 -06:00
toActivityPubObject = function (this: VideoChannelInstance) {
let sharesObject
if (Array.isArray(this.VideoChannelShares)) {
const shares: string[] = []
for (const videoChannelShare of this.VideoChannelShares) {
const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account)
shares.push(shareUrl)
}
sharesObject = activityPubCollection(shares)
}
2017-10-24 12:41:09 -05:00
const json = {
2017-11-10 10:27:49 -06:00
type: 'VideoChannel' as 'VideoChannel',
id: this.url,
2017-10-24 12:41:09 -05:00
uuid: this.uuid,
2017-11-10 10:27:49 -06:00
content: this.description,
2017-10-24 12:41:09 -05:00
name: this.name,
published: this.createdAt.toISOString(),
updated: this.updatedAt.toISOString(),
shares: sharesObject
2017-10-24 12:41:09 -05:00
}
return json
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-11-09 10:51:58 -06:00
VideoChannel.belongsTo(models.Account, {
2017-10-24 12:41:09 -05:00
foreignKey: {
2017-11-09 10:51:58 -06:00
name: 'accountId',
2017-10-24 12:41:09 -05:00
allowNull: false
},
onDelete: 'CASCADE'
})
VideoChannel.hasMany(models.Video, {
foreignKey: {
name: 'channelId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
function afterDestroy (videoChannel: VideoChannelInstance) {
2017-10-24 12:41:09 -05:00
if (videoChannel.isOwned()) {
2017-11-13 10:39:41 -06:00
return sendDeleteVideoChannel(videoChannel, undefined)
2017-10-24 12:41:09 -05:00
}
return undefined
}
2017-11-09 10:51:58 -06:00
countByAccount = function (accountId: number) {
2017-10-24 12:41:09 -05:00
const query = {
where: {
2017-11-09 10:51:58 -06:00
accountId
2017-10-24 12:41:09 -05: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 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 12:41:09 -05:00
required: true,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05:00
}
]
}
return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2017-11-09 10:51:58 -06:00
listByAccount = function (accountId: number) {
2017-10-24 12:41:09 -05:00
const query = {
order: [ getSort('createdAt') ],
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 12:41:09 -05:00
where: {
2017-11-09 10:51:58 -06:00
id: accountId
2017-10-24 12:41:09 -05:00
},
required: true,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05: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 07:34:45 -06:00
loadByUrl = function (url: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
url
2017-11-16 08:22:39 -06:00
},
include: [ VideoChannel['sequelize'].models.Account ]
2017-11-10 07:34:45 -06: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 10:27:49 -06:00
}
2017-11-10 07:34:45 -06:00
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-10-24 12:41:09 -05:00
loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
uuid
},
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-10-24 12:41:09 -05:00
include: [
{
2017-11-15 04:00:25 -06:00
model: VideoChannel['sequelize'].models.Server,
2017-10-24 12:41:09 -05:00
required: true,
where: {
host: fromHost
}
}
]
}
]
}
if (t !== undefined) query.transaction = t
return VideoChannel.findOne(query)
}
2017-11-09 10:51:58 -06:00
loadByIdAndAccount = function (id: number, accountId: number) {
2017-10-24 12:41:09 -05:00
const options = {
where: {
id,
2017-11-09 10:51:58 -06:00
accountId
2017-10-24 12:41:09 -05:00
},
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05:00
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 10:51:58 -06:00
loadAndPopulateAccount = function (id: number) {
2017-10-24 12:41:09 -05:00
const options = {
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05:00
}
]
}
return VideoChannel.findById(id, options)
}
2017-11-09 10:51:58 -06:00
loadByUUIDAndPopulateAccount = function (uuid: string) {
2017-10-24 12:41:09 -05:00
const options = {
where: {
uuid
},
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05:00
}
]
}
return VideoChannel.findOne(options)
}
2017-11-09 10:51:58 -06:00
loadAndPopulateAccountAndVideos = function (id: number) {
2017-10-24 12:41:09 -05:00
const options = {
include: [
{
2017-11-09 10:51:58 -06:00
model: VideoChannel['sequelize'].models.Account,
2017-11-15 04:00:25 -06:00
include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
2017-10-24 12:41:09 -05:00
},
VideoChannel['sequelize'].models.Video
]
}
return VideoChannel.findById(id, options)
}