Skip to content

Commit

Permalink
Merge branch 'migration/v2' of github.com:waterbustech/waterbus-flutt…
Browse files Browse the repository at this point in the history
…er-sdk into migration/v2
  • Loading branch information
lambiengcode committed Oct 26, 2024
2 parents f3100b7 + 4800b67 commit c41f947
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 29 deletions.
2 changes: 2 additions & 0 deletions lib/constants/api_enpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class ApiEndpoints {
static const String joinWithPassword = 'meetings/join/password';
static const String joinWithoutPassword = 'meetings/join';
static const String meetingConversations = 'meetings/conversations';
static const String archivedConversations = 'meetings/conversations/archived';
static const String meetingMembers = 'meetings/members';
static const String archivedMeeeting = 'meetings/archived';
static const String acceptInvite = 'meetings/members/accept';
static const String records = 'meetings/records';
static const String startRecord = 'meetings/record/start';
Expand Down
88 changes: 80 additions & 8 deletions lib/core/api/chat/datasources/chat_remote_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ abstract class ChatRemoteDataSource {
required int limit,
required int status,
});
Future<List<Meeting>> getArchivedConversations({
required int skip,
required int limit,
});
Future<bool> deleteConversation({required int meetingId});
Future<Meeting?> archivedConversation({required int code});
Future<Meeting?> leaveConversation({required int code});
Future<Meeting?> addMember({required int code, required int userId});
Future<Meeting?> deleteMember({required int code, required int userId});
Future<Meeting?> acceptInvite({required int meetingId});
Future<bool> updateConversation({required Meeting meeting});
Future<bool> updateConversation({
required Meeting meeting,
String? password,
});
}

@LazySingleton(as: ChatRemoteDataSource)
Expand Down Expand Up @@ -55,6 +63,30 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
return [];
}

@override
Future<List<Meeting>> getArchivedConversations({
required int skip,
required int limit,
}) async {
final Response response = await _remoteData.getRoute(
ApiEndpoints.archivedConversations,
query: "limit=$limit&skip=$skip",
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
final Map<String, dynamic> message = {
"conversations": (response.data as List)
.map((meeting) => Meeting.fromMap(meeting))
.toList(),
"key": WaterbusSdk.privateMessageKey,
};

return await compute(_handleDecryptLastMessage, message);
}

return [];
}

static Future<List<Meeting>> _handleDecryptLastMessage(
Map<String, dynamic> map,
) async {
Expand Down Expand Up @@ -83,10 +115,13 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
}

@override
Future<bool> updateConversation({required Meeting meeting}) async {
Future<bool> updateConversation({
required Meeting meeting,
String? password,
}) async {
final Response response = await _remoteData.putRoute(
ApiEndpoints.meetings,
meeting.toMapCreate(),
meeting.toMapCreate(password: password),
);

return response.statusCode == StatusCode.ok;
Expand All @@ -108,8 +143,12 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);

if (response.statusCode == StatusCode.ok) {
final Map<String, dynamic> rawData = response.data;
return Meeting.fromMap(rawData);
final Map<String, dynamic> message = {
"conversations": [Meeting.fromMap(response.data)],
"key": WaterbusSdk.privateMessageKey,
};

return (await compute(_handleDecryptLastMessage, message)).first;
}

return null;
Expand All @@ -122,7 +161,12 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
return Meeting.fromMap(response.data);
final Map<String, dynamic> message = {
"conversations": [Meeting.fromMap(response.data)],
"key": WaterbusSdk.privateMessageKey,
};

return (await compute(_handleDecryptLastMessage, message)).first;
}

return null;
Expand All @@ -136,7 +180,12 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
return Meeting.fromMap(response.data);
final Map<String, dynamic> message = {
"conversations": [Meeting.fromMap(response.data)],
"key": WaterbusSdk.privateMessageKey,
};

return (await compute(_handleDecryptLastMessage, message)).first;
}

