Add Session.user in favor of Session.user_id

This commit is contained in:
Mads Marquart
2020-01-23 16:46:53 +01:00
parent 2c8dfc02c2
commit 01effb34b4
13 changed files with 26 additions and 26 deletions

View File

@@ -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 `<https://www.messenger.com/>`_, 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.

View File

@@ -3,13 +3,10 @@ import fbchat
# Log the user in
session = fbchat.Session.login("<email>", "<password>")
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()

View File

@@ -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)

View File

@@ -5,7 +5,7 @@ session = fbchat.Session.login("<email>", "<password>")
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("<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

View File

@@ -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])

View File

@@ -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 = []

View File

@@ -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

View File

@@ -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,
}

View File

@@ -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

View File

@@ -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()

View File

@@ -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",

View File

@@ -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",

View File

@@ -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)