Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #667

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/interract.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
# 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
thread.start_typing()
# Will start typing in the thread
thread.set_typing(True)

# Will change the thread color to #0084ff
thread.set_color("#0084ff")
Expand Down
8 changes: 4 additions & 4 deletions examples/keepbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def on_nickname_set(sender, event: fbchat.NicknameSet):
event.thread.set_nickname(event.subject.id, old_nickname)


@events.connect_via(fbchat.PeopleAdded)
def on_people_added(sender, event: fbchat.PeopleAdded):
@events.connect_via(fbchat.ParticipantsAdded)
def on_people_added(sender, event: fbchat.ParticipantsAdded):
if old_thread_id != event.thread.id:
return
if event.author.id != session.user.id:
Expand All @@ -71,8 +71,8 @@ def on_people_added(sender, event: fbchat.PeopleAdded):
event.thread.remove_participant(added.id)


@events.connect_via(fbchat.PersonRemoved)
def on_person_removed(sender, event: fbchat.PersonRemoved):
@events.connect_via(fbchat.ParticipantRemoved)
def on_person_removed(sender, event: fbchat.ParticipantRemoved):
if old_thread_id != event.thread.id:
return
# No point in trying to add ourself
Expand Down
6 changes: 3 additions & 3 deletions fbchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
UnsendEvent,
MessageReplyEvent,
# _delta_class
PeopleAdded,
PersonRemoved,
ParticipantsAdded,
ParticipantRemoved,
TitleSet,
UnfetchedThreadEvent,
MessagesDelivered,
Expand All @@ -107,7 +107,7 @@
PlanDeleted,
PlanResponded,
# __init__
Typing,
TypingStatus,
FriendRequest,
Presence,
)
Expand Down
10 changes: 5 additions & 5 deletions fbchat/_events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@


@attrs_event
class Typing(ThreadEvent):
class TypingStatus(ThreadEvent):
"""Somebody started/stopped typing in a thread."""

#: ``True`` if the user started typing, ``False`` if they stopped
status = attr.ib(type=bool)

@classmethod
def _parse_orca(cls, session, data):
def _from_orca(cls, session, data):
author = _threads.User(session=session, id=str(data["sender_fbid"]))
status = data["state"] == 1
return cls(author=author, thread=author, status=status)

@classmethod
def _parse_thread_typing(cls, session, data):
def _from_thread_typing(cls, session, data):
author = _threads.User(session=session, id=str(data["sender_fbid"]))
thread = _threads.Group(session=session, id=str(data["thread"]))
status = data["state"] == 1
Expand Down Expand Up @@ -89,10 +89,10 @@ def parse_events(session, topic, data):
) from e

elif topic == "/thread_typing":
yield Typing._parse_thread_typing(session, data)
yield TypingStatus._from_thread_typing(session, data)

elif topic == "/orca_typing_notifications":
yield Typing._parse_orca(session, data)
yield TypingStatus._from_orca(session, data)

elif topic == "/legacy_web":
if data["type"] == "jewel_requests_add":
Expand Down
59 changes: 51 additions & 8 deletions fbchat/_events/_delta_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@


@attrs_event
class PeopleAdded(ThreadEvent):
"""somebody added people to a group thread."""
class ParticipantsAdded(ThreadEvent):
"""People were added to a group thread."""

# TODO: Add message id
# TODO: Add snippet/admin text

thread = attr.ib(type="_threads.Group") # Set the correct type
#: The people who got added
Expand All @@ -29,9 +30,27 @@ def _parse(cls, session, data):
]
return cls(author=author, thread=thread, added=added, at=at)

@classmethod
def _from_send(cls, thread: "_threads.Group", added_ids: Sequence[str]):
return cls(
author=thread.session.user,
thread=thread,
added=[_threads.User(session=thread.session, id=id_) for id_ in added_ids],
at=None,
)

@classmethod
def _from_fetch(cls, thread: "_threads.Group", data):
author, at = cls._parse_fetch(thread.session, data)
added = [
_threads.User(session=thread.session, id=id_["id"])
for id_ in data["participants_added"]
]
return cls(author=author, thread=thread, added=added, at=at)


@attrs_event
class PersonRemoved(ThreadEvent):
class ParticipantRemoved(ThreadEvent):
"""Somebody removed a person from a group thread."""

# TODO: Add message id
Expand All @@ -48,21 +67,45 @@ def _parse(cls, session, data):
removed = _threads.User(session=session, id=data["leftParticipantFbId"])
return cls(author=author, thread=thread, removed=removed, at=at)

@classmethod
def _from_send(cls, thread: "_threads.Group", removed_id: str):
return cls(
author=thread.session.user,
thread=thread,
removed=_threads.User(session=thread.session, id=removed_id),
at=None,
)

