Compare commits

..
Author SHA1 Message Date
Jonas Zohren c8882a6b1a WIP: First attempt at propper docker multiarch 2021-11-04 00:33:51 +01:00
Timo Kösters 5ae753b2e7 Merge branch 'amorgan-next-patch-81816' into 'next'
Add 'Federation publicRoom Name/topic keys are correct' test to sytest whitelist

See merge request famedly/conduit!221
2021-10-27 11:49:55 +00:00
Andrew Morgan 743bdbe961 Add 'Federation publicRoom Name/topic keys are correct' test to sytest whitelist 2021-10-26 13:30:02 +00:00
Timo Kösters 6f70beb78c Merge branch 'nyaaori/fix-bad-json' into 'next'
Nyaaori/fix bad json

Closes #115

See merge request famedly/conduit!216
2021-10-16 06:59:34 +00:00
Timo Kösters 259fc580ca Merge branch 'nyaaori/otk-sync-fix' into 'next'
Remove device_one_time_keys_count from is_empty() sync checks, fixing sync issue as reported by Nekron

Closes #115

See merge request famedly/conduit!215
2021-10-16 06:54:14 +00:00
Nyaaori 55d78b1914 Bump Ruma version to fix M_BAD_JSON on login 2021-10-16 00:45:51 +00:00
Nyaaori 484a044b50 Remove device_one_time_keys_count from is_empty() sync checks, fixing sync issue as reported by Nekron 2021-10-15 23:17:08 +00:00
Nyaaori 2264a5f945 Merge branch 'next' of https://gitlab.com/famedly/conduit into next 2021-10-15 23:14:07 +00:00
Timo Kösters e88929e154 Merge branch 'nyaaori/fix-otk-reporting' into 'next'
Always send device_one_time_keys_count, fixing #178

Closes #169 and #178

See merge request famedly/conduit!213
2021-10-15 13:01:52 +00:00
Nyaaori d996d1b0e6 Always send device_one_time_keys_count, fixing #178 2021-10-15 12:38:20 +00:00
Timo Kösters dc8bc4a880 Merge branch 'up-ruma' into 'next'
Upgrade Ruma

See merge request famedly/conduit!210
2021-10-15 11:38:12 +00:00
Timo Kösters 87694f4369 Merge branch 'moa31-next-patch-76037' into 'next'
Change listen address when running inside a Container to prevent Bad Gateway error

See merge request famedly/conduit!207
2021-10-15 10:51:19 +00:00
Timo Kösters 335027e739 Merge branch 'nyaaori/admin-check' into 'next'
Implement admin check and add config option for allowing room creation

See merge request famedly/conduit!211
2021-10-15 10:47:55 +00:00
Timo Kösters 0ccda5f1c7 Merge branch 'nyaaori/allow_encryption' into 'next'
Make allow_encryption work again, fixing #115

Closes #115

