Description
Related to #18464
Problem
Room deletion (v2, and possibly v1) will purge the room upon deletion. It will first send a bunch of kick events, which are intended to go to clients to let them know to remove the room from their room list. The problem is however immediately after sending these the room is purged from the database so unless you are federated or happened to be syncing exactly as the leave was sent, you will miss the leave event. The leave events are purged along with the room itself.
Subsequent syncs made after the room is purged never include the leave event, so any clients that did not sync will be stuck with broken state.
Proposed solution
Synapse should generate synthetic leave events for users with devices that existed at the point in time the room was alive. Rather than relying on events in the room which may be purged, the homeserver should keep a mapping of user, device and room_id of deleted rooms that can be popped as the device syncs to ensure they see a leave down the sync in all cases. This minimises the retention of data onto the server with the bare minimum required to execute the leaves, without leaving any metadata.
To keep in mind that quite like device messages, these rows could hang around forever if devices do not sync so a sensible cleanup period should be incorporated (perhaps a month, or so) where these rows are purged anyway.