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

Skip to content

Commit 52dc63f

Browse files
committed
Refactor
1 parent 7de077d commit 52dc63f

File tree

1 file changed

+132
-93
lines changed

1 file changed

+132
-93
lines changed

src/utils/fauna.ts

Lines changed: 132 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ import faunadb from 'faunadb'
33
import { User } from 'src/pages/members'
44
const q = faunadb.query
55

6-
export function getUserFromUserRef({
7-
ref,
6+
function isFollowingUser({
87
session,
8+
userRef,
99
}: {
10-
ref: Expr
1110
session: any
11+
userRef: Expr
1212
}) {
13-
const userDoc = q.Get(ref)
14-
const userId = q.Select(['ref', 'id'], userDoc)
1513
let isFollowing = false
1614

1715
if (session) {
1816
const followerId = (session.user as User).id
1917
const ref = q.Match(q.Index('unique_user_and_follower'), [
20-
q.Ref(q.Collection('users'), userId),
18+
userRef,
2119
q.Ref(q.Collection('users'), followerId),
2220
])
2321
isFollowing = q.If(
@@ -26,6 +24,20 @@ export function getUserFromUserRef({
2624
false
2725
) as boolean
2826
}
27+
return isFollowing
28+
}
29+
30+
export function getUserFromUserRef({
31+
ref,
32+
session,
33+
}: {
34+
ref: Expr
35+
session: any
36+
}) {
37+
const userDoc = q.Get(ref)
38+
const userId = q.Select(['ref', 'id'], userDoc)
39+
const isFollowing = isFollowingUser({ session, userRef: ref })
40+
2941
return {
3042
id: userId,
3143
name: q.Select(['data', 'name'], userDoc, null),
@@ -40,27 +52,18 @@ export function getUserFromUserRef({
4052
}
4153
}
4254

43-
export function getUpdateFromUpdateRef({
44-
ref: goalUpdateRef,
55+
function hasLikedUpdate({
4556
session,
57+
goalUpdateRef,
4658
}: {
47-
ref: Expr
4859
session: any
60+
goalUpdateRef: Expr
4961
}) {
50-
const goalUpdateDoc = q.Get(goalUpdateRef)
51-
const goalDoc = q.Get(q.Select(['data', 'goal'], goalUpdateDoc))
52-
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], goalUpdateDoc))
53-
const description = q.Select(['data', 'description'], goalUpdateDoc)
54-
55-
const createdAt = q.ToMillis(
56-
q.Select(['data', 'timestamps', 'createdAt'], goalUpdateDoc)
57-
)
58-
const updateId = q.Select(['ref', 'id'], goalUpdateDoc)
5962
let hasLiked = false
6063
if (session) {
6164
const userId = (session.user as User).id
6265
const ref = q.Match(q.Index('unique_update_user_like'), [
63-
q.Ref(q.Collection('goal_updates'), updateId),
66+
goalUpdateRef,
6467
q.Ref(q.Collection('users'), userId),
6568
])
6669
hasLiked = q.If(
@@ -69,48 +72,118 @@ export function getUpdateFromUpdateRef({
6972
false
7073
) as boolean
7174
}
75+
return hasLiked
76+
}
77+
78+
function hasLikedComment({
79+
commentRef,
80+
session,
81+
}: {
82+
commentRef: Expr
83+
session: any
84+
}) {
85+
let hasLiked = false
86+
if (session) {
87+
const userId = (session.user as User).id
88+
const ref = q.Match(q.Index('unique_comment_user_like'), [
89+
commentRef,
90+
q.Ref(q.Collection('users'), userId),
91+
])
92+
hasLiked = q.If(
93+
q.Exists(ref),
94+
q.Select(['data', 'liked'], q.Get(ref)),
95+
false
96+
) as boolean
97+
}
98+
return hasLiked
99+
}
100+
101+
function getAllUsersWhoLikedUpdate({
102+
goalUpdateRef,
103+
session,
104+
}: {
105+
goalUpdateRef: Expr
106+
session: any
107+
}) {
108+
return q.Map(
109+
q.Filter(
110+
q.Paginate(q.Match(q.Index('all_likes_by_update'), goalUpdateRef)),
111+
(updateLikeRef) => q.Select(['data', 'liked'], q.Get(updateLikeRef))
112+
),
113+
(likeRef) =>
114+
getUserFromUserRef({
115+
ref: q.Select(['data', 'user'], q.Get(likeRef)),
116+
session,
117+
})
118+
)
119+
}
120+
121+
function getAllUsersWhoLikedComment({
122+
commentRef,
123+
session,
124+
}: {
125+
commentRef: Expr
126+
session
127+
}) {
128+
return q.Map(
129+
q.Filter(
130+
q.Paginate(q.Match(q.Index('all_likes_by_comment'), commentRef)),
131+
(commentLikeRef) => {
132+
return q.Select(['data', 'liked'], q.Get(commentLikeRef))
133+
}
134+
),
135+
(likeRef) =>
136+
getUserFromUserRef({
137+
ref: q.Select(['data', 'user'], q.Get(likeRef)),
138+
session,
139+
})
140+
)
141+
}
142+
143+
function getAllCommentsOfUpdate({
144+
goalUpdateRef,
145+
session,
146+
}: {
147+
goalUpdateRef: Expr
148+
session: any
149+
}) {
150+
return q.Map(
151+
q.Paginate(q.Match(q.Index('all_comments_by_update'), goalUpdateRef)),
152+
(commentRef) => getCommentFromCommentRef({ ref: commentRef, session })
153+
)
154+
}
155+
156+
export function getUpdateFromUpdateRef({
157+
ref: goalUpdateRef,
158+
session,
159+
}: {
160+
ref: Expr
161+
session: any
162+
}) {
163+
const goalUpdateDoc = q.Get(goalUpdateRef)
164+
const goalDoc = q.Get(q.Select(['data', 'goal'], goalUpdateDoc))
165+
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], goalUpdateDoc))
166+
const description = q.Select(['data', 'description'], goalUpdateDoc)
167+
const createdAt = q.ToMillis(
168+
q.Select(['data', 'timestamps', 'createdAt'], goalUpdateDoc)
169+
)
170+
const updateId = q.Select(['ref', 'id'], goalUpdateDoc)
171+
const hasLiked = hasLikedUpdate({ goalUpdateRef, session })
72172
return {
73173
id: updateId,
74174
goal: {
75175
id: q.Select(['ref', 'id'], goalDoc),
76176
title: q.Select(['data', 'title'], goalDoc),
77177
},
78-
comments: q.Map(
79-
q.Paginate(q.Match(q.Index('all_comments_by_update'), goalUpdateRef)),
80-
(commentRef) => {
81-
return getCommentFromCommentRef({ ref: commentRef, session })
82-
}
83-
),
178+
comments: getAllCommentsOfUpdate({ goalUpdateRef, session }),
84179
hasLiked,
85-
likes: q.Map(
86-
q.Filter(
87-
q.Paginate(q.Match(q.Index('all_likes_by_update'), goalUpdateRef)),
88-
(updateLikeRef) => {
89-
return q.Select(['data', 'liked'], q.Get(updateLikeRef))
90-
}
91-
),
92-
(likeRef) => {
93-
const likeDoc = q.Get(likeRef)
94-
const userRef = q.Select(['data', 'user'], likeDoc)
95-
96-
return getUserFromUserRef({ ref: userRef, session })
97-
}
98-
),
180+
likes: getAllUsersWhoLikedUpdate({ goalUpdateRef, session }),
99181
description,
100182
createdAt,
101-
postedBy: {
102-
id: q.Select(['ref', 'id'], postedByDoc),
103-
name: q.Select(['data', 'name'], postedByDoc, null),
104-
image: q.Select(['data', 'image'], postedByDoc, null),
105-
username: q.Select(['data', 'username'], postedByDoc, null),
106-
account: {
107-
firstName: q.Select(
108-
['data', 'account', 'firstName'],
109-
postedByDoc,
110-
null
111-
),
112-
},
113-
},
183+
postedBy: getUserFromUserRef({
184+
ref: q.Select(['ref'], postedByDoc),
185+
session,
186+
}),
114187
}
115188
}
116189

@@ -123,53 +196,19 @@ export function getCommentFromCommentRef({
123196
}) {
124197
const commentDoc = q.Get(commentRef)
125198
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], commentDoc))
126-
let hasLiked = false
127-
const commentId = q.Select(['ref', 'id'], commentDoc)
128-
if (session) {
129-
const userId = (session.user as User).id
130-
const ref = q.Match(q.Index('unique_comment_user_like'), [
131-
q.Ref(q.Collection('update_comments'), commentId),
132-
q.Ref(q.Collection('users'), userId),
133-
])
134-
hasLiked = q.If(
135-
q.Exists(ref),
136-
q.Select(['data', 'liked'], q.Get(ref)),
137-
false
138-
) as boolean
139-
}
199+
const hasLiked = hasLikedComment({ commentRef, session })
140200
return {
141-
id: commentId,
201+
id: q.Select(['ref', 'id'], commentDoc),
142202
updateId: q.Select(['data', 'update', 'id'], commentDoc),
143203
description: q.Select(['data', 'description'], commentDoc),
144204
createdAt: q.ToMillis(
145205
q.Select(['data', 'timestamps', 'createdAt'], commentDoc)
146206
),
147207
hasLiked,
148-
likes: q.Map(
149-
q.Filter(
150-
q.Paginate(q.Match(q.Index('all_likes_by_comment'), commentRef)),
151-
(commentLikeRef) => {
152-
return q.Select(['data', 'liked'], q.Get(commentLikeRef))
153-
}
154-
),
155-
(likeRef) => {
156-
const likeDoc = q.Get(likeRef)
157-
const userRef = q.Select(['data', 'user'], likeDoc)
158-
return getUserFromUserRef({ ref: userRef, session })
159-
}
160-
),
161-
postedBy: {
162-
id: q.Select(['ref', 'id'], postedByDoc),
163-
name: q.Select(['data', 'name'], postedByDoc, null),
164-
image: q.Select(['data', 'image'], postedByDoc, null),
165-
username: q.Select(['data', 'username'], postedByDoc, null),
166-
account: {
167-
firstName: q.Select(
168-
['data', 'account', 'firstName'],
169-
postedByDoc,
170-
null
171-
),
172-
},
173-
},
208+
likes: getAllUsersWhoLikedComment({ commentRef, session }),
209+
postedBy: getUserFromUserRef({
210+
ref: q.Select(['ref'], postedByDoc),
211+
session,
212+
}),
174213
}
175214
}

0 commit comments

Comments
 (0)