100 lines
2.9 KiB
Dart
100 lines
2.9 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:taafee_mobile/core/network/socket/event.dart';
|
|
import 'package:taafee_mobile/core/network/socket/events.dart';
|
|
import 'package:taafee_mobile/core/network/socket/socket.dart';
|
|
import 'package:taafee_mobile/features/chat/data_layer/model/chat_user.dart';
|
|
import 'package:taafee_mobile/features/chat/data_layer/model/message.dart';
|
|
import 'package:taafee_mobile/features/chat/data_layer/model/room.dart';
|
|
|
|
class ChatResource {
|
|
SocketIO io = Get.find<SocketIO>();
|
|
|
|
/// ------------- rooms ---------------- ///
|
|
//private
|
|
|
|
Future<List<Room>> getRooms(
|
|
{String? searchQuery, required int currentPage}) async {
|
|
var ack = await io.emit(Event(Events.roomGet, {
|
|
"pagination": {"page": currentPage},
|
|
if (searchQuery != null) "q": searchQuery
|
|
}));
|
|
return Room.fromJsonList(ack);
|
|
}
|
|
|
|
Future<Room> createRoom({required int id}) async {
|
|
var ack = await io.emit(
|
|
Event(Events.roomCreate, {
|
|
"peer": {"id": id}
|
|
}),
|
|
reciveDataOnError: true);
|
|
print('my ack is $ack');
|
|
if (ack['error'] != null &&
|
|
ack['error']['msg'] == 'The resource already exists') {
|
|
print(ack['error']['justification'].toString().substring(51, 53));
|
|
int roomId = ack['error']['meta']['roomId'];
|
|
return Room(
|
|
id: roomId,
|
|
user: ChatUser.zero(),
|
|
lastMessageDate: DateTime.now().toLocal());
|
|
}
|
|
return Room.fromJson(ack);
|
|
}
|
|
|
|
Future<List<MessageModel>> getCurrentRoomMessages(
|
|
{required int roomId, required int page}) async {
|
|
Map<String, Map<String, int>> body = {
|
|
"room": {"id": roomId},
|
|
"pagination": {"page": page}
|
|
};
|
|
var ack = await io.emit(Event(Events.messageGet, body));
|
|
return MessageModel.fromJsonList(ack);
|
|
}
|
|
|
|
Future<void> blockRoom({required int id}) async {
|
|
Map<String, dynamic> body = {
|
|
"room": {"id": id}
|
|
};
|
|
|
|
await io.emit(Event(Events.roomBlock, body));
|
|
}
|
|
|
|
Future<void> unblockRoom({required int id}) async {
|
|
Map<String, dynamic> body = {
|
|
"room": {"id": id}
|
|
};
|
|
await io.emit(Event(Events.roomUnblock, body));
|
|
}
|
|
|
|
//support
|
|
Future<Room?> checkSupportRoomStatus() async {
|
|
var ack = await io.emit(Event(Events.supportRoomStatus));
|
|
|
|
if (ack.isEmpty) {
|
|
return null;
|
|
}
|
|
return Room.fromJson(ack[0]);
|
|
}
|
|
|
|
Future<Room> createSupportRoom() async {
|
|
var ack = await io.emit(Event(Events.supportRoomCreate));
|
|
|
|
return Room.fromJson(ack);
|
|
}
|
|
|
|
/// ------------ messages ------------ ///
|
|
Future<MessageModel> sendMessage(
|
|
{required MessageModel messageModel, required int roomId}) async {
|
|
Map<String, dynamic> ack = await io.emit(Event(Events.messageCreate, {
|
|
"room": {"id": roomId},
|
|
"message": messageModel.toJson(),
|
|
}));
|
|
return MessageModel.fromJson(ack);
|
|
}
|
|
|
|
Future<void> readMessage({required int messageId}) async {
|
|
await io.emit(Event(Events.messageRead, {
|
|
"message": {"id": messageId}
|
|
}));
|
|
}
|
|
}
|