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

Skip to content

Commit 4180825

Browse files
committed
Add notifications
1 parent c74086d commit 4180825

22 files changed

+705
-163
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CreateCollection({
2+
name: "activities"
3+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CreateCollection({
2+
name: "notifications"
3+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CreateIndex({
2+
name: "all_notifications_by_user",
3+
source: Collection("notifications"),
4+
terms: [{
5+
field: ["data", "user"]
6+
}]
7+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CreateCollection({
2+
name: 'activities'
3+
})
4+
5+
/**
6+
# "data": {
7+
# // this user performed this activity
8+
# "user": User,
9+
10+
# // This resource has been added/changed as a result of this action
11+
# "resource": User | UPDATE_LIKE | COMMENT_LIKE | UPDATE_COMMENT
12+
13+
# "type": "LIKED_UPDATE" | "LIKED_COMMENT" | "COMMENTED" | "FOLLOWED"
14+
15+
# "timestamps": {
16+
# "createdAt": Time,
17+
# "updatedAt": Time
18+
# }
19+
# }
20+
**/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CreateCollection({
2+
name: 'notifications'
3+
})
4+
5+
/**
6+
# "data": {
7+
# // this user will receive the notification
8+
# "user": User,
9+
# "activity": ACTIVITY
10+
# isRead: Boolean,
11+
# "timestamps": {
12+
# "createdAt": Time,
13+
# "updatedAt": Time
14+
# }
15+
# }
16+
**/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CreateIndex({
2+
name: 'all_notifications_by_user',
3+
source: Collection('notifications'),
4+
terms: [
5+
{
6+
field: ['data', 'user'],
7+
},
8+
],
9+
})

src/adapters/fauna/shell.mjs

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

1111
async function main() {
12-
const response = client.query(q.Do())
12+
const response = await client.query(
13+
q.Do(
14+
q.Map(
15+
q.Paginate(q.Documents(q.Collection('notifications'))),
16+
(goalUpdate) => q.Delete(goalUpdate)
17+
),
18+
q.Map(q.Paginate(q.Documents(q.Collection('activities'))), (goalUpdate) =>
19+
q.Delete(goalUpdate)
20+
)
21+
)
22+
)
1323
console.log(JSON.stringify(response, null, 2))
24+
console.log('THE_END')
1425
}
1526

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

src/components/AppNavBar.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ const navbarItems = [
2424
value: 'our-chatroom',
2525
href: '/chat',
2626
},
27+
{
28+
title: 'Notifications',
29+
value: 'notifications',
30+
href: '/notifications',
31+
},
2732
]
2833

2934
export default function AppNavBar() {

src/components/HomePageFeed.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function HomePageFeedUpdate({
166166
href={`/${postedBy.username}`}
167167
className="hover:underline"
168168
>
169-
{postedBy.name}
169+
{postedBy.account?.firstName}
170170
</A>
171171
</p>
172172
<p className="text-sm text-gray-500">
@@ -300,11 +300,7 @@ export function HomePageFeedUpdate({
300300
<UpdateComments>
301301
<UpdateCommentsList>
302302
{update.comments.data.map((comment, index) => (
303-
<UpdateComment
304-
updateId={update.id}
305-
key={comment.id}
306-
comment={comment}
307-
>
303+
<UpdateComment key={comment.id} comment={comment}>
308304
{comment.description}
309305
</UpdateComment>
310306
))}

src/components/goals/UpdateComment.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ export type GoalUpdateType = {
2323
}
2424

2525
export default function UpdateComment({
26-
updateId,
2726
comment,
2827
children,
2928
}: {
30-
updateId: string
3129
children: string
3230
comment: UpdateCommentType
3331
}) {
@@ -90,7 +88,7 @@ export default function UpdateComment({
9088
<li>
9189
{isInEditMode ? (
9290
<EditComment
93-
updateId={updateId}
91+
updateId={comment.updateId}
9492
comment={comment}
9593
cancelEditMode={() => setIsInEditMode(false)}
9694
/>

src/components/modal/MemberList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default function MembersList({ users }: { users: User[] }) {
8686
<div className="flow-root my-6">
8787
<ul className="-my-5 divide-y divide-gray-200">
8888
{users.map((user) => (
89-
<Member user={user} />
89+
<Member user={user} key={user.id} />
9090
))}
9191
</ul>
9292
</div>

src/pages/api/fauna/add-comment.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const FaunaCreateHandler: NextApiHandler = async (
2525
const userId = (session.user as User).id
2626

2727
try {
28-
const response = await client.query(
28+
const response: any = await client.query(
2929
q.Create(q.Collection('update_comments'), {
3030
data: {
3131
postedBy: q.Ref(q.Collection('users'), userId),
@@ -38,6 +38,41 @@ const FaunaCreateHandler: NextApiHandler = async (
3838
},
3939
})
4040
)
41+
await client.query(
42+
q.Let(
43+
{
44+
activityDoc: q.Create(q.Collection('activities'), {
45+
data: {
46+
user: q.Ref(q.Collection('users'), userId),
47+
resource: q.Ref(q.Collection('update_comments'), response.ref.id),
48+
type: 'COMMENTED_ON_UPDATE',
49+
timestamps: {
50+
createdAt: q.Now(),
51+
updatedAt: q.Now(),
52+
},
53+
},
54+
}),
55+
notificationDoc: q.Create(q.Collection('notifications'), {
56+
data: {
57+
user: q.Select(
58+
['data', 'postedBy'],
59+
q.Get(q.Ref(q.Collection('goal_updates'), updateId))
60+
),
61+
activity: q.Ref(
62+
q.Collection('activities'),
63+
q.Select(['ref', 'id'], q.Var('activityDoc'))
64+
),
65+
isRead: false,
66+
timestamps: {
67+
createdAt: q.Now(),
68+
updatedAt: q.Now(),
69+
},
70+
},
71+
}),
72+
},
73+
{}
74+
)
75+
)
4176
res.status(201).json({ response })
4277
} catch (error) {
4378
console.error(error)

src/pages/api/fauna/all-updates.ts

Lines changed: 2 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
33
import faunadb from 'faunadb'
44
import { getSession } from 'next-auth/client'
55
import { User } from 'src/pages/members'
6-
import { getUserFromUserRef } from 'src/utils/fauna'
6+
import { getUpdateFromUpdateRef, getUserFromUserRef } from 'src/utils/fauna'
77
const q = faunadb.query
88
const isProduction = process.env.NODE_ENV === 'production'
99
const client = new faunadb.Client({
@@ -22,128 +22,7 @@ const FaunaCreateHandler: NextApiHandler = async (
2222
const response: any = await client.query(
2323
q.Map(q.Paginate(q.Match(q.Index('all_recent_updates'))), (result) => {
2424
const goalUpdateRef = q.Select(1, result)
25-
const goalUpdateDoc = q.Get(goalUpdateRef)
26-
const goalDoc = q.Get(q.Select(['data', 'goal'], goalUpdateDoc))
27-
const postedByDoc = q.Get(q.Select(['data', 'postedBy'], goalUpdateDoc))
28-
const description = q.Select(['data', 'description'], goalUpdateDoc)
29-
30-
const createdAt = q.ToMillis(
31-
q.Select(['data', 'timestamps', 'createdAt'], goalUpdateDoc)
32-
)
33-
const updateId = q.Select(['ref', 'id'], goalUpdateDoc)
34-
let hasLiked = false
35-
if (session) {
36-
const userId = (session.user as User).id
37-
const ref = q.Match(q.Index('unique_update_user_like'), [
38-
q.Ref(q.Collection('goal_updates'), updateId),
39-
q.Ref(q.Collection('users'), userId),
40-
])
41-
hasLiked = q.If(
42-
q.Exists(ref),
43-
q.Select(['data', 'liked'], q.Get(ref)),
44-
false
45-
) as boolean
46-
}
47-
return {
48-
id: updateId,
49-
goal: {
50-
id: q.Select(['ref', 'id'], goalDoc),
51-
title: q.Select(['data', 'title'], goalDoc),
52-
},
53-
comments: q.Map(
54-
q.Paginate(
55-
q.Match(q.Index('all_comments_by_update'), goalUpdateRef)
56-
),
57-
(commentRef) => {
58-
const commentDoc = q.Get(commentRef)
59-
const postedByDoc = q.Get(
60-
q.Select(['data', 'postedBy'], commentDoc)
61-
)
62-
let hasLiked = false
63-
const commentId = q.Select(['ref', 'id'], commentDoc)
64-
if (session) {
65-
const userId = (session.user as User).id
66-
const ref = q.Match(q.Index('unique_comment_user_like'), [
67-
q.Ref(q.Collection('update_comments'), commentId),
68-
q.Ref(q.Collection('users'), userId),
69-
])
70-
hasLiked = q.If(
71-
q.Exists(ref),
72-
q.Select(['data', 'liked'], q.Get(ref)),
73-
false
74-
) as boolean
75-
}
76-
return {
77-
id: commentId,
78-
description: q.Select(['data', 'description'], commentDoc),
79-
createdAt: q.ToMillis(
80-
q.Select(['data', 'timestamps', 'createdAt'], commentDoc)
81-
),
82-
hasLiked,
83-
likes: q.Map(
84-
q.Filter(
85-
q.Paginate(
86-
q.Match(q.Index('all_likes_by_comment'), commentRef)
87-
),
88-
(commentLikeRef) => {
89-
return q.Select(['data', 'liked'], q.Get(commentLikeRef))
90-
}
91-
),
92-
(likeRef) => {
93-
const likeDoc = q.Get(likeRef)
94-
const userRef = q.Select(['data', 'user'], likeDoc)
95-
return getUserFromUserRef({ ref: userRef, session })
96-
}
97-
),
98-
postedBy: {
99-
id: q.Select(['ref', 'id'], postedByDoc),
100-
name: q.Select(['data', 'name'], postedByDoc, null),
101-
image: q.Select(['data', 'image'], postedByDoc, null),
102-
username: q.Select(['data', 'username'], postedByDoc, null),
103-
account: {
104-
firstName: q.Select(
105-
['data', 'account', 'firstName'],
106-
postedByDoc,
107-
null
108-
),
109-
},
110-
},
111-
}
112-
}
113-
),
114-
hasLiked,
115-
likes: q.Map(
116-
q.Filter(
117-
q.Paginate(
118-
q.Match(q.Index('all_likes_by_update'), goalUpdateRef)
119-
),
120-
(updateLikeRef) => {
121-
return q.Select(['data', 'liked'], q.Get(updateLikeRef))
122-
}
123-
),
124-
(likeRef) => {
125-
const likeDoc = q.Get(likeRef)
126-
const userRef = q.Select(['data', 'user'], likeDoc)
127-
128-
return getUserFromUserRef({ ref: userRef, session })
129-
}
130-
),
131-
description,
132-
createdAt,
133-
postedBy: {
134-
id: q.Select(['ref', 'id'], postedByDoc),
135-
name: q.Select(['data', 'name'], postedByDoc, null),
136-
image: q.Select(['data', 'image'], postedByDoc, null),
137-
username: q.Select(['data', 'username'], postedByDoc, null),
138-
account: {
139-
firstName: q.Select(
140-
['data', 'account', 'firstName'],
141-
postedByDoc,
142-
null
143-
),
144-
},
145-
},
146-
}
25+
return getUpdateFromUpdateRef({ ref: goalUpdateRef, session })
14726
})
14827
)
14928

0 commit comments

Comments
 (0)