2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
|
|
|
|
import { removeVideoChannelToFriends } from '../../lib'
|
|
|
|
|
|
|
|
import { addMethodsToModel, getSort } from '../utils'
|
|
|
|
import {
|
|
|
|
VideoChannelInstance,
|
|
|
|
VideoChannelAttributes,
|
|
|
|
|
|
|
|
VideoChannelMethods
|
|
|
|
} from './video-channel-interface'
|
|
|
|
|
|
|
|
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 listOwned: VideoChannelMethods.ListOwned
|
|
|
|
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-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: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isUrl: true
|
|
|
|
}
|
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,
|
2017-10-24 12:41:09 -05:00
|
|
|
listOwned,
|
2017-11-09 10:51:58 -06:00
|
|
|
loadByIdAndAccount,
|
|
|
|
loadAndPopulateAccount,
|
|
|
|
loadByUUIDAndPopulateAccount,
|
2017-10-24 12:41:09 -05:00
|
|
|
loadByUUID,
|
|
|
|
loadByHostAndUUID,
|
2017-11-09 10:51:58 -06:00
|
|
|
loadAndPopulateAccountAndVideos,
|
|
|
|
countByAccount
|
2017-10-24 12:41:09 -05:00
|
|
|
]
|
|
|
|
const instanceMethods = [
|
|
|
|
isOwned,
|
|
|
|
toFormattedJSON,
|
2017-11-09 10:51:58 -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) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const json = {
|
|
|
|
uuid: this.uuid,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
2017-11-09 10:51:58 -06:00
|
|
|
ownerUUID: this.Account.uuid
|
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'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:05:20 -05:00
|
|
|
function afterDestroy (videoChannel: VideoChannelInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
if (videoChannel.isOwned()) {
|
|
|
|
const removeVideoChannelToFriendsParams = {
|
|
|
|
uuid: videoChannel.uuid
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:05:20 -05:00
|
|
|
return removeVideoChannelToFriends(removeVideoChannelToFriendsParams)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
listOwned = function () {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
remote: false
|
|
|
|
},
|
2017-11-09 10:51:58 -06:00
|
|
|
include: [ VideoChannel['sequelize'].models.Account ]
|
2017-10-24 12:41:09 -05: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 10:51:58 -06:00
|
|
|
model: VideoChannel['sequelize'].models.Account,
|
2017-10-24 12:41:09 -05: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 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,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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: [
|
|
|
|
{
|
|
|
|
model: VideoChannel['sequelize'].models.Pod,
|
|
|
|
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-10-24 12:41:09 -05:00
|
|
|
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-24 12:41:09 -05:00
|
|
|
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-24 12:41:09 -05:00
|
|
|
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-24 12:41:09 -05:00
|
|
|
include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
|
|
|
|
},
|
|
|
|
VideoChannel['sequelize'].models.Video
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoChannel.findById(id, options)
|
|
|
|
}
|