From ccd2fe1ee6074397f2df222fa3d25179fc0668e5 Mon Sep 17 00:00:00 2001 From: Muhammad Abdul Hakim Shibghatallah <70675129+abdulhakim2902@users.noreply.github.com> Date: Tue, 16 May 2023 13:06:12 +0700 Subject: [PATCH] fix: handle get post by originPostId (#866) Co-authored-by: Abdul Hakim --- src/controllers/user/post.controller.ts | 4 +++- src/services/post.service.ts | 23 +++++++++++++++++++++-- src/services/user.service.ts | 8 +++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/controllers/user/post.controller.ts b/src/controllers/user/post.controller.ts index ec73e437c..18ba50a4a 100644 --- a/src/controllers/user/post.controller.ts +++ b/src/controllers/user/post.controller.ts @@ -24,6 +24,7 @@ import { UpdatePostDto, } from '../../models'; import {UserService} from '../../services'; +import {PlatformType} from '../../enums'; @authenticate('jwt') export class UserPostController { @@ -78,8 +79,9 @@ export class UserPostController { async findById( @param.path.string('id') id: string, @param.filter(Post, {exclude: 'where'}) filter?: FilterExcludingWhere, + @param.query.string('platform') platform?: PlatformType, ): Promise { - return this.userService.post(id, filter); + return this.userService.post(id, filter, platform); } @get('/user/posts/action') diff --git a/src/services/post.service.ts b/src/services/post.service.ts index 3136ae057..cb6822eaf 100644 --- a/src/services/post.service.ts +++ b/src/services/post.service.ts @@ -28,6 +28,7 @@ import { Friend, People, Post, + PostRelations, PostWithRelations, User, } from '../models'; @@ -228,11 +229,29 @@ export class PostService { public async findById( id: string, - filter?: Filter, + filter = {} as Filter, withImporter = false, userId?: string, + platform?: PlatformType, ): Promise { - const currentPost = await this.postRepository.findById(id, filter); + let currentPost: Post & PostRelations; + + if (platform && platform !== PlatformType.MYRIAD) { + const post = await this.postRepository.findOne({ + where: { + originPostId: id, + platform, + }, + }); + if (!post) { + throw new HttpErrors.NotFound('PostNotFound'); + } + + currentPost = post; + } else { + currentPost = await this.postRepository.findById(id, filter); + } + if (!withImporter) return currentPost; await this.validateUnrestrictedPost(currentPost, userId); return this.postWithImporterInfo(currentPost, userId); diff --git a/src/services/user.service.ts b/src/services/user.service.ts index d03ad870c..3d057d366 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -20,6 +20,7 @@ import { ControllerType, FriendStatusType, PermissionKeys, + PlatformType, ReferenceType, } from '../enums'; import {TokenServiceBindings} from '../keys'; @@ -774,12 +775,17 @@ export class UserService { ); } - public async post(id: string, filter?: Filter): Promise { + public async post( + id: string, + filter?: Filter, + platform?: PlatformType, + ): Promise { return this.postService.findById( id, filter, true, this.currentUser[securityId], + platform, ); }