Skip to content

Commit

Permalink
Merge pull request #85 from cuappdev/tony/blockingupdated
Browse files Browse the repository at this point in the history
implement blocking filtering
  • Loading branch information
akmatchev authored Apr 18, 2024
2 parents 3ecbf09 + 3f61822 commit 6ebf41a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 60 deletions.
32 changes: 16 additions & 16 deletions src/api/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ export class PostController {
}

@Get()
async getPosts(): Promise<GetPostsResponse> {
return { posts: await this.postService.getAllPosts() };
async getPosts(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.getAllPosts(user) };
}

@Get('id/:id/')
async getPostById(@Params() params: UuidParam): Promise<GetPostResponse> {
return { post: await this.postService.getPostById(params) };
async getPostById(@CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<GetPostResponse> {
return { post: await this.postService.getPostById(user, params) };
}

@Get('userId/:id/')
async getPostsByUserId(@Params() params: UuidParam): Promise<GetPostsResponse> {
return { posts: await this.postService.getPostsByUserId(params) };
async getPostsByUserId(@CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<GetPostsResponse> {
return { posts: await this.postService.getPostsByUserId(user, params) };
}

@Post()
Expand All @@ -49,23 +49,23 @@ export class PostController {
}

@Post('search/')
async searchPosts(@Body() getSearchedPostsRequest: GetSearchedPostsRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.searchPosts(getSearchedPostsRequest) };
async searchPosts(@CurrentUser() user: UserModel, @Body() getSearchedPostsRequest: GetSearchedPostsRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.searchPosts(user, getSearchedPostsRequest) };
}

@Post('filter/')
async filterPosts(@Body() filterPostsRequest: FilterPostsRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPosts(filterPostsRequest) };
async filterPosts(@CurrentUser() user: UserModel, @Body() filterPostsRequest: FilterPostsRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPosts(user, filterPostsRequest) };
}

@Post('filterByPrice/')
async filterPostsByPrice(@Body() filterPostsByPriceRequest: FilterPostsByPriceRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPostsByPrice(filterPostsByPriceRequest) };
async filterPostsByPrice(@CurrentUser() user: UserModel, @Body() filterPostsByPriceRequest: FilterPostsByPriceRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPostsByPrice(user, filterPostsByPriceRequest) };
}

@Get('archive/')
async getArchivedPosts(): Promise<GetPostsResponse> {
return { posts: await this.postService.getArchivedPosts() };
async getArchivedPosts(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.getArchivedPosts(user) };
}

@Get('archive/userId/:id/')
Expand Down Expand Up @@ -104,7 +104,7 @@ export class PostController {
}

@Get('similar/postId/:id/')
async similarPosts(@Params() params: UuidParam): Promise<GetPostsResponse> {
return { posts: await this.postService.similarPosts(params) };
async similarPosts(@CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<GetPostsResponse> {
return { posts: await this.postService.similarPosts(user, params) };
}
}
62 changes: 44 additions & 18 deletions src/services/PostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ export class PostService {
this.transactions = new TransactionsManager(entityManager);
}

public async getAllPosts(): Promise<PostModel[]> {
public async getAllPosts(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
// filter out posts from inactive users
return (await postRepository.getAllPosts()).filter((post) => post.user?.isActive);
const activeUserPosts = this.filterInactiveUserPosts(await postRepository.getAllPosts());
return this.filterBlockedUserPosts(activeUserPosts, user);
});
}

public async getPostById(params: UuidParam): Promise<PostModel> {
public async getPostById(user: UserModel, params: UuidParam): Promise<PostModel> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const post = await postRepository.getPostById(params.id);
if (!post) throw new NotFoundError('Post not found!');
if (!post.user?.isActive) throw new NotFoundError('User is not active!');
const postUnblocked = await this.filterBlockedUserPosts([post], user);
if (postUnblocked.length == 0) throw new ForbiddenError('User is blocked!');
return post;
});
}

public async getPostsByUserId(params: UuidParam): Promise<PostModel[]> {
public async getPostsByUserId(currentUser: UserModel, params: UuidParam): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
const user = await userRepository.getUserById(params.id)
if (!user) throw new NotFoundError('User not found!');
if (!user.isActive) throw new NotFoundError('User is not active!');
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.getPostsByUserId(params.id);
return posts;
return this.filterBlockedUserPosts(user.posts, currentUser);
});
}

Expand Down Expand Up @@ -97,7 +97,7 @@ export class PostService {
});
}

public async searchPosts(getSearchedPostsRequest: GetSearchedPostsRequest): Promise<PostModel[]> {
public async searchPosts(user: UserModel, getSearchedPostsRequest: GetSearchedPostsRequest): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const postsByTitle = await postRepository.searchPostsByTitle(getSearchedPostsRequest.keywords);
Expand All @@ -116,30 +116,35 @@ export class PostService {
posts.push(pd);
}
});
return posts.filter((post) => post.user?.isActive);
let activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async filterPosts(filterPostsRequest: FilterPostsRequest): Promise<PostModel[]> {
public async filterPosts(user: UserModel, filterPostsRequest: FilterPostsRequest): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterPosts(filterPostsRequest.category);
return posts.filter((post) => post.user?.isActive);
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async filterPostsByPrice(filterPostsByPriceRequest: FilterPostsByPriceRequest): Promise<PostModel[]> {
public async filterPostsByPrice(user: UserModel, filterPostsByPriceRequest: FilterPostsByPriceRequest): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterPostsByPrice(filterPostsByPriceRequest.lowerBound, filterPostsByPriceRequest.upperBound)
return posts.filter((post) => post.user?.isActive);
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
})
}

public async getArchivedPosts(): Promise<PostModel[]> {
public async getArchivedPosts(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
return (await postRepository.getArchivedPosts()).filter((post) => post.user?.isActive);
const posts = await postRepository.getArchivedPosts();
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

Expand Down Expand Up @@ -232,7 +237,7 @@ export class PostService {
return result;
}

public async similarPosts(params: UuidParam): Promise<PostModel[]> {
public async similarPosts(user: UserModel, params: UuidParam): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const post = await postRepository.getPostById(params.id);
Expand All @@ -256,7 +261,28 @@ export class PostService {
});
}
}
return posts.filter((post) => post.user?.isActive);
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public filterInactiveUserPosts(posts: PostModel[]): PostModel[] {
return posts.filter((post) => post.user?.isActive);
}

public async filterBlockedUserPosts(posts: PostModel[], user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
const userWithBlockedInfo = await userRepository.getUserWithBlockedInfo(user.id);
const blockedUsers = userWithBlockedInfo?.blocking;
return posts.filter((post) => {
if (blockedUsers) {
for (const blockedUser of blockedUsers) {
if (post.user?.id == blockedUser.id) return false;
}
}
return true;
});
});
}
}
Expand Down
Loading

0 comments on commit 6ebf41a

Please sign in to comment.