Support room version 3

This commit is contained in:
Nyaaori 2021-11-01 08:57:27 +00:00 committed by Timo Kösters
parent d8a3b257f2
commit 4b28146ee7
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
5 changed files with 41 additions and 9 deletions

View file

@ -975,7 +975,8 @@ pub(crate) async fn invite_helper<'a>(
let pub_key_map = RwLock::new(BTreeMap::new());
// We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&response.event) {
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&response.event, &db)
{
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json

View file

@ -151,7 +151,7 @@ impl Globals {
// Supported and stable room versions
let stable_room_versions = vec![RoomVersionId::V6];
// Experimental, partially supported room versions
let unstable_room_versions = vec![RoomVersionId::V4, RoomVersionId::V5];
let unstable_room_versions = vec![RoomVersionId::V3, RoomVersionId::V4, RoomVersionId::V5];
let s = Self {
globals,

View file

@ -3437,4 +3437,27 @@ impl Rooms {
Ok(())
}
/// Returns the room's version.
#[tracing::instrument(skip(self))]
pub fn get_room_version(&self, room_id: &RoomId) -> RoomVersionId {
let create_event = self
.room_state_get(room_id, &StateEventType::RoomCreate, "")
.unwrap();
let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref()
.map(|create_event| {
serde_json::from_str(create_event.content.get()).map_err(|e| {
warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.")
})
})
.transpose()
.unwrap();
create_event_content
.map(|create_event| create_event.room_version)
.expect("Invalid room version")
}
}

View file

@ -1,11 +1,11 @@
use crate::Error;
use crate::{Database, Error};
use ruma::{
events::{
room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyRoomEvent, AnyStateEvent,
AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, RoomEventType, StateEvent,
},
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UInt, UserId,
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, UInt, UserId,
};
use serde::{Deserialize, Serialize};
use serde_json::{
@ -332,16 +332,24 @@ impl Ord for PduEvent {
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json(
pdu: &RawJsonValue,
db: &Database,
) -> crate::Result<(Box<EventId>, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.get()).map_err(|e| {
let value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response")
})?;
let room_id = value
.get("room_id")
.and_then(|id| RoomId::parse(id.as_str()?).ok())
.expect("Invalid room id in event");
let room_version_id = db.rooms.get_room_version(&room_id);
let event_id = format!(
"${}",
// Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
ruma::signatures::reference_hash(&value, &room_version_id)
.expect("ruma can calculate reference hashes")
)
.try_into()

View file

@ -659,7 +659,7 @@ pub async fn send_transaction_message_route(
for pdu in &body.pdus {
// We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(pdu) {
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(pdu, &db) {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json
@ -1859,7 +1859,7 @@ pub(crate) fn fetch_and_handle_outliers<'a>(
Ok(res) => {
warn!("Got {} over federation", next_id);
let (calculated_event_id, value) =
match crate::pdu::gen_event_id_canonical_json(&res.pdu) {
match crate::pdu::gen_event_id_canonical_json(&res.pdu, &db) {
Ok(t) => t,
Err(_) => {
back_off((*next_id).to_owned());
@ -2820,7 +2820,7 @@ async fn create_join_event(
// let mut auth_cache = EventMap::new();
// We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(pdu) {
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(pdu, &db) {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json