+import { WhereOptions } from 'sequelize'
import {
AfterUpdate,
AllowNull,
Table,
UpdatedAt
} from 'sequelize-typescript'
+import { afterCommitIfTransaction } from '@server/helpers/database-utils'
+import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import'
+import { VideoImport, VideoImportState } from '@shared/models'
+import { AttributesOnly } from '@shared/typescript-utils'
+import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
+import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants'
+import { UserModel } from '../user/user'
import { getSort, throwIfNotValid } from '../utils'
import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
-import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
-import { VideoImport, VideoImportState } from '../../../shared'
-import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
-import { UserModel } from '../account/user'
-import * as Bluebird from 'bluebird'
-import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import'
@DefaultScope(() => ({
include: [
}
]
})
-export class VideoImportModel extends Model<VideoImportModel> {
+export class VideoImportModel extends Model<Partial<AttributesOnly<VideoImportModel>>> {
@CreatedAt
createdAt: Date
@AfterUpdate
static deleteVideoIfFailed (instance: VideoImportModel, options) {
if (instance.state === VideoImportState.FAILED) {
- return instance.Video.destroy({ transaction: options.transaction })
+ return afterCommitIfTransaction(options.transaction, () => instance.Video.destroy())
}
return undefined
}
- static loadAndPopulateVideo (id: number): Bluebird<MVideoImportDefault> {
+ static loadAndPopulateVideo (id: number): Promise<MVideoImportDefault> {
return VideoImportModel.findByPk(id)
}
- static listUserVideoImportsForApi (userId: number, start: number, count: number, sort: string) {
+ static listUserVideoImportsForApi (options: {
+ userId: number
+ start: number
+ count: number
+ sort: string
+
+ targetUrl?: string
+ }) {
+ const { userId, start, count, sort, targetUrl } = options
+
+ const where: WhereOptions = { userId }
+
+ if (targetUrl) where['targetUrl'] = targetUrl
+
const query = {
distinct: true,
include: [
offset: start,
limit: count,
order: getSort(sort),
- where: {
- userId
- }
+ where
}
- return VideoImportModel.findAndCountAll<MVideoImportDefault>(query)
- .then(({ rows, count }) => {
- return {
- data: rows,
- total: count
- }
- })
+ return Promise.all([
+ VideoImportModel.unscoped().count(query),
+ VideoImportModel.findAll<MVideoImportDefault>(query)
+ ]).then(([ total, data ]) => ({ total, data }))
}
getTargetIdentifier () {