diff options
Diffstat (limited to 'server/models/video/video-blacklist.ts')
-rw-r--r-- | server/models/video/video-blacklist.ts | 142 |
1 files changed, 59 insertions, 83 deletions
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index ae8286285..6db562719 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts | |||
@@ -1,104 +1,80 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2 | |||
3 | import { SortType } from '../../helpers' | 2 | import { SortType } from '../../helpers' |
4 | import { addMethodsToModel, getSortOnModel } from '../utils' | 3 | import { getSortOnModel } from '../utils' |
5 | import { VideoInstance } from './video-interface' | 4 | import { VideoModel } from './video' |
6 | import { | ||
7 | BlacklistedVideoInstance, | ||
8 | BlacklistedVideoAttributes, | ||
9 | |||
10 | BlacklistedVideoMethods | ||
11 | } from './video-blacklist-interface' | ||
12 | |||
13 | let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes> | ||
14 | let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON | ||
15 | let listForApi: BlacklistedVideoMethods.ListForApi | ||
16 | let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId | ||
17 | 5 | ||
18 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | 6 | @Table({ |
19 | BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo', | 7 | tableName: 'videoBlacklist', |
20 | {}, | 8 | indexes: [ |
21 | { | 9 | { |
22 | indexes: [ | 10 | fields: [ 'videoId' ], |
23 | { | 11 | unique: true |
24 | fields: [ 'videoId' ], | ||
25 | unique: true | ||
26 | } | ||
27 | ] | ||
28 | } | 12 | } |
29 | ) | ||
30 | |||
31 | const classMethods = [ | ||
32 | associate, | ||
33 | |||
34 | listForApi, | ||
35 | loadByVideoId | ||
36 | ] | 13 | ] |
37 | const instanceMethods = [ | 14 | }) |
38 | toFormattedJSON | 15 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { |
39 | ] | ||
40 | addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) | ||
41 | |||
42 | return BlacklistedVideo | ||
43 | } | ||
44 | 16 | ||
45 | // ------------------------------ METHODS ------------------------------ | 17 | @CreatedAt |
18 | createdAt: Date | ||
46 | 19 | ||
47 | toFormattedJSON = function (this: BlacklistedVideoInstance) { | 20 | @UpdatedAt |
48 | let video: VideoInstance | 21 | updatedAt: Date |
49 | |||
50 | video = this.Video | ||
51 | |||
52 | return { | ||
53 | id: this.id, | ||
54 | videoId: this.videoId, | ||
55 | createdAt: this.createdAt, | ||
56 | updatedAt: this.updatedAt, | ||
57 | name: video.name, | ||
58 | uuid: video.uuid, | ||
59 | description: video.description, | ||
60 | duration: video.duration, | ||
61 | views: video.views, | ||
62 | likes: video.likes, | ||
63 | dislikes: video.dislikes, | ||
64 | nsfw: video.nsfw | ||
65 | } | ||
66 | } | ||
67 | 22 | ||
68 | // ------------------------------ STATICS ------------------------------ | 23 | @ForeignKey(() => VideoModel) |
24 | @Column | ||
25 | videoId: number | ||
69 | 26 | ||
70 | function associate (models) { | 27 | @BelongsTo(() => VideoModel, { |
71 | BlacklistedVideo.belongsTo(models.Video, { | ||
72 | foreignKey: { | 28 | foreignKey: { |
73 | name: 'videoId', | ||
74 | allowNull: false | 29 | allowNull: false |
75 | }, | 30 | }, |
76 | onDelete: 'CASCADE' | 31 | onDelete: 'cascade' |
77 | }) | 32 | }) |
78 | } | 33 | Video: VideoModel |
34 | |||
35 | static listForApi (start: number, count: number, sort: SortType) { | ||
36 | const query = { | ||
37 | offset: start, | ||
38 | limit: count, | ||
39 | order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], | ||
40 | include: [ { model: VideoModel } ] | ||
41 | } | ||
79 | 42 | ||
80 | listForApi = function (start: number, count: number, sort: SortType) { | 43 | return VideoBlacklistModel.findAndCountAll(query) |
81 | const query = { | 44 | .then(({ rows, count }) => { |
82 | offset: start, | 45 | return { |
83 | limit: count, | 46 | data: rows, |
84 | order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], | 47 | total: count |
85 | include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] | 48 | } |
49 | }) | ||
86 | } | 50 | } |
87 | 51 | ||
88 | return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { | 52 | static loadByVideoId (id: number) { |
89 | return { | 53 | const query = { |
90 | data: rows, | 54 | where: { |
91 | total: count | 55 | videoId: id |
56 | } | ||
92 | } | 57 | } |
93 | }) | ||
94 | } | ||
95 | 58 | ||
96 | loadByVideoId = function (id: number) { | 59 | return VideoBlacklistModel.findOne(query) |
97 | const query = { | ||
98 | where: { | ||
99 | videoId: id | ||
100 | } | ||
101 | } | 60 | } |
102 | 61 | ||
103 | return BlacklistedVideo.findOne(query) | 62 | toFormattedJSON () { |
63 | const video = this.Video | ||
64 | |||
65 | return { | ||
66 | id: this.id, | ||
67 | videoId: this.videoId, | ||
68 | createdAt: this.createdAt, | ||
69 | updatedAt: this.updatedAt, | ||
70 | name: video.name, | ||
71 | uuid: video.uuid, | ||
72 | description: video.description, | ||
73 | duration: video.duration, | ||
74 | views: video.views, | ||
75 | likes: video.likes, | ||
76 | dislikes: video.dislikes, | ||
77 | nsfw: video.nsfw | ||
78 | } | ||
79 | } | ||
104 | } | 80 | } |