Merge pull request 'fix(chat): Issue-440 chat v2' (#519) from mso7nueh/eatvio_app:Issue-440 into main

Reviewed-on: eatvio/eatvio_app#519
This commit is contained in:
mso7nueh
2025-06-20 08:48:07 +00:00
5 changed files with 61 additions and 11 deletions

View File

@@ -226,6 +226,7 @@ Future<void> init() async {
'accept': 'application/json',
'content-type': 'application/json',
},
validateStatus: (status) => status != null && status < 500,
),
)..interceptors.addAll(
[

View File

@@ -154,4 +154,25 @@ class ChatRepository {
rethrow;
}
}
Future<String> getChatByUserSlug(String slug) async {
try {
final response = await chatService.getChatByUserSlug(slug: slug);
if (response.response.statusCode == 404) {
return '404';
} else {
return response.data['id'];
}
} catch (e) {
return 'error';
}
}
Future<void> sendFirstChatMessageForCreateChatRoom(String userSlug) async {
try {
return await chatService.sendMessageByUserSlug(userSlug: userSlug, data: 'Создал чат!');
} catch (e) {
rethrow;
}
}
}

View File

@@ -24,7 +24,7 @@ abstract class ChatService {
@Path() required String id,
});
@POST('/api/chat/{id}/message/send')
@POST('/api/v2/chat/message/send/{id}')
@MultiPart()
Future<void> sendMessage({
@Path() required String id,
@@ -80,4 +80,17 @@ abstract class ChatService {
@Path() required int id,
@Body() required PlanComplainModel body,
});
@GET('/api/v2/chat/get_by_user/{slug}')
Future<HttpResponse> getChatByUserSlug({
@Path() required String slug,
});
@POST('/api/v2/chat/message/send/{userSlug}')
@MultiPart()
Future<void> sendMessageByUserSlug({
@Path() required String userSlug,
@Part() String? data,
@Part(name: 'media_data[]', contentType: 'multipart/form-data') List<MultipartFile>? mediaData,
});
}

View File

@@ -1,12 +1,15 @@
import 'dart:developer';
import 'dart:io';
import 'package:eatvio_app/core/app_router.dart';
import 'package:eatvio_app/core/image_compressing/image_compress_utility.dart';
import 'package:eatvio_app/core/utils/toasts.dart';
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
import 'package:eatvio_app/features/consultant/become_consultant/repositories/consultant_repository.dart';
import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_state.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
class ProfileCubit extends Cubit<ProfileState> {
final ProfileRepository profileRepository;
@@ -103,11 +106,30 @@ class ProfileCubit extends Cubit<ProfileState> {
}
}
Future<String> getChatRoomIdBySlug({
getChatRoomIdBySlug({
required String slug,
}) async {
try {
return (await chatRepository.getChatRoomIdByUserSlug(slug: slug)).id;
final chatIdOrError = await chatRepository.getChatByUserSlug(slug);
if (chatIdOrError == '404') {
await chatRepository.sendFirstChatMessageForCreateChatRoom(slug);
final chatIdOrError = await chatRepository.getChatByUserSlug(slug);
AppRouter.navigatorKey.currentContext?.pushNamed(
'chatScreen',
extra: {
'chatId': chatIdOrError,
},
);
} else if (chatIdOrError == 'error') {
showErrorToast('Ошибка создания чата');
} else {
AppRouter.navigatorKey.currentContext?.pushNamed(
'chatScreen',
extra: {
'chatId': chatIdOrError,
},
);
}
} catch (e, st) {
log(e.toString(), stackTrace: st);
return '';

View File

@@ -1232,14 +1232,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
children: [
const SizedBox(width: 16),
GestureDetector(
onTap: () async {
GoRouter.of(context).pushNamed(
'chatScreen',
extra: {
'chatId': await profileCubit.getChatRoomIdBySlug(slug: widget.author!.authorSlug),
},
);
},
onTap: () async => await profileCubit.getChatRoomIdBySlug(slug: widget.author!.authorSlug),
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(8),