Run `black` on synapse/handlers/user_directory.py (#4812)

This got done on the develop branch in #4635, but the subsequent merge to
hotfixes (88af0317a) discarded the changes for some reason.

Fixing this here and now means (a) there are fewer differences between
matrix-org-hotfixes and develop, making future patches easier to merge, and (b)
fixes some pep8 errors on the hotfixes branch which have been annoying me for
some time.
matrix-org-hotfixes-identity
Richard van der Hoff 2019-03-06 11:56:03 +00:00 committed by GitHub
parent c7285607a3
commit 9e9572c79e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 43 deletions

View File

@ -14,7 +14,6 @@
# limitations under the License. # limitations under the License.
import logging import logging
import synapse.metrics
from six import iteritems from six import iteritems
@ -29,6 +28,7 @@ from synapse.util.metrics import Measure
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class UserDirectoryHandler(object): class UserDirectoryHandler(object):
"""Handles querying of and keeping updated the user_directory. """Handles querying of and keeping updated the user_directory.
@ -130,7 +130,7 @@ class UserDirectoryHandler(object):
# Support users are for diagnostics and should not appear in the user directory. # Support users are for diagnostics and should not appear in the user directory.
if not is_support: if not is_support:
yield self.store.update_profile_in_user_dir( yield self.store.update_profile_in_user_dir(
user_id, profile.display_name, profile.avatar_url, None, user_id, profile.display_name, profile.avatar_url, None
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -166,8 +166,9 @@ class UserDirectoryHandler(object):
self.pos = deltas[-1]["stream_id"] self.pos = deltas[-1]["stream_id"]
# Expose current event processing position to prometheus # Expose current event processing position to prometheus
synapse.metrics.event_processing_positions.labels( synapse.metrics.event_processing_positions.labels("user_dir").set(
"user_dir").set(self.pos) self.pos
)
yield self.store.update_user_directory_stream_pos(self.pos) yield self.store.update_user_directory_stream_pos(self.pos)
@ -191,21 +192,25 @@ class UserDirectoryHandler(object):
logger.info("Handling room %d/%d", num_processed_rooms + 1, len(room_ids)) logger.info("Handling room %d/%d", num_processed_rooms + 1, len(room_ids))
yield self._handle_initial_room(room_id) yield self._handle_initial_room(room_id)
num_processed_rooms += 1 num_processed_rooms += 1
yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.) yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.0)
logger.info("Processed all rooms.") logger.info("Processed all rooms.")
if self.search_all_users: if self.search_all_users:
num_processed_users = 0 num_processed_users = 0
user_ids = yield self.store.get_all_local_users() user_ids = yield self.store.get_all_local_users()
logger.info("Doing initial update of user directory. %d users", len(user_ids)) logger.info(
"Doing initial update of user directory. %d users", len(user_ids)
)
for user_id in user_ids: for user_id in user_ids:
# We add profiles for all users even if they don't match the # We add profiles for all users even if they don't match the
# include pattern, just in case we want to change it in future # include pattern, just in case we want to change it in future
logger.info("Handling user %d/%d", num_processed_users + 1, len(user_ids)) logger.info(
"Handling user %d/%d", num_processed_users + 1, len(user_ids)
)
yield self._handle_local_user(user_id) yield self._handle_local_user(user_id)
num_processed_users += 1 num_processed_users += 1
yield self.clock.sleep(self.INITIAL_USER_SLEEP_MS / 1000.) yield self.clock.sleep(self.INITIAL_USER_SLEEP_MS / 1000.0)
logger.info("Processed all users") logger.info("Processed all users")
@ -224,24 +229,24 @@ class UserDirectoryHandler(object):
if not is_in_room: if not is_in_room:
return return
is_public = yield self.store.is_room_world_readable_or_publicly_joinable(room_id) is_public = yield self.store.is_room_world_readable_or_publicly_joinable(
room_id
)
users_with_profile = yield self.state.get_current_user_in_room(room_id) users_with_profile = yield self.state.get_current_user_in_room(room_id)
user_ids = set(users_with_profile) user_ids = set(users_with_profile)
unhandled_users = user_ids - self.initially_handled_users unhandled_users = user_ids - self.initially_handled_users
yield self.store.add_profiles_to_user_dir( yield self.store.add_profiles_to_user_dir(
room_id, { room_id,
user_id: users_with_profile[user_id] for user_id in unhandled_users {user_id: users_with_profile[user_id] for user_id in unhandled_users},
}
) )
self.initially_handled_users |= unhandled_users self.initially_handled_users |= unhandled_users
if is_public: if is_public:
yield self.store.add_users_to_public_room( yield self.store.add_users_to_public_room(
room_id, room_id, user_ids=user_ids - self.initially_handled_users_in_public
user_ids=user_ids - self.initially_handled_users_in_public
) )
self.initially_handled_users_in_public |= user_ids self.initially_handled_users_in_public |= user_ids
@ -253,7 +258,7 @@ class UserDirectoryHandler(object):
count = 0 count = 0
for user_id in user_ids: for user_id in user_ids:
if count % self.INITIAL_ROOM_SLEEP_COUNT == 0: if count % self.INITIAL_ROOM_SLEEP_COUNT == 0:
yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.) yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.0)
if not self.is_mine_id(user_id): if not self.is_mine_id(user_id):
count += 1 count += 1
@ -268,7 +273,7 @@ class UserDirectoryHandler(object):
continue continue
if count % self.INITIAL_ROOM_SLEEP_COUNT == 0: if count % self.INITIAL_ROOM_SLEEP_COUNT == 0:
yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.) yield self.clock.sleep(self.INITIAL_ROOM_SLEEP_MS / 1000.0)
count += 1 count += 1
user_set = (user_id, other_user_id) user_set = (user_id, other_user_id)
@ -290,25 +295,23 @@ class UserDirectoryHandler(object):
if len(to_insert) > self.INITIAL_ROOM_BATCH_SIZE: if len(to_insert) > self.INITIAL_ROOM_BATCH_SIZE:
yield self.store.add_users_who_share_room( yield self.store.add_users_who_share_room(
room_id, not is_public, to_insert, room_id, not is_public, to_insert
) )
to_insert.clear() to_insert.clear()
if len(to_update) > self.INITIAL_ROOM_BATCH_SIZE: if len(to_update) > self.INITIAL_ROOM_BATCH_SIZE:
yield self.store.update_users_who_share_room( yield self.store.update_users_who_share_room(
room_id, not is_public, to_update, room_id, not is_public, to_update
) )
to_update.clear() to_update.clear()
if to_insert: if to_insert:
yield self.store.add_users_who_share_room( yield self.store.add_users_who_share_room(room_id, not is_public, to_insert)
room_id, not is_public, to_insert,
)
to_insert.clear() to_insert.clear()
if to_update: if to_update:
yield self.store.update_users_who_share_room( yield self.store.update_users_who_share_room(
room_id, not is_public, to_update, room_id, not is_public, to_update
) )
to_update.clear() to_update.clear()
@ -329,11 +332,12 @@ class UserDirectoryHandler(object):
# may have become public or not and add/remove the users in said room # may have become public or not and add/remove the users in said room
if typ in (EventTypes.RoomHistoryVisibility, EventTypes.JoinRules): if typ in (EventTypes.RoomHistoryVisibility, EventTypes.JoinRules):
yield self._handle_room_publicity_change( yield self._handle_room_publicity_change(
room_id, prev_event_id, event_id, typ, room_id, prev_event_id, event_id, typ
) )
elif typ == EventTypes.Member: elif typ == EventTypes.Member:
change = yield self._get_key_change( change = yield self._get_key_change(
prev_event_id, event_id, prev_event_id,
event_id,
key_name="membership", key_name="membership",
public_value=Membership.JOIN, public_value=Membership.JOIN,
) )
@ -343,7 +347,7 @@ class UserDirectoryHandler(object):
if change is None: if change is None:
# Handle any profile changes # Handle any profile changes
yield self._handle_profile_change( yield self._handle_profile_change(
state_key, room_id, prev_event_id, event_id, state_key, room_id, prev_event_id, event_id
) )
continue continue
@ -375,13 +379,15 @@ class UserDirectoryHandler(object):
if typ == EventTypes.RoomHistoryVisibility: if typ == EventTypes.RoomHistoryVisibility:
change = yield self._get_key_change( change = yield self._get_key_change(
prev_event_id, event_id, prev_event_id,
event_id,
key_name="history_visibility", key_name="history_visibility",
public_value="world_readable", public_value="world_readable",
) )
elif typ == EventTypes.JoinRules: elif typ == EventTypes.JoinRules:
change = yield self._get_key_change( change = yield self._get_key_change(
prev_event_id, event_id, prev_event_id,
event_id,
key_name="join_rule", key_name="join_rule",
public_value=JoinRules.PUBLIC, public_value=JoinRules.PUBLIC,
) )
@ -506,7 +512,7 @@ class UserDirectoryHandler(object):
) )
if self.is_mine_id(other_user_id) and not is_appservice: if self.is_mine_id(other_user_id) and not is_appservice:
shared_is_private = yield self.store.get_if_users_share_a_room( shared_is_private = yield self.store.get_if_users_share_a_room(
other_user_id, user_id, other_user_id, user_id
) )
if shared_is_private is True: if shared_is_private is True:
# We've already marked in the database they share a private room # We've already marked in the database they share a private room
@ -521,13 +527,11 @@ class UserDirectoryHandler(object):
to_insert.add((other_user_id, user_id)) to_insert.add((other_user_id, user_id))
if to_insert: if to_insert:
yield self.store.add_users_who_share_room( yield self.store.add_users_who_share_room(room_id, not is_public, to_insert)
room_id, not is_public, to_insert,
)
if to_update: if to_update:
yield self.store.update_users_who_share_room( yield self.store.update_users_who_share_room(
room_id, not is_public, to_update, room_id, not is_public, to_update
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -546,15 +550,15 @@ class UserDirectoryHandler(object):
row = yield self.store.get_user_in_public_room(user_id) row = yield self.store.get_user_in_public_room(user_id)
update_user_in_public = row and row["room_id"] == room_id update_user_in_public = row and row["room_id"] == room_id
if (update_user_in_public or update_user_dir): if update_user_in_public or update_user_dir:
# XXX: Make this faster? # XXX: Make this faster?
rooms = yield self.store.get_rooms_for_user(user_id) rooms = yield self.store.get_rooms_for_user(user_id)
for j_room_id in rooms: for j_room_id in rooms:
if (not update_user_in_public and not update_user_dir): if not update_user_in_public and not update_user_dir:
break break
is_in_room = yield self.store.is_host_joined( is_in_room = yield self.store.is_host_joined(
j_room_id, self.server_name, j_room_id, self.server_name
) )
if not is_in_room: if not is_in_room:
@ -582,19 +586,19 @@ class UserDirectoryHandler(object):
# Get a list of user tuples that were in the DB due to this room and # Get a list of user tuples that were in the DB due to this room and
# users (this includes tuples where the other user matches `user_id`) # users (this includes tuples where the other user matches `user_id`)
user_tuples = yield self.store.get_users_in_share_dir_with_room_id( user_tuples = yield self.store.get_users_in_share_dir_with_room_id(
user_id, room_id, user_id, room_id
) )
for user_id, other_user_id in user_tuples: for user_id, other_user_id in user_tuples:
# For each user tuple get a list of rooms that they still share, # For each user tuple get a list of rooms that they still share,
# trying to find a private room, and update the entry in the DB # trying to find a private room, and update the entry in the DB
rooms = yield self.store.get_rooms_in_common_for_users(user_id, other_user_id) rooms = yield self.store.get_rooms_in_common_for_users(
user_id, other_user_id
)
# If they dont share a room anymore, remove the mapping # If they dont share a room anymore, remove the mapping
if not rooms: if not rooms:
yield self.store.remove_user_who_share_room( yield self.store.remove_user_who_share_room(user_id, other_user_id)
user_id, other_user_id,
)
continue continue
found_public_share = None found_public_share = None
@ -608,13 +612,13 @@ class UserDirectoryHandler(object):
else: else:
found_public_share = None found_public_share = None
yield self.store.update_users_who_share_room( yield self.store.update_users_who_share_room(
room_id, not is_public, [(user_id, other_user_id)], room_id, not is_public, [(user_id, other_user_id)]
) )
break break
if found_public_share: if found_public_share:
yield self.store.update_users_who_share_room( yield self.store.update_users_who_share_room(
room_id, not is_public, [(user_id, other_user_id)], room_id, not is_public, [(user_id, other_user_id)]
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -642,7 +646,7 @@ class UserDirectoryHandler(object):
if prev_name != new_name or prev_avatar != new_avatar: if prev_name != new_name or prev_avatar != new_avatar:
yield self.store.update_profile_in_user_dir( yield self.store.update_profile_in_user_dir(
user_id, new_name, new_avatar, room_id, user_id, new_name, new_avatar, room_id
) )
@defer.inlineCallbacks @defer.inlineCallbacks