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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
import { Op } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
Is,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
import { isVideoChannelSyncStateValid } from '@server/helpers/custom-validators/video-channel-syncs'
import { CONSTRAINTS_FIELDS, VIDEO_CHANNEL_SYNC_STATE } from '@server/initializers/constants'
import { MChannelSync, MChannelSyncChannel, MChannelSyncFormattable } from '@server/types/models'
import { VideoChannelSync, VideoChannelSyncState } from '@shared/models'
import { AttributesOnly } from '@shared/typescript-utils'
import { AccountModel } from '../account/account'
import { UserModel } from '../user/user'
import { getChannelSyncSort, throwIfNotValid } from '../shared'
import { VideoChannelModel } from './video-channel'
@DefaultScope(() => ({
include: [
{
model: VideoChannelModel, // Default scope includes avatar and server
required: true
}
]
}))
@Table({
tableName: 'videoChannelSync',
indexes: [
{
fields: [ 'videoChannelId' ]
}
]
})
export class VideoChannelSyncModel extends Model<Partial<AttributesOnly<VideoChannelSyncModel>>> {
@AllowNull(false)
@Default(null)
@Is('VideoChannelExternalChannelUrl', value => throwIfNotValid(value, isUrlValid, 'externalChannelUrl', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNEL_SYNCS.EXTERNAL_CHANNEL_URL.max))
externalChannelUrl: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoChannelModel)
@Column
videoChannelId: number
@BelongsTo(() => VideoChannelModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
VideoChannel: VideoChannelModel
@AllowNull(false)
@Default(VideoChannelSyncState.WAITING_FIRST_RUN)
@Is('VideoChannelSyncState', value => throwIfNotValid(value, isVideoChannelSyncStateValid, 'state'))
@Column
state: VideoChannelSyncState
@AllowNull(true)
@Column(DataType.DATE)
lastSyncAt: Date
static listByAccountForAPI (options: {
accountId: number
start: number
count: number
sort: string
}) {
const getQuery = (forCount: boolean) => {
const videoChannelModel = forCount
? VideoChannelModel.unscoped()
: VideoChannelModel
return {
offset: options.start,
limit: options.count,
order: getChannelSyncSort(options.sort),
include: [
{
model: videoChannelModel,
required: true,
where: {
accountId: options.accountId
}
}
]
}
}
return Promise.all([
VideoChannelSyncModel.unscoped().count(getQuery(true)),
VideoChannelSyncModel.unscoped().findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static countByAccount (accountId: number) {
const query = {
include: [
{
model: VideoChannelModel.unscoped(),
required: true,
where: {
accountId
}
}
]
}
return VideoChannelSyncModel.unscoped().count(query)
}
static loadWithChannel (id: number): Promise<MChannelSyncChannel> {
return VideoChannelSyncModel.findByPk(id)
}
static async listSyncs (): Promise<MChannelSync[]> {
const query = {
include: [
{
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
model: AccountModel.unscoped(),
required: true,
include: [ {
attributes: [],
model: UserModel.unscoped(),
required: true,
where: {
videoQuota: {
[Op.ne]: 0
},
videoQuotaDaily: {
[Op.ne]: 0
}
}
} ]
}
]
}
]
}
return VideoChannelSyncModel.unscoped().findAll(query)
}
toFormattedJSON (this: MChannelSyncFormattable): VideoChannelSync {
return {
id: this.id,
state: {
id: this.state,
label: VIDEO_CHANNEL_SYNC_STATE[this.state]
},
externalChannelUrl: this.externalChannelUrl,
createdAt: this.createdAt.toISOString(),
channel: this.VideoChannel.toFormattedSummaryJSON(),
lastSyncAt: this.lastSyncAt?.toISOString()
}
}
}
|