Self-Hosting Update (#842)

This commit is contained in:
Luke Vella 2023-09-11 15:34:55 +01:00 committed by GitHub
parent 3e616d1e41
commit 7a5f9ae474
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 945 additions and 781 deletions

View file

@ -19,7 +19,13 @@ export async function createContext(
opts.req.session.user = user;
await opts.req.session.save();
}
return { user, session: opts.req.session, req: opts.req, res: opts.res };
return {
user,
session: opts.req.session,
req: opts.req,
res: opts.res,
isSelfHosted: process.env.NEXT_PUBLIC_SELF_HOSTED === "true",
};
}
export type Context = trpc.inferAsyncReturnType<typeof createContext>;

View file

@ -9,7 +9,6 @@ import utc from "dayjs/plugin/utc";
import * as ics from "ics";
import { z } from "zod";
import { getSubscriptionStatus } from "../../utils/auth";
import { getTimeZoneAbbreviation } from "../../utils/date";
import { nanoid } from "../../utils/nanoid";
import {
@ -74,8 +73,6 @@ export const polls = router({
const participantUrlId = nanoid();
const pollId = nanoid();
const { active: isPro } = await getSubscriptionStatus(ctx.user.id);
const poll = await prisma.poll.create({
select: {
adminUrlId: true,
@ -117,13 +114,9 @@ export const polls = router({
})),
},
},
...(isPro
? {
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
hideScores: input.hideScores,
}
: undefined),
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
hideScores: input.hideScores,
},
});
@ -171,11 +164,9 @@ export const polls = router({
hideScores: z.boolean().optional(),
}),
)
.mutation(async ({ ctx, input }) => {
.mutation(async ({ input }) => {
const pollId = await getPollIdFromAdminUrlId(input.urlId);
const { active: isPro } = await getSubscriptionStatus(ctx.user.id);
if (input.optionsToDelete && input.optionsToDelete.length > 0) {
await prisma.option.deleteMany({
where: {
@ -218,13 +209,9 @@ export const polls = router({
description: input.description,
timeZone: input.timeZone,
closed: input.closed,
...(isPro
? {
hideScores: input.hideScores,
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
}
: undefined),
hideScores: input.hideScores,
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
},
});
}),

View file

@ -19,7 +19,8 @@ export const middleware = t.middleware;
export const possiblyPublicProcedure = t.procedure.use(
middleware(async ({ ctx, next }) => {
if (process.env.AUTH_REQUIRED === "true" && ctx.user.isGuest) {
// On self-hosted instances, these procedures require login
if (ctx.isSelfHosted && ctx.user.isGuest) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Login is required",
@ -38,6 +39,11 @@ export const proProcedure = t.procedure.use(
});
}
if (ctx.isSelfHosted) {
// Self-hosted instances don't have paid subscriptions
return next();
}
const { active: isPro } = await getSubscriptionStatus(ctx.user.id);
if (!isPro) {