Remove methods to set the default thread

This has been done to value explicitness over implicitness, and also
since the question of whether thread_id=None is acceptable was dependent
on mutable variables in Client.
This commit is contained in:
Mads Marquart
2019-08-30 17:48:05 +02:00
parent c70a39c568
commit 7c758501fc
3 changed files with 4 additions and 93 deletions

View File

@@ -87,8 +87,6 @@ class Client:
"""
self._sticky, self._pool = (None, None)
self._seq = "0"
self._default_thread_id = None
self._default_thread_type = None
self._pull_channel = 0
self._markAlive = True
self._buddylist = dict()
@@ -235,45 +233,6 @@ class Client:
END LOGIN METHODS
"""
"""
DEFAULT THREAD METHODS
"""
def _getThread(self, given_thread_id=None, given_thread_type=None):
"""Check if thread ID is given and if default is set, and return correct values.
Returns:
tuple: Thread ID and thread type
Raises:
ValueError: If thread ID is not given and there is no default
"""
if given_thread_id is None:
if self._default_thread_id is not None:
return self._default_thread_id, self._default_thread_type
else:
raise ValueError("Thread ID is not set")
else:
return given_thread_id, given_thread_type
def setDefaultThread(self, thread_id, thread_type):
"""Set default thread to send messages to.
Args:
thread_id: User/Group ID to default to. See :ref:`intro_threads`
thread_type (ThreadType): See :ref:`intro_threads`
"""
self._default_thread_id = thread_id
self._default_thread_type = thread_type
def resetDefaultThread(self):
"""Reset default thread."""
self.setDefaultThread(None, None)
"""
END DEFAULT THREAD METHODS
"""
"""
FETCH METHODS
"""
@@ -494,8 +453,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {
"query": query,
"snippetOffset": offset,
@@ -756,8 +713,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
params = {
"id": thread_id,
"message_limit": limit,
@@ -902,7 +857,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
message_info = self._forcedFetch(thread_id, mid).get("message")
return Message._from_graphql(message_info)
@@ -987,7 +941,6 @@ class Client:
Returns:
typing.Iterable: :class:`ImageAttachment` or :class:`VideoAttachment`
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"id": thread_id, "first": 48}
thread_id = str(thread_id)
j, = self.graphql_requests(_graphql.from_query_id("515216185516880", data))
@@ -1049,7 +1002,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data.update(message._to_send_data())
@@ -1069,7 +1021,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data["action_type"] = "ma-type:user-generated-message"
@@ -1134,7 +1085,6 @@ class Client:
def _sendLocation(
self, location, current=True, message=None, thread_id=None, thread_type=None
):
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
if message is not None:
@@ -1203,7 +1153,6 @@ class Client:
`files` should be a list of tuples, with a file's ID and mimetype.
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data.update(self._oldMessage(message)._to_send_data())
@@ -1319,7 +1268,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {
"attachment_id": attachment_id,
"recipient_map[{}]".format(_util.generateOfflineThreadingID()): thread_id,
@@ -1369,7 +1317,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = Group(thread_id)._to_send_data()
data["action_type"] = "ma-type:log-message"
@@ -1399,14 +1346,10 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"uid": user_id, "tid": thread_id}
j = self._payload_post("/chat/remove_participants/", data)
def _adminStatus(self, admin_ids, admin, thread_id=None):
thread_id, thread_type = self._getThread(thread_id, None)
data = {"add": admin, "thread_fbid": thread_id}
admin_ids = _util.require_list(admin_ids)
@@ -1450,14 +1393,10 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"set_mode": int(require_admin_approval), "thread_fbid": thread_id}
j = self._payload_post("/messaging/set_approval_mode/?dpr=1", data)
def _usersApproval(self, user_ids, approve, thread_id=None):
thread_id, thread_type = self._getThread(thread_id, None)
user_ids = _util.require_list(user_ids)
data = {
@@ -1506,8 +1445,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"thread_image_id": image_id, "thread_id": thread_id}
j = self._payload_post("/messaging/set_thread_image/?dpr=1", data)
@@ -1555,8 +1492,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
if thread_type == ThreadType.USER:
# The thread is a user, so we change the user's nickname
return self.changeNickname(
@@ -1580,8 +1515,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
data = {
"nickname": nickname,
"participant_id": user_id,
@@ -1601,8 +1534,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {
"color_choice": color.value if color != ThreadColor.MESSENGER_BLUE else "",
"thread_or_other_fbid": thread_id,
@@ -1625,8 +1556,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"emoji_choice": emoji, "thread_or_other_fbid": thread_id}
j = self._payload_post(
"/messaging/save_thread_emoji/?source=thread_settings&dpr=1", data
@@ -1663,8 +1592,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {
"event_type": "EVENT",
"event_time": _util.datetime_to_seconds(plan.time),
@@ -1741,8 +1668,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
# We're using ordered dictionaries, because the Facebook endpoint that parses
# the POST parameters is badly implemented, and deals with ordering the options
# wrongly. If you can find a way to fix this for the endpoint, or if you find
@@ -1799,8 +1724,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, thread_type)
data = {
"typ": status.value,
"thread": thread_id,
@@ -2012,7 +1935,6 @@ class Client:
Raises:
FBchatException: If request failed
"""
thread_id, thread_type = self._getThread(thread_id, None)
j = self._payload_post("/ajax/mercury/mark_spam.php?dpr=1", {"id": thread_id})
return True
@@ -2042,7 +1964,6 @@ class Client:
mute_time (datetime.timedelta): Time to mute, use ``None`` to mute forever
thread_id: User/Group ID to mute. See :ref:`intro_threads`
"""
thread_id, thread_type = self._getThread(thread_id, None)
if mute_time is None:
mute_settings = -1
else:
@@ -2065,7 +1986,6 @@ class Client:
mute: Boolean. True to mute, False to unmute
thread_id: User/Group ID to mute. See :ref:`intro_threads`
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"reactions_mute_mode": int(mute), "thread_fbid": thread_id}
j = self._payload_post(
"/ajax/mercury/change_reactions_mute_thread/?dpr=1", data
@@ -2086,7 +2006,6 @@ class Client:
mute: Boolean. True to mute, False to unmute
thread_id: User/Group ID to mute. See :ref:`intro_threads`
"""
thread_id, thread_type = self._getThread(thread_id, None)
data = {"mentions_mute_mode": int(mute), "thread_fbid": thread_id}
j = self._payload_post("/ajax/mercury/change_mentions_mute_thread/?dpr=1", data)

View File

@@ -45,9 +45,11 @@ def client2(pytestconfig):
@pytest.fixture(scope="module")
def client(client1, thread):
client1.setDefaultThread(thread["id"], thread["type"])
# TODO: These are commented out since `setDefaultThread` is removed - But now any
# test that use this fixture is likely broken!
# client1.setDefaultThread(thread["id"], thread["type"])
yield client1
client1.resetDefaultThread()
# client1.resetDefaultThread()
@pytest.fixture(scope="session", params=["client1", "client2"])

View File

@@ -38,13 +38,3 @@ def test_sessions(client1):
Client("no email needed", "no password needed", session_cookies=session)
client1.setSession(session)
assert client1.isLoggedIn()
@pytest.mark.tryfirst
def test_default_thread(client1, thread):
client1.setDefaultThread(thread["id"], thread["type"])
assert client1.send(Message(text="Sent to the specified thread"))
client1.resetDefaultThread()
with pytest.raises(ValueError):
client1.send(Message(text="Should not be sent"))