Small refactoring

The `muteX` methods return values are now checked using `check_request`, `seq` is now parsed in `_parseMessage` and a few other things.
This commit is contained in:
Mads Marquart
2019-03-31 00:16:36 +01:00
parent bc27f756ed
commit 1eeae78a9f

View File

@@ -850,17 +850,12 @@ class Client(object):
result = j["payload"]["search_snippets"][query] result = j["payload"]["search_snippets"][query]
if fetch_messages: if fetch_messages:
return { search_method = self.searchForMessages
thread_id: self.searchForMessages(
query, limit=message_limit, thread_id=thread_id
)
for thread_id in result
}
else: else:
search_method = self.searchForMessageIDs
return { return {
thread_id: self.searchForMessageIDs( thread_id: search_method(query, limit=message_limit, thread_id=thread_id)
query, limit=message_limit, thread_id=thread_id
)
for thread_id in result for thread_id in result
} }
@@ -915,11 +910,11 @@ class Client(object):
""" """
threads = self.fetchThreadInfo(*user_ids) threads = self.fetchThreadInfo(*user_ids)
users = {} users = {}
for k in threads: for id_, thread in threads.items():
if threads[k].type == ThreadType.USER: if thread.type == ThreadType.USER:
users[k] = threads[k] users[id_] = thread
else: else:
raise FBchatUserError("Thread {} was not a user".format(threads[k])) raise FBchatUserError("Thread {} was not a user".format(thread))
return users return users
@@ -937,11 +932,11 @@ class Client(object):
""" """
threads = self.fetchThreadInfo(*page_ids) threads = self.fetchThreadInfo(*page_ids)
pages = {} pages = {}
for k in threads: for id_, thread in threads.items():
if threads[k].type == ThreadType.PAGE: if thread.type == ThreadType.PAGE:
pages[k] = threads[k] pages[id_] = thread
else: else:
raise FBchatUserError("Thread {} was not a page".format(threads[k])) raise FBchatUserError("Thread {} was not a page".format(thread))
return pages return pages
@@ -956,11 +951,11 @@ class Client(object):
""" """
threads = self.fetchThreadInfo(*group_ids) threads = self.fetchThreadInfo(*group_ids)
groups = {} groups = {}
for k in threads: for id_, thread in threads.items():
if threads[k].type == ThreadType.GROUP: if thread.type == ThreadType.GROUP:
groups[k] = threads[k] groups[id_] = thread
else: else:
raise FBchatUserError("Thread {} was not a group".format(threads[k])) raise FBchatUserError("Thread {} was not a group".format(thread))
return groups return groups
@@ -1662,15 +1657,11 @@ class Client(object):
Deprecated. Use :func:`fbchat.Client._sendFiles` instead Deprecated. Use :func:`fbchat.Client._sendFiles` instead
""" """
if is_gif: if is_gif:
return self._sendFiles( mimetype = "image/gif"
files=[(image_id, "image/png")],
message=message,
thread_id=thread_id,
thread_type=thread_type,
)
else: else:
mimetype = "image/png"
return self._sendFiles( return self._sendFiles(
files=[(image_id, "image/gif")], files=[(image_id, mimetype)],
message=message, message=message,
thread_id=thread_id, thread_id=thread_id,
thread_type=thread_type, thread_type=thread_type,
@@ -2329,8 +2320,7 @@ class Client(object):
""" """
thread_id, thread_type = self._getThread(thread_id, None) thread_id, thread_type = self._getThread(thread_id, None)
data = {"mute_settings": str(mute_time), "thread_fbid": thread_id} data = {"mute_settings": str(mute_time), "thread_fbid": thread_id}
r = self._post(self.req_url.MUTE_THREAD, data) content = self._post(self.req_url.MUTE_THREAD, data, fix_request=True)
r.raise_for_status()
def unmuteThread(self, thread_id=None): def unmuteThread(self, thread_id=None):
""" """
@@ -2349,8 +2339,7 @@ class Client(object):
""" """
thread_id, thread_type = self._getThread(thread_id, None) thread_id, thread_type = self._getThread(thread_id, None)
data = {"reactions_mute_mode": int(mute), "thread_fbid": thread_id} data = {"reactions_mute_mode": int(mute), "thread_fbid": thread_id}
r = self._post(self.req_url.MUTE_REACTIONS, data) r = self._post(self.req_url.MUTE_REACTIONS, data, fix_request=True)
r.raise_for_status()
def unmuteThreadReactions(self, thread_id=None): def unmuteThreadReactions(self, thread_id=None):
""" """
@@ -2369,8 +2358,7 @@ class Client(object):
""" """
thread_id, thread_type = self._getThread(thread_id, None) thread_id, thread_type = self._getThread(thread_id, None)
data = {"mentions_mute_mode": int(mute), "thread_fbid": thread_id} data = {"mentions_mute_mode": int(mute), "thread_fbid": thread_id}
r = self._post(self.req_url.MUTE_MENTIONS, data) r = self._post(self.req_url.MUTE_MENTIONS, data, fix_request=True)
r.raise_for_status()
def unmuteThreadMentions(self, thread_id=None): def unmuteThreadMentions(self, thread_id=None):
""" """
@@ -2407,9 +2395,7 @@ class Client(object):
"clientid": self.client_id, "clientid": self.client_id,
"state": "active" if self._markAlive else "offline", "state": "active" if self._markAlive else "offline",
} }
j = self._get(self.req_url.STICKY, data, fix_request=True, as_json=True) return self._get(self.req_url.STICKY, data, fix_request=True, as_json=True)
self.seq = j.get("seq", "0")
return j
def _parseDelta(self, m): def _parseDelta(self, m):
def getThreadIdAndThreadType(msg_metadata): def getThreadIdAndThreadType(msg_metadata):
@@ -3014,6 +3000,7 @@ class Client(object):
def _parseMessage(self, content): def _parseMessage(self, content):
"""Get message and author name from content. May contain multiple messages in the content.""" """Get message and author name from content. May contain multiple messages in the content."""
self.seq = j.get("seq", "0")
if "lb_info" in content: if "lb_info" in content:
self.sticky = content["lb_info"]["sticky"] self.sticky = content["lb_info"]["sticky"]