1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 09:49:20 +02:00

Prefer handle param name

This commit is contained in:
Chocobozzz 2025-04-10 09:55:55 +02:00
parent 6f68db1be9
commit e7753c1b62
No known key found for this signature in database
GPG key ID: 583A612D890159BE
16 changed files with 63 additions and 80 deletions

View file

@ -92,7 +92,7 @@ activityPubClientRouter.get(
asyncMiddleware(accountPlaylistsController)
)
activityPubClientRouter.get(
'/accounts?/:name/likes/:videoId',
'/accounts?/:accountName/likes/:videoId',
executeIfActivityPub,
activityPubRateLimiter,
cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
@ -100,7 +100,7 @@ activityPubClientRouter.get(
asyncMiddleware(getAccountVideoRateFactory('like'))
)
activityPubClientRouter.get(
'/accounts?/:name/dislikes/:videoId',
'/accounts?/:accountName/dislikes/:videoId',
executeIfActivityPub,
activityPubRateLimiter,
cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),

View file

@ -30,7 +30,8 @@ import {
import { ActorFollowModel } from '../../../models/actor/actor-follow.js'
const serverFollowsRouter = express.Router()
serverFollowsRouter.get('/following',
serverFollowsRouter.get(
'/following',
listFollowsValidator,
paginationValidator,
instanceFollowingSortValidator,
@ -39,7 +40,8 @@ serverFollowsRouter.get('/following',
asyncMiddleware(listFollowing)
)
serverFollowsRouter.post('/following',
serverFollowsRouter.post(
'/following',
authenticate,
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
followValidator,
@ -47,14 +49,16 @@ serverFollowsRouter.post('/following',
asyncMiddleware(addFollow)
)
serverFollowsRouter.delete('/following/:hostOrHandle',
serverFollowsRouter.delete(
'/following/:hostOrHandle',
authenticate,
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
asyncMiddleware(removeFollowingValidator),
asyncMiddleware(removeFollowing)
)
serverFollowsRouter.get('/followers',
serverFollowsRouter.get(
'/followers',
listFollowsValidator,
paginationValidator,
instanceFollowersSortValidator,
@ -63,14 +67,16 @@ serverFollowsRouter.get('/followers',
asyncMiddleware(listFollowers)
)
serverFollowsRouter.delete('/followers/:nameWithHost',
serverFollowsRouter.delete(
'/followers/:handle',
authenticate,
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
asyncMiddleware(getFollowerValidator),
asyncMiddleware(removeFollower)
)
serverFollowsRouter.post('/followers/:nameWithHost/reject',
serverFollowsRouter.post(
'/followers/:handle/reject',
authenticate,
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
asyncMiddleware(getFollowerValidator),
@ -78,7 +84,8 @@ serverFollowsRouter.post('/followers/:nameWithHost/reject',
asyncMiddleware(rejectFollower)
)
serverFollowsRouter.post('/followers/:nameWithHost/accept',
serverFollowsRouter.post(
'/followers/:handle/accept',
authenticate,
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
asyncMiddleware(getFollowerValidator),

View file

@ -24,30 +24,15 @@ const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
// Special route that add OpenGraph and oEmbed tags
// Do not use a template engine for a so little thing
clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ],
clientsRateLimiter,
asyncMiddleware(generateWatchPlaylistHtmlPage)
)
clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ], clientsRateLimiter, asyncMiddleware(generateWatchPlaylistHtmlPage))
clientsRouter.use([ '/w/:id', '/videos/watch/:id' ],
clientsRateLimiter,
asyncMiddleware(generateWatchHtmlPage)
)
clientsRouter.use([ '/w/:id', '/videos/watch/:id' ], clientsRateLimiter, asyncMiddleware(generateWatchHtmlPage))
clientsRouter.use([ '/accounts/:nameWithHost', '/a/:nameWithHost' ],
clientsRateLimiter,
asyncMiddleware(generateAccountHtmlPage)
)
clientsRouter.use([ '/accounts/:handle', '/a/:handle' ], clientsRateLimiter, asyncMiddleware(generateAccountHtmlPage))
clientsRouter.use([ '/video-channels/:nameWithHost', '/c/:nameWithHost' ],
clientsRateLimiter,
asyncMiddleware(generateVideoChannelHtmlPage)
)
clientsRouter.use([ '/video-channels/:handle', '/c/:handle' ], clientsRateLimiter, asyncMiddleware(generateVideoChannelHtmlPage))
clientsRouter.use('/@:nameWithHost',
clientsRateLimiter,
asyncMiddleware(generateActorHtmlPage)
)
clientsRouter.use('/@:handle', clientsRateLimiter, asyncMiddleware(generateActorHtmlPage))
// ---------------------------------------------------------------------------
@ -118,10 +103,7 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response) =>
// Always serve index client page (the client is a single page application, let it handle routing)
// Try to provide the right language index.html
clientsRouter.use('/(:language)?',
clientsRateLimiter,
asyncMiddleware(serveIndexHTML)
)
clientsRouter.use('/(:language)?', clientsRateLimiter, asyncMiddleware(serveIndexHTML))
// ---------------------------------------------------------------------------
@ -206,19 +188,19 @@ async function generateWatchPlaylistHtmlPage (req: express.Request, res: express
}
async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
const html = await ClientHtml.getAccountHTMLPage(req.params.handle, req, res)
return sendHTML(html, res, true)
}
async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
const html = await ClientHtml.getVideoChannelHTMLPage(req.params.handle, req, res)
return sendHTML(html, res, true)
}
async function generateActorHtmlPage (req: express.Request, res: express.Response) {
const html = await ClientHtml.getActorHTMLPage(req.params.nameWithHost, req, res)
const html = await ClientHtml.getActorHTMLPage(req.params.handle, req, res)
return sendHTML(html, res, true)
}

View file

@ -56,9 +56,9 @@ function getLinkOrThrow (webfingerData: WebFingerData) {
return selfLink.href
}
function webfingerLookup (nameWithHost: string) {
function webfingerLookup (handle: string) {
return new Promise<WebFingerData>((res, rej) => {
webfinger.lookup(nameWithHost, (err, p) => {
webfinger.lookup(handle, (err, p) => {
if (err) return rej(err)
return res(p.object)

View file

@ -8,7 +8,6 @@ import { ActorHtml } from './shared/actor-html.js'
import { PageHtml } from './shared/page-html.js'
class ClientHtml {
static invalidateCache () {
PageHtml.invalidateCache()
}
@ -39,16 +38,16 @@ class ClientHtml {
// ---------------------------------------------------------------------------
static getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
return ActorHtml.getAccountHTMLPage(nameWithHost, req, res)
static getAccountHTMLPage (handle: string, req: express.Request, res: express.Response) {
return ActorHtml.getAccountHTMLPage(handle, req, res)
}
static getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
return ActorHtml.getVideoChannelHTMLPage(nameWithHost, req, res)
static getVideoChannelHTMLPage (handle: string, req: express.Request, res: express.Response) {
return ActorHtml.getVideoChannelHTMLPage(handle, req, res)
}
static getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
return ActorHtml.getActorHTMLPage(nameWithHost, req, res)
static getActorHTMLPage (handle: string, req: express.Request, res: express.Response) {
return ActorHtml.getActorHTMLPage(handle, req, res)
}
}

View file

@ -11,8 +11,8 @@ import { PageHtml } from './page-html.js'
import { TagsHtml, TagsOptions } from './tags-html.js'
export class ActorHtml {
static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
static async getAccountHTMLPage (handle: string, req: express.Request, res: express.Response) {
const accountModelPromise = AccountModel.loadByHandle(handle)
return this.getAccountOrChannelHTMLPage({
loader: () => accountModelPromise,
@ -22,8 +22,8 @@ export class ActorHtml {
})
}
static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
const videoChannel = await VideoChannelModel.loadByHandleAndPopulateAccount(nameWithHost)
static async getVideoChannelHTMLPage (handle: string, req: express.Request, res: express.Response) {
const videoChannel = await VideoChannelModel.loadByHandleAndPopulateAccount(handle)
return this.getAccountOrChannelHTMLPage({
loader: () => Promise.resolve(videoChannel),
@ -33,10 +33,10 @@ export class ActorHtml {
})
}
static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
static async getActorHTMLPage (handle: string, req: express.Request, res: express.Response) {
const [ account, channel ] = await Promise.all([
AccountModel.loadByNameWithHost(nameWithHost),
VideoChannelModel.loadByHandleAndPopulateAccount(nameWithHost)
AccountModel.loadByHandle(handle),
VideoChannelModel.loadByHandleAndPopulateAccount(handle)
])
return this.getAccountOrChannelHTMLPage({

View file

@ -177,7 +177,7 @@ function buildModerationHelpers () {
},
blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
const accountToBlock = await AccountModel.loadByHandle(options.handleToBlock)
if (!accountToBlock) return
const user = await UserModel.loadByAccountId(options.byAccountId)
@ -190,7 +190,7 @@ function buildModerationHelpers () {
},
unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
const targetAccount = await AccountModel.loadByHandle(options.handleToUnblock)
if (!targetAccount) return
const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)

View file

@ -13,7 +13,6 @@ const lTags = loggerTagsFactory('user-import')
type ImportObject = { handle: string | null, host: string | null, archiveFiles?: never }
export class BlocklistImporter extends AbstractUserImporter<BlocklistExportJSON, ImportObject, ImportObject> {
protected getImportObjects (json: BlocklistExportJSON) {
return [
...json.actors.map(o => ({ handle: o.handle, host: null })),
@ -38,7 +37,7 @@ export class BlocklistImporter extends AbstractUserImporter <BlocklistExportJSON
}
private async importAccountBlock (handle: string) {
const accountToBlock = await AccountModel.loadByNameWithHost(handle)
const accountToBlock = await AccountModel.loadByHandle(handle)
if (!accountToBlock) {
logger.info('Account %s was not blocked on user import because it cannot be found in the database.', handle, lTags())
return

View file

@ -15,7 +15,7 @@ export const accountHandleGetValidatorFactory = (options: {
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
if (!await doesAccountHandleExist({ handle: req.params.accountName, res, checkIsLocal, checkManage })) return
if (!await doesAccountHandleExist({ handle: req.params.handle, res, checkIsLocal, checkManage })) return
if (options.checkManage) {
const user = res.locals.oauth.token.User

View file

@ -53,7 +53,6 @@ const followValidator = [
const body: ServerFollowCreate = req.body
if (body.hosts.length === 0 && body.handles.length === 0) {
return res
.status(HttpStatusCode.BAD_REQUEST_400)
.json({
@ -94,7 +93,7 @@ const removeFollowingValidator = [
]
const getFollowerValidator = [
param('nameWithHost')
param('handle')
.custom(isValidActorHandle),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
@ -102,19 +101,19 @@ const getFollowerValidator = [
let follow: MActorFollowActorsDefault
try {
const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.handle)
const actor = await ActorModel.loadByUrl(actorUrl)
const serverActor = await getServerActor()
follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
} catch (err) {
logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
logger.warn('Cannot get actor from handle.', { handle: req.params.handle, err })
}
if (!follow) {
return res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: `Follower ${req.params.nameWithHost} not found.`
message: `Follower ${req.params.handle} not found.`
})
}

View file

@ -26,7 +26,7 @@ export async function doesAccountHandleExist (options: {
}) {
const { handle, res, checkIsLocal, checkManage } = options
const account = await AccountModel.loadByNameWithHost(handle)
const account = await AccountModel.loadByHandle(handle)
return doesAccountExist({ account, res, checkIsLocal, checkManage })
}

View file

@ -51,9 +51,6 @@ export const videoChannelsAddValidator = [
]
export const videoChannelsUpdateValidator = [
param('nameWithHost')
.exists(),
body('displayName')
.optional()
.custom(isVideoChannelDisplayNameValid),
@ -95,7 +92,7 @@ export const videoChannelsHandleValidatorFactory = (options: {
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
if (!await doesChannelHandleExist({ handle: req.params.nameWithHost, checkManage, checkIsLocal, res })) return
if (!await doesChannelHandleExist({ handle: req.params.handle, checkManage, checkIsLocal, res })) return
return next()
}

View file

@ -28,7 +28,7 @@ const videoUpdateRateValidator = [
const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
return [
param('name')
param('accountName')
.custom(isAccountNameValid),
param('videoId')
.custom(isIdValid),
@ -36,7 +36,7 @@ const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.accountName, +req.params.videoId)
if (!rate) {
return res.fail({
status: HttpStatusCode.NOT_FOUND_404,

View file

@ -14,8 +14,8 @@ const webfingerValidator = [
if (areValidationErrors(req, res)) return
// Remove 'acct:' from the beginning of the string
const nameWithHost = getHostWithPort(req.query.resource.substr(5))
const [ name ] = nameWithHost.split('@')
const handle = getHostWithPort(req.query.resource.substr(5))
const [ name ] = handle.split('@')
const actor = await ActorModel.loadLocalUrlByName(name)
if (!actor) {

View file

@ -302,8 +302,8 @@ export class AccountModel extends SequelizeModel<AccountModel> {
return AccountModel.findByPk(id, { transaction })
}
static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
const [ accountName, host ] = nameWithHost.split('@')
static loadByHandle (handle: string): Promise<MAccountDefault> {
const [ accountName, host ] = handle.split('@')
if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)

View file

@ -1202,7 +1202,7 @@ paths:
items:
$ref: '#/components/schemas/Follow'
'/api/v1/server/followers/{nameWithHost}':
'/api/v1/server/followers/{handle}':
delete:
summary: Remove or reject a follower to your server
security:
@ -1211,7 +1211,7 @@ paths:
tags:
- Instance Follows
parameters:
- name: nameWithHost
- name: handle
in: path
required: true
description: The remote actor handle to remove from your followers
@ -1224,7 +1224,7 @@ paths:
'404':
description: follower not found
'/api/v1/server/followers/{nameWithHost}/reject':
'/api/v1/server/followers/{handle}/reject':
post:
summary: Reject a pending follower to your server
security:
@ -1233,7 +1233,7 @@ paths:
tags:
- Instance Follows
parameters:
- name: nameWithHost
- name: handle
in: path
required: true
description: The remote actor handle to remove from your followers
@ -1246,7 +1246,7 @@ paths:
'404':
description: follower not found
'/api/v1/server/followers/{nameWithHost}/accept':
'/api/v1/server/followers/{handle}/accept':
post:
summary: Accept a pending follower to your server
security:
@ -1255,7 +1255,7 @@ paths:
tags:
- Instance Follows
parameters:
- name: nameWithHost
- name: handle
in: path
required: true
description: The remote actor handle to remove from your followers