Fix bug where we incorrectly removed a remote host from the list of hosts in a room when any user from that host left that room even if they weren't the last user from that host in that room

pull/10/head
Erik Johnston 2014-09-12 17:09:55 +01:00
parent ca1ae7cf9b
commit b42fe05c51
1 changed files with 45 additions and 12 deletions

View File

@ -18,6 +18,7 @@ from twisted.internet import defer
from ._base import SQLBaseStore from ._base import SQLBaseStore
from synapse.api.constants import Membership from synapse.api.constants import Membership
from synapse.util.logutils import log_function
import logging import logging
@ -29,8 +30,18 @@ class RoomMemberStore(SQLBaseStore):
def _store_room_member_txn(self, txn, event): def _store_room_member_txn(self, txn, event):
"""Store a room member in the database. """Store a room member in the database.
""" """
target_user_id = event.state_key try:
domain = self.hs.parse_userid(target_user_id).domain target_user_id = event.state_key
domain = self.hs.parse_userid(target_user_id).domain
except:
logger.exception("Failed to parse target_user_id=%s", target_user_id)
raise
logger.debug(
"_store_room_member_txn: target_user_id=%s, membership=%s",
target_user_id,
event.membership,
)
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
@ -51,12 +62,30 @@ class RoomMemberStore(SQLBaseStore):
"VALUES (?, ?)" "VALUES (?, ?)"
) )
txn.execute(sql, (event.room_id, domain)) txn.execute(sql, (event.room_id, domain))
else: elif event.membership != Membership.INVITE:
sql = ( # Check if this was the last person to have left.
"DELETE FROM room_hosts WHERE room_id = ? AND host = ?" member_events = self._get_members_query_txn(
txn,
where_clause="c.room_id = ? AND m.membership = ?",
where_values=(event.room_id, Membership.JOIN,)
) )
txn.execute(sql, (event.room_id, domain)) joined_domains = set()
for e in member_events:
try:
joined_domains.add(
self.hs.parse_userid(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:
sql = (
"DELETE FROM room_hosts WHERE room_id = ? AND host = ?"
)
txn.execute(sql, (event.room_id, domain))
@defer.inlineCallbacks @defer.inlineCallbacks
def get_room_member(self, user_id, room_id): def get_room_member(self, user_id, room_id):
@ -146,8 +175,13 @@ class RoomMemberStore(SQLBaseStore):
vals = where_dict.values() vals = where_dict.values()
return self._get_members_query(clause, vals) return self._get_members_query(clause, vals)
@defer.inlineCallbacks
def _get_members_query(self, where_clause, where_values): def _get_members_query(self, where_clause, where_values):
return self._db_pool.runInteraction(
self._get_members_query_txn,
where_clause, where_values
)
def _get_members_query_txn(self, txn, where_clause, where_values):
sql = ( sql = (
"SELECT e.* FROM events as e " "SELECT e.* FROM events as e "
"INNER JOIN room_memberships as m " "INNER JOIN room_memberships as m "
@ -157,12 +191,11 @@ class RoomMemberStore(SQLBaseStore):
"WHERE %s " "WHERE %s "
) % (where_clause,) ) % (where_clause,)
rows = yield self._execute_and_decode(sql, *where_values) txn.execute(sql, where_values)
rows = self.cursor_to_dict(txn)
# logger.debug("_get_members_query Got rows %s", rows) results = self._parse_events_txn(txn, rows)
return results
results = yield self._parse_events(rows)
defer.returnValue(results)
@defer.inlineCallbacks @defer.inlineCallbacks
def user_rooms_intersect(self, user_list): def user_rooms_intersect(self, user_list):