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'
13 let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
14 let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
15 let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
16 let isOwned: VideoChannelMethods.IsOwned
17 let countByAccount: VideoChannelMethods.CountByAccount
18 let listOwned: VideoChannelMethods.ListOwned
19 let listForApi: VideoChannelMethods.ListForApi
20 let listByAccount: VideoChannelMethods.ListByAccount
21 let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
22 let loadByUUID: VideoChannelMethods.LoadByUUID
23 let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
24 let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
25 let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
26 let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
27 let loadByUrl: VideoChannelMethods.LoadByUrl
28 let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
30 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
31 VideoChannel = sequelize.define<VideoChannelInstance, VideoChannelAttributes>('VideoChannel',
35 defaultValue: DataTypes.UUIDV4,
42 type: DataTypes.STRING,
46 const res = isVideoChannelNameValid(value)
47 if (res === false) throw new Error('Video channel name is not valid.')
52 type: DataTypes.STRING,
55 descriptionValid: value => {
56 const res = isVideoChannelDescriptionValid(value)
57 if (res === false) throw new Error('Video channel description is not valid.')
62 type: DataTypes.BOOLEAN,
67 type: DataTypes.STRING,
77 fields: [ 'accountId' ]
86 const classMethods = [
93 loadAndPopulateAccount,
94 loadByUUIDAndPopulateAccount,
97 loadAndPopulateAccountAndVideos,
102 const instanceMethods = [
107 addMethodsToModel(VideoChannel, classMethods, instanceMethods)
112 // ------------------------------ METHODS ------------------------------
114 isOwned = function (this: VideoChannelInstance) {
115 return this.remote === false
118 toFormattedJSON = function (this: VideoChannelInstance) {
123 description: this.description,
124 isLocal: this.isOwned(),
125 createdAt: this.createdAt,
126 updatedAt: this.updatedAt
129 if (this.Account !== undefined) {
131 name: this.Account.name,
132 uuid: this.Account.uuid
136 if (Array.isArray(this.Videos)) {
137 json['videos'] = this.Videos.map(v => v.toFormattedJSON())
143 toActivityPubObject = function (this: VideoChannelInstance) {
145 type: 'VideoChannel' as 'VideoChannel',
148 content: this.description,
150 published: this.createdAt,
151 updated: this.updatedAt
157 // ------------------------------ STATICS ------------------------------
159 function associate (models) {
160 VideoChannel.belongsTo(models.Account, {
168 VideoChannel.hasMany(models.Video, {
177 function afterDestroy (videoChannel: VideoChannelInstance) {
178 if (videoChannel.isOwned()) {
179 const removeVideoChannelToFriendsParams = {
180 uuid: videoChannel.uuid
183 // FIXME: send remove event to followers
189 countByAccount = function (accountId: number) {
196 return VideoChannel.count(query)
199 listOwned = function () {
204 include: [ VideoChannel['sequelize'].models.Account ]
207 return VideoChannel.findAll(query)
210 listForApi = function (start: number, count: number, sort: string) {
214 order: [ getSort(sort) ],
217 model: VideoChannel['sequelize'].models.Account,
219 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
224 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
225 return { total: count, data: rows }
229 listByAccount = function (accountId: number) {
231 order: [ getSort('createdAt') ],
234 model: VideoChannel['sequelize'].models.Account,
239 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
244 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
245 return { total: count, data: rows }
249 loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
250 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
256 if (t !== undefined) query.transaction = t
258 return VideoChannel.findOne(query)
261 loadByUrl = function (url: string, t?: Sequelize.Transaction) {
262 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
268 if (t !== undefined) query.transaction = t
270 return VideoChannel.findOne(query)
273 loadByUUIDOrUrl = function (uuid: string, url: string, t?: Sequelize.Transaction) {
274 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
283 if (t !== undefined) query.transaction = t
285 return VideoChannel.findOne(query)
288 loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
289 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
295 model: VideoChannel['sequelize'].models.Account,
298 model: VideoChannel['sequelize'].models.Pod,
309 if (t !== undefined) query.transaction = t
311 return VideoChannel.findOne(query)
314 loadByIdAndAccount = function (id: number, accountId: number) {
322 model: VideoChannel['sequelize'].models.Account,
323 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
328 return VideoChannel.findOne(options)
331 loadAndPopulateAccount = function (id: number) {
335 model: VideoChannel['sequelize'].models.Account,
336 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
341 return VideoChannel.findById(id, options)
344 loadByUUIDAndPopulateAccount = function (uuid: string) {
351 model: VideoChannel['sequelize'].models.Account,
352 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
357 return VideoChannel.findOne(options)
360 loadAndPopulateAccountAndVideos = function (id: number) {
364 model: VideoChannel['sequelize'].models.Account,
365 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
367 VideoChannel['sequelize'].models.Video
371 return VideoChannel.findById(id, options)