Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorter URLs channel and account + /@:actorName #4009

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add server API actors route
  • Loading branch information
kimsible committed May 5, 2021
commit 1e37d32f4bdc51045cd85d01ea1035fd53e0d32c
37 changes: 37 additions & 0 deletions server/controllers/api/actor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as express from 'express'
import { JobQueue } from '../../lib/job-queue'
import { asyncMiddleware } from '../../middlewares'
import { actorNameWithHostGetValidator } from '../../middlewares/validators'

const actorRouter = express.Router()

actorRouter.get('/:actorName',
asyncMiddleware(actorNameWithHostGetValidator),
getActor
)

// ---------------------------------------------------------------------------

export {
actorRouter
}

// ---------------------------------------------------------------------------

function getActor (req: express.Request, res: express.Response) {
let accountOrVideoChannel

if (res.locals.account) {
accountOrVideoChannel = res.locals.account
}

if (res.locals.videoChannel) {
accountOrVideoChannel = res.locals.videoChannel
}

if (accountOrVideoChannel.isOutdated()) {
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: accountOrVideoChannel.Actor.url } })
}

return res.json(accountOrVideoChannel.toFormattedJSON())
}
2 changes: 2 additions & 0 deletions server/controllers/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { pluginRouter } from './plugins'
import { searchRouter } from './search'
import { serverRouter } from './server'
import { usersRouter } from './users'
import { actorRouter } from './actor'
import { videoChannelRouter } from './video-channel'
import { videoPlaylistRouter } from './video-playlist'
import { videosRouter } from './videos'
Expand All @@ -39,6 +40,7 @@ apiRouter.use('/bulk', bulkRouter)
apiRouter.use('/oauth-clients', oauthClientsRouter)
apiRouter.use('/config', configRouter)
apiRouter.use('/users', usersRouter)
apiRouter.use('/actors', actorRouter)
apiRouter.use('/accounts', accountsRouter)
apiRouter.use('/video-channels', videoChannelRouter)
apiRouter.use('/video-playlists', videoPlaylistRouter)
Expand Down
10 changes: 10 additions & 0 deletions server/helpers/custom-validators/actor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isAccountNameValid } from './accounts'
import { isVideoChannelNameValid } from './video-channels'

function isActorNameValid (value: string) {
return isAccountNameValid(value) || isVideoChannelNameValid(value)
}

export {
isActorNameValid
}
20 changes: 11 additions & 9 deletions server/helpers/middlewares/video-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { MChannelBannerAccountDefault } from '@server/types/models'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
import { VideoChannelModel } from '../../models/video/video-channel'

async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
async function doesLocalVideoChannelNameExist (name: string, res: express.Response, sendNotFound = true) {
const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)

return processVideoChannelExist(videoChannel, res)
return processVideoChannelExist(videoChannel, res, sendNotFound)
}

async function doesVideoChannelIdExist (id: number, res: express.Response) {
async function doesVideoChannelIdExist (id: number, res: express.Response, sendNotFound = true) {
const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)

return processVideoChannelExist(videoChannel, res)
return processVideoChannelExist(videoChannel, res, sendNotFound)
}

async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response, sendNotFound = true) {
const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)

return processVideoChannelExist(videoChannel, res)
return processVideoChannelExist(videoChannel, res, sendNotFound)
}

// ---------------------------------------------------------------------------
Expand All @@ -29,10 +29,12 @@ export {
doesVideoChannelNameWithHostExist
}

function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) {
function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response, sendNotFound = true) {
if (!videoChannel) {
res.status(HttpStatusCode.NOT_FOUND_404)
.json({ error: 'Video channel not found' })
if (sendNotFound) {
res.status(HttpStatusCode.NOT_FOUND_404)
.json({ error: 'Video channel not found' })
}

return false
}
Expand Down
59 changes: 59 additions & 0 deletions server/middlewares/validators/actor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as express from 'express'
import { param } from 'express-validator'
import { isActorNameValid } from '../../helpers/custom-validators/actor'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
import {
doesAccountNameWithHostExist,
doesLocalAccountNameExist,
doesVideoChannelNameWithHostExist,
doesLocalVideoChannelNameExist
} from '../../helpers/middlewares'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'

const localActorValidator = [
param('actorName').custom(isActorNameValid).withMessage('Should have a valid actor name'),

async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking localActorValidator parameters', { parameters: req.params })

if (areValidationErrors(req, res)) return

const isAccount = await doesLocalAccountNameExist(req.params.actorName, res, false)
const isVideoChannel = await doesLocalVideoChannelNameExist(req.params.actorName, res, false)

if (!isAccount || !isVideoChannel) {
res.status(HttpStatusCode.NOT_FOUND_404)
.json({ error: 'Actor not found' })
}

return next()
}
]

const actorNameWithHostGetValidator = [
param('actorName').exists().withMessage('Should have an actor name with host'),

async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking actorNameWithHostGetValidator parameters', { parameters: req.params })

if (areValidationErrors(req, res)) return

const isAccount = await doesAccountNameWithHostExist(req.params.actorName, res, false)
const isVideoChannel = await doesVideoChannelNameWithHostExist(req.params.actorName, res, false)

if (!isAccount && !isVideoChannel) {
res.status(HttpStatusCode.NOT_FOUND_404)
.json({ error: 'Actor not found' })
}

return next()
}
]

// ---------------------------------------------------------------------------

export {
localActorValidator,
actorNameWithHostGetValidator
}
1 change: 1 addition & 0 deletions server/middlewares/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './abuse'
export * from './account'
export * from './actor'
export * from './actor-image'
export * from './blocklist'
export * from './oembed'
Expand Down