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

This commit is contained in:
Erik Johnston 2014-09-12 17:09:55 +01:00
parent ca1ae7cf9b
commit b42fe05c51

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.
""" """
try:
target_user_id = event.state_key target_user_id = event.state_key
domain = self.hs.parse_userid(target_user_id).domain 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,7 +62,25 @@ 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:
# Check if this was the last person to have left.
member_events = self._get_members_query_txn(
txn,
where_clause="c.room_id = ? AND m.membership = ?",
where_values=(event.room_id, Membership.JOIN,)
)
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 = ( sql = (
"DELETE FROM room_hosts WHERE room_id = ? AND host = ?" "DELETE FROM room_hosts WHERE room_id = ? AND host = ?"
) )
@ -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):