1
0
Fork 0

Add ability to filter my imports by target URL

This commit is contained in:
Chocobozzz 2022-01-19 14:58:16 +01:00
parent 419b520ca4
commit d511df2890
No known key found for this signature in database
GPG key ID: 583A612D890159BE
6 changed files with 59 additions and 20 deletions

View file

@ -30,6 +30,7 @@ import { AccountVideoRateModel } from '../../../models/account/account-video-rat
import { UserModel } from '../../../models/user/user'
import { VideoModel } from '../../../models/video/video'
import { VideoImportModel } from '../../../models/video/video-import'
import { pick } from '@shared/core-utils'
const auditLogger = auditLoggerFactory('users')
@ -133,12 +134,11 @@ async function getUserVideos (req: express.Request, res: express.Response) {
async function getUserVideoImports (req: express.Request, res: express.Response) {
const user = res.locals.oauth.token.User
const resultList = await VideoImportModel.listUserVideoImportsForApi(
user.id,
req.query.start as number,
req.query.count as number,
req.query.sort
)
const resultList = await VideoImportModel.listUserVideoImportsForApi({
userId: user.id,
...pick(req.query, [ 'targetUrl', 'start', 'count', 'sort' ])
})
return res.json(getFormattedObjects(resultList.data, resultList.total))
}

View file

@ -42,7 +42,7 @@ import { generateVideoMiniature } from '../../thumbnail'
async function processVideoImport (job: Job) {
const payload = job.data as VideoImportPayload
const videoImport = await getVideoImportOrDie(payload.videoImportId)
const videoImport = await getVideoImportOrDie(payload)
if (videoImport.state === VideoImportState.CANCELLED) {
logger.info('Do not process import since it has been cancelled', { payload })
return
@ -89,10 +89,10 @@ async function processYoutubeDLImport (job: Job, videoImport: MVideoImportDefaul
)
}
async function getVideoImportOrDie (videoImportId: number) {
const videoImport = await VideoImportModel.loadAndPopulateVideo(videoImportId)
async function getVideoImportOrDie (payload: VideoImportPayload) {
const videoImport = await VideoImportModel.loadAndPopulateVideo(payload.videoImportId)
if (!videoImport || !videoImport.Video) {
throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.')
throw new Error(`Cannot import video ${payload.videoImportId}: the video import or video linked to this import does not exist anymore.`)
}
return videoImport

View file

@ -1,3 +1,4 @@
import { WhereOptions } from 'sequelize'
import {
AfterUpdate,
AllowNull,
@ -125,7 +126,20 @@ export class VideoImportModel extends Model<Partial<AttributesOnly<VideoImportMo
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: [
@ -138,9 +152,7 @@ export class VideoImportModel extends Model<Partial<AttributesOnly<VideoImportMo
offset: start,
limit: count,
order: getSort(sort),
where: {
userId
}
where
}
return VideoImportModel.findAndCountAll<MVideoImportDefault>(query)

View file

@ -219,6 +219,14 @@ describe('Test video imports', function () {
expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
})
it('Should filter my imports on target URL', async function () {
const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ targetUrl: FIXTURE_URLS.youtube })
expect(total).to.equal(1)
expect(videoImports).to.have.lengthOf(1)
expect(videoImports[0].targetUrl).to.equal(FIXTURE_URLS.youtube)
})
it('Should have the video listed on the two instances', async function () {
this.timeout(120_000)
@ -459,6 +467,10 @@ describe('Test video imports', function () {
const { data } = await server.imports.getMyVideoImports()
expect(data).to.have.lengthOf(0)
})
after(async function () {
await cleanupTests([ server ])
})
})
describe('Auto update', function () {

View file

@ -136,9 +136,26 @@ describe('Test CLI wrapper', function () {
expect(videoDetails.channel.name).to.equal('user_channel')
expect(videoDetails.support).to.equal('super support text')
expect(videoDetails.nsfw).to.be.false
})
it('Should not import again the same video', async function () {
if (areHttpImportTestsDisabled()) return
this.timeout(60000)
const params = `--target-url ${FIXTURE_URLS.youtube} --channel-name user_channel`
await cliCommand.execWithEnv(`${cmd} import ${params}`)
await waitJobs([ server ])
const { total, data } = await server.videos.list()
expect(total).to.equal(2)
const videos = data.filter(v => v.name === 'small video - youtube')
expect(videos).to.have.lengthOf(1)
// So we can reimport it
await server.videos.remove({ token: userAccessToken, id: video.id })
await server.videos.remove({ token: userAccessToken, id: videos[0].id })
})
it('Should import and override some imported attributes', async function () {

View file

@ -56,18 +56,16 @@ export class ImportsCommand extends AbstractCommand {
getMyVideoImports (options: OverrideCommandOptions & {
sort?: string
targetUrl?: string
} = {}) {
const { sort } = options
const { sort, targetUrl } = options
const path = '/api/v1/users/me/videos/imports'
const query = {}
if (sort) query['sort'] = sort
return this.getRequestBody<ResultList<VideoImport>>({
...options,
path,
query: { sort },
query: { sort, targetUrl },
implicitToken: true,
defaultExpectedStatus: HttpStatusCode.OK_200
})