Add Rooms admin forward extremities DELETE endpoint

Signed-off-by: Jason Robinson <jasonr@matrix.org>
This commit is contained in:
Jason Robinson 2021-01-08 00:12:23 +02:00
parent c91045f56c
commit 85c0999bfb
2 changed files with 53 additions and 1 deletions

View File

@ -543,6 +543,11 @@ class ForwardExtremitiesRestServlet(RestServlet):
room_id = await self.resolve_room_id(room_identifier) room_id = await self.resolve_room_id(room_identifier)
deleted_count = await self.store.delete_forward_extremities_for_room(room_id)
return 200, {
"deleted": deleted_count,
}
async def on_GET(self, request, room_identifier): async def on_GET(self, request, room_identifier):
requester = await self.auth.get_user_by_req(request) requester = await self.auth.get_user_by_req(request)
await assert_user_is_admin(self.auth, requester.user) await assert_user_is_admin(self.auth, requester.user)

View File

@ -4,7 +4,54 @@ from synapse.storage._base import SQLBaseStore
class EventForwardExtremitiesStore(SQLBaseStore): class EventForwardExtremitiesStore(SQLBaseStore):
async def delete_forward_extremities_for_room(self, room_id: str) -> int:
"""Delete any extra forward extremities for a room.
Returns count deleted.
"""
def delete_forward_extremities_for_room_txn(txn):
# First we need to get the event_id to not delete
sql = (
"SELECT "
" last_value(event_id) OVER w AS event_id"
" FROM event_forward_extremities"
" NATURAL JOIN events"
" where room_id = ?"
" WINDOW w AS ("
" PARTITION BY room_id"
" ORDER BY stream_ordering"
" range between unbounded preceding and unbounded following"
" )"
" ORDER BY stream_ordering"
)
txn.execute(sql, (room_id,))
rows = txn.fetchall()
# TODO: should this raise a SynapseError instead of better to blow?
event_id = rows[0][0]
# Now delete the extra forward extremities
sql = (
"DELETE FROM event_forward_extremities "
"WHERE"
" event_id != ?"
" AND room_id = ?"
)
# TODO we should not commit yet
txn.execute(sql, (event_id, room_id))
# TODO flush the cache then commit
return txn.rowcount
return await self.db_pool.runInteraction(
"delete_forward_extremities_for_room", delete_forward_extremities_for_room_txn,
)
async def get_forward_extremities_for_room(self, room_id: str) -> List[Dict]: async def get_forward_extremities_for_room(self, room_id: str) -> List[Dict]:
"""Get list of forward extremities for a room."""
def get_forward_extremities_for_room_txn(txn): def get_forward_extremities_for_room_txn(txn):
sql = ( sql = (
"SELECT event_id, state_group FROM event_forward_extremities NATURAL JOIN event_to_state_groups " "SELECT event_id, state_group FROM event_forward_extremities NATURAL JOIN event_to_state_groups "
@ -16,5 +63,5 @@ class EventForwardExtremitiesStore(SQLBaseStore):
return [{"event_id": row[0], "state_group": row[1]} for row in rows] return [{"event_id": row[0], "state_group": row[1]} for row in rows]
return await self.db_pool.runInteraction( return await self.db_pool.runInteraction(
"get_forward_extremities_for_room", get_forward_extremities_for_room_txn "get_forward_extremities_for_room", get_forward_extremities_for_room_txn,
) )