Skip to content

Commit

Permalink
feat: add list of assets per space endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Rybasher committed Jun 7, 2024
1 parent d5297d1 commit 520b17b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mirror-web-server/src/metadata.ts

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions mirror-web-server/src/space/space.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,18 @@ export class SpaceController {
* END Section: Owner permissions for role modification
*/

@Get('assets-list/:spaceId')
@FirebaseTokenAuthGuard()
public async getAssetsListPerSpace(
@UserToken('user_id') userId: UserId,
@Param('spaceId') spaceId: SpaceId
) {
return await this.spaceService.getAssetsListPerSpaceWithRolesCheck(
userId,
spaceId
)
}

@Post(':id/kickme')
@FirebaseTokenAuthGuard()
@ApiOkResponse({ type: SpacePublicData })
Expand Down
11 changes: 10 additions & 1 deletion mirror-web-server/src/space/space.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum ZoneSpaceWsMessage {
UPDATE = 'zone_update_space',
UPDATE_SPACE_VARIABLES = 'zone_update_space_variables',
PUBLISH = 'zone_publish_space',
GET_SPACE_VERSION = 'zone_get_space_version'
GET_SPACE_VERSION = 'zone_get_space_version',
GET_ASSETS = 'zone_get_space_assets'
}

@WebSocketGateway()
Expand All @@ -27,6 +28,14 @@ export class SpaceGateway {
private readonly logger: Logger
) {}

@SubscribeMessage(ZoneSpaceWsMessage.GET_ASSETS)
public getAssetPerSpaceWithRolesCheck(
@MessageBody('id') id: string,
@MessageBody('userId') userId: string
) {
return this.spaceService.getAssetsListPerSpaceWithRolesCheck(userId, id)
}

@SubscribeMessage(ZoneSpaceWsMessage.GET_ONE)
public findOne(@MessageBody('id') id: string) {
this.logger.log(
Expand Down
71 changes: 71 additions & 0 deletions mirror-web-server/src/space/space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
import { MirrorDBService } from '../mirror-db/mirror-db.service'
import { ScriptEntityService } from '../script-entity/script-entity.service'
import { RemixSpaceDto } from './dto/remix-space-dto'
import { AssetDocument } from '../asset/asset.schema'

/**
* @description This mirrors _standardPopulateFields and should be kept up to date with it. However, this "populate a lot of things" approach is deprecated
Expand Down Expand Up @@ -2072,6 +2073,76 @@ export class SpaceService implements IRoleConsumer {
.exec()
}

// get all assets in a space
public async getAssetsListPerSpaceWithRolesCheck(
userId: UserId,
spaceId: SpaceId
): Promise<AssetDocument[]> {
const space = await this.getSpace(spaceId)

if (!this.canFindWithRolesCheck(userId, space)) {
throw new NotFoundException('Not found or insufficient permissions')
}

const aggregate = [
{
$match: {
space: new ObjectId(spaceId)
}
},
{
$lookup: {
from: 'assets',
localField: 'asset',
foreignField: '_id',
as: 'asset'
}
},
{
$unwind: '$asset'
},
{
$replaceRoot: { newRoot: '$asset' }
}
]

return await this.spaceObjectModel.aggregate(aggregate).exec()
}

public async getAssetsListPerSpaceAdmin(
spaceId: SpaceId
): Promise<AssetDocument[]> {
const space = await this.getSpace(spaceId)

if (!space) {
throw new NotFoundException('Not found')
}

const aggregate = [
{
$match: {
space: new ObjectId(spaceId)
}
},
{
$lookup: {
from: 'assets',
localField: 'asset',
foreignField: '_id',
as: 'asset'
}
},
{
$unwind: '$asset'
},
{
$replaceRoot: { newRoot: '$asset' }
}
]

return await this.spaceObjectModel.aggregate(aggregate).exec()
}

public async kickUserByAdmin(user_id: UserId, space_id: SpaceId) {
const space = await this.getSpace(space_id)
if (!space) {
Expand Down

0 comments on commit 520b17b

Please sign in to comment.