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

abstracted doc ids using enums #476

Open
wants to merge 2 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
27 changes: 20 additions & 7 deletions fbchat/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def logout(self):

def _forced_fetch(self, thread_id, mid):
params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}}
j, = self.graphql_requests(_graphql.from_doc_id("1768656253222505", params))
j, = self.graphql_requests(
_graphql.from_doc_id(_graphql.DocID.FETCH_INFO, params)
)
return j

def fetch_threads(self, thread_location, before=None, after=None, limit=None):
Expand Down Expand Up @@ -615,7 +617,9 @@ def fetch_thread_info(self, *thread_ids):
"load_read_receipts": False,
"before": None,
}
queries.append(_graphql.from_doc_id("2147762685294928", params))
queries.append(
_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_INFO, params)
)

j = self.graphql_requests(*queries)

Expand Down Expand Up @@ -679,7 +683,9 @@ def fetch_thread_messages(self, thread_id=None, limit=20, before=None):
"load_read_receipts": True,
"before": _util.datetime_to_millis(before) if before else None,
}
j, = self.graphql_requests(_graphql.from_doc_id("1860982147341344", params))
j, = self.graphql_requests(
_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_MESSAGES, params)
)

if j.get("message_thread") is None:
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
Expand Down Expand Up @@ -733,7 +739,9 @@ def fetch_thread_list(
"includeDeliveryReceipts": True,
"includeSeqID": False,
}
j, = self.graphql_requests(_graphql.from_doc_id("1349387578499440", params))
j, = self.graphql_requests(
_graphql.from_doc_id(_graphql.DocID.FETCH_THREAD_LIST, params)
)

rtn = []
for node in j["viewer"]["message_threads"]["nodes"]:
Expand Down Expand Up @@ -853,7 +861,9 @@ def fetch_plan_info(self, plan_id):
return Plan._from_fetch(j)

def _get_private_data(self):
j, = self.graphql_requests(_graphql.from_doc_id("1868889766468115", {}))
j, = self.graphql_requests(
_graphql.from_doc_id(_graphql.DocID.GET_PRIVATE_DATA, {})
)
return j["viewer"]

def get_phone_numbers(self):
Expand Down Expand Up @@ -1370,7 +1380,7 @@ def _users_approval(self, user_ids, approve, thread_id=None):
"surface": "ADMIN_MODEL_APPROVAL_CENTER",
}
j, = self.graphql_requests(
_graphql.from_doc_id("1574519202665847", {"data": data})
_graphql.from_doc_id(_graphql.DocID.USER_APPROVAL, {"data": data})
)

def accept_users_to_group(self, user_ids, thread_id=None):
Expand Down Expand Up @@ -1540,7 +1550,10 @@ def react_to_message(self, message_id, reaction):
"message_id": str(message_id),
"reaction": reaction.value if reaction else None,
}
data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})}
data = {
"doc_id": _graphql.DocID.MESSAGE_REACT,
"variables": json.dumps({"data": data}),
}
j = self._payload_post("/webgraphql/mutation", data)
_util.handle_graphql_errors(j)

Expand Down
11 changes: 11 additions & 0 deletions fbchat/_graphql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import json
import re
from ._core import log
Expand All @@ -24,6 +25,16 @@ def decode(self, s, _w=WHITESPACE.match):
# End shameless copy


class DocID(enum.Enum):
FETCH_INFO = "1768656253222505"
FETCH_THREAD_INFO = "2147762685294928"
FETCH_THREAD_MESSAGES = "1860982147341344"
FETCH_THREAD_LIST = "1349387578499440"
GET_PRIVATE_DATA = "1868889766468115"
USER_APPROVAL = "1574519202665847"
MESSAGE_REACT = "1491398900900362"


def queries_to_json(*queries):
"""
Queries should be a list of GraphQL objects
Expand Down