add message repository

This commit is contained in:
Peter Maquiran
2024-06-04 13:12:38 +01:00
parent e8d68c45d7
commit 4fb5bfc4d0
2 changed files with 81 additions and 0 deletions
@@ -0,0 +1,53 @@
import { Injectable } from '@angular/core';
import { Subject, BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MessageLiveDataSourceService {
private messageSubject: Subject<any> = new Subject<any>();
private isPaused: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
private messageBuffer: any[] = []; // Buffer to store messages when paused
private authentication!: string
constructor() { }
handleMessage(event: any) {
const data: any = event.data || MessageEvent;
if (this.isPaused.getValue()) {
this.messageBuffer.push({}); // Buffer the message if paused
} else {
this.messageSubject.next({} as any);
}
}
pauseBroadcast() {
this.isPaused.next(true);
}
resumeBroadcast() {
this.isPaused.next(false);
this.flushMessageBuffer(); // Emit all buffered messages
}
private flushMessageBuffer() {
while (this.messageBuffer.length > 0) {
const bufferedMessage = this.messageBuffer.shift();
this.messageSubject.next(bufferedMessage);
}
}
subscribe(endpoint: any): Observable<any> {
return this.messageSubject.pipe(
filter(message =>
message &&
message.endpoint === endpoint &&
message.authentication == this.authentication
)
)
}
}
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { HttpService } from 'src/app/services/http.service';
import { MessageOutPutDTO } from '../../dto/message/messageOutputDTO';
import { MessageListInputDTO } from '../../dto/message/messageListInputDTO';
@Injectable({
providedIn: 'root'
})
export class MessageRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(private httpService: HttpService) {}
async sendMessage(message: any) {
return await this.httpService.post<any>(`${this.baseUrl}/Messages`, message);
}
async reactToMessage(id: string, reaction: any) {
return await this.httpService.post<any>(`${this.baseUrl}/Messages/${id}/React`, reaction);
}
async getMessagesFromRoom(id: MessageListInputDTO) {
return await this.httpService.get<MessageOutPutDTO>(`${this.baseUrl}/Room/${id}/Messages`);
}
}