Skip to content

Commit

Permalink
feat: add curhat notification
Browse files Browse the repository at this point in the history
  • Loading branch information
RMA1403 committed Dec 14, 2024
1 parent e416c68 commit 66641e9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/repositories/chatroom.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UserPinnedChatrooms,
} from '~/db/schema';
import { TChatroom } from '~/types/curhat.types';
import { getUsersByRole } from './user-role.repo';

type ChatroomWithMessages = Chatroom & {
messages: ChatroomMessage[];
Expand Down Expand Up @@ -164,3 +165,27 @@ export async function pinChatroom(
}
});
}

/**
* Get the ids of all curhat admin and the user who created the chatroom
*/
export async function getChatroomParticipantIds(
db: Database,
chatroomId: string,
) {
const chatroomPromise = getChatroomById(db, chatroomId);
const adminUsersPromise = getUsersByRole(db, 'curhatadmin');

const [chatroom, adminUsers] = await Promise.all([
chatroomPromise,
adminUsersPromise,
]);

if (!chatroom) {
return [];
}

return [chatroom.userId, ...adminUsers.map((u) => u.id)].filter(
(id) => id !== null,
);
}
13 changes: 12 additions & 1 deletion src/repositories/user-role.repo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { eq } from 'drizzle-orm';
import { Database } from '~/db/drizzle';
import { userRoles } from '~/db/schema';
import { userRoles, UserRolesEnum } from '~/db/schema';

export async function getUserRoles(db: Database, userId: string) {
const roles = (
await db.select().from(userRoles).where(eq(userRoles.userId, userId))
).map((r) => r.role);
return roles;
}

export async function getUsersByRole(db: Database, role: UserRolesEnum) {
return (
await db.query.userRoles.findMany({
where: eq(userRoles.role, role),
with: {
user: true,
},
})
).map((ur) => ur.user);
}
38 changes: 37 additions & 1 deletion src/websocket/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { ServerType } from '@hono/node-server';
import { Server } from 'socket.io';
import { env } from '~/configs/env.config';
import { db } from '~/db/drizzle';
import { saveMessage } from '~/repositories/chatroom.repo';
import { sendNotificationToAll } from '~/lib/push-manager';
import {
getChatroomById,
getChatroomParticipantIds,
saveMessage,
} from '~/repositories/chatroom.repo';
import { getPushSubscriptionsByUserIds } from '~/repositories/push.repo';

interface Message {
chatroomId: string;
Expand All @@ -29,6 +35,12 @@ export default function setupWebsocket(httpServer: ServerType) {
msg.message,
msg.replyId,
);

sendNotification(msg).then(
() => {},
() => {},
);

socket.to(msg.chatroomId).emit('reply', {
...savedMessage,
userId: undefined,
Expand All @@ -37,3 +49,27 @@ export default function setupWebsocket(httpServer: ServerType) {
});
});
}

/**
* Send notification to all participants in a chatroom except the sender.
*/
async function sendNotification(msg: Message) {
const participantIds = (
await getChatroomParticipantIds(db, msg.chatroomId)
).filter((ids) => ids !== msg.userId);

const pushSubscriptions = await getPushSubscriptionsByUserIds(
db,
participantIds,
);

await sendNotificationToAll(pushSubscriptions, {
title: 'New Curhat Message',
options: {
body: msg.message,
data: {
url: '/home/curhat',
},
},
});
}

0 comments on commit 66641e9

Please sign in to comment.