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

feat: add list of assets per space endpoints #207

Merged
merged 2 commits into from
Jun 10, 2024
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
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(
Rybasher marked this conversation as resolved.
Show resolved Hide resolved
Rybasher marked this conversation as resolved.
Show resolved Hide resolved
@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
Loading