]>
Commit | Line | Data |
---|---|---|
74d63469 | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
b49f22d8 | 2 | import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' |
16c016e8 | 3 | import { AttributesOnly } from '@shared/core-utils' |
74d63469 | 4 | import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' |
b49f22d8 | 5 | import { AccountModel } from '../account/account' |
74d63469 | 6 | import { getSort } from '../utils' |
b49f22d8 | 7 | import { ScopeNames as VideoScopeNames, VideoModel } from './video' |
74d63469 GR |
8 | |
9 | enum ScopeNames { | |
b876eaf1 C |
10 | WITH_ACCOUNTS = 'WITH_ACCOUNTS', |
11 | WITH_VIDEO = 'WITH_VIDEO' | |
74d63469 GR |
12 | } |
13 | ||
14 | @Table({ | |
15 | tableName: 'videoChangeOwnership', | |
16 | indexes: [ | |
17 | { | |
b876eaf1 | 18 | fields: [ 'videoId' ] |
74d63469 GR |
19 | }, |
20 | { | |
b876eaf1 | 21 | fields: [ 'initiatorAccountId' ] |
74d63469 GR |
22 | }, |
23 | { | |
b876eaf1 | 24 | fields: [ 'nextOwnerAccountId' ] |
74d63469 GR |
25 | } |
26 | ] | |
27 | }) | |
3acc5084 | 28 | @Scopes(() => ({ |
b876eaf1 | 29 | [ScopeNames.WITH_ACCOUNTS]: { |
74d63469 GR |
30 | include: [ |
31 | { | |
3acc5084 | 32 | model: AccountModel, |
74d63469 GR |
33 | as: 'Initiator', |
34 | required: true | |
35 | }, | |
36 | { | |
3acc5084 | 37 | model: AccountModel, |
74d63469 GR |
38 | as: 'NextOwner', |
39 | required: true | |
b876eaf1 C |
40 | } |
41 | ] | |
42 | }, | |
43 | [ScopeNames.WITH_VIDEO]: { | |
44 | include: [ | |
74d63469 | 45 | { |
d7a25329 C |
46 | model: VideoModel.scope([ |
47 | VideoScopeNames.WITH_THUMBNAILS, | |
48 | VideoScopeNames.WITH_WEBTORRENT_FILES, | |
4c9e9d2e RK |
49 | VideoScopeNames.WITH_STREAMING_PLAYLISTS, |
50 | VideoScopeNames.WITH_ACCOUNT_DETAILS | |
d7a25329 | 51 | ]), |
b876eaf1 | 52 | required: true |
74d63469 | 53 | } |
3acc5084 | 54 | ] |
74d63469 | 55 | } |
3acc5084 | 56 | })) |
16c016e8 | 57 | export class VideoChangeOwnershipModel extends Model<Partial<AttributesOnly<VideoChangeOwnershipModel>>> { |
74d63469 GR |
58 | @CreatedAt |
59 | createdAt: Date | |
60 | ||
61 | @UpdatedAt | |
62 | updatedAt: Date | |
63 | ||
64 | @AllowNull(false) | |
65 | @Column | |
66 | status: VideoChangeOwnershipStatus | |
67 | ||
68 | @ForeignKey(() => AccountModel) | |
69 | @Column | |
70 | initiatorAccountId: number | |
71 | ||
72 | @BelongsTo(() => AccountModel, { | |
73 | foreignKey: { | |
74 | name: 'initiatorAccountId', | |
75 | allowNull: false | |
76 | }, | |
77 | onDelete: 'cascade' | |
78 | }) | |
79 | Initiator: AccountModel | |
80 | ||
81 | @ForeignKey(() => AccountModel) | |
82 | @Column | |
83 | nextOwnerAccountId: number | |
84 | ||
85 | @BelongsTo(() => AccountModel, { | |
86 | foreignKey: { | |
87 | name: 'nextOwnerAccountId', | |
88 | allowNull: false | |
89 | }, | |
90 | onDelete: 'cascade' | |
91 | }) | |
92 | NextOwner: AccountModel | |
93 | ||
94 | @ForeignKey(() => VideoModel) | |
95 | @Column | |
96 | videoId: number | |
97 | ||
98 | @BelongsTo(() => VideoModel, { | |
99 | foreignKey: { | |
100 | allowNull: false | |
101 | }, | |
102 | onDelete: 'cascade' | |
103 | }) | |
104 | Video: VideoModel | |
105 | ||
106 | static listForApi (nextOwnerId: number, start: number, count: number, sort: string) { | |
5cf84858 | 107 | const query = { |
74d63469 GR |
108 | offset: start, |
109 | limit: count, | |
110 | order: getSort(sort), | |
111 | where: { | |
112 | nextOwnerAccountId: nextOwnerId | |
113 | } | |
5cf84858 C |
114 | } |
115 | ||
b876eaf1 C |
116 | return Promise.all([ |
117 | VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query), | |
453e83ea | 118 | VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query) |
b876eaf1 | 119 | ]).then(([ count, rows ]) => ({ total: count, data: rows })) |
74d63469 GR |
120 | } |
121 | ||
b49f22d8 | 122 | static load (id: number): Promise<MVideoChangeOwnershipFull> { |
b876eaf1 C |
123 | return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]) |
124 | .findByPk(id) | |
74d63469 GR |
125 | } |
126 | ||
1ca9f7c3 | 127 | toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership { |
74d63469 GR |
128 | return { |
129 | id: this.id, | |
130 | status: this.status, | |
131 | initiatorAccount: this.Initiator.toFormattedJSON(), | |
132 | nextOwnerAccount: this.NextOwner.toFormattedJSON(), | |
4c9e9d2e | 133 | video: this.Video.toFormattedJSON(), |
74d63469 GR |
134 | createdAt: this.createdAt |
135 | } | |
136 | } | |
137 | } |