ably updates...

This commit is contained in:
Atridad Lahiji 2023-07-12 18:15:19 -06:00
parent 90606df040
commit 2c85c17ee2
No known key found for this signature in database
3 changed files with 32 additions and 5 deletions

View file

@ -2,13 +2,21 @@ 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 ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); 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,
channel: string, channel: string,
event: EventType, event: EventType,
message: string message: string
) => { ) => {
const channelName = ably.channels.get(`${env.APP_ENV}-${channel}`); const channelName = ablyInstance.channels.get(`${env.APP_ENV}-${channel}`);
await channelName.publish(event, message); await channelName.publish(event, message);
}; };

View file

@ -1,9 +1,11 @@
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";
import { env } from "~/env.mjs";
export const roomRouter = createTRPCRouter({ export const roomRouter = createTRPCRouter({
// Create // Create
@ -15,6 +17,8 @@ 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,
@ -29,6 +33,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.session.user.id}`, `${ctx.session.user.id}`,
"ROOM_LIST_UPDATE", "ROOM_LIST_UPDATE",
"CREATE" "CREATE"
@ -120,6 +125,8 @@ 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({
@ -202,7 +209,7 @@ export const roomRouter = createTRPCRouter({
}); });
if (newRoom) { if (newRoom) {
await publishToChannel(`${newRoom.id}`, "ROOM_UPDATE", "UPDATE"); await publishToChannel(ably, `${newRoom.id}`, "ROOM_UPDATE", "UPDATE");
} }
return !!newRoom; return !!newRoom;
@ -212,6 +219,8 @@ 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,
@ -224,12 +233,18 @@ 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.session.user.id}`, `${ctx.session.user.id}`,
"ROOM_LIST_UPDATE", "ROOM_LIST_UPDATE",
"DELETE" "DELETE"
); );
await publishToChannel(`${deletedRoom.id}`, "ROOM_UPDATE", "DELETE"); await publishToChannel(
ably,
`${deletedRoom.id}`,
"ROOM_UPDATE",
"DELETE"
);
} }
return !!deletedRoom; return !!deletedRoom;

View file

@ -1,9 +1,11 @@
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";
import { fetchCache, invalidateCache, setCache } from "~/server/redis"; import { fetchCache, invalidateCache, setCache } from "~/server/redis";
import { env } from "~/env.mjs";
export const voteRouter = createTRPCRouter({ export const voteRouter = createTRPCRouter({
countAll: protectedProcedure.query(async ({ ctx }) => { countAll: protectedProcedure.query(async ({ ctx }) => {
@ -66,6 +68,8 @@ 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: {
@ -100,7 +104,7 @@ 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(`${vote.roomId}`, "VOTE_UPDATE", "UPDATE"); await publishToChannel(ably, `${vote.roomId}`, "VOTE_UPDATE", "UPDATE");
} }
return !!vote; return !!vote;