import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { FilterQuery, Model, Types } from 'mongoose'; import { Follow, FollowDocument } from '../follows/schemas/follow.schema'; import { Post, PostDocument } from '../posts/schemas/post.schema'; @Injectable() export class FeedRepository { constructor( @InjectModel(Post.name) private readonly postModel: Model, @InjectModel(Follow.name) private readonly followModel: Model, ) {} async findFollowingIds(userId: string): Promise { const rows = await this.followModel .find({ followerId: new Types.ObjectId(userId) }) .select({ followingId: 1 }) .lean() .exec(); return rows.map((row) => row.followingId.toString()); } async findCandidatePosts( filter: FilterQuery, limit: number, ): Promise { const activeFilter: FilterQuery = { ...filter, isDeleted: { $ne: true }, }; return this.postModel .find(activeFilter) .populate({ path: 'authorId', select: 'name username stageName avatar isVerified isDisabled location latitude longitude musicGenres musicRoles favoriteInstruments favoriteMaqamat', }) .sort({ createdAt: -1 }) .limit(limit) .exec(); } async findTrendingPublicPosts(skip: number, limit: number): Promise { return this.postModel .find({ visibility: 'public', isDeleted: { $ne: true } }) .populate({ path: 'authorId', select: 'name username stageName avatar isVerified isDisabled' }) .sort({ likesCount: -1, commentsCount: -1, savesCount: -1, createdAt: -1 }) .skip(skip) .limit(limit) .exec(); } async count(filter: FilterQuery): Promise { return this.postModel.countDocuments({ ...filter, isDeleted: { $ne: true } }).exec(); } }