starting implementation of user status changing

This commit is contained in:
talksik
2022-03-23 21:09:50 -04:00
parent 3cdb4c896b
commit 500330e02c
6 changed files with 68 additions and 8 deletions
+27
View File
@@ -2,6 +2,8 @@ import express, { Application, Request, Response } from "express";
import { NextFunction } from "express";
import SocketChannels from "@nirvana/core/sockets/channels";
import { UserService } from "./services/user.service";
import { UserStatus } from "@nirvana/core/models";
import { connectToDatabase } from "./services/database.service";
import cors from "cors";
import getContactsRoutes from "./routes/contacts";
@@ -101,6 +103,31 @@ io.on("connection", function (socket: any) {
);
});
// change of user status to all of users' rooms and db update
socket.on(
SocketChannels.SEND_USER_STATUS_UPDATE,
async (userGoogleId: string, newStatus: UserStatus) => {
console.log("new status for user");
const resultUpdate = await UserService.updateUserStatus(
userGoogleId,
newStatus
);
// tell all rooms that the user is part of
// that this user has updated their status
if (resultUpdate?.modifiedCount) {
socket.rooms.forEach((roomId: string) => {
io.in(roomId).emit(
SocketChannels.SEND_USER_STATUS_UPDATE,
userGoogleId,
newStatus
);
});
}
}
);
// ==== DISCONNECT ====
socket.on("disconnect", () => {
console.log("user disconnected");