Skip to content

Commit

Permalink
fix: handle get post by originPostId (#866)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdul Hakim <hakim@undercurrent.tech>
  • Loading branch information
abdulhakim2902 and abdulhakim2902 committed May 16, 2023
1 parent fc1f0c0 commit ccd2fe1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/controllers/user/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UpdatePostDto,
} from '../../models';
import {UserService} from '../../services';
import {PlatformType} from '../../enums';

@authenticate('jwt')
export class UserPostController {
Expand Down Expand Up @@ -78,8 +79,9 @@ export class UserPostController {
async findById(
@param.path.string('id') id: string,
@param.filter(Post, {exclude: 'where'}) filter?: FilterExcludingWhere<Post>,
@param.query.string('platform') platform?: PlatformType,
): Promise<Post> {
return this.userService.post(id, filter);
return this.userService.post(id, filter, platform);
}

@get('/user/posts/action')
Expand Down
23 changes: 21 additions & 2 deletions src/services/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
Friend,
People,
Post,
PostRelations,
PostWithRelations,
User,
} from '../models';
Expand Down Expand Up @@ -228,11 +229,29 @@ export class PostService {

public async findById(
id: string,
filter?: Filter<Post>,
filter = {} as Filter<Post>,
withImporter = false,
userId?: string,
platform?: PlatformType,
): Promise<Post> {
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);
Expand Down
8 changes: 7 additions & 1 deletion src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ControllerType,
FriendStatusType,
PermissionKeys,
PlatformType,
ReferenceType,
} from '../enums';
import {TokenServiceBindings} from '../keys';
Expand Down Expand Up @@ -774,12 +775,17 @@ export class UserService {
);
}

public async post(id: string, filter?: Filter<Post>): Promise<Post> {
public async post(
id: string,
filter?: Filter<Post>,
platform?: PlatformType,
): Promise<Post> {
return this.postService.findById(
id,
filter,
true,
this.currentUser[securityId],
platform,
);
}

Expand Down

0 comments on commit ccd2fe1

Please sign in to comment.