2023-07-31 08:34:36 -04:00
|
|
|
import { VideoChannelSync, VideoChannelSyncState, type VideoChannelSyncStateType } from '@peertube/peertube-models'
|
|
|
|
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'
|
|
|
|
import { isVideoChannelSyncStateValid } from '@server/helpers/custom-validators/video-channel-syncs.js'
|
|
|
|
import { CONSTRAINTS_FIELDS, VIDEO_CHANNEL_SYNC_STATE } from '@server/initializers/constants.js'
|
|
|
|
import { MChannelSync, MChannelSyncChannel, MChannelSyncFormattable } from '@server/types/models/index.js'
|
2022-08-10 03:53:39 -04:00
|
|
|
import { Op } from 'sequelize'
|
|
|
|
import {
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
DefaultScope,
|
|
|
|
ForeignKey,
|
2024-02-22 04:12:04 -05:00
|
|
|
Is, Table,
|
2022-08-10 03:53:39 -04:00
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { AccountModel } from '../account/account.js'
|
2024-02-22 04:12:04 -05:00
|
|
|
import { SequelizeModel, getChannelSyncSort, throwIfNotValid } from '../shared/index.js'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { UserModel } from '../user/user.js'
|
|
|
|
import { VideoChannelModel } from './video-channel.js'
|
2022-08-10 03:53:39 -04:00
|
|
|
|
|
|
|
@DefaultScope(() => ({
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoChannelModel, // Default scope includes avatar and server
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoChannelSync',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoChannelId' ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 04:12:04 -05:00
|
|
|
export class VideoChannelSyncModel extends SequelizeModel<VideoChannelSyncModel> {
|
2022-08-10 03:53:39 -04:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2023-07-31 08:34:36 -04:00
|
|
|
@AllowNull(false)
|
|
|
|
@Default(VideoChannelSyncState.WAITING_FIRST_RUN)
|
|
|
|
@Is('VideoChannelSyncState', value => throwIfNotValid(value, isVideoChannelSyncStateValid, 'state'))
|
|
|
|
@Column
|
|
|
|
state: VideoChannelSyncStateType
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column(DataType.DATE)
|
|
|
|
lastSyncAt: Date
|
|
|
|
|
2022-08-10 03:53:39 -04:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoChannelModel)
|
|
|
|
@Column
|
|
|
|
videoChannelId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoChannelModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 08:34:36 -04:00
|
|
|
VideoChannel: Awaited<VideoChannelModel>
|
2022-08-10 03:53:39 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|