Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added(server): onJoinCall and onLeaveCall wss event listeners. #159

Merged
merged 8 commits into from
Dec 2, 2024
70 changes: 0 additions & 70 deletions apps/nova/src/components/Chat/MessageGroup.tsx

This file was deleted.

21 changes: 15 additions & 6 deletions apps/nova/src/components/CurrentLessons/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { UsePaginateResult } from "@/types/query";
import { Element, ILesson, IUser, Void } from "@litespace/types";
import { UpcomingLessonCard } from "@litespace/luna/UpcomingLessonCard";
import { LessonCard } from "@litespace/luna/LessonCard";
import { asFullAssetUrl } from "@litespace/luna/backend";

type Lessons = ILesson.FindUserLessonsApiResponse["list"];
Expand All @@ -23,23 +23,32 @@ export const Content: React.FC<ContentProps> = ({ query }) => {
const tutor = item.members.find(
(member) => member.role === IUser.Role.Tutor
);
if (!tutor) return;

const student = item.members.find(
(member) => member.role === IUser.Role.Student
);

if (!tutor || !student) return;

return (
<UpcomingLessonCard
<LessonCard
key={item.lesson.id}
start={item.lesson.start}
duration={item.lesson.duration}
onJoin={() => console.log("join")}
onCancel={() => console.log("canceled")}
onRebook={() => console.log("rebook")}
canceled={!!item.lesson.canceledBy}
canceled={
item.lesson.canceledBy === tutor.userId
? "tutor"
: item.lesson.canceledBy === student?.userId
? "student"
: null
}
tutor={{
id: tutor.userId,
name: tutor.name,
image: tutor.image ? asFullAssetUrl(tutor.image) : undefined,
rating: 3,
studentCount: 100,
}}
/>
);
Expand Down
10 changes: 3 additions & 7 deletions apps/nova/src/components/UpcomingLessons/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ export const Content: React.FC<ContentProps> = ({ query }) => {
const canceled = useCallback(
(item: Lessons[0], tutor: ILesson.PopuldatedMember) => {
if (!item.lesson.canceledBy && !item.lesson.canceledBy) return null;
if (
item.lesson.canceledBy === tutor.userId ||
item.call.canceledBy === tutor.userId
)
return "tutor";
if (item.lesson.canceledBy === tutor.userId) return "tutor";
return "student";
},
[]
Expand Down Expand Up @@ -51,8 +47,8 @@ export const Content: React.FC<ContentProps> = ({ query }) => {
return (
<LessonCard
key={item.lesson.id}
start={item.call.start}
duration={item.call.duration}
start={item.lesson.start}
duration={item.lesson.duration}
onJoin={() => console.log("join")}
onCancel={() => console.log("canceled")}
onRebook={() => console.log("rebook")}
Expand Down
1 change: 0 additions & 1 deletion packages/luna/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"./utils": "./dist/components/utils/index.js",
"./VideoPlayer": "./dist/components/VideoPlayer/index.js",
"./WeekdayPicker": "./dist/components/WeekdayPicker/index.js",
"./UpcomingLessonCard": "./dist/components/Lessons/UpcomingLessonCard.jsx",
"./hooks/intl": "./dist/hooks/intl.js",
"./hooks/validation": "./dist/hooks/validation.js",
"./hooks/duration": "./dist/hooks/duration.js",
Expand Down
23 changes: 13 additions & 10 deletions packages/models/src/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ export class Calls {
}

async addMember({
call,
user,
callId,
userId,
tx,
}: WithOptionalTx<{ call: number; user: number }>): Promise<ICall.Member> {
}: WithOptionalTx<{
callId: number;
userId: number;
}>): Promise<ICall.Member> {
const rows = await this.builder(tx)
.members.insert({
call_id: call,
user_id: user,
call_id: callId,
user_id: userId,
})
.returning("*");

Expand All @@ -52,13 +55,13 @@ export class Calls {
}

async removeMember({
call,
user,
callId,
userId,
tx,
}: WithOptionalTx<{ user: number; call: number }>): Promise<void> {
}: WithOptionalTx<{ userId: number; callId: number }>): Promise<void> {
await this.builder(tx)
.members.where(this.columns.members("call_id"), call)
.andWhere(this.columns.members("user_id"), user)
.members.where(this.columns.members("call_id"), callId)
.andWhere(this.columns.members("user_id"), userId)
.delete();
}

Expand Down
12 changes: 6 additions & 6 deletions packages/models/tests/calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe(nameof(Calls), () => {
expect(await calls.findCallMembers([call.id])).to.be.of.length(0);

const member = await calls.addMember({
call: call.id,
user: tutor.id,
callId: call.id,
userId: tutor.id,
});
expect(await calls.findCallMembers([call.id])).to.be.of.length(1);
expect(member.callId).to.be.eq(call.id);
Expand All @@ -44,15 +44,15 @@ describe(nameof(Calls), () => {
expect(await calls.findCallMembers([call.id])).to.be.of.length(0);

await calls.addMember({
call: call.id,
user: tutor.id,
callId: call.id,
userId: tutor.id,
});

expect(await calls.findCallMembers([call.id])).to.be.of.length(1);

await calls.removeMember({
call: call.id,
user: tutor.id,
callId: call.id,
userId: tutor.id,
});

expect(await calls.findCallMembers([call.id])).to.be.of.length(0);
Expand Down
6 changes: 3 additions & 3 deletions packages/models/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "es6",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"baseUrl": "./",
"skipLibCheck": true,
"module": "NodeNext",
"module": "nodenext",
"declaration": true,
"declarationDir": "types",
"sourceMap": true,
"outDir": "dist",
"moduleResolution": "NodeNext",
"moduleResolution": "nodenext",
"allowSyntheticDefaultImports": true,
"emitDeclarationOnly": true,
"resolveJsonModule": true,
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ export type FindUserCallsApiResponse = {
calls: Self[];
members: Record<string, PopuldatedMember[]>;
};

export type Type = "lesson" | "interview";
32 changes: 29 additions & 3 deletions packages/types/src/wss.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMessage, IRule, ITutor, IUser, Server } from "@/index";
import { IMessage, IRule, ITutor, Server, ICall } from "@/index";

/**
* Events emitted by the client
Expand All @@ -9,6 +9,8 @@ export enum ClientEvent {
UpdateMessage = "UpdateMessage",
DeleteMessage = "DeleteMessage",
MarkAsRead = "MarkAsRead",
JoinCall = "JoinCall",
LeaveCall = "LeaveCall",
/**
* @deprecated
*/
Expand All @@ -29,23 +31,37 @@ export enum ServerEvent {
RoomMessageUpdated = "RoomMessageUpdated",
RoomMessageDeleted = "RoomMessageDeleted",
JoinedRooms = "JoinedRooms",

MessageRead = "MessageRead",

MemberJoinedCall = "MemberJoinedCallId",
MemberLeftCall = "MemberLeftCallId",

/**
* @deprecated
*/
UserJoinedCall = "UserJoinedCall",
UserSharedScreen = "UserSharedScreen",
UserStatusChanged = "UserStatusChanged",
UserTyping = "UserTyping",

CameraToggled = "CameraToggle",
MicToggled = "MicToggled",

TutorsCacheUpdated = "TutorsCacheUpdated",
TutorUpdated = "TutorUpdated",

LessonBooked = "LessonBooked",
LessonCanceled = "LessonCanceled",
TutorUpdated = "TutorUpdated",

RuleDeleted = "RuleDeleted",
RuleUpdated = "RuleUpdated",
RuleCreated = "RuleCreated",

InvoiceUpdated = "InvoiceUpdated",
InvoiceDeleted = "InvoiceDeleted",

ServerStats = "ServerStats",
UserTyping = "UserTyping",
}

export enum Room {
Expand All @@ -68,6 +84,11 @@ export type ClientEventsMap = {
[ClientEvent.ToggleCamera]: EventCallback<{ call: number; camera: boolean }>;
[ClientEvent.ToggleMic]: EventCallback<{ call: number; mic: boolean }>;
[ClientEvent.UserTyping]: EventCallback<{ roomId: number }>;
[ClientEvent.JoinCall]: EventCallback<{
callId: number;
type: ICall.Type;
}>;
[ClientEvent.LeaveCall]: EventCallback<{ callId: number }>;
};

/**
Expand All @@ -80,7 +101,12 @@ export type ServerEventsMap = {
roomId: number;
messageId: number;
}>;

[ServerEvent.UserJoinedCall]: EventCallback<{ peerId: string }>;

[ServerEvent.MemberJoinedCall]: EventCallback<{ userId: number }>;
[ServerEvent.MemberLeftCall]: EventCallback<{ userId: number }>;

[ServerEvent.CameraToggled]: EventCallback<{ user: number; camera: boolean }>;
[ServerEvent.MicToggled]: EventCallback<{ user: number; mic: boolean }>;
[ServerEvent.MessageRead]: EventCallback<{ messageId: number }>;
Expand Down
8 changes: 8 additions & 0 deletions services/server/fixtures/wss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export class ClientSocket {
this.client.emit(Wss.ClientEvent.UserTyping, { roomId });
}

joinCall(callId: number) {
this.client.emit(Wss.ClientEvent.JoinCall, { callId });
}

leaveCall(callId: number) {
this.client.emit(Wss.ClientEvent.LeaveCall, { callId });
}

/**
* Wait for event.
*
Expand Down
Loading
Loading