Add before, after and limit parameters to fetchThreads

This commit is contained in:
2FWAH
2018-09-21 19:12:46 +02:00
parent d4859b675a
commit 5fa1d86191

View File

@@ -487,11 +487,15 @@ class Client(object):
})) }))
return j return j
def fetchThreads(self, thread_location, after=None, limit=None): def fetchThreads(self, thread_location, before=None, after=None, limit=None):
""" """
Get all threads in thread_location. Get all threads in thread_location.
Threads will be sorted from newest to oldest.
:param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER :param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
:param before: Fetch only thread before this epoch (in ms) (default all threads)
:param after: Fetch only thread after this epoch (in ms) (default all threads)
:param limit: The max. amount of threads to fetch (default all threads)
:return: :class:`models.Thread` objects :return: :class:`models.Thread` objects
:rtype: list :rtype: list
:raises: FBchatException if request failed :raises: FBchatException if request failed
@@ -500,15 +504,40 @@ class Client(object):
threads += self.fetchThreadList(thread_location=thread_location) threads += self.fetchThreadList(thread_location=thread_location)
if not threads: if not threads:
return [] return []
while True: while True:
lastThreadTimestamp = threads[-1].last_message_timestamp # break if limit is exceeded
candidates = self.fetchThreadList(before=lastThreadTimestamp, thread_location=thread_location) # return at max 20 threads before lastThreadTimestamp (included) if limit and len(threads) >= limit:
break
last_thread_timestamp = threads[-1].last_message_timestamp
# fetchThreadList returns at max 20 threads before last_thread_timestamp (included)
candidates = self.fetchThreadList(before=last_thread_timestamp,
thread_location=thread_location
)
if len(candidates) > 1: if len(candidates) > 1:
threads += candidates[1:] threads += candidates[1:]
else: else: # End of threads
break break
return threads # from newest to oldest
# FB returns a sorted list of threads
if (before is not None and int(last_thread_timestamp) > before) or \
(after is not None and int(last_thread_timestamp) < after):
break
# Return only threads between before and after (if set)
if before is not None or after is not None:
for t in list(threads):
last_message_timestamp = int(t.last_message_timestamp)
if (before is not None and last_message_timestamp > before) or \
(after is not None and last_message_timestamp < after):
threads.remove(t)
if limit and len(threads) > limit:
return threads[:limit]
return threads
def fetchAllUsersFromThreads(self, threads): def fetchAllUsersFromThreads(self, threads):
""" """
Get all users involved in threads. Get all users involved in threads.