-
Notifications
You must be signed in to change notification settings - Fork 10
/
fauna.ts
214 lines (203 loc) · 5 KB
/
fauna.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Expr } from 'faunadb'
import faunadb from 'faunadb'
import { User } from 'src/pages/members'
const q = faunadb.query
function isFollowingUser({
session,
userRef,
}: {
session: any
userRef: Expr
}) {
let isFollowing = false
if (session) {
const followerId = (session.user as User).id
const ref = q.Match(q.Index('unique_user_and_follower'), [
userRef,
q.Ref(q.Collection('users'), followerId),
])
isFollowing = q.If(
q.Exists(ref),
q.Select(['data', 'isFollowing'], q.Get(ref)),
false
) as boolean
}
return isFollowing
}
export function getUserFromUserRef({
ref,
session,
}: {
ref: Expr
session: any
}) {
const userDoc = q.Get(ref)
const userId = q.Select(['ref', 'id'], userDoc)
const isFollowing = isFollowingUser({ session, userRef: ref })
return {
id: userId,
name: q.Select(['data', 'name'], userDoc, null),
image: q.Select(['data', 'image'], userDoc, null),
username: q.Select(['data', 'username'], userDoc, null),
account: {
firstName: q.Select(['data', 'account', 'firstName'], userDoc, null),
bio: q.Select(['data', 'account', 'bio'], userDoc, null),
},
socials: q.Select(['data', 'socials'], userDoc, null),
isFollowing,
}
}
function hasLikedUpdate({
session,
goalUpdateRef,
}: {
session: any
goalUpdateRef: Expr
}) {
let hasLiked = false
if (session) {
const userId = (session.user as User).id
const ref = q.Match(q.Index('unique_update_user_like'), [
goalUpdateRef,
q.Ref(q.Collection('users'), userId),
])
hasLiked = q.If(
q.Exists(ref),
q.Select(['data', 'liked'], q.Get(ref)),
false
) as boolean
}
return hasLiked
}
function hasLikedComment({
commentRef,
session,
}: {
commentRef: Expr
session: any
}) {
let hasLiked = false
if (session) {
const userId = (session.user as User).id
const ref = q.Match(q.Index('unique_comment_user_like'), [
commentRef,
q.Ref(q.Collection('users'), userId),
])
hasLiked = q.If(
q.Exists(ref),
q.Select(['data', 'liked'], q.Get(ref)),
false
) as boolean
}
return hasLiked
}
function getAllUsersWhoLikedUpdate({
goalUpdateRef,
session,
}: {
goalUpdateRef: Expr
session: any
}) {
return q.Map(
q.Filter(
q.Paginate(q.Match(q.Index('all_likes_by_update'), goalUpdateRef)),
(updateLikeRef) => q.Select(['data', 'liked'], q.Get(updateLikeRef))
),
(likeRef) =>
getUserFromUserRef({
ref: q.Select(['data', 'user'], q.Get(likeRef)),
session,
})
)
}
function getAllUsersWhoLikedComment({
commentRef,
session,
}: {
commentRef: Expr
session
}) {
return q.Map(
q.Filter(
q.Paginate(q.Match(q.Index('all_likes_by_comment'), commentRef)),
(commentLikeRef) => {
return q.Select(['data', 'liked'], q.Get(commentLikeRef))
}
),
(likeRef) =>
getUserFromUserRef({
ref: q.Select(['data', 'user'], q.Get(likeRef)),
session,
})
)
}
function getAllCommentsOfUpdate({
goalUpdateRef,
session,
}: {
goalUpdateRef: Expr
session: any
}) {
return q.Map(
q.Paginate(q.Match(q.Index('all_comments_by_update'), goalUpdateRef)),
(commentRef) => getCommentFromCommentRef({ ref: commentRef, session })
)
}
export function getUpdateFromUpdateRef({
ref: goalUpdateRef,
session,
}: {
ref: Expr
session: any
}) {
const goalUpdateDoc = q.Get(goalUpdateRef)
const goalDoc = q.Get(q.Select(['data', 'goal'], goalUpdateDoc))
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], goalUpdateDoc))
const description = q.Select(['data', 'description'], goalUpdateDoc)
const createdAt = q.ToMillis(
q.Select(['data', 'timestamps', 'createdAt'], goalUpdateDoc)
)
const updateId = q.Select(['ref', 'id'], goalUpdateDoc)
const hasLiked = hasLikedUpdate({ goalUpdateRef, session })
return {
id: updateId,
goal: {
id: q.Select(['ref', 'id'], goalDoc),
title: q.Select(['data', 'title'], goalDoc),
},
comments: getAllCommentsOfUpdate({ goalUpdateRef, session }),
hasLiked,
likes: getAllUsersWhoLikedUpdate({ goalUpdateRef, session }),
description,
createdAt,
postedBy: getUserFromUserRef({
ref: q.Select(['ref'], postedByDoc),
session,
}),
}
}
export function getCommentFromCommentRef({
ref: commentRef,
session,
}: {
ref: Expr
session: any
}) {
const commentDoc = q.Get(commentRef)
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], commentDoc))
const hasLiked = hasLikedComment({ commentRef, session })
return {
id: q.Select(['ref', 'id'], commentDoc),
updateId: q.Select(['data', 'update', 'id'], commentDoc),
description: q.Select(['data', 'description'], commentDoc),
createdAt: q.ToMillis(
q.Select(['data', 'timestamps', 'createdAt'], commentDoc)
),
hasLiked,
likes: getAllUsersWhoLikedComment({ commentRef, session }),
postedBy: getUserFromUserRef({
ref: q.Select(['ref'], postedByDoc),
session,
}),
}
}