ui/src/app/services/chat.service.ts
BENEDEK László ebbaf6716d feat: implement message bar and enhance chat functionality
- Added MessageBarComponent for sending messages.
- Integrated message sending logic in ChatService.
- Updated FeedComponent to scroll to the bottom on new messages.
- Improved layout and styling for chat components.
- Adjusted environment configuration for API base URL.
2025-06-07 02:42:59 +02:00

86 lines
2.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { catchError, from, map, merge, mergeMap, Observable, throwError } from 'rxjs';
import { webSocket } from 'rxjs/webSocket';
import { Channel } from '../models/channel';
import { Message } from '../models/message';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../environment/environment';
import { GetMessagesResponse, ListAvailableChannelsResponse, SendMessageResponse } from './responses/chat';
@Injectable({
providedIn: 'root'
})
export class ChatService {
private readonly CHANNEL: string = "channel_id";
private readonly CONTENT: string = "content";
constructor(private http: HttpClient) { }
public ListAvailableChannels(): Observable<Channel[]> {
let url = `${environment.apiBase}/chat/channels`
return this.http.get<ListAvailableChannelsResponse>(url, { withCredentials: true })
.pipe(
map(response => {
if (response.error) {
throw new Error(response.error);
}
if (response.channels) {
return response.channels;
}
throw new Error("bad API response, missing channels with no error");
}),
catchError(error => throwError(() => new Error(error.error.message)))
);
}
public GetMessages(channelID: number): Observable<Message> {
let url = `${environment.apiBase}/chat/messages/${channelID}`;
let messages: Observable<Message[]> =
this.http.get<GetMessagesResponse>(url, { withCredentials: true })
.pipe(
map(response => {
if (response.error) {
throw new Error(response.error);
}
if (response.messages) {
return response.messages;
}
throw new Error("bad API response, missing messages with no error");
}),
catchError(error => throwError(() => new Error(error.error.message)))
);
url = `${environment.apiBase}/chat/subscribe/${channelID}`;
let socket = webSocket<Message>(url);
return merge(
messages.pipe(mergeMap(msgArray => from(msgArray))),
socket.asObservable());
}
public SendMessage(channel: number, content: string): Observable<SendMessageResponse> {
let url = `${environment.apiBase}/chat/send`;
let data = new FormData();
data.append(this.CHANNEL, channel.toString());
data.append(this.CONTENT, content);
return this.http.post<SendMessageResponse>(url, data, { withCredentials: true })
.pipe(
map(response => {
if (response.error) {
throw new Error(response.error);
}
return response;
}),
catchError(error => throwError(() => new Error(error.error.message)))
);
}
}