Merge branch 'dev'

This commit is contained in:
Atridad Lahiji 2023-07-12 21:55:37 -06:00
commit b70ff40042
No known key found for this signature in database
4 changed files with 18 additions and 27 deletions

View file

@ -2,15 +2,6 @@ import Ably from "ably";
import { env } from "~/env.mjs";
import type { EventType } from "../utils/types";
const globalForAbly = globalThis as unknown as {
ably: Ably.Types.RealtimePromise;
};
export const ably =
globalForAbly.ably || new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
if (env.NODE_ENV !== "production") globalForAbly.ably = ably;
export const publishToChannel = async (
ablyInstance: Ably.Types.RealtimePromise,
channel: string,

View file

@ -1,11 +1,8 @@
import { z } from "zod";
import { publishToChannel } from "~/server/ably";
import Ably from "ably";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { fetchCache, invalidateCache, setCache } from "~/server/redis";
import { env } from "~/env.mjs";
export const roomRouter = createTRPCRouter({
// Create
@ -17,8 +14,6 @@ export const roomRouter = createTRPCRouter({
)
.mutation(async ({ ctx, input }) => {
if (ctx.session) {
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
const room = await ctx.prisma.room.create({
data: {
userId: ctx.session.user.id,
@ -33,7 +28,7 @@ export const roomRouter = createTRPCRouter({
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
await publishToChannel(
ably,
ctx.ably,
`${ctx.session.user.id}`,
"ROOM_LIST_UPDATE",
"CREATE"
@ -125,8 +120,6 @@ export const roomRouter = createTRPCRouter({
})
)
.mutation(async ({ ctx, input }) => {
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
if (input.reset) {
if (input.log) {
const oldRoom = await ctx.prisma.room.findUnique({
@ -209,7 +202,12 @@ export const roomRouter = createTRPCRouter({
});
if (newRoom) {
await publishToChannel(ably, `${newRoom.id}`, "ROOM_UPDATE", "UPDATE");
await publishToChannel(
ctx.ably,
`${newRoom.id}`,
"ROOM_UPDATE",
"UPDATE"
);
}
return !!newRoom;
@ -219,8 +217,6 @@ export const roomRouter = createTRPCRouter({
delete: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
const deletedRoom = await ctx.prisma.room.delete({
where: {
id: input.id,
@ -233,14 +229,14 @@ export const roomRouter = createTRPCRouter({
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
await publishToChannel(
ably,
ctx.ably,
`${ctx.session.user.id}`,
"ROOM_LIST_UPDATE",
"DELETE"
);
await publishToChannel(
ably,
ctx.ably,
`${deletedRoom.id}`,
"ROOM_UPDATE",
"DELETE"

View file

@ -1,11 +1,9 @@
import { z } from "zod";
import { publishToChannel } from "~/server/ably";
import Ably from "ably";
import type { Room } from "@prisma/client";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { fetchCache, invalidateCache, setCache } from "~/server/redis";
import { env } from "~/env.mjs";
export const voteRouter = createTRPCRouter({
countAll: protectedProcedure.query(async ({ ctx }) => {
@ -68,8 +66,6 @@ export const voteRouter = createTRPCRouter({
set: protectedProcedure
.input(z.object({ value: z.string(), roomId: z.string() }))
.mutation(async ({ ctx, input }) => {
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
const vote = await ctx.prisma.vote.upsert({
where: {
userId_roomId: {
@ -104,7 +100,12 @@ export const voteRouter = createTRPCRouter({
await invalidateCache(`kv_votecount_admin`);
await invalidateCache(`kv_votes_${input.roomId}`);
await publishToChannel(ably, `${vote.roomId}`, "VOTE_UPDATE", "UPDATE");
await publishToChannel(
ctx.ably,
`${vote.roomId}`,
"VOTE_UPDATE",
"UPDATE"
);
}
return !!vote;

View file

@ -19,6 +19,7 @@ import { type Session } from "next-auth";
import { getServerAuthSession } from "~/server/auth";
import { prisma } from "~/server/db";
import Ably from "ably";
type CreateContextOptions = {
session: Session | null;
@ -37,6 +38,7 @@ type CreateContextOptions = {
const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
ably: new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY),
prisma,
};
};
@ -65,6 +67,7 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
*/
import { initTRPC, TRPCError } from "@trpc/server";
import superjson from "superjson";
import { env } from "~/env.mjs";
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,