-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Email to Sign / In and Sign Up Page, Settings Page, User can delete all Messages #232
Changes from all commits
5fc3802
888d2c1
344d475
f2bc7eb
90d9a49
e14987e
681136b
5376643
4bd0c3e
ef498bf
54fbcb5
415a4b9
7cecf67
69d4f4d
7d4b05d
a9a8690
82b63a6
10f1d1c
5db507f
091b23b
465f481
d7b020d
ebe38ca
7ca5f63
a8eaff9
6aa8461
59c9b7c
53da4ef
dd764f7
f9bbaa1
411310b
d3a0067
98be76d
52476a6
c43a2ac
39e3aa4
dc28a31
2a1bb35
d9963fe
9a7bbb8
d26d587
3756ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,56 @@ export const getMessages = query({ | |
}, | ||
}); | ||
|
||
export const createDeleteRequest = mutation({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the mutation for creating a delete all messages request |
||
args: { chatId: v.string() }, | ||
handler: async (ctx, args) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
|
||
if (identity === null) { | ||
console.error("Unauthenticated call to mutation"); | ||
return null; | ||
} | ||
|
||
const convexUser = await ctx | ||
.table("users") | ||
.get("clerkId", identity.tokenIdentifier); | ||
|
||
const parsedChatId = ctx.table("privateChats").normalizeId(args.chatId); | ||
|
||
if (!parsedChatId) { | ||
throw new ConvexError("chatId was invalid"); | ||
} | ||
|
||
if (!convexUser) { | ||
throw new ConvexError( | ||
"Mismatch between Clerk and Convex. This is an error by us.", | ||
); | ||
} | ||
|
||
const usersInChat = await ctx | ||
.table("privateChats") | ||
.getX(parsedChatId) | ||
.edge("users"); | ||
|
||
if ( | ||
!usersInChat.some((user) => user.clerkId === identity.tokenIdentifier) | ||
) { | ||
throw new ConvexError( | ||
"UNAUTHORIZED REQUEST: User tried to send a message in a chat in which he is not in.", | ||
); | ||
} | ||
|
||
await ctx.table("messages").insert({ | ||
userId: convexUser._id, | ||
privateChatId: parsedChatId, | ||
content: "", | ||
type: "request", | ||
deleted: false, | ||
readBy: [convexUser._id], | ||
}); | ||
}, | ||
}); | ||
|
||
export const createMessage = mutation({ | ||
args: { chatId: v.string(), content: v.string() }, | ||
handler: async (ctx, args) => { | ||
|
@@ -84,12 +134,62 @@ export const createMessage = mutation({ | |
userId: convexUser._id, | ||
privateChatId: parsedChatId, | ||
content: args.content.trim(), | ||
type: "message", | ||
deleted: false, | ||
readBy: [convexUser._id], | ||
}); | ||
}, | ||
}); | ||
|
||
export const deleteAllMessagesInChat = mutation({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the mutation for delete all messages |
||
args: { chatId: v.string() }, | ||
handler: async (ctx, args) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
|
||
if (identity === null) { | ||
console.error("Unauthenticated call to mutation"); | ||
return null; | ||
} | ||
|
||
const parsedChatId = ctx.table("privateChats").normalizeId(args.chatId); | ||
|
||
if (!parsedChatId) { | ||
throw new ConvexError("chatId was invalid"); | ||
} | ||
|
||
const chat = ctx.table("privateChats").getX(parsedChatId); | ||
const messagesInChat = await chat.edge("messages"); | ||
|
||
for (const message of messagesInChat) { | ||
await message.delete(); | ||
} | ||
}, | ||
}); | ||
|
||
export const rejectRequest = mutation({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reject request mutation |
||
args: { messageId: v.string(), chatId: v.string() }, | ||
handler: async (ctx, args) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
|
||
if (identity === null) { | ||
console.error("Unauthenticated call to mutation"); | ||
return null; | ||
} | ||
|
||
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId); | ||
|
||
if (!parsedMessageId) { | ||
throw new ConvexError("chatId was invalid"); | ||
} | ||
|
||
const message = await ctx.table("messages").getX(parsedMessageId); | ||
|
||
await message.patch({ | ||
type: "rejected", | ||
}); | ||
}, | ||
}); | ||
|
||
export const deleteMessage = mutation({ | ||
args: { messageId: v.string(), chatId: v.string() }, | ||
handler: async (ctx, args) => { | ||
|
@@ -145,6 +245,12 @@ export const markMessageRead = mutation({ | |
); | ||
} | ||
|
||
const message = await ctx.table("messages").get(args.messageId); | ||
|
||
if (!message) { | ||
return null; | ||
} | ||
|
||
await ctx | ||
.table("messages") | ||
.getX(args.messageId) | ||
|
@@ -153,5 +259,7 @@ export const markMessageRead = mutation({ | |
add: [convexUser._id], | ||
}, | ||
}); | ||
|
||
return { success: true }; | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ const schema = defineEntSchema({ | |
.field("clerkId", v.string(), { unique: true }) | ||
.field("username", v.string(), { unique: true }) | ||
.field("firstName", v.optional(v.string())) | ||
.field("email", v.optional(v.string())) | ||
.field("lastName", v.optional(v.string())) | ||
.edges("privateChats") | ||
.edges("messages", { ref: true }) | ||
|
@@ -22,6 +23,7 @@ const schema = defineEntSchema({ | |
|
||
messages: defineEnt({}) | ||
.field("content", v.string()) | ||
.field("type", v.string(), { default: "message" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the type of a message. |
||
.field("deleted", v.boolean(), { default: false }) | ||
.edge("privateChat") | ||
.edge("user") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { query } from "./lib/functions"; | ||
import { ConvexError } from "convex/values"; | ||
import { mutation, query } from "./lib/functions"; | ||
import { v } from "convex/values"; | ||
|
||
export const getUserData = query({ | ||
handler: async (ctx) => { | ||
|
@@ -13,3 +13,41 @@ export const getUserData = query({ | |
return ctx.table("users").getX("clerkId", identity.tokenIdentifier); | ||
}, | ||
}); | ||
|
||
export const updateUserData = mutation({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I update the user data (firstname, lastname, email) |
||
args: { | ||
data: v.object({ | ||
firstName: v.optional(v.string()), | ||
lastName: v.optional(v.string()), | ||
email: v.optional(v.string()), | ||
}), | ||
}, | ||
handler: async (ctx, args) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
|
||
if (identity === null) { | ||
console.error("Unauthenticated call to mutation"); | ||
return null; | ||
} | ||
|
||
const user = ctx.table("users").getX("clerkId", identity.tokenIdentifier); | ||
|
||
if (args.data.email) { | ||
await user.patch({ | ||
email: args.data.email, | ||
}); | ||
} | ||
|
||
if (args.data.lastName) { | ||
await user.patch({ | ||
lastName: args.data.lastName, | ||
}); | ||
} | ||
|
||
if (args.data.firstName) { | ||
await user.patch({ | ||
firstName: args.data.firstName, | ||
}); | ||
} | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ export default function Page({ params }: { params: { chatId: string } }) { | |
_creationTime: now, | ||
content, | ||
deleted: false, | ||
type: "message", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the normal type of any message that is not a request |
||
privateChatId: chatId, | ||
from: userInfo.data, | ||
readBy: [userInfo.data], | ||
|
@@ -203,6 +204,13 @@ export default function Page({ params }: { params: { chatId: string } }) { | |
setInputValue(""); | ||
scrollToBottom(true); | ||
} | ||
|
||
const createDeleteRequest = useMutation(api.messages.createDeleteRequest); | ||
|
||
const createMessageRequestHandler = (chatId: string) => async () => { | ||
await createDeleteRequest({ chatId }); | ||
}; | ||
|
||
const [menuActive, setMenuActive] = useState(false); | ||
|
||
const menuClick = () => { | ||
|
@@ -234,7 +242,10 @@ export default function Page({ params }: { params: { chatId: string } }) { | |
className="relative flex flex-col" | ||
> | ||
<DevMode className="top-20 z-10"> | ||
chatId: {params.chatId} | ||
<button onClick={createMessageRequestHandler(params.chatId)}> | ||
Delete Chat Request | ||
</button> | ||
<p>chatId: {params.chatId}</p> | ||
<div onClick={() => devMode$.set(false)}>Disable dev mode</div> | ||
</DevMode> | ||
<div className="flex h-20 w-full items-center justify-between bg-primary py-6"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const ChatsPage = () => { | ||
return <div className="ml-24">Chats</div>; | ||
}; | ||
|
||
export default ChatsPage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const NotificationPage = () => { | ||
return <div className="ml-24">Notification</div>; | ||
}; | ||
|
||
export default NotificationPage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set the value of the emailfield