X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-blacklist.ts;h=ae8286285cd1bb37d1a29591625fc27834f7c8eb;hb=c893d4514e6ecbf282c7985fe5f82b8acd8a1137;hp=f4479986cf13c3ff8e64fe5b8f58e64eb115b9a8;hpb=74889a71fe687dda74f2a687653122327807af36;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index f4479986c..ae8286285 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,8 +1,9 @@ import * as Sequelize from 'sequelize' -import { addMethodsToModel, getSort } from '../utils' +import { SortType } from '../../helpers' +import { addMethodsToModel, getSortOnModel } from '../utils' +import { VideoInstance } from './video-interface' import { - BlacklistedVideoClass, BlacklistedVideoInstance, BlacklistedVideoAttributes, @@ -10,11 +11,8 @@ import { } from './video-blacklist-interface' let BlacklistedVideo: Sequelize.Model -let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON -let countTotal: BlacklistedVideoMethods.CountTotal -let list: BlacklistedVideoMethods.List +let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON let listForApi: BlacklistedVideoMethods.ListForApi -let loadById: BlacklistedVideoMethods.LoadById let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { @@ -33,14 +31,11 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da const classMethods = [ associate, - countTotal, - list, listForApi, - loadById, loadByVideoId ] const instanceMethods = [ - toFormatedJSON + toFormattedJSON ] addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) @@ -49,11 +44,24 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da // ------------------------------ METHODS ------------------------------ -toFormatedJSON = function () { +toFormattedJSON = function (this: BlacklistedVideoInstance) { + let video: VideoInstance + + video = this.Video + return { id: this.id, videoId: this.videoId, - createdAt: this.createdAt + createdAt: this.createdAt, + updatedAt: this.updatedAt, + name: video.name, + uuid: video.uuid, + description: video.description, + duration: video.duration, + views: video.views, + likes: video.likes, + dislikes: video.dislikes, + nsfw: video.nsfw } } @@ -61,43 +69,36 @@ toFormatedJSON = function () { function associate (models) { BlacklistedVideo.belongsTo(models.Video, { - foreignKey: 'videoId', - onDelete: 'cascade' + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'CASCADE' }) } -countTotal = function (callback: BlacklistedVideoMethods.CountTotalCallback) { - return BlacklistedVideo.count().asCallback(callback) -} - -list = function (callback: BlacklistedVideoMethods.ListCallback) { - return BlacklistedVideo.findAll().asCallback(callback) -} - -listForApi = function (start: number, count: number, sort: string, callback: BlacklistedVideoMethods.ListForApiCallback) { +listForApi = function (start: number, count: number, sort: SortType) { const query = { offset: start, limit: count, - order: [ getSort(sort) ] + order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], + include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] } - return BlacklistedVideo.findAndCountAll(query).asCallback(function (err, result) { - if (err) return callback(err) - - return callback(null, result.rows, result.count) + return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } }) } -loadById = function (id: number, callback: BlacklistedVideoMethods.LoadByIdCallback) { - return BlacklistedVideo.findById(id).asCallback(callback) -} - -loadByVideoId = function (id: string, callback: BlacklistedVideoMethods.LoadByIdCallback) { +loadByVideoId = function (id: number) { const query = { where: { videoId: id } } - return BlacklistedVideo.find(query).asCallback(callback) + return BlacklistedVideo.findOne(query) }