Skip to content

Commit

Permalink
fixing a bug where notifications were sent to all users instead of ju…
Browse files Browse the repository at this point in the history
…st the one who has to receive them
  • Loading branch information
DonKoko committed Jul 24, 2023
1 parent ccb9107 commit 931414c
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/atoms/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { User } from "@prisma/client";
import { atom } from "jotai";
import type { Icon } from "~/components/shared/icons-map";

Expand All @@ -13,6 +14,7 @@ export interface NotificationType {
className?: string;
};
time?: number;
senderId: User["id"] | null;
}

export const notificationAtom = atom<NotificationType>({
Expand All @@ -24,6 +26,7 @@ export const notificationAtom = atom<NotificationType>({
variant: "gray",
className: "",
},
senderId: null,
});

/** Opens the Toast and shows the notification */
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Toaster = () => {
};

/** New notification coming from the server */
const newNotification = useEventSource("/api/sse/notification", {
const newNotification = useEventSource(`/api/sse/notification`, {
event: "new-notification",
});
/** When the stream sends us a new notification update the state so it displays */
Expand Down
2 changes: 2 additions & 0 deletions app/routes/_layout+/assets.$assetId.note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const action = async ({ request, params }: ActionArgs) => {
title: "Note created",
message: "Your note has been created successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});
const note = await createNote({
...result.data,
Expand Down Expand Up @@ -68,6 +69,7 @@ export const action = async ({ request, params }: ActionArgs) => {
title: "Note deleted",
message: "Your note has been deleted successfully",
icon: { name: "trash", variant: "error" },
senderId: authSession.userId,
});

const deleted = await deleteNote({
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/assets.$assetId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function action({ request, params }: ActionArgs) {
title: "Asset deleted",
message: "Your asset has been deleted successfully",
icon: { name: "trash", variant: "error" },
senderId: authSession.userId,
});

return redirect(`/assets`, {
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/assets.$assetId_.edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export async function action({ request, params }: ActionArgs) {
title: "Asset updated",
message: "Your asset has been updated successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});

return json(
Expand Down
4 changes: 2 additions & 2 deletions app/routes/_layout+/assets.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export async function action({ request }: LoaderArgs) {
title: "Asset created",
message: "Your asset has been created successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});


if (asset.location) {
await createNote({
content: `**${asset.user.firstName} ${asset.user.lastName}** set the location of **${asset.title}** to **${asset.location.name}**`,
Expand All @@ -110,7 +110,7 @@ export async function action({ request }: LoaderArgs) {
assetId: asset.id,
});
}

return redirect(`/assets`, {
headers: {
"Set-Cookie": await commitAuthSession(request, { authSession }),
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/categories.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function action({ request }: LoaderArgs) {
title: "Category created",
message: "Your category has been created successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});

return redirect(`/categories`, {
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function action({ request }: ActionArgs) {
title: "Category deleted",
message: "Your category has been deleted successfully",
icon: { name: "trash", variant: "error" },
senderId: userId,
});

return json({ success: true });
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/locations.$locationId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export async function action({ request, params }: ActionArgs) {
title: "Location deleted",
message: "Your location has been deleted successfully",
icon: { name: "trash", variant: "error" },
senderId: authSession.userId,
});

return redirect(`/locations`, {
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/locations.$locationId_.edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export async function action({ request, params }: ActionArgs) {
title: "Location updated",
message: "Your location has been updated successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});

return json(
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/locations.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export async function action({ request }: ActionArgs) {
title: "Location created",
message: "Your location has been created successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});

return redirect(`/locations/${location.id}`, {
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/settings.user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export async function action({ request }: ActionArgs) {
title: "User updated",
message: "Your settings have been updated successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});
return json(
{ success: true },
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/tags.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function action({ request }: LoaderArgs) {
title: "Tag created",
message: "Your tag has been created successfully",
icon: { name: "success", variant: "success" },
senderId: authSession.userId,
});

return redirect(`/tags`, {
Expand Down
1 change: 1 addition & 0 deletions app/routes/_layout+/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function action({ request }: ActionArgs) {
title: "Tag deleted",
message: "Your tag has been deleted successfully",
icon: { name: "trash", variant: "error" },
senderId: userId,
});

return json({ success: true });
Expand Down
6 changes: 5 additions & 1 deletion app/routes/api+/sse.notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import { requireAuthSession } from "~/modules/auth";
import { emitter } from "~/utils/emitter/emitter.server";

export async function loader({ request }: LoaderArgs) {
await requireAuthSession(request);
const authSession = await requireAuthSession(request);

return eventStream(request.signal, function setup(send) {
/** Notification is a strigified json object with the shape {@link Notification} */
function handle(notification: string) {
/** We only send the notification if the logged in userId is the same as the senderId.
* We do this to prevent other users receiving notifications
*/
if (authSession.userId !== JSON.parse(notification).senderId) return;
send({ event: "new-notification", data: notification });
}
emitter.on("notification", handle);
Expand Down
1 change: 1 addition & 0 deletions app/utils/emitter/send-notification.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NotificationType } from "~/atoms/notifications";
import { emitter } from "./emitter.server";
// import { useUserData } from "~/hooks";

export function sendNotification(
notification: Omit<NotificationType, "open">
Expand Down

0 comments on commit 931414c

Please sign in to comment.