]>
Commit | Line | Data |
---|---|---|
74d63469 GR |
1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { AccountModel } from '../account/account' | |
b876eaf1 | 3 | import { ScopeNames as VideoScopeNames, VideoModel } from './video' |
74d63469 GR |
4 | import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' |
5 | import { getSort } from '../utils' | |
1ca9f7c3 | 6 | import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership' |
453e83ea | 7 | import * as Bluebird from 'bluebird' |
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, | |
49 | VideoScopeNames.WITH_STREAMING_PLAYLISTS | |
50 | ]), | |
b876eaf1 | 51 | required: true |
74d63469 | 52 | } |
3acc5084 | 53 | ] |
74d63469 | 54 | } |
3acc5084 | 55 | })) |
74d63469 GR |
56 | export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> { |
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 | ||
453e83ea | 121 | static load (id: number): Bluebird<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(), | |
132 | video: { | |
133 | id: this.Video.id, | |
134 | uuid: this.Video.uuid, | |
135 | url: this.Video.url, | |
136 | name: this.Video.name | |
137 | }, | |
138 | createdAt: this.createdAt | |
139 | } | |
140 | } | |
141 | } |