Skip to content

Commit

Permalink
optimised for single async call
Browse files Browse the repository at this point in the history
  • Loading branch information
endofline committed Apr 8, 2015
1 parent bc909ef commit 3726795
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions hangups/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,38 @@ def get_all(self):
def add_user_from_conv_part(self, conv_part):
"""Add new User from ClientConversationParticipantData"""
user_ = User.from_conv_part_data(conv_part, self._self_user.id_)
fallback_ids = []
if user_.id_ not in self._user_dict:
logging.warning('Adding fallback User: {}'.format(user_))
fallback_ids.append(user_.id_.chat_id)
self._user_dict[user_.id_] = user_

try:
"""getentitybyid can do multiple queries with a list, do it one-by-one for testing"""
asyncio.async(
self._client.getentitybyid([user_.id_.chat_id])
).add_done_callback(self._on_getentitybyid)
except Exception as e:
logging.info("EXCEPTION: {}".format(e))
if len(fallback_ids) > 0:
asyncio.async(
self._client.getentitybyid(fallback_ids)
).add_done_callback(self._on_getentitybyid)

self._user_dict[user_.id_] = user_
return user_

def _on_getentitybyid(self, future):
response = future.result()

entity_result = response["entity_result"]
first_entity = entity_result[0]["entity"][0]

gaia_id = first_entity["id"]["gaia_id"]
chat_id = first_entity["id"]["chat_id"]
display_name = first_entity["properties"]["display_name"]
first_name = first_entity["properties"]["first_name"]

user_id = UserID(chat_id=chat_id,
gaia_id=gaia_id)

if user_id in self._user_dict:
self._user_dict[user_id] = User(user_id, display_name, first_name, None, [], False)
logging.info("_on_getentitybyid(): updated {}".format(user_id))
list_entity = response["entity"]
for entity in list_entity:
try:
gaia_id = entity["id"]["gaia_id"]
chat_id = entity["id"]["chat_id"]
display_name = entity["properties"]["display_name"]
first_name = entity["properties"]["first_name"]
except KeyError:
logging.warning("incomplete data from getentitybyid: {}".format(entity))
continue

user_id = UserID(chat_id=chat_id,
gaia_id=gaia_id)

if user_id in self._user_dict:
self._user_dict[user_id] = User(user_id, display_name, first_name, None, [], False)

def _on_state_update(self, state_update):
"""Receive a ClientStateUpdate"""
Expand Down

0 comments on commit 3726795

Please sign in to comment.