Implement [MSC3664](https://github.com/matrix-org/matrix-doc/pull/3664). Contributed by Nico.