Maybe this
This commit is contained in:
parent
2c85c17ee2
commit
236475ed38
4 changed files with 18 additions and 25 deletions
|
@ -2,15 +2,6 @@ import Ably from "ably";
|
||||||
import { env } from "~/env.mjs";
|
import { env } from "~/env.mjs";
|
||||||
import type { EventType } from "../utils/types";
|
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 (
|
export const publishToChannel = async (
|
||||||
ablyInstance: Ably.Types.RealtimePromise,
|
ablyInstance: Ably.Types.RealtimePromise,
|
||||||
channel: string,
|
channel: string,
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { publishToChannel } from "~/server/ably";
|
import { publishToChannel } from "~/server/ably";
|
||||||
import Ably from "ably";
|
|
||||||
|
|
||||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||||
|
|
||||||
import { fetchCache, invalidateCache, setCache } from "~/server/redis";
|
import { fetchCache, invalidateCache, setCache } from "~/server/redis";
|
||||||
|
@ -17,8 +15,6 @@ export const roomRouter = createTRPCRouter({
|
||||||
)
|
)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
if (ctx.session) {
|
if (ctx.session) {
|
||||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
|
||||||
|
|
||||||
const room = await ctx.prisma.room.create({
|
const room = await ctx.prisma.room.create({
|
||||||
data: {
|
data: {
|
||||||
userId: ctx.session.user.id,
|
userId: ctx.session.user.id,
|
||||||
|
@ -33,7 +29,7 @@ export const roomRouter = createTRPCRouter({
|
||||||
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
||||||
|
|
||||||
await publishToChannel(
|
await publishToChannel(
|
||||||
ably,
|
ctx.ably,
|
||||||
`${ctx.session.user.id}`,
|
`${ctx.session.user.id}`,
|
||||||
"ROOM_LIST_UPDATE",
|
"ROOM_LIST_UPDATE",
|
||||||
"CREATE"
|
"CREATE"
|
||||||
|
@ -125,8 +121,6 @@ export const roomRouter = createTRPCRouter({
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
|
||||||
|
|
||||||
if (input.reset) {
|
if (input.reset) {
|
||||||
if (input.log) {
|
if (input.log) {
|
||||||
const oldRoom = await ctx.prisma.room.findUnique({
|
const oldRoom = await ctx.prisma.room.findUnique({
|
||||||
|
@ -209,7 +203,12 @@ export const roomRouter = createTRPCRouter({
|
||||||
});
|
});
|
||||||
|
|
||||||
if (newRoom) {
|
if (newRoom) {
|
||||||
await publishToChannel(ably, `${newRoom.id}`, "ROOM_UPDATE", "UPDATE");
|
await publishToChannel(
|
||||||
|
ctx.ably,
|
||||||
|
`${newRoom.id}`,
|
||||||
|
"ROOM_UPDATE",
|
||||||
|
"UPDATE"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return !!newRoom;
|
return !!newRoom;
|
||||||
|
@ -219,8 +218,6 @@ export const roomRouter = createTRPCRouter({
|
||||||
delete: protectedProcedure
|
delete: protectedProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(z.object({ id: z.string() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
|
||||||
|
|
||||||
const deletedRoom = await ctx.prisma.room.delete({
|
const deletedRoom = await ctx.prisma.room.delete({
|
||||||
where: {
|
where: {
|
||||||
id: input.id,
|
id: input.id,
|
||||||
|
@ -233,14 +230,14 @@ export const roomRouter = createTRPCRouter({
|
||||||
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
||||||
|
|
||||||
await publishToChannel(
|
await publishToChannel(
|
||||||
ably,
|
ctx.ably,
|
||||||
`${ctx.session.user.id}`,
|
`${ctx.session.user.id}`,
|
||||||
"ROOM_LIST_UPDATE",
|
"ROOM_LIST_UPDATE",
|
||||||
"DELETE"
|
"DELETE"
|
||||||
);
|
);
|
||||||
|
|
||||||
await publishToChannel(
|
await publishToChannel(
|
||||||
ably,
|
ctx.ably,
|
||||||
`${deletedRoom.id}`,
|
`${deletedRoom.id}`,
|
||||||
"ROOM_UPDATE",
|
"ROOM_UPDATE",
|
||||||
"DELETE"
|
"DELETE"
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { publishToChannel } from "~/server/ably";
|
import { publishToChannel } from "~/server/ably";
|
||||||
import Ably from "ably";
|
|
||||||
|
|
||||||
import type { Room } from "@prisma/client";
|
import type { Room } from "@prisma/client";
|
||||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||||
|
@ -68,8 +67,6 @@ export const voteRouter = createTRPCRouter({
|
||||||
set: protectedProcedure
|
set: protectedProcedure
|
||||||
.input(z.object({ value: z.string(), roomId: z.string() }))
|
.input(z.object({ value: z.string(), roomId: z.string() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
|
||||||
|
|
||||||
const vote = await ctx.prisma.vote.upsert({
|
const vote = await ctx.prisma.vote.upsert({
|
||||||
where: {
|
where: {
|
||||||
userId_roomId: {
|
userId_roomId: {
|
||||||
|
@ -104,7 +101,12 @@ export const voteRouter = createTRPCRouter({
|
||||||
await invalidateCache(`kv_votecount_admin`);
|
await invalidateCache(`kv_votecount_admin`);
|
||||||
await invalidateCache(`kv_votes_${input.roomId}`);
|
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;
|
return !!vote;
|
||||||
|
|
|
@ -19,6 +19,7 @@ import { type Session } from "next-auth";
|
||||||
|
|
||||||
import { getServerAuthSession } from "~/server/auth";
|
import { getServerAuthSession } from "~/server/auth";
|
||||||
import { prisma } from "~/server/db";
|
import { prisma } from "~/server/db";
|
||||||
|
import Ably from "ably";
|
||||||
|
|
||||||
type CreateContextOptions = {
|
type CreateContextOptions = {
|
||||||
session: Session | null;
|
session: Session | null;
|
||||||
|
@ -37,6 +38,7 @@ type CreateContextOptions = {
|
||||||
const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
||||||
return {
|
return {
|
||||||
session: opts.session,
|
session: opts.session,
|
||||||
|
ably: new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY),
|
||||||
prisma,
|
prisma,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -65,6 +67,7 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
|
||||||
*/
|
*/
|
||||||
import { initTRPC, TRPCError } from "@trpc/server";
|
import { initTRPC, TRPCError } from "@trpc/server";
|
||||||
import superjson from "superjson";
|
import superjson from "superjson";
|
||||||
|
import { env } from "~/env.mjs";
|
||||||
|
|
||||||
const t = initTRPC.context<typeof createTRPCContext>().create({
|
const t = initTRPC.context<typeof createTRPCContext>().create({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
|
|
Loading…
Add table
Reference in a new issue