2021-01-11 21:43:58 +00:00
|
|
|
#
|
2023-11-21 20:29:58 +00:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 11:26:48 +00:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2023-11-21 20:29:58 +00:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2021-01-11 21:43:58 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2021-01-09 19:57:41 +00:00
|
|
|
import logging
|
2023-10-26 19:12:28 +00:00
|
|
|
from typing import List, Optional, Tuple, cast
|
2021-01-07 21:01:59 +00:00
|
|
|
|
2021-01-09 19:57:41 +00:00
|
|
|
from synapse.api.errors import SynapseError
|
2021-11-12 15:58:17 +00:00
|
|
|
from synapse.storage.database import LoggingTransaction
|
|
|
|
from synapse.storage.databases.main import CacheInvalidationWorkerStore
|
|
|
|
from synapse.storage.databases.main.event_federation import EventFederationWorkerStore
|
2021-01-07 21:01:59 +00:00
|
|
|
|
2021-01-09 19:57:41 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-01-07 21:01:59 +00:00
|
|
|
|
2021-01-07 22:12:23 +00:00
|
|
|
|
2021-11-12 15:58:17 +00:00
|
|
|
class EventForwardExtremitiesStore(
|
|
|
|
EventFederationWorkerStore,
|
|
|
|
CacheInvalidationWorkerStore,
|
|
|
|
):
|
2021-01-07 22:12:23 +00:00
|
|
|
async def delete_forward_extremities_for_room(self, room_id: str) -> int:
|
|
|
|
"""Delete any extra forward extremities for a room.
|
|
|
|
|
2021-01-09 19:57:41 +00:00
|
|
|
Invalidates the "get_latest_event_ids_in_room" cache if any forward
|
|
|
|
extremities were deleted.
|
|
|
|
|
2021-01-07 22:12:23 +00:00
|
|
|
Returns count deleted.
|
|
|
|
"""
|
2021-01-09 19:57:41 +00:00
|
|
|
|
2021-11-12 15:58:17 +00:00
|
|
|
def delete_forward_extremities_for_room_txn(txn: LoggingTransaction) -> int:
|
2021-01-07 22:12:23 +00:00
|
|
|
# First we need to get the event_id to not delete
|
2021-01-11 21:43:58 +00:00
|
|
|
sql = """
|
2021-01-11 21:49:58 +00:00
|
|
|
SELECT event_id FROM event_forward_extremities
|
|
|
|
INNER JOIN events USING (room_id, event_id)
|
2021-01-11 21:43:58 +00:00
|
|
|
WHERE room_id = ?
|
2021-01-11 21:49:58 +00:00
|
|
|
ORDER BY stream_ordering DESC
|
|
|
|
LIMIT 1
|
2021-01-11 21:43:58 +00:00
|
|
|
"""
|
2021-01-07 22:12:23 +00:00
|
|
|
txn.execute(sql, (room_id,))
|
|
|
|
rows = txn.fetchall()
|
2021-01-09 19:57:41 +00: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 07:47:03 +00:00
|
|
|
msg = "No forward extremity event found for room %s" % room_id
|
2021-01-09 19:57:41 +00:00
|
|
|
logger.warning(msg)
|
|
|
|
raise SynapseError(400, msg)
|
2021-01-07 22:12:23 +00:00
|
|
|
|
|
|
|
# Now delete the extra forward extremities
|
2021-01-11 21:43:58 +00:00
|
|
|
sql = """
|
|
|
|
DELETE FROM event_forward_extremities
|
|
|
|
WHERE event_id != ? AND room_id = ?
|
|
|
|
"""
|
2021-01-07 22:12:23 +00:00
|
|
|
|
|
|
|
txn.execute(sql, (event_id, room_id))
|
2022-04-19 15:49:45 +00:00
|
|
|
|
|
|
|
deleted_count = txn.rowcount
|
2021-01-09 19:57:41 +00:00
|
|
|
logger.info(
|
|
|
|
"Deleted %s extra forward extremities for room %s",
|
2022-04-19 15:49:45 +00:00
|
|
|
deleted_count,
|
2021-01-09 19:57:41 +00:00
|
|
|
room_id,
|
|
|
|
)
|
2021-01-07 22:12:23 +00:00
|
|
|
|
2022-04-19 15:49:45 +00:00
|
|
|
if deleted_count > 0:
|
2021-01-09 19:57:41 +00:00
|
|
|
# Invalidate the cache
|
|
|
|
self._invalidate_cache_and_stream(
|
|
|
|
txn,
|
|
|
|
self.get_latest_event_ids_in_room,
|
|
|
|
(room_id,),
|
|
|
|
)
|
2021-01-07 22:12:23 +00:00
|
|
|
|
2022-04-19 15:49:45 +00:00
|
|
|
return deleted_count
|
2021-01-07 22:12:23 +00:00
|
|
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2021-01-09 19:57:41 +00:00
|
|
|
"delete_forward_extremities_for_room",
|
|
|
|
delete_forward_extremities_for_room_txn,
|
2021-01-07 22:12:23 +00:00
|
|
|
)
|
|
|
|
|
2021-11-12 15:58:17 +00:00
|
|
|
async def get_forward_extremities_for_room(
|
|
|
|
self, room_id: str
|
2023-10-26 19:12:28 +00:00
|
|
|
) -> List[Tuple[str, int, int, Optional[int]]]:
|
|
|
|
"""
|
|
|
|
Get list of forward extremities for a room.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples of event_id, state_group, depth, and received_ts.
|
|
|
|
"""
|
2021-01-09 19:57:41 +00:00
|
|
|
|
2021-11-12 15:58:17 +00:00
|
|
|
def get_forward_extremities_for_room_txn(
|
|
|
|
txn: LoggingTransaction,
|
2023-10-26 19:12:28 +00:00
|
|
|
) -> List[Tuple[str, int, int, Optional[int]]]:
|
2021-01-11 21:43:58 +00:00
|
|
|
sql = """
|
2021-01-23 19:34:32 +00:00
|
|
|
SELECT event_id, state_group, depth, received_ts
|
2021-01-11 21:55:44 +00:00
|
|
|
FROM event_forward_extremities
|
2021-01-26 08:13:35 +00:00
|
|
|
INNER JOIN event_to_state_groups USING (event_id)
|
2021-01-26 08:21:02 +00:00
|
|
|
INNER JOIN events USING (room_id, event_id)
|
2021-01-11 21:43:58 +00:00
|
|
|
WHERE room_id = ?
|
|
|
|
"""
|
2021-01-07 21:01:59 +00:00
|
|
|
|
|
|
|
txn.execute(sql, (room_id,))
|
2023-10-26 19:12:28 +00:00
|
|
|
return cast(List[Tuple[str, int, int, Optional[int]]], txn.fetchall())
|
2021-01-07 21:01:59 +00:00
|
|
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2021-01-07 22:12:23 +00:00
|
|
|
"get_forward_extremities_for_room",
|
|
|
|
get_forward_extremities_for_room_txn,
|
2021-01-07 21:01:59 +00:00
|
|
|
)
|