return null;
Expand All @@ -153,7 +202,30 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
return Meeting.fromMap(response.data);
final Map<String, dynamic> message = {
"conversations": [Meeting.fromMap(response.data)],
"key": WaterbusSdk.privateMessageKey,
};

return (await compute(_handleDecryptLastMessage, message)).first;
}

return null;
}

@override
Future<Meeting?> archivedConversation({required int code}) async {
final Response response = await _remoteData.postRoute(
'${ApiEndpoints.archivedMeeeting}/$code',
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
final Map<String, dynamic> message = {
"conversations": [Meeting.fromMap(response.data)],
"key": WaterbusSdk.privateMessageKey,
};

return (await compute(_handleDecryptLastMessage, message)).first;
}

return null;
Expand Down
39 changes: 37 additions & 2 deletions lib/core/api/chat/repositories/chat_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ abstract class ChatRepository {
required int limit,
required int skip,
});
Future<bool> updateConversation({required Meeting meeting});
Future<List<Meeting>> getArchivedConversations({
required int skip,
required int limit,
});
Future<bool> updateConversation({
required Meeting meeting,
String? password,
});
Future<bool> deleteConversation(int meetingId);
Future<Meeting?> leaveConversation({required int code});
Future<Meeting?> addMember({required int code, required int userId});
Future<Meeting?> deleteMember({required int code, required int userId});
Future<Meeting?> acceptInvite({required int meetingId});
Future<Meeting?> archivedConversation({required int code});
}

@LazySingleton(as: ChatRepository)
Expand All @@ -41,6 +49,20 @@ class ChatRepositoryImpl extends ChatRepository {
return conversations;
}

@override
Future<List<Meeting>> getArchivedConversations({
required limit,
required skip,
}) async {
final List<Meeting> archivedConversations =
await _remoteDataSource.getArchivedConversations(
skip: skip,
limit: limit,
);

return archivedConversations;
}

