Sanitize RoomMemberStore

This commit is contained in:
Erik Johnston 2015-03-23 15:29:04 +00:00
parent d5272b1d2c
commit 9a7f496298
2 changed files with 61 additions and 82 deletions

View File

@ -310,25 +310,6 @@ class RoomMemberHandler(BaseHandler):
# paginating # paginating
defer.returnValue(chunk_data) defer.returnValue(chunk_data)
@defer.inlineCallbacks
def get_room_member(self, room_id, member_user_id, auth_user_id):
"""Retrieve a room member from a room.
Args:
room_id : The room the member is in.
member_user_id : The member's user ID
auth_user_id : The user ID of the user making this request.
Returns:
The room member, or None if this member does not exist.
Raises:
SynapseError if something goes wrong.
"""
yield self.auth.check_joined_room(room_id, auth_user_id)
member = yield self.store.get_room_member(user_id=member_user_id,
room_id=room_id)
defer.returnValue(member)
@defer.inlineCallbacks @defer.inlineCallbacks
def change_membership(self, event, context, do_auth=True): def change_membership(self, event, context, do_auth=True):
""" Change the membership status of a user in a room. """ Change the membership status of a user in a room.

View File

@ -74,23 +74,10 @@ class RoomMemberStore(SQLBaseStore):
txn.execute(sql, (event.room_id, domain)) txn.execute(sql, (event.room_id, domain))
elif event.membership != Membership.INVITE: elif event.membership != Membership.INVITE:
# Check if this was the last person to have left. # Check if this was the last person to have left.
member_events = self._get_members_query_txn( joined_domains = self._get_joined_hosts_for_room_txn(
txn, txn, event.room_id
where_clause=("c.room_id = ? AND m.membership = ?"
" AND m.user_id != ?"),
where_values=(event.room_id, Membership.JOIN, target_user_id,)
) )
joined_domains = set()
for e in member_events:
try:
joined_domains.add(
UserID.from_string(e.state_key).domain
)
except:
# FIXME: How do we deal with invalid user ids in the db?
logger.exception("Invalid user_id: %s", event.state_key)
if domain not in joined_domains: if domain not in joined_domains:
sql = ( sql = (
"DELETE FROM room_hosts WHERE room_id = ? AND host = ?" "DELETE FROM room_hosts WHERE room_id = ? AND host = ?"
@ -100,7 +87,6 @@ class RoomMemberStore(SQLBaseStore):
self.get_rooms_for_user.invalidate(target_user_id) self.get_rooms_for_user.invalidate(target_user_id)
@defer.inlineCallbacks
def get_room_member(self, user_id, room_id): def get_room_member(self, user_id, room_id):
"""Retrieve the current state of a room member. """Retrieve the current state of a room member.
@ -110,41 +96,27 @@ class RoomMemberStore(SQLBaseStore):
Returns: Returns:
Deferred: Results in a MembershipEvent or None. Deferred: Results in a MembershipEvent or None.
""" """
rows = yield self._get_members_by_dict({ def f(txn):
"e.room_id": room_id, events = self._get_members_events_txn(
"m.user_id": user_id, txn,
}) room_id,
user_id=user_id,
defer.returnValue(rows[0] if rows else None)
def _get_room_member(self, txn, user_id, room_id):
sql = (
"SELECT e.* FROM events as e"
" INNER JOIN room_memberships as m"
" ON e.event_id = m.event_id"
" INNER JOIN current_state_events as c"
" ON m.event_id = c.event_id"
" WHERE m.user_id = ? and e.room_id = ?"
" LIMIT 1"
) )
txn.execute(sql, (user_id, room_id))
rows = self.cursor_to_dict(txn) return events[0] if events else None
if rows:
return self._parse_events_txn(txn, rows)[0] return self.runInteraction("get_room_member", f)
else:
return None
def get_users_in_room(self, room_id): def get_users_in_room(self, room_id):
def f(txn): def f(txn):
sql = (
"SELECT m.user_id FROM room_memberships as m" rows = self._get_members_rows_txn(
" INNER JOIN current_state_events as c" txn,
" ON m.event_id = c.event_id" room_id=room_id,
" WHERE m.membership = ? AND m.room_id = ?" membership=Membership.JOIN,
) )
txn.execute(sql, (Membership.JOIN, room_id)) return [r["user_id"] for r in rows]
return [r[0] for r in txn.fetchall()]
return self.runInteraction("get_users_in_room", f) return self.runInteraction("get_users_in_room", f)
def get_room_members(self, room_id, membership=None): def get_room_members(self, room_id, membership=None):
@ -159,11 +131,14 @@ class RoomMemberStore(SQLBaseStore):
list of namedtuples representing the members in this room. list of namedtuples representing the members in this room.
""" """
where = {"m.room_id": room_id} def f(txn):
if membership: return self._get_members_events_txn(
where["m.membership"] = membership txn,
room_id,
membership=membership,
)
return self._get_members_by_dict(where) return self.runInteraction("get_room_members", f)
def get_rooms_for_user_where_membership_is(self, user_id, membership_list): def get_rooms_for_user_where_membership_is(self, user_id, membership_list):
""" Get all the rooms for this user where the membership for this user """ Get all the rooms for this user where the membership for this user
@ -209,28 +184,52 @@ class RoomMemberStore(SQLBaseStore):
] ]
def get_joined_hosts_for_room(self, room_id): def get_joined_hosts_for_room(self, room_id):
return self._simple_select_onecol( return self.runInteraction(
"room_hosts", "get_joined_hosts_for_room",
{"room_id": room_id}, self._get_joined_hosts_for_room_txn,
"host", room_id,
desc="get_joined_hosts_for_room",
) )
def _get_members_by_dict(self, where_dict): def _get_joined_hosts_for_room_txn(self, txn, room_id):
clause = " AND ".join("%s = ?" % k for k in where_dict.keys()) rows = self._get_members_rows_txn(
vals = where_dict.values() txn,
return self._get_members_query(clause, vals) room_id, membership=Membership.JOIN
)
joined_domains = set(
UserID.from_string(r["user_id"]).domain
for r in rows
)
return joined_domains
def _get_members_query(self, where_clause, where_values): def _get_members_query(self, where_clause, where_values):
return self.runInteraction( return self.runInteraction(
"get_members_query", self._get_members_query_txn, "get_members_query", self._get_members_events_txn,
where_clause, where_values where_clause, where_values
) )
def _get_members_query_txn(self, txn, where_clause, where_values): def _get_members_events_txn(self, txn, room_id, membership=None, user_id=None):
rows = self._get_members_rows_txn(
txn,
room_id, membership, user_id,
)
return self._get_events_txn(txn, [r["event_id"] for r in rows])
def _get_members_rows_txn(self, txn, room_id, membership=None, user_id=None):
where_clause = "c.room_id = ?"
where_values = [room_id]
if membership:
where_clause += " AND m.membership = ?"
where_values.append(membership)
if user_id:
where_clause += " AND m.user_id = ?"
where_values.append(user_id)
sql = ( sql = (
"SELECT e.* FROM events as e " "SELECT m.* FROM room_memberships as m "
"INNER JOIN room_memberships as m "
"ON e.event_id = m.event_id " "ON e.event_id = m.event_id "
"INNER JOIN current_state_events as c " "INNER JOIN current_state_events as c "
"ON m.event_id = c.event_id " "ON m.event_id = c.event_id "
@ -242,8 +241,7 @@ class RoomMemberStore(SQLBaseStore):
txn.execute(sql, where_values) txn.execute(sql, where_values)
rows = self.cursor_to_dict(txn) rows = self.cursor_to_dict(txn)
results = self._parse_events_txn(txn, rows) return rows
return results
@cached() @cached()
def get_rooms_for_user(self, user_id): def get_rooms_for_user(self, user_id):