Add shared posts to user profiles
Deploy To Ghaymah / deploy (push) Has been cancelled

This commit is contained in:
boutmoun123
2026-07-05 01:07:41 +03:00
parent 4b9589a3fc
commit 7cd239f8e0
6 changed files with 808 additions and 28 deletions
+34 -2
View File
@@ -1,6 +1,7 @@
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Types } from 'mongoose';
import { InjectConnection } from '@nestjs/mongoose';
import { Connection, Types } from 'mongoose';
import { performance } from 'perf_hooks';
import { PostType } from '../../common/enums/post-type.enum';
import { PostVisibility } from '../../common/enums/post-visibility.enum';
@@ -12,6 +13,7 @@ import { BlocksService } from '../blocks/blocks.service';
import { FollowsService } from '../follows/follows.service';
import { LikesRepository } from '../likes/likes.repository';
import { MarketplaceService } from '../marketplace/marketplace.service';
import { PostShareTarget } from '../posts/dto/share-post.dto';
import { SavesRepository } from '../saves/saves.repository';
import { UserDocument } from '../users/schemas/user.schema';
import { UsersRepository } from '../users/users.repository';
@@ -23,6 +25,7 @@ type FeedPostItem = Record<string, unknown> & {
feedScore?: number;
likedByMe: boolean;
savedByMe: boolean;
isSharedByMe: boolean;
followingAuthor: boolean;
isOwnPost: boolean;
canComment: boolean;
@@ -71,6 +74,7 @@ export class FeedService {
private readonly followsService: FollowsService,
private readonly marketplaceService: MarketplaceService,
private readonly blocksService: BlocksService,
@InjectConnection() private readonly connection: Connection,
) {}
async getMyFeed(currentUserId: string, query: FeedQueryDto) {
@@ -333,12 +337,14 @@ export class FeedService {
.map((item) => this.extractEntityId(item._id ?? item.id))
.filter(Boolean);
const followingSet = new Set(followingIds);
const [likedPostIds, savedPostIds] = await Promise.all([
const [likedPostIds, savedPostIds, sharedPostIds] = await Promise.all([
this.likesRepository.findLikedPostIds(currentUserId, postIds),
this.savesRepository.findSavedPostIds(currentUserId, postIds),
this.findSharedPostIds(currentUserId, postIds),
]);
const likedSet = new Set(likedPostIds);
const savedSet = new Set(savedPostIds);
const sharedSet = new Set(sharedPostIds);
return items.map((item) => {
const postId = this.extractEntityId(item._id ?? item.id);
@@ -356,6 +362,7 @@ export class FeedService {
feedItemType: 'post',
likedByMe: likedSet.has(postId),
savedByMe: savedSet.has(postId),
isSharedByMe: sharedSet.has(postId),
followingAuthor: !!authorId && followingSet.has(authorId),
isOwnPost: authorId === currentUserId,
canComment: true,
@@ -372,6 +379,31 @@ export class FeedService {
});
}
private async findSharedPostIds(currentUserId: string, postIds: string[]): Promise<string[]> {
if (!Types.ObjectId.isValid(currentUserId) || postIds.length === 0) {
return [];
}
const validPostIds = Array.from(new Set(postIds.filter((id) => Types.ObjectId.isValid(id))));
if (!validPostIds.length) {
return [];
}
const rows = await this.connection
.collection<{ postId: Types.ObjectId }>('postshares')
.find({
userId: new Types.ObjectId(currentUserId),
postId: { $in: validPostIds.map((id) => new Types.ObjectId(id)) },
target: PostShareTarget.PROFILE,
isDeleted: false,
deletedAt: null,
})
.project({ postId: 1 })
.toArray();
return rows.map((row) => row.postId.toString());
}
private async mixHomeFeedItems(
currentUserId: string,
posts: FeedPostItem[],