2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-12 10:53:50 -06:00
|
|
|
import {
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
HasMany,
|
|
|
|
Is,
|
|
|
|
IsUUID,
|
2017-12-14 04:18:49 -06:00
|
|
|
Model,
|
|
|
|
Scopes,
|
2017-12-12 10:53:50 -06:00
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
|
|
|
import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
|
|
|
|
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
|
|
|
|
import { sendDeleteVideoChannel } from '../../lib/activitypub/send'
|
|
|
|
import { AccountModel } from '../account/account'
|
2017-12-14 04:18:49 -06:00
|
|
|
import { ActorModel } from '../activitypub/actor'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { ServerModel } from '../server/server'
|
|
|
|
import { getSort, throwIfNotValid } from '../utils'
|
|
|
|
import { VideoModel } from './video'
|
|
|
|
import { VideoChannelShareModel } from './video-channel-share'
|
|
|
|
|
2017-12-14 03:07:57 -06: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 10:53:50 -06:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoChannel',
|
|
|
|
indexes: [
|
2017-10-24 12:41:09 -05:00
|
|
|
{
|
2017-12-12 10:53:50 -06:00
|
|
|
fields: [ 'accountId' ]
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
|
|
|
export class VideoChannelModel extends Model<VideoChannelModel> {
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(false)
|
|
|
|
@Default(DataType.UUIDV4)
|
|
|
|
@IsUUID(4)
|
|
|
|
@Column(DataType.UUID)
|
|
|
|
uuid: string
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(false)
|
|
|
|
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
|
|
|
|
@Column
|
|
|
|
name: string
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(true)
|
|
|
|
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
|
|
|
|
@Column
|
|
|
|
description: string
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
remote: boolean
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-14 04:18:49 -06:00
|
|
|
@ForeignKey(() => ActorModel)
|
|
|
|
@Column
|
|
|
|
actorId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => ActorModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
Actor: ActorModel
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => AccountModel)
|
|
|
|
@Column
|
|
|
|
accountId: number
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@BelongsTo(() => AccountModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Account: AccountModel
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@HasMany(() => VideoModel, {
|
2017-10-24 12:41:09 -05:00
|
|
|
foreignKey: {
|
2017-12-12 10:53:50 -06:00
|
|
|
name: 'channelId',
|
2017-10-24 12:41:09 -05:00
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Videos: VideoModel[]
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@HasMany(() => VideoChannelShareModel, {
|
2017-10-24 12:41:09 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'channelId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
VideoChannelShares: VideoChannelShareModel[]
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AfterDestroy
|
|
|
|
static sendDeleteIfOwned (instance: VideoChannelModel) {
|
|
|
|
if (instance.isOwned()) {
|
|
|
|
return sendDeleteVideoChannel(instance, undefined)
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return undefined
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static countByAccount (accountId: number) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
accountId
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
|
|
|
|
return VideoChannelModel.count(query)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static listForApi (start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-12-14 03:07:57 -06:00
|
|
|
order: [ getSort(sort) ]
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findAndCountAll(query)
|
2017-12-12 10:53:50 -06:00
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return { total: count, data: rows }
|
|
|
|
})
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06: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 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return VideoChannelModel.findAndCountAll(query)
|
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return { total: count, data: rows }
|
|
|
|
})
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadByUrl (url: string, t?: Sequelize.Transaction) {
|
|
|
|
const query: IFindOptions<VideoChannelModel> = {
|
2017-12-14 04:18:49 -06:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
|
|
|
|
if (t !== undefined) query.transaction = t
|
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadByUUIDOrUrl (uuid: string, url: string, t?: Sequelize.Transaction) {
|
|
|
|
const query: IFindOptions<VideoChannelModel> = {
|
|
|
|
where: {
|
|
|
|
[ Sequelize.Op.or ]: [
|
|
|
|
{ uuid },
|
|
|
|
{ url }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
if (t !== undefined) query.transaction = t
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return VideoChannelModel.findOne(query)
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadByIdAndAccount (id: number, accountId: number) {
|
|
|
|
const options = {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
accountId
|
2017-12-14 03:07:57 -06:00
|
|
|
}
|
2017-11-10 10:27:49 -06:00
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadAndPopulateAccount (id: number) {
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findById(id)
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadByUUIDAndPopulateAccount (uuid: string) {
|
|
|
|
const options = {
|
|
|
|
where: {
|
|
|
|
uuid
|
2017-12-14 03:07:57 -06:00
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadAndPopulateAccountAndVideos (id: number) {
|
|
|
|
const options = {
|
|
|
|
include: [
|
|
|
|
VideoModel
|
|
|
|
]
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoChannelModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]).findById(id, options)
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
isOwned () {
|
|
|
|
return this.remote === false
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06: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 12:41:09 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
if (this.Account !== undefined) {
|
|
|
|
json[ 'owner' ] = {
|
|
|
|
name: this.Account.name,
|
|
|
|
uuid: this.Account.uuid
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(this.Videos)) {
|
|
|
|
json[ 'videos' ] = this.Videos.map(v => v.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
toActivityPubObject () {
|
2017-12-14 04:18:49 -06:00
|
|
|
return this.Actor.toActivityPubObject(this.name, this.uuid, 'VideoChannel')
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
}
|