forked from eatvio/eatvio_app
148 lines
4.3 KiB
Dart
148 lines
4.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/core/token_storage.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_messages_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_room_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_room_pagination_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/order_id_to_conversation_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/payment_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/plan_complain_model.dart';
|
|
import 'package:eatvio_app/features/chat/services/chat_service.dart';
|
|
import 'package:eatvio_app/features/plans/models/chosen_plan_model.dart';
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
class ChatRepository {
|
|
final ChatService chatService;
|
|
|
|
final _newUserMessagesController = StreamController<ChatMessageModel>();
|
|
|
|
Stream<ChatMessageModel> get newUserMessagesStream => _newUserMessagesController.stream;
|
|
|
|
ChatRepository({
|
|
required this.chatService,
|
|
});
|
|
|
|
void emitNewMessageToChats(ChatMessageModel message) {
|
|
_newUserMessagesController.add(message);
|
|
}
|
|
|
|
Future<ChatMessageModel> sendMessage({
|
|
required String slug,
|
|
String? data,
|
|
List<File>? files,
|
|
}) async {
|
|
final multipartFiles = <MultipartFile>[];
|
|
|
|
if (files != null) {
|
|
for (final file in files) {
|
|
final fileBytes = await file.readAsBytes();
|
|
final multipartFile = MultipartFile.fromBytes(
|
|
fileBytes,
|
|
filename: file.path.split('/').last,
|
|
contentType: MediaType('application', 'octet-stream'),
|
|
);
|
|
multipartFiles.add(multipartFile);
|
|
}
|
|
}
|
|
|
|
// Если data пустая строка или null, не передаем её вообще
|
|
// Сервер требует либо непустую строку, либо отсутствие поля
|
|
final dataToSend = (data == null || data.trim().isEmpty) ? null : data;
|
|
|
|
return await chatService.sendMessage(
|
|
slug: slug,
|
|
data: dataToSend,
|
|
mediaData: multipartFiles.isEmpty ? null : multipartFiles,
|
|
socketId: (await SocketIdStorage.getSocketIdFromStorage())!,
|
|
);
|
|
|
|
}
|
|
|
|
Future<ChatRoomModel> getChatRoom(String uuid) async {
|
|
return (await chatService.getChatRoom(uuid: uuid));
|
|
|
|
}
|
|
|
|
Future<ChatRoomPaginationModel> getChatRooms({String? query, int page = 1}) async {
|
|
return (await chatService.getChatRooms(query: query, page: page));
|
|
|
|
}
|
|
|
|
Future<ChatMessagesModel> getChatMessages(String id, int page) async {
|
|
return (await chatService.getChatMessages(id: id, page: page));
|
|
|
|
}
|
|
|
|
Future<ChatRoomModel> getChatRoomIdByUserSlug({required String slug}) async {
|
|
return (await chatService.getChatRoomIdByUserSlug(user: slug));
|
|
|
|
}
|
|
|
|
Future<void> markMessagesAsRead(String id, List<int> messageIds) async {
|
|
await chatService.markMessagesAsRead(id: id, body: {'messages_ids': messageIds});
|
|
|
|
}
|
|
|
|
Future<void> cancelChosenPlan({
|
|
required int id,
|
|
}) async {
|
|
await chatService.cancelChosenPlan(id: id);
|
|
|
|
}
|
|
|
|
Future<void> choseStartPlanDate({required int id, required Map<String, String> date}) async {
|
|
await chatService.choseStartPlanDate(id: id, date: date);
|
|
|
|
}
|
|
|
|
Future<PaymentModel> getPaymentLink({
|
|
required int id,
|
|
required String paymentProvider,
|
|
}) async {
|
|
return await chatService.getPaymentLink(
|
|
id: id,
|
|
body: {'payment_provider': paymentProvider},
|
|
);
|
|
|
|
}
|
|
|
|
Future<OrderIdToConversationModel> resolveOrderIdToConversation({
|
|
required String orderId,
|
|
}) async {
|
|
return await chatService.resolveOrderIdToConversation(orderId: orderId);
|
|
|
|
}
|
|
|
|
Future<ChosenPlanModel> getChosenPlan({
|
|
required int id,
|
|
}) async {
|
|
return await chatService.getChosenPlan(id: id);
|
|
|
|
}
|
|
|
|
Future<void> finishChosenPlan({
|
|
required int id,
|
|
}) async {
|
|
await chatService.finishChosenPlan(id: id);
|
|
|
|
}
|
|
|
|
Future<void> complainAboutConsultation({
|
|
required int id,
|
|
required PlanComplainModel body,
|
|
}) async {
|
|
await chatService.complainAboutConsultation(id: id, body: body);
|
|
|
|
}
|
|
|
|
Future<String> getChatByUserSlug(String slug) async {
|
|
final response = await chatService.getChatByUserSlug(slug: slug);
|
|
if (response.response.statusCode == 404) {
|
|
return '404';
|
|
}
|
|
return response.data['id'];
|
|
}
|
|
}
|