From 42b288ee98d383dcaf6f5ec24b27b61da2c54104 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 29 Aug 2018 11:15:59 +0200 Subject: [PATCH] Fixed `onAdminRemoved` and `onAdminAdded`, and added tests for that --- fbchat/client.py | 22 +++++++++++----------- tests/test_thread_interraction.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index d4fa991..d9f732c 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -2090,10 +2090,10 @@ class Client(object): target_id = delta["untypedData"]["TARGET_ID"] admin_event = delta["untypedData"]["ADMIN_EVENT"] if admin_event == "add_admin": - self.onAdminsAdded(mid=mid, added_id=target_id, author_id=author_id, thread_id=thread_id, + self.onAdminAdded(mid=mid, added_id=target_id, author_id=author_id, thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m) elif admin_event == "remove_admin": - self.onAdminsRemoved(mid=mid, removed_id=target_id, author_id=author_id, thread_id=thread_id, + self.onAdminRemoved(mid=mid, removed_id=target_id, author_id=author_id, thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m) # Group approval mode change @@ -2537,35 +2537,35 @@ class Client(object): log.info("Nickname change from {} in {} ({}) for {}: {}".format(author_id, thread_id, thread_type.name, changed_for, new_nickname)) - def onAdminsAdded(self, mid=None, added_ids=None, author_id=None, thread_id=None, thread_type=ThreadType.USER, ts=None, msg=None): + def onAdminAdded(self, mid=None, added_id=None, author_id=None, thread_id=None, thread_type=ThreadType.GROUP, ts=None, msg=None): """ - Called when the client is listening, and somebody adds admins to a group thread + Called when the client is listening, and somebody adds an admin to a group thread :param mid: The action ID - :param added_ids: The IDs of the admins who got added + :param added_id: The ID of the admin who got added :param author_id: The ID of the person who added the admins :param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads` :param ts: A timestamp of the action :param msg: A full set of the data recieved """ - log.info("{} added admins: {} in {}".format(author_id, ', '.join(added_ids), thread_id)) + log.info("{} added admin: {} in {}".format(author_id, added_id, thread_id)) - def onAdminsRemoved(self, mid=None, removed_ids=None, author_id=None, thread_id=None, thread_type=ThreadType.USER, ts=None, msg=None): + def onAdminRemoved(self, mid=None, removed_id=None, author_id=None, thread_id=None, thread_type=ThreadType.GROUP, ts=None, msg=None): """ - Called when the client is listening, and somebody removes admins from a group thread + Called when the client is listening, and somebody removes an admin from a group thread :param mid: The action ID - :param added_ids: The IDs of the admins who got removed + :param removed_id: The ID of the admin who got removed :param author_id: The ID of the person who removed the admins :param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads` :param ts: A timestamp of the action :param msg: A full set of the data recieved """ - log.info("{} removed admins: {} in {}".format(author_id, ', '.join(removed_ids), thread_id)) + log.info("{} removed admin: {} in {}".format(author_id, removed_id, thread_id)) - def onApprovalModeChange(self, mid=None, approval_mode=None, author_id=None, thread_id=None, thread_type=ThreadType.USER, ts=None, msg=None): + def onApprovalModeChange(self, mid=None, approval_mode=None, author_id=None, thread_id=None, thread_type=ThreadType.GROUP, ts=None, msg=None): """ Called when the client is listening, and somebody changes approval mode in a group thread diff --git a/tests/test_thread_interraction.py b/tests/test_thread_interraction.py index e897dbb..0732714 100644 --- a/tests/test_thread_interraction.py +++ b/tests/test_thread_interraction.py @@ -31,6 +31,22 @@ def test_remove_from_and_add_to_group(client1, client2, group, catch_event): ) +def test_remove_from_and_add_admins_to_group(client1, client2, group, catch_event): + # Test both methods, while ensuring that the user gets added as group admin + try: + with catch_event("onAdminRemoved") as x: + client1.removeGroupAdmins(client2.uid, group["id"]) + assert subset( + x.res, removed_id=client2.uid, author_id=client1.uid, thread_id=group["id"] + ) + finally: + with catch_event("onAdminAdded") as x: + client1.addGroupAdmins(client2.uid, group["id"]) + assert subset( + x.res, added_id=client2.uid, author_id=client1.uid, thread_id=group["id"] + ) + + @pytest.mark.xfail( raises=FBchatFacebookError, reason="Apparently changeThreadTitle is broken" )