Move Client._getSendData into the Thread / Group models

This commit is contained in:
Mads Marquart
2019-08-28 17:39:57 +02:00
parent 5e86d4a48a
commit 1f342d0c71
3 changed files with 17 additions and 18 deletions

View File

@@ -1031,17 +1031,6 @@ class Client(object):
def _oldMessage(self, message): def _oldMessage(self, message):
return message if isinstance(message, Message) else Message(text=message) return message if isinstance(message, Message) else Message(text=message)
def _getSendData(self, thread_id=None, thread_type=ThreadType.USER):
data = {}
# Set recipient
if thread_type in [ThreadType.USER, ThreadType.PAGE]:
data["other_user_fbid"] = thread_id
elif thread_type == ThreadType.GROUP:
data["thread_fbid"] = thread_id
return data
def _doSendRequest(self, data, get_thread_id=False): def _doSendRequest(self, data, get_thread_id=False):
"""Send the data to `SendURL`, and returns the message ID or None on failure.""" """Send the data to `SendURL`, and returns the message ID or None on failure."""
mid, thread_id = self._state._do_send_request(data) mid, thread_id = self._state._do_send_request(data)
@@ -1065,7 +1054,8 @@ class Client(object):
FBchatException: If request failed FBchatException: If request failed
""" """
thread_id, thread_type = self._getThread(thread_id, thread_type) thread_id, thread_type = self._getThread(thread_id, thread_type)
data = self._getSendData(thread_id=thread_id, thread_type=thread_type) thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data.update(message._to_send_data()) data.update(message._to_send_data())
return self._doSendRequest(data) return self._doSendRequest(data)
@@ -1104,7 +1094,8 @@ class Client(object):
FBchatException: If request failed FBchatException: If request failed
""" """
thread_id, thread_type = self._getThread(thread_id, thread_type) thread_id, thread_type = self._getThread(thread_id, thread_type)
data = self._getSendData(thread_id=thread_id, thread_type=thread_type) thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data["action_type"] = "ma-type:user-generated-message" data["action_type"] = "ma-type:user-generated-message"
data["lightweight_action_attachment[lwa_state]"] = ( data["lightweight_action_attachment[lwa_state]"] = (
"INITIATED" if wave_first else "RECIPROCATED" "INITIATED" if wave_first else "RECIPROCATED"
@@ -1168,7 +1159,8 @@ class Client(object):
self, location, current=True, message=None, thread_id=None, thread_type=None self, location, current=True, message=None, thread_id=None, thread_type=None
): ):
thread_id, thread_type = self._getThread(thread_id, thread_type) thread_id, thread_type = self._getThread(thread_id, thread_type)
data = self._getSendData(thread_id=thread_id, thread_type=thread_type) thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
if message is not None: if message is not None:
data.update(message._to_send_data()) data.update(message._to_send_data())
data["action_type"] = "ma-type:user-generated-message" data["action_type"] = "ma-type:user-generated-message"
@@ -1236,7 +1228,8 @@ class Client(object):
`files` should be a list of tuples, with a file's ID and mimetype. `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_id, thread_type = self._getThread(thread_id, thread_type)
data = self._getSendData(thread_id=thread_id, thread_type=thread_type) thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data.update(self._oldMessage(message)._to_send_data()) data.update(self._oldMessage(message)._to_send_data())
data["action_type"] = "ma-type:user-generated-message" data["action_type"] = "ma-type:user-generated-message"
data["has_attachment"] = True data["has_attachment"] = True
@@ -1417,8 +1410,7 @@ class Client(object):
Raises: Raises:
FBchatException: If request failed FBchatException: If request failed
""" """
data = self._getSendData() data = self._oldMessage(message)._to_send_data()
data.update(self._oldMessage(message)._to_send_data())
if len(user_ids) < 2: if len(user_ids) < 2:
raise FBchatUserError("Error when creating group: Not enough participants") raise FBchatUserError("Error when creating group: Not enough participants")
@@ -1444,7 +1436,7 @@ class Client(object):
FBchatException: If request failed FBchatException: If request failed
""" """
thread_id, thread_type = self._getThread(thread_id, None) thread_id, thread_type = self._getThread(thread_id, None)
data = self._getSendData(thread_id=thread_id, thread_type=ThreadType.GROUP) data = Group(thread_id)._to_send_data()
data["action_type"] = "ma-type:log-message" data["action_type"] = "ma-type:log-message"
data["log_message_type"] = "log:subscribe" data["log_message_type"] = "log:subscribe"

View File

@@ -104,6 +104,9 @@ class Group(Thread):
plan=plan, plan=plan,
) )
def _to_send_data(self):
return {"thread_fbid": self.uid}
@attr.s(cmp=False, init=False) @attr.s(cmp=False, init=False)
class Room(Group): class Room(Group):

View File

@@ -141,3 +141,7 @@ class Thread(object):
else: else:
rtn["own_nickname"] = pc[1].get("nickname") rtn["own_nickname"] = pc[1].get("nickname")
return rtn return rtn
def _to_send_data(self):
# TODO: Only implement this in subclasses
return {"other_user_fbid": self.uid}