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 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 (
ablyInstance: Ably.Types.RealtimePromise,
channel: string,
event: EventType,
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);
};

View file

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

View file

@ -1,9 +1,11 @@
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 }) => {
@ -66,6 +68,8 @@ 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: {
@ -100,7 +104,7 @@ export const voteRouter = createTRPCRouter({
await invalidateCache(`kv_votecount_admin`);
await invalidateCache(`kv_votes_${input.roomId}`);
await publishToChannel(`${vote.roomId}`, "VOTE_UPDATE", "UPDATE");
await publishToChannel(ably, `${vote.roomId}`, "VOTE_UPDATE", "UPDATE");
}
return !!vote;