From 35bf0c83c80f59ca79f4b84fac8700f17adeb22d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Oct 2017 10:02:18 +0200 Subject: [PATCH] Video blacklist refractoring --- client/src/app/+admin/admin-routing.module.ts | 4 +- client/src/app/+admin/admin.component.ts | 1 - client/src/app/+admin/admin.module.ts | 9 ++- .../+admin/blacklist/blacklist-list/index.ts | 1 - .../app/+admin/blacklist/blacklist.routes.ts | 27 -------- client/src/app/+admin/blacklist/index.ts | 4 -- .../src/app/+admin/blacklist/shared/index.ts | 1 - .../src/app/+admin/video-blacklist/index.ts | 3 + .../video-blacklist-list/index.ts | 1 + .../video-blacklist-list.component.html} | 4 +- .../video-blacklist-list.component.ts} | 15 ++--- .../video-blacklist.component.ts} | 3 +- .../video-blacklist/video-blacklist.routes.ts | 27 ++++++++ client/src/app/app.component.ts | 2 +- .../app/core/menu/menu-admin.component.html | 2 +- client/src/app/shared/index.ts | 1 + client/src/app/shared/shared.module.ts | 2 + .../src/app/shared/video-blacklist/index.ts | 1 + .../video-blacklist.service.ts} | 24 ++++--- .../+video-watch/video-watch.component.ts | 19 +++--- client/src/app/videos/shared/video.service.ts | 6 -- server/controllers/api/blacklist.ts | 60 ----------------- server/controllers/api/index.ts | 2 - server/controllers/api/videos/blacklist.ts | 54 +++++++++++++-- server/helpers/custom-validators/videos.ts | 34 +++++++++- server/lib/blacklist.ts | 20 ------ server/lib/index.ts | 1 - server/middlewares/validators/blacklist.ts | 35 ---------- server/middlewares/validators/index.ts | 2 +- .../middlewares/validators/video-blacklist.ts | 67 +++++++++++++++++++ server/middlewares/validators/videos.ts | 58 +--------------- .../tests/api/check-params/video-blacklist.ts | 12 ++-- server/tests/utils/video-blacklist.ts | 6 +- 33 files changed, 239 insertions(+), 269 deletions(-) delete mode 100644 client/src/app/+admin/blacklist/blacklist-list/index.ts delete mode 100644 client/src/app/+admin/blacklist/blacklist.routes.ts delete mode 100644 client/src/app/+admin/blacklist/index.ts delete mode 100644 client/src/app/+admin/blacklist/shared/index.ts create mode 100644 client/src/app/+admin/video-blacklist/index.ts create mode 100644 client/src/app/+admin/video-blacklist/video-blacklist-list/index.ts rename client/src/app/+admin/{blacklist/blacklist-list/blacklist-list.component.html => video-blacklist/video-blacklist-list/video-blacklist-list.component.html} (86%) rename client/src/app/+admin/{blacklist/blacklist-list/blacklist-list.component.ts => video-blacklist/video-blacklist-list/video-blacklist-list.component.ts} (76%) rename client/src/app/+admin/{blacklist/blacklist.component.ts => video-blacklist/video-blacklist.component.ts} (73%) create mode 100644 client/src/app/+admin/video-blacklist/video-blacklist.routes.ts create mode 100644 client/src/app/shared/video-blacklist/index.ts rename client/src/app/{+admin/blacklist/shared/blacklist.service.ts => shared/video-blacklist/video-blacklist.service.ts} (56%) delete mode 100644 server/controllers/api/blacklist.ts delete mode 100644 server/lib/blacklist.ts delete mode 100644 server/middlewares/validators/blacklist.ts create mode 100644 server/middlewares/validators/video-blacklist.ts diff --git a/client/src/app/+admin/admin-routing.module.ts b/client/src/app/+admin/admin-routing.module.ts index 0cd3e54c2..c3e4895ac 100644 --- a/client/src/app/+admin/admin-routing.module.ts +++ b/client/src/app/+admin/admin-routing.module.ts @@ -9,7 +9,7 @@ import { RequestSchedulersRoutes } from './request-schedulers' import { UsersRoutes } from './users' import { VideoAbusesRoutes } from './video-abuses' import { AdminGuard } from './admin-guard.service' -import { BlacklistRoutes } from './blacklist' +import { VideoBlacklistRoutes } from './video-blacklist' const adminRoutes: Routes = [ { @@ -27,7 +27,7 @@ const adminRoutes: Routes = [ ...RequestSchedulersRoutes, ...UsersRoutes, ...VideoAbusesRoutes, - ...BlacklistRoutes + ...VideoBlacklistRoutes ] } ] diff --git a/client/src/app/+admin/admin.component.ts b/client/src/app/+admin/admin.component.ts index 93ec216bf..ecd62ee61 100644 --- a/client/src/app/+admin/admin.component.ts +++ b/client/src/app/+admin/admin.component.ts @@ -3,6 +3,5 @@ import { Component } from '@angular/core' @Component({ template: '' }) - export class AdminComponent { } diff --git a/client/src/app/+admin/admin.module.ts b/client/src/app/+admin/admin.module.ts index c2dd60774..f29c501b0 100644 --- a/client/src/app/+admin/admin.module.ts +++ b/client/src/app/+admin/admin.module.ts @@ -6,7 +6,7 @@ import { FriendsComponent, FriendAddComponent, FriendListComponent, FriendServic import { RequestSchedulersComponent, RequestSchedulersStatsComponent, RequestSchedulersService } from './request-schedulers' import { UsersComponent, UserAddComponent, UserUpdateComponent, UserListComponent, UserService } from './users' import { VideoAbusesComponent, VideoAbuseListComponent } from './video-abuses' -import { BlacklistComponent, BlacklistListComponent, BlacklistService } from './blacklist' +import { VideoBlacklistComponent, VideoBlacklistListComponent } from './video-blacklist' import { SharedModule } from '../shared' import { AdminGuard } from './admin-guard.service' @@ -31,8 +31,8 @@ import { AdminGuard } from './admin-guard.service' UserUpdateComponent, UserListComponent, - BlacklistComponent, - BlacklistListComponent, + VideoBlacklistComponent, + VideoBlacklistListComponent, VideoAbusesComponent, VideoAbuseListComponent @@ -46,8 +46,7 @@ import { AdminGuard } from './admin-guard.service' FriendService, RequestSchedulersService, UserService, - AdminGuard, - BlacklistService + AdminGuard ] }) export class AdminModule { } diff --git a/client/src/app/+admin/blacklist/blacklist-list/index.ts b/client/src/app/+admin/blacklist/blacklist-list/index.ts deleted file mode 100644 index 45f60a2b9..000000000 --- a/client/src/app/+admin/blacklist/blacklist-list/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './blacklist-list.component' diff --git a/client/src/app/+admin/blacklist/blacklist.routes.ts b/client/src/app/+admin/blacklist/blacklist.routes.ts deleted file mode 100644 index 780347ca8..000000000 --- a/client/src/app/+admin/blacklist/blacklist.routes.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Routes } from '@angular/router' - -import { BlacklistComponent } from './blacklist.component' -import { BlacklistListComponent } from './blacklist-list' - -export const BlacklistRoutes: Routes = [ - { - path: 'blacklist', - component: BlacklistComponent, - children: [ - { - path: '', - redirectTo: 'list', - pathMatch: 'full' - }, - { - path: 'list', - component: BlacklistListComponent, - data: { - meta: { - title: 'Blacklisted videos' - } - } - } - ] - } -] diff --git a/client/src/app/+admin/blacklist/index.ts b/client/src/app/+admin/blacklist/index.ts deleted file mode 100644 index 675dc1263..000000000 --- a/client/src/app/+admin/blacklist/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './shared' -export * from './blacklist-list' -export * from './blacklist.component' -export * from './blacklist.routes' diff --git a/client/src/app/+admin/blacklist/shared/index.ts b/client/src/app/+admin/blacklist/shared/index.ts deleted file mode 100644 index ad22e2d51..000000000 --- a/client/src/app/+admin/blacklist/shared/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './blacklist.service' diff --git a/client/src/app/+admin/video-blacklist/index.ts b/client/src/app/+admin/video-blacklist/index.ts new file mode 100644 index 000000000..a5451e2e1 --- /dev/null +++ b/client/src/app/+admin/video-blacklist/index.ts @@ -0,0 +1,3 @@ +export * from './video-blacklist-list' +export * from './video-blacklist.component' +export * from './video-blacklist.routes' diff --git a/client/src/app/+admin/video-blacklist/video-blacklist-list/index.ts b/client/src/app/+admin/video-blacklist/video-blacklist-list/index.ts new file mode 100644 index 000000000..4daf64187 --- /dev/null +++ b/client/src/app/+admin/video-blacklist/video-blacklist-list/index.ts @@ -0,0 +1 @@ +export * from './video-blacklist-list.component' diff --git a/client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.html b/client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.html similarity index 86% rename from client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.html rename to client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.html index 5d4636ee9..05d116798 100644 --- a/client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.html +++ b/client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.html @@ -18,8 +18,8 @@ - - + + diff --git a/client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.ts b/client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.ts similarity index 76% rename from client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.ts rename to client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.ts index b308054ed..56024b247 100644 --- a/client/src/app/+admin/blacklist/blacklist-list/blacklist-list.component.ts +++ b/client/src/app/+admin/video-blacklist/video-blacklist-list/video-blacklist-list.component.ts @@ -4,16 +4,15 @@ import { SortMeta } from 'primeng/components/common/sortmeta' import { NotificationsService } from 'angular2-notifications' import { ConfirmService } from '../../../core' -import { RestTable, RestPagination } from '../../../shared' -import { BlacklistService } from '../shared' +import { VideoBlacklistService, RestTable, RestPagination } from '../../../shared' import { BlacklistedVideo } from '../../../../../../shared' @Component({ - selector: 'my-blacklist-list', - templateUrl: './blacklist-list.component.html', + selector: 'my-video-blacklist-list', + templateUrl: './video-blacklist-list.component.html', styleUrls: [] }) -export class BlacklistListComponent extends RestTable implements OnInit { +export class VideoBlacklistListComponent extends RestTable implements OnInit { blacklist: BlacklistedVideo[] = [] totalRecords = 0 rowsPerPage = 10 @@ -23,7 +22,7 @@ export class BlacklistListComponent extends RestTable implements OnInit { constructor ( private notificationsService: NotificationsService, private confirmService: ConfirmService, - private blacklistService: BlacklistService + private videoBlacklistService: VideoBlacklistService ) { super() } @@ -39,7 +38,7 @@ export class BlacklistListComponent extends RestTable implements OnInit { res => { if (res === false) return - this.blacklistService.removeVideoFromBlacklist(entry).subscribe( + this.videoBlacklistService.removeVideoFromBlacklist(entry.videoId).subscribe( status => { this.notificationsService.success('Success', `Video ${entry.name} removed from the blacklist.`) this.loadData() @@ -52,7 +51,7 @@ export class BlacklistListComponent extends RestTable implements OnInit { } protected loadData () { - this.blacklistService.getBlacklist(this.pagination, this.sort) + this.videoBlacklistService.listBlacklist(this.pagination, this.sort) .subscribe( resultList => { this.blacklist = resultList.data diff --git a/client/src/app/+admin/blacklist/blacklist.component.ts b/client/src/app/+admin/video-blacklist/video-blacklist.component.ts similarity index 73% rename from client/src/app/+admin/blacklist/blacklist.component.ts rename to client/src/app/+admin/video-blacklist/video-blacklist.component.ts index ce8fe4298..24b8c98f8 100644 --- a/client/src/app/+admin/blacklist/blacklist.component.ts +++ b/client/src/app/+admin/video-blacklist/video-blacklist.component.ts @@ -3,6 +3,5 @@ import { Component } from '@angular/core' @Component({ template: '' }) - -export class BlacklistComponent { +export class VideoBlacklistComponent { } diff --git a/client/src/app/+admin/video-blacklist/video-blacklist.routes.ts b/client/src/app/+admin/video-blacklist/video-blacklist.routes.ts new file mode 100644 index 000000000..682b6f8bd --- /dev/null +++ b/client/src/app/+admin/video-blacklist/video-blacklist.routes.ts @@ -0,0 +1,27 @@ +import { Routes } from '@angular/router' + +import { VideoBlacklistComponent } from './video-blacklist.component' +import { VideoBlacklistListComponent } from './video-blacklist-list' + +export const VideoBlacklistRoutes: Routes = [ + { + path: 'video-blacklist', + component: VideoBlacklistComponent, + children: [ + { + path: '', + redirectTo: 'list', + pathMatch: 'full' + }, + { + path: 'list', + component: VideoBlacklistListComponent, + data: { + meta: { + title: 'Blacklisted videos' + } + } + } + ] + } +] diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index 68719f73d..7d890e72a 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -11,7 +11,7 @@ import { UserService } from './shared' }) export class AppComponent implements OnInit { notificationOptions = { - timeOut: 3000, + timeOut: 5000, lastOnBottom: true, clickToClose: true, maxLength: 0, diff --git a/client/src/app/core/menu/menu-admin.component.html b/client/src/app/core/menu/menu-admin.component.html index f512a4e67..edacdee6d 100644 --- a/client/src/app/core/menu/menu-admin.component.html +++ b/client/src/app/core/menu/menu-admin.component.html @@ -20,7 +20,7 @@ Video abuses - + Video blacklist diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts index 212645c51..79bf5ef43 100644 --- a/client/src/app/shared/index.ts +++ b/client/src/app/shared/index.ts @@ -4,5 +4,6 @@ export * from './rest' export * from './search' export * from './users' export * from './video-abuse' +export * from './video-blacklist' export * from './shared.module' export * from './utils' diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts index 56da62fc4..47f651590 100644 --- a/client/src/app/shared/shared.module.ts +++ b/client/src/app/shared/shared.module.ts @@ -18,6 +18,7 @@ import { RestExtractor, RestService } from './rest' import { SearchComponent, SearchService } from './search' import { UserService } from './users' import { VideoAbuseService } from './video-abuse' +import { VideoBlacklistService } from './video-blacklist' @NgModule({ imports: [ @@ -67,6 +68,7 @@ import { VideoAbuseService } from './video-abuse' RestService, SearchService, VideoAbuseService, + VideoBlacklistService, UserService ] }) diff --git a/client/src/app/shared/video-blacklist/index.ts b/client/src/app/shared/video-blacklist/index.ts new file mode 100644 index 000000000..bfb026441 --- /dev/null +++ b/client/src/app/shared/video-blacklist/index.ts @@ -0,0 +1 @@ +export * from './video-blacklist.service' diff --git a/client/src/app/+admin/blacklist/shared/blacklist.service.ts b/client/src/app/shared/video-blacklist/video-blacklist.service.ts similarity index 56% rename from client/src/app/+admin/blacklist/shared/blacklist.service.ts rename to client/src/app/shared/video-blacklist/video-blacklist.service.ts index ee2bf82e9..17373d52e 100644 --- a/client/src/app/+admin/blacklist/shared/blacklist.service.ts +++ b/client/src/app/shared/video-blacklist/video-blacklist.service.ts @@ -6,13 +6,13 @@ import 'rxjs/add/operator/map' import { SortMeta } from 'primeng/components/common/sortmeta' -import { RestExtractor, RestPagination, RestService } from '../../../shared' -import { Utils } from '../../../shared' -import { BlacklistedVideo, ResultList } from '../../../../../../shared' +import { RestExtractor, RestPagination, RestService } from '../rest' +import { Utils } from '../utils' +import { BlacklistedVideo, ResultList } from '../../../../../shared' @Injectable() -export class BlacklistService { - private static BASE_BLACKLISTS_URL = API_URL + '/api/v1/blacklist/' +export class VideoBlacklistService { + private static BASE_VIDEOS_URL = API_URL + '/api/v1/videos/' constructor ( private authHttp: HttpClient, @@ -20,22 +20,28 @@ export class BlacklistService { private restExtractor: RestExtractor ) {} - getBlacklist (pagination: RestPagination, sort: SortMeta): Observable> { + listBlacklist (pagination: RestPagination, sort: SortMeta): Observable> { let params = new HttpParams() params = this.restService.addRestGetParams(params, pagination, sort) - return this.authHttp.get>(BlacklistService.BASE_BLACKLISTS_URL, { params }) + return this.authHttp.get>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) .map(res => this.restExtractor.convertResultListDateToHuman(res)) .map(res => this.restExtractor.applyToResultListData(res, this.formatBlacklistedVideo.bind(this))) .catch(res => this.restExtractor.handleError(res)) } - removeVideoFromBlacklist (entry: BlacklistedVideo) { - return this.authHttp.delete(BlacklistService.BASE_BLACKLISTS_URL + entry.id) + removeVideoFromBlacklist (videoId: number) { + return this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist') .map(this.restExtractor.extractDataBool) .catch(res => this.restExtractor.handleError(res)) } + blacklistVideo (videoId: number) { + return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', {}) + .map(this.restExtractor.extractDataBool) + .catch(res => this.restExtractor.handleError(res)) + } + private formatBlacklistedVideo (blacklistedVideo: BlacklistedVideo) { return Object.assign(blacklistedVideo, { createdAt: Utils.dateToHuman(blacklistedVideo.createdAt) diff --git a/client/src/app/videos/+video-watch/video-watch.component.ts b/client/src/app/videos/+video-watch/video-watch.component.ts index 874dd5997..bd98e877c 100644 --- a/client/src/app/videos/+video-watch/video-watch.component.ts +++ b/client/src/app/videos/+video-watch/video-watch.component.ts @@ -14,7 +14,7 @@ import { VideoMagnetComponent } from './video-magnet.component' import { VideoShareComponent } from './video-share.component' import { VideoReportComponent } from './video-report.component' import { Video, VideoService } from '../shared' -import { WebTorrentService } from './webtorrent.service' +import { VideoBlacklistService } from '../../shared' import { UserVideoRateType, VideoRateType } from '../../../../../shared' @Component({ @@ -45,6 +45,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { private route: ActivatedRoute, private router: Router, private videoService: VideoService, + private videoBlacklistService: VideoBlacklistService, private confirmService: ConfirmService, private metaService: MetaService, private authService: AuthService, @@ -137,15 +138,15 @@ export class VideoWatchComponent implements OnInit, OnDestroy { res => { if (res === false) return - this.videoService.blacklistVideo(this.video.id) - .subscribe( - status => { - this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`) - this.router.navigate(['/videos/list']) - }, + this.videoBlacklistService.blacklistVideo(this.video.id) + .subscribe( + status => { + this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`) + this.router.navigate(['/videos/list']) + }, - error => this.notificationsService.error('Error', error.text) - ) + error => this.notificationsService.error('Error', error.text) + ) } ) } diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts index 037c20416..fe7f9108d 100644 --- a/client/src/app/videos/shared/video.service.ts +++ b/client/src/app/videos/shared/video.service.ts @@ -122,12 +122,6 @@ export class VideoService { .catch(res => this.restExtractor.handleError(res)) } - blacklistVideo (id: number) { - return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {}) - .map(this.restExtractor.extractDataBool) - .catch(res => this.restExtractor.handleError(res)) - } - private videoPaginationToRestPagination (videoPagination: VideoPagination) { const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage const count: number = videoPagination.itemsPerPage diff --git a/server/controllers/api/blacklist.ts b/server/controllers/api/blacklist.ts deleted file mode 100644 index 9b2d8017e..000000000 --- a/server/controllers/api/blacklist.ts +++ /dev/null @@ -1,60 +0,0 @@ -import * as express from 'express' - -import { database } from '../../initializers' -import { getFormattedObjects } from '../../helpers' -import { BlacklistedVideo } from '../../../shared' -import { BlacklistedVideoInstance } from '../../models' - -import { - removeVideoFromBlacklist -} from '../../lib' -import { - authenticate, - ensureIsAdmin, - paginationValidator, - blacklistSortValidator, - setBlacklistSort, - setPagination, - blacklistRemoveValidator -} from '../../middlewares' - -const blacklistRouter = express.Router() - -blacklistRouter.get('/', - authenticate, - ensureIsAdmin, - paginationValidator, - blacklistSortValidator, - setBlacklistSort, - setPagination, - listBlacklist -) - -blacklistRouter.delete('/:id', - authenticate, - ensureIsAdmin, - blacklistRemoveValidator, - removeVideoFromBlacklistController -) - -// --------------------------------------------------------------------------- - -export { - blacklistRouter -} - -// --------------------------------------------------------------------------- - -function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - database.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) - .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) - .catch(err => next(err)) -} - -function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { - const entry = res.locals.blacklistEntryToRemove as BlacklistedVideoInstance - - removeVideoFromBlacklist(entry) - .then(() => res.sendStatus(204)) - .catch(err => next(err)) -} diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts index fdc887915..a9205b33c 100644 --- a/server/controllers/api/index.ts +++ b/server/controllers/api/index.ts @@ -9,7 +9,6 @@ import { remoteRouter } from './remote' import { requestSchedulerRouter } from './request-schedulers' import { usersRouter } from './users' import { videosRouter } from './videos' -import { blacklistRouter } from './blacklist' const apiRouter = express.Router() @@ -20,7 +19,6 @@ apiRouter.use('/remote', remoteRouter) apiRouter.use('/request-schedulers', requestSchedulerRouter) apiRouter.use('/users', usersRouter) apiRouter.use('/videos', videosRouter) -apiRouter.use('/blacklist', blacklistRouter) apiRouter.use('/ping', pong) apiRouter.use('/*', badRequest) diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index d8f2068ec..66311598e 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,22 +1,46 @@ import * as express from 'express' -import { database as db } from '../../../initializers/database' -import { logger } from '../../../helpers' +import { database as db } from '../../../initializers' +import { logger, getFormattedObjects } from '../../../helpers' import { authenticate, ensureIsAdmin, - videosBlacklistValidator + videosBlacklistAddValidator, + videosBlacklistRemoveValidator, + paginationValidator, + blacklistSortValidator, + setBlacklistSort, + setPagination } from '../../../middlewares' +import { BlacklistedVideoInstance } from '../../../models' +import { BlacklistedVideo } from '../../../../shared' const blacklistRouter = express.Router() -blacklistRouter.post('/:id/blacklist', +blacklistRouter.post('/:videoId/blacklist', authenticate, ensureIsAdmin, - videosBlacklistValidator, + videosBlacklistAddValidator, addVideoToBlacklist ) +blacklistRouter.get('/blacklist', + authenticate, + ensureIsAdmin, + paginationValidator, + blacklistSortValidator, + setBlacklistSort, + setPagination, + listBlacklist +) + +blacklistRouter.delete('/:videoId/blacklist', + authenticate, + ensureIsAdmin, + videosBlacklistRemoveValidator, + removeVideoFromBlacklistController +) + // --------------------------------------------------------------------------- export { @@ -39,3 +63,23 @@ function addVideoToBlacklist (req: express.Request, res: express.Response, next: return next(err) }) } + +function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { + db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) + .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) + .catch(err => next(err)) +} + +function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { + const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance + + blacklistedVideo.destroy() + .then(() => { + logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + res.sendStatus(204) + }) + .catch(err => { + logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) + next(err) + }) +} diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index a31aca019..05d1dc607 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -1,5 +1,7 @@ import { values } from 'lodash' import * as validator from 'validator' +import * as Promise from 'bluebird' +import * as express from 'express' import 'express-validator' import 'multer' @@ -8,10 +10,13 @@ import { VIDEO_CATEGORIES, VIDEO_LICENCES, VIDEO_LANGUAGES, - VIDEO_RATE_TYPES + VIDEO_RATE_TYPES, + database as db } from '../../initializers' import { isUserUsernameValid } from './users' import { isArray, exists } from './misc' +import { VideoInstance } from '../../models' +import { logger } from '../../helpers' import { VideoRateType } from '../../../shared' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS @@ -138,6 +143,30 @@ function isVideoFileInfoHashValid (value: string) { return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) } +function checkVideoExists (id: string, res: express.Response, callback: () => void) { + let promise: Promise + if (validator.isInt(id)) { + promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id) + } else { // UUID + promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id) + } + + promise.then(video => { + if (!video) { + return res.status(404) + .json({ error: 'Video not found' }) + .end() + } + + res.locals.video = video + callback() + }) + .catch(err => { + logger.error('Error in video request validator.', err) + return res.sendStatus(500) + }) +} + // --------------------------------------------------------------------------- export { @@ -166,5 +195,6 @@ export { isVideoDislikesValid, isVideoEventCountValid, isVideoFileSizeValid, - isVideoFileResolutionValid + isVideoFileResolutionValid, + checkVideoExists } diff --git a/server/lib/blacklist.ts b/server/lib/blacklist.ts deleted file mode 100644 index dcf8aa03c..000000000 --- a/server/lib/blacklist.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { logger } from '../helpers' -import { BlacklistedVideoInstance } from '../models' - -function removeVideoFromBlacklist (entry: BlacklistedVideoInstance) { - return entry.destroy() - .then(() => { - logger.info('Video removed from the blacklist') - }) - .catch(err => { - logger.error('Some error while removing video from the blacklist.', err) - }) -} - -// --------------------------------------------------------------------------- - -export { - removeVideoFromBlacklist -} - -// --------------------------------------------------------------------------- diff --git a/server/lib/index.ts b/server/lib/index.ts index df781f29f..8628da4dd 100644 --- a/server/lib/index.ts +++ b/server/lib/index.ts @@ -3,4 +3,3 @@ export * from './jobs' export * from './request' export * from './friends' export * from './oauth-model' -export * from './blacklist' diff --git a/server/middlewares/validators/blacklist.ts b/server/middlewares/validators/blacklist.ts deleted file mode 100644 index fe8fa40a4..000000000 --- a/server/middlewares/validators/blacklist.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { param } from 'express-validator/check' -import * as express from 'express' - -import { database as db } from '../../initializers/database' -import { checkErrors } from './utils' -import { logger } from '../../helpers' - -const blacklistRemoveValidator = [ - param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) - - checkErrors(req, res, () => { - db.BlacklistedVideo.loadById(req.params.id) - .then(entry => { - if (!entry) return res.status(404).send('Blacklisted video not found') - - res.locals.blacklistEntryToRemove = entry - - next() - }) - .catch(err => { - logger.error('Error in blacklistRemove request validator', { error: err }) - return res.sendStatus(500) - }) - }) - } -] - -// --------------------------------------------------------------------------- - -export { - blacklistRemoveValidator -} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index a6198e22c..418fa5f1d 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -4,4 +4,4 @@ export * from './pods' export * from './sort' export * from './users' export * from './videos' -export * from './blacklist' +export * from './video-blacklist' diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts new file mode 100644 index 000000000..30c6d4bd9 --- /dev/null +++ b/server/middlewares/validators/video-blacklist.ts @@ -0,0 +1,67 @@ +import { param } from 'express-validator/check' +import * as express from 'express' + +import { database as db } from '../../initializers/database' +import { checkErrors } from './utils' +import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers' + +const videosBlacklistRemoveValidator = [ + param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) + + checkErrors(req, res, () => { + checkVideoExists(req.params.videoId, res, () => { + checkVideoIsBlacklisted(req, res, next) + }) + }) + } +] + +const videosBlacklistAddValidator = [ + param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) + + checkErrors(req, res, () => { + checkVideoExists(req.params.videoId, res, () => { + checkVideoIsBlacklistable(req, res, next) + }) + }) + } +] + +// --------------------------------------------------------------------------- + +export { + videosBlacklistAddValidator, + videosBlacklistRemoveValidator +} +// --------------------------------------------------------------------------- + +function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { + if (res.locals.video.isOwned() === true) { + return res.status(403) + .json({ error: 'Cannot blacklist a local video' }) + .end() + } + + callback() +} + +function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) { + db.BlacklistedVideo.loadByVideoId(res.locals.video.id) + .then(blacklistedVideo => { + if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found') + + res.locals.blacklistedVideo = blacklistedVideo + + callback() + }) + .catch(err => { + logger.error('Error in blacklistRemove request validator', { error: err }) + return res.sendStatus(500) + }) +} diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 5f213f974..deed07524 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -1,7 +1,5 @@ import { body, param, query } from 'express-validator/check' import * as express from 'express' -import * as Promise from 'bluebird' -import * as validator from 'validator' import { database as db } from '../../initializers/database' import { checkErrors } from './utils' @@ -20,9 +18,9 @@ import { isVideoIdOrUUIDValid, isVideoAbuseReasonValid, isVideoRatingTypeValid, - getDurationFromVideoFile + getDurationFromVideoFile, + checkVideoExists } from '../../helpers' -import { VideoInstance } from '../../models' const videosAddValidator = [ body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'), @@ -186,20 +184,6 @@ const videoRateValidator = [ } ] -const videosBlacklistValidator = [ - param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) - - checkErrors(req, res, () => { - checkVideoExists(req.params.id, res, () => { - checkVideoIsBlacklistable(req, res, next) - }) - }) - } -] - // --------------------------------------------------------------------------- export { @@ -211,37 +195,11 @@ export { videoAbuseReportValidator, - videoRateValidator, - - videosBlacklistValidator + videoRateValidator } // --------------------------------------------------------------------------- -function checkVideoExists (id: string, res: express.Response, callback: () => void) { - let promise: Promise - if (validator.isInt(id)) { - promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id) - } else { // UUID - promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id) - } - - promise.then(video => { - if (!video) { - return res.status(404) - .json({ error: 'Video not found' }) - .end() - } - - res.locals.video = video - callback() - }) - .catch(err => { - logger.error('Error in video request validator.', err) - return res.sendStatus(500) - }) -} - function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { // Retrieve the user who did the request db.User.loadById(userId) @@ -269,13 +227,3 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac return res.sendStatus(500) }) } - -function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { - if (res.locals.video.isOwned() === true) { - return res.status(403) - .json({ error: 'Cannot blacklist a local video' }) - .end() - } - - callback() -} diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts index 80e6f8011..eb16b3af0 100644 --- a/server/tests/api/check-params/video-blacklist.ts +++ b/server/tests/api/check-params/video-blacklist.ts @@ -81,10 +81,10 @@ describe('Test video blacklist API validators', function () { }) describe('When removing a video in blacklist', function () { - const basePath = '/api/v1/blacklist/' + const basePath = '/api/v1/videos/' it('Should fail with a non authenticated user', async function () { - const path = basePath + server.video.id + const path = basePath + server.video.id + '/blacklist' await request(server.url) .delete(path) @@ -94,7 +94,7 @@ describe('Test video blacklist API validators', function () { }) it('Should fail with a non admin user', async function () { - const path = basePath + server.video.id + const path = basePath + server.video.id + '/blacklist' await request(server.url) .delete(path) @@ -104,7 +104,7 @@ describe('Test video blacklist API validators', function () { }) it('Should fail with an incorrect id', async function () { - const path = basePath + 'foobar' + const path = basePath + 'foobar/blacklist' await request(server.url) .delete(path) @@ -115,7 +115,7 @@ describe('Test video blacklist API validators', function () { it('Should fail with a not blacklisted video', async function () { // The video was not added to the blacklist so it should fail - const path = basePath + server.video.id + const path = basePath + server.video.id + '/blacklist' await request(server.url) .delete(path) @@ -126,7 +126,7 @@ describe('Test video blacklist API validators', function () { }) describe('When listing videos in blacklist', function () { - const basePath = '/api/v1/blacklist/' + const basePath = '/api/v1/videos/blacklist/' it('Should fail with a non authenticated user', async function () { const path = basePath diff --git a/server/tests/utils/video-blacklist.ts b/server/tests/utils/video-blacklist.ts index 5729d13d8..3a499f46a 100644 --- a/server/tests/utils/video-blacklist.ts +++ b/server/tests/utils/video-blacklist.ts @@ -11,7 +11,7 @@ function addVideoToBlacklist (url: string, token: string, videoId: number, speci } function removeVideoFromBlacklist (url: string, token: string, videoId: number, specialStatus = 204) { - const path = '/api/v1/blacklist/' + videoId + const path = '/api/v1/videos/' + videoId + '/blacklist' return request(url) .delete(path) @@ -21,7 +21,7 @@ function removeVideoFromBlacklist (url: string, token: string, videoId: number, } function getBlacklistedVideosList (url: string, token: string, specialStatus = 200) { - const path = '/api/v1/blacklist/' + const path = '/api/v1/videos/blacklist/' return request(url) .get(path) @@ -33,7 +33,7 @@ function getBlacklistedVideosList (url: string, token: string, specialStatus = 2 } function getSortedBlacklistedVideosList (url: string, token: string, sort: string, specialStatus = 200) { - const path = '/api/v1/blacklist/' + const path = '/api/v1/videos/blacklist/' return request(url) .get(path) -- 2.25.1