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 toAddRemoteJSON: VideoChannelMethods.ToAddRemoteJSON
17 let toUpdateRemoteJSON: VideoChannelMethods.ToUpdateRemoteJSON
18 let isOwned: VideoChannelMethods.IsOwned
19 let countByAuthor: VideoChannelMethods.CountByAuthor
20 let listOwned: VideoChannelMethods.ListOwned
21 let listForApi: VideoChannelMethods.ListForApi
22 let listByAuthor: VideoChannelMethods.ListByAuthor
23 let loadByIdAndAuthor: VideoChannelMethods.LoadByIdAndAuthor
24 let loadByUUID: VideoChannelMethods.LoadByUUID
25 let loadAndPopulateAuthor: VideoChannelMethods.LoadAndPopulateAuthor
26 let loadByUUIDAndPopulateAuthor: VideoChannelMethods.LoadByUUIDAndPopulateAuthor
27 let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
28 let loadAndPopulateAuthorAndVideos: VideoChannelMethods.LoadAndPopulateAuthorAndVideos
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,
70 fields: [ 'authorId' ]
79 const classMethods = [
86 loadAndPopulateAuthor,
87 loadByUUIDAndPopulateAuthor,
90 loadAndPopulateAuthorAndVideos,
93 const instanceMethods = [
99 addMethodsToModel(VideoChannel, classMethods, instanceMethods)
104 // ------------------------------ METHODS ------------------------------
106 isOwned = function (this: VideoChannelInstance) {
107 return this.remote === false
110 toFormattedJSON = function (this: VideoChannelInstance) {
115 description: this.description,
116 isLocal: this.isOwned(),
117 createdAt: this.createdAt,
118 updatedAt: this.updatedAt
121 if (this.Author !== undefined) {
123 name: this.Author.name,
124 uuid: this.Author.uuid
128 if (Array.isArray(this.Videos)) {
129 json['videos'] = this.Videos.map(v => v.toFormattedJSON())
135 toAddRemoteJSON = function (this: VideoChannelInstance) {
139 description: this.description,
140 createdAt: this.createdAt,
141 updatedAt: this.updatedAt,
142 ownerUUID: this.Author.uuid
148 toUpdateRemoteJSON = function (this: VideoChannelInstance) {
152 description: this.description,
153 createdAt: this.createdAt,
154 updatedAt: this.updatedAt,
155 ownerUUID: this.Author.uuid
161 // ------------------------------ STATICS ------------------------------
163 function associate (models) {
164 VideoChannel.belongsTo(models.Author, {
172 VideoChannel.hasMany(models.Video, {
181 function afterDestroy (videoChannel: VideoChannelInstance, options: { transaction: Sequelize.Transaction }) {
182 if (videoChannel.isOwned()) {
183 const removeVideoChannelToFriendsParams = {
184 uuid: videoChannel.uuid
187 return removeVideoChannelToFriends(removeVideoChannelToFriendsParams, options.transaction)
193 countByAuthor = function (authorId: number) {
200 return VideoChannel.count(query)
203 listOwned = function () {
208 include: [ VideoChannel['sequelize'].models.Author ]
211 return VideoChannel.findAll(query)
214 listForApi = function (start: number, count: number, sort: string) {
218 order: [ getSort(sort) ],
221 model: VideoChannel['sequelize'].models.Author,
223 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
228 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
229 return { total: count, data: rows }
233 listByAuthor = function (authorId: number) {
235 order: [ getSort('createdAt') ],
238 model: VideoChannel['sequelize'].models.Author,
243 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
248 return VideoChannel.findAndCountAll(query).then(({ rows, count }) => {
249 return { total: count, data: rows }
253 loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
254 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
260 if (t !== undefined) query.transaction = t
262 return VideoChannel.findOne(query)
265 loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
266 const query: Sequelize.FindOptions<VideoChannelAttributes> = {
272 model: VideoChannel['sequelize'].models.Author,
275 model: VideoChannel['sequelize'].models.Pod,
286 if (t !== undefined) query.transaction = t
288 return VideoChannel.findOne(query)
291 loadByIdAndAuthor = function (id: number, authorId: number) {
299 model: VideoChannel['sequelize'].models.Author,
300 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
305 return VideoChannel.findOne(options)
308 loadAndPopulateAuthor = function (id: number) {
312 model: VideoChannel['sequelize'].models.Author,
313 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
318 return VideoChannel.findById(id, options)
321 loadByUUIDAndPopulateAuthor = function (uuid: string) {
328 model: VideoChannel['sequelize'].models.Author,
329 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
334 return VideoChannel.findOne(options)
337 loadAndPopulateAuthorAndVideos = function (id: number) {
341 model: VideoChannel['sequelize'].models.Author,
342 include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
344 VideoChannel['sequelize'].models.Video
348 return VideoChannel.findById(id, options)