]>
Commit | Line | Data |
---|---|---|
0c9668f7 C |
1 | import { FindOptions, literal } from 'sequelize' |
2 | import { AllowNull, Column, CreatedAt, HasMany, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
3 | import { MRunnerRegistrationToken } from '@server/types/models/runners' | |
4 | import { RunnerRegistrationToken } from '@shared/models' | |
5 | import { AttributesOnly } from '@shared/typescript-utils' | |
6 | import { getSort } from '../shared' | |
7 | import { RunnerModel } from './runner' | |
8 | ||
9 | /** | |
10 | * | |
11 | * Tokens used by PeerTube runners to register themselves to the PeerTube instance | |
12 | * | |
13 | */ | |
14 | ||
15 | @Table({ | |
16 | tableName: 'runnerRegistrationToken', | |
17 | indexes: [ | |
18 | { | |
19 | fields: [ 'registrationToken' ], | |
20 | unique: true | |
21 | } | |
22 | ] | |
23 | }) | |
24 | export class RunnerRegistrationTokenModel extends Model<Partial<AttributesOnly<RunnerRegistrationTokenModel>>> { | |
25 | ||
26 | @AllowNull(false) | |
27 | @Column | |
28 | registrationToken: string | |
29 | ||
30 | @CreatedAt | |
31 | createdAt: Date | |
32 | ||
33 | @UpdatedAt | |
34 | updatedAt: Date | |
35 | ||
36 | @HasMany(() => RunnerModel, { | |
37 | foreignKey: { | |
38 | allowNull: true | |
39 | }, | |
40 | onDelete: 'cascade' | |
41 | }) | |
42 | Runners: RunnerModel[] | |
43 | ||
44 | static load (id: number) { | |
45 | return RunnerRegistrationTokenModel.findByPk(id) | |
46 | } | |
47 | ||
48 | static loadByRegistrationToken (registrationToken: string) { | |
49 | const query = { | |
50 | where: { registrationToken } | |
51 | } | |
52 | ||
53 | return RunnerRegistrationTokenModel.findOne(query) | |
54 | } | |
55 | ||
56 | static countTotal () { | |
57 | return RunnerRegistrationTokenModel.unscoped().count() | |
58 | } | |
59 | ||
60 | static listForApi (options: { | |
61 | start: number | |
62 | count: number | |
63 | sort: string | |
64 | }) { | |
65 | const { start, count, sort } = options | |
66 | ||
67 | const query: FindOptions = { | |
68 | attributes: { | |
69 | include: [ | |
70 | [ | |
71 | literal('(SELECT COUNT(*) FROM "runner" WHERE "runner"."runnerRegistrationTokenId" = "RunnerRegistrationTokenModel"."id")'), | |
72 | 'registeredRunnersCount' | |
73 | ] | |
74 | ] | |
75 | }, | |
76 | offset: start, | |
77 | limit: count, | |
78 | order: getSort(sort) | |
79 | } | |
80 | ||
81 | return Promise.all([ | |
82 | RunnerRegistrationTokenModel.count(query), | |
83 | RunnerRegistrationTokenModel.findAll<MRunnerRegistrationToken>(query) | |
84 | ]).then(([ total, data ]) => ({ total, data })) | |
85 | } | |
86 | ||
87 | // --------------------------------------------------------------------------- | |
88 | ||
89 | toFormattedJSON (this: MRunnerRegistrationToken): RunnerRegistrationToken { | |
90 | const registeredRunnersCount = this.get('registeredRunnersCount') as number | |
91 | ||
92 | return { | |
93 | id: this.id, | |
94 | ||
95 | registrationToken: this.registrationToken, | |
96 | ||
97 | createdAt: this.createdAt, | |
98 | updatedAt: this.updatedAt, | |
99 | ||
100 | registeredRunnersCount | |
101 | } | |
102 | } | |
103 | } |