universo-platform-3d
405 строк · 10.8 Кб
1import {2MessageBody,3SubscribeMessage,4WebSocketGateway
5} from '@nestjs/websockets'6import { SpaceObjectService } from './space-object.service'7import { Logger, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common'8import { CreateSpaceObjectDto } from './dto/create-space-object.dto'9import { UpdateSpaceObjectDto } from './dto/update-space-object.dto'10import { UpdateBatchSpaceObjectDto } from './dto/update-batch-space-object.dto'11import { GodotSocketInterceptor } from '../godot-server/godot-socket.interceptor'12import { GodotSocketExceptionFilter } from '../godot-server/godot-socket-exception.filter'13import { SpaceObjectId, UserId } from '../util/mongo-object-id-helpers'14import {15AdminTokenWS,16UserTokenWS
17} from '../godot-server/get-user-ws.decorator'18import { da } from 'date-fns/locale'19import { WsAuthGuard } from '../godot-server/ws-auth.guard'20
21enum ZoneSpaceObjectWsMessage {22GET = 'zone_get_space_object',23CREATE = 'zone_create_space_object',24UPDATE = 'zone_update_space_object',25REMOVE = 'zone_delete_space_object',26GET_PAGE = 'zone_get_space_objects_page',27GET_BATCH = 'zone_get_batch_space_objects',28GET_PRELOAD_SPACE_OBJECTS = 'zone_get_preload_space_objects',29UPDATE_BATCH = 'zone_update_batch_space_objects',30REMOVE_BATCH = 'zone_delete_batch_space_objects'31}
32
33@WebSocketGateway()34@UseGuards(WsAuthGuard)35@UseInterceptors(GodotSocketInterceptor)36@UseFilters(GodotSocketExceptionFilter)37export class SpaceObjectGateway {38constructor(39private readonly spaceObjectService: SpaceObjectService,40private readonly logger: Logger41) {}42
43@SubscribeMessage(ZoneSpaceObjectWsMessage.CREATE)44public create(45@AdminTokenWS() isAdmin: boolean,46@UserTokenWS('user_id') userId: UserId,47@MessageBody('dto')48createSpaceObjectDto: CreateSpaceObjectDto & { creatorUserId?: UserId }49) {50this.logger.log(51`${JSON.stringify(52{53ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.CREATE,54createSpaceObjectDto: createSpaceObjectDto55// TODO: this needs to be updated to include the creator's user ID! 2023-04-21 01:01:06. Do not merge until the Godot server adds that in.56},57null,58259)}`,60SpaceObjectGateway.name61)62
63if (userId) {64return this.spaceObjectService.createOneWithRolesCheck(65userId,66createSpaceObjectDto
67)68}69
70if (isAdmin) {71return this.spaceObjectService.createOneAdmin(createSpaceObjectDto)72}73return74}75
76@SubscribeMessage(ZoneSpaceObjectWsMessage.GET_PAGE)77public getAllBySpaceIdPaginated(78@AdminTokenWS() isAdmin: boolean,79@UserTokenWS('user_id') userId: UserId,80@MessageBody('id') spaceId: string,81@MessageBody('page') page: number,82@MessageBody('perPage') perPage: number83) {84this.logger.log(85`${JSON.stringify(86{87ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.GET_PAGE,88spaceId,89page,90perPage
91},92null,93294)}`,95SpaceObjectGateway.name96)97
98if (isAdmin) {99return this.spaceObjectService.getAllBySpaceIdPaginatedAdmin(spaceId, {100page,101perPage
102})103}104
105if (userId) {106return this.spaceObjectService.getAllBySpaceIdPaginatedWithRolesCheck(107userId,108spaceId,109{110page,111perPage
112}113)114}115return116}117
118@SubscribeMessage(ZoneSpaceObjectWsMessage.GET_BATCH)119public findMany(120@AdminTokenWS() isAdmin: boolean,121@UserTokenWS('user_id') userId: UserId,122@MessageBody('batch') ids: string[],123@MessageBody('page') page: number,124@MessageBody('perPage') perPage: number125) {126this.logger.log(127`${JSON.stringify(128{129ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.GET_BATCH,130'batch (ids)': ids,131page,132perPage
133},134null,1352136)}`,137SpaceObjectGateway.name138)139if (isAdmin) {140return this.spaceObjectService.findManyAdmin(ids, { page, perPage })141}142
143if (userId) {144return this.spaceObjectService.findManyWithRolesCheck(userId, ids, {145page,146perPage
147})148}149return150}151
152@SubscribeMessage(ZoneSpaceObjectWsMessage.GET_PRELOAD_SPACE_OBJECTS)153public getAllPreloadSpaceObjectsBySpaceIdPaginated(154@AdminTokenWS() isAdmin: boolean,155@UserTokenWS('user_id') userId: UserId,156@MessageBody('id') spaceId: string,157@MessageBody('page') page: number,158@MessageBody('perPage') perPage: number159) {160const options = { space: spaceId, preloadBeforeSpaceStarts: true }161this.logger.log(162`${JSON.stringify(163{164ZoneSpaceObjectWsMessage:165ZoneSpaceObjectWsMessage.GET_PRELOAD_SPACE_OBJECTS,166spaceId,167page,168perPage,169options
170},171null,1722173)}`,174SpaceObjectGateway.name175)176
177if (isAdmin) {178return this.spaceObjectService.getAllBySpaceIdPaginatedAdmin(179spaceId,180{181page,182perPage
183},184options
185)186}187
188if (userId) {189return this.spaceObjectService.getAllBySpaceIdPaginatedWithRolesCheck(190userId,191spaceId,192{193page,194perPage
195},196options
197)198}199return200}201
202/**203* @description Returns the spaceObject with optional populated properties
204* populateParent: true: populates the single parentSpaceObject property to be the object
205* recursiveParentLookup: true: will recursively populate the parentSpaceObject property AND the parentSpaceObject (so populateParent is disregarded)
206* recursiveChildrenPopulate: true: will recursively populate childSpaceObjects
207*
208* Walkthrough: https://www.loom.com/share/a4e5e27f27b849fabdb1806235c7e48b
209* @date 2023-07-04 17:43
210*/
211@SubscribeMessage(ZoneSpaceObjectWsMessage.GET)212public async findOneWithSingleParentSpaceObject(213@AdminTokenWS() isAdmin: boolean,214@UserTokenWS('user_id') userId: UserId,215@MessageBody('id') spaceObjectId: SpaceObjectId,216@MessageBody('populateParent') populateParent = false, // if true, decently fast, 1 lookup217@MessageBody('recursiveParentPopulate') recursiveParentPopulate = false, // if true, slower with $graphLookup218@MessageBody('recursiveChildrenPopulate') recursiveChildrenPopulate = false // if true, slowest because 2 $graphLookups219) {220if (!isAdmin && !userId) {221return222}223this.logger.log(224`${JSON.stringify(225{226ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.GET,227id: spaceObjectId,228populateParent,229recursiveParentPopulate,230recursiveChildrenPopulate
231},232null,2332234)}`,235SpaceObjectGateway.name236)237let returnData: any238// first check the parent populates, prioritizing recursiveParentPopulate239if (recursiveParentPopulate) {240returnData =241await this.spaceObjectService.findOneAdminWithPopulatedParentSpaceObjectRecursiveLookup(242spaceObjectId
243)244} else if (populateParent) {245returnData =246await this.spaceObjectService.findOneAdminWithPopulatedParentSpaceObject(247spaceObjectId
248)249} else {250// no recursive parent lookup nor parent lookup, so just find the spaceObject by itself251let data252
253if (userId) {254data = await this.spaceObjectService.findOneWithRolesCheck(255userId,256spaceObjectId
257)258}259
260if (isAdmin) {261data = await this.spaceObjectService.findOneAdmin(spaceObjectId)262}263
264returnData = data.toJSON() // needs to be converted to JSON so it can be modified below265}266
267// check children populates268if (recursiveChildrenPopulate) {269const childLookup =270await this.spaceObjectService.findOneAdminWithPopulatedChildSpaceObjectsRecursiveLookup(271spaceObjectId
272)273// if it worked, merge the data274
275if (childLookup) {276returnData['childSpaceObjects'] = childLookup.childSpaceObjects277} else {278returnData['childSpaceObjects'] = []279}280}281
282return returnData283}284
285@SubscribeMessage(ZoneSpaceObjectWsMessage.UPDATE_BATCH)286public async updateMany(287@UserTokenWS('user_id') userId: UserId,288@AdminTokenWS() isAdmin: boolean,289@MessageBody() batchDto: UpdateBatchSpaceObjectDto290) {291this.logger.log(292`${JSON.stringify(293{294ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.UPDATE_BATCH,295batchDto
296},297null,2982299)}`,300SpaceObjectGateway.name301)302
303if (isAdmin) {304return await this.spaceObjectService.updateManyAdmin(batchDto)305}306
307if (userId) {308return await this.spaceObjectService.updateManyWithRolesCheck(309userId,310batchDto
311)312}313return314}315
316@SubscribeMessage(ZoneSpaceObjectWsMessage.UPDATE)317public updateOne(318@AdminTokenWS() isAdmin: boolean,319@UserTokenWS('user_id') userId: UserId,320@MessageBody('id') id: string,321@MessageBody('dto') updateSpaceObjectDto: UpdateSpaceObjectDto322) {323this.logger.log(324`${JSON.stringify(325{326ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.UPDATE,327id,328updateSpaceObjectDto
329},330null,3312332)}`,333SpaceObjectGateway.name334)335
336if (userId) {337return this.spaceObjectService.updateOneWithRolesCheck(338userId,339id,340updateSpaceObjectDto
341)342}343
344if (isAdmin) {345return this.spaceObjectService.updateOneAdmin(id, updateSpaceObjectDto)346}347return348}349
350@SubscribeMessage(ZoneSpaceObjectWsMessage.REMOVE_BATCH)351public removeMany(352@UserTokenWS('user_id') userId: UserId,353@AdminTokenWS() isAdmin: boolean,354@MessageBody('batch') ids: string[]355) {356this.logger.log(357`${JSON.stringify(358{359ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.REMOVE_BATCH,360'batch (ids)': ids361},362null,3632364)}`,365SpaceObjectGateway.name366)367
368if (isAdmin) {369return this.spaceObjectService.removeManyAdmin(ids)370}371
372if (userId) {373return this.spaceObjectService.removeManyWithRolesCheck(userId, ids)374}375return376}377
378@SubscribeMessage(ZoneSpaceObjectWsMessage.REMOVE)379public removeOne(380@AdminTokenWS() isAdmin: boolean,381@UserTokenWS('user_id') userId: UserId,382@MessageBody('id') id: string383) {384this.logger.log(385`${JSON.stringify(386{387ZoneSpaceObjectWsMessage: ZoneSpaceObjectWsMessage.REMOVE,388id: id389},390null,3912392)}`,393SpaceObjectGateway.name394)395
396if (userId) {397return this.spaceObjectService.removeOneWithRolesCheck(userId, id)398}399
400if (isAdmin) {401return this.spaceObjectService.removeOneAdmin(id)402}403return404}405}
406