1 import * as Sequelize from 'sequelize'
3 import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
5 import { addMethodsToModel, getSort } from '../utils'
8 VideoChannelAttributes,
11 } from './video-channel-interface'
12 import { sendDeleteVideoChannel } from '../../lib/activitypub/send-request'
14 let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
15 let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
16 let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
17 let isOwned: VideoChannelMethods.IsOwned
18 let countByAccount: VideoChannelMethods.CountByAccount
19 let listOwned: VideoChannelMethods.ListOwned
20 let listForApi: VideoChannelMethods.ListForApi
21 let listByAccount: VideoChannelMethods.ListByAccount
22 let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
23 let loadByUUID: VideoChannelMethods.LoadByUUID
24 let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
25 let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
26 let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
27 let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
28 let loadByUrl: VideoChannelMethods.LoadByUrl
29 let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
31 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
32 VideoChannel = sequelize.define<VideoChannelInstance, VideoChannelAttributes>('VideoChannel',
36 defaultValue: DataTypes.UUIDV4,
43 type: DataTypes.STRING,
47 const res = isVideoChannelNameValid(value)
48 if (res === false) throw new Error('Video channel name is not valid.')
53 type: DataTypes.STRING,
56 descriptionValid: value => {
57 const res = isVideoChannelDescriptionValid(value)
58 if (res === false) throw new Error('Video channel description is not valid.')
63 type: DataTypes.BOOLEAN,
68 type: DataTypes.STRING,
78 fields: [ 'accountId' ]
87 const classMethods = [
94 loadAndPopulateAccount,
95 loadByUUIDAndPopulateAccount,
98 loadAndPopulateAccountAndVideos,
103 const instanceMethods = [
108 addMethodsToModel(VideoChannel, classMethods, instanceMethods)
113 // ------------------------------ METHODS ------------------------------
115 isOwned = function (this: VideoChannelInstance) {
116 return this.remote === false
119 toFormattedJSON = function (this: VideoChannelInstance) {
124 description: this.description,
125 isLocal: this.isOwned(),
126 createdAt: this.createdAt,
127 updatedAt: this.updatedAt
130 if (this.Account !== undefined) {
132 name: this.Account.name,
133 uuid: this.Account.uuid
137 if (Array.isArray(this.Videos)) {
138 json['videos'] = this.Videos.map(v => v.toFormattedJSON())
144 toActivityPubObject = function (this: VideoChannelInstance) {
146 type: 'VideoChannel' as 'VideoChannel',
149 content: this.description,
151 published: this.createdAt,
152 updated: this.updatedAt
158 // ------------------------------ STATICS ------------------------------
160 function associate (models) {
161 VideoChannel.belongsTo(models.Account, {
169 VideoChannel.hasMany(models.Video, {
178 function afterDestroy (videoChannel: VideoChannelInstance) {
179 if (videoChannel.isOwned()) {
180 return sendDeleteVideoChannel(videoChannel, undefined)
186 countByAccount = function (accountId: number) {
193 return VideoChannel.count(query)
196 listOwned = function () {
201 include: [ VideoChannel['sequelize'].models.Account ]
204 return VideoChannel.findAll(query)
207 listForApi = function (start: number, count: number, sort: string) {
211 order: [ getSort(sort) ],
214 model: VideoChannel['sequelize'].models.Account,
216 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
221 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
222 return { total: count, data: rows }
226 listByAccount = function (accountId: number) {
228 order: [ getSort('createdAt') ],
231 model: VideoChannel['sequelize'].models.Account,
236 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
241 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
242 return { total: count, data: rows }
246 loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
247 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
253 if (t !== undefined) query.transaction = t
255 return VideoChannel.findOne(query)
258 loadByUrl = function (url: string, t?: Sequelize.Transaction) {
259 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
265 if (t !== undefined) query.transaction = t
267 return VideoChannel.findOne(query)
270 loadByUUIDOrUrl = function (uuid: string, url: string, t?: Sequelize.Transaction) {
271 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
280 if (t !== undefined) query.transaction = t
282 return VideoChannel.findOne(query)
285 loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
286 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
292 model: VideoChannel['sequelize'].models.Account,
295 model: VideoChannel['sequelize'].models.Pod,
306 if (t !== undefined) query.transaction = t
308 return VideoChannel.findOne(query)
311 loadByIdAndAccount = function (id: number, accountId: number) {
319 model: VideoChannel['sequelize'].models.Account,
320 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
325 return VideoChannel.findOne(options)
328 loadAndPopulateAccount = function (id: number) {
332 model: VideoChannel['sequelize'].models.Account,
333 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
338 return VideoChannel.findById(id, options)
341 loadByUUIDAndPopulateAccount = function (uuid: string) {
348 model: VideoChannel['sequelize'].models.Account,
349 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
354 return VideoChannel.findOne(options)
357 loadAndPopulateAccountAndVideos = function (id: number) {
361 model: VideoChannel['sequelize'].models.Account,
362 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
364 VideoChannel['sequelize'].models.Video
368 return VideoChannel.findById(id, options)