@classmethod
def _from_fetch(cls, thread: "_threads.Group", data):
author, at = cls._parse_fetch(thread.session, data)
removed = _threads.User(
session=thread.session, id=data["participants_removed"][0]["id"]
)
return cls(author=author, thread=thread, removed=removed, at=at)


@attrs_event
class TitleSet(ThreadEvent):
"""Somebody changed a group's title."""

thread = attr.ib(type="_threads.Group") # Set the correct type
#: The new title
title = attr.ib(type=str)
#: The new title. If ``None``, the title is cleared
title = attr.ib(type=Optional[str])
#: When the title was set
at = attr.ib(type=datetime.datetime)

@classmethod
def _parse(cls, session, data):
author, thread, at = cls._parse_metadata(session, data)
return cls(author=author, thread=thread, title=data["name"], at=at)
title = data["name"] or None
return cls(author=author, thread=thread, title=title, at=at)

@classmethod
def _from_fetch(cls, thread, data):
author, at = cls._parse_fetch(thread.session, data)
title = data["thread_name"] or None
return cls(author=author, thread=thread, title=title, at=at)


@attrs_event
Expand Down Expand Up @@ -184,9 +227,9 @@ def parse_delta(session, data):
if class_ == "AdminTextMessage":
return _delta_type.parse_admin_message(session, data)
elif class_ == "ParticipantsAddedToGroupThread":
return PeopleAdded._parse(session, data)
return ParticipantsAdded._parse(session, data)
elif class_ == "ParticipantLeftGroupThread":
return PersonRemoved._parse(session, data)
return ParticipantRemoved._parse(session, data)
elif class_ == "MarkFolderSeen":
# TODO: Finish this
folders = [_models.ThreadLocation._parse(folder) for folder in data["folders"]]
Expand Down
31 changes: 27 additions & 4 deletions fbchat/_events/_delta_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,33 @@ def _parse(cls, session, data):
color = _threads.ThreadABC._parse_color(data["untypedData"]["theme_color"])
return cls(author=author, thread=thread, color=color, at=at)

@classmethod
def _from_fetch(cls, thread: "_threads.ThreadABC", data):
author, at = cls._parse_fetch(thread.session, data)
color = data["extensible_message_admin_text"]["theme_color"]
color = _threads.ThreadABC._parse_color(color)
return cls(author=author, thread=thread, color=color, at=at)


@attrs_event
class EmojiSet(ThreadEvent):
"""Somebody set the emoji in a thread."""

#: The new emoji
emoji = attr.ib(type=str)
#: The new emoji. If ``None``, the emoji was reset to the default "LIKE" icon
emoji = attr.ib(type=Optional[str])
#: When the emoji was set
at = attr.ib(type=datetime.datetime)

@classmethod
def _parse(cls, session, data):
author, thread, at = cls._parse_metadata(session, data)
emoji = data["untypedData"]["thread_icon"]
emoji = data["untypedData"]["thread_icon"] or None
return cls(author=author, thread=thread, emoji=emoji, at=at)

@classmethod
def _from_fetch(cls, thread: "_threads.ThreadABC", data):
author, at = cls._parse_fetch(thread.session, data)
emoji = data["extensible_message_admin_text"]["thread_icon"]
return cls(author=author, thread=thread, emoji=emoji, at=at)


Expand All @@ -43,7 +56,7 @@ class NicknameSet(ThreadEvent):
"""Somebody set the nickname of a person in a thread."""

#: The person whose nickname was set
subject = attr.ib(type=str)
subject = attr.ib(type="_threads.User")
#: The new nickname. If ``None``, the nickname was cleared
nickname = attr.ib(type=Optional[str])
#: When the nickname was set
Expand All @@ -60,6 +73,16 @@ def _parse(cls, session, data):
author=author, thread=thread, subject=subject, nickname=nickname, at=at
)

@classmethod
def _from_fetch(cls, thread, data):
author, at = cls._parse_fetch(thread.session, data)
extra = data["extensible_message_admin_text"]
subject = _threads.User(session=thread.session, id=extra["participant_id"])
nickname = extra["nickname"] or None
return cls(
author=author, thread=thread, subject=subject, nickname=nickname, at=at
)


@attrs_event
class AdminsAdded(ThreadEvent):
Expand Down
2 changes: 1 addition & 1 deletion fbchat/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def _from_session(cls, session):
raise _exception.NotLoggedIn("Could not find fb_dtsg")
fb_dtsg = res.group(1)

revision = int(r.text.split('"client_revision":', 1)[1].split(",", 1)[0])
revision = int(user_id)

logout_h_element = soup.find("input", {"name": "h"})
logout_h = logout_h_element["value"] if logout_h_element else None
Expand Down
Loading