Added GraphQL alternative to fetchThreadList; fixes #241

This commit is contained in:
Marco Gavelli
2018-02-17 14:29:31 +01:00
parent 3142524809
commit c12dcd9263
3 changed files with 54 additions and 0 deletions

View File

@@ -754,6 +754,36 @@ class Client(object):
return list(reversed([graphql_to_message(message) for message in j['message_thread']['messages']['nodes']]))
def fetchThreadListGraphQL(self, limit=20, thread_location=ThreadLocation.INBOX, before=None):
"""Get thread list of your facebook account
:param limit: Max. number of threads to retrieve. Capped at 20
:param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
:param before: A timestamp, indicating from which point to retrieve messages
:type limit: int
:type before: int
:return: :class:`models.Thread` objects
:rtype: list
:raises: FBchatException if request failed
"""
if limit > 20 or limit < 1:
raise FBchatUserError('`limit` should be between 1 and 20')
if thread_location in ThreadLocation:
loc_str = thread_location.value
else:
raise FBchatUserError('"thread_location" must be a value of ThreadLocation')
j = self.graphql_request(GraphQL(doc_id='1349387578499440', params={
'limit': limit,
'tags': [loc_str],
'before': before,
'includeDeliveryReceipts': True,
'includeSeqID': False}))
return [graphql_to_thread(node) for node in j['viewer']['message_threads']['nodes']]
def fetchThreadList(self, offset=0, limit=20, thread_location=ThreadLocation.INBOX):
"""Get thread list of your facebook account

View File

@@ -172,6 +172,26 @@ def graphql_to_user(user):
message_count=user.get('messages_count')
)
def graphql_to_thread(thread):
if thread['thread_type'] == 'GROUP':
return graphql_to_group(thread)
elif thread['thread_type'] == 'ONE_TO_ONE':
if thread.get('image') is None:
thread['image'] = {}
c_info = get_customization_info(thread)
return Group(
thread['thread_key']['other_user_id'],
participants=set([node['messaging_actor']['id'] for node in thread['all_participants']['nodes']]),
nicknames=c_info.get('nicknames'),
color=c_info.get('color'),
emoji=c_info.get('emoji'),
photo=thread['image'].get('uri'),
name=thread.get('name'),
message_count=thread.get('messages_count')
)
else:
raise FBchatException('Unknown thread type: {}, with data: {}'.format(thread.get('thread_type'), thread))
def graphql_to_group(group):
if group.get('image') is None:
group['image'] = {}