diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 22f5154..53dc45a 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -67,6 +67,16 @@ class ConversationsHelper { .addInt("userID", userID) .execWithThrow(); + /// Update admin status of a user in a conversation + /// + /// Throws in case of failure + static Future setAdmin(int convID, int userID, bool admin) async => + await APIRequest.withLogin("conversations/setAdmin") + .addInt("convID", convID) + .addInt("userID", userID) + .addBool("setAdmin", admin) + .execWithThrow(); + /// Update an existing conversation /// /// Throws in case of failure diff --git a/lib/models/conversation.dart b/lib/models/conversation.dart index 481eaeb..220be60 100644 --- a/lib/models/conversation.dart +++ b/lib/models/conversation.dart @@ -66,6 +66,9 @@ class Conversation extends SerializableElement { /// Check if the last message has been seen or not bool get sawLastMessage => lastActivity <= membership.lastAccessTime; + /// Check out whether a conversation is managed or not + bool get isManaged => groupID != null; + Conversation.fromJson(Map map) : id = map["id"], name = map["name"], diff --git a/lib/ui/screens/update_conversation_screen.dart b/lib/ui/screens/update_conversation_screen.dart index 0086733..2c0bf5a 100644 --- a/lib/ui/screens/update_conversation_screen.dart +++ b/lib/ui/screens/update_conversation_screen.dart @@ -63,7 +63,9 @@ class _UpdateConversationScreen extends State { get isAdmin => !isUpdating || _conversation.isAdmin; - bool get _canAddMembers => isAdmin || _conversation.canEveryoneAddMembers; + bool get _canAddMembers => + (isAdmin || _conversation.canEveryoneAddMembers) && + (!isUpdating || !_conversation.isManaged); get _isValid => _members.length > 0; @@ -221,7 +223,7 @@ class _UpdateConversationScreen extends State { break; case _MembersMenuChoices.TOGGLE_ADMIN_STATUS: - // TODO: Handle this case. + _toggleAdminStatus(user); break; } } @@ -255,6 +257,23 @@ class _UpdateConversationScreen extends State { } } + void _toggleAdminStatus(User user) async { + try { + final setAdmin = !_admins.contains(user.id); + await ConversationsHelper.setAdmin(_conversation.id, user.id, setAdmin); + + setState(() { + if (!setAdmin) + _admins.remove(user.id); + else + _admins.add(user.id); + }); + } catch (e, s) { + logError(e, s); + snack(context, tr("Failed to toggle admin status of user!")); + } + } + /// Submit the conversation Future _submitForm() async { try {