@@ -3,21 +3,19 @@ import faunadb from 'faunadb'
3
3
import { User } from 'src/pages/members'
4
4
const q = faunadb . query
5
5
6
- export function getUserFromUserRef ( {
7
- ref,
6
+ function isFollowingUser ( {
8
7
session,
8
+ userRef,
9
9
} : {
10
- ref : Expr
11
10
session : any
11
+ userRef : Expr
12
12
} ) {
13
- const userDoc = q . Get ( ref )
14
- const userId = q . Select ( [ 'ref' , 'id' ] , userDoc )
15
13
let isFollowing = false
16
14
17
15
if ( session ) {
18
16
const followerId = ( session . user as User ) . id
19
17
const ref = q . Match ( q . Index ( 'unique_user_and_follower' ) , [
20
- q . Ref ( q . Collection ( 'users' ) , userId ) ,
18
+ userRef ,
21
19
q . Ref ( q . Collection ( 'users' ) , followerId ) ,
22
20
] )
23
21
isFollowing = q . If (
@@ -26,6 +24,20 @@ export function getUserFromUserRef({
26
24
false
27
25
) as boolean
28
26
}
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
+
29
41
return {
30
42
id : userId ,
31
43
name : q . Select ( [ 'data' , 'name' ] , userDoc , null ) ,
@@ -40,27 +52,18 @@ export function getUserFromUserRef({
40
52
}
41
53
}
42
54
43
- export function getUpdateFromUpdateRef ( {
44
- ref : goalUpdateRef ,
55
+ function hasLikedUpdate ( {
45
56
session,
57
+ goalUpdateRef,
46
58
} : {
47
- ref : Expr
48
59
session : any
60
+ goalUpdateRef : Expr
49
61
} ) {
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 )
59
62
let hasLiked = false
60
63
if ( session ) {
61
64
const userId = ( session . user as User ) . id
62
65
const ref = q . Match ( q . Index ( 'unique_update_user_like' ) , [
63
- q . Ref ( q . Collection ( 'goal_updates' ) , updateId ) ,
66
+ goalUpdateRef ,
64
67
q . Ref ( q . Collection ( 'users' ) , userId ) ,
65
68
] )
66
69
hasLiked = q . If (
@@ -69,48 +72,118 @@ export function getUpdateFromUpdateRef({
69
72
false
70
73
) as boolean
71
74
}
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 } )
72
172
return {
73
173
id : updateId ,
74
174
goal : {
75
175
id : q . Select ( [ 'ref' , 'id' ] , goalDoc ) ,
76
176
title : q . Select ( [ 'data' , 'title' ] , goalDoc ) ,
77
177
} ,
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 } ) ,
84
179
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 } ) ,
99
181
description,
100
182
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
+ } ) ,
114
187
}
115
188
}
116
189
@@ -123,53 +196,19 @@ export function getCommentFromCommentRef({
123
196
} ) {
124
197
const commentDoc = q . Get ( commentRef )
125
198
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 } )
140
200
return {
141
- id : commentId ,
201
+ id : q . Select ( [ 'ref' , 'id' ] , commentDoc ) ,
142
202
updateId : q . Select ( [ 'data' , 'update' , 'id' ] , commentDoc ) ,
143
203
description : q . Select ( [ 'data' , 'description' ] , commentDoc ) ,
144
204
createdAt : q . ToMillis (
145
205
q . Select ( [ 'data' , 'timestamps' , 'createdAt' ] , commentDoc )
146
206
) ,
147
207
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
+ } ) ,
174
213
}
175
214
}
0 commit comments