X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideo-channel.ts;h=b2916278752aee9095a78514f180011aadab33de;hb=43df00a30dc07ec6fc1b30d41eac011e4ea8641b;hp=149d6cfb4a58d093d8cdd9e3d182897b7ac32860;hpb=213e30ef90806369529684ac9c247d73b8dc7928;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index 149d6cfb4..b29162787 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts @@ -1,9 +1,12 @@ -import * as express from 'express' +import express from 'express' +import { pickCommonVideoQuery } from '@server/helpers/query' import { Hooks } from '@server/lib/plugins/hooks' +import { ActorFollowModel } from '@server/models/actor/actor-follow' import { getServerActor } from '@server/models/application/application' +import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils' import { MChannelBannerAccountDefault } from '@server/types/models' import { ActorImageType, VideoChannelCreate, VideoChannelUpdate } from '../../../shared' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger' import { resetSequelizeInstance } from '../../helpers/database-utils' import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' @@ -13,14 +16,15 @@ import { CONFIG } from '../../initializers/config' import { MIMETYPES } from '../../initializers/constants' import { sequelizeTypescript } from '../../initializers/database' import { sendUpdateActor } from '../../lib/activitypub/send' -import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/actor-image' import { JobQueue } from '../../lib/job-queue' +import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/local-actor' import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel' import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, commonVideosFiltersValidator, + ensureUserCanManageChannel, optionalAuthenticate, paginationValidator, setDefaultPagination, @@ -32,7 +36,13 @@ import { videoChannelsUpdateValidator, videoPlaylistsSortValidator } from '../../middlewares' -import { videoChannelsNameWithHostValidator, videoChannelsOwnSearchValidator, videosSortValidator } from '../../middlewares/validators' +import { + ensureAuthUserOwnsChannelValidator, + videoChannelsFollowersSortValidator, + videoChannelsListValidator, + videoChannelsNameWithHostValidator, + videosSortValidator +} from '../../middlewares/validators' import { updateAvatarValidator, updateBannerValidator } from '../../middlewares/validators/actor-image' import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists' import { AccountModel } from '../../models/account/account' @@ -51,7 +61,7 @@ videoChannelRouter.get('/', videoChannelsSortValidator, setDefaultSort, setDefaultPagination, - videoChannelsOwnSearchValidator, + videoChannelsListValidator, asyncMiddleware(listVideoChannels) ) @@ -64,8 +74,8 @@ videoChannelRouter.post('/', videoChannelRouter.post('/:nameWithHost/avatar/pick', authenticate, reqAvatarFile, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureUserCanManageChannel, updateAvatarValidator, asyncMiddleware(updateVideoChannelAvatar) ) @@ -73,29 +83,31 @@ videoChannelRouter.post('/:nameWithHost/avatar/pick', videoChannelRouter.post('/:nameWithHost/banner/pick', authenticate, reqBannerFile, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureUserCanManageChannel, updateBannerValidator, asyncMiddleware(updateVideoChannelBanner) ) videoChannelRouter.delete('/:nameWithHost/avatar', authenticate, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureUserCanManageChannel, asyncMiddleware(deleteVideoChannelAvatar) ) videoChannelRouter.delete('/:nameWithHost/banner', authenticate, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureUserCanManageChannel, asyncMiddleware(deleteVideoChannelBanner) ) videoChannelRouter.put('/:nameWithHost', authenticate, - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureUserCanManageChannel, + videoChannelsUpdateValidator, asyncRetryTransactionMiddleware(updateVideoChannel) ) @@ -107,7 +119,7 @@ videoChannelRouter.delete('/:nameWithHost', videoChannelRouter.get('/:nameWithHost', asyncMiddleware(videoChannelsNameWithHostValidator), - asyncMiddleware(getVideoChannel) + getVideoChannel ) videoChannelRouter.get('/:nameWithHost/video-playlists', @@ -131,6 +143,17 @@ videoChannelRouter.get('/:nameWithHost/videos', asyncMiddleware(listVideoChannelVideos) ) +videoChannelRouter.get('/:nameWithHost/followers', + authenticate, + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, + paginationValidator, + videoChannelsFollowersSortValidator, + setDefaultSort, + setDefaultPagination, + asyncMiddleware(listVideoChannelFollowers) +) + // --------------------------------------------------------------------------- export { @@ -162,6 +185,7 @@ async function updateVideoChannelBanner (req: express.Request, res: express.Resp return res.json({ banner: banner.toFormattedJSON() }) } + async function updateVideoChannelAvatar (req: express.Request, res: express.Response) { const avatarPhysicalFile = req.files['avatarfile'][0] const videoChannel = res.locals.videoChannel @@ -179,7 +203,7 @@ async function deleteVideoChannelAvatar (req: express.Request, res: express.Resp await deleteLocalActorImageFile(videoChannel, ActorImageType.AVATAR) - return res.sendStatus(HttpStatusCode.NO_CONTENT_204) + return res.status(HttpStatusCode.NO_CONTENT_204).end() } async function deleteVideoChannelBanner (req: express.Request, res: express.Response) { @@ -187,7 +211,7 @@ async function deleteVideoChannelBanner (req: express.Request, res: express.Resp await deleteLocalActorImageFile(videoChannel, ActorImageType.BANNER) - return res.sendStatus(HttpStatusCode.NO_CONTENT_204) + return res.status(HttpStatusCode.NO_CONTENT_204).end() } async function addVideoChannel (req: express.Request, res: express.Response) { @@ -221,10 +245,6 @@ async function updateVideoChannel (req: express.Request, res: express.Response) try { await sequelizeTypescript.transaction(async t => { - const sequelizeOptions = { - transaction: t - } - if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.name = videoChannelInfoToUpdate.displayName if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.description = videoChannelInfoToUpdate.description @@ -238,7 +258,7 @@ async function updateVideoChannel (req: express.Request, res: express.Response) } } - const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) as MChannelBannerAccountDefault + const videoChannelInstanceUpdated = await videoChannelInstance.save({ transaction: t }) as MChannelBannerAccountDefault await sendUpdateActor(videoChannelInstanceUpdated, t) auditLogger.update( @@ -283,7 +303,7 @@ async function removeVideoChannel (req: express.Request, res: express.Response) return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() } -async function getVideoChannel (req: express.Request, res: express.Response) { +function getVideoChannel (req: express.Request, res: express.Response) { const videoChannel = res.locals.videoChannel if (videoChannel.isOutdated()) { @@ -309,24 +329,25 @@ async function listVideoChannelPlaylists (req: express.Request, res: express.Res } async function listVideoChannelVideos (req: express.Request, res: express.Response) { + const serverActor = await getServerActor() + const videoChannelInstance = res.locals.videoChannel - const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined + + const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res) + ? null + : { + actorId: serverActor.id, + orLocalVideos: true + } + const countVideos = getCountVideos(req) + const query = pickCommonVideoQuery(req.query) const apiOptions = await Hooks.wrapObject({ - followerActorId, - start: req.query.start, - count: req.query.count, - sort: req.query.sort, - includeLocalVideos: true, - categoryOneOf: req.query.categoryOneOf, - licenceOneOf: req.query.licenceOneOf, - languageOneOf: req.query.languageOneOf, - tagsOneOf: req.query.tagsOneOf, - tagsAllOf: req.query.tagsAllOf, - filter: req.query.filter, - nsfw: buildNSFWFilter(res, req.query.nsfw), - withFiles: false, + ...query, + + displayOnlyForFollower, + nsfw: buildNSFWFilter(res, query.nsfw), videoChannelId: videoChannelInstance.id, user: res.locals.oauth ? res.locals.oauth.token.User : undefined, countVideos @@ -338,5 +359,20 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon 'filter:api.video-channels.videos.list.result' ) + return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) +} + +async function listVideoChannelFollowers (req: express.Request, res: express.Response) { + const channel = res.locals.videoChannel + + const resultList = await ActorFollowModel.listFollowersForApi({ + actorIds: [ channel.actorId ], + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + state: 'accepted' + }) + return res.json(getFormattedObjects(resultList.data, resultList.total)) }