starting implementation of user status changing
This commit is contained in:
@@ -2,6 +2,8 @@ import express, { Application, Request, Response } from "express";
|
|||||||
|
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
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 { connectToDatabase } from "./services/database.service";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import getContactsRoutes from "./routes/contacts";
|
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 ====
|
// ==== DISCONNECT ====
|
||||||
socket.on("disconnect", () => {
|
socket.on("disconnect", () => {
|
||||||
console.log("user disconnected");
|
console.log("user disconnected");
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
||||||
|
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from "mongodb";
|
||||||
|
import { UserStatus } from "../../core/models/user.model";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { collections } from "./database.service";
|
import { collections } from "./database.service";
|
||||||
|
|
||||||
@@ -58,8 +59,9 @@ export class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async createUserIfNotExists(newUser: User) {
|
static async createUserIfNotExists(newUser: User) {
|
||||||
const exists = (await collections.users?.findOne({ id: newUser.googleId }))
|
const exists = (
|
||||||
?._id;
|
await collections.users?.findOne({ googleId: newUser.googleId })
|
||||||
|
)?._id;
|
||||||
|
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return await collections.users?.insertOne(newUser);
|
return await collections.users?.insertOne(newUser);
|
||||||
@@ -78,4 +80,15 @@ export class UserService {
|
|||||||
)
|
)
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async updateUserStatus(userGoogleId: string, newStatus: UserStatus) {
|
||||||
|
const query = { googleId: userGoogleId };
|
||||||
|
const updateDoc = {
|
||||||
|
$set: { status: newStatus, lastUpdatedDate: new Date() },
|
||||||
|
};
|
||||||
|
|
||||||
|
const resultUpdate = await collections.users?.updateOne(query, updateDoc);
|
||||||
|
|
||||||
|
return resultUpdate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,6 +107,9 @@ export function useGetUserDetails() {
|
|||||||
{
|
{
|
||||||
retry: false,
|
retry: false,
|
||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
|
onError: (err) => {
|
||||||
|
console.log(err);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||||
|
import { UserStatus } from "../../../core/models/user.model";
|
||||||
import { socket } from "../nirvanaApp";
|
import { socket } from "../nirvanaApp";
|
||||||
import { useGetAllContactBasicDetails } from "./index";
|
import { useGetAllContactBasicDetails } from "./index";
|
||||||
|
|
||||||
@@ -62,10 +63,18 @@ export default function useSocketData() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
socket.on(
|
||||||
|
SocketChannels.SEND_USER_STATUS_UPDATE,
|
||||||
|
(userGoogleId: string, newStatus: UserStatus) => {
|
||||||
|
console.log("someone updated their status...hmmm");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket.removeAllListeners(SocketChannels.SEND_STARTED_SPEAKING);
|
socket.removeAllListeners(SocketChannels.SEND_STARTED_SPEAKING);
|
||||||
socket.removeAllListeners(SocketChannels.SEND_STOPPED_SPEAKING);
|
socket.removeAllListeners(SocketChannels.SEND_STOPPED_SPEAKING);
|
||||||
socket.removeAllListeners(SocketChannels.SEND_AUDIO_CLIP);
|
socket.removeAllListeners(SocketChannels.SEND_AUDIO_CLIP);
|
||||||
|
socket.removeAllListeners(SocketChannels.SEND_USER_STATUS_UPDATE);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { useRecoilState, useSetRecoilState } from "recoil";
|
|||||||
import { GlobalHotKeys } from "react-hotkeys";
|
import { GlobalHotKeys } from "react-hotkeys";
|
||||||
import { STORE_ITEMS } from "../../../electron/constants";
|
import { STORE_ITEMS } from "../../../electron/constants";
|
||||||
import UserAvatarWithStatus from "../../../components/User/userAvatarWithStatus";
|
import UserAvatarWithStatus from "../../../components/User/userAvatarWithStatus";
|
||||||
|
import { UserStatus } from "@nirvana/core/models";
|
||||||
import { useGetUserDetails } from "../../../controller/index";
|
import { useGetUserDetails } from "../../../controller/index";
|
||||||
import { useRef } from "react";
|
import { useRef } from "react";
|
||||||
|
|
||||||
@@ -26,10 +27,20 @@ export default function Header() {
|
|||||||
setAuthTokens(null);
|
setAuthTokens(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateStatus = (newStatus: UserStatus) => {};
|
||||||
const ProfileMenu = (
|
const ProfileMenu = (
|
||||||
<Menu>
|
<Menu>
|
||||||
<Menu.Item key="0">
|
<Menu.Item key="0">
|
||||||
<button onClick={logOut}>Log Out</button>
|
<button onClick={logOut}>Log Out</button>
|
||||||
|
{user.status === UserStatus.online ? (
|
||||||
|
<button onClick={() => updateStatus(UserStatus.offline)}>
|
||||||
|
Set status as away
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button onClick={() => updateStatus(UserStatus.offline)}>
|
||||||
|
Set status as away
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</Menu.Item>
|
</Menu.Item>
|
||||||
</Menu>
|
</Menu>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export default function SelectedConversation() {
|
|||||||
$selectedConversation
|
$selectedConversation
|
||||||
);
|
);
|
||||||
const { data: userDetailsData } = useGetUserDetails();
|
const { data: userDetailsData } = useGetUserDetails();
|
||||||
const { data: convoDetailsResponse, isFetching } =
|
const { data: convoDetailsResponse, isLoading } =
|
||||||
useConversationDetails(selectedConvo);
|
useConversationDetails(selectedConvo);
|
||||||
const { mutate: sendContactRequest } = useSendContactRequest();
|
const { mutate: sendContactRequest } = useSendContactRequest();
|
||||||
const { mutate: updateRelationshipState } = useUpdateRelationshipState();
|
const { mutate: updateRelationshipState } = useUpdateRelationshipState();
|
||||||
@@ -137,10 +137,6 @@ export default function SelectedConversation() {
|
|||||||
return <></>;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFetching) {
|
|
||||||
return <span className="text-slate-200">Loading conversation details</span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: add cancel request option
|
// todo: add cancel request option
|
||||||
|
|
||||||
const sendRequest = () => {
|
const sendRequest = () => {
|
||||||
@@ -174,7 +170,7 @@ export default function SelectedConversation() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isFetching) return <span>Please wait</span>;
|
if (isLoading) return <span>Please wait</span>;
|
||||||
|
|
||||||
const renderMainContent = () => {
|
const renderMainContent = () => {
|
||||||
// if there's no relationship, then don't show messages, show "send request" button
|
// if there's no relationship, then don't show messages, show "send request" button
|
||||||
@@ -274,6 +270,7 @@ export default function SelectedConversation() {
|
|||||||
action: "keyup",
|
action: "keyup",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlers = {
|
const handlers = {
|
||||||
CLOSE_SELECTED_CONVO: handleClose,
|
CLOSE_SELECTED_CONVO: handleClose,
|
||||||
START_RECORDING: startRecording,
|
START_RECORDING: startRecording,
|
||||||
|
|||||||
Reference in New Issue
Block a user