Compare commits

..
Author SHA1 Message Date
Nyaaori 04fcac20b3 Cleanup rooms.rs, globals.rs, and pdu.rs 2021-11-24 03:58:56 +00:00
Nyaaori 25065a58e8 Support room version 3 2021-11-24 03:58:55 +00:00
Nyaaori 526b5b4d7f Enable room version 4 2021-11-24 03:58:55 +00:00
Nyaaori 8eb5843f61 Refactor room version support, add default room version config 2021-11-24 03:58:52 +00:00
15 changed files with 206 additions and 137 deletions
+56 -31
View File
@@ -142,12 +142,8 @@ build:debug:cargo:x86_64-unknown-linux-musl:
DOCKER_HOST: tcp://docker:2375/
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2
PLATFORMS: "linux/arm/v6,linux/arm/v7,linux/arm64,linux/amd64"
PLATFORMS: "linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64"
DOCKER_FILE: "docker/ci-binaries-packaging.Dockerfile"
cache:
paths:
- docker_cache
key: "$CI_JOB_NAME"
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Only log in to Dockerhub if the credentials are given:
@@ -160,51 +156,80 @@ build:debug:cargo:x86_64-unknown-linux-musl:
- mkdir -p linux/ && mv ./conduit-x86_64-unknown-linux-musl linux/amd64
- mkdir -p linux/arm/ && mv ./conduit-arm-unknown-linux-musleabihf linux/arm/v6
- mkdir -p linux/arm/ && mv ./conduit-armv7-unknown-linux-musleabihf linux/arm/v7
- mv ./conduit-aarch64-unknown-linux-musl linux/arm64
- 'export CREATED=$(date -u +''%Y-%m-%dT%H:%M:%SZ'') && echo "Docker image creation date: $CREATED"'
# Build and push image:
- mkdir -p linux/arm64/ && mv ./conduit-aarch64-unknown-linux-musl linux/arm64/v8
# Actually create multiarch image:
- >
docker buildx build
--pull
--push
--cache-from=type=local,src=$CI_PROJECT_DIR/docker_cache
--cache-to=type=local,dest=$CI_PROJECT_DIR/docker_cache
--build-arg CREATED=$CREATED
--build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
--build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
--build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
--platform "$PLATFORMS"
--tag "$TAG"
--tag "$TAG-alpine"
--tag "$TAG-commit-$CI_COMMIT_SHORT_SHA"
--tag "$GL_IMAGE_TAG"
--tag "$GL_IMAGE_TAG-commit-$CI_COMMIT_SHORT_SHA"
--file "$DOCKER_FILE" .
# Only try to push to docker hub, if auth data for dockerhub exists:
- if [ -n "${DOCKER_HUB}" ]; then docker push "$DH_IMAGE_TAG"; fi
- if [ -n "${DOCKER_HUB}" ]; then docker push "$DH_IMAGE_TAG-commit-$CI_COMMIT_SHORT_SHA"; fi
docker:next:gitlab:
build:docker:next:
extends: .docker-shared-settings
rules:
- if: '$CI_COMMIT_BRANCH == "next"'
variables:
TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:next"
GL_IMAGE_TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:next"
DH_IMAGE_TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
docker:next:dockerhub:
extends: .docker-shared-settings
rules:
- if: '$CI_COMMIT_BRANCH == "next" && $DOCKER_HUB'
variables:
TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
docker:master:gitlab:
build:docker:master:
extends: .docker-shared-settings
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
variables:
TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:latest"
GL_IMAGE_TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:latest"
DH_IMAGE_TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest"
docker:master:dockerhub:
extends: .docker-shared-settings
rules:
- if: '$CI_COMMIT_BRANCH == "master" && $DOCKER_HUB'
variables:
TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest"
## Build a docker image by packaging up the x86_64-unknown-linux-musl binary into an alpine image
#.docker-shared-settings:
# stage: "build docker image"
# needs: []
# interruptible: true
# image:
# name: "gcr.io/kaniko-project/executor:debug"
# entrypoint: [""]
# tags: ["docker"]
# variables:
# # Configure Kaniko Caching: https://cloud.google.com/build/docs/kaniko-cache
# KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=120h --cache-repo $CI_REGISTRY_IMAGE/kaniko-ci-cache"
# before_script:
# - "mkdir -p /kaniko/.docker"
# - 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"},\"$DOCKER_HUB\":{\"username\":\"$DOCKER_HUB_USER\",\"password\":\"$DOCKER_HUB_PASSWORD\"}}}" > /kaniko/.docker/config.json'
#
#
#build:docker:next:
# extends: .docker-shared-settings
# needs:
# - "build:release:cargo:x86_64-unknown-linux-musl"
# script:
# - >
# /kaniko/executor
# $KANIKO_CACHE_ARGS
# --force
# --context $CI_PROJECT_DIR
# --build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
# --build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
# --build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
# --dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile"
# --destination "$CI_REGISTRY_IMAGE/conduit:next"
# --destination "$CI_REGISTRY_IMAGE/conduit:next-alpine"
# --destination "$CI_REGISTRY_IMAGE/conduit:commit-$CI_COMMIT_SHORT_SHA"
# --destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
# --destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next-alpine"
# --destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:commit-$CI_COMMIT_SHORT_SHA"
# rules:
# - if: '$CI_COMMIT_BRANCH == "next"'
#
#
# --------------------------------------------------------------------- #
# Run tests #
-3
View File
@@ -82,9 +82,6 @@ thread_local = "1.1.3"
# used for TURN server authentication
hmac = "0.11.0"
sha-1 = "0.9.8"
# used to embed Element Web into Conduit
rust-embed="6.3.0"
[features]
default = ["conduit_bin", "backend_sqlite"]
+13 -10
View File
@@ -12,17 +12,20 @@ only offer Linux binaries.
You may simply download the binary that fits your machine. Run `uname -m` to see what you need. Now copy the right url:
| CPU Architecture | Download stable version |
| ------------------------------------------- | ------------------------------ |
| x84_64 / amd64 (Most servers and computers) | [Download][x84_64-musl-master] |
| armv6 | [Download][armv6-musl-master] |
| armv7 (e.g. Raspberry Pi by default) | [Download][armv7-musl-master] |
| armv8 / aarch64 | [Download][armv8-musl-master] |
| CPU Architecture | Download link |
| ------------------------------------------- | ----------------------- |
| x84_64 / amd64 (Most servers and computers) | [Download][x84_64-musl] |
| armv6 | [Download][armv6-musl] |
| armv7 (e.g. Raspberry Pi by default) | [Download][armv7-musl] |
| armv8 / aarch64 | [Download][armv8-musl] |
[x84_64-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-x86_64-unknown-linux-musl?job=build:release:cargo:x86_64-unknown-linux-musl
[armv6-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-arm-unknown-linux-musleabihf?job=build:release:cargo:arm-unknown-linux-musleabihf
[armv7-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-armv7-unknown-linux-musleabihf?job=build:release:cargo:armv7-unknown-linux-musleabihf
[armv8-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-aarch64-unknown-linux-musl?job=build:release:cargo:aarch64-unknown-linux-musl
[x84_64-musl]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-x86_64-unknown-linux-musl?job=build:release:cargo:x86_64-unknown-linux-musl
[armv6-musl]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-arm-unknown-linux-musleabihf?job=build:release:cargo:arm-unknown-linux-musleabihf
[armv7-musl]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-armv7-unknown-linux-musleabihf?job=build:release:cargo:armv7-unknown-linux-musleabihf
[armv8-musl]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-aarch64-unknown-linux-musl?job=build:release:cargo:aarch64-unknown-linux-musl
```bash
$ sudo wget -O /usr/local/bin/matrix-conduit <url>
+2 -2
View File
@@ -54,11 +54,11 @@ RUN apk add --no-cache \
RUN mkdir -p /srv/conduit/.local/share/conduit
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
COPY ./docker/healthcheck.sh /srv/conduit/
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
# Copy over the actual Conduit binary from the builder stage
COPY --from=builder /usr/src/conduit/target/release/conduit /srv/conduit/conduit
COPY --from=builder /usr/src/conduit/target/release/conduit /srv/conduit/
# Improve security: Don't run stuff as root, that does not need to run as root:
# Add www-data user and group with UID 82, as used by alpine
+1 -1
View File
@@ -47,7 +47,7 @@ LABEL org.opencontainers.image.created=${CREATED} \
RUN mkdir -p /srv/conduit/.local/share/conduit
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
COPY ./docker/healthcheck.sh /srv/conduit/
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
+2 -2
View File
@@ -34,7 +34,7 @@ use ruma::{
EventType,
},
identifiers::RoomName,
push, RoomAliasId, RoomId, RoomVersionId, UserId,
push, RoomAliasId, RoomId, UserId,
};
use serde_json::value::to_raw_value;
use tracing::info;
@@ -282,7 +282,7 @@ pub async fn register_route(
let mut content = RoomCreateEventContent::new(conduit_user.clone());
content.federate = true;
content.predecessor = None;
content.room_version = RoomVersionId::Version6;
content.room_version = db.globals.default_room_version();
// 1. The room create event
db.rooms.build_and_append_pdu(
+18 -10
View File
@@ -1,9 +1,6 @@
use crate::{ConduitResult, Ruma};
use ruma::{
api::client::r0::capabilities::{
get_capabilities, Capabilities, RoomVersionStability, RoomVersionsCapability,
},
RoomVersionId,
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
use ruma::api::client::r0::capabilities::{
get_capabilities, Capabilities, RoomVersionStability, RoomVersionsCapability,
};
use std::collections::BTreeMap;
@@ -17,17 +14,28 @@ use rocket::get;
feature = "conduit_bin",
get("/_matrix/client/r0/capabilities", data = "<_body>")
)]
#[tracing::instrument(skip(_body))]
#[tracing::instrument(skip(_body, db))]
pub async fn get_capabilities_route(
_body: Ruma<get_capabilities::Request>,
db: DatabaseGuard,
) -> ConduitResult<get_capabilities::Response> {
let mut available = BTreeMap::new();
available.insert(RoomVersionId::Version5, RoomVersionStability::Stable);
available.insert(RoomVersionId::Version6, RoomVersionStability::Stable);
if db.globals.allow_unstable_room_versions() {
for room_version in &db.globals.unstable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Stable);
}
} else {
for room_version in &db.globals.unstable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Unstable);
}
}
for room_version in &db.globals.stable_room_versions {
available.insert(room_version.clone(), RoomVersionStability::Stable);
}
let mut capabilities = Capabilities::new();
capabilities.room_versions = RoomVersionsCapability {
default: RoomVersionId::Version6,
default: db.globals.default_room_version(),
available,
};
+7 -10
View File
@@ -551,7 +551,7 @@ async fn join_room_by_id_helper(
federation::membership::create_join_event_template::v1::Request {
room_id,
user_id: sender_user,
ver: &[RoomVersionId::Version5, RoomVersionId::Version6],
ver: &db.globals.supported_room_versions(),
},
)
.await;
@@ -566,12 +566,7 @@ async fn join_room_by_id_helper(
let (make_join_response, remote_server) = make_join_response_and_server?;
let room_version = match make_join_response.room_version {
Some(room_version)
if room_version == RoomVersionId::Version5
|| room_version == RoomVersionId::Version6 =>
{
room_version
}
Some(room_version) if db.rooms.is_supported_version(&db, &room_version) => room_version,
_ => return Err(Error::BadServerResponse("Room version is not supported")),
};
@@ -890,9 +885,10 @@ pub(crate) async fn invite_helper<'a>(
None
};
// If there was no create event yet, assume we are creating a version 6 room right now
// If there was no create event yet, assume we are creating a room with the default
// version right now
let room_version_id = create_event_content
.map_or(RoomVersionId::Version6, |create_event| {
.map_or(db.globals.default_room_version(), |create_event| {
create_event.room_version
});
let room_version =
@@ -1039,7 +1035,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
+4 -7
View File
@@ -23,7 +23,7 @@ use ruma::{
EventType,
},
serde::{CanonicalJsonObject, JsonObject},
RoomAliasId, RoomId, RoomVersionId,
RoomAliasId, RoomId,
};
use serde_json::{json, value::to_raw_value};
use std::{
@@ -109,7 +109,7 @@ pub async fn create_room_route(
let room_version = match body.room_version.clone() {
Some(room_version) => {
if room_version == RoomVersionId::Version5 || room_version == RoomVersionId::Version6 {
if db.rooms.is_supported_version(&db, &room_version) {
room_version
} else {
return Err(Error::BadRequest(
@@ -118,7 +118,7 @@ pub async fn create_room_route(
));
}
}
None => RoomVersionId::Version6,
None => db.globals.default_room_version(),
};
let content = match &body.creation_content {
@@ -505,10 +505,7 @@ pub async fn upgrade_room_route(
) -> ConduitResult<upgrade_room::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !matches!(
body.new_version,
RoomVersionId::Version5 | RoomVersionId::Version6
) {
if !db.rooms.is_supported_version(&db, &body.new_version) {
return Err(Error::BadRequest(
ErrorKind::UnsupportedRoomVersion,
"This server does not support that room version.",
+9 -1
View File
@@ -24,7 +24,7 @@ use rocket::{
request::{FromRequest, Request},
Shutdown, State,
};
use ruma::{DeviceId, EventId, RoomId, ServerName, UserId};
use ruma::{DeviceId, EventId, RoomId, RoomVersionId, ServerName, UserId};
use serde::{de::IgnoredAny, Deserialize};
use std::{
collections::{BTreeMap, HashMap, HashSet},
@@ -63,8 +63,12 @@ pub struct Config {
allow_federation: bool,
#[serde(default = "true_fn")]
allow_room_creation: bool,
#[serde(default = "true_fn")]
allow_unstable_room_versions: bool,
#[serde(default = "false_fn")]
pub allow_jaeger: bool,
#[serde(default = "default_room_version")]
default_room_version: RoomVersionId,
#[serde(default = "false_fn")]
pub tracing_flame: bool,
#[serde(default)]
@@ -137,6 +141,10 @@ fn default_max_concurrent_requests() -> u16 {
100
}
fn default_room_version() -> RoomVersionId {
RoomVersionId::Version6
}
fn default_log() -> String {
"info,state_res=warn,rocket=off,_=off,sled=off".to_owned()
}
+41 -2
View File
@@ -4,7 +4,8 @@ use ruma::{
client::r0::sync::sync_events,
federation::discovery::{ServerSigningKeys, VerifyKey},
},
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, UserId,
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, UserId,
};
use std::{
collections::{BTreeMap, HashMap},
@@ -39,6 +40,8 @@ pub struct Globals {
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
dns_resolver: TokioAsyncResolver,
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
pub stable_room_versions: Vec<RoomVersionId>,
pub unstable_room_versions: Vec<RoomVersionId>,
pub(super) server_signingkeys: Arc<dyn Tree>,
pub bad_event_ratelimiter: Arc<RwLock<HashMap<EventId, RateLimitState>>>,
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
@@ -132,7 +135,16 @@ impl Globals {
.as_ref()
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
let s = Self {
// Supported and stable room versions
let stable_room_versions = vec![RoomVersionId::Version6];
// Experimental, partially supported room versions
let unstable_room_versions = vec![
RoomVersionId::Version3,
RoomVersionId::Version4,
RoomVersionId::Version5,
];
let mut s = Self {
globals,
config,
keypair: Arc::new(keypair),
@@ -143,6 +155,8 @@ impl Globals {
tls_name_override,
server_signingkeys,
jwt_decoding_key,
stable_room_versions,
unstable_room_versions,
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
bad_signature_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
servername_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
@@ -155,6 +169,14 @@ impl Globals {
fs::create_dir_all(s.get_media_folder())?;
if !s
.supported_room_versions()
.contains(&s.config.default_room_version)
{
error!("Room version in config isn't supported, falling back to Version 6");
s.config.default_room_version = RoomVersionId::Version6;
};
Ok(s)
}
@@ -214,6 +236,14 @@ impl Globals {
self.config.allow_room_creation
}
pub fn allow_unstable_room_versions(&self) -> bool {
self.config.allow_unstable_room_versions
}
pub fn default_room_version(&self) -> RoomVersionId {
self.config.default_room_version.clone()
}
pub fn trusted_servers(&self) -> &[Box<ServerName>] {
&self.config.trusted_servers
}
@@ -246,6 +276,15 @@ impl Globals {
&self.config.turn_secret
}
pub fn supported_room_versions(&self) -> Vec<RoomVersionId> {
let mut room_versions: Vec<RoomVersionId> = vec![];
room_versions.extend(self.stable_room_versions.clone());
if self.allow_unstable_room_versions() {
room_versions.extend(self.unstable_room_versions.clone());
};
room_versions
}
/// TODO: the key valid until timestamp is only honored in room version > 4
/// Remove the outdated keys and insert the new ones.
///
+30 -7
View File
@@ -129,6 +129,12 @@ pub struct Rooms {
}
impl Rooms {
/// Returns true if a given room version is supported
#[tracing::instrument(skip(self, db))]
pub fn is_supported_version(&self, db: &Database, room_version: &RoomVersionId) -> bool {
db.globals.supported_room_versions().contains(room_version)
}
/// Builds a StateMap by iterating over all keys that start
/// with state_hash, this gives the full state for the given state_hash.
#[tracing::instrument(skip(self))]
@@ -1983,9 +1989,10 @@ impl Rooms {
None
};
// If there was no create event yet, assume we are creating a version 6 room right now
// If there was no create event yet, assume we are creating a room with the default
// version right now
let room_version_id = create_event_content
.map_or(RoomVersionId::Version6, |create_event| {
.map_or(db.globals.default_room_version(), |create_event| {
create_event.room_version
});
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");
@@ -2778,11 +2785,7 @@ impl Rooms {
let (make_leave_response, remote_server) = make_leave_response_and_server?;
let room_version_id = match make_leave_response.room_version {
Some(version)
if version == RoomVersionId::Version5 || version == RoomVersionId::Version6 =>
{
version
}
Some(version) if self.is_supported_version(&db, &version) => version,
_ => return Err(Error::BadServerResponse("Room version is not supported")),
};
@@ -3448,4 +3451,24 @@ impl Rooms {
Ok(())
}
/// Returns the room's version.
#[tracing::instrument(skip(self))]
pub fn get_room_version(&self, room_id: &RoomId) -> Result<RoomVersionId> {
let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?;
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()?;
let room_version = create_event_content
.map(|create_event| create_event.room_version)
.ok_or_else(|| Error::BadDatabase("Invalid room version"))?;
Ok(room_version)
}
}
+1 -38
View File
@@ -17,15 +17,11 @@ mod pdu;
mod ruma_wrapper;
mod utils;
use std::ffi::OsStr;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
use database::Config;
pub use database::Database;
pub use error::{Error, Result};
use http::StatusCode;
use opentelemetry::trace::{FutureExt, Tracer};
pub use pdu::PduEvent;
pub use rocket::State;
@@ -38,12 +34,8 @@ use rocket::{
providers::{Env, Format, Toml},
Figment,
},
get,
http::{ContentType, Status},
response, routes, Request,
routes, Request,
};
use rust_embed::RustEmbed;
use tokio::sync::RwLock;
use tracing_subscriber::{prelude::*, EnvFilter};
@@ -184,7 +176,6 @@ fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<
server_server::claim_keys_route,
],
)
.mount("/", routes![dist])
.register(
"/",
catchers![
@@ -285,34 +276,6 @@ async fn main() {
}
}
#[derive(RustEmbed)]
#[folder = "element_web"]
struct ElementWebAsset;
#[get("/<file..>")]
fn dist<'r>(file: PathBuf) -> response::Result<'r> {
let filename = file.display().to_string();
ElementWebAsset::get(&filename).map_or_else(
|| Err(Status::NotFound),
|embedded_file| {
let ext = file
.as_path()
.extension()
.and_then(OsStr::to_str)
.ok_or_else(|| Status::new(400))?;
let content_type = ContentType::from_extension(ext).ok_or_else(|| Status::new(400))?;
let response: Result<response::Response<'r>, Status> = response::Response::build()
.header(content_type)
.sized_body(embedded_file.data.len(), Cursor::new(embedded_file.data))
.ok();
return response;
},
)
}
#[catch(404)]
fn not_found_catcher(_: &Request<'_>) -> String {
"404 Not Found".to_owned()
+12 -4
View File
@@ -1,4 +1,4 @@
use crate::Error;
use crate::{Database, Error};
use ruma::{
events::{
room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyInitialStateEvent,
@@ -6,7 +6,7 @@ use ruma::{
EventType, 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::{
@@ -331,16 +331,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<(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::try_from(id.as_str()?).ok())
.expect("Invalid room id in event");
let room_version_id = db.rooms.get_room_version(&room_id);
let event_id = EventId::try_from(&*format!(
"${}",
// Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, &RoomVersionId::Version6)
ruma::signatures::reference_hash(&value, &room_version_id?)
.expect("ruma can calculate reference hashes")
))
.expect("ruma's reference hashes are valid event ids");
+10 -9
View File
@@ -725,7 +725,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
@@ -1911,7 +1911,7 @@ pub(crate) fn fetch_and_handle_outliers<'a>(
Ok(res) => {
warn!("Got {} over federation", 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((**id).clone());
@@ -2670,10 +2670,12 @@ pub fn create_join_event_template_route(
None
};
// If there was no create event yet, assume we are creating a version 6 room right now
let room_version_id = create_event_content.map_or(RoomVersionId::Version6, |create_event| {
create_event.room_version
});
// If there was no create event yet, assume we are creating a room with the default version
// right now
let room_version_id = create_event_content
.map_or(db.globals.default_room_version(), |create_event| {
create_event.room_version
});
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");
if !body.ver.contains(&room_version_id) {
@@ -2813,7 +2815,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
@@ -2937,8 +2939,7 @@ pub async fn create_invite_route(
return Err(Error::bad_config("Federation is disabled."));
}
if body.room_version != RoomVersionId::Version5 && body.room_version != RoomVersionId::Version6
{
if !db.rooms.is_supported_version(&db, &body.room_version) {
return Err(Error::BadRequest(
ErrorKind::IncompatibleRoomVersion {
room_version: body.room_version.clone(),