Skip to content

Commit

Permalink
feat(admin): suspend users integration. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan2232004 authored Dec 20, 2024
1 parent a244b73 commit a62fee2
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 136 deletions.
5 changes: 4 additions & 1 deletion lib/core/theme/palette.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ class Palette {
static const Color black = Color.fromRGBO(0, 0, 0, 1);
static const Color drawerHeader = Color.fromRGBO(34, 45, 58, 1);
static const Color valid = Color.fromRGBO(76, 175, 80, 1.0);
}
static const Color banned = Color.fromRGBO(238, 104, 111, 1);
static const Color deactivated = Color.fromRGBO(255, 152, 0, 1);
static const Color active = Color.fromRGBO(76, 175, 80, 1.0);
}
118 changes: 0 additions & 118 deletions lib/core/view/widget/link_text_field.dart

This file was deleted.

34 changes: 34 additions & 0 deletions lib/features/chat/repository/chat_remote_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,39 @@ class ChatRemoteRepository {
}
}

Future<({AppError? appError})> filterGroup(
String sessionID, String chatID) async {
try {
final response = await _dio.patch(
'/groups/filter/:$chatID',
options: Options(headers: {'X-Session-Token': sessionID}),
);

debugPrint("message: ${response.data['message']}");

return (appError: null);
} catch (e) {
debugPrint('!!! Failed to filter $chatID, ${e.toString()}');
return (appError: AppError('Failed to filter', code: 500));
}
}

Future<({AppError? appError})> unFilterGroup(
String sessionID, String chatID) async {
try {
final response = await _dio.patch(
'/groups/unfilter/:$chatID',
options: Options(headers: {'X-Session-Token': sessionID}),
);

debugPrint("message: ${response.data['message']}");

return (appError: null);
} catch (e) {
debugPrint('!!! Failed to unFilter $chatID, ${e.toString()}');
return (appError: AppError('Failed to unFilter', code: 500));
}
}

