diff options
Diffstat (limited to 'server/models/video-blacklist.ts')
-rw-r--r-- | server/models/video-blacklist.ts | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/server/models/video-blacklist.ts b/server/models/video-blacklist.ts new file mode 100644 index 000000000..1f00702c7 --- /dev/null +++ b/server/models/video-blacklist.ts | |||
@@ -0,0 +1,87 @@ | |||
1 | import { getSort } from './utils' | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | module.exports = function (sequelize, DataTypes) { | ||
6 | const BlacklistedVideo = sequelize.define('BlacklistedVideo', | ||
7 | {}, | ||
8 | { | ||
9 | indexes: [ | ||
10 | { | ||
11 | fields: [ 'videoId' ], | ||
12 | unique: true | ||
13 | } | ||
14 | ], | ||
15 | classMethods: { | ||
16 | associate, | ||
17 | |||
18 | countTotal, | ||
19 | list, | ||
20 | listForApi, | ||
21 | loadById, | ||
22 | loadByVideoId | ||
23 | }, | ||
24 | instanceMethods: { | ||
25 | toFormatedJSON | ||
26 | }, | ||
27 | hooks: {} | ||
28 | } | ||
29 | ) | ||
30 | |||
31 | return BlacklistedVideo | ||
32 | } | ||
33 | |||
34 | // ------------------------------ METHODS ------------------------------ | ||
35 | |||
36 | function toFormatedJSON () { | ||
37 | return { | ||
38 | id: this.id, | ||
39 | videoId: this.videoId, | ||
40 | createdAt: this.createdAt | ||
41 | } | ||
42 | } | ||
43 | |||
44 | // ------------------------------ STATICS ------------------------------ | ||
45 | |||
46 | function associate (models) { | ||
47 | this.belongsTo(models.Video, { | ||
48 | foreignKey: 'videoId', | ||
49 | onDelete: 'cascade' | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | function countTotal (callback) { | ||
54 | return this.count().asCallback(callback) | ||
55 | } | ||
56 | |||
57 | function list (callback) { | ||
58 | return this.findAll().asCallback(callback) | ||
59 | } | ||
60 | |||
61 | function listForApi (start, count, sort, callback) { | ||
62 | const query = { | ||
63 | offset: start, | ||
64 | limit: count, | ||
65 | order: [ getSort(sort) ] | ||
66 | } | ||
67 | |||
68 | return this.findAndCountAll(query).asCallback(function (err, result) { | ||
69 | if (err) return callback(err) | ||
70 | |||
71 | return callback(null, result.rows, result.count) | ||
72 | }) | ||
73 | } | ||
74 | |||
75 | function loadById (id, callback) { | ||
76 | return this.findById(id).asCallback(callback) | ||
77 | } | ||
78 | |||
79 | function loadByVideoId (id, callback) { | ||
80 | const query = { | ||
81 | where: { | ||
82 | videoId: id | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return this.find(query).asCallback(callback) | ||
87 | } | ||