Fetch remote AP objects
[oweals/peertube.git] / server / models / video / video-blacklist.ts
1 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { SortType } from '../../helpers/utils'
3 import { getSortOnModel } from '../utils'
4 import { VideoModel } from './video'
5
6 @Table({
7   tableName: 'videoBlacklist',
8   indexes: [
9     {
10       fields: [ 'videoId' ],
11       unique: true
12     }
13   ]
14 })
15 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
16
17   @CreatedAt
18   createdAt: Date
19
20   @UpdatedAt
21   updatedAt: Date
22
23   @ForeignKey(() => VideoModel)
24   @Column
25   videoId: number
26
27   @BelongsTo(() => VideoModel, {
28     foreignKey: {
29       allowNull: false
30     },
31     onDelete: 'cascade'
32   })
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     }
42
43     return VideoBlacklistModel.findAndCountAll(query)
44       .then(({ rows, count }) => {
45         return {
46           data: rows,
47           total: count
48         }
49       })
50   }
51
52   static loadByVideoId (id: number) {
53     const query = {
54       where: {
55         videoId: id
56       }
57     }
58
59     return VideoBlacklistModel.findOne(query)
60   }
61
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   }
80 }