2021-01-11 22:43:58 +01:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
import logging
|
|
|
|
from typing import Dict, List
|
2021-01-07 22:01:59 +01:00
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
from synapse.api.errors import SynapseError
|
2021-01-07 22:01:59 +01:00
|
|
|
from synapse.storage._base import SQLBaseStore
|
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-01-07 22:01:59 +01:00
|
|
|
|
2021-01-07 23:12:23 +01:00
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
class EventForwardExtremitiesStore(SQLBaseStore):
|
2021-01-07 23:12:23 +01:00
|
|
|
async def delete_forward_extremities_for_room(self, room_id: str) -> int:
|
|
|
|
"""Delete any extra forward extremities for a room.
|
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
Invalidates the "get_latest_event_ids_in_room" cache if any forward
|
|
|
|
extremities were deleted.
|
|
|
|
|
2021-01-07 23:12:23 +01:00
|
|
|
Returns count deleted.
|
|
|
|
"""
|
2021-01-09 20:57:41 +01:00
|
|
|
|
2021-01-07 23:12:23 +01:00
|
|
|
def delete_forward_extremities_for_room_txn(txn):
|
|
|
|
# First we need to get the event_id to not delete
|
2021-01-11 22:43:58 +01:00
|
|
|
sql = """
|
2021-01-11 22:49:58 +01:00
|
|
|
SELECT event_id FROM event_forward_extremities
|
|
|
|
INNER JOIN events USING (room_id, event_id)
|
2021-01-11 22:43:58 +01:00
|
|
|
WHERE room_id = ?
|
2021-01-11 22:49:58 +01:00
|
|
|
ORDER BY stream_ordering DESC
|
|
|
|
LIMIT 1
|
2021-01-11 22:43:58 +01:00
|
|
|
"""
|
2021-01-07 23:12:23 +01:00
|
|
|
txn.execute(sql, (room_id,))
|
|
|
|
rows = txn.fetchall()
|
2021-01-09 20:57:41 +01:00
|
|
|
try:
|
|
|
|
event_id = rows[0][0]
|
|
|
|
logger.debug(
|
|
|
|
"Found event_id %s as the forward extremity to keep for room %s",
|
|
|
|
event_id,
|
|
|
|
room_id,
|
|
|
|
)
|
|
|
|
except KeyError:
|
2021-01-11 08:47:03 +01:00
|
|
|
msg = "No forward extremity event found for room %s" % room_id
|
2021-01-09 20:57:41 +01:00
|
|
|
logger.warning(msg)
|
|
|
|
raise SynapseError(400, msg)
|
2021-01-07 23:12:23 +01:00
|
|
|
|
|
|
|
# Now delete the extra forward extremities
|
2021-01-11 22:43:58 +01:00
|
|
|
sql = """
|
|
|
|
DELETE FROM event_forward_extremities
|
|
|
|
WHERE event_id != ? AND room_id = ?
|
|
|
|
"""
|
2021-01-07 23:12:23 +01:00
|
|
|
|
|
|
|
txn.execute(sql, (event_id, room_id))
|
2021-01-09 20:57:41 +01:00
|
|
|
logger.info(
|
|
|
|
"Deleted %s extra forward extremities for room %s",
|
|
|
|
txn.rowcount,
|
|
|
|
room_id,
|
|
|
|
)
|
2021-01-07 23:12:23 +01:00
|
|
|
|
2021-01-09 20:57:41 +01:00
|
|
|
if txn.rowcount > 0:
|
|
|
|
# Invalidate the cache
|
|
|
|
self._invalidate_cache_and_stream(
|
2021-02-16 23:32:34 +01:00
|
|
|
txn,
|
|
|
|
self.get_latest_event_ids_in_room,
|
|
|
|
(room_id,),
|
2021-01-09 20:57:41 +01:00
|
|
|
)
|
2021-01-07 23:12:23 +01:00
|
|
|
|
|
|
|
return txn.rowcount
|
|
|
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2021-01-09 20:57:41 +01:00
|
|
|
"delete_forward_extremities_for_room",
|
|
|
|
delete_forward_extremities_for_room_txn,
|
2021-01-07 23:12:23 +01:00
|
|
|
)
|
|
|
|
|
2021-01-07 22:01:59 +01:00
|
|
|
async def get_forward_extremities_for_room(self, room_id: str) -> List[Dict]:
|
2021-01-07 23:12:23 +01:00
|
|
|
"""Get list of forward extremities for a room."""
|
2021-01-09 20:57:41 +01:00
|
|
|
|
2021-01-07 22:01:59 +01:00
|
|
|
def get_forward_extremities_for_room_txn(txn):
|
2021-01-11 22:43:58 +01:00
|
|
|
sql = """
|
2021-01-23 20:34:32 +01:00
|
|
|
SELECT event_id, state_group, depth, received_ts
|
2021-01-11 22:55:44 +01:00
|
|
|
FROM event_forward_extremities
|
2021-01-26 09:13:35 +01:00
|
|
|
INNER JOIN event_to_state_groups USING (event_id)
|
2021-01-26 09:21:02 +01:00
|
|
|
INNER JOIN events USING (room_id, event_id)
|
2021-01-11 22:43:58 +01:00
|
|
|
WHERE room_id = ?
|
|
|
|
"""
|
2021-01-07 22:01:59 +01:00
|
|
|
|
|
|
|
txn.execute(sql, (room_id,))
|
2021-01-11 22:43:58 +01:00
|
|
|
return self.db_pool.cursor_to_dict(txn)
|
2021-01-07 22:01:59 +01:00
|
|
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2021-02-16 23:32:34 +01:00
|
|
|
"get_forward_extremities_for_room",
|
|
|
|
get_forward_extremities_for_room_txn,
|
2021-01-07 22:01:59 +01:00
|
|
|
)
|