Compare commits

...

2 Commits

Author SHA1 Message Date
Brendan Abolivier 7216a7a95f
Lint 2020-04-02 19:42:17 +02:00
Brendan Abolivier 3da9f9a653
Incorporate review 2020-04-02 19:30:29 +02:00
2 changed files with 34 additions and 45 deletions

View File

@ -80,9 +80,6 @@ class ResourceLimitsServerNotices(object):
# In practice, not sure we can ever get here # In practice, not sure we can ever get here
return return
# Retrieve or create a server notices room for this user. This function is
# cached, so if the user hasn't been invited to a room previously created yet,
# a new one won't be created.
room_id = yield self._server_notices_manager.get_or_create_notice_room_for_user( room_id = yield self._server_notices_manager.get_or_create_notice_room_for_user(
user_id user_id
) )

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging import logging
from typing import Optional
from twisted.internet import defer from twisted.internet import defer
@ -70,7 +69,7 @@ class ServerNoticesManager(object):
Deferred[FrozenEvent] Deferred[FrozenEvent]
""" """
room_id = yield self.get_or_create_notice_room_for_user(user_id) room_id = yield self.get_or_create_notice_room_for_user(user_id)
yield self.invite_user_in_notice_room(user_id, room_id) yield self.invite_user_to_notice_room(user_id, room_id)
system_mxid = self._config.server_notices_mxid system_mxid = self._config.server_notices_mxid
requester = create_requester(system_mxid) requester = create_requester(system_mxid)
@ -96,7 +95,8 @@ class ServerNoticesManager(object):
def get_or_create_notice_room_for_user(self, user_id): def get_or_create_notice_room_for_user(self, user_id):
"""Get the room for notices for a given user """Get the room for notices for a given user
If we have not yet created a notice room for this user, create it If we have not yet created a notice room for this user, create it, but don't
invite the user to it.
Args: Args:
user_id (str): complete user id for the user we want a room for user_id (str): complete user id for the user we want a room for
@ -109,9 +109,25 @@ class ServerNoticesManager(object):
assert self._is_mine_id(user_id), "Cannot send server notices to remote users" assert self._is_mine_id(user_id), "Cannot send server notices to remote users"
existing_room_id = yield self._get_existing_notice_room_for_user(user_id) rooms = yield self._store.get_rooms_for_local_user_where_membership_is(
if existing_room_id: user_id, [Membership.INVITE, Membership.JOIN]
return existing_room_id )
for room in rooms:
# it's worth noting that there is an asymmetry here in that we
# expect the user to be invited or joined, but the system user must
# be joined. This is kinda deliberate, in that if somebody somehow
# manages to invite the system user to a room, that doesn't make it
# the server notices room.
user_ids = yield self._store.get_users_in_room(room.room_id)
if self.server_notices_mxid in user_ids:
# we found a room which our user shares with the system notice
# user
logger.info(
"Using existing server notices room %s for user %s",
room.room_id,
user_id,
)
return room.room_id
# apparently no existing notice room: create a new one # apparently no existing notice room: create a new one
logger.info("Creating server notices room for %s", user_id) logger.info("Creating server notices room for %s", user_id)
@ -151,37 +167,9 @@ class ServerNoticesManager(object):
return room_id return room_id
@defer.inlineCallbacks @defer.inlineCallbacks
def _get_existing_notice_room_for_user(self, user_id): def invite_user_to_notice_room(self, user_id: str, room_id: str):
# type: (str) -> defer.Deferred[Optional[str]] """Invite the given user to the given server notices room, unless the user has
"""Retrieve the ID for the existing notice room for the given user ID if it already joined or been invited to this room.
exists.
Returns:
The ID of the notice room, None if no room exist for this user.
"""
room_id = None
rooms = yield self._store.get_rooms_for_local_user_where_membership_is(
user_id, [Membership.INVITE, Membership.JOIN]
)
for room in rooms:
# it's worth noting that there is an asymmetry here in that we
# expect the user to be invited or joined, but the system user must
# be joined. This is kinda deliberate, in that if somebody somehow
# manages to invite the system user to a room, that doesn't make it
# the server notices room.
user_ids = yield self._store.get_users_in_room(room.room_id)
if self.server_notices_mxid in user_ids:
# we found a room which our user shares with the system notice
# user
logger.info("Using room %s", room.room_id)
room_id = room.room_id
return room_id
@defer.inlineCallbacks
def invite_user_in_notice_room(self, user_id: str, room_id: str):
"""Invite the given user to the given server notices room.
Args: Args:
user_id: The ID of the user to invite. user_id: The ID of the user to invite.
@ -189,11 +177,15 @@ class ServerNoticesManager(object):
""" """
requester = create_requester(self.server_notices_mxid) requester = create_requester(self.server_notices_mxid)
# Make sure a notice room doesn't already exist for this user, otherwise the auth # Check whether the user has already joined or been invited to this room. If
# checks will make the current request (i.e. sync) fail. # that's the case, don't invite because otherwise it would make the auth checks
existing_room_id = yield self._get_existing_notice_room_for_user(user_id) # fail.
if existing_room_id: joined_rooms = yield self._store.get_rooms_for_local_user_where_membership_is(
return user_id, [Membership.INVITE, Membership.JOIN]
)
for room in joined_rooms:
if room.room_id == room_id:
return
yield self._room_member_handler.update_membership( yield self._room_member_handler.update_membership(
requester=requester, requester=requester,