Split User into User/UserData
This commit is contained in:
@@ -15,7 +15,7 @@ from ._core import Image
|
|||||||
from ._exception import FBchatException, FBchatFacebookError
|
from ._exception import FBchatException, FBchatFacebookError
|
||||||
from ._session import Session
|
from ._session import Session
|
||||||
from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread
|
from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread
|
||||||
from ._user import TypingStatus, User, ActiveStatus
|
from ._user import TypingStatus, User, UserData, ActiveStatus
|
||||||
from ._group import Group
|
from ._group import Group
|
||||||
from ._page import Page
|
from ._page import Page
|
||||||
from ._message import EmojiSize, MessageReaction, Mention, Message
|
from ._message import EmojiSize, MessageReaction, Mention, Message
|
||||||
|
@@ -8,7 +8,7 @@ from . import _util, _graphql, _session
|
|||||||
|
|
||||||
from ._exception import FBchatException, FBchatFacebookError
|
from ._exception import FBchatException, FBchatFacebookError
|
||||||
from ._thread import ThreadLocation, ThreadColor
|
from ._thread import ThreadLocation, ThreadColor
|
||||||
from ._user import TypingStatus, User, ActiveStatus
|
from ._user import TypingStatus, User, UserData, ActiveStatus
|
||||||
from ._group import Group
|
from ._group import Group
|
||||||
from ._page import Page
|
from ._page import Page
|
||||||
from ._message import EmojiSize, MessageReaction, Mention, Message
|
from ._message import EmojiSize, MessageReaction, Mention, Message
|
||||||
@@ -204,7 +204,7 @@ class Client:
|
|||||||
if data["id"] in ["0", 0]:
|
if data["id"] in ["0", 0]:
|
||||||
# Skip invalid users
|
# Skip invalid users
|
||||||
continue
|
continue
|
||||||
users.append(User._from_all_fetch(self.session, data))
|
users.append(UserData._from_all_fetch(self.session, data))
|
||||||
return users
|
return users
|
||||||
|
|
||||||
def search_for_users(self, name, limit=10):
|
def search_for_users(self, name, limit=10):
|
||||||
@@ -224,7 +224,8 @@ class Client:
|
|||||||
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_USER, params))
|
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_USER, params))
|
||||||
|
|
||||||
return [
|
return [
|
||||||
User._from_graphql(self.session, node) for node in j[name]["users"]["nodes"]
|
UserData._from_graphql(self.session, node)
|
||||||
|
for node in j[name]["users"]["nodes"]
|
||||||
]
|
]
|
||||||
|
|
||||||
def search_for_pages(self, name, limit=10):
|
def search_for_pages(self, name, limit=10):
|
||||||
@@ -288,7 +289,7 @@ class Client:
|
|||||||
rtn = []
|
rtn = []
|
||||||
for node in j[name]["threads"]["nodes"]:
|
for node in j[name]["threads"]["nodes"]:
|
||||||
if node["__typename"] == "User":
|
if node["__typename"] == "User":
|
||||||
rtn.append(User._from_graphql(self.session, node))
|
rtn.append(UserData._from_graphql(self.session, node))
|
||||||
elif node["__typename"] == "MessageThread":
|
elif node["__typename"] == "MessageThread":
|
||||||
# MessageThread => Group thread
|
# MessageThread => Group thread
|
||||||
rtn.append(Group._from_graphql(self.session, node))
|
rtn.append(Group._from_graphql(self.session, node))
|
||||||
@@ -500,7 +501,7 @@ class Client:
|
|||||||
raise FBchatException("Could not fetch thread {}".format(_id))
|
raise FBchatException("Could not fetch thread {}".format(_id))
|
||||||
entry.update(pages_and_users[_id])
|
entry.update(pages_and_users[_id])
|
||||||
if "first_name" in entry["type"]:
|
if "first_name" in entry["type"]:
|
||||||
rtn[_id] = User._from_graphql(self.session, entry)
|
rtn[_id] = UserData._from_graphql(self.session, entry)
|
||||||
else:
|
else:
|
||||||
rtn[_id] = Page._from_graphql(self.session, entry)
|
rtn[_id] = Page._from_graphql(self.session, entry)
|
||||||
else:
|
else:
|
||||||
@@ -549,7 +550,7 @@ class Client:
|
|||||||
if _type == "GROUP":
|
if _type == "GROUP":
|
||||||
rtn.append(Group._from_graphql(self.session, node))
|
rtn.append(Group._from_graphql(self.session, node))
|
||||||
elif _type == "ONE_TO_ONE":
|
elif _type == "ONE_TO_ONE":
|
||||||
rtn.append(User._from_thread_fetch(self.session, node))
|
rtn.append(UserData._from_thread_fetch(self.session, node))
|
||||||
else:
|
else:
|
||||||
raise FBchatException(
|
raise FBchatException(
|
||||||
"Unknown thread type: {}, with data: {}".format(_type, node)
|
"Unknown thread type: {}, with data: {}".format(_type, node)
|
||||||
|
@@ -48,6 +48,38 @@ class User(_thread.ThreadABC):
|
|||||||
session = attr.ib(type=_session.Session)
|
session = attr.ib(type=_session.Session)
|
||||||
#: The user's unique identifier.
|
#: The user's unique identifier.
|
||||||
id = attr.ib(converter=str)
|
id = attr.ib(converter=str)
|
||||||
|
|
||||||
|
def _to_send_data(self):
|
||||||
|
return {"other_user_fbid": self.id}
|
||||||
|
|
||||||
|
def confirm_friend_request(self):
|
||||||
|
"""Confirm a friend request, adding the user to your friend list."""
|
||||||
|
data = {"to_friend": self.id, "action": "confirm"}
|
||||||
|
j = self.session._payload_post("/ajax/add_friend/action.php?dpr=1", data)
|
||||||
|
|
||||||
|
def remove_friend(self):
|
||||||
|
"""Remove the user from the client's friend list."""
|
||||||
|
data = {"uid": self.id}
|
||||||
|
j = self.session._payload_post("/ajax/profile/removefriendconfirm.php", data)
|
||||||
|
|
||||||
|
def block(self):
|
||||||
|
"""Block messages from the user."""
|
||||||
|
data = {"fbid": self.id}
|
||||||
|
j = self.session._payload_post("/messaging/block_messages/?dpr=1", data)
|
||||||
|
|
||||||
|
def unblock(self):
|
||||||
|
"""Unblock a previously blocked user."""
|
||||||
|
data = {"fbid": self.id}
|
||||||
|
j = self.session._payload_post("/messaging/unblock_messages/?dpr=1", data)
|
||||||
|
|
||||||
|
|
||||||
|
@attrs_default
|
||||||
|
class UserData(User):
|
||||||
|
"""Represents data about a Facebook user.
|
||||||
|
|
||||||
|
Inherits `User`, and implements `ThreadABC`.
|
||||||
|
"""
|
||||||
|
|
||||||
#: The user's picture
|
#: The user's picture
|
||||||
photo = attr.ib(None)
|
photo = attr.ib(None)
|
||||||
#: The name of the user
|
#: The name of the user
|
||||||
@@ -79,29 +111,6 @@ class User(_thread.ThreadABC):
|
|||||||
#: The default emoji
|
#: The default emoji
|
||||||
emoji = attr.ib(None)
|
emoji = attr.ib(None)
|
||||||
|
|
||||||
def _to_send_data(self):
|
|
||||||
return {"other_user_fbid": self.id}
|
|
||||||
|
|
||||||
def confirm_friend_request(self):
|
|
||||||
"""Confirm a friend request, adding the user to your friend list."""
|
|
||||||
data = {"to_friend": self.id, "action": "confirm"}
|
|
||||||
j = self.session._payload_post("/ajax/add_friend/action.php?dpr=1", data)
|
|
||||||
|
|
||||||
def remove_friend(self):
|
|
||||||
"""Remove the user from the client's friend list."""
|
|
||||||
data = {"uid": self.id}
|
|
||||||
j = self.session._payload_post("/ajax/profile/removefriendconfirm.php", data)
|
|
||||||
|
|
||||||
def block(self):
|
|
||||||
"""Block messages from the user."""
|
|
||||||
data = {"fbid": self.id}
|
|
||||||
j = self.session._payload_post("/messaging/block_messages/?dpr=1", data)
|
|
||||||
|
|
||||||
def unblock(self):
|
|
||||||
"""Unblock a previously blocked user."""
|
|
||||||
data = {"fbid": self.id}
|
|
||||||
j = self.session._payload_post("/messaging/unblock_messages/?dpr=1", data)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _from_graphql(cls, session, data):
|
def _from_graphql(cls, session, data):
|
||||||
if data.get("profile_picture") is None:
|
if data.get("profile_picture") is None:
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import datetime
|
import datetime
|
||||||
import fbchat
|
import fbchat
|
||||||
from fbchat._user import User, ActiveStatus
|
from fbchat._user import UserData, ActiveStatus
|
||||||
|
|
||||||
|
|
||||||
def test_user_from_graphql(session):
|
def test_user_from_graphql(session):
|
||||||
@@ -16,7 +16,7 @@ def test_user_from_graphql(session):
|
|||||||
"gender": "FEMALE",
|
"gender": "FEMALE",
|
||||||
"viewer_affinity": 0.4560002,
|
"viewer_affinity": 0.4560002,
|
||||||
}
|
}
|
||||||
assert User(
|
assert UserData(
|
||||||
session=session,
|
session=session,
|
||||||
id="1234",
|
id="1234",
|
||||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||||
@@ -27,7 +27,7 @@ def test_user_from_graphql(session):
|
|||||||
is_friend=True,
|
is_friend=True,
|
||||||
gender="female_singular",
|
gender="female_singular",
|
||||||
affinity=0.4560002,
|
affinity=0.4560002,
|
||||||
) == User._from_graphql(session, data)
|
) == UserData._from_graphql(session, data)
|
||||||
|
|
||||||
|
|
||||||
def test_user_from_thread_fetch(session):
|
def test_user_from_thread_fetch(session):
|
||||||
@@ -138,7 +138,7 @@ def test_user_from_thread_fetch(session):
|
|||||||
"read_receipts": ...,
|
"read_receipts": ...,
|
||||||
"delivery_receipts": ...,
|
"delivery_receipts": ...,
|
||||||
}
|
}
|
||||||
assert User(
|
assert UserData(
|
||||||
session=session,
|
session=session,
|
||||||
id="1234",
|
id="1234",
|
||||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||||
@@ -154,7 +154,7 @@ def test_user_from_thread_fetch(session):
|
|||||||
own_nickname="B",
|
own_nickname="B",
|
||||||
color=None,
|
color=None,
|
||||||
emoji=None,
|
emoji=None,
|
||||||
) == User._from_thread_fetch(session, data)
|
) == UserData._from_thread_fetch(session, data)
|
||||||
|
|
||||||
|
|
||||||
def test_user_from_all_fetch(session):
|
def test_user_from_all_fetch(session):
|
||||||
@@ -177,7 +177,7 @@ def test_user_from_all_fetch(session):
|
|||||||
"is_nonfriend_messenger_contact": False,
|
"is_nonfriend_messenger_contact": False,
|
||||||
"is_blocked": False,
|
"is_blocked": False,
|
||||||
}
|
}
|
||||||
assert User(
|
assert UserData(
|
||||||
session=session,
|
session=session,
|
||||||
id="1234",
|
id="1234",
|
||||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||||
@@ -186,7 +186,7 @@ def test_user_from_all_fetch(session):
|
|||||||
first_name="Abc",
|
first_name="Abc",
|
||||||
is_friend=True,
|
is_friend=True,
|
||||||
gender="female_singular",
|
gender="female_singular",
|
||||||
) == User._from_all_fetch(session, data)
|
) == UserData._from_all_fetch(session, data)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(reason="can't gather test data, the pulling is broken")
|
@pytest.mark.skip(reason="can't gather test data, the pulling is broken")
|
||||||
|
Reference in New Issue
Block a user