Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

⚡️ Optimize invite page queries (#2350)

Luke Vellad544a0136d8b
diff --git a/apps/web/src/app/[locale]/invite/[urlId]/invite-page.tsx b/apps/web/src/app/[locale]/invite/[urlId]/invite-page.tsx
index 3f0867136..c9b7571dd 100644
--- a/apps/web/src/app/[locale]/invite/[urlId]/invite-page.tsx
+++ b/apps/web/src/app/[locale]/invite/[urlId]/invite-page.tsx
@@ -8,14 +8,15 @@ import { PollFooter } from "@/components/poll/poll-footer";
import { PollViewTracker } from "@/components/poll/poll-view-tracker";
import { ResponsiveResults } from "@/components/poll/responsive-results";
import { VotingForm } from "@/components/poll/voting-form";
-import { useUser } from "@/components/user-provider";
import { usePoll } from "@/contexts/poll";
import { Trans } from "@/i18n/client";
+import { authClient } from "@/lib/auth-client";
const GoToApp = () => {
const poll = usePoll();
- const { ownsObject } = useUser();
- if (!ownsObject(poll)) {
+ const { data: session } = authClient.useSession();
+
+ if (!session || session.user.id !== poll.userId) {
return null;
}
diff --git a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
index a45dd8bc9..5bddee134 100644
--- a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
+++ b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
@@ -3,40 +3,67 @@ import { absoluteUrl } from "@rallly/utils/absolute-url";
import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
import type { Metadata } from "next";
import { notFound } from "next/navigation";
+import { cache } from "react";
import { PermissionProvider } from "@/contexts/permissions";
import { PreferencesProvider } from "@/contexts/preferences";
import { createPublicSSRHelper } from "@/trpc/server/create-ssr-helper";
+import { decryptToken } from "@/utils/session";
import { InvitePageLoader } from "./invite-page-loader";
import Providers from "./providers";
-export default async function Page(props: {
- params: Promise<{ urlId: string }>;
-}) {
- const params = await props.params;
-
+const getPollMetadata = cache(async (urlId: string) => {
const poll = await prisma.poll.findUnique({
- where: { id: params.urlId },
- select: { deleted: true, user: { select: { banned: true } } },
+ where: { id: urlId },
+ select: {
+ id: true,
+ title: true,
+ deleted: true,
+ user: {
+ select: {
+ name: true,
+ banned: true,
+ },
+ },
+ },
});
if (!poll || poll.deleted || poll.user?.banned) {
notFound();
}
+ return poll;
+});
+
+export default async function Page(props: {
+ params: Promise<{ urlId: string }>;
+ searchParams: Promise<{ token?: string }>;
+}) {
+ const params = await props.params;
+
+ await getPollMetadata(params.urlId);
const trpc = await createPublicSSRHelper();
await Promise.all([
- trpc.user.getMe.prefetch(),
trpc.polls.get.prefetch({ urlId: params.urlId }),
trpc.polls.participants.list.prefetch({ pollId: params.urlId }),
trpc.polls.comments.list.prefetch({ pollId: params.urlId }),
]);
+ const token = (await props.searchParams).token;
+
+ let impersonatedUserId: string | null = null;
+ if (token) {
+ const res = await decryptToken<{ userId: string }>(token);
+ if (res) {
+ impersonatedUserId = res.userId;
+ }
+ }
+
return (
<HydrationBoundary state={dehydrate(trpc.queryClient)}>
<PreferencesProvider>
<Providers>
- <PermissionProvider>
+ <PermissionProvider impersonatedUserId={impersonatedUserId}>
<InvitePageLoader />
</PermissionProvider>
</Providers>
@@ -55,27 +82,7 @@ export async function generateMetadata(props: {
const { urlId } = params;
- const poll = await prisma.poll.findUnique({
- where: {
- id: urlId as string,
- },
- select: {
- id: true,
- title: true,
- deleted: true,
- updatedAt: true,
- user: {
- select: {
- name: true,
- banned: true,
- },
- },
- },
- });
-
- if (!poll || poll.deleted || poll.user?.banned) {
- notFound();
- }
+ const poll = await getPollMetadata(urlId);
const { title, id, user } = poll;
diff --git a/apps/web/src/contexts/permissions.tsx b/apps/web/src/contexts/permissions.tsx
index 42aaca64a..cafc6f1b0 100644
--- a/apps/web/src/contexts/permissions.tsx
+++ b/apps/web/src/contexts/permissions.tsx
@@ -1,32 +1,26 @@
"use client";
-import { useSearchParams } from "next/navigation";
import React from "react";
import { useParticipants } from "@/components/participants-provider";
-import { useUser } from "@/components/user-provider";
import { usePoll } from "@/contexts/poll";
import { useRole } from "@/contexts/role";
-import { trpc } from "@/trpc/client";
+import { authClient } from "@/lib/auth-client";
const PermissionsContext = React.createContext<{
- userId: string | null;
+ impersonatedUserId: string | null;
}>({
- userId: null,
+ impersonatedUserId: null,
});
-export const PermissionProvider = ({ children }: React.PropsWithChildren) => {
- const searchParams = useSearchParams();
- const token = searchParams.get("token");
-
- const { data } = trpc.auth.getUserPermission.useQuery(
- { token: token ?? "" },
- { enabled: !!token },
- );
-
- const userId = data?.userId ?? null;
-
+export const PermissionProvider = ({
+ children,
+ impersonatedUserId,
+}: {
+ children: React.ReactNode;
+ impersonatedUserId: string | null;
+}) => {
return (
- <PermissionsContext.Provider value={{ userId }}>
+ <PermissionsContext.Provider value={{ impersonatedUserId }}>
{children}
</PermissionsContext.Provider>
);
@@ -35,12 +29,12 @@ export const PermissionProvider = ({ children }: React.PropsWithChildren) => {
export const usePermissions = () => {
const poll = usePoll();
const context = React.useContext(PermissionsContext);
- const { user, ownsObject } = useUser();
+ const { data: session } = authClient.useSession();
const role = useRole();
const { participants } = useParticipants();
return {
isUser: (userId: string) =>
- userId === user?.id || userId === context.userId,
+ userId === session?.user?.id || userId === context.impersonatedUserId,
canAddNewParticipant: poll.status === "open",
canEditParticipant: (participantId: string) => {
if (poll.status !== "open") {
@@ -60,8 +54,9 @@ export const usePermissions = () => {
}
if (
- ownsObject(participant) ||
- (context.userId && participant.userId === context.userId)
+ participant.userId === session?.user?.id ||
+ (context.impersonatedUserId &&
+ participant.userId === context.impersonatedUserId)
) {
return true;
}