Add shortcuts to easily delete threads/messages

This commit is contained in:
Mads Marquart
2020-02-05 18:45:20 +01:00
parent 94c985cb10
commit 7bdacb91ba
3 changed files with 45 additions and 15 deletions

View File

@@ -594,17 +594,7 @@ class Client:
>>> group = fbchat.Group(session=session, id="1234")
>>> client.delete_threads([group])
"""
data_unpin = {}
data_delete = {}
for i, thread in enumerate(threads):
data_unpin["ids[{}]".format(thread.id)] = "false"
data_delete["ids[{}]".format(i)] = thread.id
j_unpin = self.session._payload_post(
"/ajax/mercury/change_pinned_status.php?dpr=1", data_unpin
)
j_delete = self.session._payload_post(
"/ajax/mercury/delete_threads.php?dpr=1", data_delete
)
_threads.ThreadABC._delete_many(self.session, (t.id for t in threads))
def delete_messages(self, messages: Iterable[_models.Message]):
"""Bulk delete specified messages.
@@ -617,7 +607,4 @@ class Client:
>>> message2 = fbchat.Message(thread=thread, id="2345")
>>> client.delete_threads([message1, message2])
"""
data = {}
for i, message in enumerate(messages):
data["message_ids[{}]".format(i)] = message.id
j = self.session._payload_post("/ajax/mercury/delete_messages.php?dpr=1", data)
_models.Message._delete_many(self.session, (m.id for m in messages))

View File

@@ -94,9 +94,28 @@ class Message:
"""The session to use when making requests."""
return self.thread.session
@staticmethod
def _delete_many(session, message_ids):
data = {}
for i, id_ in enumerate(message_ids):
data["message_ids[{}]".format(i)] = id_
j = session._payload_post("/ajax/mercury/delete_messages.php?dpr=1", data)
def delete(self):
"""Delete the message (removes it only for the user).
If you want to delete multiple messages, please use `Client.delete_messages`.
Example:
>>> message.delete()
"""
self._delete_many(self.session, [self.id])
def unsend(self):
"""Unsend the message (removes it for everyone).
The message must to be sent by you, and less than 10 minutes ago.
Example:
>>> message.unsend()
"""

View File

@@ -75,6 +75,10 @@ class ThreadABC(metaclass=abc.ABCMeta):
"""
raise NotImplementedError
def fetch(self):
# TODO: This
raise NotImplementedError
def wave(self, first: bool = True) -> str:
"""Wave hello to the thread.
@@ -695,6 +699,26 @@ class ThreadABC(metaclass=abc.ABCMeta):
data = {"id": self.id}
j = self.session._payload_post("/ajax/mercury/mark_spam.php?dpr=1", data)
@staticmethod
def _delete_many(session, thread_ids):
data = {}
for i, id_ in enumerate(thread_ids):
data["ids[{}]".format(i)] = id_
# Not needed any more
# j = session._payload_post("/ajax/mercury/change_pinned_status.php?dpr=1", ...)
# Both /ajax/mercury/delete_threads.php (with an s) doesn't work
j = session._payload_post("/ajax/mercury/delete_thread.php", data)
def delete(self):
"""Delete the thread.
If you want to delete multiple threads, please use `Client.delete_threads`.
Example:
>>> message.delete()
"""
self._delete_many(self.session, [self.id])
def _forced_fetch(self, message_id: str) -> dict:
params = {
"thread_and_message_id": {"thread_id": self.id, "message_id": message_id}