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

Add 2 methods: fetch all threads, fetch all users involved in threads #300

Merged
merged 13 commits into from
Jan 31, 2019
44 changes: 44 additions & 0 deletions fbchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,51 @@ def resetDefaultThread(self):
"""
FETCH METHODS
"""

def fetchThreads(self, thread_location):
"""
Get all threads in thread_location.

:param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
:return: :class:`models.Thread` objects
:rtype: list
:raises: FBchatException if request failed
"""
Threads = []
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
Threads += self.fetchThreadList(thread_location=thread_location)
if len(Threads) == 0:
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
return []
while True:
lastThreadTimestamp = Threads[-1].last_message_timestamp
candidates = self.fetchThreadList(before=lastThreadTimestamp, thread_location=thread_location) # return at max 20 threads before lastThreadTimestamp (included)
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
if len(candidates) > 1:
Threads += candidates[1:]
else:
break
return Threads # from newest to oldest

def fetchAllUsersFromThreads(self, threads):
"""
Get all users involved in threads.

:param threads: models.Thread: List of threads to check for users
:return: :class:`models.User` objects
:rtype: list
:raises: FBchatException if request failed
"""
Users = []
for thread in threads:
if thread.type == ThreadType.USER:
if thread.uid not in [user.uid for user in Users]:
Users.append(thread)
elif thread.type == ThreadType.GROUP:
for userID in thread.participants:
if userID not in [user.uid for user in Users]:
Users.append(self.fetchUserInfo(userID)[userID])
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
else:
pass
return Users

def fetchAllUsers(self):
"""
Gets all users the client is currently chatting with
Expand Down