From 01effb34b433fedbcfd478b518d5858a872f17de Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 23 Jan 2020 16:46:53 +0100 Subject: [PATCH] Add Session.user in favor of Session.user_id --- docs/intro.rst | 2 +- examples/basic_usage.py | 7 ++----- examples/echobot.py | 2 +- examples/interract.py | 4 ++-- examples/keepbot.py | 6 +++--- fbchat/_client.py | 2 +- fbchat/_events/_delta_class.py | 3 +-- fbchat/_models/_message.py | 2 +- fbchat/_mqtt.py | 4 ++-- fbchat/_session.py | 12 ++++++++---- fbchat/_threads/_abc.py | 2 +- fbchat/_threads/_group.py | 4 ++-- tests/events/test_delta_class.py | 2 +- 13 files changed, 26 insertions(+), 26 deletions(-) diff --git a/docs/intro.rst b/docs/intro.rst index 0e90837..39a17fd 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -46,7 +46,7 @@ A thread basically just means "something I can chat with", but more precisely, i - The conversation between you and a single Facebook user (`User`) - The conversation between you and a Facebook Page (`Page`) -You can get your own user ID with `Session.user_id`. +You can get your own user ID with `Session.user.id`. Getting the ID of a specific group thread is fairly trivial, you only need to login to ``_, click on the group you want to find the ID of, and then read the id from the address bar. The URL will look something like this: ``https://www.messenger.com/t/1234567890``, where ``1234567890`` would be the ID of the group. diff --git a/examples/basic_usage.py b/examples/basic_usage.py index aa0002b..a20528c 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -3,13 +3,10 @@ import fbchat # Log the user in session = fbchat.Session.login("", "") -print("Own id: {}".format(session.user_id)) - -# Create helper User class -user = fbchat.Thread(session=session, id=session.user_id) +print("Own id: {}".format(session.user.id)) # Send a message to yourself -user.send_text("Hi me!") +session.user.send_text("Hi me!") # Log the user out session.logout() diff --git a/examples/echobot.py b/examples/echobot.py index affc8fc..d196621 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -8,7 +8,7 @@ listener = fbchat.Listener.connect(session, chat_on=False, foreground=False) def on_message(event): print(f"{event.message.text} from {event.author.id} in {event.thread.id}") # If you're not the author, echo - if event.author.id != session.user_id: + if event.author.id != session.user.id: event.thread.send_text(event.message.text) diff --git a/examples/interract.py b/examples/interract.py index ec2471f..498e9bb 100644 --- a/examples/interract.py +++ b/examples/interract.py @@ -5,7 +5,7 @@ session = fbchat.Session.login("", "") client = fbchat.Client(session) -thread = fbchat.User(session=session, id=session.user_id) +thread = session.user # thread = fbchat.User(session=session, id="0987654321") # thread = fbchat.Group(session=session, id="1234567890") @@ -48,7 +48,7 @@ if isinstance(thread, fbchat.Group): thread.set_title("") -# Will change the nickname of the user `<user_id>` to `<new nickname>` +# Will change the nickname of the user `<user id>` to `<new nickname>` thread.set_nickname(fbchat.User(session=session, id="<user id>"), "<new nickname>") # Will set the typing status of the thread diff --git a/examples/keepbot.py b/examples/keepbot.py index 56bef6c..1529f90 100644 --- a/examples/keepbot.py +++ b/examples/keepbot.py @@ -58,7 +58,7 @@ def on_nickname_set(event: fbchat.NicknameSet): def on_people_added(event: fbchat.PeopleAdded): if old_thread_id != event.thread.id: return - if event.author.id != session.user_id: + if event.author.id != session.user.id: print(f"{', '.join(x.id for x in event.added)} got added. They will be removed") for added in event.added: event.thread.remove_participant(added.id) @@ -68,9 +68,9 @@ def on_person_removed(event: fbchat.PersonRemoved): if old_thread_id != event.thread.id: return # No point in trying to add ourself - if event.removed.id == session.user_id: + if event.removed.id == session.user.id: return - if event.author.id != session.user_id: + if event.author.id != session.user.id: print(f"{event.removed.id} got removed. They will be re-added") event.thread.add_participants([removed.id]) diff --git a/fbchat/_client.py b/fbchat/_client.py index c0550cd..23d185a 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -42,7 +42,7 @@ class Client: >>> users[0].name "A user" """ - data = {"viewer": self.session.user_id} + data = {"viewer": self.session.user.id} j = self.session._payload_post("/chat/user_info_all", data) users = [] diff --git a/fbchat/_events/_delta_class.py b/fbchat/_events/_delta_class.py index 704caf0..9c3c909 100644 --- a/fbchat/_events/_delta_class.py +++ b/fbchat/_events/_delta_class.py @@ -132,12 +132,11 @@ class ThreadsRead(Event): @classmethod def _parse(cls, session, data): - author = _threads.User(session=session, id=session.user_id) threads = [ cls._get_thread(session, {"threadKey": x}) for x in data["threadKeys"] ] at = _util.millis_to_datetime(int(data["actionTimestamp"])) - return cls(author=author, threads=threads, at=at) + return cls(author=session.user, threads=threads, at=at) @attrs_event diff --git a/fbchat/_models/_message.py b/fbchat/_models/_message.py index 8ea4318..5afd1a2 100644 --- a/fbchat/_models/_message.py +++ b/fbchat/_models/_message.py @@ -123,7 +123,7 @@ class Message: data = { "action": "ADD_REACTION" if reaction else "REMOVE_REACTION", "client_mutation_id": "1", - "actor_id": self.session.user_id, + "actor_id": self.session.user.id, "message_id": self.id, "reaction": reaction, } diff --git a/fbchat/_mqtt.py b/fbchat/_mqtt.py index 2a582f8..762fced 100644 --- a/fbchat/_mqtt.py +++ b/fbchat/_mqtt.py @@ -190,7 +190,7 @@ class Listener: "max_deltas_able_to_process": 1000, "delta_batch_size": 500, "encoding": "JSON", - "entity_fbid": self.session.user_id, + "entity_fbid": self.session.user.id, } # If we don't have a sync_token, create a new messenger queue @@ -250,7 +250,7 @@ class Listener: username = { # The user ID - "u": self.session.user_id, + "u": self.session.user.id, # Session ID "s": session_id, # Active status setting diff --git a/fbchat/_session.py b/fbchat/_session.py index 658281f..6e34d67 100644 --- a/fbchat/_session.py +++ b/fbchat/_session.py @@ -158,9 +158,13 @@ class Session: _logout_h = attr.ib(None, type=str) @property - def user_id(self) -> str: - """The logged in user's ID.""" - return self._user_id + def user(self): + """The logged in user.""" + from . import _threads + + # TODO: Consider caching the result + + return _threads.User(session=self, id=self._user_id) def __repr__(self) -> str: # An alternative repr, to illustrate that you can't create the class directly @@ -191,7 +195,7 @@ class Session: >>> import getpass >>> import fbchat >>> session = fbchat.Session.login("<email or phone>", getpass.getpass()) - >>> session.user_id + >>> session.user.id "1234" """ session = session_factory() diff --git a/fbchat/_threads/_abc.py b/fbchat/_threads/_abc.py index 92ce6b4..ca690df 100644 --- a/fbchat/_threads/_abc.py +++ b/fbchat/_threads/_abc.py @@ -513,7 +513,7 @@ class ThreadABC(metaclass=abc.ABCMeta): # def set_theme(self, theme_id: str): # data = { # "client_mutation_id": "0", - # "actor_id": self.session.user_id, + # "actor_id": self.session.user.id, # "thread_id": self.id, # "theme_id": theme_id, # "source": "SETTINGS", diff --git a/fbchat/_threads/_group.py b/fbchat/_threads/_group.py index 0020d6a..7cfd35b 100644 --- a/fbchat/_threads/_group.py +++ b/fbchat/_threads/_group.py @@ -41,7 +41,7 @@ class Group(ThreadABC): data["log_message_type"] = "log:subscribe" for i, user_id in enumerate(user_ids): - if user_id == self.session.user_id: + if user_id == self.session.user.id: raise ValueError( "Error when adding users: Cannot add self to group thread" ) @@ -138,7 +138,7 @@ class Group(ThreadABC): def _users_approval(self, user_ids: Iterable[str], approve: bool): data = { "client_mutation_id": "0", - "actor_id": self.session.user_id, + "actor_id": self.session.user.id, "thread_fbid": self.id, "user_ids": list(user_ids), "response": "ACCEPT" if approve else "DENY", diff --git a/tests/events/test_delta_class.py b/tests/events/test_delta_class.py index 6038ca1..6d9a57b 100644 --- a/tests/events/test_delta_class.py +++ b/tests/events/test_delta_class.py @@ -224,7 +224,7 @@ def test_mark_read(session): "class": "MarkRead", } assert ThreadsRead( - author=User(session=session, id=session.user_id), + author=session.user, threads=[Group(session=session, id="1234"), User(session=session, id="2345")], at=datetime.datetime(2020, 9, 13, 12, 26, 40, tzinfo=datetime.timezone.utc), ) == parse_delta(session, data)