Remove ThreadType completely

This commit is contained in:
Mads Marquart
2020-01-09 11:13:48 +01:00
parent ded6039b69
commit 2ec0be9635
19 changed files with 250 additions and 601 deletions

View File

@@ -17,8 +17,6 @@ Threads
-------
.. autoclass:: Thread()
.. autoclass:: ThreadType(Enum)
:undoc-members:
.. autoclass:: Page()
.. autoclass:: User()
.. autoclass:: Group()

View File

@@ -44,11 +44,7 @@ When you're done using the client, and want to securely logout, use `Client.logo
Threads
-------
A thread can refer to two things: A Messenger group chat or a single Facebook user
`ThreadType` is an enumerator with two values: ``USER`` and ``GROUP``.
These will specify whether the thread is a single user chat or a group chat.
This is required for many of ``fbchat``'s functions, since Facebook differentiates between these two internally
A thread can refer to two things: A Messenger group chat (`Group`) or a single Facebook user (`User`).
Searching for group chats and finding their ID can be done via. `Client.search_for_groups`,
and searching for users is possible via. `Client.search_for_users`. See :ref:`intro_fetching`
@@ -68,13 +64,14 @@ The same method can be applied to some user accounts, though if they've set a cu
Here's an snippet showing the usage of thread IDs and thread types, where ``<user id>`` and ``<group id>``
corresponds to the ID of a single user, and the ID of a group respectively::
client.send(Message(text='<message>'), thread_id='<user id>', thread_type=ThreadType.USER)
client.send(Message(text='<message>'), thread_id='<group id>', thread_type=ThreadType.GROUP)
user.send(Message(text='<message>'))
group.send(Message(text='<message>'))
Some functions (e.g. `Client.change_thread_color`) don't require a thread type, so in these cases you just provide the thread ID::
Some functions don't require a thread type, so in these cases you just provide the thread ID::
client.change_thread_color(ThreadColor.BILOBA_FLOWER, thread_id='<user id>')
client.change_thread_color(ThreadColor.MESSENGER_BLUE, thread_id='<group id>')
thread = fbchat.Thread(session=session, id="<user-or-group-id>")
thread.set_color(ThreadColor.BILOBA_FLOWER)
thread.set_color(ThreadColor.MESSENGER_BLUE)
.. _intro_message_ids:
@@ -89,7 +86,7 @@ Some of ``fbchat``'s functions require these ID's, like `Client.react_to_message
and some of then provide this ID, like `Client.send`.
This snippet shows how to send a message, and then use the returned ID to react to that message with a 😍 emoji::
message_id = client.send(Message(text='message'), thread_id=thread_id, thread_type=thread_type)
message_id = thread.send(Message(text='message'))
client.react_to_message(message_id, MessageReaction.LOVE)
@@ -106,7 +103,8 @@ like adding users to and removing users from a group chat, logically only works
The simplest way of using ``fbchat`` is to send a message.
The following snippet will, as you've probably already figured out, send the message ``test message`` to your account::
message_id = client.send(Message(text='test message'), thread_id=session.user_id, thread_type=ThreadType.USER)
user = User(session=session, id=session.user_id)
message_id = user.send(Message(text='test message'))
You can see a full example showing all the possible thread interactions with ``fbchat`` by going to :ref:`examples`
@@ -173,7 +171,7 @@ meaning it will simply print information to the console when an event happens
The event actions can be changed by subclassing the `Client`, and then overwriting the event methods::
class CustomClient(Client):
def on_message(self, mid, author_id, message_object, thread_id, thread_type, ts, metadata, msg, **kwargs):
def on_message(self, mid, author_id, message_object, thread, ts, metadata, msg, **kwargs):
# Do something with message_object here
pass
@@ -182,7 +180,7 @@ The event actions can be changed by subclassing the `Client`, and then overwriti
**Notice:** The following snippet is as equally valid as the previous one::
class CustomClient(Client):
def on_message(self, message_object, author_id, thread_id, thread_type, **kwargs):
def on_message(self, message_object, author_id, thread, **kwargs):
# Do something with message_object here
pass

View File

@@ -5,15 +5,11 @@ session = fbchat.Session.login("<email>", "<password>")
print("Own id: {}".format(sesion.user_id))
# Create helper client class
client = fbchat.Client(session)
# Create helper User class
user = fbchat.Thread(session=session, id=session.user_id)
# Send a message to yourself
client.send(
fbchat.Message(text="Hi me!"),
thread_id=session.user_id,
thread_type=fbchat.ThreadType.USER,
)
user.send(fbchat.Message(text="Hi me!"))
# Log the user out
session.logout()

View File

@@ -2,15 +2,15 @@ import fbchat
# Subclass fbchat.Client and override required methods
class EchoBot(fbchat.Client):
def on_message(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.mark_as_delivered(thread_id, message_object.id)
self.mark_as_read(thread_id)
def on_message(self, author_id, message_object, thread, **kwargs):
self.mark_as_delivered(thread.id, message_object.id)
self.mark_as_read(thread.id)
print("{} from {} in {}".format(message_object, thread_id, thread_type.name))
print("{} from {} in {}".format(message_object, thread))
# If you're not the author, echo
if author_id != session.user_id:
self.send(message_object, thread_id=thread_id, thread_type=thread_type)
thread.send(message_object)
session = fbchat.Session.login("<email>", "<password>")

View File

@@ -39,8 +39,13 @@ threads += client.fetch_thread_list(offset=20, limit=10)
print("Threads: {}".format(threads))
# If we have a thread id, we can use `fetch_thread_info` to fetch a `Thread` object
thread = client.fetch_thread_info("<thread id>")["<thread id>"]
print("thread's name: {}".format(thread.name))
# Gets the last 10 messages sent to the thread
messages = client.fetch_thread_messages(thread_id="<thread id>", limit=10)
messages = thread.fetch_messages(limit=10)
# Since the message come in reversed order, reverse them
messages.reverse()
@@ -49,22 +54,15 @@ for message in messages:
print(message.text)
# If we have a thread id, we can use `fetch_thread_info` to fetch a `Thread` object
thread = client.fetch_thread_info("<thread id>")["<thread id>"]
print("thread's name: {}".format(thread.name))
print("thread's type: {}".format(thread.type))
# `search_for_threads` searches works like `search_for_users`, but gives us a list of threads instead
thread = client.search_for_threads("<name of thread>")[0]
print("thread's name: {}".format(thread.name))
print("thread's type: {}".format(thread.type))
# Here should be an example of `getUnread`
# Print image url for 20 last images from thread.
images = client.fetch_thread_images("<thread id>")
images = thread.fetch_images()
for image in itertools.islice(image, 20):
print(image.large_preview_url)

View File

@@ -4,94 +4,64 @@ session = fbchat.Session.login("<email>", "<password>")
client = fbchat.Client(session)
thread_id = "1234567890"
thread_type = fbchat.ThreadType.GROUP
thread = User(session=session, id=session.user_id)
# thread = User(session=session, id="0987654321")
# thread = Group(session=session, id="1234567890")
# Will send a message to the thread
client.send(
fbchat.Message(text="<message>"), thread_id=thread_id, thread_type=thread_type
)
thread.send(fbchat.Message(text="<message>"))
# Will send the default `like` emoji
client.send(
fbchat.Message(emoji_size=fbchat.EmojiSize.LARGE),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(emoji_size=fbchat.EmojiSize.LARGE))
# Will send the emoji `👍`
client.send(
fbchat.Message(text="👍", emoji_size=fbchat.EmojiSize.LARGE),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(text="👍", emoji_size=fbchat.EmojiSize.LARGE))
# Will send the sticker with ID `767334476626295`
client.send(
fbchat.Message(sticker=fbchat.Sticker("767334476626295")),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(sticker=fbchat.Sticker("767334476626295")))
# Will send a message with a mention
client.send(
thread.send(
fbchat.Message(
text="This is a @mention",
mentions=[fbchat.Mention(thread_id, offset=10, length=8)],
),
thread_id=thread_id,
thread_type=thread_type,
)
)
# Will send the image located at `<image path>`
client.send_local_image(
"<image path>",
message=fbchat.Message(text="This is a local image"),
thread_id=thread_id,
thread_type=thread_type,
thread.send_local_image(
"<image path>", message=fbchat.Message(text="This is a local image")
)
# Will download the image at the URL `<image url>`, and then send it
client.send_remote_image(
"<image url>",
message=fbchat.Message(text="This is a remote image"),
thread_id=thread_id,
thread_type=thread_type,
thread.send_remote_image(
"<image url>", message=fbchat.Message(text="This is a remote image")
)
# Only do these actions if the thread is a group
if thread_type == fbchat.ThreadType.GROUP:
# Will remove the user with ID `<user id>` from the thread
client.remove_user_from_group("<user id>", thread_id=thread_id)
# Will add the user with ID `<user id>` to the thread
client.add_users_to_group("<user id>", thread_id=thread_id)
# Will add the users with IDs `<1st user id>`, `<2nd user id>` and `<3th user id>` to the thread
client.add_users_to_group(
["<1st user id>", "<2nd user id>", "<3rd user id>"], thread_id=thread_id
)
if isinstance(thread, fbchat.Group):
# Will remove the user with ID `<user id>` from the group
thread.remove_participant("<user id>")
# Will add the users with IDs `<1st user id>`, `<2nd user id>` and `<3th user id>` to the group
thread.add_participants(["<1st user id>", "<2nd user id>", "<3rd user id>"])
# Will change the title of the group to `<title>`
thread.change_title("<title>")
# Will change the nickname of the user `<user_id>` to `<new nickname>`
client.change_nickname(
"<new nickname>", "<user id>", thread_id=thread_id, thread_type=thread_type
)
thread.set_nickname(fbchat.User(session=session, id="<user id>"), "<new nickname>")
# Will change the title of the thread to `<title>`
client.change_thread_title("<title>", thread_id=thread_id, thread_type=thread_type)
# Will set the typing status of the thread to `TYPING`
client.set_typing_status(
fbchat.TypingStatus.TYPING, thread_id=thread_id, thread_type=thread_type
)
# Will set the typing status of the thread
thread.start_typing()
# Will change the thread color to `MESSENGER_BLUE`
client.change_thread_color(fbchat.ThreadColor.MESSENGER_BLUE, thread_id=thread_id)
thread.set_color(fbchat.ThreadColor.MESSENGER_BLUE)
# Will change the thread emoji to `👍`
client.change_thread_emoji("👍", thread_id=thread_id)
thread.set_emoji("👍")
# Will react to a message with a 😍 emoji
client.react_to_message("<message id>", fbchat.MessageReaction.LOVE)
# message = fbchat.Message(session=session, id="<message id>")
#
# # Will react to a message with a 😍 emoji
# message.react(fbchat.MessageReaction.LOVE)

View File

@@ -16,50 +16,48 @@ old_nicknames = {
class KeepBot(fbchat.Client):
def on_color_change(self, author_id, new_color, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and old_color != new_color:
def on_color_change(self, author_id, new_color, thread, **kwargs):
if old_thread_id == thread.id and old_color != new_color:
print(
"{} changed the thread color. It will be changed back".format(author_id)
)
self.change_thread_color(old_color, thread_id=thread_id)
thread.set_color(old_color)
def on_emoji_change(self, author_id, new_emoji, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and new_emoji != old_emoji:
def on_emoji_change(self, author_id, new_emoji, thread, **kwargs):
if old_thread_id == thread.id and new_emoji != old_emoji:
print(
"{} changed the thread emoji. It will be changed back".format(author_id)
)
self.change_thread_emoji(old_emoji, thread_id=thread_id)
thread.set_emoji(old_emoji)
def on_people_added(self, added_ids, author_id, thread_id, **kwargs):
if old_thread_id == thread_id and author_id != session.user_id:
def on_people_added(self, added_ids, author_id, thread, **kwargs):
if old_thread_id == thread.id and author_id != session.user_id:
print("{} got added. They will be removed".format(added_ids))
for added_id in added_ids:
self.remove_user_from_group(added_id, thread_id=thread_id)
thread.remove_participant(added_id)
def on_person_removed(self, removed_id, author_id, thread_id, **kwargs):
def on_person_removed(self, removed_id, author_id, thread, **kwargs):
# No point in trying to add ourself
if (
old_thread_id == thread_id
old_thread_id == thread.id
and removed_id != session.user_id
and author_id != session.user_id
):
print("{} got removed. They will be re-added".format(removed_id))
self.add_users_to_group(removed_id, thread_id=thread_id)
thread.add_participants(removed_id)
def on_title_change(self, author_id, new_title, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and old_title != new_title:
def on_title_change(self, author_id, new_title, thread, **kwargs):
if old_thread_id == thread.id and old_title != new_title:
print(
"{} changed the thread title. It will be changed back".format(author_id)
)
self.change_thread_title(
old_title, thread_id=thread_id, thread_type=thread_type
)
thread.set_title(old_title)
def on_nickname_change(
self, author_id, changed_for, new_nickname, thread_id, thread_type, **kwargs
self, author_id, changed_for, new_nickname, thread, **kwargs
):
if (
old_thread_id == thread_id
old_thread_id == thread.id
and changed_for in old_nicknames
and old_nicknames[changed_for] != new_nickname
):
@@ -68,11 +66,8 @@ class KeepBot(fbchat.Client):
author_id, changed_for
)
)
self.change_nickname(
old_nicknames[changed_for],
changed_for,
thread_id=thread_id,
thread_type=thread_type,
thread.set_nickname(
changed_for, old_nicknames[changed_for],
)

View File

@@ -2,21 +2,17 @@ import fbchat
class RemoveBot(fbchat.Client):
def on_message(self, author_id, message_object, thread_id, thread_type, **kwargs):
def on_message(self, author_id, message_object, thread, **kwargs):
# We can only kick people from group chats, so no need to try if it's a user chat
if (
message_object.text == "Remove me!"
and thread_type == fbchat.ThreadType.GROUP
):
print("{} will be removed from {}".format(author_id, thread_id))
self.remove_user_from_group(author_id, thread_id=thread_id)
if message_object.text == "Remove me!" and isinstance(thread, fbchat.Group):
print("{} will be removed from {}".format(author_id, thread))
thread.remove_participant(author_id)
else:
# Sends the data to the inherited on_message, so that we can still see when a message is recieved
super(RemoveBot, self).on_message(
author_id=author_id,
message_object=message_object,
thread_id=thread_id,
thread_type=thread_type,
thread=thread,
**kwargs,
)

View File

@@ -14,7 +14,7 @@ from . import _core, _util
from ._core import Image
from ._exception import FBchatException, FBchatFacebookError
from ._session import Session
from ._thread import ThreadType, ThreadLocation, ThreadColor, ThreadABC, Thread
from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread
from ._user import TypingStatus, User, ActiveStatus
from ._group import Group
from ._page import Page

File diff suppressed because it is too large Load Diff

View File

@@ -6,27 +6,6 @@ from . import _util, _exception, _session
from typing import MutableMapping, Any, Iterable, Tuple
class ThreadType(Enum):
"""Used to specify what type of Facebook thread is being used.
See :ref:`intro_threads` for more info.
"""
USER = 1
GROUP = 2
PAGE = 3
def _to_class(self):
"""Convert this enum value to the corresponding class."""
from . import _user, _group, _page
return {
ThreadType.USER: _user.User,
ThreadType.GROUP: _group.Group,
ThreadType.PAGE: _page.Page,
}[self]
class ThreadLocation(Enum):
"""Used to specify where a thread is located (inbox, pending, archived, other)."""
@@ -106,7 +85,7 @@ class ThreadABC(metaclass=abc.ABCMeta):
)
data["lightweight_action_attachment[lwa_type]"] = "WAVE"
# TODO: This!
# if thread_type == ThreadType.USER:
# if isinstance(self, _user.User):
# data["specific_to_list[0]"] = "fbid:{}".format(thread_id)
message_id, thread_id = self.session._do_send_request(data)
return message_id
@@ -372,7 +351,7 @@ class ThreadABC(metaclass=abc.ABCMeta):
"typ": "1" if typing else "0",
"thread": self.id,
# TODO: This
"to": self.id if thread_type == ThreadType.USER else "",
# "to": self.id if isinstance(self, _user.User) else "",
"source": "mercury-chat",
}
j = self.session._payload_post("/ajax/messaging/typ.php", data)

View File

@@ -3,7 +3,7 @@ import json
from utils import *
from contextlib import contextmanager
from fbchat import ThreadType, Message, Mention
from fbchat import Message, Mention
@pytest.fixture(scope="session")
@@ -13,14 +13,14 @@ def session():
@pytest.fixture(scope="session")
def user(client2):
return {"id": client2.id, "type": ThreadType.USER}
return {"id": client2.id, "type": None}
@pytest.fixture(scope="session")
def group(pytestconfig):
return {
"id": load_variable("group_id", pytestconfig.cache),
"type": ThreadType.GROUP,
"type": None,
}
@@ -29,11 +29,9 @@ def group(pytestconfig):
params=["user", "group", pytest.param("none", marks=[pytest.mark.xfail()])],
)
def thread(request, user, group):
return {
"user": user,
"group": group,
"none": {"id": "0", "type": ThreadType.GROUP},
}[request.param]
return {"user": user, "group": group, "none": {"id": "0", "type": None},}[
request.param
]
@pytest.fixture(scope="session")
@@ -103,9 +101,7 @@ def compare(client, thread):
def inner(caught_event, **kwargs):
d = {
"author_id": client.id,
"thread_id": client.id
if thread["type"] == ThreadType.USER
else thread["id"],
"thread_id": client.id if thread["type"] == None else thread["id"],
"thread_type": thread["type"],
}
d.update(kwargs)

View File

@@ -1,7 +1,7 @@
import pytest
from os import path
from fbchat import ThreadType, Message, Mention, EmojiSize, Sticker
from fbchat import Message, Mention, EmojiSize, Sticker
from utils import subset, STICKER_LIST, EMOJI_LIST
pytestmark = pytest.mark.online

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Plan, FBchatFacebookError, ThreadType
from fbchat import Plan, FBchatFacebookError
from utils import random_hex, subset
from time import time
@@ -93,7 +93,7 @@ def test_on_plan_ended(client, thread, catch_event, compare):
x.wait(180)
assert subset(
x.res,
thread_id=client.id if thread["type"] == ThreadType.USER else thread["id"],
thread_id=client.id if thread["type"] is None else thread["id"],
thread_type=thread["type"],
)

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Poll, PollOption, ThreadType
from fbchat import Poll, PollOption
from utils import random_hex, subset
pytestmark = pytest.mark.online
@@ -49,12 +49,7 @@ def poll_data(request, client1, group, catch_event):
def test_create_poll(client1, group, catch_event, poll_data):
event, poll, _ = poll_data
assert subset(
event,
author_id=client1.id,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
assert subset(event, author_id=client1.id, thread=group)
assert subset(
vars(event["poll"]), title=poll.title, options_count=len(poll.options)
)
@@ -88,12 +83,7 @@ def test_update_poll_vote(client1, group, catch_event, poll_data):
new_options=new_options,
)
assert subset(
x.res,
author_id=client1.id,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
assert subset(x.res, author_id=client1.id, thread=group)
assert subset(
vars(x.res["poll"]), title=poll.title, options_count=len(options + new_options)
)

View File

@@ -1,5 +1,4 @@
import pytest
from fbchat import ThreadType
pytestmark = pytest.mark.online

View File

@@ -1,12 +1,6 @@
import pytest
import fbchat
from fbchat import ThreadType, ThreadColor, ThreadABC, Thread
def test_thread_type_to_class():
assert fbchat.User == ThreadType.USER._to_class()
assert fbchat.Group == ThreadType.GROUP._to_class()
assert fbchat.Page == ThreadType.PAGE._to_class()
from fbchat import ThreadColor, ThreadABC, Thread
def test_thread_color_from_graphql():

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Message, ThreadType, FBchatFacebookError, TypingStatus, ThreadColor
from fbchat import Message, FBchatFacebookError, TypingStatus, ThreadColor
from utils import random_hex, subset
from os import path
@@ -42,14 +42,8 @@ def test_remove_from_and_add_admins_to_group(client1, client2, group, catch_even
def test_change_title(client1, group, catch_event):
title = random_hex()
with catch_event("on_title_change") as x:
client1.change_thread_title(title, group["id"], thread_type=ThreadType.GROUP)
assert subset(
x.res,
author_id=client1.id,
new_title=title,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
client1.change_thread_title(title, group["id"])
assert subset(x.res, author_id=client1.id, new_title=title, thread=group)
def test_change_nickname(client, client_all, catch_event, compare):

View File

@@ -5,7 +5,7 @@ import pytest
from os import environ
from random import randrange
from contextlib import contextmanager
from fbchat import ThreadType, EmojiSize, FBchatFacebookError, Sticker, Client
from fbchat import EmojiSize, FBchatFacebookError, Sticker, Client
log = logging.getLogger("fbchat.tests").addHandler(logging.NullHandler())