Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f2ee61

Browse files
committed
Fix the bug in who to follow list
1 parent 259094c commit 6f2ee61

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

src/adapters/fauna/shell.mjs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,49 @@ const client = new faunadb.Client({
99
})
1010

1111
async function main() {
12+
const userId = '290862832026649088'
13+
const allUsers = q.Filter(q.Documents(q.Collection('users')), (ref) =>
14+
q.Not(q.Equals(q.Select(['ref', 'id'], q.Get(ref)), userId))
15+
)
16+
17+
const isFollowing = (followingId) => {
18+
const followerId = userId
19+
return q.Let(
20+
{
21+
ref: q.Match(q.Index('unique_user_and_follower'), [
22+
q.Ref(q.Collection('users'), followingId),
23+
q.Ref(q.Collection('users'), followerId),
24+
]),
25+
},
26+
q.If(
27+
q.Exists(q.Var('ref')),
28+
q.Select(['data', 'isFollowing'], q.Get(q.Var('ref'))),
29+
false
30+
)
31+
)
32+
}
33+
34+
const whoToFollow = q.Paginate(
35+
q.Filter(allUsers, (ref) =>
36+
q.Not(isFollowing(q.Select(['ref', 'id'], q.Get(ref))))
37+
)
38+
)
39+
1240
const response = await client.query(
13-
q.CreateIndex({
14-
name: 'followers_by_user',
15-
source: q.Collection('user_followers'),
16-
unique: false,
17-
terms: [
18-
{
19-
field: ['data', 'user'],
41+
q.Map(whoToFollow, (ref) => {
42+
const doc = q.Get(ref)
43+
return {
44+
id: q.Select(['ref', 'id'], doc),
45+
name: q.Select(['data', 'name'], doc, null),
46+
image: q.Select(['data', 'image'], doc, null),
47+
username: q.Select(['data', 'username'], doc, null),
48+
account: {
49+
firstName: q.Select(['data', 'account', 'firstName'], doc, null),
2050
},
21-
],
51+
}
2252
})
2353
)
24-
console.log({ response: JSON.stringify({ response }) })
54+
console.log({ response: JSON.stringify(response, null, 2) })
2555
}
2656

2757
main().catch((e) => console.error(e))

src/components/HomePageFeed.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ function HomePageAside({ updates }: { updates: HomePageFeedUpdateType[] }) {
429429
<ul className="-my-4 divide-y divide-gray-200">
430430
{!isLoading &&
431431
!isError &&
432-
response.users.slice(0, 3).map((user: User) => (
432+
response.users.map((user: User) => (
433433
<li
434434
className="flex items-center py-4 space-x-3"
435435
key={user.id}

src/pages/api/fauna/who-to-follow.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ const client = new faunadb.Client({
1212
...(isProduction ? {} : { port: 8443 }),
1313
})
1414

15+
const isFollowing = ({
16+
followerId,
17+
followingId,
18+
}: {
19+
followerId: string
20+
followingId: string
21+
}) => {
22+
return q.Let(
23+
{
24+
ref: q.Match(q.Index('unique_user_and_follower'), [
25+
q.Ref(q.Collection('users'), followingId),
26+
q.Ref(q.Collection('users'), followerId),
27+
]),
28+
},
29+
q.If(
30+
q.Exists(q.Var('ref')),
31+
q.Select(['data', 'isFollowing'], q.Get(q.Var('ref'))),
32+
false
33+
)
34+
)
35+
}
36+
1537
const FaunaCreateHandler: NextApiHandler = async (
1638
req: NextApiRequest,
1739
res: NextApiResponse
@@ -25,8 +47,17 @@ const FaunaCreateHandler: NextApiHandler = async (
2547
const userId = (session.user as User).id
2648

2749
try {
50+
const allUsers = q.Filter(q.Documents(q.Collection('users')), (ref) =>
51+
q.Not(q.Equals(q.Select(['ref', 'id'], q.Get(ref)), userId))
52+
)
53+
const whoToFollow = q.Filter(allUsers, (ref) => {
54+
const followerId = userId
55+
const followingId = q.Select(['ref', 'id'], q.Get(ref)) as string
56+
return q.Not(isFollowing({ followerId, followingId }))
57+
})
58+
2859
const response: any = await client.query(
29-
q.Map(q.Paginate(q.Documents(q.Collection('users'))), (userRef) => {
60+
q.Map(q.Paginate(whoToFollow, { size: 3 }), (userRef) => {
3061
const userDoc = q.Get(userRef)
3162
return {
3263
id: q.Select(['ref', 'id'], userDoc),

0 commit comments

Comments
 (0)