Fetch missing users in a single request

This commit is contained in:
2FWAH
2018-06-12 08:38:02 +02:00
committed by GitHub
parent e0bb9960fb
commit 2edb95dfdd

View File

@@ -473,7 +473,7 @@ class Client(object):
FETCH METHODS FETCH METHODS
""" """
def fetchThreads(self, thread_location): def fetchThreads(self, thread_location, after=None, limit=None):
""" """
Get all threads in thread_location. Get all threads in thread_location.
@@ -505,16 +505,19 @@ class Client(object):
:raises: FBchatException if request failed :raises: FBchatException if request failed
""" """
users = [] users = []
users_to_fetch = [] # It's more efficient to fetch all users in one request
for thread in threads: for thread in threads:
if thread.type == ThreadType.USER: if thread.type == ThreadType.USER:
if thread.uid not in [user.uid for user in users]: if thread.uid not in [user.uid for user in users]:
users.append(thread) users.append(thread)
elif thread.type == ThreadType.GROUP: elif thread.type == ThreadType.GROUP:
for userID in thread.participants: for user_id in thread.participants:
if userID not in [user.uid for user in users]: if user_id not in [user.uid for user in users] and user_id not in users_to_fetch:
users.append(self.fetchUserInfo(userID)[userID]) users_to_fetch.append(user_id)
else: else:
pass pass
for user_id,user in self.fetchUserInfo(*users_to_fetch).items():
users.append(user)
return users return users
def fetchAllUsers(self): def fetchAllUsers(self):