See merge request famedly/conduit!212
2021-10-15 10:20:05 +00:00
Nyaaori 9082a531c9 Make allow_encryption work again, fixing #115 2021-10-15 09:57:49 +00:00
Jonas Platte 91afa1e0e0 Make Result alias usable with any error type 2021-10-13 12:25:58 +02:00
Jonas Platte f2ef5677e0 Reduce turbofish usage
Should make the code a little bit easier to read.
2021-10-13 12:25:58 +02:00
Jonas Platte 1c4d9af586 Enable more lints and apply their suggestions 2021-10-13 10:24:39 +02:00
Jonas PlatteandTimo Kösters 09895a20c8 Upgrade Ruma
Co-authored-by: Timo Kösters <timo@koesters.xyz>
2021-10-13 10:16:45 +02:00
Nyaaori 636db8cfaa Make allow_encryption work again, fixing #115 2021-09-24 22:44:26 +00:00
Nyaaori 6bc8fb2ae7 Implement admin check and add config option for allowing room creation 2021-09-24 07:16:34 +00:00
Luc-pascal Ceccaldi 5b23d3d06e Change listen address when running inside a Container to prevent Bad Gateway error 2021-09-23 07:49:52 +00:00
44 changed files with 1265 additions and 1529 deletions
+78 -31
View File
@@ -20,9 +20,11 @@ variables:
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "master"'
- if: '$CI_COMMIT_BRANCH == "next"' - if: '$CI_COMMIT_BRANCH == "next"'
# TODO: Remove this after debugging
- if: '$CI_COMMIT_BRANCH == "multiarch-docker-support"'
- if: '$CI_COMMIT_TAG' - if: '$CI_COMMIT_TAG'
interruptible: true interruptible: true
image: "rust:latest" image: "rust:1.56.0"
tags: ["docker"] tags: ["docker"]
cache: cache:
paths: paths:
@@ -30,16 +32,16 @@ variables:
- target/ - target/
key: "build_cache-$TARGET-release" key: "build_cache-$TARGET-release"
variables: variables:
CARGO_PROFILE_RELEASE_LTO=true CARGO_PROFILE_RELEASE_LTO: "off" # TODO: change to true again
CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "256" # TODO: Change to 1 again
RUSTUP_HOME: "/root/.rustup"
before_script: before_script:
- echo $USER
- export PATH="root/.cargo/bin:$PATH"
- 'echo "Building for target $TARGET"' - 'echo "Building for target $TARGET"'
- 'mkdir -p cargohome && CARGOHOME="cargohome"' - 'mkdir -p cargohome && CARGOHOME="cargohome"'
- "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging - "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging
- 'apt-get update -yqq' - 'time rustup target add "$TARGET"'
- 'echo "Installing packages: $NEEDED_PACKAGES"'
- "apt-get install -yqq --no-install-recommends $NEEDED_PACKAGES"
- "rustup target add $TARGET"
script: script:
- time cargo build --target $TARGET --release - time cargo build --target $TARGET --release
- 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"' - 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"'
@@ -57,52 +59,64 @@ build:release:cargo:x86_64-unknown-linux-gnu:
- "conduit-x86_64-unknown-linux-gnu" - "conduit-x86_64-unknown-linux-gnu"
expose_as: "Conduit for x86_64-unknown-linux-gnu" expose_as: "Conduit for x86_64-unknown-linux-gnu"
build:release:cargo:armv7-unknown-linux-gnueabihf: build:release:cargo:x86_64-unknown-linux-musl:
image: registry.gitlab.fachschaften.org/jfowl/cargo-cross-for-ci/cargo-cross-builder:rust1.56.0-x86_64-unknown-linux-musl
extends: .build-cargo-shared-settings extends: .build-cargo-shared-settings
variables:
TARGET: "x86_64-unknown-linux-musl"
artifacts:
name: "conduit-x86_64-unknown-linux-musl"
paths:
- "conduit-x86_64-unknown-linux-musl"
expose_as: "Conduit for x86_64-unknown-linux-musl"
build:release:cargo:armv7-unknown-linux-gnueabihf:
extends: .build-cargo-shared-settings
image:
name: registry.gitlab.fachschaften.org/jfowl/cargo-cross-for-ci/cargo-cross-builder:rust1.56.0-armv7-unknown-linux-gnueabihf
variables: variables:
TARGET: "armv7-unknown-linux-gnueabihf" TARGET: "armv7-unknown-linux-gnueabihf"
NEEDED_PACKAGES: "build-essential gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libc6-dev-armhf-cross"
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc
CC_armv7_unknown_linux_gnueabihf: arm-linux-gnueabihf-gcc
CXX_armv7_unknown_linux_gnueabihf: arm-linux-gnueabihf-g++
artifacts: artifacts:
name: "conduit-armv7-unknown-linux-gnueabihf" name: "conduit-armv7-unknown-linux-gnueabihf"
paths: paths:
- "conduit-armv7-unknown-linux-gnueabihf" - "conduit-armv7-unknown-linux-gnueabihf"
expose_as: "Conduit for armv7-unknown-linux-gnueabihf" expose_as: "Conduit for armv7-unknown-linux-gnueabihf"
build:release:cargo:armv7-unknown-linux-musleabihf:
extends: .build-cargo-shared-settings
image:
name: registry.gitlab.fachschaften.org/jfowl/cargo-cross-for-ci/cargo-cross-builder:rust1.56.0-armv7-unknown-linux-musleabihf
variables:
TARGET: "armv7-unknown-linux-musleabihf"
artifacts:
name: "conduit-armv7-unknown-linux-musleabihf"
paths:
- "conduit-armv7-unknown-linux-musleabihf"
expose_as: "Conduit for armv7-unknown-linux-musleabihf"
build:release:cargo:aarch64-unknown-linux-gnu: build:release:cargo:aarch64-unknown-linux-gnu:
image: registry.gitlab.fachschaften.org/jfowl/cargo-cross-for-ci/cargo-cross-builder:rust1.56.0-aarch64-unknown-linux-gnu
extends: .build-cargo-shared-settings extends: .build-cargo-shared-settings
variables: variables:
TARGET: "aarch64-unknown-linux-gnu" TARGET: "aarch64-unknown-linux-gnu"
NEEDED_PACKAGES: "build-essential gcc-10-aarch64-linux-gnu g++-aarch64-linux-gnu libc6-dev-arm64-cross"
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
CC_aarch64_unknown_linux_gnu: aarch64-linux-gnu-gcc
CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++
TARGET_CC: "/usr/bin/aarch64-linux-gnu-gcc-10"
TARGET_AR: "/usr/bin/aarch64-linux-gnu-gcc-ar-10"
artifacts: artifacts:
name: "conduit-aarch64-unknown-linux-gnu" name: "conduit-aarch64-unknown-linux-gnu"
paths: paths:
- "conduit-aarch64-unknown-linux-gnu" - "conduit-aarch64-unknown-linux-gnu"
expose_as: "Conduit for aarch64-unknown-linux-gnu" expose_as: "Conduit for aarch64-unknown-linux-gnu"
build:release:cargo:x86_64-unknown-linux-musl: build:release:cargo:aarch64-unknown-linux-musl:
image: registry.gitlab.fachschaften.org/jfowl/cargo-cross-for-ci/cargo-cross-builder:rust1.56.0-aarch64-unknown-linux-musl
extends: .build-cargo-shared-settings extends: .build-cargo-shared-settings
image: "rust:alpine"
variables: variables:
TARGET: "x86_64-unknown-linux-musl" TARGET: "aarch64-unknown-linux-musl"
before_script:
- 'echo "Building for target $TARGET"'
- 'mkdir -p cargohome && CARGOHOME="cargohome"'
- "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging
- "rustup target add $TARGET"
- "apk add libc-dev"
artifacts: artifacts:
name: "conduit-x86_64-unknown-linux-musl" name: "conduit-aarch64-unknown-linux-musl"
paths: paths:
- "conduit-x86_64-unknown-linux-musl" - "conduit-aarch64-unknown-linux-musl"
expose_as: "Conduit for x86_64-unknown-linux-musl" expose_as: "Conduit for aarch64-unknown-linux-musl"
@@ -213,7 +227,6 @@ build:cargo-deb:x86_64-unknown-linux-gnu:
- "mkdir -p /kaniko/.docker" - "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' - '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: build:docker:next:
extends: .docker-shared-settings extends: .docker-shared-settings
needs: needs:
@@ -280,6 +293,40 @@ build:docker:tags:
- if: '$CI_COMMIT_TAG' - if: '$CI_COMMIT_TAG'
build:docker:new-way-of-building:
stage: "build docker image"
image: jdrouet/docker-with-buildx:stable
tags: ["docker"]
services:
- docker:dind
needs:
- "build:release:cargo:x86_64-unknown-linux-musl"
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: '$CI_COMMIT_BRANCH == "next"'
# TODO: Remove this after debugging
- if: '$CI_COMMIT_BRANCH == "multiarch-docker-support"'
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2
# PLATFORMS: "linux/arm/v7,linux/arm64/v8,linux/amd64"
PLATFORMS: "linux/amd64"
IMAGE_TAG: "$CI_REGISTRY_IMAGE/debug-conduit:multiarch-test-dont-use-yet"
DOCKER_FILE: "docker/ci-binaries-packaging.Dockerfile"
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
# Prepare buildx to build multiarch stuff:
- docker context create 'ci-context'
- docker buildx create --name 'multiarch-builder' --use 'ci-context'
# Copy binaries to their docker arch path
- mkdir -p linux/ && mv ./conduit-x86_64-unknown-linux-musl linux/amd64
# Actually build multiarch image:
- docker buildx build --push --platform $PLATFORMS --tag $IMAGE_TAG --file $DOCKER_FILE .
# --------------------------------------------------------------------- # # --------------------------------------------------------------------- #
# Run tests # # Run tests #
Generated
+27 -49
View File
@@ -254,7 +254,6 @@ dependencies = [
"opentelemetry-jaeger", "opentelemetry-jaeger",
"parking_lot", "parking_lot",
"rand 0.8.4", "rand 0.8.4",
"rayon",
"regex", "regex",
"reqwest", "reqwest",
"ring", "ring",
@@ -1748,31 +1747,6 @@ dependencies = [
"rand_core 0.6.3", "rand_core 0.6.3",
] ]
[[package]]
name = "rayon"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils 0.8.5",
"lazy_static",
"num_cpus",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.10" version = "0.2.10"
@@ -1994,7 +1968,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@@ -2014,8 +1988,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api" name = "ruma-api"
version = "0.18.3" version = "0.18.5"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@@ -2030,8 +2004,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api-macros" name = "ruma-api-macros"
version = "0.18.3" version = "0.18.5"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@@ -2042,7 +2016,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"ruma-api", "ruma-api",
"ruma-common", "ruma-common",
@@ -2055,8 +2029,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.12.2" version = "0.12.3"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"assign", "assign",
"bytes", "bytes",
@@ -2076,7 +2050,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.6.0" version = "0.6.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"js_int", "js_int",
@@ -2090,8 +2064,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.24.5" version = "0.24.6"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"indoc", "indoc",
"js_int", "js_int",
@@ -2106,8 +2080,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events-macros" name = "ruma-events-macros"
version = "0.24.5" version = "0.24.6"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@@ -2118,7 +2092,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@@ -2133,9 +2107,10 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers" name = "ruma-identifiers"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"paste", "paste",
"percent-encoding",
"rand 0.8.4", "rand 0.8.4",
"ruma-identifiers-macros", "ruma-identifiers-macros",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@@ -2147,7 +2122,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-macros" name = "ruma-identifiers-macros"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"quote", "quote",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@@ -2157,12 +2132,15 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "ruma-identity-service-api" name = "ruma-identity-service-api"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@@ -2175,7 +2153,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-push-gateway-api" name = "ruma-push-gateway-api"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@@ -2190,7 +2168,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde" name = "ruma-serde"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"bytes", "bytes",
"form_urlencoded", "form_urlencoded",
@@ -2204,7 +2182,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde-macros" name = "ruma-serde-macros"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@@ -2215,7 +2193,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"ed25519-dalek", "ed25519-dalek",
@@ -2232,7 +2210,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-state-res" name = "ruma-state-res"
version = "0.4.1" version = "0.4.1"
source = "git+https://github.com/timokoesters/ruma?rev=6e9823de284967a41100db0e3134319f4b6a0cfa#6e9823de284967a41100db0e3134319f4b6a0cfa" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"itertools 0.10.1", "itertools 0.10.1",
"js_int", "js_int",
+2 -3
View File
@@ -19,8 +19,8 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request
# Used for matrix spec type definitions and helpers # Used for matrix spec type definitions and helpers
#ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { git = "https://github.com/ruma/ruma", rev = "a6a1224652912a957b09f136ec5da2686be6e0e2", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } ruma = { git = "https://github.com/ruma/ruma", rev = "58cdcae1f9a8f4824bcbec1de1bb13e659c66804", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/timokoesters/ruma", rev = "6e9823de284967a41100db0e3134319f4b6a0cfa", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { git = "https://github.com/timokoesters/ruma", rev = "50c1db7e0a3a21fc794b0cce3b64285a4c750c71", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
# Used for long polling and federation sender, should be the same as rocket::tokio # Used for long polling and federation sender, should be the same as rocket::tokio
@@ -79,7 +79,6 @@ num_cpus = "1.13.0"
threadpool = "1.8.1" threadpool = "1.8.1"
heed = { git = "https://github.com/timokoesters/heed.git", rev = "f6f825da7fb2c758867e05ad973ef800a6fe1d5d", optional = true } heed = { git = "https://github.com/timokoesters/heed.git", rev = "f6f825da7fb2c758867e05ad973ef800a6fe1d5d", optional = true }
thread_local = "1.1.3" thread_local = "1.1.3"
rayon = "1.5.1"
[features] [features]
default = ["conduit_bin", "backend_sqlite"] default = ["conduit_bin", "backend_sqlite"]
+39 -84
View File
@@ -1,97 +1,52 @@
# Using multistage build: # syntax=docker/dockerfile:1
# https://docs.docker.com/develop/develop-images/multistage-build/ FROM rust:1.53-alpine as builder
# https://whitfin.io/speeding-up-rust-docker-builds/ WORKDIR /usr/src/conduit
# == Build dependencies without our own code separately for caching ==
#
# Need a fake main.rs since Cargo refuses to build anything otherwise.
#
# See https://github.com/rust-lang/cargo/issues/2644 for a Cargo feature
# request that would allow just dependencies to be compiled, presumably
# regardless of whether source files are available.
RUN mkdir src && echo 'fn main() {}' > src/main.rs
COPY Cargo.toml Cargo.lock ./
RUN cargo build
# TODO: RUN cargo build --release
# == Actual build ==
RUN rm -r src
COPY src src
# main.rs has to have its timestamp updated for this to work correctly since
# otherwise the build with the fake main.rs from above is newer than the
# source files (COPY preserves timestamps).
RUN touch src/main.rs
RUN cargo install --path .
# TODO: RUN cargo install --release --path .
########################## BUILD IMAGE ########################## # This build stage is going to be run later
# Alpine build image to build Conduit's statically compiled binary
FROM alpine:3.14 as builder
# Install packages needed for building all crates
RUN apk add --no-cache \
cargo \
openssl-dev
# Specifies if the local project is build or if Conduit gets build
# from the official git repository. Defaults to the git repo.
ARG LOCAL=false
# Specifies which revision/commit is build. Defaults to HEAD
ARG GIT_REF=origin/master
# Copy project files from current folder
COPY . .
# Build it from the copied local files or from the official git repository
RUN if [[ $LOCAL == "true" ]]; then \
mv ./docker/healthcheck.sh . ; \
echo "Building from local source..." ; \
cargo install --path . ; \
else \
echo "Building revision '${GIT_REF}' from online source..." ; \
cargo install --git "https://gitlab.com/famedly/conduit.git" --rev ${GIT_REF} ; \
echo "Loadings healthcheck script from online source..." ; \
wget "https://gitlab.com/famedly/conduit/-/raw/${GIT_REF#origin/}/docker/healthcheck.sh" ; \
fi
########################## RUNTIME IMAGE ##########################
# Create new stage with a minimal image for the actual
# runtime image/container
FROM alpine:3.14 FROM alpine:3.14
ARG CREATED
ARG VERSION
ARG GIT_REF=origin/master
ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml"
# Labels according to https://github.com/opencontainers/image-spec/blob/master/annotations.md
# including a custom label specifying the build command
LABEL org.opencontainers.image.created=${CREATED} \
org.opencontainers.image.authors="Conduit Contributors" \
org.opencontainers.image.title="Conduit" \
org.opencontainers.image.version=${VERSION} \
org.opencontainers.image.vendor="Conduit Contributors" \
org.opencontainers.image.description="A Matrix homeserver written in Rust" \
org.opencontainers.image.url="https://conduit.rs/" \
org.opencontainers.image.revision=${GIT_REF} \
org.opencontainers.image.source="https://gitlab.com/famedly/conduit.git" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.documentation="" \
org.opencontainers.image.ref.name="" \
org.label-schema.docker.build="docker build . -t matrixconduit/matrix-conduit:latest --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)" \
maintainer="Weasy666"
# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose.
EXPOSE 6167
# Copy config files from context and the binary from
# the "builder" stage to the current stage into folder
# /srv/conduit and create data folder for database
RUN mkdir -p /srv/conduit/.local/share/conduit
COPY --from=builder /root/.cargo/bin/conduit /srv/conduit/
COPY --from=builder ./healthcheck.sh /srv/conduit/
# Add www-data user and group with UID 82, as used by alpine
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
RUN set -x ; \
addgroup -Sg 82 www-data 2>/dev/null ; \
adduser -S -D -H -h /srv/conduit -G www-data -g www-data www-data 2>/dev/null ; \
addgroup www-data www-data 2>/dev/null && exit 0 ; exit 1
# Change ownership of Conduit files to www-data user and group
RUN chown -cR www-data:www-data /srv/conduit
# Install packages needed to run Conduit # Install packages needed to run Conduit
RUN apk add --no-cache \ RUN apk add --no-cache \
ca-certificates \ ca-certificates \
curl \ curl \
libgcc libgcc
# Test if Conduit is still alive, uses the same endpoint as Element # Prepare path for database and media files
HEALTHCHECK --start-period=5s --interval=60s CMD ./healthcheck.sh RUN mkdir -p /srv/conduit/.local/share/conduit
# TODO: Change ? or maybe leave it like that
RUN mkdir -p /srv/conduit/.local/share/conduit
COPY --from=builder /usr/local/cargo/bin/conduit /srv/conduit/
# TODO: Check if we don't want to just use ENVs for running condit in docker
ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml"
# TODO: not needed, but documents it?
EXPOSE 6167
# Set user to www-data
USER www-data
# Set container home directory
WORKDIR /srv/conduit WORKDIR /srv/conduit
# Run Conduit
ENTRYPOINT [ "/srv/conduit/conduit" ] ENTRYPOINT [ "/srv/conduit/conduit" ]
+1
View File
@@ -40,6 +40,7 @@ trusted_servers = ["matrix.org"]
#workers = 4 # default: cpu core count * 2 #workers = 4 # default: cpu core count * 2
address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy
#address = "0.0.0.0" # If Conduit is running in a container, make sure the reverse proxy (ie. Traefik) can reach it.
proxy = "none" # more examples can be found at src/database/proxy.rs:6 proxy = "none" # more examples can be found at src/database/proxy.rs:6
+11 -7
View File
@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
# --------------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------------
# This Dockerfile is intended to be built as part of Conduit's CI pipeline. # This Dockerfile is intended to be built as part of Conduit's CI pipeline.
# It does not build Conduit in Docker, but just copies the matching build artifact from the build job. # It does not build Conduit in Docker, but just copies the matching build artifact from the build job.
@@ -33,7 +34,7 @@ LABEL org.opencontainers.image.created=${CREATED} \
org.opencontainers.image.revision=${GIT_REF} \ org.opencontainers.image.revision=${GIT_REF} \
org.opencontainers.image.source="https://gitlab.com/famedly/conduit.git" \ org.opencontainers.image.source="https://gitlab.com/famedly/conduit.git" \
org.opencontainers.image.licenses="Apache-2.0" \ org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.documentation="" \ org.opencontainers.image.documentation="https://gitlab.com/famedly/conduit" \
org.opencontainers.image.ref.name="" org.opencontainers.image.ref.name=""
# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose. # Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose.
@@ -42,9 +43,16 @@ EXPOSE 6167
# create data folder for database # create data folder for database
RUN mkdir -p /srv/conduit/.local/share/conduit RUN mkdir -p /srv/conduit/.local/share/conduit
# Copy the Conduit binary into the image at the latest possible moment to maximise caching: # Test if Conduit is still alive, uses the same endpoint as Element
COPY ./conduit-x86_64-unknown-linux-musl /srv/conduit/conduit
COPY ./docker/healthcheck.sh /srv/conduit/ COPY ./docker/healthcheck.sh /srv/conduit/
HEALTHCHECK --start-period=5s --interval=60s CMD ./healthcheck.sh
# Copy the Conduit binary into the image at the latest possible moment to maximise caching:
# depending on the target platform (e.g. "linux/arm/v7", "linux/arm64/v8", or "linux/amd64")
# copy the matching binary into this docker image
ARG TARGETPLATFORM
COPY ./$TARGETPLATFORM /srv/conduit/conduit
# Add www-data user and group with UID 82, as used by alpine # Add www-data user and group with UID 82, as used by alpine
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install # https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
@@ -57,10 +65,6 @@ RUN set -x ; \
RUN chown -cR www-data:www-data /srv/conduit RUN chown -cR www-data:www-data /srv/conduit
RUN chmod +x /srv/conduit/healthcheck.sh RUN chmod +x /srv/conduit/healthcheck.sh
# Test if Conduit is still alive, uses the same endpoint as Element
HEALTHCHECK --start-period=5s --interval=60s CMD ./healthcheck.sh
# Set user to www-data # Set user to www-data
USER www-data USER www-data
# Set container home directory # Set container home directory
+1 -1
View File
@@ -1 +1 @@
1.52 1.53
+37 -71
View File
@@ -15,20 +15,28 @@ use ruma::{
ThirdPartyIdRemovalStatus, ThirdPartyIdRemovalStatus,
}, },
contact::get_contacts, contact::get_contacts,
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}, },
events::{ events::{
room::{ room::{
canonical_alias, guest_access, history_visibility, join_rules, member, message, name, canonical_alias::RoomCanonicalAliasEventContent,
topic, create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
topic::RoomTopicEventContent,
}, },
EventType, EventType,
}, },
identifiers::RoomName, identifiers::RoomName,
push, RoomAliasId, RoomId, RoomVersionId, UserId, push, RoomAliasId, RoomId, RoomVersionId, UserId,
}; };
use serde_json::value::RawValue; use serde_json::value::to_raw_value;
use tracing::info; use tracing::info;
use register::RegistrationKind; use register::RegistrationKind;
@@ -148,7 +156,7 @@ pub async fn register_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.dummy".to_owned()], stages: vec![AuthType::Dummy],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@@ -271,7 +279,7 @@ pub async fn register_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let mut content = ruma::events::room::create::CreateEventContent::new(conduit_user.clone()); let mut content = RoomCreateEventContent::new(conduit_user.clone());
content.federate = true; content.federate = true;
content.predecessor = None; content.predecessor = None;
content.room_version = RoomVersionId::Version6; content.room_version = RoomVersionId::Version6;
@@ -280,10 +288,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: RawValue::from_string( content: to_raw_value(&content).expect("event is valid, we just created it"),
serde_json::to_string(&content).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -298,9 +303,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Join,
membership: member::MembershipState::Join,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@@ -309,8 +313,6 @@ pub async fn register_route(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(conduit_user.to_string()), state_key: Some(conduit_user.to_string()),
redacts: None, redacts: None,
@@ -329,16 +331,11 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: RawValue::from_string( content: to_raw_value(&RoomPowerLevelsEventContent {
serde_json::to_string(
&ruma::events::room::power_levels::PowerLevelsEventContent {
users, users,
..Default::default() ..Default::default()
}, })
)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -353,13 +350,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomJoinRules, event_type: EventType::RoomJoinRules,
content: RawValue::from_string( content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
serde_json::to_string(&join_rules::JoinRulesEventContent::new(
join_rules::JoinRule::Invite,
))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -374,13 +366,10 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomHistoryVisibility, event_type: EventType::RoomHistoryVisibility,
content: RawValue::from_string( content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
serde_json::to_string(&history_visibility::HistoryVisibilityEventContent::new( HistoryVisibility::Shared,
history_visibility::HistoryVisibility::Shared,
)) ))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -395,13 +384,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomGuestAccess, event_type: EventType::RoomGuestAccess,
content: RawValue::from_string( content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
serde_json::to_string(&guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::Forbidden,
))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -419,11 +403,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomName, event_type: EventType::RoomName,
content: RawValue::from_string( content: to_raw_value(&RoomNameEventContent::new(Some(room_name)))
serde_json::to_string(&name::NameEventContent::new(Some(room_name)))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -437,13 +418,10 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTopic, event_type: EventType::RoomTopic,
content: RawValue::from_string( content: to_raw_value(&RoomTopicEventContent {
serde_json::to_string(&topic::TopicEventContent {
topic: format!("Manage {}", db.globals.server_name()), topic: format!("Manage {}", db.globals.server_name()),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -462,14 +440,11 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCanonicalAlias, event_type: EventType::RoomCanonicalAlias,
content: RawValue::from_string( content: to_raw_value(&RoomCanonicalAliasEventContent {
serde_json::to_string(&canonical_alias::CanonicalAliasEventContent {
alias: Some(alias.clone()), alias: Some(alias.clone()),
alt_aliases: Vec::new(), alt_aliases: Vec::new(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -486,9 +461,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Invite,
membership: member::MembershipState::Invite,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@@ -497,8 +471,6 @@ pub async fn register_route(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
@@ -511,9 +483,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Join,
membership: member::MembershipState::Join,
displayname: Some(displayname), displayname: Some(displayname),
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@@ -522,8 +493,6 @@ pub async fn register_route(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
@@ -538,11 +507,11 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMessage, event_type: EventType::RoomMessage,
content: RawValue::from_string(serde_json::to_string(&message::MessageEventContent::text_html( content: to_raw_value(&RoomMessageEventContent::text_html(
"## Thank you for trying out Conduit!\n\nConduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Website: https://conduit.rs\n> Git and Documentation: https://gitlab.com/famedly/conduit\n> Report issues: https://gitlab.com/famedly/conduit/-/issues\n\nHere are some rooms you can join (by typing the command):\n\nConduit room (Ask questions and get notified on updates):\n`/join #conduit:fachschaften.org`\n\nConduit lounge (Off-topic, only Conduit users are allowed to join)\n`/join #conduit-lounge:conduit.rs`".to_owned(), "## Thank you for trying out Conduit!\n\nConduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Website: https://conduit.rs\n> Git and Documentation: https://gitlab.com/famedly/conduit\n> Report issues: https://gitlab.com/famedly/conduit/-/issues\n\nHere are some rooms you can join (by typing the command):\n\nConduit room (Ask questions and get notified on updates):\n`/join #conduit:fachschaften.org`\n\nConduit lounge (Off-topic, only Conduit users are allowed to join)\n`/join #conduit-lounge:conduit.rs`".to_owned(),
"<h2>Thank you for trying out Conduit!</h2>\n<p>Conduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Website: https://conduit.rs<br>Git and Documentation: https://gitlab.com/famedly/conduit<br>Report issues: https://gitlab.com/famedly/conduit/-/issues</p>\n</blockquote>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>Conduit room (Ask questions and get notified on updates):<br><code>/join #conduit:fachschaften.org</code></p>\n<p>Conduit lounge (Off-topic, only Conduit users are allowed to join)<br><code>/join #conduit-lounge:conduit.rs</code></p>\n".to_owned(), "<h2>Thank you for trying out Conduit!</h2>\n<p>Conduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Website: https://conduit.rs<br>Git and Documentation: https://gitlab.com/famedly/conduit<br>Report issues: https://gitlab.com/famedly/conduit/-/issues</p>\n</blockquote>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>Conduit room (Ask questions and get notified on updates):<br><code>/join #conduit:fachschaften.org</code></p>\n<p>Conduit lounge (Off-topic, only Conduit users are allowed to join)<br><code>/join #conduit-lounge:conduit.rs</code></p>\n".to_owned(),
)) ))
.expect("event is valid, we just created it")).expect("string is valid"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: None, state_key: None,
redacts: None, redacts: None,
@@ -594,7 +563,7 @@ pub async fn change_password_route(
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@@ -686,7 +655,7 @@ pub async fn deactivate_route(
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@@ -730,8 +699,8 @@ pub async fn deactivate_route(
for room_id in all_rooms { for room_id in all_rooms {
let room_id = room_id?; let room_id = room_id?;
let event = member::MemberEventContent { let event = RoomMemberEventContent {
membership: member::MembershipState::Leave, membership: MembershipState::Leave,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@@ -753,10 +722,7 @@ pub async fn deactivate_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
+6 -6
View File
@@ -30,7 +30,7 @@ pub async fn set_global_account_data_route(
) -> ConduitResult<set_global_account_data::Response> { ) -> ConduitResult<set_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data = serde_json::from_str::<serde_json::Value>(body.data.get()) let data: serde_json::Value = serde_json::from_str(body.data.get())
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
let event_type = body.event_type.to_string(); let event_type = body.event_type.to_string();
@@ -68,7 +68,7 @@ pub async fn set_room_account_data_route(
) -> ConduitResult<set_room_account_data::Response> { ) -> ConduitResult<set_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data = serde_json::from_str::<serde_json::Value>(body.data.get()) let data: serde_json::Value = serde_json::from_str(body.data.get())
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
let event_type = body.event_type.to_string(); let event_type = body.event_type.to_string();
@@ -103,9 +103,9 @@ pub async fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> { ) -> ConduitResult<get_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event = db let event: Box<RawJsonValue> = db
.account_data .account_data
.get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())? .get(None, sender_user, body.event_type.clone().into())?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get()) let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
@@ -132,9 +132,9 @@ pub async fn get_room_account_data_route(
) -> ConduitResult<get_room_account_data::Response> { ) -> ConduitResult<get_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event = db let event: Box<RawJsonValue> = db
.account_data .account_data
.get::<Box<RawJsonValue>>( .get(
Some(&body.room_id), Some(&body.room_id),
sender_user, sender_user,
body.event_type.clone().into(), body.event_type.clone().into(),
+8 -8
View File
@@ -48,7 +48,7 @@ pub async fn get_context_route(
))? ))?
.to_room_event(); .to_room_event();
let events_before = db let events_before: Vec<_> = db
.rooms .rooms
.pdus_until(sender_user, &body.room_id, base_token)? .pdus_until(sender_user, &body.room_id, base_token)?
.take( .take(
@@ -58,19 +58,19 @@ pub async fn get_context_route(
/ 2, / 2,
) )
.filter_map(|r| r.ok()) // Remove buggy events .filter_map(|r| r.ok()) // Remove buggy events
.collect::<Vec<_>>(); .collect();
let start_token = events_before let start_token = events_before
.last() .last()
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok()) .and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
.map(|count| count.to_string()); .map(|count| count.to_string());
let events_before = events_before let events_before: Vec<_> = events_before
.into_iter() .into_iter()
.map(|(_, pdu)| pdu.to_room_event()) .map(|(_, pdu)| pdu.to_room_event())
.collect::<Vec<_>>(); .collect();
let events_after = db let events_after: Vec<_> = db
.rooms .rooms
.pdus_after(sender_user, &body.room_id, base_token)? .pdus_after(sender_user, &body.room_id, base_token)?
.take( .take(
@@ -80,17 +80,17 @@ pub async fn get_context_route(
/ 2, / 2,
) )
.filter_map(|r| r.ok()) // Remove buggy events .filter_map(|r| r.ok()) // Remove buggy events
.collect::<Vec<_>>(); .collect();
let end_token = events_after let end_token = events_after
.last() .last()
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok()) .and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
.map(|count| count.to_string()); .map(|count| count.to_string());
let events_after = events_after let events_after: Vec<_> = events_after
.into_iter() .into_iter()
.map(|(_, pdu)| pdu.to_room_event()) .map(|(_, pdu)| pdu.to_room_event())
.collect::<Vec<_>>(); .collect();
let mut resp = get_context::Response::new(); let mut resp = get_context::Response::new();
resp.start = start_token; resp.start = start_token;
+5 -5
View File
@@ -3,7 +3,7 @@ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
r0::{ r0::{
device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, device::{self, delete_device, delete_devices, get_device, get_devices, update_device},
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}; };
@@ -25,11 +25,11 @@ pub async fn get_devices_route(
) -> ConduitResult<get_devices::Response> { ) -> ConduitResult<get_devices::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let devices = db let devices: Vec<device::Device> = db
.users .users
.all_devices_metadata(sender_user) .all_devices_metadata(sender_user)
.filter_map(|r| r.ok()) // Filter out buggy devices .filter_map(|r| r.ok()) // Filter out buggy devices
.collect::<Vec<device::Device>>(); .collect();
Ok(get_devices::Response { devices }.into()) Ok(get_devices::Response { devices }.into())
} }
@@ -109,7 +109,7 @@ pub async fn delete_device_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@@ -172,7 +172,7 @@ pub async fn delete_devices_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
+39 -46
View File
@@ -17,7 +17,14 @@ use ruma::{
}, },
directory::{Filter, IncomingFilter, IncomingRoomNetwork, PublicRoomsChunk, RoomNetwork}, directory::{Filter, IncomingFilter, IncomingRoomNetwork, PublicRoomsChunk, RoomNetwork},
events::{ events::{
room::{avatar, canonical_alias, guest_access, history_visibility, name, topic}, room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
name::RoomNameEventContent,
topic::RoomTopicEventContent,
},
EventType, EventType,
}, },
ServerName, UInt, ServerName, UInt,
@@ -216,7 +223,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
} }
} }
let mut all_rooms = db let mut all_rooms: Vec<_> = db
.rooms .rooms
.public_rooms() .public_rooms()
.map(|room_id| { .map(|room_id| {
@@ -227,28 +234,22 @@ pub(crate) async fn get_public_rooms_filtered_helper(
canonical_alias: db canonical_alias: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")? .room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")?
.map_or(Ok::<_, Error>(None), |s| { .map_or(Ok(None), |s| {
Ok( serde_json::from_str(s.content.get())
serde_json::from_str::<canonical_alias::CanonicalAliasEventContent>( .map(|c: RoomCanonicalAliasEventContent| c.alias)
s.content.get(),
)
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid canonical alias event in database.") Error::bad_database("Invalid canonical alias event in database.")
})? })
.alias,
)
})?, })?,
name: db name: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomName, "")? .room_state_get(&room_id, &EventType::RoomName, "")?
.map_or(Ok::<_, Error>(None), |s| { .map_or(Ok(None), |s| {
Ok( serde_json::from_str(s.content.get())
serde_json::from_str::<name::NameEventContent>(s.content.get()) .map(|c: RoomNameEventContent| c.name)
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room name event in database.") Error::bad_database("Invalid room name event in database.")
})? })
.name,
)
})?, })?,
num_joined_members: db num_joined_members: db
.rooms .rooms
@@ -262,56 +263,48 @@ pub(crate) async fn get_public_rooms_filtered_helper(
topic: db topic: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomTopic, "")? .room_state_get(&room_id, &EventType::RoomTopic, "")?
.map_or(Ok::<_, Error>(None), |s| { .map_or(Ok(None), |s| {
Ok(Some( serde_json::from_str(s.content.get())
serde_json::from_str::<topic::TopicEventContent>(s.content.get()) .map(|c: RoomTopicEventContent| Some(c.topic))
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room topic event in database.") Error::bad_database("Invalid room topic event in database.")
})? })
.topic,
))
})?, })?,
world_readable: db world_readable: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")?
.map_or(Ok::<_, Error>(false), |s| { .map_or(Ok(false), |s| {
Ok(serde_json::from_str::< serde_json::from_str(s.content.get())
history_visibility::HistoryVisibilityEventContent, .map(|c: RoomHistoryVisibilityEventContent| {
>(s.content.get()) c.history_visibility == HistoryVisibility::WorldReadable
})
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
) )
})? })
.history_visibility
== history_visibility::HistoryVisibility::WorldReadable)
})?, })?,
guest_can_join: db guest_can_join: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomGuestAccess, "")? .room_state_get(&room_id, &EventType::RoomGuestAccess, "")?
.map_or(Ok::<_, Error>(false), |s| { .map_or(Ok(false), |s| {
Ok( serde_json::from_str(s.content.get())
serde_json::from_str::<guest_access::GuestAccessEventContent>( .map(|c: RoomGuestAccessEventContent| {
s.content.get(), c.guest_access == GuestAccess::CanJoin
) })
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room guest access event in database.") Error::bad_database("Invalid room guest access event in database.")
})? })
.guest_access
== guest_access::GuestAccess::CanJoin,
)
})?, })?,
avatar_url: db avatar_url: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomAvatar, "")? .room_state_get(&room_id, &EventType::RoomAvatar, "")?
.map(|s| { .map(|s| {
Ok::<_, Error>( serde_json::from_str(s.content.get())
serde_json::from_str::<avatar::AvatarEventContent>(s.content.get()) .map(|c: RoomAvatarEventContent| c.url)
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room avatar event in database.") Error::bad_database("Invalid room avatar event in database.")
})? })
.url,
)
}) })
.transpose()? .transpose()?
// url is now an Option<String> so we must flatten // url is now an Option<String> so we must flatten
@@ -352,17 +345,17 @@ pub(crate) async fn get_public_rooms_filtered_helper(
} }
}) })
// We need to collect all, so we can sort by member count // We need to collect all, so we can sort by member count
.collect::<Vec<_>>(); .collect();
all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members));
let total_room_count_estimate = (all_rooms.len() as u32).into(); let total_room_count_estimate = (all_rooms.len() as u32).into();
let chunk = all_rooms let chunk: Vec<_> = all_rooms
.into_iter() .into_iter()
.skip(num_since as usize) .skip(num_since as usize)
.take(limit as usize) .take(limit as usize)
.collect::<Vec<_>>(); .collect();
let prev_batch = if num_since == 0 { let prev_batch = if num_since == 0 {
None None
+4 -4
View File
@@ -10,7 +10,7 @@ use ruma::{
claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures, claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures,
upload_signing_keys, upload_signing_keys,
}, },
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}, },
federation, federation,
@@ -148,7 +148,7 @@ pub async fn upload_signing_keys_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@@ -395,7 +395,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
let mut failures = BTreeMap::new(); let mut failures = BTreeMap::new();
let mut futures = get_over_federation let mut futures: FuturesUnordered<_> = get_over_federation
.into_iter() .into_iter()
.map(|(server, vec)| async move { .map(|(server, vec)| async move {
let mut device_keys_input_fed = BTreeMap::new(); let mut device_keys_input_fed = BTreeMap::new();
@@ -415,7 +415,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
.await, .await,
) )
}) })
.collect::<FuturesUnordered<_>>(); .collect();
while let Some((server, response)) = futures.next().await { while let Some((server, response)) = futures.next().await {
match response { match response {
+171 -264
View File
@@ -1,11 +1,9 @@
use crate::{ use crate::{
client_server, client_server,
database::DatabaseGuard, database::DatabaseGuard,
pdu::{PduBuilder, PduEvent}, pdu::{EventHash, PduBuilder, PduEvent},
server_server, utils, ConduitResult, Database, Error, Result, Ruma, server_server, utils, ConduitResult, Database, Error, Result, Ruma,
}; };
use member::{MemberEventContent, MembershipState};
use rayon::prelude::*;
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
@@ -16,27 +14,27 @@ use ruma::{
unban_user, IncomingThirdPartySigned, unban_user, IncomingThirdPartySigned,
}, },
}, },
federation::{ federation::{self, membership::create_invite},
self,
membership::{create_invite, create_join_event},
},
}, },
events::{ events::{
pdu::Pdu, room::{
room::{create::CreateEventContent, member}, create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
},
EventType, EventType,
}, },
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue},
state_res::{self, RoomVersion}, state_res::{self, RoomVersion},
uint, EventId, RoomId, RoomVersionId, ServerName, UserId, uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde_json::value::RawValue; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
sync::{Arc, RwLock}, sync::{Arc, RwLock},
time::{Duration, Instant},
}; };
use tracing::{error, warn}; use tracing::{debug, error, warn};
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::{get, post}; use rocket::{get, post};
@@ -58,19 +56,17 @@ pub async fn join_room_by_id_route(
) -> ConduitResult<join_room_by_id::Response> { ) -> ConduitResult<join_room_by_id::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut servers = db let mut servers: HashSet<_> = db
.rooms .rooms
.invite_state(sender_user, &body.room_id)? .invite_state(sender_user, &body.room_id)?
.unwrap_or_default() .unwrap_or_default()
.iter() .iter()
.filter_map(|event| { .filter_map(|event| serde_json::from_str(event.json().get()).ok())
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok() .filter_map(|event: serde_json::Value| event.get("sender").cloned())
})
.filter_map(|event| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| UserId::try_from(sender).ok()) .filter_map(|sender| UserId::try_from(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect::<HashSet<_>>(); .collect();
servers.insert(body.room_id.server_name().to_owned()); servers.insert(body.room_id.server_name().to_owned());
@@ -107,19 +103,17 @@ pub async fn join_room_by_id_or_alias_route(
let (servers, room_id) = match RoomId::try_from(body.room_id_or_alias.clone()) { let (servers, room_id) = match RoomId::try_from(body.room_id_or_alias.clone()) {
Ok(room_id) => { Ok(room_id) => {
let mut servers = db let mut servers: HashSet<_> = db
.rooms .rooms
.invite_state(sender_user, &room_id)? .invite_state(sender_user, &room_id)?
.unwrap_or_default() .unwrap_or_default()
.iter() .iter()
.filter_map(|event| { .filter_map(|event| serde_json::from_str(event.json().get()).ok())
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok() .filter_map(|event: serde_json::Value| event.get("sender").cloned())
})
.filter_map(|event| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| UserId::try_from(sender).ok()) .filter_map(|sender| UserId::try_from(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect::<HashSet<_>>(); .collect();
servers.insert(room_id.server_name().to_owned()); servers.insert(room_id.server_name().to_owned());
(servers, room_id) (servers, room_id)
@@ -208,7 +202,7 @@ pub async fn kick_user_route(
) -> ConduitResult<kick_user::Response> { ) -> ConduitResult<kick_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = serde_json::from_str::<Raw<ruma::events::room::member::MemberEventContent>>( let mut event: RoomMemberEventContent = serde_json::from_str(
db.rooms db.rooms
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@@ -222,11 +216,9 @@ pub async fn kick_user_route(
.content .content
.get(), .get(),
) )
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Leave; event.membership = MembershipState::Leave;
// TODO: reason // TODO: reason
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
@@ -242,10 +234,7 @@ pub async fn kick_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@@ -287,8 +276,8 @@ pub async fn ban_user_route(
&body.user_id.to_string(), &body.user_id.to_string(),
)? )?
.map_or( .map_or(
Ok::<_, Error>(member::MemberEventContent { Ok(RoomMemberEventContent {
membership: member::MembershipState::Ban, membership: MembershipState::Ban,
displayname: db.users.displayname(&body.user_id)?, displayname: db.users.displayname(&body.user_id)?,
avatar_url: db.users.avatar_url(&body.user_id)?, avatar_url: db.users.avatar_url(&body.user_id)?,
is_direct: None, is_direct: None,
@@ -297,11 +286,12 @@ pub async fn ban_user_route(
reason: None, reason: None,
}), }),
|event| { |event| {
let mut event = serde_json::from_str(event.content.get())
serde_json::from_str::<member::MemberEventContent>(event.content.get()) .map(|event: RoomMemberEventContent| RoomMemberEventContent {
.map_err(|_| Error::bad_database("Invalid member event in database."))?; membership: MembershipState::Ban,
event.membership = ruma::events::room::member::MembershipState::Ban; ..event
Ok(event) })
.map_err(|_| Error::bad_database("Invalid member event in database."))
}, },
)?; )?;
@@ -318,10 +308,7 @@ pub async fn ban_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@@ -353,7 +340,7 @@ pub async fn unban_user_route(
) -> ConduitResult<unban_user::Response> { ) -> ConduitResult<unban_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = serde_json::from_str::<ruma::events::room::member::MemberEventContent>( let mut event: RoomMemberEventContent = serde_json::from_str(
db.rooms db.rooms
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@@ -369,7 +356,7 @@ pub async fn unban_user_route(
) )
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Leave; event.membership = MembershipState::Leave;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
db.globals db.globals
@@ -384,10 +371,7 @@ pub async fn unban_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@@ -571,7 +555,6 @@ async fn join_room_by_id_helper(
}, },
) )
.await; .await;
warn!("Make join done");
make_join_response_and_server = make_join_response.map(|r| (r, remote_server)); make_join_response_and_server = make_join_response.map(|r| (r, remote_server));
@@ -592,9 +575,8 @@ async fn join_room_by_id_helper(
_ => return Err(Error::BadServerResponse("Room version is not supported")), _ => return Err(Error::BadServerResponse("Room version is not supported")),
}; };
let mut join_event_stub = let mut join_event_stub: CanonicalJsonObject =
serde_json::from_str::<CanonicalJsonObject>(make_join_response.event.json().get()) serde_json::from_str(make_join_response.event.get()).map_err(|_| {
.map_err(|_| {
Error::BadServerResponse("Invalid make_join event json received from server.") Error::BadServerResponse("Invalid make_join event json received from server.")
})?; })?;
@@ -613,8 +595,8 @@ async fn join_room_by_id_helper(
); );
join_event_stub.insert( join_event_stub.insert(
"content".to_owned(), "content".to_owned(),
to_canonical_value(member::MemberEventContent { to_canonical_value(RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@@ -662,198 +644,98 @@ async fn join_room_by_id_helper(
federation::membership::create_join_event::v2::Request { federation::membership::create_join_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id: &event_id,
pdu: PduEvent::convert_to_outgoing_federation_event(join_event.clone()), pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
}, },
) )
.await?; .await?;
warn!("Send join done");
db.rooms.get_or_create_shortroomid(room_id, &db.globals)?; db.rooms.get_or_create_shortroomid(room_id, &db.globals)?;
let pdu = PduEvent::from_id_val(&event_id, join_event.clone()) let pdu = PduEvent::from_id_val(&event_id, join_event.clone())
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?; .map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
let pub_key_map = Arc::new(RwLock::new(BTreeMap::new())); let mut state = HashMap::new();
let missing_servers = Arc::new(RwLock::new(BTreeMap::new())); let pub_key_map = RwLock::new(BTreeMap::new());
let create_join_event::RoomState { server_server::fetch_join_signing_keys(
state: mut room_state_state, &send_join_response,
auth_chain: mut room_state_auth_chain, &room_version,
} = send_join_response.room_state; &pub_key_map,
db,
)
.await?;
for result in send_join_response
.room_state
.state
.iter()
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, db))
{
let (event_id, value) = match result {
Ok(t) => t,
Err(_) => continue,
};
let pdu = PduEvent::from_id_val(&event_id, value.clone()).map_err(|e| {
warn!("{:?}: {}", value, e);
Error::BadServerResponse("Invalid PDU in send_join response.")
})?;
db.rooms.add_pdu_outlier(&event_id, &value)?;
if let Some(state_key) = &pdu.state_key {
let shortstatekey =
db.rooms
.get_or_create_shortstatekey(&pdu.kind, state_key, &db.globals)?;
state.insert(shortstatekey, pdu.event_id.clone());
}
}
let incoming_shortstatekey = db.rooms.get_or_create_shortstatekey(
&pdu.kind,
pdu.state_key
.as_ref()
.expect("Pdu is a membership state event"),
&db.globals,
)?;
state.insert(incoming_shortstatekey, pdu.event_id.clone());
let create_shortstatekey = db let create_shortstatekey = db
.rooms .rooms
.get_shortstatekey(&EventType::RoomCreate, "")? .get_shortstatekey(&EventType::RoomCreate, "")?
.expect("Room exists"); .expect("Room exists");
let mut saw_create_event = false; if state.get(&create_shortstatekey).is_none() {
warn!("Parsing send join response state");
const CHUNK_SIZE: usize = 500;
let mut parsed_state = room_state_state
.par_chunks_mut(CHUNK_SIZE)
.filter_map(|pdus| {
let mut r = HashMap::with_capacity(CHUNK_SIZE);
for pdu in pdus {
let (id, value) = get_event_id(&pdu, &room_version).ok()?;
r.insert(id, value);
}
let mut missing_servers = missing_servers.write().unwrap();
let mut pub_key_map = pub_key_map.write().unwrap();
for (_, value) in &r {
server_server::get_server_keys_from_cache(
&value,
&mut missing_servers,
&mut pub_key_map,
&db,
)
.ok()?;
}
Some(r)
})
.collect::<Vec<_>>();
warn!("Parsing send join response auth chain");
let mut parsed_chain = room_state_auth_chain
.par_chunks_mut(CHUNK_SIZE)
.filter_map(|pdus| {
let mut r = HashMap::with_capacity(CHUNK_SIZE);
for pdu in pdus {
let (id, value) = get_event_id(&pdu, &room_version).ok()?;
r.insert(id, value);
}
let mut missing_servers = missing_servers.write().unwrap();
let mut pub_key_map = pub_key_map.write().unwrap();
for (_, value) in &r {
server_server::get_server_keys_from_cache(
&value,
&mut missing_servers,
&mut pub_key_map,
&db,
)
.ok()?;
}
Some(r)
})
.collect::<Vec<_>>();
warn!("Fetching send join signing keys");
server_server::fetch_join_signing_keys(missing_servers, &pub_key_map, db).await?;
warn!("Validating state");
parsed_state.par_iter_mut().for_each(|chunk| {
let mut bad_events = Vec::new();
for (event_id, value) in chunk.iter_mut() {
if let Err(e) = ruma::signatures::verify_event(
&*pub_key_map.read().unwrap(),
&value,
&room_version,
) {
warn!("Event {} failed verification {:?} {}", event_id, value, e);
bad_events.push(event_id.clone());
continue;
}
value.insert(
"event_id".to_owned(),
CanonicalJsonValue::String(event_id.as_str().to_owned()),
);
}
for id in bad_events {
chunk.remove(&id);
}
});
warn!("Inserting state");
db.rooms
.add_pdu_outlier_batch(&mut parsed_state.iter().flatten())?;
warn!("Compressing state");
let state = parsed_state
.iter()
.flatten()
.map(|(event_id, value)| {
let kind = if let Some(s) = value.get("type").and_then(|s| s.as_str()) {
s
} else {
warn!("Event {} has no type: {:?}", event_id, value);
return Ok(None);
};
if let Some(state_key) = value.get("state_key").and_then(|s| s.as_str()) {
let shortstatekey = db.rooms.get_or_create_shortstatekey(
&EventType::from(kind),
state_key,
&db.globals,
)?;
if shortstatekey == create_shortstatekey {
saw_create_event = true;
}
Ok(Some(db.rooms.compress_state_event(
shortstatekey,
&event_id,
&db.globals,
)?))
} else {
Ok(None)
}
})
.filter_map(|r| r.transpose())
.collect::<Result<HashSet<_>>>()?;
if !saw_create_event {
return Err(Error::BadServerResponse("State contained no create event.")); return Err(Error::BadServerResponse("State contained no create event."));
} }
warn!("Validating chain"); db.rooms.force_state(
parsed_chain.par_iter_mut().for_each(|chunk| {
let mut bad_events = Vec::new();
for (event_id, value) in chunk.iter_mut() {
if let Err(e) = ruma::signatures::verify_event(
&*pub_key_map.read().unwrap(),
&value,
&room_version,
) {
warn!("Event {} failed verification {:?} {}", event_id, value, e);
bad_events.push(event_id.clone());
continue;
}
value.insert(
"event_id".to_owned(),
CanonicalJsonValue::String(event_id.as_str().to_owned()),
);
}
for id in bad_events {
chunk.remove(&id);
}
});
warn!("Inserting chain");
db.rooms
.add_pdu_outlier_batch(&mut parsed_chain.iter().flatten())?;
warn!("Forcing state of room");
db.rooms.force_state_new(
room_id, room_id,
state, state
&mut parsed_state.iter().flat_map(|m| m.values()), .into_iter()
.map(|(k, id)| db.rooms.compress_state_event(k, &id, &db.globals))
.collect::<Result<_>>()?,
db, db,
)?; )?;
for result in send_join_response
.room_state
.auth_chain
.iter()
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, db))
{
let (event_id, value) = match result {
Ok(t) => t,
Err(_) => continue,
};
db.rooms.add_pdu_outlier(&event_id, &value)?;
}
// We append to state before appending the pdu, so we don't have a moment in time with the // We append to state before appending the pdu, so we don't have a moment in time with the
// pdu without it's state. This is okay because append_pdu can't fail. // pdu without it's state. This is okay because append_pdu can't fail.
warn!("Appending join event to state");
let statehashid = db.rooms.append_to_state(&pdu, &db.globals)?; let statehashid = db.rooms.append_to_state(&pdu, &db.globals)?;
warn!("Adding join event to db");
db.rooms.append_pdu( db.rooms.append_pdu(
&pdu, &pdu,
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"), utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
@@ -861,13 +743,12 @@ async fn join_room_by_id_helper(
db, db,
)?; )?;
warn!("Updating room state to join event");
// We set the room state after inserting the pdu, so that we never have a moment in time // We set the room state after inserting the pdu, so that we never have a moment in time
// where events in the current room state do not exist // where events in the current room state do not exist
db.rooms.set_room_state(room_id, statehashid)?; db.rooms.set_room_state(room_id, statehashid)?;
} else { } else {
let event = member::MemberEventContent { let event = RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@@ -879,10 +760,7 @@ async fn join_room_by_id_helper(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
@@ -901,15 +779,16 @@ async fn join_room_by_id_helper(
Ok(join_room_by_id::Response::new(room_id.clone()).into()) Ok(join_room_by_id::Response::new(room_id.clone()).into())
} }
fn get_event_id( fn validate_and_add_event_id(
pdu: &Raw<Pdu>, pdu: &RawJsonValue,
room_version: &RoomVersionId, room_version: &RoomVersionId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
db: &Database,
) -> Result<(EventId, CanonicalJsonObject)> { ) -> Result<(EventId, CanonicalJsonObject)> {
let value = serde_json::from_str::<CanonicalJsonObject>(pdu.json().get()).map_err(|e| { let mut value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
warn!("Invalid PDU in server response: {:?}: {:?}", pdu, e); error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
let event_id = EventId::try_from(&*format!( let event_id = EventId::try_from(&*format!(
"${}", "${}",
ruma::signatures::reference_hash(&value, room_version) ruma::signatures::reference_hash(&value, room_version)
@@ -917,6 +796,49 @@ fn get_event_id(
)) ))
.expect("ruma's reference hashes are valid event ids"); .expect("ruma's reference hashes are valid event ids");
let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) {
Entry::Vacant(e) => {
e.insert((Instant::now(), 1));
}
Entry::Occupied(mut e) => *e.get_mut() = (Instant::now(), e.get().1 + 1),
};
if let Some((time, tries)) = db
.globals
.bad_event_ratelimiter
.read()
.unwrap()
.get(&event_id)
{
// Exponential backoff
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
}
if time.elapsed() < min_elapsed_duration {
debug!("Backing off from {}", event_id);
return Err(Error::BadServerResponse("bad event, still backing off"));
}
}
if let Err(e) = ruma::signatures::verify_event(
&*pub_key_map
.read()
.map_err(|_| Error::bad_database("RwLock is poisoned."))?,
&value,
room_version,
) {
warn!("Event {} failed verification {:?} {}", event_id, pdu, e);
back_off(event_id);
return Err(Error::BadServerResponse("Event failed verification."));
}
value.insert(
"event_id".to_owned(),
CanonicalJsonValue::String(event_id.as_str().to_owned()),
);
Ok((event_id, value)) Ok((event_id, value))
} }
@@ -939,24 +861,21 @@ pub(crate) async fn invite_helper<'a>(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let prev_events = db let prev_events: Vec<_> = db
.rooms .rooms
.get_pdu_leaves(room_id)? .get_pdu_leaves(room_id)?
.into_iter() .into_iter()
.take(20) .take(20)
.collect::<Vec<_>>(); .collect();
let create_event = db let create_event = db
.rooms .rooms
.room_state_get(room_id, &EventType::RoomCreate, "")?; .room_state_get(room_id, &EventType::RoomCreate, "")?;
let create_event_content = create_event let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_str::<Raw<CreateEventContent>>(create_event.content.get()) serde_json::from_str(create_event.content.get()).map_err(|e| {
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
}) })
@@ -979,8 +898,7 @@ pub(crate) async fn invite_helper<'a>(
let room_version = let room_version =
RoomVersion::new(&room_version_id).expect("room version is supported"); RoomVersion::new(&room_version_id).expect("room version is supported");
let content = RawValue::from_string( let content = to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&MemberEventContent {
avatar_url: None, avatar_url: None,
displayname: None, displayname: None,
is_direct: Some(is_direct), is_direct: Some(is_direct),
@@ -989,9 +907,7 @@ pub(crate) async fn invite_helper<'a>(
blurhash: None, blurhash: None,
reason: None, reason: None,
}) })
.expect("member event is valid value"), .expect("member event is valid value");
)
.expect("string is valid");
let state_key = user_id.to_string(); let state_key = user_id.to_string();
let kind = EventType::RoomMember; let kind = EventType::RoomMember;
@@ -1031,7 +947,6 @@ pub(crate) async fn invite_helper<'a>(
.expect("time is valid"), .expect("time is valid"),
kind, kind,
content, content,
parsed_content: RwLock::new(None),
state_key: Some(state_key), state_key: Some(state_key),
prev_events, prev_events,
depth, depth,
@@ -1043,14 +958,9 @@ pub(crate) async fn invite_helper<'a>(
unsigned: if unsigned.is_empty() { unsigned: if unsigned.is_empty() {
None None
} else { } else {
Some( Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
RawValue::from_string(
serde_json::to_string(&unsigned).expect("to_string always works"),
)
.expect("string is valid"),
)
}, },
hashes: ruma::events::pdu::EventHash { hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: None, signatures: None,
@@ -1117,11 +1027,11 @@ pub(crate) async fn invite_helper<'a>(
&db.globals, &db.globals,
user_id.server_name(), user_id.server_name(),
create_invite::v2::Request { create_invite::v2::Request {
room_id: room_id.clone(), room_id,
event_id: expected_event_id.clone(), event_id: &expected_event_id,
room_version: room_version_id, room_version: &room_version_id,
event: PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()), event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
invite_room_state, invite_room_state: &invite_room_state,
}, },
) )
.await?; .await?;
@@ -1144,7 +1054,7 @@ pub(crate) async fn invite_helper<'a>(
warn!("Server {} changed invite event, that's not allowed in the spec: ours: {:?}, theirs: {:?}", user_id.server_name(), pdu_json, value); warn!("Server {} changed invite event, that's not allowed in the spec: ours: {:?}, theirs: {:?}", user_id.server_name(), pdu_json, value);
} }
let origin = serde_json::from_value::<Box<ServerName>>( let origin: Box<ServerName> = serde_json::from_value(
serde_json::to_value(value.get("origin").ok_or(Error::BadRequest( serde_json::to_value(value.get("origin").ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Event needs an origin field.", "Event needs an origin field.",
@@ -1198,9 +1108,8 @@ pub(crate) async fn invite_helper<'a>(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Invite,
membership: member::MembershipState::Invite,
displayname: db.users.displayname(user_id)?, displayname: db.users.displayname(user_id)?,
avatar_url: db.users.avatar_url(user_id)?, avatar_url: db.users.avatar_url(user_id)?,
is_direct: Some(is_direct), is_direct: Some(is_direct),
@@ -1209,8 +1118,6 @@ pub(crate) async fn invite_helper<'a>(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
+17 -12
View File
@@ -45,6 +45,14 @@ pub async fn send_message_event_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// Forbid m.room.encrypted if encryption is disabled
if &body.event_type == "m.room.encrypted" && !db.globals.allow_encryption() {
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Encryption has been disabled",
));
}
// Check if this is a new transaction id // Check if this is a new transaction id
if let Some(response) = if let Some(response) =
db.transaction_ids db.transaction_ids
@@ -132,14 +140,11 @@ pub async fn get_message_events_route(
let to = body.to.as_ref().map(|t| t.parse()); let to = body.to.as_ref().map(|t| t.parse());
// Use limit or else 10 // Use limit or else 10
let limit = body let limit = body.limit.try_into().map_or(10_usize, |l: u32| l as usize);
.limit
.try_into()
.map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?;
match body.dir { match body.dir {
get_message_events::Direction::Forward => { get_message_events::Direction::Forward => {
let events_after = db let events_after: Vec<_> = db
.rooms .rooms
.pdus_after(sender_user, &body.room_id, from)? .pdus_after(sender_user, &body.room_id, from)?
.take(limit) .take(limit)
@@ -151,14 +156,14 @@ pub async fn get_message_events_route(
.ok() .ok()
}) })
.take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to`
.collect::<Vec<_>>(); .collect();
let end_token = events_after.last().map(|(count, _)| count.to_string()); let end_token = events_after.last().map(|(count, _)| count.to_string());
let events_after = events_after let events_after: Vec<_> = events_after
.into_iter() .into_iter()
.map(|(_, pdu)| pdu.to_room_event()) .map(|(_, pdu)| pdu.to_room_event())
.collect::<Vec<_>>(); .collect();
let mut resp = get_message_events::Response::new(); let mut resp = get_message_events::Response::new();
resp.start = Some(body.from.to_owned()); resp.start = Some(body.from.to_owned());
@@ -169,7 +174,7 @@ pub async fn get_message_events_route(
Ok(resp.into()) Ok(resp.into())
} }
get_message_events::Direction::Backward => { get_message_events::Direction::Backward => {
let events_before = db let events_before: Vec<_> = db
.rooms .rooms
.pdus_until(sender_user, &body.room_id, from)? .pdus_until(sender_user, &body.room_id, from)?
.take(limit) .take(limit)
@@ -181,14 +186,14 @@ pub async fn get_message_events_route(
.ok() .ok()
}) })
.take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to`
.collect::<Vec<_>>(); .collect();
let start_token = events_before.last().map(|(count, _)| count.to_string()); let start_token = events_before.last().map(|(count, _)| count.to_string());
let events_before = events_before let events_before: Vec<_> = events_before
.into_iter() .into_iter()
.map(|(_, pdu)| pdu.to_room_event()) .map(|(_, pdu)| pdu.to_room_event())
.collect::<Vec<_>>(); .collect();
let mut resp = get_message_events::Response::new(); let mut resp = get_message_events::Response::new();
resp.start = Some(body.from.to_owned()); resp.start = Some(body.from.to_owned());
+4 -10
View File
@@ -9,9 +9,9 @@ use ruma::{
}, },
federation::{self, query::get_profile_information::v1::ProfileField}, federation::{self, query::get_profile_information::v1::ProfileField},
}, },
events::EventType, events::{room::member::RoomMemberEventContent, EventType},
}; };
use serde_json::value::RawValue; use serde_json::value::to_raw_value;
use std::{convert::TryInto, sync::Arc}; use std::{convert::TryInto, sync::Arc};
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
@@ -45,8 +45,7 @@ pub async fn set_displayname_route(
Ok::<_, Error>(( Ok::<_, Error>((
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&ruma::events::room::member::MemberEventContent {
displayname: body.displayname.clone(), displayname: body.displayname.clone(),
..serde_json::from_str( ..serde_json::from_str(
db.rooms db.rooms
@@ -67,8 +66,6 @@ pub async fn set_displayname_route(
.map_err(|_| Error::bad_database("Database contains invalid PDU."))? .map_err(|_| Error::bad_database("Database contains invalid PDU."))?
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
@@ -191,8 +188,7 @@ pub async fn set_avatar_url_route(
Ok::<_, Error>(( Ok::<_, Error>((
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&ruma::events::room::member::MemberEventContent {
avatar_url: body.avatar_url.clone(), avatar_url: body.avatar_url.clone(),
..serde_json::from_str( ..serde_json::from_str(
db.rooms db.rooms
@@ -213,8 +209,6 @@ pub async fn set_avatar_url_route(
.map_err(|_| Error::bad_database("Database contains invalid PDU."))? .map_err(|_| Error::bad_database("Database contains invalid PDU."))?
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
+17 -17
View File
@@ -8,7 +8,7 @@ use ruma::{
set_pushrule_enabled, RuleKind, set_pushrule_enabled, RuleKind,
}, },
}, },
events::{push_rules, EventType}, events::{push_rules::PushRulesEvent, EventType},
push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit}, push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit},
}; };
@@ -29,9 +29,9 @@ pub async fn get_pushrules_all_route(
) -> ConduitResult<get_pushrules_all::Response> { ) -> ConduitResult<get_pushrules_all::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event = db let event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -57,9 +57,9 @@ pub async fn get_pushrule_route(
) -> ConduitResult<get_pushrule::Response> { ) -> ConduitResult<get_pushrule::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event = db let event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -122,9 +122,9 @@ pub async fn set_pushrule_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -222,9 +222,9 @@ pub async fn get_pushrule_actions_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -284,9 +284,9 @@ pub async fn set_pushrule_actions_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -356,9 +356,9 @@ pub async fn get_pushrule_enabled_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -420,9 +420,9 @@ pub async fn set_pushrule_enabled_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<ruma::events::push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
@@ -497,9 +497,9 @@ pub async fn delete_pushrule_route(
)); ));
} }
let mut event = db let mut event: PushRulesEvent = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)? .get(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"PushRules event not found.", "PushRules event not found.",
+3 -6
View File
@@ -3,12 +3,12 @@ use std::sync::Arc;
use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma}; use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma};
use ruma::{ use ruma::{
api::client::r0::redact::redact_event, api::client::r0::redact::redact_event,
events::{room::redaction, EventType}, events::{room::redaction::RoomRedactionEventContent, EventType},
}; };
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::put; use rocket::put;
use serde_json::value::RawValue; use serde_json::value::to_raw_value;
/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}` /// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
/// ///
@@ -39,13 +39,10 @@ pub async fn redact_event_route(
let event_id = db.rooms.build_and_append_pdu( let event_id = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomRedaction, event_type: EventType::RoomRedaction,
content: RawValue::from_string( content: to_raw_value(&RoomRedactionEventContent {
serde_json::to_string(&redaction::RedactionEventContent {
reason: body.reason.clone(), reason: body.reason.clone(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: None, state_key: None,
redacts: Some(body.event_id.clone()), redacts: Some(body.event_id.clone()),
+54 -75
View File
@@ -8,12 +8,24 @@ use ruma::{
r0::room::{self, aliases, create_room, get_room_event, upgrade_room}, r0::room::{self, aliases, create_room, get_room_event, upgrade_room},
}, },
events::{ events::{
room::{guest_access, history_visibility, join_rules, member, name, topic}, room::{
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
tombstone::RoomTombstoneEventContent,
topic::RoomTopicEventContent,
},
EventType, EventType,
}, },
serde::JsonObject,
RoomAliasId, RoomId, RoomVersionId, RoomAliasId, RoomId, RoomVersionId,
}; };
use serde_json::value::RawValue; use serde_json::value::to_raw_value;
use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc}; use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc};
use tracing::{info, warn}; use tracing::{info, warn};
@@ -61,6 +73,16 @@ pub async fn create_room_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
if !db.globals.allow_room_creation()
&& !body.from_appservice
&& !db.users.is_admin(sender_user, &db.rooms, &db.globals)?
{
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Room creation has been disabled.",
));
}
let alias: Option<RoomAliasId> = let alias: Option<RoomAliasId> =
body.room_alias_name body.room_alias_name
.as_ref() .as_ref()
@@ -80,7 +102,7 @@ pub async fn create_room_route(
} }
})?; })?;
let mut content = ruma::events::room::create::CreateEventContent::new(sender_user.clone()); let mut content = RoomCreateEventContent::new(sender_user.clone());
content.federate = body.creation_content.federate; content.federate = body.creation_content.federate;
content.predecessor = body.creation_content.predecessor.clone(); content.predecessor = body.creation_content.predecessor.clone();
content.room_version = match body.room_version.clone() { content.room_version = match body.room_version.clone() {
@@ -101,10 +123,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: RawValue::from_string( content: to_raw_value(&content).expect("event is valid, we just created it"),
serde_json::to_string(&content).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -119,9 +138,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Join,
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: Some(body.is_direct), is_direct: Some(body.is_direct),
@@ -130,8 +148,6 @@ pub async fn create_room_route(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
@@ -163,17 +179,14 @@ pub async fn create_room_route(
} }
} }
let mut power_levels_content = let mut power_levels_content = serde_json::to_value(RoomPowerLevelsEventContent {
serde_json::to_value(ruma::events::room::power_levels::PowerLevelsEventContent {
users, users,
..Default::default() ..Default::default()
}) })
.expect("event is valid, we just created it"); .expect("event is valid, we just created it");
if let Some(power_level_content_override) = &body.power_level_content_override { if let Some(power_level_content_override) = &body.power_level_content_override {
let json = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>( let json: JsonObject = serde_json::from_str(power_level_content_override.json().get())
power_level_content_override.json().get(),
)
.map_err(|_| { .map_err(|_| {
Error::BadRequest(ErrorKind::BadJson, "Invalid power_level_content_override.") Error::BadRequest(ErrorKind::BadJson, "Invalid power_level_content_override.")
})?; })?;
@@ -186,8 +199,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: RawValue::from_string(power_levels_content.to_string()) content: to_raw_value(&power_levels_content)
.expect("string is valid"), .expect("to_raw_value always works on serde_json::Value"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -203,16 +216,11 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCanonicalAlias, event_type: EventType::RoomCanonicalAlias,
content: RawValue::from_string( content: to_raw_value(&RoomCanonicalAliasEventContent {
serde_json::to_string(
&ruma::events::room::canonical_alias::CanonicalAliasEventContent {
alias: Some(room_alias_id.clone()), alias: Some(room_alias_id.clone()),
alt_aliases: vec![], alt_aliases: vec![],
}, })
)
.expect("We checked that alias earlier, it must be fine"), .expect("We checked that alias earlier, it must be fine"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -230,15 +238,12 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomJoinRules, event_type: EventType::RoomJoinRules,
content: RawValue::from_string( content: to_raw_value(&RoomJoinRulesEventContent::new(match preset {
serde_json::to_string(&join_rules::JoinRulesEventContent::new(match preset { create_room::RoomPreset::PublicChat => JoinRule::Public,
create_room::RoomPreset::PublicChat => join_rules::JoinRule::Public,
// according to spec "invite" is the default // according to spec "invite" is the default
_ => join_rules::JoinRule::Invite, _ => JoinRule::Invite,
})) }))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -253,13 +258,10 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomHistoryVisibility, event_type: EventType::RoomHistoryVisibility,
content: RawValue::from_string( content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
serde_json::to_string(&history_visibility::HistoryVisibilityEventContent::new( HistoryVisibility::Shared,
history_visibility::HistoryVisibility::Shared,
)) ))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -274,14 +276,11 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomGuestAccess, event_type: EventType::RoomGuestAccess,
content: RawValue::from_string( content: to_raw_value(&RoomGuestAccessEventContent::new(match preset {
serde_json::to_string(&guest_access::GuestAccessEventContent::new(match preset { create_room::RoomPreset::PublicChat => GuestAccess::Forbidden,
create_room::RoomPreset::PublicChat => guest_access::GuestAccess::Forbidden, _ => GuestAccess::CanJoin,
_ => guest_access::GuestAccess::CanJoin,
})) }))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -313,11 +312,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomName, event_type: EventType::RoomName,
content: RawValue::from_string( content: to_raw_value(&RoomNameEventContent::new(Some(name.clone())))
serde_json::to_string(&name::NameEventContent::new(Some(name.clone())))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -333,13 +329,10 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTopic, event_type: EventType::RoomTopic,
content: RawValue::from_string( content: to_raw_value(&RoomTopicEventContent {
serde_json::to_string(&topic::TopicEventContent {
topic: topic.clone(), topic: topic.clone(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -490,14 +483,11 @@ pub async fn upgrade_room_route(
let tombstone_event_id = db.rooms.build_and_append_pdu( let tombstone_event_id = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTombstone, event_type: EventType::RoomTombstone,
content: RawValue::from_string( content: to_raw_value(&RoomTombstoneEventContent {
serde_json::to_string(&ruma::events::room::tombstone::TombstoneEventContent { body: "This room has been replaced".to_owned(),
body: "This room has been replaced".to_string(),
replacement_room: replacement_room.clone(), replacement_room: replacement_room.clone(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -521,7 +511,7 @@ pub async fn upgrade_room_route(
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// Get the old room federations status // Get the old room federations status
let federate = serde_json::from_str::<ruma::events::room::create::CreateEventContent>( let federate = serde_json::from_str::<RoomCreateEventContent>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomCreate, "")? .room_state_get(&body.room_id, &EventType::RoomCreate, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
@@ -538,8 +528,7 @@ pub async fn upgrade_room_route(
)); ));
// Send a m.room.create event containing a predecessor field and the applicable room_version // Send a m.room.create event containing a predecessor field and the applicable room_version
let mut create_event_content = let mut create_event_content = RoomCreateEventContent::new(sender_user.clone());
ruma::events::room::create::CreateEventContent::new(sender_user.clone());
create_event_content.federate = federate; create_event_content.federate = federate;
create_event_content.room_version = body.new_version.clone(); create_event_content.room_version = body.new_version.clone();
create_event_content.predecessor = predecessor; create_event_content.predecessor = predecessor;
@@ -547,11 +536,8 @@ pub async fn upgrade_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: RawValue::from_string( content: to_raw_value(&create_event_content)
serde_json::to_string(&create_event_content)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@@ -566,9 +552,8 @@ pub async fn upgrade_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&member::MemberEventContent { membership: MembershipState::Join,
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@@ -577,8 +562,6 @@ pub async fn upgrade_room_route(
reason: None, reason: None,
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
@@ -605,7 +588,7 @@ pub async fn upgrade_room_route(
// Replicate transferable state events to the new room // Replicate transferable state events to the new room
for event_type in transferable_state_events { for event_type in transferable_state_events {
let event_content = match db.rooms.room_state_get(&body.room_id, &event_type, "")? { let event_content = match db.rooms.room_state_get(&body.room_id, &event_type, "")? {
Some(v) => v.content, Some(v) => v.content.clone(),
None => continue, // Skipping missing events. None => continue, // Skipping missing events.
}; };
@@ -631,8 +614,7 @@ pub async fn upgrade_room_route(
} }
// Get the old room power levels // Get the old room power levels
let mut power_levels_event_content = let mut power_levels_event_content: RoomPowerLevelsEventContent = serde_json::from_str(
serde_json::from_str::<ruma::events::room::power_levels::PowerLevelsEventContent>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomPowerLevels, "")? .room_state_get(&body.room_id, &EventType::RoomPowerLevels, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
@@ -653,11 +635,8 @@ pub async fn upgrade_room_route(
let _ = db.rooms.build_and_append_pdu( let _ = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: RawValue::from_string( content: to_raw_value(&power_levels_event_content)
serde_json::to_string(&power_levels_event_content)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
+3 -3
View File
@@ -74,7 +74,7 @@ pub async fn search_events_route(
} }
} }
let results = results let results: Vec<_> = results
.iter() .iter()
.map(|result| { .map(|result| {
Ok::<_, Error>(SearchResult { Ok::<_, Error>(SearchResult {
@@ -95,7 +95,7 @@ pub async fn search_events_route(
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.skip(skip) .skip(skip)
.take(limit) .take(limit)
.collect::<Vec<_>>(); .collect();
let next_batch = if results.len() < limit as usize { let next_batch = if results.len() < limit as usize {
None None
@@ -114,7 +114,7 @@ pub async fn search_events_route(
.search_term .search_term
.split_terminator(|c: char| !c.is_alphanumeric()) .split_terminator(|c: char| !c.is_alphanumeric())
.map(str::to_lowercase) .map(str::to_lowercase)
.collect::<Vec<_>>(), .collect(),
}, },
}) })
.into()) .into())
+9 -3
View File
@@ -60,10 +60,10 @@ pub async fn login_route(
// Validate login method // Validate login method
// TODO: Other login methods // TODO: Other login methods
let user_id = match &body.login_info { let user_id = match &body.login_info {
login::IncomingLoginInfo::Password { login::IncomingLoginInfo::Password(login::IncomingPassword {
identifier, identifier,
password, password,
} => { }) => {
let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier { let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
matrix_id matrix_id
} else { } else {
@@ -97,7 +97,7 @@ pub async fn login_route(
user_id user_id
} }
login::IncomingLoginInfo::Token { token } => { login::IncomingLoginInfo::Token(login::IncomingToken { token }) => {
if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() { if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() {
let token = jsonwebtoken::decode::<Claims>( let token = jsonwebtoken::decode::<Claims>(
token, token,
@@ -116,6 +116,12 @@ pub async fn login_route(
)); ));
} }
} }
_ => {
return Err(Error::BadRequest(
ErrorKind::Unknown,
"Unsupported login type.",
));
}
}; };
// Generate new device id if the user didn't specify one // Generate new device id if the user didn't specify one
+17 -9
View File
@@ -10,8 +10,8 @@ use ruma::{
}, },
events::{ events::{
room::{ room::{
canonical_alias::CanonicalAliasEventContent, canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, HistoryVisibilityEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
}, },
AnyStateEventContent, EventType, AnyStateEventContent, EventType,
}, },
@@ -73,6 +73,14 @@ pub async fn send_state_event_for_empty_key_route(
) -> ConduitResult<send_state_event::Response> { ) -> ConduitResult<send_state_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
// Forbid m.room.encryption if encryption is disabled
if &body.event_type == "m.room.encryption" && !db.globals.allow_encryption() {
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Encryption has been disabled",
));
}
let event_id = send_state_event_for_key_helper( let event_id = send_state_event_for_key_helper(
&db, &db,
sender_user, sender_user,
@@ -112,13 +120,13 @@ pub async fn get_state_events_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_str::<HistoryVisibilityEventContent>(event.content.get()) serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
) )
}) })
.map(|e| e.history_visibility)
}), }),
Some(Ok(HistoryVisibility::WorldReadable)) Some(Ok(HistoryVisibility::WorldReadable))
) )
@@ -164,13 +172,13 @@ pub async fn get_state_events_for_key_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_str::<HistoryVisibilityEventContent>(event.content.get()) serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
) )
}) })
.map(|e| e.history_visibility)
}), }),
Some(Ok(HistoryVisibility::WorldReadable)) Some(Ok(HistoryVisibility::WorldReadable))
) )
@@ -220,13 +228,13 @@ pub async fn get_state_events_for_empty_key_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_str::<HistoryVisibilityEventContent>(event.content.get()) serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
) )
}) })
.map(|e| e.history_visibility)
}), }),
Some(Ok(HistoryVisibility::WorldReadable)) Some(Ok(HistoryVisibility::WorldReadable))
) )
@@ -265,7 +273,7 @@ async fn send_state_event_for_key_helper(
// TODO: Review this check, error if event is unparsable, use event type, allow alias if it // TODO: Review this check, error if event is unparsable, use event type, allow alias if it
// previously existed // previously existed
if let Ok(canonical_alias) = if let Ok(canonical_alias) =
serde_json::from_str::<CanonicalAliasEventContent>(json.json().get()) serde_json::from_str::<RoomCanonicalAliasEventContent>(json.json().get())
{ {
let mut aliases = canonical_alias.alt_aliases.clone(); let mut aliases = canonical_alias.alt_aliases.clone();
+29 -35
View File
@@ -1,7 +1,10 @@
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse}; use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
use ruma::{ use ruma::{
api::client::r0::{sync::sync_events, uiaa::UiaaResponse}, api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType}, events::{
room::member::{MembershipState, RoomMemberEventContent},
AnySyncEphemeralRoomEvent, EventType,
},
serde::Raw, serde::Raw,
DeviceId, RoomId, UserId, DeviceId, RoomId, UserId,
}; };
@@ -57,7 +60,7 @@ use rocket::{get, tokio};
pub async fn sync_events_route( pub async fn sync_events_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<sync_events::Request<'_>>, body: Ruma<sync_events::Request<'_>>,
) -> std::result::Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> { ) -> Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");
@@ -179,7 +182,7 @@ async fn sync_helper(
full_state: bool, full_state: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
// bool = caching allowed // bool = caching allowed
) -> std::result::Result<(sync_events::Response, bool), Error> { ) -> Result<(sync_events::Response, bool), Error> {
// TODO: match body.set_presence { // TODO: match body.set_presence {
db.rooms.edus.ping_presence(&sender_user)?; db.rooms.edus.ping_presence(&sender_user)?;
@@ -241,13 +244,13 @@ async fn sync_helper(
}); });
// Take the last 10 events for the timeline // Take the last 10 events for the timeline
let timeline_pdus = non_timeline_pdus let timeline_pdus: Vec<_> = non_timeline_pdus
.by_ref() .by_ref()
.take(10) .take(10)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into_iter() .into_iter()
.rev() .rev()
.collect::<Vec<_>>(); .collect();
let send_notification_counts = !timeline_pdus.is_empty() let send_notification_counts = !timeline_pdus.is_empty()
|| db || db
@@ -287,10 +290,10 @@ async fn sync_helper(
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus .filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember) .filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
.map(|(_, pdu)| { .map(|(_, pdu)| {
let content = serde_json::from_str::< let content: RoomMemberEventContent =
ruma::events::room::member::MemberEventContent, serde_json::from_str(pdu.content.get()).map_err(|_| {
>(pdu.content.get()) Error::bad_database("Invalid member event in database.")
.map_err(|_| Error::bad_database("Invalid member event in database."))?; })?;
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
let user_id = UserId::try_from(state_key.clone()).map_err(|_| { let user_id = UserId::try_from(state_key.clone()).map_err(|_| {
@@ -343,11 +346,11 @@ async fn sync_helper(
let (joined_member_count, invited_member_count, heroes) = calculate_counts()?; let (joined_member_count, invited_member_count, heroes) = calculate_counts()?;
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?; let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
let state_events = current_state_ids let state_events: Vec<_> = current_state_ids
.iter() .iter()
.map(|(_, id)| db.rooms.get_pdu(id)) .map(|(_, id)| db.rooms.get_pdu(id))
.filter_map(|r| r.ok().flatten()) .filter_map(|r| r.ok().flatten())
.collect::<Vec<_>>(); .collect();
( (
heroes, heroes,
@@ -363,7 +366,7 @@ async fn sync_helper(
// Incremental /sync // Incremental /sync
let since_shortstatehash = since_shortstatehash.unwrap(); let since_shortstatehash = since_shortstatehash.unwrap();
let since_sender_member = db let since_sender_member: Option<RoomMemberEventContent> = db
.rooms .rooms
.state_get( .state_get(
since_shortstatehash, since_shortstatehash,
@@ -371,9 +374,7 @@ async fn sync_helper(
sender_user.as_str(), sender_user.as_str(),
)? )?
.and_then(|pdu| { .and_then(|pdu| {
serde_json::from_str::<ruma::events::room::member::MemberEventContent>( serde_json::from_str(pdu.content.get())
pdu.content.get(),
)
.map_err(|_| Error::bad_database("Invalid PDU in database.")) .map_err(|_| Error::bad_database("Invalid PDU in database."))
.ok() .ok()
}); });
@@ -430,9 +431,9 @@ async fn sync_helper(
continue; continue;
} }
let new_membership = serde_json::from_str::< let new_membership = serde_json::from_str::<RoomMemberEventContent>(
ruma::events::room::member::MemberEventContent, state_event.content.get(),
>(state_event.content.get()) )
.map_err(|_| Error::bad_database("Invalid PDU in database."))? .map_err(|_| Error::bad_database("Invalid PDU in database."))?
.membership; .membership;
@@ -521,18 +522,18 @@ async fn sync_helper(
Ok(Some(db.rooms.pdu_count(pdu_id)?.to_string())) Ok(Some(db.rooms.pdu_count(pdu_id)?.to_string()))
})?; })?;
let room_events = timeline_pdus let room_events: Vec<_> = timeline_pdus
.iter() .iter()
.map(|(_, pdu)| pdu.to_sync_room_event()) .map(|(_, pdu)| pdu.to_sync_room_event())
.collect::<Vec<_>>(); .collect();
let mut edus = db let mut edus: Vec<_> = db
.rooms .rooms
.edus .edus
.readreceipts_since(&room_id, since) .readreceipts_since(&room_id, since)
.filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| r.ok()) // Filter out buggy events
.map(|(_, _, v)| v) .map(|(_, _, v)| v)
.collect::<Vec<_>>(); .collect();
if db.rooms.edus.last_typing_update(&room_id, &db.globals)? > since { if db.rooms.edus.last_typing_update(&room_id, &db.globals)? > since {
edus.push( edus.push(
@@ -561,7 +562,7 @@ async fn sync_helper(
.map_err(|_| Error::bad_database("Invalid account event in database.")) .map_err(|_| Error::bad_database("Invalid account event in database."))
.ok() .ok()
}) })
.collect::<Vec<_>>(), .collect(),
}, },
summary: sync_events::RoomSummary { summary: sync_events::RoomSummary {
heroes, heroes,
@@ -626,7 +627,7 @@ async fn sync_helper(
} }
let mut left_rooms = BTreeMap::new(); let mut left_rooms = BTreeMap::new();
let all_left_rooms = db.rooms.rooms_left(&sender_user).collect::<Vec<_>>(); let all_left_rooms: Vec<_> = db.rooms.rooms_left(&sender_user).collect();
for result in all_left_rooms { for result in all_left_rooms {
let (room_id, left_state_events) = result?; let (room_id, left_state_events) = result?;
@@ -666,7 +667,7 @@ async fn sync_helper(
} }
let mut invited_rooms = BTreeMap::new(); let mut invited_rooms = BTreeMap::new();
let all_invited_rooms = db.rooms.rooms_invited(&sender_user).collect::<Vec<_>>(); let all_invited_rooms: Vec<_> = db.rooms.rooms_invited(&sender_user).collect();
for result in all_invited_rooms { for result in all_invited_rooms {
let (room_id, invite_state_events) = result?; let (room_id, invite_state_events) = result?;
@@ -735,7 +736,7 @@ async fn sync_helper(
presence: sync_events::Presence { presence: sync_events::Presence {
events: presence_updates events: presence_updates
.into_iter() .into_iter()
.map(|(_, v)| Raw::from(v)) .map(|(_, v)| Raw::new(&v).expect("PresenceEvent always serializes successfully"))
.collect(), .collect(),
}, },
account_data: sync_events::GlobalAccountData { account_data: sync_events::GlobalAccountData {
@@ -748,19 +749,13 @@ async fn sync_helper(
.map_err(|_| Error::bad_database("Invalid account event in database.")) .map_err(|_| Error::bad_database("Invalid account event in database."))
.ok() .ok()
}) })
.collect::<Vec<_>>(), .collect(),
}, },
device_lists: sync_events::DeviceLists { device_lists: sync_events::DeviceLists {
changed: device_list_updates.into_iter().collect(), changed: device_list_updates.into_iter().collect(),
left: device_list_left.into_iter().collect(), left: device_list_left.into_iter().collect(),
}, },
device_one_time_keys_count: if db.users.last_one_time_keys_update(&sender_user)? > since device_one_time_keys_count: db.users.count_one_time_keys(&sender_user, &sender_device)?,
|| since == 0
{
db.users.count_one_time_keys(&sender_user, &sender_device)?
} else {
BTreeMap::new()
},
to_device: sync_events::ToDevice { to_device: sync_events::ToDevice {
events: db events: db
.users .users
@@ -774,7 +769,6 @@ async fn sync_helper(
&& response.presence.is_empty() && response.presence.is_empty()
&& response.account_data.is_empty() && response.account_data.is_empty()
&& response.device_lists.is_empty() && response.device_lists.is_empty()
&& response.device_one_time_keys_count.is_empty()
&& response.to_device.is_empty() && response.to_device.is_empty()
{ {
// Hang a few seconds so requests are not spammed // Hang a few seconds so requests are not spammed
+13 -10
View File
@@ -1,7 +1,10 @@
use crate::{database::DatabaseGuard, ConduitResult, Ruma}; use crate::{database::DatabaseGuard, ConduitResult, Ruma};
use ruma::{ use ruma::{
api::client::r0::tag::{create_tag, delete_tag, get_tags}, api::client::r0::tag::{create_tag, delete_tag, get_tags},
events::EventType, events::{
tag::{TagEvent, TagEventContent},
EventType,
},
}; };
use std::collections::BTreeMap; use std::collections::BTreeMap;
@@ -26,9 +29,9 @@ pub async fn update_tag_route(
let mut tags_event = db let mut tags_event = db
.account_data .account_data
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)? .get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent { .unwrap_or_else(|| TagEvent {
content: ruma::events::tag::TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}); });
@@ -68,9 +71,9 @@ pub async fn delete_tag_route(
let mut tags_event = db let mut tags_event = db
.account_data .account_data
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)? .get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent { .unwrap_or_else(|| TagEvent {
content: ruma::events::tag::TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}); });
@@ -108,9 +111,9 @@ pub async fn get_tags_route(
Ok(get_tags::Response { Ok(get_tags::Response {
tags: db tags: db
.account_data .account_data
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)? .get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent { .unwrap_or_else(|| TagEvent {
content: ruma::events::tag::TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}) })
+6 -4
View File
@@ -61,6 +61,8 @@ pub struct Config {
allow_encryption: bool, allow_encryption: bool,
#[serde(default = "false_fn")] #[serde(default = "false_fn")]
allow_federation: bool, allow_federation: bool,
#[serde(default = "true_fn")]
allow_room_creation: bool,
#[serde(default = "false_fn")] #[serde(default = "false_fn")]
pub allow_jaeger: bool, pub allow_jaeger: bool,
#[serde(default = "false_fn")] #[serde(default = "false_fn")]
@@ -499,13 +501,13 @@ impl Database {
if let Some(parent_stateinfo) = states_parents.last() { if let Some(parent_stateinfo) = states_parents.last() {
let statediffnew = current_state let statediffnew = current_state
.difference(&parent_stateinfo.1) .difference(&parent_stateinfo.1)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect::<HashSet<_>>();
let statediffremoved = parent_stateinfo let statediffremoved = parent_stateinfo
.1 .1
.difference(&current_state) .difference(&current_state)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect::<HashSet<_>>();
(statediffnew, statediffremoved) (statediffnew, statediffremoved)
@@ -699,7 +701,7 @@ impl Database {
println!("Deleting starts"); println!("Deleting starts");
let batch2 = db let batch2: Vec<_> = db
.rooms .rooms
.tokenids .tokenids
.iter() .iter()
@@ -711,7 +713,7 @@ impl Database {
None None
} }
}) })
.collect::<Vec<_>>(); .collect();
for key in batch2 { for key in batch2 {
println!("del"); println!("del");
+2 -2
View File
@@ -22,7 +22,7 @@ pub trait Tree: Send + Sync {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>; fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>;
fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>; fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>;
fn remove(&self, key: &[u8]) -> Result<()>; fn remove(&self, key: &[u8]) -> Result<()>;
@@ -35,7 +35,7 @@ pub trait Tree: Send + Sync {
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>; ) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
fn increment(&self, key: &[u8]) -> Result<Vec<u8>>; fn increment(&self, key: &[u8]) -> Result<Vec<u8>>;
fn increment_batch<'a>(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>; fn increment_batch(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>;
fn scan_prefix<'a>( fn scan_prefix<'a>(
&'a self, &'a self,
+7 -10
View File
@@ -6,17 +6,17 @@ use std::{
use crate::{pdu::PduBuilder, Database}; use crate::{pdu::PduBuilder, Database};
use rocket::futures::{channel::mpsc, stream::StreamExt}; use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{ use ruma::{
events::{room::message, EventType}, events::{room::message::RoomMessageEventContent, EventType},
UserId, UserId,
}; };
use serde_json::value::RawValue; use serde_json::value::to_raw_value;
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard}; use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
use tracing::warn; use tracing::warn;
pub enum AdminCommand { pub enum AdminCommand {
RegisterAppservice(serde_yaml::Value), RegisterAppservice(serde_yaml::Value),
ListAppservices, ListAppservices,
SendMessage(message::MessageEventContent), SendMessage(RoomMessageEventContent),
} }
#[derive(Clone)] #[derive(Clone)]
@@ -59,7 +59,7 @@ impl Admin {
drop(guard); drop(guard);
let send_message = |message: message::MessageEventContent, let send_message = |message: RoomMessageEventContent,
guard: RwLockReadGuard<'_, Database>, guard: RwLockReadGuard<'_, Database>,
mutex_lock: &MutexGuard<'_, ()>| { mutex_lock: &MutexGuard<'_, ()>| {
guard guard
@@ -67,11 +67,8 @@ impl Admin {
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMessage, event_type: EventType::RoomMessage,
content: RawValue::from_string( content: to_raw_value(&message)
serde_json::to_string(&message)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: None, state_key: None,
redacts: None, redacts: None,
@@ -110,9 +107,9 @@ impl Admin {
count, count,
appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ") appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ")
); );
send_message(message::MessageEventContent::text_plain(output), guard, &state_lock); send_message(RoomMessageEventContent::text_plain(output), guard, &state_lock);
} else { } else {
send_message(message::MessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock); send_message(RoomMessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
} }
} }
AdminCommand::SendMessage(message) => { AdminCommand::SendMessage(message) => {
+7 -4
View File
@@ -57,8 +57,7 @@ pub struct RotationHandler(broadcast::Sender<()>, broadcast::Receiver<()>);
impl RotationHandler { impl RotationHandler {
pub fn new() -> Self { pub fn new() -> Self {
let (s, r) = broadcast::channel::<()>(1); let (s, r) = broadcast::channel(1);
Self(s, r) Self(s, r)
} }
@@ -211,6 +210,10 @@ impl Globals {
self.config.allow_federation self.config.allow_federation
} }
pub fn allow_room_creation(&self) -> bool {
self.config.allow_room_creation
}
pub fn trusted_servers(&self) -> &[Box<ServerName>] { pub fn trusted_servers(&self) -> &[Box<ServerName>] {
&self.config.trusted_servers &self.config.trusted_servers
} }
@@ -274,8 +277,8 @@ impl Globals {
let signingkeys = self let signingkeys = self
.server_signingkeys .server_signingkeys
.get(origin.as_bytes())? .get(origin.as_bytes())?
.and_then(|bytes| serde_json::from_slice::<ServerSigningKeys>(&bytes).ok()) .and_then(|bytes| serde_json::from_slice(&bytes).ok())
.map(|keys| { .map(|keys: ServerSigningKeys| {
let mut tree = keys.verify_keys; let mut tree = keys.verify_keys;
tree.extend( tree.extend(
keys.old_verify_keys keys.old_verify_keys
+7 -6
View File
@@ -81,7 +81,7 @@ impl KeyBackups {
)?; )?;
self.backupid_etag self.backupid_etag
.insert(&key, &globals.next_count()?.to_be_bytes())?; .insert(&key, &globals.next_count()?.to_be_bytes())?;
Ok(version.to_string()) Ok(version.to_owned())
} }
pub fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>> { pub fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>> {
@@ -94,15 +94,15 @@ impl KeyBackups {
.iter_from(&last_possible_key, true) .iter_from(&last_possible_key, true)
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.next() .next()
.map_or(Ok(None), |(key, _)| { .map(|(key, _)| {
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
.expect("rsplit always returns an element"), .expect("rsplit always returns an element"),
) )
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid.")) .map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))
.map(Some)
}) })
.transpose()
} }
pub fn get_latest_backup(&self, user_id: &UserId) -> Result<Option<(String, BackupAlgorithm)>> { pub fn get_latest_backup(&self, user_id: &UserId) -> Result<Option<(String, BackupAlgorithm)>> {
@@ -115,7 +115,7 @@ impl KeyBackups {
.iter_from(&last_possible_key, true) .iter_from(&last_possible_key, true)
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.next() .next()
.map_or(Ok(None), |(key, value)| { .map(|(key, value)| {
let version = utils::string_from_bytes( let version = utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@@ -123,13 +123,14 @@ impl KeyBackups {
) )
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))?; .map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))?;
Ok(Some(( Ok((
version, version,
serde_json::from_slice(&value).map_err(|_| { serde_json::from_slice(&value).map_err(|_| {
Error::bad_database("Algorithm in backupid_algorithm is invalid.") Error::bad_database("Algorithm in backupid_algorithm is invalid.")
})?, })?,
))) ))
}) })
.transpose()
} }
pub fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<BackupAlgorithm>> { pub fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<BackupAlgorithm>> {
+3 -3
View File
@@ -125,7 +125,7 @@ impl WildCardedDomain {
} }
impl std::str::FromStr for WildCardedDomain { impl std::str::FromStr for WildCardedDomain {
type Err = std::convert::Infallible; type Err = std::convert::Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
// maybe do some domain validation? // maybe do some domain validation?
Ok(if s.starts_with("*.") { Ok(if s.starts_with("*.") {
WildCardedDomain::WildCarded(s[1..].to_owned()) WildCardedDomain::WildCarded(s[1..].to_owned())
@@ -136,8 +136,8 @@ impl std::str::FromStr for WildCardedDomain {
}) })
} }
} }
impl<'de> serde::de::Deserialize<'de> for WildCardedDomain { impl<'de> Deserialize<'de> for WildCardedDomain {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: serde::de::Deserializer<'de>, D: serde::de::Deserializer<'de>,
{ {
+4 -4
View File
@@ -10,7 +10,7 @@ use ruma::{
IncomingResponse, OutgoingRequest, SendAccessToken, IncomingResponse, OutgoingRequest, SendAccessToken,
}, },
events::{ events::{
room::{name::NameEventContent, power_levels::PowerLevelsEventContent}, room::{name::RoomNameEventContent, power_levels::RoomPowerLevelsEventContent},
AnySyncRoomEvent, EventType, AnySyncRoomEvent, EventType,
}, },
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak}, push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
@@ -179,7 +179,7 @@ pub async fn send_push_notice(
let mut notify = None; let mut notify = None;
let mut tweaks = Vec::new(); let mut tweaks = Vec::new();
let power_levels: PowerLevelsEventContent = db let power_levels: RoomPowerLevelsEventContent = db
.rooms .rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")? .room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| { .map(|ev| {
@@ -228,7 +228,7 @@ pub async fn send_push_notice(
pub fn get_actions<'a>( pub fn get_actions<'a>(
user: &UserId, user: &UserId,
ruleset: &'a Ruleset, ruleset: &'a Ruleset,
power_levels: &PowerLevelsEventContent, power_levels: &RoomPowerLevelsEventContent,
pdu: &Raw<AnySyncRoomEvent>, pdu: &Raw<AnySyncRoomEvent>,
room_id: &RoomId, room_id: &RoomId,
db: &Database, db: &Database,
@@ -325,7 +325,7 @@ async fn send_notice(
db.rooms db.rooms
.room_state_get(&event.room_id, &EventType::RoomName, "")? .room_state_get(&event.room_id, &EventType::RoomName, "")?
{ {
serde_json::from_str::<NameEventContent>(room_name_pdu.content.get()) serde_json::from_str::<RoomNameEventContent>(room_name_pdu.content.get())
.map_err(|_| Error::bad_database("Invalid room name event in database."))? .map_err(|_| Error::bad_database("Invalid room name event in database."))?
.name .name
} else { } else {
+191 -309
View File
@@ -1,10 +1,11 @@
mod edus; mod edus;
pub use edus::RoomEdus; pub use edus::RoomEdus;
use member::MembershipState;
use serde_json::value::RawValue;
use crate::{pdu::PduBuilder, server_server, utils, Database, Error, PduEvent, Result}; use crate::{
pdu::{EventHash, PduBuilder},
server_server, utils, Database, Error, PduEvent, Result,
};
use lru_cache::LruCache; use lru_cache::LruCache;
use regex::Regex; use regex::Regex;
use ring::digest; use ring::digest;
@@ -12,18 +13,27 @@ use rocket::http::RawStr;
use ruma::{ use ruma::{
api::{client::error::ErrorKind, federation}, api::{client::error::ErrorKind, federation},
events::{ events::{
ignored_user_list, push_rules, direct::DirectEvent,
ignored_user_list::IgnoredUserListEvent,
push_rules::PushRulesEvent,
room::{ room::{
create::CreateEventContent, member, message, power_levels::PowerLevelsEventContent, create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
power_levels::RoomPowerLevelsEventContent,
}, },
tag::TagEvent,
AnyStrippedStateEvent, AnySyncStateEvent, EventType, AnyStrippedStateEvent, AnySyncStateEvent, EventType,
}, },
push::{self, Action, Tweak}, push::{Action, Ruleset, Tweak},
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res::{self, RoomVersion, StateMap}, state_res::{self, RoomVersion, StateMap},
uint, EventId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId, uint, EventId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde::Deserialize;
use serde_json::value::to_raw_value;
use std::{ use std::{
borrow::Cow,
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
mem::size_of, mem::size_of,
@@ -43,7 +53,7 @@ pub type StateHashId = Vec<u8>;
pub type CompressedStateEvent = [u8; 2 * size_of::<u64>()]; pub type CompressedStateEvent = [u8; 2 * size_of::<u64>()];
pub struct Rooms { pub struct Rooms {
pub edus: edus::RoomEdus, pub edus: RoomEdus,
pub(super) pduid_pdu: Arc<dyn Tree>, // PduId = ShortRoomId + Count pub(super) pduid_pdu: Arc<dyn Tree>, // PduId = ShortRoomId + Count
pub(super) eventid_pduid: Arc<dyn Tree>, pub(super) eventid_pduid: Arc<dyn Tree>,
pub(super) roomid_pduleaves: Arc<dyn Tree>, pub(super) roomid_pduleaves: Arc<dyn Tree>,
@@ -97,7 +107,7 @@ pub struct Rooms {
/// RoomId + EventId -> Parent PDU EventId. /// RoomId + EventId -> Parent PDU EventId.
pub(super) referencedevents: Arc<dyn Tree>, pub(super) referencedevents: Arc<dyn Tree>,
pub(super) pdu_cache: Mutex<LruCache<EventId, Vec<u8>>>, pub(super) pdu_cache: Mutex<LruCache<EventId, Arc<PduEvent>>>,
pub(super) shorteventid_cache: Mutex<LruCache<u64, Arc<EventId>>>, pub(super) shorteventid_cache: Mutex<LruCache<u64, Arc<EventId>>>,
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<HashSet<u64>>>>, pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<HashSet<u64>>>>,
pub(super) eventidshort_cache: Mutex<LruCache<EventId, u64>>, pub(super) eventidshort_cache: Mutex<LruCache<EventId, u64>>,
@@ -138,7 +148,7 @@ impl Rooms {
pub fn state_full( pub fn state_full(
&self, &self,
shortstatehash: u64, shortstatehash: u64,
) -> Result<HashMap<(EventType, String), PduEvent>> { ) -> Result<HashMap<(EventType, String), Arc<PduEvent>>> {
let full_state = self let full_state = self
.load_shortstatehash_info(shortstatehash)? .load_shortstatehash_info(shortstatehash)?
.pop() .pop()
@@ -200,7 +210,7 @@ impl Rooms {
shortstatehash: u64, shortstatehash: u64,
event_type: &EventType, event_type: &EventType,
state_key: &str, state_key: &str,
) -> Result<Option<PduEvent>> { ) -> Result<Option<Arc<PduEvent>>> {
self.state_get_id(shortstatehash, event_type, state_key)? self.state_get_id(shortstatehash, event_type, state_key)?
.map_or(Ok(None), |event_id| self.get_pdu(&event_id)) .map_or(Ok(None), |event_id| self.get_pdu(&event_id))
} }
@@ -211,16 +221,16 @@ impl Rooms {
self.eventid_shorteventid self.eventid_shorteventid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or(Ok(None), |shorteventid| { .map_or(Ok(None), |shorteventid| {
self.shorteventid_shortstatehash.get(&shorteventid)?.map_or( self.shorteventid_shortstatehash
Ok::<_, Error>(None), .get(&shorteventid)?
|bytes| { .map(|bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid shortstatehash bytes in shorteventid_shortstatehash", "Invalid shortstatehash bytes in shorteventid_shortstatehash",
) )
})?)) })
}, })
) .transpose()
}) })
} }
@@ -245,7 +255,7 @@ impl Rooms {
sender: &UserId, sender: &UserId,
state_key: Option<&str>, state_key: Option<&str>,
content: &serde_json::value::RawValue, content: &serde_json::value::RawValue,
) -> Result<StateMap<PduEvent>> { ) -> Result<StateMap<Arc<PduEvent>>> {
let shortstatehash = let shortstatehash =
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
current_shortstatehash current_shortstatehash
@@ -253,12 +263,8 @@ impl Rooms {
return Ok(HashMap::new()); return Ok(HashMap::new());
}; };
let auth_events = state_res::auth_types_for_event( let auth_events = state_res::auth_types_for_event(kind, sender, state_key, content)
kind, .expect("content is a valid JSON object");
sender,
state_key,
&serde_json::from_str(content.get()).expect("RawValue is valid json"),
);
let mut sauthevents = auth_events let mut sauthevents = auth_events
.into_iter() .into_iter()
@@ -335,75 +341,6 @@ impl Rooms {
.transpose() .transpose()
} }
/// Force the creation of a new StateHash and insert it into the db. This version should be
/// used when creating new rooms.
///
/// Whatever `state` is supplied to `force_state` becomes the new current room state snapshot.
#[tracing::instrument(skip(self, new_state_ids_compressed, state_values, db))]
pub fn force_state_new(
&self,
room_id: &RoomId,
new_state_ids_compressed: HashSet<CompressedStateEvent>,
state_values: &mut dyn Iterator<Item = &CanonicalJsonObject>,
db: &Database,
) -> Result<()> {
let state_hash = self.calculate_hash(
&new_state_ids_compressed
.iter()
.map(|bytes| &bytes[..])
.collect::<Vec<_>>(),
);
let (new_shortstatehash, _) =
self.get_or_create_shortstatehash(&state_hash, &db.globals)?;
self.save_state_from_diff(
new_shortstatehash,
new_state_ids_compressed.clone(),
HashSet::new(),
2, // every state change is 2 event changes on average
Vec::new(),
)?;
for pdu in state_values {
if pdu.get("type").and_then(|val| val.as_str()) == Some("m.room.member") {
let content = pdu
.get("content")
.and_then(|o| o.as_object())
.ok_or_else(|| Error::bad_database("Invalid content in pdu."))?;
if let Some(membership) = content.get("membership").and_then(|membership| {
serde_json::from_str::<member::MembershipState>(
&serde_json::to_string(membership).expect("json is valid string"),
)
.ok()
}) {
if let Some(state_key) = pdu
.get("state_key")
.and_then(|o| o.as_str())
.and_then(|state_key| UserId::try_from(state_key).ok())
{
if let Some(sender) = pdu
.get("sender")
.and_then(|o| o.as_str())
.and_then(|state_key| UserId::try_from(state_key).ok())
{
self.update_membership(
room_id, &state_key, membership, &sender, None, db, false,
)?;
}
}
}
}
}
self.update_joined_count(room_id, db)?;
self.roomid_shortstatehash
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
Ok(())
}
/// Force the creation of a new StateHash and insert it into the db. /// Force the creation of a new StateHash and insert it into the db.
/// ///
/// Whatever `state` is supplied to `force_state` becomes the new current room state snapshot. /// Whatever `state` is supplied to `force_state` becomes the new current room state snapshot.
@@ -435,16 +372,16 @@ impl Rooms {
let (statediffnew, statediffremoved) = if let Some(parent_stateinfo) = states_parents.last() let (statediffnew, statediffremoved) = if let Some(parent_stateinfo) = states_parents.last()
{ {
let statediffnew = new_state_ids_compressed let statediffnew: HashSet<_> = new_state_ids_compressed
.difference(&parent_stateinfo.1) .difference(&parent_stateinfo.1)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect();
let statediffremoved = parent_stateinfo let statediffremoved: HashSet<_> = parent_stateinfo
.1 .1
.difference(&new_state_ids_compressed) .difference(&new_state_ids_compressed)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect();
(statediffnew, statediffremoved) (statediffnew, statediffremoved)
} else { } else {
@@ -466,36 +403,43 @@ impl Rooms {
.ok() .ok()
.map(|(_, id)| id) .map(|(_, id)| id)
}) { }) {
if let Some(pdu) = self.get_pdu_json(&event_id)? { let pdu = match self.get_pdu_json(&event_id)? {
if pdu.get("type").and_then(|val| val.as_str()) == Some("m.room.member") { Some(pdu) => pdu,
let content = pdu None => continue,
.get("content") };
.and_then(|o| o.as_object())
.ok_or_else(|| Error::bad_database("Invalid content in pdu."))?; if pdu.get("type").and_then(|val| val.as_str()) != Some("m.room.member") {
if let Some(membership) = content.get("membership").and_then(|membership| { continue;
serde_json::from_str::<member::MembershipState>(
&serde_json::to_string(membership).expect("json is valid string"),
)
.ok()
}) {
if let Some(state_key) = pdu
.get("state_key")
.and_then(|o| o.as_str())
.and_then(|state_key| UserId::try_from(state_key).ok())
{
if let Some(sender) = pdu
.get("sender")
.and_then(|o| o.as_str())
.and_then(|state_key| UserId::try_from(state_key).ok())
{
self.update_membership(
room_id, &state_key, membership, &sender, None, db, false,
)?;
}
}
}
} }
let pdu: PduEvent = match serde_json::from_str(
&serde_json::to_string(&pdu).expect("CanonicalJsonObj can be serialized to JSON"),
) {
Ok(pdu) => pdu,
Err(_) => continue,
};
#[derive(Deserialize)]
struct ExtractMembership {
membership: MembershipState,
} }
let membership = match serde_json::from_str::<ExtractMembership>(pdu.content.get()) {
Ok(e) => e.membership,
Err(_) => continue,
};
let state_key = match pdu.state_key {
Some(k) => k,
None => continue,
};
let user_id = match UserId::try_from(state_key) {
Ok(id) => id,
Err(_) => continue,
};
self.update_membership(room_id, &user_id, membership, &pdu.sender, None, db, false)?;
} }
self.update_joined_count(room_id, db)?; self.update_joined_count(room_id, db)?;
@@ -557,7 +501,7 @@ impl Rooms {
if parent != 0_u64 { if parent != 0_u64 {
let mut response = self.load_shortstatehash_info(parent)?; let mut response = self.load_shortstatehash_info(parent)?;
let mut state = response.last().unwrap().1.clone(); let mut state = response.last().unwrap().1.clone();
state.extend(added.iter().cloned()); state.extend(added.iter().copied());
for r in &removed { for r in &removed {
state.remove(r); state.remove(r);
} }
@@ -989,7 +933,7 @@ impl Rooms {
pub fn room_state_full( pub fn room_state_full(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
) -> Result<HashMap<(EventType, String), PduEvent>> { ) -> Result<HashMap<(EventType, String), Arc<PduEvent>>> {
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
self.state_full(current_shortstatehash) self.state_full(current_shortstatehash)
} else { } else {
@@ -1019,7 +963,7 @@ impl Rooms {
room_id: &RoomId, room_id: &RoomId,
event_type: &EventType, event_type: &EventType,
state_key: &str, state_key: &str,
) -> Result<Option<PduEvent>> { ) -> Result<Option<Arc<PduEvent>>> {
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
self.state_get(current_shortstatehash, event_type, state_key) self.state_get(current_shortstatehash, event_type, state_key)
} else { } else {
@@ -1039,7 +983,8 @@ impl Rooms {
pub fn get_pdu_count(&self, event_id: &EventId) -> Result<Option<u64>> { pub fn get_pdu_count(&self, event_id: &EventId) -> Result<Option<u64>> {
self.eventid_pduid self.eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or(Ok(None), |pdu_id| self.pdu_count(&pdu_id).map(Some)) .map(|pdu_id| self.pdu_count(&pdu_id))
.transpose()
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@@ -1067,7 +1012,7 @@ impl Rooms {
pub fn get_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> { pub fn get_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
self.eventid_pduid self.eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or_else::<Result<_>, _, _>( .map_or_else(
|| self.eventid_outlierpdu.get(event_id.as_bytes()), || self.eventid_outlierpdu.get(event_id.as_bytes()),
|pduid| { |pduid| {
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| { Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| {
@@ -1100,14 +1045,12 @@ impl Rooms {
) -> Result<Option<CanonicalJsonObject>> { ) -> Result<Option<CanonicalJsonObject>> {
self.eventid_pduid self.eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or_else::<Result<_>, _, _>( .map(|pduid| {
|| Ok(None), self.pduid_pdu
|pduid| { .get(&pduid)?
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| { .ok_or_else(|| Error::bad_database("Invalid pduid in eventid_pduid."))
Error::bad_database("Invalid pduid in eventid_pduid.") })
})?)) .transpose()?
},
)?
.map(|pdu| { .map(|pdu| {
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db.")) serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
}) })
@@ -1117,9 +1060,7 @@ impl Rooms {
/// Returns the pdu's id. /// Returns the pdu's id.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn get_pdu_id(&self, event_id: &EventId) -> Result<Option<Vec<u8>>> { pub fn get_pdu_id(&self, event_id: &EventId) -> Result<Option<Vec<u8>>> {
self.eventid_pduid self.eventid_pduid.get(event_id.as_bytes())
.get(event_id.as_bytes())?
.map_or(Ok(None), |pdu_id| Ok(Some(pdu_id)))
} }
/// Returns the pdu. /// Returns the pdu.
@@ -1129,14 +1070,12 @@ impl Rooms {
pub fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> { pub fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
self.eventid_pduid self.eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or_else::<Result<_>, _, _>( .map(|pduid| {
|| Ok(None), self.pduid_pdu
|pduid| { .get(&pduid)?
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| { .ok_or_else(|| Error::bad_database("Invalid pduid in eventid_pduid."))
Error::bad_database("Invalid pduid in eventid_pduid.") })
})?)) .transpose()?
},
)?
.map(|pdu| { .map(|pdu| {
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db.")) serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
}) })
@@ -1147,34 +1086,34 @@ impl Rooms {
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn get_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> { pub fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) { if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) {
return Ok(Some( return Ok(Some(Arc::clone(p)));
serde_json::from_slice(p).expect("pdus in cache are valid"),
));
} }
if let Some(pdu) = self if let Some(pdu) = self
.eventid_pduid .eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map_or_else::<Result<_>, _, _>( .map_or_else(
|| { || self.eventid_outlierpdu.get(event_id.as_bytes()),
let r = self.eventid_outlierpdu.get(event_id.as_bytes());
r
},
|pduid| { |pduid| {
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| { Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| {
Error::bad_database("Invalid pduid in eventid_pduid.") Error::bad_database("Invalid pduid in eventid_pduid.")
})?)) })?))
}, },
)? )?
.map(|pdu| {
serde_json::from_slice(&pdu)
.map_err(|_| Error::bad_database("Invalid PDU in db."))
.map(Arc::new)
})
.transpose()?
{ {
let parsed = self.pdu_cache
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db.")); .lock()
.unwrap()
self.pdu_cache.lock().unwrap().insert(event_id.clone(), pdu); .insert(event_id.clone(), Arc::clone(&pdu));
Ok(Some(pdu))
parsed
} else { } else {
Ok(None) Ok(None)
} }
@@ -1299,23 +1238,6 @@ impl Rooms {
) )
} }
/// Append the PDU as an outlier.
///
/// Any event given to this will be processed (state-res) on another thread.
#[tracing::instrument(skip(self, batch))]
pub fn add_pdu_outlier_batch(
&self,
batch: &mut dyn Iterator<Item = (&EventId, &CanonicalJsonObject)>,
) -> Result<()> {
self.eventid_outlierpdu
.insert_batch(&mut batch.map(|(event_id, pdu)| {
(
event_id.as_bytes().to_vec(),
serde_json::to_vec(&pdu).expect("CanonicalJsonObject is valid"),
)
}))
}
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn mark_event_soft_failed(&self, event_id: &EventId) -> Result<()> { pub fn mark_event_soft_failed(&self, event_id: &EventId) -> Result<()> {
self.softfailedeventids.insert(event_id.as_bytes(), &[]) self.softfailedeventids.insert(event_id.as_bytes(), &[])
@@ -1413,7 +1335,7 @@ impl Rooms {
drop(insert_lock); drop(insert_lock);
// See if the event matches any known pushers // See if the event matches any known pushers
let power_levels: PowerLevelsEventContent = db let power_levels: RoomPowerLevelsEventContent = db
.rooms .rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")? .room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| { .map(|ev| {
@@ -1436,9 +1358,9 @@ impl Rooms {
let rules_for_user = db let rules_for_user = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, user, EventType::PushRules)? .get(None, user, EventType::PushRules)?
.map(|ev| ev.content.global) .map(|ev: PushRulesEvent| ev.content.global)
.unwrap_or_else(|| push::Ruleset::server_default(user)); .unwrap_or_else(|| Ruleset::server_default(user));
let mut highlight = false; let mut highlight = false;
let mut notify = false; let mut notify = false;
@@ -1492,33 +1414,21 @@ impl Rooms {
} }
EventType::RoomMember => { EventType::RoomMember => {
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
#[derive(Deserialize)]
struct ExtractMembership {
membership: MembershipState,
}
// if the state_key fails // if the state_key fails
let target_user_id = UserId::try_from(state_key.clone()) let target_user_id = UserId::try_from(state_key.clone())
.expect("This state_key was previously validated"); .expect("This state_key was previously validated");
let content = serde_json::from_str::<serde_json::Value>(pdu.content.get()) let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
.map_err(|_| Error::bad_database("Invalid content in pdu."))?; .map_err(|_| Error::bad_database("Invalid content in pdu."))?;
let membership = serde_json::from_value::<member::MembershipState>( let invite_state = match content.membership {
content MembershipState::Invite => {
.get("membership")
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid member event content",
))?
.clone(),
)
.map_err(|_| {
Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid membership state content.",
)
})?;
let invite_state = match membership {
member::MembershipState::Invite => {
let state = self.calculate_invite_state(pdu)?; let state = self.calculate_invite_state(pdu)?;
Some(state) Some(state)
} }
_ => None, _ => None,
@@ -1529,7 +1439,7 @@ impl Rooms {
self.update_membership( self.update_membership(
&pdu.room_id, &pdu.room_id,
&target_user_id, &target_user_id,
membership, content.membership,
&pdu.sender, &pdu.sender,
invite_state, invite_state,
db, db,
@@ -1538,10 +1448,16 @@ impl Rooms {
} }
} }
EventType::RoomMessage => { EventType::RoomMessage => {
let content = serde_json::from_str::<serde_json::Value>(pdu.content.get()) #[derive(Deserialize)]
struct ExtractBody<'a> {
#[serde(borrow)]
body: Option<Cow<'a, str>>,
}
let content = serde_json::from_str::<ExtractBody<'_>>(pdu.content.get())
.map_err(|_| Error::bad_database("Invalid content in pdu."))?; .map_err(|_| Error::bad_database("Invalid content in pdu."))?;
if let Some(body) = content.get("body").and_then(|b| b.as_str()) { if let Some(body) = content.body {
let mut batch = body let mut batch = body
.split_terminator(|c: char| !c.is_alphanumeric()) .split_terminator(|c: char| !c.is_alphanumeric())
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
@@ -1569,11 +1485,11 @@ impl Rooms {
{ {
let mut lines = body.lines(); let mut lines = body.lines();
let command_line = lines.next().expect("each string has at least one line"); let command_line = lines.next().expect("each string has at least one line");
let body = lines.collect::<Vec<_>>(); let body: Vec<_> = lines.collect();
let mut parts = command_line.split_whitespace().skip(1); let mut parts = command_line.split_whitespace().skip(1);
if let Some(command) = parts.next() { if let Some(command) = parts.next() {
let args = parts.collect::<Vec<_>>(); let args: Vec<_> = parts.collect();
match command { match command {
"register_appservice" => { "register_appservice" => {
@@ -1592,18 +1508,16 @@ impl Rooms {
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!(
"Could not parse appservice config: {}", "Could not parse appservice config: {}",
e e
), )),
),
)); ));
} }
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Expected code block in command body.", "Expected code block in command body.",
), ),
)); ));
@@ -1636,12 +1550,10 @@ impl Rooms {
.count(); .count();
let elapsed = start.elapsed(); let elapsed = start.elapsed();
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!(
"Loaded auth chain with length {} in {:?}", "Loaded auth chain with length {} in {:?}",
count, elapsed count, elapsed
), )),
),
)); ));
} }
} }
@@ -1674,14 +1586,17 @@ impl Rooms {
) { ) {
Ok(pdu) => { Ok(pdu) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
format!("EventId: {:?}\n{:#?}", event_id, pdu), format!(
"EventId: {:?}\n{:#?}",
event_id, pdu
),
), ),
)); ));
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
format!("EventId: {:?}\nCould not parse event: {}", event_id, e), format!("EventId: {:?}\nCould not parse event: {}", event_id, e),
), ),
)); ));
@@ -1690,18 +1605,16 @@ impl Rooms {
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!(
"Invalid json in command body: {}", "Invalid json in command body: {}",
e e
), )),
),
)); ));
} }
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Expected code block in command body.", "Expected code block in command body.",
), ),
)); ));
@@ -1723,7 +1636,7 @@ impl Rooms {
serde_json::to_string_pretty(&json) serde_json::to_string_pretty(&json)
.expect("canonical json is valid json"); .expect("canonical json is valid json");
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_html( RoomMessageEventContent::text_html(
format!("{}\n```json\n{}\n```", format!("{}\n```json\n{}\n```",
if outlier { if outlier {
"PDU is outlier" "PDU is outlier"
@@ -1737,7 +1650,7 @@ impl Rooms {
} }
None => { None => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"PDU not found.", "PDU not found.",
), ),
)); ));
@@ -1745,14 +1658,14 @@ impl Rooms {
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Event ID could not be parsed.", "Event ID could not be parsed.",
), ),
)); ));
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Usage: get_pdu <eventid>", "Usage: get_pdu <eventid>",
), ),
)); ));
@@ -1760,7 +1673,7 @@ impl Rooms {
} }
_ => { _ => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain(format!( RoomMessageEventContent::text_plain(format!(
"Unrecognized command: {}", "Unrecognized command: {}",
command command
)), )),
@@ -1853,16 +1766,16 @@ impl Rooms {
let (statediffnew, statediffremoved) = let (statediffnew, statediffremoved) =
if let Some(parent_stateinfo) = states_parents.last() { if let Some(parent_stateinfo) = states_parents.last() {
let statediffnew = state_ids_compressed let statediffnew: HashSet<_> = state_ids_compressed
.difference(&parent_stateinfo.1) .difference(&parent_stateinfo.1)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect();
let statediffremoved = parent_stateinfo let statediffremoved: HashSet<_> = parent_stateinfo
.1 .1
.difference(&state_ids_compressed) .difference(&state_ids_compressed)
.cloned() .copied()
.collect::<HashSet<_>>(); .collect();
(statediffnew, statediffremoved) (statediffnew, statediffremoved)
} else { } else {
@@ -2052,13 +1965,10 @@ impl Rooms {
let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?; let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?;
let create_event_content = create_event let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_str::<Raw<CreateEventContent>>(create_event.content.get()) serde_json::from_str(create_event.content.get()).map_err(|e| {
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
}) })
@@ -2114,7 +2024,6 @@ impl Rooms {
.expect("time is valid"), .expect("time is valid"),
kind: event_type, kind: event_type,
content, content,
parsed_content: RwLock::new(None),
state_key, state_key,
prev_events, prev_events,
depth, depth,
@@ -2126,14 +2035,9 @@ impl Rooms {
unsigned: if unsigned.is_empty() { unsigned: if unsigned.is_empty() {
None None
} else { } else {
Some( Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
RawValue::from_string(
serde_json::to_string(&unsigned).expect("to_string always works"),
)
.expect("string is valid"),
)
}, },
hashes: ruma::events::pdu::EventHash { hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: None, signatures: None,
@@ -2416,7 +2320,7 @@ impl Rooms {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
user_id: &UserId, user_id: &UserId,
membership: member::MembershipState, membership: MembershipState,
sender: &UserId, sender: &UserId,
last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>, last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
db: &Database, db: &Database,
@@ -2445,7 +2349,7 @@ impl Rooms {
roomuser_id.extend_from_slice(user_id.as_bytes()); roomuser_id.extend_from_slice(user_id.as_bytes());
match &membership { match &membership {
member::MembershipState::Join => { MembershipState::Join => {
// Check if the user never joined this room // Check if the user never joined this room
if !self.once_joined(user_id, room_id)? { if !self.once_joined(user_id, room_id)? {
// Add the user ID to the join list then // Add the user ID to the join list then
@@ -2454,21 +2358,16 @@ impl Rooms {
// Check if the room has a predecessor // Check if the room has a predecessor
if let Some(predecessor) = self if let Some(predecessor) = self
.room_state_get(room_id, &EventType::RoomCreate, "")? .room_state_get(room_id, &EventType::RoomCreate, "")?
.and_then(|create| { .and_then(|create| serde_json::from_str(create.content.get()).ok())
serde_json::from_str::<ruma::events::room::create::CreateEventContent>( .and_then(|content: RoomCreateEventContent| content.predecessor)
create.content.get(),
)
.ok()
})
.and_then(|content| content.predecessor)
{ {
// Copy user settings from predecessor to the current room: // Copy user settings from predecessor to the current room:
// - Push rules // - Push rules
// //
// TODO: finish this once push rules are implemented. // TODO: finish this once push rules are implemented.
// //
// let mut push_rules_event_content = account_data // let mut push_rules_event_content: PushRulesEvent = account_data
// .get::<ruma::events::push_rules::PushRulesEvent>( // .get(
// None, // None,
// user_id, // user_id,
// EventType::PushRules, // EventType::PushRules,
@@ -2488,13 +2387,11 @@ impl Rooms {
// .ok(); // .ok();
// Copy old tags to new room // Copy old tags to new room
if let Some(tag_event) = if let Some(tag_event) = db.account_data.get::<TagEvent>(
db.account_data.get::<ruma::events::tag::TagEvent>(
Some(&predecessor.room_id), Some(&predecessor.room_id),
user_id, user_id,
EventType::Tag, EventType::Tag,
)? )? {
{
db.account_data db.account_data
.update( .update(
Some(room_id), Some(room_id),
@@ -2508,11 +2405,8 @@ impl Rooms {
// Copy direct chat flag // Copy direct chat flag
if let Some(mut direct_event) = if let Some(mut direct_event) =
db.account_data.get::<ruma::events::direct::DirectEvent>( db.account_data
None, .get::<DirectEvent>(None, user_id, EventType::Direct)?
user_id,
EventType::Direct,
)?
{ {
let mut room_ids_updated = false; let mut room_ids_updated = false;
@@ -2547,11 +2441,11 @@ impl Rooms {
self.userroomid_leftstate.remove(&userroom_id)?; self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?; self.roomuserid_leftcount.remove(&roomuser_id)?;
} }
member::MembershipState::Invite => { MembershipState::Invite => {
// We want to know if the sender is ignored by the receiver // We want to know if the sender is ignored by the receiver
let is_ignored = db let is_ignored = db
.account_data .account_data
.get::<ignored_user_list::IgnoredUserListEvent>( .get::<IgnoredUserListEvent>(
None, // Ignored users are in global account data None, // Ignored users are in global account data
user_id, // Receiver user_id, // Receiver
EventType::IgnoredUserList, EventType::IgnoredUserList,
@@ -2580,7 +2474,7 @@ impl Rooms {
self.userroomid_leftstate.remove(&userroom_id)?; self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?; self.roomuserid_leftcount.remove(&roomuser_id)?;
} }
member::MembershipState::Leave | member::MembershipState::Ban => { MembershipState::Leave | MembershipState::Ban => {
if update_joined_count if update_joined_count
&& self && self
.room_members(room_id) .room_members(room_id)
@@ -2805,7 +2699,7 @@ impl Rooms {
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let mut event = serde_json::from_str::<member::MemberEventContent>( let mut event: RoomMemberEventContent = serde_json::from_str(
self.room_state_get(room_id, &EventType::RoomMember, &user_id.to_string())? self.room_state_get(room_id, &EventType::RoomMember, &user_id.to_string())?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
@@ -2816,15 +2710,12 @@ impl Rooms {
) )
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = member::MembershipState::Leave; event.membership = MembershipState::Leave;
self.build_and_append_pdu( self.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: RawValue::from_string( content: to_raw_value(&event).expect("event is valid, we just created it"),
serde_json::to_string(&event).expect("event is valid, we just created it"),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
@@ -2858,16 +2749,14 @@ impl Rooms {
"User is not invited.", "User is not invited.",
))?; ))?;
let servers = invite_state let servers: HashSet<_> = invite_state
.iter() .iter()
.filter_map(|event| { .filter_map(|event| serde_json::from_str(event.json().get()).ok())
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok() .filter_map(|event: serde_json::Value| event.get("sender").cloned())
})
.filter_map(|event| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| UserId::try_from(sender).ok()) .filter_map(|sender| UserId::try_from(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect::<HashSet<_>>(); .collect();
for remote_server in servers { for remote_server in servers {
let make_leave_response = db let make_leave_response = db
@@ -2898,10 +2787,9 @@ impl Rooms {
}; };
let mut leave_event_stub = let mut leave_event_stub =
serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.json().get()) serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.get()).map_err(
.map_err(|_| { |_| Error::BadServerResponse("Invalid make_leave event json received from server."),
Error::BadServerResponse("Invalid make_leave event json received from server.") )?;
})?;
// TODO: Is origin needed? // TODO: Is origin needed?
leave_event_stub.insert( leave_event_stub.insert(
@@ -2952,7 +2840,7 @@ impl Rooms {
federation::membership::create_leave_event::v2::Request { federation::membership::create_leave_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id: &event_id,
pdu: PduEvent::convert_to_outgoing_federation_event(leave_event.clone()), pdu: &PduEvent::convert_to_outgoing_federation_event(leave_event.clone()),
}, },
) )
.await?; .await?;
@@ -3017,14 +2905,13 @@ impl Rooms {
pub fn id_from_alias(&self, alias: &RoomAliasId) -> Result<Option<RoomId>> { pub fn id_from_alias(&self, alias: &RoomAliasId) -> Result<Option<RoomId>> {
self.alias_roomid self.alias_roomid
.get(alias.alias().as_bytes())? .get(alias.alias().as_bytes())?
.map_or(Ok(None), |bytes| { .map(|bytes| {
Ok(Some(
RoomId::try_from(utils::string_from_bytes(&bytes).map_err(|_| { RoomId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Room ID in alias_roomid is invalid unicode.") Error::bad_database("Room ID in alias_roomid is invalid unicode.")
})?) })?)
.map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid."))?, .map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid."))
))
}) })
.transpose()
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@@ -3084,11 +2971,11 @@ impl Rooms {
.to_vec(); .to_vec();
let prefix_clone = prefix.clone(); let prefix_clone = prefix.clone();
let words = search_string let words: Vec<_> = search_string
.split_terminator(|c: char| !c.is_alphanumeric()) .split_terminator(|c: char| !c.is_alphanumeric())
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
.map(str::to_lowercase) .map(str::to_lowercase)
.collect::<Vec<_>>(); .collect();
let iterators = words.clone().into_iter().map(move |word| { let iterators = words.clone().into_iter().map(move |word| {
let mut prefix2 = prefix.clone(); let mut prefix2 = prefix.clone();
@@ -3101,12 +2988,7 @@ impl Rooms {
self.tokenids self.tokenids
.iter_from(&last_possible_id, true) // Newest pdus first .iter_from(&last_possible_id, true) // Newest pdus first
.take_while(move |(k, _)| k.starts_with(&prefix2)) .take_while(move |(k, _)| k.starts_with(&prefix2))
.map(|(key, _)| { .map(|(key, _)| key[key.len() - size_of::<u64>()..].to_vec())
let pdu_id = key[key.len() - size_of::<u64>()..].to_vec();
Ok::<_, Error>(pdu_id)
})
.filter_map(|r| r.ok())
}); });
Ok(( Ok((
@@ -3338,11 +3220,11 @@ impl Rooms {
self.roomuserid_leftcount self.roomuserid_leftcount
.get(&key)? .get(&key)?
.map_or(Ok(None), |bytes| { .map(|bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes)
Error::bad_database("Invalid leftcount in db.") .map_err(|_| Error::bad_database("Invalid leftcount in db."))
})?))
}) })
.transpose()
} }
/// Returns an iterator over all rooms this user joined. /// Returns an iterator over all rooms this user joined.
+13 -11
View File
@@ -162,11 +162,12 @@ impl RoomEdus {
Ok(self Ok(self
.roomuserid_lastprivatereadupdate .roomuserid_lastprivatereadupdate
.get(&key)? .get(&key)?
.map_or(Ok::<_, Error>(None), |bytes| { .map(|bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.") Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.")
})?)) })
})? })
.transpose()?
.unwrap_or(0)) .unwrap_or(0))
} }
@@ -286,11 +287,12 @@ impl RoomEdus {
Ok(self Ok(self
.roomid_lasttypingupdate .roomid_lasttypingupdate
.get(room_id.as_bytes())? .get(room_id.as_bytes())?
.map_or(Ok::<_, Error>(None), |bytes| { .map(|bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.") Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
})?)) })
})? })
.transpose()?
.unwrap_or(0)) .unwrap_or(0))
} }
@@ -331,7 +333,7 @@ impl RoomEdus {
&self, &self,
user_id: &UserId, user_id: &UserId,
room_id: &RoomId, room_id: &RoomId,
presence: ruma::events::presence::PresenceEvent, presence: PresenceEvent,
globals: &super::super::globals::Globals, globals: &super::super::globals::Globals,
) -> Result<()> { ) -> Result<()> {
// TODO: Remove old entry? Or maybe just wipe completely from time to time? // TODO: Remove old entry? Or maybe just wipe completely from time to time?
@@ -399,7 +401,7 @@ impl RoomEdus {
self.presenceid_presence self.presenceid_presence
.get(&presence_id)? .get(&presence_id)?
.map(|value| { .map(|value| {
let mut presence = serde_json::from_slice::<PresenceEvent>(&value) let mut presence: PresenceEvent = serde_json::from_slice(&value)
.map_err(|_| Error::bad_database("Invalid presence event in db."))?; .map_err(|_| Error::bad_database("Invalid presence event in db."))?;
let current_timestamp: UInt = utils::millis_since_unix_epoch() let current_timestamp: UInt = utils::millis_since_unix_epoch()
.try_into() .try_into()
@@ -521,7 +523,7 @@ impl RoomEdus {
) )
.map_err(|_| Error::bad_database("Invalid UserId in presenceid_presence."))?; .map_err(|_| Error::bad_database("Invalid UserId in presenceid_presence."))?;
let mut presence = serde_json::from_slice::<PresenceEvent>(&value) let mut presence: PresenceEvent = serde_json::from_slice(&value)
.map_err(|_| Error::bad_database("Invalid presence event in db."))?; .map_err(|_| Error::bad_database("Invalid presence event in db."))?;
let current_timestamp: UInt = utils::millis_since_unix_epoch() let current_timestamp: UInt = utils::millis_since_unix_epoch()
+9 -9
View File
@@ -27,7 +27,7 @@ use ruma::{
OutgoingRequest, OutgoingRequest,
}, },
device_id, device_id,
events::{push_rules, AnySyncEphemeralRoomEvent, EventType}, events::{push_rules::PushRulesEvent, AnySyncEphemeralRoomEvent, EventType},
push, push,
receipt::ReceiptType, receipt::ReceiptType,
uint, MilliSecondsSinceUnixEpoch, ServerName, UInt, UserId, uint, MilliSecondsSinceUnixEpoch, ServerName, UInt, UserId,
@@ -165,13 +165,13 @@ impl Sending {
} }
// Find events that have been added since starting the last request // Find events that have been added since starting the last request
let new_events = guard.sending.servernameevent_data let new_events: Vec<_> = guard.sending.servernameevent_data
.scan_prefix(prefix.clone()) .scan_prefix(prefix.clone())
.filter_map(|(k, v)| { .filter_map(|(k, v)| {
Self::parse_servercurrentevent(&k, v).ok().map(|ev| (ev, k)) Self::parse_servercurrentevent(&k, v).ok().map(|ev| (ev, k))
}) })
.take(30) .take(30)
.collect::<Vec<_>>(); .collect::<>();
// TODO: find edus // TODO: find edus
@@ -344,8 +344,8 @@ impl Sending {
continue; continue;
} }
let event = let event: AnySyncEphemeralRoomEvent =
serde_json::from_str::<AnySyncEphemeralRoomEvent>(read_receipt.json().get()) serde_json::from_str(read_receipt.json().get())
.map_err(|_| Error::bad_database("Invalid edu event in read_receipts."))?; .map_err(|_| Error::bad_database("Invalid edu event in read_receipts."))?;
let federation_event = match event { let federation_event = match event {
AnySyncEphemeralRoomEvent::Receipt(r) => { AnySyncEphemeralRoomEvent::Receipt(r) => {
@@ -398,7 +398,7 @@ impl Sending {
let edu = Edu::DeviceListUpdate(DeviceListUpdateContent { let edu = Edu::DeviceListUpdate(DeviceListUpdateContent {
user_id, user_id,
device_id: device_id!("dummy"), device_id: device_id!("dummy"),
device_display_name: "Dummy".to_owned(), device_display_name: Some("Dummy".to_owned()),
stream_id: uint!(1), stream_id: uint!(1),
prev_id: Vec::new(), prev_id: Vec::new(),
deleted: None, deleted: None,
@@ -485,7 +485,7 @@ impl Sending {
kind: OutgoingKind, kind: OutgoingKind,
events: Vec<SendingEventType>, events: Vec<SendingEventType>,
db: Arc<RwLock<Database>>, db: Arc<RwLock<Database>>,
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> { ) -> Result<OutgoingKind, (OutgoingKind, Error)> {
let db = db.read().await; let db = db.read().await;
match &kind { match &kind {
@@ -612,9 +612,9 @@ impl Sending {
let rules_for_user = db let rules_for_user = db
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, &userid, EventType::PushRules) .get(None, &userid, EventType::PushRules)
.unwrap_or_default() .unwrap_or_default()
.map(|ev| ev.content.global) .map(|ev: PushRulesEvent| ev.content.global)
.unwrap_or_else(|| push::Ruleset::server_default(&userid)); .unwrap_or_else(|| push::Ruleset::server_default(&userid));
let unread: UInt = db let unread: UInt = db
+11 -14
View File
@@ -5,7 +5,8 @@ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
r0::uiaa::{ r0::uiaa::{
IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId, UiaaInfo, AuthType, IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId,
UiaaInfo,
}, },
}, },
signatures::CanonicalJsonValue, signatures::CanonicalJsonValue,
@@ -99,10 +100,10 @@ impl Uiaa {
} }
// Password was correct! Let's add it to `completed` // Password was correct! Let's add it to `completed`
uiaainfo.completed.push("m.login.password".to_owned()); uiaainfo.completed.push(AuthType::Password);
} }
IncomingAuthData::Dummy(_) => { IncomingAuthData::Dummy(_) => {
uiaainfo.completed.push("m.login.dummy".to_owned()); uiaainfo.completed.push(AuthType::Dummy);
} }
k => error!("type not supported: {:?}", k), k => error!("type not supported: {:?}", k),
} }
@@ -174,16 +175,14 @@ impl Uiaa {
self.userdevicesessionid_uiaarequest self.userdevicesessionid_uiaarequest
.get(&userdevicesessionid)? .get(&userdevicesessionid)?
.map_or(Ok(None), |bytes| { .map(|bytes| {
Ok::<_, Error>(Some(
serde_json::from_str::<CanonicalJsonValue>( serde_json::from_str::<CanonicalJsonValue>(
&utils::string_from_bytes(&bytes).map_err(|_| { &utils::string_from_bytes(&bytes)
Error::bad_database("Invalid uiaa request bytes in db.") .map_err(|_| Error::bad_database("Invalid uiaa request bytes in db."))?,
})?,
) )
.map_err(|_| Error::bad_database("Invalid uiaa request in db."))?, .map_err(|_| Error::bad_database("Invalid uiaa request in db."))
))
}) })
.transpose()
} }
fn update_uiaa_session( fn update_uiaa_session(
@@ -224,7 +223,7 @@ impl Uiaa {
userdevicesessionid.push(0xff); userdevicesessionid.push(0xff);
userdevicesessionid.extend_from_slice(session.as_bytes()); userdevicesessionid.extend_from_slice(session.as_bytes());
let uiaainfo = serde_json::from_slice::<UiaaInfo>( serde_json::from_slice(
&self &self
.userdevicesessionid_uiaainfo .userdevicesessionid_uiaainfo
.get(&userdevicesessionid)? .get(&userdevicesessionid)?
@@ -233,8 +232,6 @@ impl Uiaa {
"UIAA session does not exist.", "UIAA session does not exist.",
))?, ))?,
) )
.map_err(|_| Error::bad_database("UiaaInfo in userdeviceid_uiaainfo is invalid."))?; .map_err(|_| Error::bad_database("UiaaInfo in userdeviceid_uiaainfo is invalid."))
Ok(uiaainfo)
} }
} }
+23 -5
View File
@@ -5,7 +5,8 @@ use ruma::{
events::{AnyToDeviceEvent, EventType}, events::{AnyToDeviceEvent, EventType},
identifiers::MxcUri, identifiers::MxcUri,
serde::Raw, serde::Raw,
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, UInt, UserId, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, RoomAliasId, UInt,
UserId,
}; };
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc}; use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc};
use tracing::warn; use tracing::warn;
@@ -53,6 +54,22 @@ impl Users {
.is_empty()) .is_empty())
} }
/// Check if a user is an admin
#[tracing::instrument(skip(self, user_id, rooms, globals))]
pub fn is_admin(
&self,
user_id: &UserId,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<bool> {
let admin_room_alias_id =
RoomAliasId::try_from(format!("#admins:{}", globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_id = rooms.id_from_alias(&admin_room_alias_id)?.unwrap();
Ok(rooms.is_joined(user_id, &admin_room_id)?)
}
/// Create a new user account on this homeserver. /// Create a new user account on this homeserver.
#[tracing::instrument(skip(self, user_id, password))] #[tracing::instrument(skip(self, user_id, password))]
pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> { pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
@@ -603,10 +620,11 @@ impl Users {
key.push(0xff); key.push(0xff);
key.extend_from_slice(key_id.as_bytes()); key.extend_from_slice(key_id.as_bytes());
let mut cross_signing_key = let mut cross_signing_key: serde_json::Value =
serde_json::from_slice::<serde_json::Value>(&self.keyid_key.get(&key)?.ok_or( serde_json::from_slice(&self.keyid_key.get(&key)?.ok_or(Error::BadRequest(
Error::BadRequest(ErrorKind::InvalidParam, "Tried to sign nonexistent key."), ErrorKind::InvalidParam,
)?) "Tried to sign nonexistent key.",
))?)
.map_err(|_| Error::bad_database("key in keyid_key is invalid."))?; .map_err(|_| Error::bad_database("key in keyid_key is invalid."))?;
let signatures = cross_signing_key let signatures = cross_signing_key
+1 -1
View File
@@ -20,7 +20,7 @@ use {
tracing::error, tracing::error,
}; };
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
+6
View File
@@ -1,3 +1,9 @@
#![warn(
rust_2018_idioms,
unused_qualifications,
clippy::cloned_instead_of_copied,
clippy::str_to_string
)]
#![allow(clippy::suspicious_else_formatting)] #![allow(clippy::suspicious_else_formatting)]
#![deny(clippy::dbg_macro)] #![deny(clippy::dbg_macro)]
+6 -1
View File
@@ -1,4 +1,9 @@
#![warn(rust_2018_idioms)] #![warn(
rust_2018_idioms,
unused_qualifications,
clippy::cloned_instead_of_copied,
clippy::str_to_string
)]
#![allow(clippy::suspicious_else_formatting)] #![allow(clippy::suspicious_else_formatting)]
#![deny(clippy::dbg_macro)] #![deny(clippy::dbg_macro)]
+39 -73
View File
@@ -1,20 +1,29 @@
use crate::Error; use crate::Error;
use ruma::{ use ruma::{
events::{ events::{
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent, room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyInitialStateEvent,
AnyInitialStateEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent,
AnySyncStateEvent, EventType, StateEvent, EventType, StateEvent,
}, },
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, UInt, state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UInt, UserId,
UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, value::RawValue}; use serde_json::{
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom, sync::RwLock}; json,
value::{to_raw_value, RawValue as RawJsonValue},
};
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
use tracing::warn; use tracing::warn;
#[derive(Deserialize, Serialize, Debug)] /// Content hashes of a PDU.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EventHash {
/// The SHA-256 hash.
pub sha256: String,
}
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct PduEvent { pub struct PduEvent {
pub event_id: EventId, pub event_id: EventId,
pub room_id: RoomId, pub room_id: RoomId,
@@ -22,9 +31,7 @@ pub struct PduEvent {
pub origin_server_ts: UInt, pub origin_server_ts: UInt,
#[serde(rename = "type")] #[serde(rename = "type")]
pub kind: EventType, pub kind: EventType,
pub content: Box<serde_json::value::RawValue>, pub content: Box<RawJsonValue>,
#[serde(skip)]
pub parsed_content: RwLock<Option<serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>, pub state_key: Option<String>,
pub prev_events: Vec<EventId>, pub prev_events: Vec<EventId>,
@@ -33,10 +40,10 @@ pub struct PduEvent {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<EventId>, pub redacts: Option<EventId>,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub unsigned: Option<Box<serde_json::value::RawValue>>, pub unsigned: Option<Box<RawJsonValue>>,
pub hashes: EventHash, pub hashes: EventHash,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<Box<serde_json::value::RawValue>>, // BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>> pub signatures: Option<Box<RawJsonValue>>, // BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
} }
impl PduEvent { impl PduEvent {
@@ -62,8 +69,8 @@ impl PduEvent {
_ => &[], _ => &[],
}; };
let mut old_content = let mut old_content: BTreeMap<String, serde_json::Value> =
serde_json::from_str::<BTreeMap<String, serde_json::Value>>(self.content.get()) serde_json::from_str(self.content.get())
.map_err(|_| Error::bad_database("PDU in db has invalid content."))?; .map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
let mut new_content = serde_json::Map::new(); let mut new_content = serde_json::Map::new();
@@ -74,28 +81,22 @@ impl PduEvent {
} }
} }
self.unsigned = Some(RawValue::from_string(serde_json::to_string(&json!({ self.unsigned = Some(to_raw_value(&json!({
"redacted_because": serde_json::to_value(reason).expect("to_value(PduEvent) always works") "redacted_because": serde_json::to_value(reason).expect("to_value(PduEvent) always works")
})).expect("to string always works")).expect("string is valid")); })).expect("to string always works"));
self.content = RawValue::from_string( self.content = to_raw_value(&new_content).expect("to string always works");
serde_json::to_string(&new_content).expect("to string always works"),
)
.expect("string is valid");
Ok(()) Ok(())
} }
pub fn remove_transaction_id(&mut self) -> crate::Result<()> { pub fn remove_transaction_id(&mut self) -> crate::Result<()> {
if let Some(unsigned) = &self.unsigned { if let Some(unsigned) = &self.unsigned {
let mut unsigned = let mut unsigned: BTreeMap<String, Box<RawJsonValue>> =
serde_json::from_str::<BTreeMap<String, Box<RawValue>>>(unsigned.get()) serde_json::from_str(unsigned.get())
.map_err(|_| Error::bad_database("Invalid unsigned in pdu event"))?; .map_err(|_| Error::bad_database("Invalid unsigned in pdu event"))?;
unsigned.remove("transaction_id"); unsigned.remove("transaction_id");
self.unsigned = Some( self.unsigned = Some(to_raw_value(&unsigned).expect("unsigned is valid"));
RawValue::from_string(serde_json::to_string(&unsigned).expect("unsigned is valid"))
.expect("string is valid"),
);
} }
Ok(()) Ok(())
@@ -211,7 +212,7 @@ impl PduEvent {
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> { pub fn to_member_event(&self) -> Raw<StateEvent<RoomMemberEventContent>> {
let json = json!({ let json = json!({
"content": self.content, "content": self.content,
"type": self.kind, "type": self.kind,
@@ -231,7 +232,7 @@ impl PduEvent {
#[tracing::instrument] #[tracing::instrument]
pub fn convert_to_outgoing_federation_event( pub fn convert_to_outgoing_federation_event(
mut pdu_json: CanonicalJsonObject, mut pdu_json: CanonicalJsonObject,
) -> Raw<ruma::events::pdu::Pdu> { ) -> Box<RawJsonValue> {
if let Some(unsigned) = pdu_json if let Some(unsigned) = pdu_json
.get_mut("unsigned") .get_mut("unsigned")
.and_then(|val| val.as_object_mut()) .and_then(|val| val.as_object_mut())
@@ -248,10 +249,7 @@ impl PduEvent {
// ) // )
// .expect("Raw::from_value always works") // .expect("Raw::from_value always works")
serde_json::from_value::<Raw<_>>( to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
)
.expect("Raw::from_value always works")
} }
pub fn from_id_val( pub fn from_id_val(
@@ -259,7 +257,7 @@ impl PduEvent {
mut json: CanonicalJsonObject, mut json: CanonicalJsonObject,
) -> Result<Self, serde_json::Error> { ) -> Result<Self, serde_json::Error> {
json.insert( json.insert(
"event_id".to_string(), "event_id".to_owned(),
CanonicalJsonValue::String(event_id.as_str().to_owned()), CanonicalJsonValue::String(event_id.as_str().to_owned()),
); );
@@ -284,14 +282,8 @@ impl state_res::Event for PduEvent {
&self.kind &self.kind
} }
fn content(&self) -> serde_json::Value { fn content(&self) -> &RawJsonValue {
self.parsed_content &self.content
.write()
.unwrap()
.get_or_insert_with(|| {
serde_json::to_value(&self.content).expect("content is valid json")
})
.clone()
} }
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
@@ -306,10 +298,6 @@ impl state_res::Event for PduEvent {
Box::new(self.prev_events.iter()) Box::new(self.prev_events.iter())
} }
fn depth(&self) -> &UInt {
&self.depth
}
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> { fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> {
Box::new(self.auth_events.iter()) Box::new(self.auth_events.iter())
} }
@@ -317,24 +305,6 @@ impl state_res::Event for PduEvent {
fn redacts(&self) -> Option<&EventId> { fn redacts(&self) -> Option<&EventId> {
self.redacts.as_ref() self.redacts.as_ref()
} }
fn hashes(&self) -> &EventHash {
&self.hashes
}
fn signatures(&self) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
self.signatures
.as_ref()
.map(|raw| serde_json::from_str(raw.get()).expect("string is valid signatures json"))
.unwrap_or_default()
}
fn unsigned(&self) -> BTreeMap<String, serde_json::Value> {
self.unsigned
.as_ref()
.map(|raw| serde_json::from_str(raw.get()).expect("string is valid json"))
.unwrap_or_default()
}
} }
// These impl's allow us to dedup state snapshots when resolving state // These impl's allow us to dedup state snapshots when resolving state
@@ -360,9 +330,9 @@ impl Ord for PduEvent {
/// ///
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`. /// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json( pub(crate) fn gen_event_id_canonical_json(
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &RawJsonValue,
) -> crate::Result<(EventId, CanonicalJsonObject)> { ) -> crate::Result<(EventId, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.json().get()).map_err(|e| { let value = serde_json::from_str(pdu.get()).map_err(|e| {
warn!("Error parsing incoming event {:?}: {:?}", pdu, e); warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
@@ -383,7 +353,7 @@ pub(crate) fn gen_event_id_canonical_json(
pub struct PduBuilder { pub struct PduBuilder {
#[serde(rename = "type")] #[serde(rename = "type")]
pub event_type: EventType, pub event_type: EventType,
pub content: Box<serde_json::value::RawValue>, pub content: Box<RawJsonValue>,
pub unsigned: Option<BTreeMap<String, serde_json::Value>>, pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
pub state_key: Option<String>, pub state_key: Option<String>,
pub redacts: Option<EventId>, pub redacts: Option<EventId>,
@@ -394,12 +364,8 @@ impl From<AnyInitialStateEvent> for PduBuilder {
fn from(event: AnyInitialStateEvent) -> Self { fn from(event: AnyInitialStateEvent) -> Self {
Self { Self {
event_type: EventType::from(event.event_type()), event_type: EventType::from(event.event_type()),
content: RawValue::from_string( content: to_raw_value(&event.content())
serde_json::to_string(&event.content()).expect( .expect("AnyStateEventContent came from JSON and can thus turn back into JSON."),
"AnyStateEventContent came from JSON and can thus turn back into JSON.",
),
)
.expect("string is valid"),
unsigned: None, unsigned: None,
state_key: Some(event.state_key().to_owned()), state_key: Some(event.state_key().to_owned()),
redacts: None, redacts: None,
+1 -1
View File
@@ -344,7 +344,7 @@ impl<T: Outgoing> Deref for Ruma<T> {
} }
/// This struct converts ruma responses into rocket http responses. /// This struct converts ruma responses into rocket http responses.
pub type ConduitResult<T> = std::result::Result<RumaResponse<T>, Error>; pub type ConduitResult<T> = Result<RumaResponse<T>, Error>;
pub fn response<T: OutgoingResponse>(response: RumaResponse<T>) -> response::Result<'static> { pub fn response<T: OutgoingResponse>(response: RumaResponse<T>) -> response::Result<'static> {
let http_response = response let http_response = response
+138 -118
View File
@@ -1,6 +1,7 @@
use crate::{ use crate::{
client_server::{self, claim_keys_helper, get_keys_helper}, client_server::{self, claim_keys_helper, get_keys_helper},
database::{rooms::CompressedStateEvent, DatabaseGuard}, database::{rooms::CompressedStateEvent, DatabaseGuard},
pdu::EventHash,
utils, ConduitResult, Database, Error, PduEvent, Result, Ruma, utils, ConduitResult, Database, Error, PduEvent, Result, Ruma,
}; };
use get_profile_information::v1::ProfileField; use get_profile_information::v1::ProfileField;
@@ -41,22 +42,22 @@ use ruma::{
events::{ events::{
receipt::{ReceiptEvent, ReceiptEventContent}, receipt::{ReceiptEvent, ReceiptEventContent},
room::{ room::{
create::CreateEventContent, create::RoomCreateEventContent,
member::{MemberEventContent, MembershipState}, member::{MembershipState, RoomMemberEventContent},
}, },
AnyEphemeralRoomEvent, EventType, AnyEphemeralRoomEvent, EventType,
}, },
int,
receipt::ReceiptType, receipt::ReceiptType,
serde::Raw, serde::JsonObject,
signatures::{CanonicalJsonObject, CanonicalJsonValue}, signatures::{CanonicalJsonObject, CanonicalJsonValue},
state_res::{self, RoomVersion, StateMap}, state_res::{self, RoomVersion, StateMap},
to_device::DeviceIdOrAllDevices, to_device::DeviceIdOrAllDevices,
uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, ServerSigningKeyId,
}; };
use serde_json::value::RawValue; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{ use std::{
cell::RefCell,
collections::{btree_map, hash_map, BTreeMap, BTreeSet, HashMap, HashSet}, collections::{btree_map, hash_map, BTreeMap, BTreeSet, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
fmt::Debug, fmt::Debug,
@@ -64,7 +65,6 @@ use std::{
mem, mem,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
pin::Pin, pin::Pin,
result::Result as StdResult,
sync::{Arc, RwLock, RwLockWriteGuard}, sync::{Arc, RwLock, RwLockWriteGuard},
time::{Duration, Instant, SystemTime}, time::{Duration, Instant, SystemTime},
}; };
@@ -336,7 +336,7 @@ fn add_port_to_hostname(destination_str: &str) -> FedDest {
None => (destination_str, ":8448"), None => (destination_str, ":8448"),
Some(pos) => destination_str.split_at(pos), Some(pos) => destination_str.split_at(pos),
}; };
FedDest::Named(host.to_string(), port.to_string()) FedDest::Named(host.to_owned(), port.to_owned())
} }
/// Returns: actual_destination, host header /// Returns: actual_destination, host header
@@ -358,7 +358,7 @@ async fn find_actual_destination(
if let Some(pos) = destination_str.find(':') { if let Some(pos) = destination_str.find(':') {
// 2: Hostname with included port // 2: Hostname with included port
let (host, port) = destination_str.split_at(pos); let (host, port) = destination_str.split_at(pos);
FedDest::Named(host.to_string(), port.to_string()) FedDest::Named(host.to_owned(), port.to_owned())
} else { } else {
match request_well_known(globals, destination.as_str()).await { match request_well_known(globals, destination.as_str()).await {
// 3: A .well-known file is available // 3: A .well-known file is available
@@ -370,7 +370,7 @@ async fn find_actual_destination(
if let Some(pos) = delegated_hostname.find(':') { if let Some(pos) = delegated_hostname.find(':') {
// 3.2: Hostname with port in .well-known file // 3.2: Hostname with port in .well-known file
let (host, port) = delegated_hostname.split_at(pos); let (host, port) = delegated_hostname.split_at(pos);
FedDest::Named(host.to_string(), port.to_string()) FedDest::Named(host.to_owned(), port.to_owned())
} else { } else {
// Delegated hostname has no port in this branch // Delegated hostname has no port in this branch
if let Some(hostname_override) = if let Some(hostname_override) =
@@ -454,12 +454,12 @@ async fn find_actual_destination(
let hostname = if let Ok(addr) = hostname.parse::<SocketAddr>() { let hostname = if let Ok(addr) = hostname.parse::<SocketAddr>() {
FedDest::Literal(addr) FedDest::Literal(addr)
} else if let Ok(addr) = hostname.parse::<IpAddr>() { } else if let Ok(addr) = hostname.parse::<IpAddr>() {
FedDest::Named(addr.to_string(), ":8448".to_string()) FedDest::Named(addr.to_string(), ":8448".to_owned())
} else if let Some(pos) = hostname.find(':') { } else if let Some(pos) = hostname.find(':') {
let (host, port) = hostname.split_at(pos); let (host, port) = hostname.split_at(pos);
FedDest::Named(host.to_string(), port.to_string()) FedDest::Named(host.to_owned(), port.to_owned())
} else { } else {
FedDest::Named(hostname, ":8448".to_string()) FedDest::Named(hostname, ":8448".to_owned())
}; };
(actual_destination, hostname) (actual_destination, hostname)
} }
@@ -476,11 +476,7 @@ async fn query_srv_record(
.map(|srv| { .map(|srv| {
srv.iter().next().map(|result| { srv.iter().next().map(|result| {
FedDest::Named( FedDest::Named(
result result.target().to_string().trim_end_matches('.').to_owned(),
.target()
.to_string()
.trim_end_matches('.')
.to_string(),
format!(":{}", result.port()), format!(":{}", result.port()),
) )
}) })
@@ -745,7 +741,7 @@ pub async fn send_transaction_message_route(
Some(id) => id, Some(id) => id,
None => { None => {
// Event is invalid // Event is invalid
resolved_map.insert(event_id, Err("Event needs a valid RoomId.".to_string())); resolved_map.insert(event_id, Err("Event needs a valid RoomId.".to_owned()));
continue; continue;
} }
}; };
@@ -959,11 +955,11 @@ pub(crate) async fn handle_incoming_pdu<'a>(
is_timeline_event: bool, is_timeline_event: bool,
db: &'a Database, db: &'a Database,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> StdResult<Option<Vec<u8>>, String> { ) -> Result<Option<Vec<u8>>, String> {
match db.rooms.exists(room_id) { match db.rooms.exists(room_id) {
Ok(true) => {} Ok(true) => {}
_ => { _ => {
return Err("Room is unknown to this server.".to_string()); return Err("Room is unknown to this server.".to_owned());
} }
} }
@@ -1007,12 +1003,12 @@ pub(crate) async fn handle_incoming_pdu<'a>(
// 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events // 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events
let mut graph = HashMap::new(); let mut graph = HashMap::new();
let mut eventid_info = HashMap::new(); let mut eventid_info = HashMap::new();
let mut todo_outlier_stack = incoming_pdu let mut todo_outlier_stack: Vec<_> = incoming_pdu
.prev_events .prev_events
.iter() .iter()
.cloned() .cloned()
.map(Arc::new) .map(Arc::new)
.collect::<Vec<_>>(); .collect();
let mut amount = 0; let mut amount = 0;
@@ -1072,7 +1068,7 @@ pub(crate) async fn handle_incoming_pdu<'a>(
// and lexically by event_id. // and lexically by event_id.
println!("{}", event_id); println!("{}", event_id);
Ok(( Ok((
0, int!(0),
MilliSecondsSinceUnixEpoch( MilliSecondsSinceUnixEpoch(
eventid_info eventid_info
.get(event_id) .get(event_id)
@@ -1096,7 +1092,7 @@ pub(crate) async fn handle_incoming_pdu<'a>(
let start_time = Instant::now(); let start_time = Instant::now();
let event_id = pdu.event_id.clone(); let event_id = pdu.event_id.clone();
if let Err(e) = upgrade_outlier_to_timeline_pdu( if let Err(e) = upgrade_outlier_to_timeline_pdu(
&pdu, pdu,
json, json,
&create_event, &create_event,
origin, origin,
@@ -1120,7 +1116,7 @@ pub(crate) async fn handle_incoming_pdu<'a>(
} }
upgrade_outlier_to_timeline_pdu( upgrade_outlier_to_timeline_pdu(
&incoming_pdu, incoming_pdu,
val, val,
&create_event, &create_event,
origin, origin,
@@ -1140,7 +1136,7 @@ fn handle_outlier_pdu<'a>(
value: BTreeMap<String, CanonicalJsonValue>, value: BTreeMap<String, CanonicalJsonValue>,
db: &'a Database, db: &'a Database,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> AsyncRecursiveType<'a, StdResult<(PduEvent, BTreeMap<String, CanonicalJsonValue>), String>> { ) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>), String>> {
Box::pin(async move { Box::pin(async move {
// TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
@@ -1153,10 +1149,8 @@ fn handle_outlier_pdu<'a>(
// 2. Check signatures, otherwise drop // 2. Check signatures, otherwise drop
// 3. check content hash, redact if doesn't match // 3. check content hash, redact if doesn't match
let create_event_content = serde_json::from_str::<CreateEventContent>( let create_event_content: RoomCreateEventContent =
create_event.content.get(), serde_json::from_str(create_event.content.get()).map_err(|e| {
)
.map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
"Invalid create event in db.".to_owned() "Invalid create event in db.".to_owned()
})?; })?;
@@ -1172,14 +1166,14 @@ fn handle_outlier_pdu<'a>(
Err(e) => { Err(e) => {
// Drop // Drop
warn!("Dropping bad event {}: {}", event_id, e); warn!("Dropping bad event {}: {}", event_id, e);
return Err("Signature verification failed".to_string()); return Err("Signature verification failed".to_owned());
} }
Ok(ruma::signatures::Verified::Signatures) => { Ok(ruma::signatures::Verified::Signatures) => {
// Redact // Redact
warn!("Calculated hash does not match: {}", event_id); warn!("Calculated hash does not match: {}", event_id);
match ruma::signatures::redact(&value, room_version_id) { match ruma::signatures::redact(&value, room_version_id) {
Ok(obj) => obj, Ok(obj) => obj,
Err(_) => return Err("Redaction failed".to_string()), Err(_) => return Err("Redaction failed".to_owned()),
} }
} }
Ok(ruma::signatures::Verified::All) => value, Ok(ruma::signatures::Verified::All) => value,
@@ -1194,7 +1188,7 @@ fn handle_outlier_pdu<'a>(
let incoming_pdu = serde_json::from_value::<PduEvent>( let incoming_pdu = serde_json::from_value::<PduEvent>(
serde_json::to_value(&val).expect("CanonicalJsonObj is a valid JsonValue"), serde_json::to_value(&val).expect("CanonicalJsonObj is a valid JsonValue"),
) )
.map_err(|_| "Event is not a valid PDU.".to_string())?; .map_err(|_| "Event is not a valid PDU.".to_owned())?;
// 4. fetch any missing auth events doing all checks listed here starting at 1. These are not timeline events // 4. fetch any missing auth events doing all checks listed here starting at 1. These are not timeline events
// 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events" // 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events"
@@ -1252,7 +1246,11 @@ fn handle_outlier_pdu<'a>(
} }
// The original create event must be in the auth events // The original create event must be in the auth events
if auth_events.get(&(EventType::RoomCreate, "".to_owned())) != Some(create_event) { if auth_events
.get(&(EventType::RoomCreate, "".to_owned()))
.map(|a| a.as_ref())
!= Some(create_event)
{
return Err("Incoming event refers to wrong create event.".to_owned()); return Err("Incoming event refers to wrong create event.".to_owned());
} }
@@ -1263,7 +1261,7 @@ fn handle_outlier_pdu<'a>(
db.rooms db.rooms
.get_pdu(&incoming_pdu.auth_events[0]) .get_pdu(&incoming_pdu.auth_events[0])
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
.filter(|maybe_create| maybe_create == create_event) .filter(|maybe_create| **maybe_create == *create_event)
} else { } else {
None None
}; };
@@ -1275,9 +1273,9 @@ fn handle_outlier_pdu<'a>(
None::<PduEvent>, // TODO: third party invite None::<PduEvent>, // TODO: third party invite
|k, s| auth_events.get(&(k.clone(), s.to_owned())), |k, s| auth_events.get(&(k.clone(), s.to_owned())),
) )
.map_err(|_e| "Auth check failed".to_string())? .map_err(|_e| "Auth check failed".to_owned())?
{ {
return Err("Event has failed auth check with auth events.".to_string()); return Err("Event has failed auth check with auth events.".to_owned());
} }
debug!("Validation successful."); debug!("Validation successful.");
@@ -1288,20 +1286,20 @@ fn handle_outlier_pdu<'a>(
.map_err(|_| "Failed to add pdu as outlier.".to_owned())?; .map_err(|_| "Failed to add pdu as outlier.".to_owned())?;
debug!("Added pdu as outlier."); debug!("Added pdu as outlier.");
Ok((incoming_pdu, val)) Ok((Arc::new(incoming_pdu), val))
}) })
} }
#[tracing::instrument(skip(incoming_pdu, val, create_event, origin, db, room_id, pub_key_map))] #[tracing::instrument(skip(incoming_pdu, val, create_event, origin, db, room_id, pub_key_map))]
async fn upgrade_outlier_to_timeline_pdu( async fn upgrade_outlier_to_timeline_pdu(
incoming_pdu: &PduEvent, incoming_pdu: Arc<PduEvent>,
val: BTreeMap<String, CanonicalJsonValue>, val: BTreeMap<String, CanonicalJsonValue>,
create_event: &PduEvent, create_event: &PduEvent,
origin: &ServerName, origin: &ServerName,
db: &Database, db: &Database,
room_id: &RoomId, room_id: &RoomId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> StdResult<Option<Vec<u8>>, String> { ) -> Result<Option<Vec<u8>>, String> {
if let Ok(Some(pduid)) = db.rooms.get_pdu_id(&incoming_pdu.event_id) { if let Ok(Some(pduid)) = db.rooms.get_pdu_id(&incoming_pdu.event_id) {
return Ok(Some(pduid)); return Ok(Some(pduid));
} }
@@ -1314,8 +1312,8 @@ async fn upgrade_outlier_to_timeline_pdu(
return Err("Event has been soft failed".into()); return Err("Event has been soft failed".into());
} }
let create_event_content = let create_event_content: RoomCreateEventContent =
serde_json::from_str::<CreateEventContent>(create_event.content.get()).map_err(|e| { serde_json::from_str(create_event.content.get()).map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
"Invalid create event in db.".to_owned() "Invalid create event in db.".to_owned()
})?; })?;
@@ -1426,20 +1424,16 @@ async fn upgrade_outlier_to_timeline_pdu(
fork_states.push(state); fork_states.push(state);
} }
let event_cache = RefCell::new(HashMap::new());
state_at_incoming_event = match state_res::resolve( state_at_incoming_event = match state_res::resolve(
room_version_id, room_version_id,
&fork_states, &fork_states,
auth_chain_sets, auth_chain_sets,
|id| match event_cache.borrow_mut().entry(id.clone()) { |id| {
hash_map::Entry::Vacant(v) => {
let res = db.rooms.get_pdu(id); let res = db.rooms.get_pdu(id);
if let Err(e) = &res { if let Err(e) = &res {
error!("LOOK AT ME Failed to fetch event: {}", e); error!("LOOK AT ME Failed to fetch event: {}", e);
} }
v.insert(res.ok().flatten().map(Arc::new)).clone() res.ok().flatten()
}
hash_map::Entry::Occupied(o) => o.get().as_ref().map(Arc::clone),
}, },
) { ) {
Ok(new_state) => Some( Ok(new_state) => Some(
@@ -1452,7 +1446,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.map_err(|_| "Failed to get_or_create_shortstatekey".to_owned())?; .map_err(|_| "Failed to get_or_create_shortstatekey".to_owned())?;
Ok((shortstatekey, Arc::new(event_id))) Ok((shortstatekey, Arc::new(event_id)))
}) })
.collect::<StdResult<_, String>>()?, .collect::<Result<_, String>>()?,
), ),
Err(e) => { Err(e) => {
warn!("State resolution on prev events failed, either an event could not be found or deserialization: {}", e); warn!("State resolution on prev events failed, either an event could not be found or deserialization: {}", e);
@@ -1550,7 +1544,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db.rooms db.rooms
.get_pdu(&incoming_pdu.auth_events[0]) .get_pdu(&incoming_pdu.auth_events[0])
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
.filter(|maybe_create| maybe_create == create_event) .filter(|maybe_create| **maybe_create == *create_event)
} else { } else {
None None
}; };
@@ -1634,7 +1628,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(*shortstatekey, id, &db.globals) .compress_state_event(*shortstatekey, id, &db.globals)
.map_err(|_| "Failed to compress_state_event".to_owned()) .map_err(|_| "Failed to compress_state_event".to_owned())
}) })
.collect::<StdResult<_, String>>()?; .collect::<Result<_, _>>()?;
// 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it // 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it
debug!("starting soft fail auth check"); debug!("starting soft fail auth check");
@@ -1754,7 +1748,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(*k, id, &db.globals) .compress_state_event(*k, id, &db.globals)
.map_err(|_| "Failed to compress_state_event.".to_owned()) .map_err(|_| "Failed to compress_state_event.".to_owned())
}) })
.collect::<StdResult<_, String>>()? .collect::<Result<_, _>>()?
} else { } else {
// We do need to force an update to this room's state // We do need to force an update to this room's state
update_state = true; update_state = true;
@@ -1773,7 +1767,7 @@ async fn upgrade_outlier_to_timeline_pdu(
); );
} }
let fork_states = &fork_states let fork_states: Vec<_> = fork_states
.into_iter() .into_iter()
.map(|map| { .map(|map| {
map.into_iter() map.into_iter()
@@ -1784,23 +1778,19 @@ async fn upgrade_outlier_to_timeline_pdu(
}) })
.collect::<Result<StateMap<_>>>() .collect::<Result<StateMap<_>>>()
}) })
.collect::<Result<Vec<_>>>() .collect::<Result<_>>()
.map_err(|_| "Failed to get_statekey_from_short.".to_owned())?; .map_err(|_| "Failed to get_statekey_from_short.".to_owned())?;
let event_cache = RefCell::new(HashMap::new());
let state = match state_res::resolve( let state = match state_res::resolve(
room_version_id, room_version_id,
fork_states, &fork_states,
auth_chain_sets, auth_chain_sets,
|id| match event_cache.borrow_mut().entry(id.clone()) { |id| {
hash_map::Entry::Vacant(v) => {
let res = db.rooms.get_pdu(id); let res = db.rooms.get_pdu(id);
if let Err(e) = &res { if let Err(e) = &res {
error!("LOOK AT ME Failed to fetch event: {}", e); error!("LOOK AT ME Failed to fetch event: {}", e);
} }
v.insert(res.ok().flatten().map(Arc::new)).clone() res.ok().flatten()
}
hash_map::Entry::Occupied(o) => o.get().as_ref().map(Arc::clone),
}, },
) { ) {
Ok(new_state) => new_state, Ok(new_state) => new_state,
@@ -1820,7 +1810,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(shortstatekey, &event_id, &db.globals) .compress_state_event(shortstatekey, &event_id, &db.globals)
.map_err(|_| "Failed to compress state event".to_owned()) .map_err(|_| "Failed to compress state event".to_owned())
}) })
.collect::<StdResult<_, String>>()? .collect::<Result<_, _>>()?
}; };
// Set the new room state to the resolved state // Set the new room state to the resolved state
@@ -1873,7 +1863,7 @@ pub(crate) fn fetch_and_handle_outliers<'a>(
create_event: &'a PduEvent, create_event: &'a PduEvent,
room_id: &'a RoomId, room_id: &'a RoomId,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> AsyncRecursiveType<'a, Vec<(PduEvent, Option<BTreeMap<String, CanonicalJsonValue>>)>> { ) -> AsyncRecursiveType<'a, Vec<(Arc<PduEvent>, Option<BTreeMap<String, CanonicalJsonValue>>)>> {
Box::pin(async move { Box::pin(async move {
let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) { let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) {
hash_map::Entry::Vacant(e) => { hash_map::Entry::Vacant(e) => {
@@ -2040,12 +2030,12 @@ pub(crate) async fn fetch_signing_keys(
trace!("Loading signing keys for {}", origin); trace!("Loading signing keys for {}", origin);
let mut result = db let mut result: BTreeMap<_, _> = db
.globals .globals
.signing_keys_for(origin)? .signing_keys_for(origin)?
.into_iter() .into_iter()
.map(|(k, v)| (k.to_string(), v.key)) .map(|(k, v)| (k.to_string(), v.key))
.collect::<BTreeMap<_, _>>(); .collect();
if contains_all_ids(&result) { if contains_all_ids(&result) {
return Ok(result); return Ok(result);
@@ -2250,14 +2240,10 @@ pub(crate) fn get_auth_chain<'a>(
continue; continue;
} }
let chunk_key = chunk let chunk_key: Vec<u64> = chunk.iter().map(|(short, _)| short).copied().collect();
.iter()
.map(|(short, _)| short)
.copied()
.collect::<Vec<u64>>();
if let Some(cached) = db.rooms.get_auth_chain_from_cache(&chunk_key)? { if let Some(cached) = db.rooms.get_auth_chain_from_cache(&chunk_key)? {
hits += 1; hits += 1;
full_auth_chain.extend(cached.iter().cloned()); full_auth_chain.extend(cached.iter().copied());
continue; continue;
} }
misses += 1; misses += 1;
@@ -2268,7 +2254,7 @@ pub(crate) fn get_auth_chain<'a>(
for (sevent_id, event_id) in chunk { for (sevent_id, event_id) in chunk {
if let Some(cached) = db.rooms.get_auth_chain_from_cache(&[sevent_id])? { if let Some(cached) = db.rooms.get_auth_chain_from_cache(&[sevent_id])? {
hits2 += 1; hits2 += 1;
chunk_cache.extend(cached.iter().cloned()); chunk_cache.extend(cached.iter().copied());
} else { } else {
misses2 += 1; misses2 += 1;
let auth_chain = Arc::new(get_auth_chain_inner(room_id, &event_id, db)?); let auth_chain = Arc::new(get_auth_chain_inner(room_id, &event_id, db)?);
@@ -2569,9 +2555,9 @@ pub fn get_room_state_route(
Ok(get_room_state::v1::Response { Ok(get_room_state::v1::Response {
auth_chain: auth_chain_ids auth_chain: auth_chain_ids
.map(|id| { .map(|id| {
Ok::<_, Error>(PduEvent::convert_to_outgoing_federation_event( db.rooms.get_pdu_json(&id).map(|maybe_json| {
db.rooms.get_pdu_json(&id)?.unwrap(), PduEvent::convert_to_outgoing_federation_event(maybe_json.unwrap())
)) })
}) })
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.collect(), .collect(),
@@ -2655,21 +2641,21 @@ pub fn create_join_event_template_route(
)); ));
} }
let prev_events = db let prev_events: Vec<_> = db
.rooms .rooms
.get_pdu_leaves(&body.room_id)? .get_pdu_leaves(&body.room_id)?
.into_iter() .into_iter()
.take(20) .take(20)
.collect::<Vec<_>>(); .collect();
let create_event = db let create_event = db
.rooms .rooms
.room_state_get(&body.room_id, &EventType::RoomCreate, "")?; .room_state_get(&body.room_id, &EventType::RoomCreate, "")?;
let create_event_content = create_event let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_str::<CreateEventContent>(create_event.content.get()).map_err(|e| { serde_json::from_str(create_event.content.get()).map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
}) })
@@ -2699,8 +2685,7 @@ pub fn create_join_event_template_route(
)); ));
} }
let content = RawValue::from_string( let content = to_raw_value(&RoomMemberEventContent {
serde_json::to_string(&MemberEventContent {
avatar_url: None, avatar_url: None,
blurhash: None, blurhash: None,
displayname: None, displayname: None,
@@ -2709,9 +2694,7 @@ pub fn create_join_event_template_route(
third_party_invite: None, third_party_invite: None,
reason: None, reason: None,
}) })
.expect("member event is valid value"), .expect("member event is valid value");
)
.expect("string is valid");
let state_key = body.user_id.to_string(); let state_key = body.user_id.to_string();
let kind = EventType::RoomMember; let kind = EventType::RoomMember;
@@ -2751,7 +2734,6 @@ pub fn create_join_event_template_route(
.expect("time is valid"), .expect("time is valid"),
kind, kind,
content, content,
parsed_content: RwLock::new(None),
state_key: Some(state_key), state_key: Some(state_key),
prev_events, prev_events,
depth, depth,
@@ -2763,14 +2745,9 @@ pub fn create_join_event_template_route(
unsigned: if unsigned.is_empty() { unsigned: if unsigned.is_empty() {
None None
} else { } else {
Some( Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
RawValue::from_string(
serde_json::to_string(&unsigned).expect("to_string always works"),
)
.expect("string is valid"),
)
}, },
hashes: ruma::events::pdu::EventHash { hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: None, signatures: None,
@@ -2809,10 +2786,7 @@ pub fn create_join_event_template_route(
Ok(create_join_event_template::v1::Response { Ok(create_join_event_template::v1::Response {
room_version: Some(room_version_id), room_version: Some(room_version_id),
event: serde_json::from_value::<Raw<_>>( event: to_raw_value(&pdu_json).expect("CanonicalJson can be serialized to JSON"),
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
)
.expect("Raw::from_value always works"),
} }
.into()) .into())
} }
@@ -2820,7 +2794,7 @@ pub fn create_join_event_template_route(
async fn create_join_event( async fn create_join_event(
db: &DatabaseGuard, db: &DatabaseGuard,
room_id: &RoomId, room_id: &RoomId,
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &RawJsonValue,
) -> Result<RoomState> { ) -> Result<RoomState> {
if !db.globals.allow_federation() { if !db.globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
@@ -2850,7 +2824,7 @@ async fn create_join_event(
} }
}; };
let origin = serde_json::from_value::<Box<ServerName>>( let origin: Box<ServerName> = serde_json::from_value(
serde_json::to_value(value.get("origin").ok_or(Error::BadRequest( serde_json::to_value(value.get("origin").ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Event needs an origin field.", "Event needs an origin field.",
@@ -2957,7 +2931,7 @@ pub async fn create_join_event_v2_route(
#[tracing::instrument(skip(db, body))] #[tracing::instrument(skip(db, body))]
pub async fn create_invite_route( pub async fn create_invite_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<create_invite::v2::Request>, body: Ruma<create_invite::v2::Request<'_>>,
) -> ConduitResult<create_invite::v2::Response> { ) -> ConduitResult<create_invite::v2::Response> {
if !db.globals.allow_federation() { if !db.globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
@@ -3024,14 +2998,12 @@ pub async fn create_invite_route(
let mut invite_state = body.invite_room_state.clone(); let mut invite_state = body.invite_room_state.clone();
let mut event = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>( let mut event: JsonObject = serde_json::from_str(body.event.get())
&body.event.json().to_string(),
)
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event bytes."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event bytes."))?;
event.insert("event_id".to_owned(), "$dummy".into()); event.insert("event_id".to_owned(), "$dummy".into());
let pdu = serde_json::from_value::<PduEvent>(event.into()).map_err(|e| { let pdu: PduEvent = serde_json::from_value(event.into()).map_err(|e| {
warn!("Invalid invite event: {}", e); warn!("Invalid invite event: {}", e);
Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event.") Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event.")
})?; })?;
@@ -3289,13 +3261,45 @@ pub(crate) async fn fetch_required_signing_keys(
// Gets a list of servers for which we don't have the signing key yet. We go over // Gets a list of servers for which we don't have the signing key yet. We go over
// the PDUs and either cache the key or add it to the list that needs to be retrieved. // the PDUs and either cache the key or add it to the list that needs to be retrieved.
pub fn get_server_keys_from_cache( fn get_server_keys_from_cache(
pdu: &CanonicalJsonObject, pdu: &RawJsonValue,
servers: &mut BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>>, servers: &mut BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>>,
room_version: &RoomVersionId,
pub_key_map: &mut RwLockWriteGuard<'_, BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &mut RwLockWriteGuard<'_, BTreeMap<String, BTreeMap<String, String>>>,
db: &Database, db: &Database,
) -> Result<()> { ) -> Result<()> {
let signatures = pdu let value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response")
})?;
let event_id = EventId::try_from(&*format!(
"${}",
ruma::signatures::reference_hash(&value, room_version)
.expect("ruma can calculate reference hashes")
))
.expect("ruma's reference hashes are valid event ids");
if let Some((time, tries)) = db
.globals
.bad_event_ratelimiter
.read()
.unwrap()
.get(&event_id)
{
// Exponential backoff
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
}
if time.elapsed() < min_elapsed_duration {
debug!("Backing off from {}", event_id);
return Err(Error::BadServerResponse("bad event, still backing off"));
}
}
let signatures = value
.get("signatures") .get("signatures")
.ok_or(Error::BadServerResponse( .ok_or(Error::BadServerResponse(
"No signatures in server response pdu.", "No signatures in server response pdu.",
@@ -3325,19 +3329,16 @@ pub fn get_server_keys_from_cache(
trace!("Loading signing keys for {}", origin); trace!("Loading signing keys for {}", origin);
let result = db let result: BTreeMap<_, _> = db
.globals .globals
.signing_keys_for(origin)? .signing_keys_for(origin)?
.into_iter() .into_iter()
.map(|(k, v)| (k.to_string(), v.key)) .map(|(k, v)| (k.to_string(), v.key))
.collect::<BTreeMap<_, _>>(); .collect();
if !contains_all_ids(&result) { if !contains_all_ids(&result) {
trace!("Signing key not loaded for {}", origin); trace!("Signing key not loaded for {}", origin);
servers.insert( servers.insert(origin.clone(), BTreeMap::new());
origin.clone(),
BTreeMap::<ServerSigningKeyId, QueryCriteria>::new(),
);
} }
pub_key_map.insert(origin.to_string(), result); pub_key_map.insert(origin.to_string(), result);
@@ -3347,11 +3348,30 @@ pub fn get_server_keys_from_cache(
} }
pub(crate) async fn fetch_join_signing_keys( pub(crate) async fn fetch_join_signing_keys(
servers: Arc<RwLock<BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>>>>, event: &create_join_event::v2::Response,
room_version: &RoomVersionId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
db: &Database, db: &Database,
) -> Result<()> { ) -> Result<()> {
let mut servers = std::mem::take(&mut *servers.write().unwrap()); let mut servers: BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>> =
BTreeMap::new();
{
let mut pkm = pub_key_map
.write()
.map_err(|_| Error::bad_database("RwLock is poisoned."))?;
// Try to fetch keys, failure is okay
// Servers we couldn't find in the cache will be added to `servers`
for pdu in &event.room_state.state {
let _ = get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm, db);
}
for pdu in &event.room_state.auth_chain {
let _ = get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm, db);
}
drop(pkm);
}
if servers.is_empty() { if servers.is_empty() {
// We had all keys locally // We had all keys locally
@@ -3399,7 +3419,7 @@ pub(crate) async fn fetch_join_signing_keys(
} }
} }
let mut futures = servers let mut futures: FuturesUnordered<_> = servers
.into_iter() .into_iter()
.map(|(server, _)| async move { .map(|(server, _)| async move {
( (
@@ -3413,16 +3433,16 @@ pub(crate) async fn fetch_join_signing_keys(
server, server,
) )
}) })
.collect::<FuturesUnordered<_>>(); .collect();
while let Some(result) = futures.next().await { while let Some(result) = futures.next().await {
if let (Ok(get_keys_response), origin) = result { if let (Ok(get_keys_response), origin) = result {
let result = db let result: BTreeMap<_, _> = db
.globals .globals
.add_signing_key(&origin, get_keys_response.server_key.clone())? .add_signing_key(&origin, get_keys_response.server_key.clone())?
.into_iter() .into_iter()
.map(|(k, v)| (k.to_string(), v.key)) .map(|(k, v)| (k.to_string(), v.key))
.collect::<BTreeMap<_, _>>(); .collect();
pub_key_map pub_key_map
.write() .write()
+1 -1
View File
@@ -123,7 +123,7 @@ pub fn deserialize_from_str<
E: std::fmt::Display, E: std::fmt::Display,
>( >(
deserializer: D, deserializer: D,
) -> std::result::Result<T, D::Error> { ) -> Result<T, D::Error> {
struct Visitor<T: FromStr<Err = E>, E>(std::marker::PhantomData<T>); struct Visitor<T: FromStr<Err = E>, E>(std::marker::PhantomData<T>);
impl<'de, T: FromStr<Err = Err>, Err: std::fmt::Display> serde::de::Visitor<'de> impl<'de, T: FromStr<Err = Err>, Err: std::fmt::Display> serde::de::Visitor<'de>
for Visitor<T, Err> for Visitor<T, Err>
+1
View File
@@ -510,3 +510,4 @@ remote user can join room with version 5
remote user can join room with version 6 remote user can join room with version 6
setting 'm.room.name' respects room powerlevel setting 'm.room.name' respects room powerlevel
setting 'm.room.power_levels' respects room powerlevel setting 'm.room.power_levels' respects room powerlevel
Federation publicRoom Name/topic keys are correct