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