@override
Future<bool> deleteConversation(int meetingId) async {
final bool isSucceed = await _remoteDataSource.deleteConversation(
Expand Down Expand Up @@ -92,11 +114,24 @@ class ChatRepositoryImpl extends ChatRepository {
}

@override
Future<bool> updateConversation({required Meeting meeting}) async {
Future<bool> updateConversation({
required Meeting meeting,
String? password,
}) async {
final bool isSucceed = await _remoteDataSource.updateConversation(
meeting: meeting,
password: password,
);

return isSucceed;
}

@override
Future<Meeting?> archivedConversation({required int code}) async {
final Meeting? meeting = await _remoteDataSource.archivedConversation(
code: code,
);

return meeting;
}
}
24 changes: 22 additions & 2 deletions lib/flutter_waterbus_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ class WaterbusSdk {
return await _sdk.leaveConversation(code: code);
}

Future<Meeting?> archivedConversation(int code) async {
return await _sdk.archivedConversation(code: code);
}

Future<bool> deleteConversation(int conversationId) async {
return await _sdk.deleteConversation(conversationId);
}
Expand All @@ -308,8 +312,24 @@ class WaterbusSdk {
);
}

Future<bool> updateConversation({required Meeting meeting}) async {
return await _sdk.updateConversation(meeting: meeting);
Future<List<Meeting>> getArchivedConversations({
required int skip,
int limit = 10,
}) async {
return await _sdk.getArchivedConversations(
limit: limit,
skip: skip,
);
}

Future<bool> updateConversation({
required Meeting meeting,
String? password,
}) async {
return await _sdk.updateConversation(
meeting: meeting,
password: password,
);
}

// Messages
Expand Down
15 changes: 7 additions & 8 deletions lib/types/models/chat_status_enum.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import 'package:collection/collection.dart';

enum ChatStatusEnum {
invite(0),
enum MeetingStatus {
archived(1),
join(2);
active(0);

const ChatStatusEnum(this.status);
const MeetingStatus(this.status);

final int status;
}

extension ChatStatusEnumX on int {
ChatStatusEnum get getChatStatusEnum {
return ChatStatusEnum.values
extension MeetingStatusX on int {
MeetingStatus get getMeetingStatusEnum {
return MeetingStatus.values
.firstWhereOrNull((status) => status.status == this) ??
ChatStatusEnum.join;
MeetingStatus.active;
}
}
13 changes: 7 additions & 6 deletions lib/types/models/meeting_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Meeting {
final int code;
final DateTime? createdAt;
final DateTime? latestJoinedAt;
final ChatStatusEnum status;
final MeetingStatus status;
final String? avatar;
MessageModel? latestMessage;

Expand All @@ -25,7 +25,7 @@ class Meeting {
this.code = -1,
this.createdAt,
this.latestJoinedAt,
this.status = ChatStatusEnum.join,
this.status = MeetingStatus.active,
this.latestMessage,
this.avatar,
});
Expand All @@ -38,7 +38,7 @@ class Meeting {
int? code,
DateTime? createdAt,
DateTime? latestJoinedAt,
ChatStatusEnum? status,
MeetingStatus? status,
MessageModel? latestMessage,
String? avatar,
}) {
Expand Down Expand Up @@ -95,7 +95,8 @@ class Meeting {
.map<Member>((member) => Member.fromMap(member))
.toList()
: [],
status: (int.tryParse(map['status'].toString()) ?? 0).getChatStatusEnum,
status: (int.tryParse(map['status']?.toString() ?? "0") ?? 0)
.getMeetingStatusEnum,
createdAt:
DateTime.fromMillisecondsSinceEpoch(int.parse(map['createdAt']))
.toLocal(),
Expand Down Expand Up @@ -123,8 +124,8 @@ class Meeting {
.toList()
: [],
code: map['code'] ?? 0,
status: (int.tryParse(map['status']?.toString() ?? "") ?? 0)
.getChatStatusEnum,
status: (int.tryParse(map['status']?.toString() ?? "0") ?? 0)
.getMeetingStatusEnum,
createdAt: DateTime.parse(map['createdAt']).toLocal(),
latestJoinedAt:
DateTime.parse(map['latestJoinedAt'] ?? map['createdAt'])
Expand Down
26 changes: 24 additions & 2 deletions lib/waterbus_sdk_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,25 @@ class SdkCore extends WaterbusSdkInterface {
}

@override
Future<bool> updateConversation({required Meeting meeting}) async {
return await _chatRepository.updateConversation(meeting: meeting);
Future<List<Meeting>> getArchivedConversations({
int limit = 10,
required int skip,
}) async {
return await _chatRepository.getArchivedConversations(
limit: limit,
skip: skip,
);
}

@override
Future<bool> updateConversation({
required Meeting meeting,
String? password,
}) async {
return await _chatRepository.updateConversation(
meeting: meeting,
password: password,
);
}

@override
Expand All @@ -354,6 +371,11 @@ class SdkCore extends WaterbusSdkInterface {
return await _chatRepository.leaveConversation(code: code);
}

@override
Future<Meeting?> archivedConversation({required int code}) async {
return await _chatRepository.archivedConversation(code: code);
}

@override
Future<Meeting?> deleteMember({
required int code,
Expand Down
10 changes: 9 additions & 1 deletion lib/waterbus_sdk_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ abstract class WaterbusSdkInterface {
int limit = 10,
required int skip,
});
Future<bool> updateConversation({required Meeting meeting});
Future<List<Meeting>> getArchivedConversations({
int limit = 10,
required int skip,
});
Future<bool> updateConversation({
required Meeting meeting,
String? password,
});
Future<bool> deleteConversation(int conversationId);
Future<Meeting?> leaveConversation({required int code});
Future<Meeting?> addMember({required int code, required int userId});
Future<Meeting?> deleteMember({required int code, required int userId});
Future<Meeting?> acceptInvite({required int meetingId});
Future<Meeting?> archivedConversation({required int code});

// Messages
Future<List<MessageModel>> getMessageByRoom({
Expand Down

0 comments on commit c41f947

Please sign in to comment.