mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-26 02:17:34 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92fffe9c82 | ||
|
|
11e51300a5 | ||
|
|
ef84e1bb02 | ||
|
|
1887d58df8 | ||
|
|
c66f6f8900 | ||
|
|
902fe7b7ab |
No files matched your search
@@ -43,7 +43,7 @@ jobs:
|
|||||||
name: Renovate
|
name: Renovate
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container:
|
||||||
image: ghcr.io/renovatebot/renovate:41.127.2@sha256:66bc84e2f889025fbb3c9df863500dcc18bc64ac85bcf629d015064377d77f31
|
image: ghcr.io/renovatebot/renovate:41.130.1@sha256:44c4cfceb47d283b5adc654ea445909de4d89e69f94b109e522ca32593a436b5
|
||||||
options: --tmpfs /tmp:exec
|
options: --tmpfs /tmp:exec
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
submodules: false
|
submodules: false
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- uses: https://github.com/cachix/install-nix-action@a809471b5c7c913aa67bec8f459a11a0decc3fce # v31.6.2
|
- uses: https://github.com/cachix/install-nix-action@9280e7aca88deada44c930f1e2c78e21c3ae3edd # v31.7.0
|
||||||
with:
|
with:
|
||||||
nix_path: nixpkgs=channel:nixos-unstable
|
nix_path: nixpkgs=channel:nixos-unstable
|
||||||
|
|
||||||
|
|||||||
+113
-38
@@ -2,7 +2,7 @@ use std::cmp::max;
|
|||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use conduwuit::{
|
use conduwuit::{
|
||||||
Err, Error, Event, Result, debug, err, info,
|
Err, Error, Event, Result, RoomVersion, debug, err, info,
|
||||||
matrix::{StateKey, pdu::PduBuilder},
|
matrix::{StateKey, pdu::PduBuilder},
|
||||||
};
|
};
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt};
|
||||||
@@ -68,37 +68,76 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
|
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// First, check if the user has permission to upgrade the room (send tombstone
|
||||||
|
// event)
|
||||||
|
let old_room_state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
||||||
|
|
||||||
|
// Check tombstone permission by attempting to create (but not send) the event
|
||||||
|
// Note that this does internally call the policy server with a fake room ID,
|
||||||
|
// which may not be good?
|
||||||
|
let tombstone_test_result = services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.create_hash_and_sign_event(
|
||||||
|
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
|
||||||
|
body: "This room has been replaced".to_owned(),
|
||||||
|
replacement_room: RoomId::new(services.globals.server_name()),
|
||||||
|
}),
|
||||||
|
sender_user,
|
||||||
|
Some(&body.room_id),
|
||||||
|
&old_room_state_lock,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Err(_e) = tombstone_test_result {
|
||||||
|
return Err!(Request(Forbidden("User does not have permission to upgrade this room.")));
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(old_room_state_lock);
|
||||||
|
|
||||||
// Create a replacement room
|
// Create a replacement room
|
||||||
let replacement_room = RoomId::new(services.globals.server_name());
|
let room_features = RoomVersion::new(&body.new_version)?;
|
||||||
|
let replacement_room: Option<&RoomId> = if room_features.room_ids_as_hashes {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(&RoomId::new(services.globals.server_name()))
|
||||||
|
};
|
||||||
|
let replacement_room_tmp = match replacement_room {
|
||||||
|
| Some(v) => v,
|
||||||
|
| None => &RoomId::new(services.globals.server_name()),
|
||||||
|
};
|
||||||
|
|
||||||
let _short_id = services
|
let _short_id = services
|
||||||
.rooms
|
.rooms
|
||||||
.short
|
.short
|
||||||
.get_or_create_shortroomid(&replacement_room)
|
.get_or_create_shortroomid(replacement_room_tmp)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
// For pre-v12 rooms, send tombstone before creating replacement room
|
||||||
|
let tombstone_event_id = if !room_features.room_ids_as_hashes {
|
||||||
// Send a m.room.tombstone event to the old room to indicate that it is not
|
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
||||||
// intended to be used any further Fail if the sender does not have the required
|
// Send a m.room.tombstone event to the old room to indicate that it is not
|
||||||
// permissions
|
// intended to be used any further
|
||||||
let tombstone_event_id = services
|
let tombstone_event_id = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.build_and_append_pdu(
|
.build_and_append_pdu(
|
||||||
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
|
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
|
||||||
body: "This room has been replaced".to_owned(),
|
body: "This room has been replaced".to_owned(),
|
||||||
replacement_room: replacement_room.clone(),
|
replacement_room: replacement_room.unwrap().to_owned(),
|
||||||
}),
|
}),
|
||||||
sender_user,
|
sender_user,
|
||||||
Some(&body.room_id),
|
Some(&body.room_id),
|
||||||
&state_lock,
|
&state_lock,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
// Change lock to replacement room
|
||||||
// Change lock to replacement room
|
drop(state_lock);
|
||||||
drop(state_lock);
|
Some(tombstone_event_id)
|
||||||
let state_lock = services.rooms.state.mutex.lock(&replacement_room).await;
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let state_lock = services.rooms.state.mutex.lock(replacement_room_tmp).await;
|
||||||
|
|
||||||
// Get the old room creation event
|
// Get the old room creation event
|
||||||
let mut create_event_content: CanonicalJsonObject = services
|
let mut create_event_content: CanonicalJsonObject = services
|
||||||
@@ -111,7 +150,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
// Use the m.room.tombstone event as the predecessor
|
// Use the m.room.tombstone event as the predecessor
|
||||||
let predecessor = Some(ruma::events::room::create::PreviousRoom::new(
|
let predecessor = Some(ruma::events::room::create::PreviousRoom::new(
|
||||||
body.room_id.clone(),
|
body.room_id.clone(),
|
||||||
Some(tombstone_event_id),
|
tombstone_event_id,
|
||||||
));
|
));
|
||||||
|
|
||||||
// Send a m.room.create event containing a predecessor field and the applicable
|
// Send a m.room.create event containing a predecessor field and the applicable
|
||||||
@@ -132,6 +171,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
// "creator" key no longer exists in V11 rooms
|
// "creator" key no longer exists in V11 rooms
|
||||||
create_event_content.remove("creator");
|
create_event_content.remove("creator");
|
||||||
},
|
},
|
||||||
|
// TODO(hydra): additional_creators
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +199,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
return Err(Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"));
|
return Err(Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"));
|
||||||
}
|
}
|
||||||
|
|
||||||
services
|
let create_event_id = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.build_and_append_pdu(
|
.build_and_append_pdu(
|
||||||
@@ -173,11 +213,18 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
timestamp: None,
|
timestamp: None,
|
||||||
},
|
},
|
||||||
sender_user,
|
sender_user,
|
||||||
Some(&replacement_room),
|
replacement_room,
|
||||||
&state_lock,
|
&state_lock,
|
||||||
)
|
)
|
||||||
.boxed()
|
.boxed()
|
||||||
.await?;
|
.await?;
|
||||||
|
let create_id = create_event_id.as_str().replace('$', "!");
|
||||||
|
let (replacement_room, state_lock) = if room_features.room_ids_as_hashes {
|
||||||
|
let parsed_room_id = RoomId::parse(&create_id)?;
|
||||||
|
(Some(parsed_room_id), services.rooms.state.mutex.lock(parsed_room_id).await)
|
||||||
|
} else {
|
||||||
|
(replacement_room, state_lock)
|
||||||
|
};
|
||||||
|
|
||||||
// Join the new room
|
// Join the new room
|
||||||
services
|
services
|
||||||
@@ -204,7 +251,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
timestamp: None,
|
timestamp: None,
|
||||||
},
|
},
|
||||||
sender_user,
|
sender_user,
|
||||||
Some(&replacement_room),
|
replacement_room,
|
||||||
&state_lock,
|
&state_lock,
|
||||||
)
|
)
|
||||||
.boxed()
|
.boxed()
|
||||||
@@ -243,7 +290,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
sender_user,
|
sender_user,
|
||||||
Some(&replacement_room),
|
replacement_room,
|
||||||
&state_lock,
|
&state_lock,
|
||||||
)
|
)
|
||||||
.boxed()
|
.boxed()
|
||||||
@@ -268,7 +315,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
services
|
services
|
||||||
.rooms
|
.rooms
|
||||||
.alias
|
.alias
|
||||||
.set_alias(alias, &replacement_room, sender_user)?;
|
.set_alias(alias, replacement_room.unwrap(), sender_user)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the old room power levels
|
// Get the old room power levels
|
||||||
@@ -310,6 +357,27 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
|
|
||||||
drop(state_lock);
|
drop(state_lock);
|
||||||
|
|
||||||
|
// For v12 rooms, send tombstone AFTER creating replacement room
|
||||||
|
if room_features.room_ids_as_hashes {
|
||||||
|
let old_room_state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
||||||
|
// For v12 rooms, no event reference in predecessor due to cyclic dependency -
|
||||||
|
// could best effort one maybe?
|
||||||
|
services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.build_and_append_pdu(
|
||||||
|
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
|
||||||
|
body: "This room has been replaced".to_owned(),
|
||||||
|
replacement_room: replacement_room.unwrap().to_owned(),
|
||||||
|
}),
|
||||||
|
sender_user,
|
||||||
|
Some(&body.room_id),
|
||||||
|
&old_room_state_lock,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
drop(old_room_state_lock);
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the old room has a space parent, and if so, whether we should update
|
// Check if the old room has a space parent, and if so, whether we should update
|
||||||
// it (m.space.parent, room_id)
|
// it (m.space.parent, room_id)
|
||||||
let parents = services
|
let parents = services
|
||||||
@@ -334,8 +402,9 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
debug!(
|
debug!(
|
||||||
"Updating space {space_id} child event for room {} to {replacement_room}",
|
"Updating space {space_id} child event for room {} to {}",
|
||||||
&body.room_id
|
&body.room_id,
|
||||||
|
replacement_room.unwrap()
|
||||||
);
|
);
|
||||||
// First, drop the space's child event
|
// First, drop the space's child event
|
||||||
let state_lock = services.rooms.state.mutex.lock(space_id).await;
|
let state_lock = services.rooms.state.mutex.lock(space_id).await;
|
||||||
@@ -359,7 +428,10 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
// Now, add a new child event for the replacement room
|
// Now, add a new child event for the replacement room
|
||||||
debug!("Adding space child event for room {replacement_room} in space {space_id}");
|
debug!(
|
||||||
|
"Adding space child event for room {} in space {space_id}",
|
||||||
|
replacement_room.unwrap()
|
||||||
|
);
|
||||||
services
|
services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
@@ -372,7 +444,7 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
suggested: child.suggested,
|
suggested: child.suggested,
|
||||||
})
|
})
|
||||||
.expect("event is valid, we just created it"),
|
.expect("event is valid, we just created it"),
|
||||||
state_key: Some(replacement_room.as_str().into()),
|
state_key: Some(replacement_room.unwrap().as_str().into()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
sender_user,
|
sender_user,
|
||||||
@@ -383,12 +455,15 @@ pub(crate) async fn upgrade_room_route(
|
|||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
debug!(
|
debug!(
|
||||||
"Finished updating space {space_id} child event for room {} to {replacement_room}",
|
"Finished updating space {space_id} child event for room {} to {}",
|
||||||
&body.room_id
|
&body.room_id,
|
||||||
|
replacement_room.unwrap()
|
||||||
);
|
);
|
||||||
drop(state_lock);
|
drop(state_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the replacement room id
|
// Return the replacement room id
|
||||||
Ok(upgrade_room::v3::Response { replacement_room })
|
Ok(upgrade_room::v3::Response {
|
||||||
|
replacement_room: replacement_room.unwrap().to_owned(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ pub use self::{
|
|||||||
room_version::RoomVersion,
|
room_version::RoomVersion,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
debug, debug_error,
|
debug, debug_error, err,
|
||||||
matrix::{Event, StateKey},
|
matrix::{Event, StateKey},
|
||||||
state_res::room_version::StateResolutionVersion,
|
state_res::room_version::StateResolutionVersion,
|
||||||
trace,
|
trace,
|
||||||
@@ -319,8 +319,19 @@ where
|
|||||||
path.pop();
|
path.pop();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let evt = fetch_event(event_id.clone()).await?;
|
trace!(event_id = event_id.as_str(), "fetching event for its auth events");
|
||||||
stack.push(evt.auth_events().map(ToOwned::to_owned).collect());
|
let evt = fetch_event(event_id.clone()).await;
|
||||||
|
if evt.is_none() {
|
||||||
|
err!("could not fetch event {} to calculate conflicted subgraph", event_id);
|
||||||
|
path.pop();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
stack.push(
|
||||||
|
evt.expect("checked")
|
||||||
|
.auth_events()
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
seen.insert(event_id);
|
seen.insert(event_id);
|
||||||
}
|
}
|
||||||
Some(subgraph)
|
Some(subgraph)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use conduwuit::{
|
|||||||
},
|
},
|
||||||
warn,
|
warn,
|
||||||
};
|
};
|
||||||
|
use database::Json;
|
||||||
use futures::{FutureExt, StreamExt, TryStreamExt};
|
use futures::{FutureExt, StreamExt, TryStreamExt};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
@@ -606,7 +607,7 @@ async fn fix_corrupt_msc4133_fields(services: &Services) -> Result {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
useridprofilekey_value.put((user, key), new_value);
|
useridprofilekey_value.put((user, key), Json(new_value));
|
||||||
fixed = fixed.saturating_add(1);
|
fixed = fixed.saturating_add(1);
|
||||||
}
|
}
|
||||||
total = total.saturating_add(1);
|
total = total.saturating_add(1);
|
||||||
|
|||||||
@@ -4,9 +4,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use conduwuit::{
|
use conduwuit::{
|
||||||
Event, PduEvent, debug, debug_error, debug_warn, implement,
|
Event, PduEvent, debug, debug_warn, implement, matrix::event::gen_event_id_canonical_json,
|
||||||
matrix::event::gen_event_id_canonical_json, trace, utils::continue_exponential_backoff_secs,
|
trace, utils::continue_exponential_backoff_secs, warn,
|
||||||
warn,
|
|
||||||
};
|
};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
CanonicalJsonValue, EventId, OwnedEventId, RoomId, ServerName,
|
CanonicalJsonValue, EventId, OwnedEventId, RoomId, ServerName,
|
||||||
@@ -52,12 +51,14 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut events_with_auth_events = Vec::with_capacity(events.clone().count());
|
let mut events_with_auth_events = Vec::with_capacity(events.clone().count());
|
||||||
|
trace!("Fetching {} outlier pdus", events.clone().count());
|
||||||
|
|
||||||
for id in events {
|
for id in events {
|
||||||
// a. Look in the main timeline (pduid_pdu tree)
|
// a. Look in the main timeline (pduid_pdu tree)
|
||||||
// b. Look at outlier pdu tree
|
// b. Look at outlier pdu tree
|
||||||
// (get_pdu_json checks both)
|
// (get_pdu_json checks both)
|
||||||
if let Ok(local_pdu) = self.services.timeline.get_pdu(id).await {
|
if let Ok(local_pdu) = self.services.timeline.get_pdu(id).await {
|
||||||
|
trace!("Found {id} in main timeline or outlier tree");
|
||||||
events_with_auth_events.push((id.to_owned(), Some(local_pdu), vec![]));
|
events_with_auth_events.push((id.to_owned(), Some(local_pdu), vec![]));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -104,7 +105,7 @@ where
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Fetching {next_id} over federation.");
|
debug!("Fetching {next_id} over federation from {origin}.");
|
||||||
match self
|
match self
|
||||||
.services
|
.services
|
||||||
.sending
|
.sending
|
||||||
@@ -115,7 +116,7 @@ where
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
| Ok(res) => {
|
| Ok(res) => {
|
||||||
debug!("Got {next_id} over federation");
|
debug!("Got {next_id} over federation from {origin}");
|
||||||
let Ok(room_version_id) = get_room_version_id(create_event) else {
|
let Ok(room_version_id) = get_room_version_id(create_event) else {
|
||||||
back_off((*next_id).to_owned());
|
back_off((*next_id).to_owned());
|
||||||
continue;
|
continue;
|
||||||
@@ -145,6 +146,9 @@ where
|
|||||||
auth_event.clone().into(),
|
auth_event.clone().into(),
|
||||||
) {
|
) {
|
||||||
| Ok(auth_event) => {
|
| Ok(auth_event) => {
|
||||||
|
trace!(
|
||||||
|
"Found auth event id {auth_event} for event {next_id}"
|
||||||
|
);
|
||||||
todo_auth_events.push_back(auth_event);
|
todo_auth_events.push_back(auth_event);
|
||||||
},
|
},
|
||||||
| _ => {
|
| _ => {
|
||||||
@@ -160,7 +164,7 @@ where
|
|||||||
events_all.insert(next_id);
|
events_all.insert(next_id);
|
||||||
},
|
},
|
||||||
| Err(e) => {
|
| Err(e) => {
|
||||||
debug_error!("Failed to fetch event {next_id}: {e}");
|
warn!("Failed to fetch auth event {next_id} from {origin}: {e}");
|
||||||
back_off((*next_id).to_owned());
|
back_off((*next_id).to_owned());
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -175,7 +179,7 @@ where
|
|||||||
// b. Look at outlier pdu tree
|
// b. Look at outlier pdu tree
|
||||||
// (get_pdu_json checks both)
|
// (get_pdu_json checks both)
|
||||||
if let Some(local_pdu) = local_pdu {
|
if let Some(local_pdu) = local_pdu {
|
||||||
trace!("Found {id} in db");
|
trace!("Found {id} in main timeline or outlier tree");
|
||||||
pdus.push((local_pdu.clone(), None));
|
pdus.push((local_pdu.clone(), None));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,6 +205,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trace!("Handling outlier {next_id}");
|
||||||
match Box::pin(self.handle_outlier_pdu(
|
match Box::pin(self.handle_outlier_pdu(
|
||||||
origin,
|
origin,
|
||||||
create_event,
|
create_event,
|
||||||
@@ -213,6 +218,7 @@ where
|
|||||||
{
|
{
|
||||||
| Ok((pdu, json)) =>
|
| Ok((pdu, json)) =>
|
||||||
if next_id == *id {
|
if next_id == *id {
|
||||||
|
trace!("Handled outlier {next_id} (original request)");
|
||||||
pdus.push((pdu, Some(json)));
|
pdus.push((pdu, Some(json)));
|
||||||
},
|
},
|
||||||
| Err(e) => {
|
| Err(e) => {
|
||||||
@@ -222,6 +228,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
trace!("Fetched and handled {} outlier pdus", pdus.len());
|
||||||
pdus
|
pdus
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
use std::collections::{BTreeMap, HashMap, hash_map};
|
use std::collections::{BTreeMap, HashMap, hash_map};
|
||||||
|
|
||||||
use conduwuit::{
|
use conduwuit::{
|
||||||
Err, Event, PduEvent, Result, debug, debug_info, err, implement, state_res, trace, warn,
|
Err, Event, PduEvent, Result, debug, debug_info, debug_warn, err, implement, state_res, trace,
|
||||||
};
|
};
|
||||||
use futures::future::ready;
|
use futures::future::ready;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId, ServerName, events::StateEventType,
|
CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, RoomId, ServerName,
|
||||||
|
events::StateEventType,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{check_room_id, get_room_version_id, to_room_version};
|
use super::{check_room_id, get_room_version_id, to_room_version};
|
||||||
@@ -74,36 +75,73 @@ where
|
|||||||
|
|
||||||
check_room_id(room_id, &pdu_event)?;
|
check_room_id(room_id, &pdu_event)?;
|
||||||
|
|
||||||
if !auth_events_known {
|
// Fetch all auth events
|
||||||
// 4. fetch any missing auth events doing all checks listed here starting at 1.
|
let mut auth_events: HashMap<OwnedEventId, PduEvent> = HashMap::new();
|
||||||
// These are not timeline events
|
|
||||||
// 5. Reject "due to auth events" if can't get all the auth events or some of
|
for aid in pdu_event.auth_events() {
|
||||||
// the auth events are also rejected "due to auth events"
|
if let Ok(auth_event) = self.services.timeline.get_pdu(aid).await {
|
||||||
// NOTE: Step 5 is not applied anymore because it failed too often
|
check_room_id(room_id, &auth_event)?;
|
||||||
debug!("Fetching auth events");
|
trace!("Found auth event {aid} for outlier event {event_id} locally");
|
||||||
Box::pin(self.fetch_and_handle_outliers(
|
auth_events.insert(aid.to_owned(), auth_event);
|
||||||
origin,
|
} else {
|
||||||
pdu_event.auth_events(),
|
debug_warn!("Could not find auth event {aid} for outlier event {event_id} locally");
|
||||||
create_event,
|
}
|
||||||
room_id,
|
}
|
||||||
))
|
|
||||||
.await;
|
// Fetch any missing ones & reject invalid ones
|
||||||
|
let missing_auth_events = if auth_events_known {
|
||||||
|
pdu_event
|
||||||
|
.auth_events()
|
||||||
|
.filter(|id| !auth_events.contains_key(*id))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
} else {
|
||||||
|
pdu_event.auth_events().collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
if !missing_auth_events.is_empty() || !auth_events_known {
|
||||||
|
debug_info!(
|
||||||
|
"Fetching {} missing auth events for outlier event {event_id}",
|
||||||
|
missing_auth_events.len()
|
||||||
|
);
|
||||||
|
for (pdu, _) in self
|
||||||
|
.fetch_and_handle_outliers(
|
||||||
|
origin,
|
||||||
|
missing_auth_events.iter().copied(),
|
||||||
|
create_event,
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
auth_events.insert(pdu.event_id().to_owned(), pdu);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debug!("No missing auth events for outlier event {event_id}");
|
||||||
|
}
|
||||||
|
// reject if we are still missing some
|
||||||
|
let still_missing = pdu_event
|
||||||
|
.auth_events()
|
||||||
|
.filter(|id| !auth_events.contains_key(*id))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
if !still_missing.is_empty() {
|
||||||
|
return Err!(Request(InvalidParam(
|
||||||
|
"Could not fetch all auth events for outlier event {event_id}, still missing: \
|
||||||
|
{still_missing:?}"
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. Reject "due to auth events" if the event doesn't pass auth based on the
|
// 6. Reject "due to auth events" if the event doesn't pass auth based on the
|
||||||
// auth events
|
// auth events
|
||||||
debug!("Checking based on auth events");
|
debug!("Checking based on auth events");
|
||||||
|
let mut auth_events_by_key: HashMap<_, _> = HashMap::with_capacity(auth_events.len());
|
||||||
// Build map of auth events
|
// Build map of auth events
|
||||||
let mut auth_events = HashMap::with_capacity(pdu_event.auth_events().count());
|
|
||||||
for id in pdu_event.auth_events() {
|
for id in pdu_event.auth_events() {
|
||||||
let Ok(auth_event) = self.services.timeline.get_pdu(id).await else {
|
let auth_event = auth_events
|
||||||
warn!("Could not find auth event {id}");
|
.get(id)
|
||||||
continue;
|
.expect("we just checked that we have all auth events")
|
||||||
};
|
.to_owned();
|
||||||
|
|
||||||
check_room_id(room_id, &auth_event)?;
|
check_room_id(room_id, &auth_event)?;
|
||||||
|
|
||||||
match auth_events.entry((
|
match auth_events_by_key.entry((
|
||||||
auth_event.kind.to_string().into(),
|
auth_event.kind.to_string().into(),
|
||||||
auth_event
|
auth_event
|
||||||
.state_key
|
.state_key
|
||||||
@@ -123,7 +161,7 @@ where
|
|||||||
|
|
||||||
// The original create event must be in the auth events
|
// The original create event must be in the auth events
|
||||||
if !matches!(
|
if !matches!(
|
||||||
auth_events.get(&(StateEventType::RoomCreate, String::new().into())),
|
auth_events_by_key.get(&(StateEventType::RoomCreate, String::new().into())),
|
||||||
Some(_) | None
|
Some(_) | None
|
||||||
) {
|
) {
|
||||||
return Err!(Request(InvalidParam("Incoming event refers to wrong create event.")));
|
return Err!(Request(InvalidParam("Incoming event refers to wrong create event.")));
|
||||||
@@ -131,7 +169,7 @@ where
|
|||||||
|
|
||||||
let state_fetch = |ty: &StateEventType, sk: &str| {
|
let state_fetch = |ty: &StateEventType, sk: &str| {
|
||||||
let key = (ty.to_owned(), sk.into());
|
let key = (ty.to_owned(), sk.into());
|
||||||
ready(auth_events.get(&key).map(ToOwned::to_owned))
|
ready(auth_events_by_key.get(&key).map(ToOwned::to_owned))
|
||||||
};
|
};
|
||||||
|
|
||||||
let auth_check = state_res::event_auth::auth_check(
|
let auth_check = state_res::event_auth::auth_check(
|
||||||
|
|||||||
@@ -274,8 +274,6 @@ pub async fn create_hash_and_sign_event(
|
|||||||
pdu_json.insert("event_id".into(), CanonicalJsonValue::String(pdu.event_id.clone().into()));
|
pdu_json.insert("event_id".into(), CanonicalJsonValue::String(pdu.event_id.clone().into()));
|
||||||
|
|
||||||
// Check with the policy server
|
// Check with the policy server
|
||||||
// TODO(hydra): Skip this check for create events (why didnt we do this
|
|
||||||
// already?)
|
|
||||||
if room_id.is_some() {
|
if room_id.is_some() {
|
||||||
trace!(
|
trace!(
|
||||||
"Checking event {} in room {} with policy server",
|
"Checking event {} in room {} with policy server",
|
||||||
|
|||||||
Reference in new issue
Block a user