feat: expand backend admin marketplace and scaling
فشلت بعض الفحوصات
/ deploy (push) Failing after 1m22s
فشلت بعض الفحوصات
/ deploy (push) Failing after 1m22s
هذا الالتزام موجود في:
@@ -1,21 +1,97 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Types } from 'mongoose';
|
||||
import { decodeOffsetCursor, encodeOffsetCursor } from '../../common/utils/cursor.util';
|
||||
import { PostType } from '../../common/enums/post-type.enum';
|
||||
import { PostVisibility } from '../../common/enums/post-visibility.enum';
|
||||
import { UsersRepository } from '../users/users.repository';
|
||||
import { decodeOffsetCursor, encodeOffsetCursor } from '../../common/utils/cursor.util';
|
||||
import { buildPaginatedResponse } from '../../common/utils/pagination.util';
|
||||
import { AppCacheService } from '../../infrastructure/cache/app-cache.service';
|
||||
import { FeedVersionService } from '../../infrastructure/cache/feed-version.service';
|
||||
import { FollowsService } from '../follows/follows.service';
|
||||
import { LikesRepository } from '../likes/likes.repository';
|
||||
import { MarketplaceService } from '../marketplace/marketplace.service';
|
||||
import { SavesRepository } from '../saves/saves.repository';
|
||||
import { UserDocument } from '../users/schemas/user.schema';
|
||||
import { UsersRepository } from '../users/users.repository';
|
||||
import { FeedQueryDto } from './dto/feed-query.dto';
|
||||
import { FeedRepository } from './feed.repository';
|
||||
|
||||
type FeedPostItem = Record<string, unknown> & {
|
||||
feedItemType: 'post';
|
||||
feedScore?: number;
|
||||
likedByMe: boolean;
|
||||
savedByMe: boolean;
|
||||
followingAuthor: boolean;
|
||||
isOwnPost: boolean;
|
||||
canComment: boolean;
|
||||
canMessage: boolean;
|
||||
engagement: {
|
||||
likesCount: number;
|
||||
commentsCount: number;
|
||||
savesCount: number;
|
||||
shareCount: number;
|
||||
viewCount: number;
|
||||
playCount: number;
|
||||
};
|
||||
};
|
||||
|
||||
type FeedCardItem =
|
||||
| {
|
||||
id: string;
|
||||
feedItemType: 'suggested_users';
|
||||
title: string;
|
||||
subtitle: string;
|
||||
items: Array<Record<string, unknown>>;
|
||||
}
|
||||
| {
|
||||
id: string;
|
||||
feedItemType: 'featured_marketplace';
|
||||
title: string;
|
||||
subtitle: string;
|
||||
listings: Array<Record<string, unknown>>;
|
||||
musicalInstruments: Array<Record<string, unknown>>;
|
||||
instruments: Array<Record<string, unknown>>;
|
||||
repairShops: Array<Record<string, unknown>>;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class FeedService {
|
||||
constructor(
|
||||
private readonly feedRepository: FeedRepository,
|
||||
private readonly usersRepository: UsersRepository,
|
||||
private readonly cacheService: AppCacheService,
|
||||
private readonly feedVersionService: FeedVersionService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly likesRepository: LikesRepository,
|
||||
private readonly savesRepository: SavesRepository,
|
||||
private readonly followsService: FollowsService,
|
||||
private readonly marketplaceService: MarketplaceService,
|
||||
) {}
|
||||
|
||||
async getMyFeed(currentUserId: string, query: FeedQueryDto) {
|
||||
const cacheEnabled =
|
||||
this.configService.get<boolean>('feedCache.enabled', { infer: true }) ?? true;
|
||||
const globalVersion = cacheEnabled ? await this.feedVersionService.getGlobalVersion() : 0;
|
||||
const includeSuggestions = this.shouldIncludeSuggestions(query);
|
||||
const cacheKey = this.buildCacheKey('me', {
|
||||
currentUserId,
|
||||
globalVersion,
|
||||
page: query.page ?? 1,
|
||||
limit: query.limit ?? 20,
|
||||
cursor: query.cursor ?? '',
|
||||
followingOnly: query.followingOnly ?? false,
|
||||
radiusKm: query.radiusKm ?? 30,
|
||||
preferredPostType: query.preferredPostType ?? '',
|
||||
includeSuggestions,
|
||||
suggestionInterval: query.suggestionInterval ?? 4,
|
||||
});
|
||||
if (cacheEnabled) {
|
||||
const cached = await this.cacheService.get<Record<string, unknown>>(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
const currentUser = await this.usersRepository.findById(currentUserId);
|
||||
if (!currentUser) {
|
||||
throw new NotFoundException('Current user not found');
|
||||
@@ -26,20 +102,10 @@ export class FeedService {
|
||||
const page = query.page ?? 1;
|
||||
const followingOnly = query.followingOnly ?? false;
|
||||
const radiusKm = query.radiusKm ?? 30;
|
||||
const skip = cursorOffset ?? (page - 1) * limit;
|
||||
|
||||
const followingIds = await this.feedRepository.findFollowingIds(currentUserId);
|
||||
const visibleAuthorIds = followingOnly ? [currentUserId, ...followingIds] : null;
|
||||
|
||||
const filter: Record<string, unknown> = {
|
||||
$or: [
|
||||
{ visibility: PostVisibility.PUBLIC },
|
||||
{ authorId: new Types.ObjectId(currentUserId) },
|
||||
],
|
||||
};
|
||||
|
||||
if (visibleAuthorIds) {
|
||||
filter.authorId = { $in: visibleAuthorIds.map((id) => new Types.ObjectId(id)) };
|
||||
}
|
||||
const filter = this.buildVisiblePostsFilter(currentUserId, followingIds, followingOnly);
|
||||
|
||||
const candidates = await this.feedRepository.findCandidatePosts(filter, Math.max(limit * 12, 300));
|
||||
|
||||
@@ -64,48 +130,268 @@ export class FeedService {
|
||||
.sort(
|
||||
(a, b) =>
|
||||
b.score - a.score ||
|
||||
new Date((b.post as any).createdAt ?? 0).getTime() - new Date((a.post as any).createdAt ?? 0).getTime(),
|
||||
new Date((b.post as any).createdAt ?? 0).getTime() -
|
||||
new Date((a.post as any).createdAt ?? 0).getTime(),
|
||||
);
|
||||
|
||||
const total = scored.length;
|
||||
const skip = cursorOffset ?? (page - 1) * limit;
|
||||
const items = scored.slice(skip, skip + limit).map((entry) => ({
|
||||
...entry.post.toObject(),
|
||||
const pagedPosts = scored.slice(skip, skip + limit).map((entry) => ({
|
||||
...(entry.post.toObject() as unknown as Record<string, unknown>),
|
||||
feedScore: Number(entry.score.toFixed(3)),
|
||||
}));
|
||||
const nextOffset = skip + items.length;
|
||||
const decoratedPosts = await this.decoratePostsForViewer(currentUserId, pagedPosts, followingIds);
|
||||
const items = includeSuggestions
|
||||
? await this.mixHomeFeedItems(currentUserId, decoratedPosts, query.suggestionInterval ?? 4)
|
||||
: decoratedPosts;
|
||||
const nextOffset = skip + pagedPosts.length;
|
||||
const nextCursor = nextOffset < total ? encodeOffsetCursor(nextOffset) : null;
|
||||
|
||||
return {
|
||||
items,
|
||||
const result = buildPaginatedResponse(items, {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit) || 1,
|
||||
offset: skip,
|
||||
currentCursor: query.cursor ?? null,
|
||||
nextCursor,
|
||||
};
|
||||
mode: 'cursor',
|
||||
});
|
||||
|
||||
if (cacheEnabled) {
|
||||
await this.cacheService.set(
|
||||
cacheKey,
|
||||
result,
|
||||
this.configService.get<number>('feedCache.userFeedTtlSeconds', { infer: true }) ?? 15,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async getTrending(query: FeedQueryDto) {
|
||||
async getTrending(currentUserId: string, query: FeedQueryDto) {
|
||||
const cacheEnabled =
|
||||
this.configService.get<boolean>('feedCache.enabled', { infer: true }) ?? true;
|
||||
const globalVersion = cacheEnabled ? await this.feedVersionService.getGlobalVersion() : 0;
|
||||
const cacheKey = this.buildCacheKey('trending', {
|
||||
currentUserId,
|
||||
globalVersion,
|
||||
page: query.page ?? 1,
|
||||
limit: query.limit ?? 20,
|
||||
cursor: query.cursor ?? '',
|
||||
preferredPostType: query.preferredPostType ?? '',
|
||||
});
|
||||
if (cacheEnabled) {
|
||||
const cached = await this.cacheService.get<Record<string, unknown>>(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
const limit = query.limit ?? 20;
|
||||
const cursorOffset = decodeOffsetCursor(query.cursor);
|
||||
const page = query.page ?? 1;
|
||||
const skip = cursorOffset ?? (page - 1) * limit;
|
||||
const followingIds = await this.feedRepository.findFollowingIds(currentUserId);
|
||||
const trendingFilter: Record<string, unknown> = { visibility: PostVisibility.PUBLIC };
|
||||
if (query.preferredPostType) {
|
||||
trendingFilter.postType = query.preferredPostType;
|
||||
}
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
this.feedRepository.findTrendingPublicPosts(skip, limit),
|
||||
this.feedRepository.count({ visibility: PostVisibility.PUBLIC }),
|
||||
const [rows, total] = await Promise.all([
|
||||
this.feedRepository.findTrendingPublicPosts(trendingFilter, skip, limit),
|
||||
this.feedRepository.count(trendingFilter),
|
||||
]);
|
||||
const nextOffset = skip + items.length;
|
||||
const decoratedPosts = await this.decoratePostsForViewer(
|
||||
currentUserId,
|
||||
rows.map((item) => item.toObject() as unknown as Record<string, unknown>),
|
||||
followingIds,
|
||||
);
|
||||
const nextOffset = skip + rows.length;
|
||||
const nextCursor = nextOffset < total ? encodeOffsetCursor(nextOffset) : null;
|
||||
|
||||
return {
|
||||
items,
|
||||
const result = buildPaginatedResponse(decoratedPosts, {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit) || 1,
|
||||
offset: skip,
|
||||
currentCursor: query.cursor ?? null,
|
||||
nextCursor,
|
||||
mode: 'cursor',
|
||||
});
|
||||
|
||||
if (cacheEnabled) {
|
||||
await this.cacheService.set(
|
||||
cacheKey,
|
||||
result,
|
||||
this.configService.get<number>('feedCache.trendingTtlSeconds', { infer: true }) ?? 30,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async decoratePostsForViewer(
|
||||
currentUserId: string,
|
||||
items: Array<Record<string, unknown>>,
|
||||
followingIds: string[],
|
||||
): Promise<FeedPostItem[]> {
|
||||
const postIds = items
|
||||
.map((item) => this.extractEntityId(item._id ?? item.id))
|
||||
.filter(Boolean);
|
||||
const followingSet = new Set(followingIds);
|
||||
const [likedPostIds, savedPostIds] = await Promise.all([
|
||||
this.likesRepository.findLikedPostIds(currentUserId, postIds),
|
||||
this.savesRepository.findSavedPostIds(currentUserId, postIds),
|
||||
]);
|
||||
const likedSet = new Set(likedPostIds);
|
||||
const savedSet = new Set(savedPostIds);
|
||||
|
||||
return items.map((item) => {
|
||||
const postId = this.extractEntityId(item._id ?? item.id);
|
||||
const authorId = this.extractEntityId(item.authorId);
|
||||
const likesCount = Number(item.likesCount ?? 0);
|
||||
const commentsCount = Number(item.commentsCount ?? 0);
|
||||
const savesCount = Number(item.savesCount ?? 0);
|
||||
const shareCount = Number(item.shareCount ?? 0);
|
||||
const viewCount = Number(item.viewCount ?? 0);
|
||||
const playCount = Number(item.playCount ?? 0);
|
||||
|
||||
return {
|
||||
...item,
|
||||
id: postId,
|
||||
feedItemType: 'post',
|
||||
likedByMe: likedSet.has(postId),
|
||||
savedByMe: savedSet.has(postId),
|
||||
followingAuthor: !!authorId && followingSet.has(authorId),
|
||||
isOwnPost: authorId === currentUserId,
|
||||
canComment: true,
|
||||
canMessage: !!authorId && authorId !== currentUserId,
|
||||
engagement: {
|
||||
likesCount,
|
||||
commentsCount,
|
||||
savesCount,
|
||||
shareCount,
|
||||
viewCount,
|
||||
playCount,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private async mixHomeFeedItems(
|
||||
currentUserId: string,
|
||||
posts: FeedPostItem[],
|
||||
suggestionInterval: number,
|
||||
): Promise<Array<FeedPostItem | FeedCardItem>> {
|
||||
const cards = await this.buildHomeCards(currentUserId);
|
||||
if (!cards.length) {
|
||||
return posts;
|
||||
}
|
||||
|
||||
const result: Array<FeedPostItem | FeedCardItem> = [];
|
||||
let cardIndex = 0;
|
||||
|
||||
for (let index = 0; index < posts.length; index += 1) {
|
||||
result.push(posts[index]);
|
||||
if ((index + 1) % suggestionInterval === 0 && cardIndex < cards.length) {
|
||||
result.push(cards[cardIndex]);
|
||||
cardIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
while (cardIndex < cards.length) {
|
||||
result.push(cards[cardIndex]);
|
||||
cardIndex += 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async buildHomeCards(currentUserId: string): Promise<FeedCardItem[]> {
|
||||
const [suggestions, listings, instruments, repairShops] = await Promise.all([
|
||||
this.followsService.getSuggestions(currentUserId, {
|
||||
page: 1,
|
||||
limit: 5,
|
||||
}),
|
||||
this.marketplaceService.getPublicListings({
|
||||
page: 1,
|
||||
limit: 3,
|
||||
isActive: true,
|
||||
} as any),
|
||||
this.marketplaceService.getPublicInstruments({
|
||||
page: 1,
|
||||
limit: 3,
|
||||
isActive: true,
|
||||
} as any),
|
||||
this.marketplaceService.getPublicRepairShops({
|
||||
page: 1,
|
||||
limit: 2,
|
||||
isActive: true,
|
||||
} as any),
|
||||
]);
|
||||
|
||||
const cards: FeedCardItem[] = [];
|
||||
if (Array.isArray(suggestions.items) && suggestions.items.length > 0) {
|
||||
cards.push({
|
||||
id: `suggested-users:${currentUserId}`,
|
||||
feedItemType: 'suggested_users',
|
||||
title: 'Suggested creators',
|
||||
subtitle: 'People you may want to follow',
|
||||
items: suggestions.items.map((entry) => ({
|
||||
...entry,
|
||||
following: false,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
(listings.items?.length ?? 0) > 0 ||
|
||||
(instruments.items?.length ?? 0) > 0 ||
|
||||
(repairShops.items?.length ?? 0) > 0
|
||||
) {
|
||||
cards.push({
|
||||
id: `featured-marketplace:${currentUserId}`,
|
||||
feedItemType: 'featured_marketplace',
|
||||
title: 'Explore marketplace',
|
||||
subtitle: 'Featured listings, musical instruments, and repair shops',
|
||||
listings: (listings.items ?? []) as unknown as Array<Record<string, unknown>>,
|
||||
musicalInstruments: (instruments.items ?? []) as unknown as Array<Record<string, unknown>>,
|
||||
instruments: (instruments.items ?? []) as unknown as Array<Record<string, unknown>>,
|
||||
repairShops: (repairShops.items ?? []) as unknown as Array<Record<string, unknown>>,
|
||||
});
|
||||
}
|
||||
|
||||
return cards;
|
||||
}
|
||||
|
||||
private buildVisiblePostsFilter(
|
||||
currentUserId: string,
|
||||
followingIds: string[],
|
||||
followingOnly: boolean,
|
||||
): Record<string, unknown> {
|
||||
const currentUserObjectId = new Types.ObjectId(currentUserId);
|
||||
const followingObjectIds = followingIds.map((id) => new Types.ObjectId(id));
|
||||
|
||||
if (followingOnly) {
|
||||
return {
|
||||
$or: [
|
||||
{ authorId: currentUserObjectId },
|
||||
{
|
||||
authorId: { $in: followingObjectIds },
|
||||
visibility: { $in: [PostVisibility.PUBLIC, PostVisibility.FOLLOWERS] },
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
$or: [
|
||||
{ visibility: PostVisibility.PUBLIC },
|
||||
{ authorId: currentUserObjectId },
|
||||
{
|
||||
authorId: { $in: followingObjectIds },
|
||||
visibility: PostVisibility.FOLLOWERS,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -113,13 +399,13 @@ export class FeedService {
|
||||
currentUser: UserDocument;
|
||||
currentUserId: string;
|
||||
followingIds: string[];
|
||||
post: any;
|
||||
post: Record<string, any>;
|
||||
preferredPostType?: PostType;
|
||||
radiusKm: number;
|
||||
}): number {
|
||||
const { currentUser, currentUserId, followingIds, post, preferredPostType, radiusKm } = input;
|
||||
const author: any = post.authorId;
|
||||
const authorId = typeof author === 'string' ? author : author?._id?.toString?.() ?? '';
|
||||
const author = post.authorId;
|
||||
const authorId = this.extractEntityId(author);
|
||||
const isOwnPost = authorId === currentUserId;
|
||||
const isFollowing = followingIds.includes(authorId);
|
||||
|
||||
@@ -127,10 +413,16 @@ export class FeedService {
|
||||
const ageHours = ageMs / (1000 * 60 * 60);
|
||||
const freshness = Math.max(0, 36 - ageHours);
|
||||
|
||||
const engagement = post.likesCount * 3 + post.commentsCount * 4 + post.savesCount * 5;
|
||||
const engagement =
|
||||
Number(post.likesCount ?? 0) * 3 +
|
||||
Number(post.commentsCount ?? 0) * 4 +
|
||||
Number(post.savesCount ?? 0) * 5 +
|
||||
Number(post.shareCount ?? 0) * 6 +
|
||||
Number(post.viewCount ?? 0) * 0.15 +
|
||||
Number(post.playCount ?? 0) * 0.25;
|
||||
const hashtagMatches = this.intersectionCount(
|
||||
this.buildPreferenceTokens(currentUser),
|
||||
(post.hashtags ?? []).map((x: string) => x.toLowerCase()),
|
||||
(post.hashtags ?? []).map((value: string) => value.toLowerCase()),
|
||||
);
|
||||
|
||||
const distanceKm = this.computeDistanceKm(
|
||||
@@ -209,4 +501,45 @@ export class FeedService {
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return earthKm * c;
|
||||
}
|
||||
|
||||
private buildCacheKey(scope: string, input: Record<string, unknown>): string {
|
||||
return `feed:${scope}:${JSON.stringify(input)}`;
|
||||
}
|
||||
|
||||
private shouldIncludeSuggestions(query: FeedQueryDto): boolean {
|
||||
return (
|
||||
query.includeSuggestions === true &&
|
||||
!(query.cursor ?? '').trim() &&
|
||||
(query.page ?? 1) === 1
|
||||
);
|
||||
}
|
||||
|
||||
private extractEntityId(value: unknown): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value instanceof Types.ObjectId) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
const candidate = value as { _id?: unknown; id?: unknown };
|
||||
if (candidate._id instanceof Types.ObjectId) {
|
||||
return candidate._id.toString();
|
||||
}
|
||||
if (typeof candidate._id === 'string') {
|
||||
return candidate._id;
|
||||
}
|
||||
if (typeof candidate.id === 'string') {
|
||||
return candidate.id;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم