1 import * as Sequelize from 'sequelize'
3 import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
4 import { removeVideoChannelToFriends } from '../../lib'
6 import { addMethodsToModel, getSort } from '../utils'
9 VideoChannelAttributes,
12 } from './video-channel-interface'
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) {
148 description: this.description,
149 createdAt: this.createdAt,
150 updatedAt: this.updatedAt,
151 ownerUUID: this.Account.uuid
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 return removeVideoChannelToFriends(removeVideoChannelToFriendsParams)
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)