Split Page into Page/PageData

This commit is contained in:
Mads Marquart
2020-01-09 14:09:33 +01:00
parent 22217c793c
commit c7ee45aaca
4 changed files with 21 additions and 11 deletions

View File

@@ -17,7 +17,7 @@ from ._session import Session
from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread
from ._user import TypingStatus, User, UserData, ActiveStatus
from ._group import Group
from ._page import Page
from ._page import Page, PageData
from ._message import EmojiSize, MessageReaction, Mention, Message
from ._attachment import Attachment, UnsentMessage, ShareAttachment
from ._sticker import Sticker

View File

@@ -10,7 +10,7 @@ from ._exception import FBchatException, FBchatFacebookError
from ._thread import ThreadLocation, ThreadColor
from ._user import TypingStatus, User, UserData, ActiveStatus
from ._group import Group
from ._page import Page
from ._page import Page, PageData
from ._message import EmojiSize, MessageReaction, Mention, Message
from ._attachment import Attachment
from ._sticker import Sticker
@@ -244,7 +244,8 @@ class Client:
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_PAGE, params))
return [
Page._from_graphql(self.session, node) for node in j[name]["pages"]["nodes"]
PageData._from_graphql(self.session, node)
for node in j[name]["pages"]["nodes"]
]
def search_for_groups(self, name, limit=10):
@@ -294,7 +295,7 @@ class Client:
# MessageThread => Group thread
rtn.append(Group._from_graphql(self.session, node))
elif node["__typename"] == "Page":
rtn.append(Page._from_graphql(self.session, node))
rtn.append(PageData._from_graphql(self.session, node))
elif node["__typename"] == "Group":
# We don't handle Facebook "Groups"
pass
@@ -503,7 +504,7 @@ class Client:
if "first_name" in entry["type"]:
rtn[_id] = UserData._from_graphql(self.session, entry)
else:
rtn[_id] = Page._from_graphql(self.session, entry)
rtn[_id] = PageData._from_graphql(self.session, entry)
else:
raise FBchatException(
"{} had an unknown thread type: {}".format(thread_ids[i], entry)

View File

@@ -11,6 +11,18 @@ class Page(_thread.ThreadABC):
session = attr.ib(type=_session.Session)
#: The unique identifier of the page.
id = attr.ib(converter=str)
def _to_send_data(self):
return {"other_user_fbid": self.id}
@attrs_default
class PageData(Page):
"""Represents data about a Facebook page.
Inherits `Page`, and implements `ThreadABC`.
"""
#: The page's picture
photo = attr.ib(None)
#: The name of the page
@@ -32,9 +44,6 @@ class Page(_thread.ThreadABC):
#: The page's category
category = attr.ib(None)
def _to_send_data(self):
return {"other_user_fbid": self.id}
@classmethod
def _from_graphql(cls, session, data):
if data.get("profile_picture") is None:

View File

@@ -1,5 +1,5 @@
import fbchat
from fbchat._page import Page
from fbchat._page import PageData
def test_page_from_graphql(session):
@@ -11,7 +11,7 @@ def test_page_from_graphql(session):
"category_type": "SCHOOL",
"city": None,
}
assert Page(
assert PageData(
session=session,
id="123456",
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
@@ -19,4 +19,4 @@ def test_page_from_graphql(session):
url="https://www.facebook.com/some-school/",
city=None,
category="SCHOOL",
) == Page._from_graphql(session, data)
) == PageData._from_graphql(session, data)