Skip to content

Commit d194e23

Browse files
committed
feat: add image to innkeeper
1 parent 191b771 commit d194e23

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

frontend/src/app/components/modal/PreviewModalButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const PreviewModalButton = ({
1616
modalId: string,
1717
e: React.MouseEvent<HTMLButtonElement>,
1818
) => {
19-
e.preventDefault;
19+
e.preventDefault();
2020
if (document) {
2121
(document.getElementById(modalId) as HTMLFormElement).showModal();
2222
}

frontend/src/app/components/status-bar/UserStatusBadge.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { UserState } from "@/libs/innkeeper-api-types";
2+
import { BiUserCircle } from "@react-icons/all-files/bi/BiUserCircle";
23

34
interface UserStateBadgeProps {
45
userState: UserState; // Update the prop name to 'userState'
@@ -17,13 +18,13 @@ const fromEpochToRelative = (epochTime: number) => {
1718
};
1819

1920
const UserStateBadge: React.FC<UserStateBadgeProps> = ({ userState }) => {
20-
const { displayName, status, lastSeen } = userState;
21+
const { displayName, status, lastSeen, imageUrl } = userState;
2122
const lastSeenRelative = fromEpochToRelative(lastSeen);
2223
return status !== "EXITED" ? (
23-
<div className="flex items-center gap-2">
24+
<div className="flex items-center">
2425
<div className="tooltip" data-tip={`Last Seen: ${lastSeenRelative}`}>
2526
<div
26-
className={`aspect-square w-4 rounded-full ${
27+
className={`mr-3 aspect-square w-4 rounded-full ${
2728
status === "INACTIVE"
2829
? "bg-gray-200"
2930
: status === "ACTIVE"
@@ -32,6 +33,15 @@ const UserStateBadge: React.FC<UserStateBadgeProps> = ({ userState }) => {
3233
}`}
3334
/>
3435
</div>
36+
{imageUrl ? (
37+
<img
38+
src={imageUrl}
39+
className="mr-2 aspect-square w-8 rounded-full"
40+
></img>
41+
) : (
42+
<BiUserCircle className="mr-2 aspect-square w-8 text-4xl" />
43+
)}
44+
3545
<h3>{displayName}</h3>
3646
</div>
3747
) : (

frontend/src/libs/innkeeper-api-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ export type WaitingUsersCount = {
1414
export type UserId = {
1515
userId: string;
1616
displayName: string;
17+
imageUrl: string;
1718
};
1819

1920
export type UserState = {
2021
userId: string;
2122
displayName: string;
23+
imageUrl: string;
2224
status: "INACTIVE" | "ACTIVE" | "EXITED";
2325
lastSeen: number; // Unix time (seconds since epoch)
2426
};

innkeeper/src/controllers/lobby.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export const handleMatchingRequest = (
3636
params: MatchingParameters,
3737
): void => {
3838
socket.data.lastMessage = getUnixTimestamp();
39-
const { userId, displayName } = socket.data;
39+
const { userId, displayName, imageUrl } = socket.data;
4040

4141
SHOULD_LOG && console.log(`User ${displayName} (${userId}) requested a match with parameters ${JSON.stringify(params)}.`);
4242

4343
// Remove user from queue in case this is a re-request.
44-
inn.removeUserFromQueue({ userId, displayName });
44+
inn.removeUserFromQueue({ userId, displayName, imageUrl });
4545

46-
const maybeMatch = inn.queueUserOrReturnMatchResult({ userId, displayName }, params);
46+
const maybeMatch = inn.queueUserOrReturnMatchResult({ userId, displayName, imageUrl }, params);
4747
if (!maybeMatch) {
4848
notifyOnQueue(io, inn, socket);
4949
SHOULD_LOG && console.log(`User ${userId} added to queue.`);
@@ -75,14 +75,14 @@ export const handleMatchingRequest = (
7575
console.error(`Could not find socket for userId ${otherUserId} (returned from queue).`);
7676
inn.removeUserFromQueue(otherUserId);
7777

78-
inn.addUserToQueue({ userId, displayName }, params);
78+
inn.addUserToQueue({ userId, displayName, imageUrl }, params);
7979
notifyOnQueue(io, inn, socket);
8080
});
8181
};
8282

8383
export const handleDisconnect = (io: InnkeeperIoServer, inn: InnState, socket: InnkeeperIoSocket): void => {
84-
const { userId, displayName } = socket.data;
85-
inn.removeUserFromQueue({ userId, displayName });
84+
const { userId, displayName, imageUrl } = socket.data;
85+
inn.removeUserFromQueue({ userId, displayName, imageUrl });
8686

8787
SHOULD_LOG && console.log(`User ${userId} disconnected from queue.`);
8888
};

innkeeper/src/types/innkeeper-api-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type WaitingUsersCount = {
1414
export type UserId = {
1515
userId: string;
1616
displayName: string;
17+
imageUrl: string;
1718
};
1819

1920
export type UserState = {
@@ -100,6 +101,7 @@ export interface InterServerEvents {}
100101
export interface InnkeeperSocketData {
101102
userId: string; // Primary key for Users table.
102103
displayName: string; // Display name for user.
104+
imageUrl: string;
103105
roomId: string | null; // convenience field populated manually by server.
104106
lastMessage: number; // Last active timestamp in Unix time (seconds since epoch). 0 indicates user has never joined.
105107
}

innkeeper/src/utils/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { InnState } from '../models';
55
import { InnkeeperIoServer, InnkeeperIoSocket, NotificationMessage } from '../types';
66

77
export const SHOULD_LOG = process.env.LOGGING === 'VERBOSE';
8+
export const URL = process.env.URL ?? 'https://peerprep.sivarn.com';
89

910
export const getUnixTimestamp = (): number => {
1011
return Math.floor(Date.now() / 1000);
@@ -31,8 +32,13 @@ export const requireUser = async (
3132
return next(new Error('Authorization failed.'));
3233
}
3334

35+
const data = await fetch(`${URL}/api/v1/users/profile`, {
36+
headers: { 'firebase-token': socket.handshake.auth.token },
37+
}).then(res => res.json());
38+
3439
socket.data.userId = userId;
35-
socket.data.displayName = (await firebaseAuth.getUser(userId)).displayName ?? 'Anonymous';
40+
socket.data.displayName = data.payload.name ?? 'Anonymous';
41+
socket.data.imageUrl = data.payload.imageUrl;
3642
socket.data.roomId = inn.getRoomId(userId);
3743
socket.data.lastMessage = getUnixTimestamp();
3844

0 commit comments

Comments
 (0)