// TODO Implement Fetch Messages
}
54 changes: 42 additions & 12 deletions lib/features/chat/view/screens/create_chat_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,22 @@ class _CreateChatScreen extends ConsumerState<CreateChatScreen>
];
for (UserModel user in users) {
if (user.id == myUser.id) continue;

Color subtextColor = Palette.accentText;

if (user.accountStatus == 'banned') {
subtextColor = Palette.banned;
} else if (user.accountStatus == 'deactivated') {
subtextColor = Palette.deactivated;
} else if (user.accountStatus == 'active') {
subtextColor = Palette.active;
}
var option = <String, dynamic>{
"avatar": true,
"text": user.username,
"imagePath": null,
"subtext": "last seen Nov 23 at 6:40 PM",
"subtext": user.accountStatus,
'subtextColor': subtextColor,
"trailingFontSize": 13.0,
"trailingPadding": const EdgeInsets.only(bottom: 20.0),
"trailingColor": Palette.accentText,
Expand Down Expand Up @@ -632,19 +643,38 @@ class _CreateChatScreen extends ConsumerState<CreateChatScreen>
confirmPadding: const EdgeInsets.only(left: 40.0),
cancelText: 'Cancel',
cancelColor: const Color.fromRGBO(100, 181, 239, 1),
onConfirm: () {
// TODO(marwan) ban the user
// if (userId != null) {
// ref.read(userViewModelProvider.notifier).blockUser(userId: userId);
// }

// Close the confirmation dialog
context.pop();
// Close the initial dialog
context.pop();
onConfirm: () async {
if (userId != null) {
if (action == 'BAN') {
await ref
.read(userViewModelProvider.notifier)
.banUser(userId: userId);
} else if (action == 'ACTIVATE') {
await ref
.read(userViewModelProvider.notifier)
.activateUser(userId: userId);
} else if (action == 'DEACTIVATE') {
await ref
.read(userViewModelProvider.notifier)
.deactivateUser(userId: userId);
}
ref.read(userViewModelProvider.notifier).fetchUsers().then((users) {
fullUserChats = <Map<String, dynamic>>[
{"options": <Map<String, dynamic>>[]}
];
setState(() {
userChats = _generateUsersList(users, false);
});
});
}
if (mounted) {
// Close the confirmation dialog
context.pop();
// Close the initial dialog
context.pop();
}
},
onCancel: () {
// TODO(marwan) activate or deactivate the user
context.pop();
context.pop();
},
Expand Down
31 changes: 26 additions & 5 deletions lib/features/chat/view_model/chatting_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ class ChattingController {
parentMessageId: parentMessgeId,
);


_localRepository.setChats(
_ref.read(chatsViewModelProvider), _ref.read(userProvider)!.id!);

Expand Down Expand Up @@ -346,10 +345,7 @@ class ChattingController {
: msgGlobalId = msgId;

final msgEvent = DeleteMessageEvent(
{
'messageId': msgId,
'chatId': chatId
},
{'messageId': msgId, 'chatId': chatId},
controller: this,
msgId: msgId,
chatId: msgGlobalId,
Expand Down Expand Up @@ -795,4 +791,29 @@ class ChattingController {
_eventHandler.addEvent(msgEvent);
return true;
}

Future<void> filterGroup({
required String chatId,
required String filter,
}) async {
final sessionID = _ref.read(tokenProvider);
final response = await _remoteRepository.filterGroup(sessionID!, chatId);
if (response.appError != null) {
debugPrint('Error: Could not filter the group');
} else {
debugPrint('!!! group filtered');
}
}

Future<void> unFilterGroup({
required String chatId,
}) async {
final sessionID = _ref.read(tokenProvider);
final response = await _remoteRepository.unFilterGroup(sessionID!, chatId);
if (response.appError != null) {
debugPrint('Error: Could not unfilter the group');
} else {
debugPrint('!!! group unfiltered');
}
}
}
66 changes: 66 additions & 0 deletions lib/features/user/repository/user_remote_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,72 @@ class UserRemoteRepository {
}
}

Future<Either<AppError, void>> banUser({
required String sessionID,
required String userID,
}) async {
try {
final response = await _dio.patch(
'/users/ban/$userID',
options: Options(headers: {'X-Session-Token': sessionID}),
);

debugPrint("message: ${response.data['message']}");

return const Right(null);
} on DioException catch (dioException) {
return Left(handleDioException(dioException));
} catch (error) {
debugPrint('!!! Failed to ban $userID, ${error.toString()}');
return Left(AppError("Failed to ban user. Please, try again later."));
}
}

Future<Either<AppError, void>> activateUser({
required String sessionID,
required String userID,
}) async {
debugPrint('Activating user $userID');
try {
final response = await _dio.patch(
'/users/activate/$userID',
options: Options(headers: {'X-Session-Token': sessionID}),
);

debugPrint("message: ${response.data['message']}");

return const Right(null);
} on DioException catch (dioException) {
return Left(handleDioException(dioException));
} catch (error) {
debugPrint('!!! Failed to activate $userID, ${error.toString()}');
return Left(
AppError("Failed to activate user. Please, try again later."));
}
}

Future<Either<AppError, void>> deactivateUser({
required String sessionID,
required String userID,
}) async {
try {
final response = await _dio.patch(
'/users/deactivate/$userID',
options: Options(headers: {'X-Session-Token': sessionID}),
);

debugPrint("message: ${response.data['message']}");

return const Right(null);
} on DioException catch (dioException) {
return Left(handleDioException(dioException));
} catch (error) {
debugPrint('!!! Failed to deactivate $userID, ${error.toString()}');
return Left(
AppError("Failed to deactivate user. Please, try again later."));
}
}

AppError handleDioException(DioException dioException) {
String? message;
if (dioException.response != null) {
Expand Down
Loading

0 comments on commit a62fee2

Please sign in to comment.