From a54618880c394ad7571f3f3222dc96ec2dd10d9a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 11:21:30 +0200 Subject: Introduce channels command --- shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/shared/abstract-command.ts | 21 +++ shared/extra-utils/videos/channels-command.ts | 157 +++++++++++++++++++++ shared/extra-utils/videos/channels.ts | 20 +++ shared/extra-utils/videos/index.ts | 5 +- shared/extra-utils/videos/video-channels.ts | 192 -------------------------- 6 files changed, 204 insertions(+), 194 deletions(-) create mode 100644 shared/extra-utils/videos/channels-command.ts create mode 100644 shared/extra-utils/videos/channels.ts delete mode 100644 shared/extra-utils/videos/video-channels.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 6a1dadbcc..68e10af5f 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -22,6 +22,7 @@ import { BlacklistCommand, CaptionsCommand, ChangeOwnershipCommand, + ChannelsCommand, HistoryCommand, ImportsCommand, LiveCommand, @@ -119,6 +120,7 @@ interface ServerInfo { historyCommand?: HistoryCommand importsCommand?: ImportsCommand streamingPlaylistsCommand?: StreamingPlaylistsCommand + channelsCommand?: ChannelsCommand } function parallelTests () { @@ -353,6 +355,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.historyCommand = new HistoryCommand(server) server.importsCommand = new ImportsCommand(server) server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server) + server.channelsCommand = new ChannelsCommand(server) res(server) }) diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index be368376f..6a9ab1348 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -1,4 +1,6 @@ +import { isAbsolute, join } from 'path' import { HttpStatusCode } from '@shared/core-utils' +import { root } from '../miscs' import { makeDeleteRequest, makeGetRequest, @@ -146,6 +148,25 @@ abstract class AbstractCommand { }) } + protected updateImageRequest (options: InternalCommonCommandOptions & { + fixture: string + fieldname: string + }) { + let filePath = '' + if (isAbsolute(options.fixture)) { + filePath = options.fixture + } else { + filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) + } + + return this.postUploadRequest({ + ...options, + + fields: {}, + attaches: { [options.fieldname]: filePath } + }) + } + private buildCommonRequestOptions (options: InternalCommonCommandOptions) { const { path } = options diff --git a/shared/extra-utils/videos/channels-command.ts b/shared/extra-utils/videos/channels-command.ts new file mode 100644 index 000000000..a98c5cc93 --- /dev/null +++ b/shared/extra-utils/videos/channels-command.ts @@ -0,0 +1,157 @@ +import { pick } from 'lodash' +import { ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' +import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' +import { unwrapBody } from '../requests' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class ChannelsCommand extends AbstractCommand { + + list (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + withStats?: boolean + } = {}) { + const path = '/api/v1/video-channels' + + return this.getRequestBody>({ + ...options, + + path, + query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]), + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + listByAccount (options: OverrideCommandOptions & { + accountName: string + start?: number + count?: number + sort?: string + withStats?: boolean + search?: string + }) { + const { accountName, sort = 'createdAt' } = options + const path = '/api/v1/accounts/' + accountName + '/video-channels' + + return this.getRequestBody>({ + ...options, + + path, + query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + async create (options: OverrideCommandOptions & { + attributes: VideoChannelCreate + }) { + const path = '/api/v1/video-channels/' + + // Default attributes + const defaultAttributes = { + displayName: 'my super video channel', + description: 'my super channel description', + support: 'my super channel support' + } + const attributes = { ...defaultAttributes, ...options.attributes } + + const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({ + ...options, + + path, + fields: attributes, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + + return body.videoChannel + } + + update (options: OverrideCommandOptions & { + channelName: string + attributes: VideoChannelUpdate + }) { + const { channelName, attributes } = options + const path = '/api/v1/video-channels/' + channelName + + return this.putBodyRequest({ + ...options, + + path, + fields: attributes, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + delete (options: OverrideCommandOptions & { + channelName: string + }) { + const path = '/api/v1/video-channels/' + options.channelName + + return this.deleteRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + get (options: OverrideCommandOptions & { + channelName: string + }) { + const path = '/api/v1/video-channels/' + options.channelName + + return this.getRequestBody({ + ...options, + + path, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + updateImage (options: OverrideCommandOptions & { + fixture: string + channelName: string | number + type: 'avatar' | 'banner' + }) { + const { channelName, fixture, type } = options + + const path = `/api/v1/video-channels/${channelName}/${type}/pick` + + return this.updateImageRequest({ + ...options, + + path, + fixture, + fieldname: type + 'file', + + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + deleteImage (options: OverrideCommandOptions & { + channelName: string | number + type: 'avatar' | 'banner' + }) { + const { channelName, type } = options + + const path = `/api/v1/video-channels/${channelName}/${type}` + + return this.deleteRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } +} diff --git a/shared/extra-utils/videos/channels.ts b/shared/extra-utils/videos/channels.ts new file mode 100644 index 000000000..a77543c92 --- /dev/null +++ b/shared/extra-utils/videos/channels.ts @@ -0,0 +1,20 @@ +import { User } from '../../models/users/user.model' +import { ServerInfo } from '../server/servers' +import { getMyUserInformation } from '../users/users' + +function setDefaultVideoChannel (servers: ServerInfo[]) { + const tasks: Promise[] = [] + + for (const server of servers) { + const p = getMyUserInformation(server.url, server.accessToken) + .then(res => { server.videoChannel = (res.body as User).videoChannels[0] }) + + tasks.push(p) + } + + return Promise.all(tasks) +} + +export { + setDefaultVideoChannel +} diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts index f87ae8eea..3bc219281 100644 --- a/shared/extra-utils/videos/index.ts +++ b/shared/extra-utils/videos/index.ts @@ -1,7 +1,9 @@ export * from './blacklist-command' -export * from './captions' export * from './captions-command' +export * from './captions' export * from './change-ownership-command' +export * from './channels' +export * from './channels-command' export * from './history-command' export * from './imports-command' export * from './live-command' @@ -11,6 +13,5 @@ export * from './playlists' export * from './services-command' export * from './streaming-playlists-command' export * from './streaming-playlists' -export * from './video-channels' export * from './video-comments' export * from './videos' diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts deleted file mode 100644 index 0aab93e52..000000000 --- a/shared/extra-utils/videos/video-channels.ts +++ /dev/null @@ -1,192 +0,0 @@ -/* eslint-disable @typescript-eslint/no-floating-promises */ - -import * as request from 'supertest' -import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' -import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' -import { makeDeleteRequest, makeGetRequest, updateImageRequest } from '../requests/requests' -import { ServerInfo } from '../server/servers' -import { MyUser, User } from '../../models/users/user.model' -import { getMyUserInformation } from '../users/users' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) { - const path = '/api/v1/video-channels' - - const req = request(url) - .get(path) - .query({ start: start }) - .query({ count: count }) - - if (sort) req.query({ sort }) - if (withStats) req.query({ withStats }) - - return req.set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function getAccountVideoChannelsList (parameters: { - url: string - accountName: string - start?: number - count?: number - sort?: string - specialStatus?: HttpStatusCode - withStats?: boolean - search?: string -}) { - const { - url, - accountName, - start, - count, - sort = 'createdAt', - specialStatus = HttpStatusCode.OK_200, - withStats = false, - search - } = parameters - - const path = '/api/v1/accounts/' + accountName + '/video-channels' - - return makeGetRequest({ - url, - path, - query: { - start, - count, - sort, - withStats, - search - }, - statusCodeExpected: specialStatus - }) -} - -function addVideoChannel ( - url: string, - token: string, - videoChannelAttributesArg: VideoChannelCreate, - expectedStatus = HttpStatusCode.OK_200 -) { - const path = '/api/v1/video-channels/' - - // Default attributes - let attributes = { - displayName: 'my super video channel', - description: 'my super channel description', - support: 'my super channel support' - } - attributes = Object.assign(attributes, videoChannelAttributesArg) - - return request(url) - .post(path) - .send(attributes) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) -} - -function updateVideoChannel ( - url: string, - token: string, - channelName: string, - attributes: VideoChannelUpdate, - expectedStatus = HttpStatusCode.NO_CONTENT_204 -) { - const body: any = {} - const path = '/api/v1/video-channels/' + channelName - - if (attributes.displayName) body.displayName = attributes.displayName - if (attributes.description) body.description = attributes.description - if (attributes.support) body.support = attributes.support - if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate - - return request(url) - .put(path) - .send(body) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) -} - -function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { - const path = '/api/v1/video-channels/' + channelName - - return request(url) - .delete(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) -} - -function getVideoChannel (url: string, channelName: string) { - const path = '/api/v1/video-channels/' + channelName - - return request(url) - .get(path) - .set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function updateVideoChannelImage (options: { - url: string - accessToken: string - fixture: string - videoChannelName: string | number - type: 'avatar' | 'banner' -}) { - const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}/pick` - - return updateImageRequest({ ...options, path, fieldname: options.type + 'file' }) -} - -function deleteVideoChannelImage (options: { - url: string - accessToken: string - videoChannelName: string | number - type: 'avatar' | 'banner' -}) { - const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}` - - return makeDeleteRequest({ - url: options.url, - token: options.accessToken, - path, - statusCodeExpected: 204 - }) -} - -function setDefaultVideoChannel (servers: ServerInfo[]) { - const tasks: Promise[] = [] - - for (const server of servers) { - const p = getMyUserInformation(server.url, server.accessToken) - .then(res => { server.videoChannel = (res.body as User).videoChannels[0] }) - - tasks.push(p) - } - - return Promise.all(tasks) -} - -async function getDefaultVideoChannel (url: string, token: string) { - const res = await getMyUserInformation(url, token) - - return (res.body as MyUser).videoChannels[0].id -} - -// --------------------------------------------------------------------------- - -export { - updateVideoChannelImage, - getVideoChannelsList, - getAccountVideoChannelsList, - addVideoChannel, - updateVideoChannel, - deleteVideoChannel, - getVideoChannel, - setDefaultVideoChannel, - deleteVideoChannelImage, - getDefaultVideoChannel -} -- cgit v1.2.3