Skip to content

Commit fe1b762

Browse files
committed
Add function to retrieve
history of posts by author
1 parent 24c2ddb commit fe1b762

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lib/prisma/feed.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ const getHistory = async ({ id }: { id: string | undefined }) => {
4747

4848
return { history: JSON.parse(JSON.stringify(history)) };
4949
}
50+
const getHistoryAuthorPost = async ({ id }: { id: string | undefined }) => {
51+
const historyAuthor = await postgres.readingHistory.findMany({
52+
where: { userId: id },
53+
select: {
54+
post: {
55+
select: {
56+
authorId: true,
57+
}
58+
},
59+
},
60+
orderBy: {
61+
createdAt: "desc",
62+
},
63+
take: 10,
64+
});
65+
66+
const history = await postgres.post.findMany({
67+
where: { authorId: { in: historyAuthor.map((history) => history.post.authorId) } },
68+
select: {
69+
id: true,
70+
},
71+
orderBy: {
72+
createdAt: "desc",
73+
},
74+
take: 10,
75+
});
76+
77+
return { history: JSON.parse(JSON.stringify(history)) };
78+
}
5079
const getFollowingsUsers = async ({ id }: { id: string | undefined }) => {
5180
const { followings: sessionFollowingsArray } = await getFollowings({ id });
5281
console.log(sessionFollowingsArray);
@@ -197,8 +226,10 @@ const sortedTagIds = Object.entries(tagCounts)
197226
.sort((a, b) => b[1] - a[1])
198227
.map(([tagId]) => tagId)
199228

229+
const { history: historyAuthor } = await getHistoryAuthorPost({id});
230+
200231
const posts = await postgres.post.findMany({
201-
where: { tags: { some: { tagId: { in: sortedTagIds } } } },
232+
where: { tags: { some: { tagId: { in: sortedTagIds } } }, id: { in: historyAuthor.map((post: any) => post.id) } },
202233
select: { id: true },
203234
});
204235

0 commit comments

Comments
 (0)