Add session attribute to Group/User/Page/Thread

This commit is contained in:
Mads Marquart
2020-01-08 23:07:13 +01:00
parent a5abb05ab3
commit 0531a9e482
9 changed files with 71 additions and 30 deletions

View File

@@ -61,6 +61,10 @@ class Client:
self._buddylist = dict()
self._session = session
@property
def session(self):
return self._session
"""
INTERNAL REQUEST METHODS
"""
@@ -214,7 +218,7 @@ class Client:
if data["id"] in ["0", 0]:
# Skip invalid users
continue
users.append(User._from_all_fetch(data))
users.append(User._from_all_fetch(self.session, data))
return users
def search_for_users(self, name, limit=10):
@@ -233,7 +237,9 @@ class Client:
params = {"search": name, "limit": limit}
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_USER, params))
return [User._from_graphql(node) for node in j[name]["users"]["nodes"]]
return [
User._from_graphql(self.session, node) for node in j[name]["users"]["nodes"]
]
def search_for_pages(self, name, limit=10):
"""Find and get pages by their name.
@@ -250,7 +256,9 @@ class Client:
params = {"search": name, "limit": limit}
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_PAGE, params))
return [Page._from_graphql(node) for node in j[name]["pages"]["nodes"]]
return [
Page._from_graphql(self.session, node) for node in j[name]["pages"]["nodes"]
]
def search_for_groups(self, name, limit=10):
"""Find and get group threads by their name.
@@ -268,7 +276,10 @@ class Client:
params = {"search": name, "limit": limit}
(j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_GROUP, params))
return [Group._from_graphql(node) for node in j["viewer"]["groups"]["nodes"]]
return [
Group._from_graphql(self.session, node)
for node in j["viewer"]["groups"]["nodes"]
]
def search_for_threads(self, name, limit=10):
"""Find and get threads by their name.
@@ -291,12 +302,12 @@ class Client:
rtn = []
for node in j[name]["threads"]["nodes"]:
if node["__typename"] == "User":
rtn.append(User._from_graphql(node))
rtn.append(User._from_graphql(self.session, node))
elif node["__typename"] == "MessageThread":
# MessageThread => Group thread
rtn.append(Group._from_graphql(node))
rtn.append(Group._from_graphql(self.session, node))
elif node["__typename"] == "Page":
rtn.append(Page._from_graphql(node))
rtn.append(Page._from_graphql(self.session, node))
elif node["__typename"] == "Group":
# We don't handle Facebook "Groups"
pass
@@ -551,16 +562,16 @@ class Client:
entry = entry["message_thread"]
if entry.get("thread_type") == "GROUP":
_id = entry["thread_key"]["thread_fbid"]
rtn[_id] = Group._from_graphql(entry)
rtn[_id] = Group._from_graphql(self.session, entry)
elif entry.get("thread_type") == "ONE_TO_ONE":
_id = entry["thread_key"]["other_user_id"]
if pages_and_users.get(_id) is None:
raise FBchatException("Could not fetch thread {}".format(_id))
entry.update(pages_and_users[_id])
if entry["type"] == ThreadType.USER:
rtn[_id] = User._from_graphql(entry)
rtn[_id] = User._from_graphql(self.session, entry)
else:
rtn[_id] = Page._from_graphql(entry)
rtn[_id] = Page._from_graphql(self.session, entry)
else:
raise FBchatException(
"{} had an unknown thread type: {}".format(thread_ids[i], entry)
@@ -641,9 +652,9 @@ class Client:
for node in j["viewer"]["message_threads"]["nodes"]:
_type = node.get("thread_type")
if _type == "GROUP":
rtn.append(Group._from_graphql(node))
rtn.append(Group._from_graphql(self.session, node))
elif _type == "ONE_TO_ONE":
rtn.append(User._from_thread_fetch(node))
rtn.append(User._from_thread_fetch(self.session, node))
else:
raise FBchatException(
"Unknown thread type: {}, with data: {}".format(_type, node)