feat(explore): add trending tab with hot posts and hashtags
- explore.getTrending: top-12 photo posts by reaction count (48h window) and top-10 hashtags by post count (7d window) - Explore page opens on Trending tab by default; species/breed tabs preserved with breed filter hidden when not applicable - Trending UI: hashtag chips linking to /hashtag/[tag] + 3-col post grid with paw-count overlay and PostCard detail dialog feat(follows): auto-trigger follower milestone posts - After each follow, count followers for followeePetId and check against all thresholds (500 / 1K / 2.5K / 5K / 10K / 25K / 50K / 100K / 250K / 500K / 1M) - Guard against duplicate awards via existing Milestone lookup - Fire-and-forget: milestone post created + fanned out to follower feeds without blocking the follow response
This commit is contained in:
1 parent
38952b1563
commit
e407064866
3 files changed
+313
-54
No files matched your search
@@ -2,6 +2,9 @@ import "server-only";
|
||||
import { z } from "zod";
|
||||
import { router, publicProcedure } from "@/trpc/init";
|
||||
|
||||
const TRENDING_POSTS_WINDOW_MS = 48 * 60 * 60 * 1000; // 48 h
|
||||
const TRENDING_HASHTAGS_WINDOW_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
||||
|
||||
export const exploreRouter = router({
|
||||
listPets: publicProcedure
|
||||
.input(
|
||||
@@ -35,4 +38,38 @@ export const exploreRouter = router({
|
||||
nextCursor: hasNextPage ? items[items.length - 1]!.id : null,
|
||||
};
|
||||
}),
|
||||
|
||||
/**
|
||||
* getTrending — trending posts (48 h window, ranked by reaction count)
|
||||
* and trending hashtags (7 day window, ranked by post count).
|
||||
*/
|
||||
getTrending: publicProcedure.query(async ({ ctx }) => {
|
||||
const now = Date.now();
|
||||
const since48h = new Date(now - TRENDING_POSTS_WINDOW_MS);
|
||||
const since7d = new Date(now - TRENDING_HASHTAGS_WINDOW_MS);
|
||||
|
||||
const [posts, hashtags] = await Promise.all([
|
||||
ctx.prisma.post.findMany({
|
||||
where: { createdAt: { gte: since48h }, type: "PHOTO" },
|
||||
include: {
|
||||
images: { orderBy: { position: "asc" } },
|
||||
pet: { select: { id: true, name: true, avatarKey: true } },
|
||||
milestone: true,
|
||||
_count: { select: { reactions: true, comments: true } },
|
||||
},
|
||||
orderBy: { reactions: { _count: "desc" } },
|
||||
take: 12,
|
||||
}),
|
||||
ctx.prisma.hashtag.findMany({
|
||||
where: {
|
||||
posts: { some: { post: { createdAt: { gte: since7d } } } },
|
||||
},
|
||||
include: { _count: { select: { posts: true } } },
|
||||
orderBy: { posts: { _count: "desc" } },
|
||||
take: 10,
|
||||
}),
|
||||
]);
|
||||
|
||||
return { posts, hashtags };
|
||||
}),
|
||||
});
|
||||
@@ -1,10 +1,25 @@
|
||||
import "server-only";
|
||||
import { z } from "zod";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { MilestoneType } from "@prisma/client";
|
||||
import { router, protectedProcedure } from "@/trpc/init";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { backfillOnFollow } from "@/lib/feed-helpers";
|
||||
import { backfillOnFollow, fanOutPost } from "@/lib/feed-helpers";
|
||||
|
||||
const FOLLOWER_MILESTONES: Partial<Record<number, MilestoneType>> = {
|
||||
500: MilestoneType.FOLLOWERS_500,
|
||||
1000: MilestoneType.FOLLOWERS_1K,
|
||||
2500: MilestoneType.FOLLOWERS_2_5K,
|
||||
5000: MilestoneType.FOLLOWERS_5K,
|
||||
10000: MilestoneType.FOLLOWERS_10K,
|
||||
25000: MilestoneType.FOLLOWERS_25K,
|
||||
50000: MilestoneType.FOLLOWERS_50K,
|
||||
100000: MilestoneType.FOLLOWERS_100K,
|
||||
250000: MilestoneType.FOLLOWERS_250K,
|
||||
500000: MilestoneType.FOLLOWERS_500K,
|
||||
1000000: MilestoneType.FOLLOWERS_1M,
|
||||
};
|
||||
|
||||
/**
|
||||
* Ownership assertion helper — verifies that a pet belongs to the requesting
|
||||
@@ -76,6 +91,35 @@ export const followsRouter = router({
|
||||
// Backfill followee's recent posts into follower's Redis feed inbox (SOCL-01)
|
||||
await backfillOnFollow(redis, followerPetId, followeePetId, ctx.prisma).catch(() => {});
|
||||
|
||||
// MILE-F: check follower milestone thresholds — fire-and-forget
|
||||
void (async () => {
|
||||
const count = await ctx.prisma.follow.count({ where: { followeePetId } });
|
||||
const milestoneType = FOLLOWER_MILESTONES[count];
|
||||
if (!milestoneType) return;
|
||||
|
||||
// Guard: don't award the same milestone twice
|
||||
const existing = await ctx.prisma.milestone.findFirst({
|
||||
where: { milestoneType, post: { petId: followeePetId } },
|
||||
});
|
||||
if (existing) return;
|
||||
|
||||
const post = await ctx.prisma.post.create({
|
||||
data: {
|
||||
petId: followeePetId,
|
||||
type: "MILESTONE",
|
||||
caption: null,
|
||||
milestone: { create: { milestoneType, notes: null } },
|
||||
},
|
||||
include: {
|
||||
images: { orderBy: { position: "asc" } },
|
||||
milestone: true,
|
||||
pet: { select: { id: true, name: true, avatarKey: true } },
|
||||
},
|
||||
});
|
||||
|
||||
await fanOutPost(redis, post.id, followeePetId, post.createdAt, ctx.prisma);
|
||||
})().catch(() => {});
|
||||
|
||||
// NOTF-01: notify the followee (fire-and-forget — never fails the follow)
|
||||
ctx.prisma.notification
|
||||
.create({
|
||||
|
||||
Reference in new issue
Block a user