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