]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-blacklist.ts
Add beautiful loading bar
[github/Chocobozzz/PeerTube.git] / server / models / video / video-blacklist.ts
index 8c42dbc21fbb2dc74681e5bb7c9ea7e9cf17cd72..ae8286285cd1bb37d1a29591625fc27834f7c8eb 100644 (file)
@@ -1,6 +1,8 @@
 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 {
   BlacklistedVideoInstance,
   BlacklistedVideoAttributes,
@@ -9,11 +11,8 @@ import {
 } from './video-blacklist-interface'
 
 let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
-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) {
@@ -32,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)
 
@@ -48,11 +44,24 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
 
 // ------------------------------ METHODS ------------------------------
 
-toFormatedJSON = function (this: BlacklistedVideoInstance) {
+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
   }
 }
 
@@ -60,24 +69,20 @@ toFormatedJSON = function (this: BlacklistedVideoInstance) {
 
 function associate (models) {
   BlacklistedVideo.belongsTo(models.Video, {
-    foreignKey: 'videoId',
-    onDelete: 'cascade'
+    foreignKey: {
+      name: 'videoId',
+      allowNull: false
+    },
+    onDelete: 'CASCADE'
   })
 }
 
-countTotal = function () {
-  return BlacklistedVideo.count()
-}
-
-list = function () {
-  return BlacklistedVideo.findAll()
-}
-
-listForApi = function (start: number, count: number, sort: string) {
+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).then(({ rows, count }) => {
@@ -88,11 +93,7 @@ listForApi = function (start: number, count: number, sort: string) {
   })
 }
 
-loadById = function (id: number) {
-  return BlacklistedVideo.findById(id)
-}
-
-loadByVideoId = function (id: string) {
+loadByVideoId = function (id: number) {
   const query = {
     where: {
       videoId: id