From 4beda9e12adc7b1f3b178cecd6863ebf3cf431f1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 19 Oct 2021 09:44:43 +0200 Subject: Add ability to view my followers --- server/controllers/api/accounts.ts | 33 +++++++++++++- server/controllers/api/server/follows.ts | 4 +- server/controllers/api/users/my-subscriptions.ts | 2 +- server/controllers/api/video-channel.ts | 55 +++++++++++++++++++----- 4 files changed, 80 insertions(+), 14 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts index 75679b0f4..77edfa7c2 100644 --- a/server/controllers/api/accounts.ts +++ b/server/controllers/api/accounts.ts @@ -1,5 +1,6 @@ import express from 'express' import { pickCommonVideoQuery } from '@server/helpers/query' +import { ActorFollowModel } from '@server/models/actor/actor-follow' import { getServerActor } from '@server/models/application/application' import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' import { getFormattedObjects } from '../../helpers/utils' @@ -20,6 +21,7 @@ import { } from '../../middlewares' import { accountNameWithHostGetValidator, + accountsFollowersSortValidator, accountsSortValidator, ensureAuthUserOwnsAccountValidator, videoChannelsSortValidator, @@ -93,6 +95,17 @@ accountsRouter.get('/:accountName/ratings', asyncMiddleware(listAccountRatings) ) +accountsRouter.get('/:accountName/followers', + authenticate, + asyncMiddleware(accountNameWithHostGetValidator), + ensureAuthUserOwnsAccountValidator, + paginationValidator, + accountsFollowersSortValidator, + setDefaultSort, + setDefaultPagination, + asyncMiddleware(listAccountFollowers) +) + // --------------------------------------------------------------------------- export { @@ -127,7 +140,7 @@ async function listAccountChannels (req: express.Request, res: express.Response) search: req.query.search } - const resultList = await VideoChannelModel.listByAccount(options) + const resultList = await VideoChannelModel.listByAccountForAPI(options) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -195,3 +208,21 @@ async function listAccountRatings (req: express.Request, res: express.Response) }) return res.json(getFormattedObjects(resultList.rows, resultList.count)) } + +async function listAccountFollowers (req: express.Request, res: express.Response) { + const account = res.locals.account + + const channels = await VideoChannelModel.listAllByAccount(account.id) + const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId)) + + const resultList = await ActorFollowModel.listFollowersForApi({ + actorIds, + 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)) +} diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 76ed75186..c613386b2 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -98,7 +98,7 @@ export { async function listFollowing (req: express.Request, res: express.Response) { const serverActor = await getServerActor() - const resultList = await ActorFollowModel.listFollowingForApi({ + const resultList = await ActorFollowModel.listInstanceFollowingForApi({ id: serverActor.id, start: req.query.start, count: req.query.count, @@ -114,7 +114,7 @@ async function listFollowing (req: express.Request, res: express.Response) { async function listFollowers (req: express.Request, res: express.Response) { const serverActor = await getServerActor() const resultList = await ActorFollowModel.listFollowersForApi({ - actorId: serverActor.id, + actorIds: [ serverActor.id ], start: req.query.start, count: req.query.count, sort: req.query.sort, diff --git a/server/controllers/api/users/my-subscriptions.ts b/server/controllers/api/users/my-subscriptions.ts index e3c0cf089..b2b441673 100644 --- a/server/controllers/api/users/my-subscriptions.ts +++ b/server/controllers/api/users/my-subscriptions.ts @@ -95,7 +95,7 @@ async function areSubscriptionsExist (req: express.Request, res: express.Respons return { name, host, uri: u } }) - const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) + const results = await ActorFollowModel.listSubscriptionsOf(user.Account.Actor.id, handles) const existObject: { [id: string ]: boolean } = {} for (const handle of handles) { diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index b79dc5933..f370c7004 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts @@ -1,6 +1,7 @@ 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 { MChannelBannerAccountDefault } from '@server/types/models' import { ActorImageType, VideoChannelCreate, VideoChannelUpdate } from '../../../shared' @@ -33,7 +34,13 @@ import { videoChannelsUpdateValidator, videoPlaylistsSortValidator } from '../../middlewares' -import { videoChannelsListValidator, videoChannelsNameWithHostValidator, 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' @@ -65,8 +72,8 @@ videoChannelRouter.post('/', videoChannelRouter.post('/:nameWithHost/avatar/pick', authenticate, reqAvatarFile, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, updateAvatarValidator, asyncMiddleware(updateVideoChannelAvatar) ) @@ -74,29 +81,31 @@ videoChannelRouter.post('/:nameWithHost/avatar/pick', videoChannelRouter.post('/:nameWithHost/banner/pick', authenticate, reqBannerFile, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, updateBannerValidator, asyncMiddleware(updateVideoChannelBanner) ) videoChannelRouter.delete('/:nameWithHost/avatar', authenticate, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, asyncMiddleware(deleteVideoChannelAvatar) ) videoChannelRouter.delete('/:nameWithHost/banner', authenticate, - // Check the rights - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, asyncMiddleware(deleteVideoChannelBanner) ) videoChannelRouter.put('/:nameWithHost', authenticate, - asyncMiddleware(videoChannelsUpdateValidator), + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, + videoChannelsUpdateValidator, asyncRetryTransactionMiddleware(updateVideoChannel) ) @@ -132,6 +141,17 @@ videoChannelRouter.get('/:nameWithHost/videos', asyncMiddleware(listVideoChannelVideos) ) +videoChannelRouter.get('/:nameWithHost/followers', + authenticate, + asyncMiddleware(videoChannelsNameWithHostValidator), + ensureAuthUserOwnsChannelValidator, + paginationValidator, + videoChannelsFollowersSortValidator, + setDefaultSort, + setDefaultPagination, + asyncMiddleware(listVideoChannelFollowers) +) + // --------------------------------------------------------------------------- export { @@ -332,3 +352,18 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon return res.json(getFormattedObjects(resultList.data, resultList.total)) } + +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)) +} -- cgit v1.2.3