Skip to content

Commit

Permalink
Introduced chat response events
Browse files Browse the repository at this point in the history
  • Loading branch information
devopvoid committed Dec 2, 2023
1 parent 2e7ee69 commit 672b4c1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/component/chat-box/chat-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export class ChatBox extends Component {

this.chatService.postMessage(chatForm.getFormData())
.then(() => {
Toaster.showSuccess(`${t("course.feature.message.sent")}`);

// Reset form only on success.
chatForm.resetForm();
})
Expand Down
22 changes: 21 additions & 1 deletion src/component/player/player.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import { EventEmitter } from '../../utils/event-emitter';
import { RootController } from '../controller/root.controller';
import { Controller } from '../controller/controller';
import { streamStatsStore } from '../../store/stream-stats.store';
import { LpChatStateEvent, LpEventServiceStateEvent, LpMediaStateEvent, LpParticipantPresenceEvent, LpQuizStateEvent, LpRecordingStateEvent, LpStreamStateEvent } from '../../event';
import { LpChatResponseEvent, LpChatStateEvent, LpEventServiceStateEvent, LpMediaStateEvent, LpParticipantPresenceEvent, LpQuizStateEvent, LpRecordingStateEvent, LpStreamStateEvent } from '../../event';
import { Toaster } from '../../utils/toaster';
import { t } from 'i18next';

export class PlayerController extends Controller implements ReactiveController {

Expand Down Expand Up @@ -69,6 +71,8 @@ export class PlayerController extends Controller implements ReactiveController {

this.eventEmitter.addEventListener("lp-event-service-state", this.onEventServiceState.bind(this));
this.eventEmitter.addEventListener("lp-chat-state", this.onChatState.bind(this));
this.eventEmitter.addEventListener("lp-chat-error", this.onChatError.bind(this));
this.eventEmitter.addEventListener("lp-chat-response", this.onChatResponse.bind(this));
this.eventEmitter.addEventListener("lp-quiz-state", this.onQuizState.bind(this));
this.eventEmitter.addEventListener("lp-recording-state", this.onRecordingState.bind(this));
this.eventEmitter.addEventListener("lp-stream-state", this.onStreamState.bind(this));
Expand Down Expand Up @@ -363,6 +367,22 @@ export class PlayerController extends Controller implements ReactiveController {
this.updateConnectionState();
}

private onChatError(event: LpChatResponseEvent) {
const response = event.detail;

if (response.statusCode != 0) {
Toaster.showError(t("course.feature.message.send.error"));
}
}

private onChatResponse(event: LpChatResponseEvent) {
const response = event.detail;

if (response.statusCode == 0) {
Toaster.showSuccess(t("course.feature.message.sent"));
}
}

private onQuizState(event: LpQuizStateEvent) {
const quizState = event.detail;

Expand Down
1 change: 1 addition & 0 deletions src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lp-chat-response.event';
export * from "./lp-chat-state.event";
export * from './lp-device-change.event';
export * from './lp-device-mute.event';
Expand Down
10 changes: 10 additions & 0 deletions src/event/lp-chat-response.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CourseFeatureResponse } from "../model/course-feature";

export type LpChatResponseEvent = CustomEvent<CourseFeatureResponse>;

declare global {
interface GlobalEventHandlersEventMap {
"lp-chat-error": LpChatResponseEvent;
"lp-chat-response": LpChatResponseEvent;
}
}
20 changes: 16 additions & 4 deletions src/service/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Client, Message, StompHeaders } from '@stomp/stompjs';
import { EventSubService } from "./event.service";
import { featureStore } from '../store/feature.store';
import { chatStore } from '../store/chat.store';
import { EventEmitter } from '../utils/event-emitter';
import { Utils } from '../utils/utils';

export enum ChatRecipientType {

Expand Down Expand Up @@ -60,10 +62,13 @@ export class ChatService extends EventTarget implements EventSubService {

private client: Client;

private eventEmitter: EventEmitter;

initialize(courseId: number, client: Client): void {

initialize(courseId: number, client: Client, eventEmitter: EventEmitter): void {
this.courseId = courseId;
this.client = client;
this.eventEmitter = eventEmitter;

client.subscribe("/topic/course/" + this.courseId + "/chat", (message: Message) => {
const chatMessage = JSON.parse(message.body);
Expand All @@ -75,6 +80,12 @@ export class ChatService extends EventTarget implements EventSubService {

chatStore.addMessage(chatMessage);
});
client.subscribe("/user/queue/chat/response", (message: Message) => {
this.handleEvent("lp-chat-response", message.body);
});
client.subscribe("/user/queue/chat/error", (message: Message) => {
this.handleEvent("lp-chat-error", message.body);
});
}

postMessage(data: FormData): Promise<void> {
Expand All @@ -93,9 +104,6 @@ export class ChatService extends EventTarget implements EventSubService {
if (!featureStore.chatFeature) {
return Promise.reject("Feature must be active");
}
if (!featureStore.chatFeature) {
return Promise.reject("Feature must be active");
}

const message: ChatMessageDto = {
serviceId: featureStore.chatFeature.featureId,
Expand All @@ -117,4 +125,8 @@ export class ChatService extends EventTarget implements EventSubService {
resolve();
});
}

private handleEvent(eventName: string, body: string) {
this.eventEmitter.dispatchEvent(Utils.createEvent(eventName, JSON.parse(body)));
}
}
4 changes: 2 additions & 2 deletions src/service/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Client, Message } from '@stomp/stompjs';

export interface EventSubService {

initialize(courseId: number, client: Client): void;
initialize(courseId: number, client: Client, eventEmitter: EventEmitter): void;

}

Expand Down Expand Up @@ -73,7 +73,7 @@ export class EventService extends EventTarget {
});

for (const subService of this.subServices) {
subService.initialize(this.courseId, client);
subService.initialize(this.courseId, client, this.eventEmitter);
}
};
client.onDisconnect = () => {
Expand Down

0 comments on commit 672b4c1

Please sign in to comment.