Compare commits

..

1 Commits

  1. 2
      .dockerignore
  2. 327
      .gitlab-ci.yml
  3. 92
      Cargo.lock
  4. 10
      Cargo.toml
  5. 55
      DEPLOY.md
  6. 132
      Dockerfile
  7. 3
      conduit-example.toml
  8. 43
      docker/README.md
  9. 53
      docker/ci-binaries-packaging.Dockerfile
  10. 6
      docker/healthcheck.sh
  11. 2
      rust-toolchain
  12. 101
      src/client_server/account.rs
  13. 6
      src/client_server/capabilities.rs
  14. 12
      src/client_server/config.rs
  15. 16
      src/client_server/context.rs
  16. 10
      src/client_server/device.rs
  17. 104
      src/client_server/directory.rs
  18. 30
      src/client_server/keys.rs
  19. 198
      src/client_server/membership.rs
  20. 45
      src/client_server/message.rs
  21. 2
      src/client_server/mod.rs
  22. 20
      src/client_server/profile.rs
  23. 44
      src/client_server/push.rs
  24. 9
      src/client_server/redact.rs
  25. 84
      src/client_server/report.rs
  26. 243
      src/client_server/room.rs
  27. 6
      src/client_server/search.rs
  28. 12
      src/client_server/session.rs
  29. 36
      src/client_server/state.rs
  30. 85
      src/client_server/sync.rs
  31. 23
      src/client_server/tag.rs
  32. 55
      src/client_server/voip.rs
  33. 44
      src/database.rs
  34. 4
      src/database/abstraction.rs
  35. 24
      src/database/admin.rs
  36. 45
      src/database/globals.rs
  37. 21
      src/database/key_backups.rs
  38. 6
      src/database/proxy.rs
  39. 36
      src/database/pusher.rs
  40. 542
      src/database/rooms.rs
  41. 59
      src/database/rooms/edus.rs
  42. 37
      src/database/sending.rs
  43. 25
      src/database/uiaa.rs
  44. 54
      src/database/users.rs
  45. 2
      src/error.rs
  46. 6
      src/lib.rs
  47. 8
      src/main.rs
  48. 132
      src/pdu.rs
  49. 12
      src/ruma_wrapper.rs
  50. 346
      src/server_server.rs
  51. 2
      src/utils.rs
  52. 1
      tests/sytest/sytest-whitelist

2
.dockerignore

@ -14,8 +14,6 @@ docker-compose* @@ -14,8 +14,6 @@ docker-compose*
# Git folder
.git
.gitea
.gitlab
.github
# Dot files
.env

327
.gitlab-ci.yml

@ -9,6 +9,7 @@ variables: @@ -9,6 +9,7 @@ variables:
FF_USE_FASTZIP: 1
CACHE_COMPRESSION_LEVEL: fastest
# --------------------------------------------------------------------- #
# Cargo: Compiling for different architectures #
# --------------------------------------------------------------------- #
@ -19,7 +20,7 @@ variables: @@ -19,7 +20,7 @@ variables:
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: '$CI_COMMIT_BRANCH == "next"'
- if: "$CI_COMMIT_TAG"
- if: '$CI_COMMIT_TAG'
interruptible: true
image: "rust:latest"
tags: ["docker"]
@ -27,184 +28,258 @@ variables: @@ -27,184 +28,258 @@ variables:
paths:
- cargohome
- target/
key: "build_cache--$TARGET--$CI_COMMIT_BRANCH--release"
key: "build_cache-$TARGET-release"
variables:
CARGO_PROFILE_RELEASE_LTO: "true"
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "1"
CARGO_PROFILE_RELEASE_LTO=true
CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
before_script:
- 'echo "Building for target $TARGET"'
- 'mkdir -p cargohome && CARGOHOME="cargohome"'
- "rustc --version && cargo --version && rustup show" # Print version info for debugging
- "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging
- 'apt-get update -yqq'
- 'echo "Installing packages: $NEEDED_PACKAGES"'
- "apt-get install -yqq --no-install-recommends $NEEDED_PACKAGES"
- "rustup target add $TARGET"
script:
- time cargo build --target $TARGET --release
- 'cp "target/$TARGET/release/conduit" "conduit-$TARGET"'
- 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"'
artifacts:
expire_in: never
build:release:cargo:x86_64-unknown-linux-musl-with-debug:
extends: .build-cargo-shared-settings
image: messense/rust-musl-cross:x86_64-musl
variables:
CARGO_PROFILE_RELEASE_DEBUG: 2 # Enable debug info for flamegraph profiling
TARGET: "x86_64-unknown-linux-musl"
after_script:
- "mv ./conduit-x86_64-unknown-linux-musl ./conduit-x86_64-unknown-linux-musl-with-debug"
artifacts:
name: "conduit-x86_64-unknown-linux-musl-with-debug"
paths:
- "conduit-x86_64-unknown-linux-musl-with-debug"
expose_as: "Conduit for x86_64-unknown-linux-musl-with-debug"
build:release:cargo:x86_64-unknown-linux-musl:
build:release:cargo:x86_64-unknown-linux-gnu:
extends: .build-cargo-shared-settings
image: messense/rust-musl-cross:x86_64-musl
variables:
TARGET: "x86_64-unknown-linux-musl"
TARGET: "x86_64-unknown-linux-gnu"
artifacts:
name: "conduit-x86_64-unknown-linux-musl"
name: "conduit-x86_64-unknown-linux-gnu"
paths:
- "conduit-x86_64-unknown-linux-musl"
expose_as: "Conduit for x86_64-unknown-linux-musl"
- "conduit-x86_64-unknown-linux-gnu"
expose_as: "Conduit for x86_64-unknown-linux-gnu"
build:release:cargo:arm-unknown-linux-musleabihf:
build:release:cargo:armv7-unknown-linux-gnueabihf:
extends: .build-cargo-shared-settings
image: messense/rust-musl-cross:arm-musleabihf
variables:
TARGET: "arm-unknown-linux-musleabihf"
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:
name: "conduit-arm-unknown-linux-musleabihf"
name: "conduit-armv7-unknown-linux-gnueabihf"
paths:
- "conduit-arm-unknown-linux-musleabihf"
expose_as: "Conduit for arm-unknown-linux-musleabihf"
- "conduit-armv7-unknown-linux-gnueabihf"
expose_as: "Conduit for armv7-unknown-linux-gnueabihf"
build:release:cargo:armv7-unknown-linux-musleabihf:
build:release:cargo:aarch64-unknown-linux-gnu:
extends: .build-cargo-shared-settings
image: messense/rust-musl-cross:armv7-musleabihf
variables:
TARGET: "armv7-unknown-linux-musleabihf"
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:
name: "conduit-armv7-unknown-linux-musleabihf"
name: "conduit-aarch64-unknown-linux-gnu"
paths:
- "conduit-armv7-unknown-linux-musleabihf"
expose_as: "Conduit for armv7-unknown-linux-musleabihf"
- "conduit-aarch64-unknown-linux-gnu"
expose_as: "Conduit for aarch64-unknown-linux-gnu"
build:release:cargo:aarch64-unknown-linux-musl:
build:release:cargo:x86_64-unknown-linux-musl:
extends: .build-cargo-shared-settings
image: messense/rust-musl-cross:aarch64-musl
image: "rust:alpine"
variables:
TARGET: "aarch64-unknown-linux-musl"
TARGET: "x86_64-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:
name: "conduit-aarch64-unknown-linux-musl"
name: "conduit-x86_64-unknown-linux-musl"
paths:
- "conduit-aarch64-unknown-linux-musl"
expose_as: "Conduit for aarch64-unknown-linux-musl"
- "conduit-x86_64-unknown-linux-musl"
expose_as: "Conduit for x86_64-unknown-linux-musl"
.cargo-debug-shared-settings:
extends: ".build-cargo-shared-settings"
rules:
- if: '$CI_COMMIT_BRANCH != "master"'
- if: '$CI_COMMIT_BRANCH'
- if: '$CI_COMMIT_TAG'
cache:
key: "build_cache--$TARGET--$CI_COMMIT_BRANCH--debug"
key: "build_cache-$TARGET-debug"
script:
- "time cargo build --target $TARGET"
- 'mv "target/$TARGET/debug/conduit" "conduit-debug-$TARGET"'
artifacts:
expire_in: 4 weeks
build:debug:cargo:x86_64-unknown-linux-gnu:
extends: ".cargo-debug-shared-settings"
variables:
TARGET: "x86_64-unknown-linux-gnu"
artifacts:
name: "conduit-debug-x86_64-unknown-linux-gnu"
paths:
- "conduit-debug-x86_64-unknown-linux-gnu"
expose_as: "Conduit DEBUG for x86_64-unknown-linux-gnu"
build:debug:cargo:x86_64-unknown-linux-musl:
extends: ".cargo-debug-shared-settings"
image: messense/rust-musl-cross:x86_64-musl
image: "rust:alpine"
variables:
TARGET: "x86_64-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:
name: "conduit-debug-x86_64-unknown-linux-musl"
paths:
- "conduit-debug-x86_64-unknown-linux-musl"
expose_as: "Conduit DEBUG for x86_64-unknown-linux-musl"
# --------------------------------------------------------------------- #
# Cargo: Compiling deb packages for different architectures #
# --------------------------------------------------------------------- #
.build-cargo-deb-shared-settings:
stage: "build"
needs: [ ]
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: '$CI_COMMIT_BRANCH == "next"'
- if: '$CI_COMMIT_TAG'
interruptible: true
image: "rust:latest"
tags: ["docker"]
cache:
paths:
- cargohome
- target/
key: "build_cache-deb-$TARGET"
before_script:
- 'echo "Building debian package for target $TARGET"'
- 'mkdir -p cargohome && CARGOHOME="cargohome"'
- "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging
- 'apt-get update -yqq'
- 'echo "Installing packages: $NEEDED_PACKAGES"'
- "apt-get install -yqq --no-install-recommends $NEEDED_PACKAGES"
- "rustup target add $TARGET"
- "cargo install cargo-deb"
script:
- time cargo deb --target $TARGET
- 'mv target/$TARGET/debian/*.deb "conduit-$TARGET.deb"'
build:cargo-deb:x86_64-unknown-linux-gnu:
extends: .build-cargo-deb-shared-settings
variables:
TARGET: "x86_64-unknown-linux-gnu"
NEEDED_PACKAGES: ""
artifacts:
name: "conduit-x86_64-unknown-linux-gnu.deb"
paths:
- "conduit-x86_64-unknown-linux-gnu.deb"
expose_as: "Debian Package x86_64"
# --------------------------------------------------------------------- #
# Create and publish docker image #
# --------------------------------------------------------------------- #
# Build a docker image by packaging up the x86_64-unknown-linux-musl binary into an alpine image
.docker-shared-settings:
stage: "build docker image"
image: jdrouet/docker-with-buildx:stable
needs: []
interruptible: true
image:
name: "gcr.io/kaniko-project/executor:debug"
entrypoint: [""]
tags: ["docker"]
services:
- docker:dind
needs:
- "build:release:cargo:x86_64-unknown-linux-musl"
- "build:release:cargo:arm-unknown-linux-musleabihf"
- "build:release:cargo:armv7-unknown-linux-musleabihf"
- "build:release:cargo:aarch64-unknown-linux-musl"
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2
PLATFORMS: "linux/arm/v6,linux/arm/v7,linux/arm64,linux/amd64"
DOCKER_FILE: "docker/ci-binaries-packaging.Dockerfile"
cache:
paths:
- docker_cache
key: "$CI_JOB_NAME"
# Configure Kaniko Caching: https://cloud.google.com/build/docs/kaniko-cache
KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=120h --cache-repo $CI_REGISTRY_IMAGE/kaniko-ci-cache"
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Only log in to Dockerhub if the credentials are given:
- if [ -n "${DOCKER_HUB}" ]; then docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_PASSWORD" "$DOCKER_HUB"; fi
- "mkdir -p /kaniko/.docker"
- 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"},\"$DOCKER_HUB\":{\"username\":\"$DOCKER_HUB_USER\",\"password\":\"$DOCKER_HUB_PASSWORD\"}}}" > /kaniko/.docker/config.json'
build:docker:next:
extends: .docker-shared-settings
needs:
- "build:release:cargo:x86_64-unknown-linux-musl"
script:
# 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
- mkdir -p linux/arm/ && mv ./conduit-arm-unknown-linux-musleabihf linux/arm/v6
- mkdir -p linux/arm/ && mv ./conduit-armv7-unknown-linux-musleabihf linux/arm/v7
- mv ./conduit-aarch64-unknown-linux-musl linux/arm64
- 'export CREATED=$(date -u +''%Y-%m-%dT%H:%M:%SZ'') && echo "Docker image creation date: $CREATED"'
# Build and push image:
- >
docker buildx build
--pull
--push
--cache-from=type=local,src=$CI_PROJECT_DIR/docker_cache
--cache-to=type=local,dest=$CI_PROJECT_DIR/docker_cache
--build-arg CREATED=$CREATED
/kaniko/executor
$KANIKO_CACHE_ARGS
--force
--context $CI_PROJECT_DIR
--build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
--build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
--build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
--platform "$PLATFORMS"
--tag "$TAG"
--tag "$TAG-alpine"
--tag "$TAG-commit-$CI_COMMIT_SHORT_SHA"
--file "$DOCKER_FILE" .
docker:next:gitlab:
extends: .docker-shared-settings
--dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile"
--destination "$CI_REGISTRY_IMAGE/conduit:next"
--destination "$CI_REGISTRY_IMAGE/conduit:next-alpine"
--destination "$CI_REGISTRY_IMAGE/conduit:commit-$CI_COMMIT_SHORT_SHA"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next-alpine"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:commit-$CI_COMMIT_SHORT_SHA"
rules:
- if: '$CI_COMMIT_BRANCH == "next"'
variables:
TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:next"
docker:next:dockerhub:
extends: .docker-shared-settings
rules:
- if: '$CI_COMMIT_BRANCH == "next" && $DOCKER_HUB'
variables:
TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
docker:master:gitlab:
build:docker:master:
extends: .docker-shared-settings
needs:
- "build:release:cargo:x86_64-unknown-linux-musl"
script:
- >
/kaniko/executor
$KANIKO_CACHE_ARGS
--context $CI_PROJECT_DIR
--build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
--build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
--build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
--dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile"
--destination "$CI_REGISTRY_IMAGE/conduit:latest"
--destination "$CI_REGISTRY_IMAGE/conduit:latest-alpine"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest-alpine"
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
variables:
TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:latest"
docker:master:dockerhub:
build:docker:tags:
extends: .docker-shared-settings
needs:
- "build:release:cargo:x86_64-unknown-linux-musl"
script:
- >
/kaniko/executor
$KANIKO_CACHE_ARGS
--context $CI_PROJECT_DIR
--build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
--build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
--build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
--dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile"
--destination "$CI_REGISTRY_IMAGE/conduit:$CI_COMMIT_TAG"
--destination "$CI_REGISTRY_IMAGE/conduit:$CI_COMMIT_TAG-alpine"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:$CI_COMMIT_TAG"
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:$CI_COMMIT_TAG-alpine"
rules:
- if: '$CI_COMMIT_BRANCH == "master" && $DOCKER_HUB'
variables:
TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest"
- if: '$CI_COMMIT_TAG'
# --------------------------------------------------------------------- #
# Run tests #
@ -226,20 +301,13 @@ test:cargo: @@ -226,20 +301,13 @@ test:cargo:
before_script:
- mkdir -p $CARGO_HOME && echo "using $CARGO_HOME to cache cargo deps"
- apt-get update -yqq
- apt-get install -yqq --no-install-recommends build-essential libssl-dev pkg-config wget
- apt-get install -yqq --no-install-recommends build-essential libssl-dev pkg-config
- rustup component add clippy rustfmt
- wget "https://faulty-storage.de/gitlab-report"
- chmod +x ./gitlab-report
script:
- rustc --version && cargo --version # Print version info for debugging
- cargo fmt --all -- --check
- "cargo test --color always --workspace --verbose --locked --no-fail-fast -- -Z unstable-options --format json | ./gitlab-report -p test > $CI_PROJECT_DIR/report.xml"
- "cargo clippy --color always --verbose --message-format=json | ./gitlab-report -p clippy > $CI_PROJECT_DIR/gl-code-quality-report.json"
artifacts:
when: always
reports:
junit: report.xml
codequality: gl-code-quality-report.json
- cargo test --workspace --verbose --locked
- cargo clippy
test:sytest:
stage: "test"
@ -262,7 +330,7 @@ test:sytest: @@ -262,7 +330,7 @@ test:sytest:
script:
- "SYTEST_EXIT_CODE=0"
- "/bootstrap.sh conduit || SYTEST_EXIT_CODE=1"
- 'perl /sytest/tap-to-junit-xml.pl --puretap --input /logs/results.tap --output $CI_PROJECT_DIR/sytest.xml "Sytest" && cp /logs/results.tap $CI_PROJECT_DIR/results.tap'
- "perl /sytest/tap-to-junit-xml.pl --puretap --input /logs/results.tap --output $CI_PROJECT_DIR/sytest.xml \"Sytest\" && cp /logs/results.tap $CI_PROJECT_DIR/results.tap"
- "exit $SYTEST_EXIT_CODE"
artifacts:
when: always
@ -272,6 +340,7 @@ test:sytest: @@ -272,6 +340,7 @@ test:sytest:
reports:
junit: "$CI_PROJECT_DIR/sytest.xml"
# --------------------------------------------------------------------- #
# Store binaries as package so they have download urls #
# --------------------------------------------------------------------- #
@ -279,31 +348,25 @@ test:sytest: @@ -279,31 +348,25 @@ test:sytest:
publish:package:
stage: "upload artifacts"
needs:
- "build:release:cargo:x86_64-unknown-linux-gnu"
- "build:release:cargo:armv7-unknown-linux-gnueabihf"
- "build:release:cargo:aarch64-unknown-linux-gnu"
- "build:release:cargo:x86_64-unknown-linux-musl"
- "build:release:cargo:arm-unknown-linux-musleabihf"
- "build:release:cargo:armv7-unknown-linux-musleabihf"
- "build:release:cargo:aarch64-unknown-linux-musl"
# - "build:cargo-deb:x86_64-unknown-linux-gnu"
- "build:cargo-deb:x86_64-unknown-linux-gnu"
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: '$CI_COMMIT_BRANCH == "next"'
- if: "$CI_COMMIT_TAG"
- if: '$CI_COMMIT_TAG'
image: curlimages/curl:latest
tags: ["docker"]
variables:
GIT_STRATEGY: "none" # Don't need a clean copy of the code, we just operate on artifacts
script:
- 'BASE_URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-x86_64-unknown-linux-gnu "${BASE_URL}/conduit-x86_64-unknown-linux-gnu"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-armv7-unknown-linux-gnueabihf "${BASE_URL}/conduit-armv7-unknown-linux-gnueabihf"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-aarch64-unknown-linux-gnu "${BASE_URL}/conduit-aarch64-unknown-linux-gnu"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-x86_64-unknown-linux-musl "${BASE_URL}/conduit-x86_64-unknown-linux-musl"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-arm-unknown-linux-musleabihf "${BASE_URL}/conduit-arm-unknown-linux-musleabihf"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-armv7-unknown-linux-musleabihf "${BASE_URL}/conduit-armv7-unknown-linux-musleabihf"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-aarch64-unknown-linux-musl "${BASE_URL}/conduit-aarch64-unknown-linux-musl"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-x86_64-unknown-linux-gnu.deb "${BASE_URL}/conduit-x86_64-unknown-linux-gnu.deb"'
# Avoid duplicate pipelines
# See: https://docs.gitlab.com/ee/ci/yaml/workflow.html#switch-between-branch-pipelines-and-merge-request-pipelines
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
when: never
- if: "$CI_COMMIT_BRANCH"

92
Cargo.lock generated

@ -245,7 +245,6 @@ dependencies = [ @@ -245,7 +245,6 @@ dependencies = [
"crossbeam",
"directories",
"heed",
"hmac",
"http",
"image",
"jsonwebtoken",
@ -267,7 +266,6 @@ dependencies = [ @@ -267,7 +266,6 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"sha-1",
"sled",
"thiserror",
"thread_local",
@ -430,16 +428,6 @@ dependencies = [ @@ -430,16 +428,6 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crypto-mac"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714"
dependencies = [
"generic-array",
"subtle",
]
[[package]]
name = "curve25519-dalek"
version = "3.2.0"
@ -909,16 +897,6 @@ dependencies = [ @@ -909,16 +897,6 @@ dependencies = [
"libc",
]
[[package]]
name = "hmac"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b"
dependencies = [
"crypto-mac",
"digest",
]
[[package]]
name = "hostname"
version = "0.3.1"
@ -1516,6 +1494,12 @@ dependencies = [ @@ -1516,6 +1494,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "paste"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58"
[[package]]
name = "pear"
version = "0.2.3"
@ -1984,7 +1968,7 @@ dependencies = [ @@ -1984,7 +1968,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.4.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"assign",
"js_int",
@ -2004,8 +1988,8 @@ dependencies = [ @@ -2004,8 +1988,8 @@ dependencies = [
[[package]]
name = "ruma-api"
version = "0.18.5"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
version = "0.18.3"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"bytes",
"http",
@ -2020,8 +2004,8 @@ dependencies = [ @@ -2020,8 +2004,8 @@ dependencies = [
[[package]]
name = "ruma-api-macros"
version = "0.18.5"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
version = "0.18.3"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2032,7 +2016,7 @@ dependencies = [ @@ -2032,7 +2016,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.4.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"ruma-api",
"ruma-common",
@ -2045,8 +2029,8 @@ dependencies = [ @@ -2045,8 +2029,8 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.12.3"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
version = "0.12.2"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"assign",
"bytes",
@ -2066,7 +2050,7 @@ dependencies = [ @@ -2066,7 +2050,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.6.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"indexmap",
"js_int",
@ -2080,8 +2064,8 @@ dependencies = [ @@ -2080,8 +2064,8 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.24.6"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
version = "0.24.5"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"indoc",
"js_int",
@ -2096,8 +2080,8 @@ dependencies = [ @@ -2096,8 +2080,8 @@ dependencies = [
[[package]]
name = "ruma-events-macros"
version = "0.24.6"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
version = "0.24.5"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2108,7 +2092,7 @@ dependencies = [ @@ -2108,7 +2092,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"js_int",
"ruma-api",
@ -2123,9 +2107,9 @@ dependencies = [ @@ -2123,9 +2107,9 @@ dependencies = [
[[package]]
name = "ruma-identifiers"
version = "0.20.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"percent-encoding",
"paste",
"rand 0.8.4",
"ruma-identifiers-macros",
"ruma-identifiers-validation",
@ -2137,7 +2121,7 @@ dependencies = [ @@ -2137,7 +2121,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-macros"
version = "0.20.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"quote",
"ruma-identifiers-validation",
@ -2147,15 +2131,12 @@ dependencies = [ @@ -2147,15 +2131,12 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
dependencies = [
"thiserror",
]
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
[[package]]
name = "ruma-identity-service-api"
version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"js_int",
"ruma-api",
@ -2168,7 +2149,7 @@ dependencies = [ @@ -2168,7 +2149,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"js_int",
"ruma-api",
@ -2183,7 +2164,7 @@ dependencies = [ @@ -2183,7 +2164,7 @@ dependencies = [
[[package]]
name = "ruma-serde"
version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"bytes",
"form_urlencoded",
@ -2197,7 +2178,7 @@ dependencies = [ @@ -2197,7 +2178,7 @@ dependencies = [
[[package]]
name = "ruma-serde-macros"
version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2208,7 +2189,7 @@ dependencies = [ @@ -2208,7 +2189,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.9.0"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"base64 0.13.0",
"ed25519-dalek",
@ -2225,7 +2206,7 @@ dependencies = [ @@ -2225,7 +2206,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
version = "0.4.1"
source = "git+https://github.com/ruma/ruma?rev=16f031fabb7871fcd738b0f25391193ee4ca28a9#16f031fabb7871fcd738b0f25391193ee4ca28a9"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2"
dependencies = [
"itertools 0.10.1",
"js_int",
@ -2437,19 +2418,6 @@ dependencies = [ @@ -2437,19 +2418,6 @@ dependencies = [
"yaml-rust",
]
[[package]]
name = "sha-1"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6"
dependencies = [
"block-buffer",
"cfg-if 1.0.0",
"cpufeatures",
"digest",
"opaque-debug",
]
[[package]]
name = "sha1"
version = "0.6.0"

10
Cargo.toml

@ -19,7 +19,7 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request @@ -19,7 +19,7 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request
# 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 = { git = "https://github.com/ruma/ruma", rev = "16f031fabb7871fcd738b0f25391193ee4ca28a9", 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/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"] }
@ -40,7 +40,7 @@ serde_json = { version = "1.0.67", features = ["raw_value"] } @@ -40,7 +40,7 @@ serde_json = { version = "1.0.67", features = ["raw_value"] }
# Used for appservice registration files
serde_yaml = "0.8.20"
# Used for pdu definition
serde = { version = "1.0.130", features = ["rc"] }
serde = "1.0.130"
# Used for secure identifiers
rand = "0.8.4"
# Used to hash passwords
@ -79,9 +79,6 @@ num_cpus = "1.13.0" @@ -79,9 +79,6 @@ num_cpus = "1.13.0"
threadpool = "1.8.1"
heed = { git = "https://github.com/timokoesters/heed.git", rev = "f6f825da7fb2c758867e05ad973ef800a6fe1d5d", optional = true }
thread_local = "1.1.3"
# used for TURN server authentication
hmac = "0.11.0"
sha-1 = "0.9.8"
[features]
default = ["conduit_bin", "backend_sqlite"]
@ -123,12 +120,13 @@ maintainer-scripts = "debian/" @@ -123,12 +120,13 @@ maintainer-scripts = "debian/"
systemd-units = { unit-name = "matrix-conduit" }
[profile.dev]
lto = 'off'
lto = 'thin'
incremental = true
[profile.release]
lto = 'thin'
incremental = true
codegen-units=32
# If you want to make flamegraphs, enable debug info:
# debug = true

55
DEPLOY.md

@ -2,27 +2,25 @@ @@ -2,27 +2,25 @@
## Getting help
If you run into any problems while setting up Conduit, write an email to `timo@koesters.xyz`, ask us
in `#conduit:matrix.org` or [open an issue on GitLab](https://gitlab.com/famedly/conduit/-/issues/new).
If you run into any problems while setting up Conduit, write an email to `timo@koesters.xyz`, ask us in `#conduit:matrix.org` or [open an issue on GitLab](https://gitlab.com/famedly/conduit/-/issues/new).
## Installing Conduit
Although you might be able to compile Conduit for Windows, we do recommend running it on a linux server. We therefore
only offer Linux binaries.
You may simply download the binary that fits your machine. Run `uname -m` to see what you need. Now copy the right url:
| CPU Architecture | Download stable version |
| ------------------------------------------- | ------------------------------ |
| x84_64 / amd64 (Most servers and computers) | [Download][x84_64-musl-master] |
| armv6 | [Download][armv6-musl-master] |
| armv7 (e.g. Raspberry Pi by default) | [Download][armv7-musl-master] |
| armv8 / aarch64 | [Download][armv8-musl-master] |
| CPU Architecture | GNU (Ubuntu, Debian, ArchLinux, ...) | MUSL (Alpine, ... ) |
| -------------------- | ------------------------------------- | ----------------------- |
| x84_64 / amd64 | [Download][x84_64-gnu] | [Download][x84_64-musl] |
| armv7 (Raspberry Pi) | [Download][armv7-gnu] | - |
| armv8 / aarch64 | [Download][armv8-gnu] | - |
[x84_64-gnu]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-x86_64-unknown-linux-gnu?job=build:release:cargo:x86_64-unknown-linux-gnu
[x84_64-musl]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-x86_64-unknown-linux-musl?job=build:release:cargo:x86_64-unknown-linux-musl
[x84_64-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-x86_64-unknown-linux-musl?job=build:release:cargo:x86_64-unknown-linux-musl
[armv6-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-arm-unknown-linux-musleabihf?job=build:release:cargo:arm-unknown-linux-musleabihf
[armv7-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-armv7-unknown-linux-musleabihf?job=build:release:cargo:armv7-unknown-linux-musleabihf
[armv8-musl-master]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-aarch64-unknown-linux-musl?job=build:release:cargo:aarch64-unknown-linux-musl
[armv7-gnu]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-armv7-unknown-linux-gnueabihf?job=build:release:cargo:armv7-unknown-linux-gnueabihf
[armv8-gnu]: https://gitlab.com/famedly/conduit/-/jobs/artifacts/master/raw/conduit-aarch64-unknown-linux-gnu?job=build:release:cargo:aarch64-unknown-linux-gnu
```bash
$ sudo wget -O /usr/local/bin/matrix-conduit <url>
@ -34,15 +32,15 @@ Alternatively, you may compile the binary yourself using @@ -34,15 +32,15 @@ Alternatively, you may compile the binary yourself using
```bash
$ cargo build --release
```
Note that this currently requires Rust 1.50.
If you want to cross compile Conduit to another architecture, read the [Cross-Compile Guide](CROSS_COMPILE.md).
## Adding a Conduit user
While Conduit can run as any user it is usually better to use dedicated users for different services. This also allows
you to make sure that the file permissions are correctly set up.
While Conduit can run as any user it is usually better to use dedicated users for different services.
This also allows you to make sure that the file permissions are correctly set up.
In Debian you can use this command to create a Conduit user:
@ -52,8 +50,9 @@ sudo adduser --system conduit --no-create-home @@ -52,8 +50,9 @@ sudo adduser --system conduit --no-create-home
## Setting up a systemd service
Now we'll set up a systemd service for Conduit, so it's easy to start/stop Conduit and set it to autostart when your
server reboots. Simply paste the default systemd service you can find below into
Now we'll set up a systemd service for Conduit, so it's easy to start/stop
Conduit and set it to autostart when your server reboots. Simply paste the
default systemd service you can find below into
`/etc/systemd/system/conduit.service`.
```systemd
@ -78,10 +77,10 @@ Finally, run @@ -78,10 +77,10 @@ Finally, run
$ sudo systemctl daemon-reload
```
## Creating the Conduit configuration file
Now we need to create the Conduit's config file in `/etc/matrix-conduit/conduit.toml`. Paste this in **and take a moment
to read it. You need to change at least the server name.**
Now we need to create the Conduit's config file in `/etc/matrix-conduit/conduit.toml`. Paste this in **and take a moment to read it. You need to change at least the server name.**
```toml
[global]
@ -129,8 +128,8 @@ address = "127.0.0.1" # This makes sure Conduit can only be reached using the re @@ -129,8 +128,8 @@ address = "127.0.0.1" # This makes sure Conduit can only be reached using the re
## Setting the correct file permissions
As we are using a Conduit specific user we need to allow it to read the config. To do that you can run this command on
Debian:
As we are using a Conduit specific user we need to allow it to read the config.
To do that you can run this command on Debian:
```bash
sudo chown -R conduit:nogroup /etc/matrix-conduit
@ -143,6 +142,7 @@ sudo mkdir -p /var/lib/matrix-conduit/conduit_db @@ -143,6 +142,7 @@ sudo mkdir -p /var/lib/matrix-conduit/conduit_db
sudo chown -R conduit:nogroup /var/lib/matrix-conduit/conduit_db
```
## Setting up the Reverse Proxy
This depends on whether you use Apache, Nginx or another web server.
@ -171,9 +171,11 @@ ProxyPassReverse /_matrix/ http://127.0.0.1:6167/_matrix/ @@ -171,9 +171,11 @@ ProxyPassReverse /_matrix/ http://127.0.0.1:6167/_matrix/
$ sudo systemctl reload apache2
```
### Nginx
If you use Nginx and not Apache, add the following server section inside the http section of `/etc/nginx/nginx.conf`
If you use Nginx and not Apache, add the following server section inside the
http section of `/etc/nginx/nginx.conf`
```nginx
server {
@ -196,13 +198,13 @@ server { @@ -196,13 +198,13 @@ server {
include /etc/letsencrypt/options-ssl-nginx.conf;
}
```
**You need to make some edits again.** When you are done, run
```bash
$ sudo systemctl reload nginx
```
## SSL Certificate
The easiest way to get an SSL certificate, if you don't have one already, is to install `certbot` and run this:
@ -211,6 +213,7 @@ The easiest way to get an SSL certificate, if you don't have one already, is to @@ -211,6 +213,7 @@ The easiest way to get an SSL certificate, if you don't have one already, is to
$ sudo certbot -d your.server.name
```
## You're done!
Now you can start Conduit with:

132
Dockerfile

@ -1,65 +1,75 @@ @@ -1,65 +1,75 @@
# syntax=docker/dockerfile:1
FROM docker.io/rust:1.53-alpine AS builder
WORKDIR /usr/src/conduit
# Using multistage build:
# https://docs.docker.com/develop/develop-images/multistage-build/
# https://whitfin.io/speeding-up-rust-docker-builds/
# Install required packages to build Conduit and it's dependencies
RUN apk add musl-dev
# == 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 && touch src/lib.rs && echo 'fn main() {}' > src/main.rs
COPY Cargo.toml Cargo.lock ./
RUN cargo build --release && rm -r src
########################## BUILD IMAGE ##########################
# Alpine build image to build Conduit's statically compiled binary
FROM alpine:3.14 as builder
# Copy over actual Conduit sources
COPY src src
# main.rs and lib.rs need their 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).
#
# Builds conduit and places the binary at /usr/src/conduit/target/release/conduit
RUN touch src/main.rs && touch src/lib.rs && cargo build --release
# ---------------------------------------------------------------------------------------------------------------
# Stuff below this line actually ends up in the resulting docker image
# ---------------------------------------------------------------------------------------------------------------
FROM docker.io/alpine:3.15.0 AS runner
# Standard port on which Conduit launches.
# You still need to map the port when using the docker command or docker-compose.
EXPOSE 6167
# 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
ARG CREATED
ARG VERSION
ARG GIT_REF=origin/master
# Note from @jfowl: I would like to remove this in the future and just have the Docker version be configured with envs.
ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml"
# Conduit needs:
# ca-certificates: for https
# libgcc: Apparently this is needed, even if I (@jfowl) don't know exactly why. But whatever, it's not that big.
RUN apk add --no-cache \
ca-certificates \
libgcc
# 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
# Created directory for the database and media files
# 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/
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
# Copy over the actual Conduit binary from the builder stage
COPY --from=builder /usr/src/conduit/target/release/conduit /srv/conduit/conduit
# Improve security: Don't run stuff as root, that does not need to run as root:
# Add www-data user and group with UID 82, as used by alpine
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
RUN set -x ; \
@ -69,13 +79,19 @@ RUN set -x ; \ @@ -69,13 +79,19 @@ RUN set -x ; \
# Change ownership of Conduit files to www-data user and group
RUN chown -cR www-data:www-data /srv/conduit
RUN chmod +x /srv/conduit/healthcheck.sh
# Change user to www-data
# Install packages needed to run Conduit
RUN apk add --no-cache \
ca-certificates \
curl \
libgcc
# 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
USER www-data
# Set container home directory
WORKDIR /srv/conduit
# Run Conduit and print backtraces on panics
ENV RUST_BACKTRACE=1
# Run Conduit
ENTRYPOINT [ "/srv/conduit/conduit" ]

3
conduit-example.toml

@ -39,8 +39,7 @@ trusted_servers = ["matrix.org"] @@ -39,8 +39,7 @@ trusted_servers = ["matrix.org"]
#log = "info,state_res=warn,rocket=off,_=off,sled=off"
#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 = "0.0.0.0" # If Conduit is running in a container, make sure the reverse proxy (ie. Traefik) can reach it.
address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy, if you are running conduit from docker, set this to 0.0.0.0
proxy = "none" # more examples can be found at src/database/proxy.rs:6

43
docker/README.md

@ -2,22 +2,38 @@ @@ -2,22 +2,38 @@
> **Note:** To run and use Conduit you should probably use it with a Domain or Subdomain behind a reverse proxy (like Nginx, Traefik, Apache, ...) with a Lets Encrypt certificate.
## Docker
### Build & Dockerfile
The Dockerfile provided by Conduit has two stages, each of which creates an image.
1. **Builder:** Builds the binary from local context or by cloning a git revision from the official repository.
2. **Runner:** Copies the built binary from **Builder** and sets up the runtime environment, like creating a volume to persist the database and applying the correct permissions.
2. **Runtime:** Copies the built binary from **Builder** and sets up the runtime environment, like creating a volume to persist the database and applying the correct permissions.
The Dockerfile includes a few build arguments that should be supplied when building it.
``` Dockerfile
ARG LOCAL=false
ARG CREATED
ARG VERSION
ARG GIT_REF=origin/master
```
- **CREATED:** Date and time as string (date-time as defined by RFC 3339). Will be used to create the Open Container Initiative compliant label `org.opencontainers.image.created`. Supply by it like this `$(date -u +'%Y-%m-%dT%H:%M:%SZ')`
- **VERSION:** The SemVer version of Conduit, which is in the image. Will be used to create the Open Container Initiative compliant label `org.opencontainers.image.version`. If you have a `Cargo.toml` in your build context, you can get it with `$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)`
- **LOCAL:** *(Optional)* A boolean value, specifies if the local build context should be used, or if the official repository will be cloned. If not supplied with the build command, it will default to `false`.
- **GIT_REF:** *(Optional)* A git ref, like `HEAD` or a commit ID. The supplied ref will be used to create the Open Container Initiative compliant label `org.opencontainers.image.revision` and will be the ref that is cloned from the repository when not building from the local context. If not supplied with the build command, it will default to `origin/master`.
To build the image you can use the following command
``` bash
docker build --tag matrixconduit/matrix-conduit:latest .
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)
```
which also will tag the resulting image as `matrixconduit/matrix-conduit:latest`.
**Note:** it ommits the two optional `build-arg`s.
### Run
@ -30,13 +46,9 @@ docker run -d -p 8448:6167 -v ~/conduit.toml:/srv/conduit/conduit.toml -v db:/sr @@ -30,13 +46,9 @@ docker run -d -p 8448:6167 -v ~/conduit.toml:/srv/conduit/conduit.toml -v db:/sr
or you can skip the build step and pull the image from one of the following registries:
| Registry | Image | Size |
| --------------- | --------------------------------------------------------------- | --------------------- |
| Docker Hub | [matrixconduit/matrix-conduit:latest][dh] | ![Image Size][shield] |
| GitLab Registry | [registry.gitlab.com/famedly/conduit/matrix-conduit:latest][gl] | ![Image Size][shield] |
[dh]: https://hub.docker.com/r/matrixconduit/matrix-conduit
[gl]: https://gitlab.com/famedly/conduit/container_registry/
[shield]: https://img.shields.io/docker/image-size/matrixconduit/matrix-conduit/latest
| --------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| Docker Hub | [matrixconduit/matrix-conduit:latest](https://hub.docker.com/r/matrixconduit/matrix-conduit) | ![Image Size](https://img.shields.io/docker/image-size/matrixconduit/matrix-conduit/latest) |
| GitLab Registry | [registry.gitlab.com/famedly/conduit/conduit:latest](https://gitlab.com/famedly/conduit/container_registry/2134341) | ![Image Size](https://img.shields.io/docker/image-size/matrixconduit/matrix-conduit/latest) |
The `-d` flag lets the container run in detached mode. You now need to supply a `conduit.toml` config file, an example can be found [here](../conduit-example.toml).
You can pass in different env vars to change config values on the fly. You can even configure Conduit completely by using env vars, but for that you need
@ -44,20 +56,23 @@ to pass `-e CONDUIT_CONFIG=""` into your container. For an overview of possible @@ -44,20 +56,23 @@ to pass `-e CONDUIT_CONFIG=""` into your container. For an overview of possible
If you just want to test Conduit for a short time, you can use the `--rm` flag, which will clean up everything related to your container after you stop it.
## Docker-compose
If the docker command is not for you or your setup, you can also use one of the provided `docker-compose` files. Depending on your proxy setup, use the [`docker-compose.traefik.yml`](docker-compose.traefik.yml) and [`docker-compose.override.traefik.yml`](docker-compose.override.traefik.yml) for Traefik (don't forget to remove `.traefik` from the filenames) or the normal [`docker-compose.yml`](../docker-compose.yml) for every other reverse proxy. Additional info about deploying
Conduit can be found [here](../DEPLOY.md).
### Build
To build the Conduit image with docker-compose, you first need to open and modify the `docker-compose.yml` file. There you need to comment the `image:` option and uncomment the `build:` option. Then call docker-compose with:
``` bash
docker-compose up
CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ') VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml) docker-compose up
```
This will also start the container right afterwards, so if want it to run in detached mode, you also should use the `-d` flag.
This will also start the container right afterwards, so if want it to run in detached mode, you also should use the `-d` flag. For possible `build-args`, please take a look at the above `Build & Dockerfile` section.
### Run
@ -86,9 +101,7 @@ So...step by step: @@ -86,9 +101,7 @@ So...step by step:
3. Create the `conduit.toml` config file, an example can be found [here](../conduit-example.toml), or set `CONDUIT_CONFIG=""` and configure Conduit per env vars.
4. Uncomment the `element-web` service if you want to host your own Element Web Client and create a `element_config.json`.
5. Create the files needed by the `well-known` service.
- `./nginx/matrix.conf` (relative to the compose file, you can change this, but then also need to change the volume mapping)
```nginx
server {
server_name <SUBDOMAIN>.<DOMAIN>;
@ -101,7 +114,6 @@ So...step by step: @@ -101,7 +114,6 @@ So...step by step:
}
}
```
- `./nginx/www/.well-known/matrix/client` (relative to the compose file, you can change this, but then also need to change the volume mapping)
```json
{
@ -116,6 +128,5 @@ So...step by step: @@ -116,6 +128,5 @@ So...step by step:
"m.server": "<SUBDOMAIN>.<DOMAIN>:443"
}
```
6. Run `docker-compose up -d`
7. Connect to your homeserver with your preferred client and create a user. You should do this immediatly after starting Conduit, because the first created user is the admin.

53
docker/ci-binaries-packaging.Dockerfile

@ -1,32 +1,26 @@ @@ -1,32 +1,26 @@
# syntax=docker/dockerfile:1
# ---------------------------------------------------------------------------------------------------------
# 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 jobs.
# It does not build Conduit in Docker, but just copies the matching build artifact from the build job.
# As a consequence, this is not a multiarch capable image. It always expects and packages a x86_64 binary.
#
# It is mostly based on the normal Conduit Dockerfile, but adjusted in a few places to maximise caching.
# Credit's for the original Dockerfile: Weasy666.
# ---------------------------------------------------------------------------------------------------------
FROM docker.io/alpine:3.15.0 AS runner
FROM alpine:3.14
# Standard port on which Conduit launches.
# You still need to map the port when using the docker command or docker-compose.
EXPOSE 6167
# Note from @jfowl: I would like to remove this in the future and just have the Docker version be configured with envs.
ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml"
# Conduit needs:
# ca-certificates: for https
# libgcc: Apparently this is needed, even if I (@jfowl) don't know exactly why. But whatever, it's not that big.
# Install packages needed to run Conduit
RUN apk add --no-cache \
ca-certificates \
curl \
libgcc
ARG CREATED
ARG VERSION
ARG GIT_REF
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} \
@ -39,24 +33,19 @@ LABEL org.opencontainers.image.created=${CREATED} \ @@ -39,24 +33,19 @@ LABEL org.opencontainers.image.created=${CREATED} \
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="https://gitlab.com/famedly/conduit" \
org.opencontainers.image.documentation="" \
org.opencontainers.image.ref.name=""
# Created directory for the database and media files
RUN mkdir -p /srv/conduit/.local/share/conduit
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose.
EXPOSE 6167
# 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
# create data folder for database
RUN mkdir -p /srv/conduit/.local/share/conduit
# Copy the Conduit binary into the image at the latest possible moment to maximise caching:
COPY ./conduit-x86_64-unknown-linux-musl /srv/conduit/conduit
COPY ./docker/healthcheck.sh /srv/conduit/
# Improve security: Don't run stuff as root, that does not need to run as root:
# Add www-data user and group with UID 82, as used by alpine
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
RUN set -x ; \
@ -68,11 +57,13 @@ RUN set -x ; \ @@ -68,11 +57,13 @@ RUN set -x ; \
RUN chown -cR www-data:www-data /srv/conduit
RUN chmod +x /srv/conduit/healthcheck.sh
# Change user to www-data
# 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
USER www-data
# Set container home directory
WORKDIR /srv/conduit
# Run Conduit and print backtraces on panics
ENV RUST_BACKTRACE=1
# Run Conduit
ENTRYPOINT [ "/srv/conduit/conduit" ]

6
docker/healthcheck.sh

@ -7,7 +7,7 @@ fi @@ -7,7 +7,7 @@ fi
# The actual health check.
# We try to first get a response on HTTP and when that fails on HTTPS and when that fails, we exit with code 1.
# TODO: Change this to a single wget call. Do we have a config value that we can check for that?
wget --no-verbose --tries=1 --spider "http://localhost:${CONDUIT_PORT}/_matrix/client/versions" || \
wget --no-verbose --tries=1 --spider "https://localhost:${CONDUIT_PORT}/_matrix/client/versions" || \
# TODO: Change this to a single curl call. Do we have a config value that we can check for that?
curl --fail -s "http://localhost:${CONDUIT_PORT}/_matrix/client/versions" || \
curl -k --fail -s "https://localhost:${CONDUIT_PORT}/_matrix/client/versions" || \
exit 1

2
rust-toolchain

@ -1 +1 @@ @@ -1 +1 @@
1.53
1.52

101
src/client_server/account.rs

@ -1,4 +1,8 @@ @@ -1,4 +1,8 @@
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
sync::Arc,
};
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
@ -7,31 +11,23 @@ use ruma::{ @@ -7,31 +11,23 @@ use ruma::{
error::ErrorKind,
r0::{
account::{
change_password, deactivate, get_3pids, get_username_availability, register,
whoami, ThirdPartyIdRemovalStatus,
change_password, deactivate, get_username_availability, register, whoami,
ThirdPartyIdRemovalStatus,
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
contact::get_contacts,
uiaa::{AuthFlow, UiaaInfo},
},
},
events::{
room::{
canonical_alias::RoomCanonicalAliasEventContent,
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,
canonical_alias, guest_access, history_visibility, join_rules, member, message, name,
topic,
},
EventType,
},
identifiers::RoomName,
push, RoomAliasId, RoomId, RoomVersionId, UserId,
};
use serde_json::value::to_raw_value;
use tracing::info;
use register::RegistrationKind;
@ -151,7 +147,7 @@ pub async fn register_route( @@ -151,7 +147,7 @@ pub async fn register_route(
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Dummy],
stages: vec!["m.login.dummy".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),
@ -274,16 +270,16 @@ pub async fn register_route( @@ -274,16 +270,16 @@ pub async fn register_route(
);
let state_lock = mutex_state.lock().await;
let mut content = RoomCreateEventContent::new(conduit_user.clone());
let mut content = ruma::events::room::create::CreateEventContent::new(conduit_user.clone());
content.federate = true;
content.predecessor = None;
content.room_version = RoomVersionId::V6;
content.room_version = RoomVersionId::Version6;
// 1. The room create event
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomCreate,
content: to_raw_value(&content).expect("event is valid, we just created it"),
content: serde_json::to_value(content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
redacts: None,
@ -298,15 +294,14 @@ pub async fn register_route( @@ -298,15 +294,14 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
@ -327,10 +322,12 @@ pub async fn register_route( @@ -327,10 +322,12 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomPowerLevels,
content: to_raw_value(&RoomPowerLevelsEventContent {
content: serde_json::to_value(
ruma::events::room::power_levels::PowerLevelsEventContent {
users,
..Default::default()
})
},
)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -346,7 +343,9 @@ pub async fn register_route( @@ -346,7 +343,9 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomJoinRules,
content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
content: serde_json::to_value(join_rules::JoinRulesEventContent::new(
join_rules::JoinRule::Invite,
))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -362,9 +361,11 @@ pub async fn register_route( @@ -362,9 +361,11 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomHistoryVisibility,
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
HistoryVisibility::Shared,
))
content: serde_json::to_value(
history_visibility::HistoryVisibilityEventContent::new(
history_visibility::HistoryVisibility::Shared,
),
)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -380,7 +381,9 @@ pub async fn register_route( @@ -380,7 +381,9 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomGuestAccess,
content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
content: serde_json::to_value(guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::Forbidden,
))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -393,12 +396,13 @@ pub async fn register_route( @@ -393,12 +396,13 @@ pub async fn register_route(
)?;
// 6. Events implied by name and topic
let room_name = RoomName::parse(format!("{} Admin Room", db.globals.server_name()))
let room_name =
Box::<RoomName>::try_from(format!("{} Admin Room", db.globals.server_name()))
.expect("Room name is valid");
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(Some(room_name)))
content: serde_json::to_value(name::NameEventContent::new(Some(room_name)))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -413,7 +417,7 @@ pub async fn register_route( @@ -413,7 +417,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomTopic,
content: to_raw_value(&RoomTopicEventContent {
content: serde_json::to_value(topic::TopicEventContent {
topic: format!("Manage {}", db.globals.server_name()),
})
.expect("event is valid, we just created it"),
@ -428,14 +432,14 @@ pub async fn register_route( @@ -428,14 +432,14 @@ pub async fn register_route(
)?;
// Room alias
let alias: Box<RoomAliasId> = format!("#admins:{}", db.globals.server_name())
let alias: RoomAliasId = format!("#admins:{}", db.globals.server_name())
.try_into()
.expect("#admins:server_name is a valid alias name");
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomCanonicalAlias,
content: to_raw_value(&RoomCanonicalAliasEventContent {
content: serde_json::to_value(canonical_alias::CanonicalAliasEventContent {
alias: Some(alias.clone()),
alt_aliases: Vec::new(),
})
@ -456,15 +460,14 @@ pub async fn register_route( @@ -456,15 +460,14 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Invite,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
@ -479,15 +482,14 @@ pub async fn register_route( @@ -479,15 +482,14 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: Some(displayname),
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
@ -504,7 +506,7 @@ pub async fn register_route( @@ -504,7 +506,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMessage,
content: to_raw_value(&RoomMessageEventContent::text_html(
content: serde_json::to_value(message::MessageEventContent::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(),
"<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(),
))
@ -560,7 +562,7 @@ pub async fn change_password_route( @@ -560,7 +562,7 @@ pub async fn change_password_route(
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
stages: vec!["m.login.password".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),
@ -652,7 +654,7 @@ pub async fn deactivate_route( @@ -652,7 +654,7 @@ pub async fn deactivate_route(
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
stages: vec!["m.login.password".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),
@ -696,15 +698,14 @@ pub async fn deactivate_route( @@ -696,15 +698,14 @@ pub async fn deactivate_route(
for room_id in all_rooms {
let room_id = room_id?;
let event = RoomMemberEventContent {
membership: MembershipState::Leave,
let event = member::MemberEventContent {
membership: member::MembershipState::Leave,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
};
let mutex_state = Arc::clone(
@ -720,7 +721,7 @@ pub async fn deactivate_route( @@ -720,7 +721,7 @@ pub async fn deactivate_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
content: serde_json::to_value(event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(sender_user.to_string()),
redacts: None,
@ -755,9 +756,9 @@ pub async fn deactivate_route( @@ -755,9 +756,9 @@ pub async fn deactivate_route(
get("/_matrix/client/r0/account/3pid", data = "<body>")
)]
pub async fn third_party_route(
body: Ruma<get_3pids::Request>,
) -> ConduitResult<get_3pids::Response> {
body: Ruma<get_contacts::Request>,
) -> ConduitResult<get_contacts::Response> {
let _sender_user = body.sender_user.as_ref().expect("user is authenticated");
Ok(get_3pids::Response::new(Vec::new()).into())
Ok(get_contacts::Response::new(Vec::new()).into())
}

6
src/client_server/capabilities.rs

@ -22,12 +22,12 @@ pub async fn get_capabilities_route( @@ -22,12 +22,12 @@ pub async fn get_capabilities_route(
_body: Ruma<get_capabilities::Request>,
) -> ConduitResult<get_capabilities::Response> {
let mut available = BTreeMap::new();
available.insert(RoomVersionId::V5, RoomVersionStability::Stable);
available.insert(RoomVersionId::V6, RoomVersionStability::Stable);
available.insert(RoomVersionId::Version5, RoomVersionStability::Stable);
available.insert(RoomVersionId::Version6, RoomVersionStability::Stable);
let mut capabilities = Capabilities::new();
capabilities.room_versions = RoomVersionsCapability {
default: RoomVersionId::V6,
default: RoomVersionId::Version6,
available,
};

12
src/client_server/config.rs

@ -30,7 +30,7 @@ pub async fn set_global_account_data_route( @@ -30,7 +30,7 @@ pub async fn set_global_account_data_route(
) -> ConduitResult<set_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data: serde_json::Value = serde_json::from_str(body.data.get())
let data = serde_json::from_str::<serde_json::Value>(body.data.get())
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
let event_type = body.event_type.to_string();
@ -68,7 +68,7 @@ pub async fn set_room_account_data_route( @@ -68,7 +68,7 @@ pub async fn set_room_account_data_route(
) -> ConduitResult<set_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data: serde_json::Value = serde_json::from_str(body.data.get())
let data = serde_json::from_str::<serde_json::Value>(body.data.get())
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
let event_type = body.event_type.to_string();
@ -103,9 +103,9 @@ pub async fn get_global_account_data_route( @@ -103,9 +103,9 @@ pub async fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: Box<RawJsonValue> = db
let event = db
.account_data
.get(None, sender_user, body.event_type.clone().into())?
.get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
@ -132,9 +132,9 @@ pub async fn get_room_account_data_route( @@ -132,9 +132,9 @@ pub async fn get_room_account_data_route(
) -> ConduitResult<get_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: Box<RawJsonValue> = db
let event = db
.account_data
.get(
.get::<Box<RawJsonValue>>(
Some(&body.room_id),
sender_user,
body.event_type.clone().into(),

16
src/client_server/context.rs

@ -48,7 +48,7 @@ pub async fn get_context_route( @@ -48,7 +48,7 @@ pub async fn get_context_route(
))?
.to_room_event();
let events_before: Vec<_> = db
let events_before = db
.rooms
.pdus_until(sender_user, &body.room_id, base_token)?
.take(
@ -58,19 +58,19 @@ pub async fn get_context_route( @@ -58,19 +58,19 @@ pub async fn get_context_route(
/ 2,
)
.filter_map(|r| r.ok()) // Remove buggy events
.collect();
.collect::<Vec<_>>();
let start_token = events_before
.last()
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
.map(|count| count.to_string());
let events_before: Vec<_> = events_before
let events_before = events_before
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect();
.collect::<Vec<_>>();
let events_after: Vec<_> = db
let events_after = db
.rooms
.pdus_after(sender_user, &body.room_id, base_token)?
.take(
@ -80,17 +80,17 @@ pub async fn get_context_route( @@ -80,17 +80,17 @@ pub async fn get_context_route(
/ 2,
)
.filter_map(|r| r.ok()) // Remove buggy events
.collect();
.collect::<Vec<_>>();
let end_token = events_after
.last()
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
.map(|count| count.to_string());
let events_after: Vec<_> = events_after
let events_after = events_after
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect();
.collect::<Vec<_>>();
let mut resp = get_context::Response::new();
resp.start = start_token;

10
src/client_server/device.rs

@ -3,7 +3,7 @@ use ruma::api::client::{ @@ -3,7 +3,7 @@ use ruma::api::client::{
error::ErrorKind,
r0::{
device::{self, delete_device, delete_devices, get_device, get_devices, update_device},
uiaa::{AuthFlow, AuthType, UiaaInfo},
uiaa::{AuthFlow, UiaaInfo},
},
};
@ -25,11 +25,11 @@ pub async fn get_devices_route( @@ -25,11 +25,11 @@ pub async fn get_devices_route(
) -> ConduitResult<get_devices::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let devices: Vec<device::Device> = db
let devices = db
.users
.all_devices_metadata(sender_user)
.filter_map(|r| r.ok()) // Filter out buggy devices
.collect();
.collect::<Vec<device::Device>>();
Ok(get_devices::Response { devices }.into())
}
@ -109,7 +109,7 @@ pub async fn delete_device_route( @@ -109,7 +109,7 @@ pub async fn delete_device_route(
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
stages: vec!["m.login.password".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),
@ -172,7 +172,7 @@ pub async fn delete_devices_route( @@ -172,7 +172,7 @@ pub async fn delete_devices_route(
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
stages: vec!["m.login.password".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),

104
src/client_server/directory.rs

@ -17,16 +17,10 @@ use ruma::{ @@ -17,16 +17,10 @@ use ruma::{
},
directory::{Filter, IncomingFilter, IncomingRoomNetwork, PublicRoomsChunk, RoomNetwork},
events::{
room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
name::RoomNameEventContent,
topic::RoomTopicEventContent,
},
room::{avatar, canonical_alias, guest_access, history_visibility, name, topic},
EventType,
},
serde::Raw,
ServerName, UInt,
};
use tracing::{info, warn};
@ -167,7 +161,7 @@ pub(crate) async fn get_public_rooms_filtered_helper( @@ -167,7 +161,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
other_server,
federation::directory::get_public_rooms_filtered::v1::Request {
limit,
since,
since: since.as_deref(),
filter: Filter {
generic_search_term: filter.generic_search_term.as_deref(),
},
@ -223,8 +217,8 @@ pub(crate) async fn get_public_rooms_filtered_helper( @@ -223,8 +217,8 @@ pub(crate) async fn get_public_rooms_filtered_helper(
}
}
let mut all_rooms: Vec<_> = db
.rooms
let mut all_rooms =
db.rooms
.public_rooms()
.map(|room_id| {
let room_id = room_id?;
@ -234,22 +228,30 @@ pub(crate) async fn get_public_rooms_filtered_helper( @@ -234,22 +228,30 @@ pub(crate) async fn get_public_rooms_filtered_helper(
canonical_alias: db
.rooms
.room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomCanonicalAliasEventContent| c.alias)
.map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::<
Raw<canonical_alias::CanonicalAliasEventContent>,
>(s.content.clone())
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid canonical alias event in database.")
})
})?
.alias)
})?,
name: db
.rooms
.room_state_get(&room_id, &EventType::RoomName, "")?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomNameEventContent| c.name)
.map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::<Raw<name::NameEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid room name event in database.")
})
})?
.name)
})?,
num_joined_members: db
.rooms
@ -263,48 +265,68 @@ pub(crate) async fn get_public_rooms_filtered_helper( @@ -263,48 +265,68 @@ pub(crate) async fn get_public_rooms_filtered_helper(
topic: db
.rooms
.room_state_get(&room_id, &EventType::RoomTopic, "")?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomTopicEventContent| Some(c.topic))
.map_or(Ok::<_, Error>(None), |s| {
Ok(Some(
serde_json::from_value::<Raw<topic::TopicEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid room topic event in database.")
})
})?
.topic,
))
})?,
world_readable: db
.rooms
.room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")?
.map_or(Ok(false), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomHistoryVisibilityEventContent| {
c.history_visibility == HistoryVisibility::WorldReadable
})
.map_or(Ok::<_, Error>(false), |s| {
Ok(serde_json::from_value::<
Raw<history_visibility::HistoryVisibilityEventContent>,
>(s.content.clone())
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
})?
.history_visibility
== history_visibility::HistoryVisibility::WorldReadable)
})?,
guest_can_join: db
.rooms
.room_state_get(&room_id, &EventType::RoomGuestAccess, "")?
.map_or(Ok(false), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomGuestAccessEventContent| {
c.guest_access == GuestAccess::CanJoin
})
.map_or(Ok::<_, Error>(false), |s| {
Ok(
serde_json::from_value::<Raw<guest_access::GuestAccessEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid room guest access event in database.")
})
})?
.guest_access
== guest_access::GuestAccess::CanJoin,
)
})?,
avatar_url: db
.rooms
.room_state_get(&room_id, &EventType::RoomAvatar, "")?
.map(|s| {
serde_json::from_str(s.content.get())
.map(|c: RoomAvatarEventContent| c.url)
Ok::<_, Error>(
serde_json::from_value::<Raw<avatar::AvatarEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid room avatar event in database.")
})
})?
.url,
)
})
.transpose()?
// url is now an Option<String> so we must flatten
@ -345,17 +367,17 @@ pub(crate) async fn get_public_rooms_filtered_helper( @@ -345,17 +367,17 @@ pub(crate) async fn get_public_rooms_filtered_helper(
}
})
// We need to collect all, so we can sort by member count
.collect();
.collect::<Vec<_>>();
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 chunk: Vec<_> = all_rooms
let chunk = all_rooms
.into_iter()
.skip(num_since as usize)
.take(limit as usize)
.collect();
.collect::<Vec<_>>();
let prev_batch = if num_since == 0 {
None

30
src/client_server/keys.rs

@ -10,7 +10,7 @@ use ruma::{ @@ -10,7 +10,7 @@ use ruma::{
claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures,
upload_signing_keys,
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
uiaa::{AuthFlow, UiaaInfo},
},
},
federation,
@ -148,7 +148,7 @@ pub async fn upload_signing_keys_route( @@ -148,7 +148,7 @@ pub async fn upload_signing_keys_route(
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
stages: vec!["m.login.password".to_owned()],
}],
completed: Vec::new(),
params: Default::default(),
@ -316,7 +316,7 @@ pub async fn get_key_changes_route( @@ -316,7 +316,7 @@ pub async fn get_key_changes_route(
pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
sender_user: Option<&UserId>,
device_keys_input: &BTreeMap<Box<UserId>, Vec<Box<DeviceId>>>,
device_keys_input: &BTreeMap<UserId, Vec<Box<DeviceId>>>,
allowed_signatures: F,
db: &Database,
) -> Result<get_keys::Response> {
@ -328,8 +328,6 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>( @@ -328,8 +328,6 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
let mut get_over_federation = HashMap::new();
for (user_id, device_ids) in device_keys_input {
let user_id: &UserId = &**user_id;
if user_id.server_name() != db.globals.server_name() {
get_over_federation
.entry(user_id.server_name())
@ -357,11 +355,11 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>( @@ -357,11 +355,11 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
container.insert(device_id, keys);
}
}
device_keys.insert(user_id.to_owned(), container);
device_keys.insert(user_id.clone(), container);
} else {
for device_id in device_ids {
let mut container = BTreeMap::new();
if let Some(mut keys) = db.users.get_device_keys(user_id, device_id)? {
if let Some(mut keys) = db.users.get_device_keys(&user_id.clone(), device_id)? {
let metadata = db.users.get_device_metadata(user_id, device_id)?.ok_or(
Error::BadRequest(
ErrorKind::InvalidParam,
@ -373,36 +371,36 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>( @@ -373,36 +371,36 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
device_display_name: metadata.display_name,
};
container.insert(device_id.to_owned(), keys);
container.insert(device_id.clone(), keys);
}
device_keys.insert(user_id.to_owned(), container);
device_keys.insert(user_id.clone(), container);
}
}
if let Some(master_key) = db.users.get_master_key(user_id, &allowed_signatures)? {
master_keys.insert(user_id.to_owned(), master_key);
master_keys.insert(user_id.clone(), master_key);
}
if let Some(self_signing_key) = db
.users
.get_self_signing_key(user_id, &allowed_signatures)?
{
self_signing_keys.insert(user_id.to_owned(), self_signing_key);
self_signing_keys.insert(user_id.clone(), self_signing_key);
}
if Some(user_id) == sender_user {
if let Some(user_signing_key) = db.users.get_user_signing_key(user_id)? {
user_signing_keys.insert(user_id.to_owned(), user_signing_key);
user_signing_keys.insert(user_id.clone(), user_signing_key);
}
}
}
let mut failures = BTreeMap::new();
let mut futures: FuturesUnordered<_> = get_over_federation
let mut futures = get_over_federation
.into_iter()
.map(|(server, vec)| async move {
let mut device_keys_input_fed = BTreeMap::new();
for (user_id, keys) in vec {
device_keys_input_fed.insert(user_id.to_owned(), keys.clone());
device_keys_input_fed.insert(user_id.clone(), keys.clone());
}
(
server,
@ -417,7 +415,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>( @@ -417,7 +415,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
.await,
)
})
.collect();
.collect::<FuturesUnordered<_>>();
while let Some((server, response)) = futures.next().await {
match response {
@ -442,7 +440,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>( @@ -442,7 +440,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
}
pub(crate) async fn claim_keys_helper(
one_time_keys_input: &BTreeMap<Box<UserId>, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>,
one_time_keys_input: &BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>,
db: &Database,
) -> Result<claim_keys::Response> {
let mut one_time_keys = BTreeMap::new();

198
src/client_server/membership.rs

@ -1,9 +1,10 @@ @@ -1,9 +1,10 @@
use crate::{
client_server,
database::DatabaseGuard,
pdu::{EventHash, PduBuilder, PduEvent},
pdu::{PduBuilder, PduEvent},
server_server, utils, ConduitResult, Database, Error, Result, Ruma,
};
use member::{MemberEventContent, MembershipState};
use ruma::{
api::{
client::{
@ -17,21 +18,17 @@ use ruma::{ @@ -17,21 +18,17 @@ use ruma::{
federation::{self, membership::create_invite},
},
events::{
room::{
create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
},
pdu::Pdu,
room::{create::CreateEventContent, member},
EventType,
},
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue},
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res::{self, RoomVersion},
uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
};
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto},
iter,
sync::{Arc, RwLock},
time::{Duration, Instant},
};
@ -57,23 +54,25 @@ pub async fn join_room_by_id_route( @@ -57,23 +54,25 @@ pub async fn join_room_by_id_route(
) -> ConduitResult<join_room_by_id::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut servers: HashSet<_> = db
let mut servers = db
.rooms
.invite_state(sender_user, &body.room_id)?
.unwrap_or_default()
.iter()
.filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|event| {
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok()
})
.filter_map(|event| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| UserId::parse(sender).ok())
.filter_map(|sender| UserId::try_from(sender).ok())
.map(|user| user.server_name().to_owned())
.collect();
.collect::<HashSet<_>>();
servers.insert(body.room_id.server_name().to_owned());
let ret = join_room_by_id_helper(
&db,
body.sender_user.as_deref(),
body.sender_user.as_ref(),
&body.room_id,
&servers,
body.third_party_signed.as_ref(),
@ -100,22 +99,23 @@ pub async fn join_room_by_id_or_alias_route( @@ -100,22 +99,23 @@ pub async fn join_room_by_id_or_alias_route(
db: DatabaseGuard,
body: Ruma<join_room_by_id_or_alias::Request<'_>>,
) -> ConduitResult<join_room_by_id_or_alias::Response> {
let sender_user = body.sender_user.as_deref().expect("user is authenticated");
let body = body.body;
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let (servers, room_id) = match Box::<RoomId>::try_from(body.room_id_or_alias) {
let (servers, room_id) = match RoomId::try_from(body.room_id_or_alias.clone()) {
Ok(room_id) => {
let mut servers: HashSet<_> = db
let mut servers = db
.rooms
.invite_state(sender_user, &room_id)?
.unwrap_or_default()
.iter()
.filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|event| {
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok()
})
.filter_map(|event| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| UserId::parse(sender).ok())
.filter_map(|sender| UserId::try_from(sender).ok())
.map(|user| user.server_name().to_owned())
.collect();
.collect::<HashSet<_>>();
servers.insert(room_id.server_name().to_owned());
(servers, room_id)
@ -129,7 +129,7 @@ pub async fn join_room_by_id_or_alias_route( @@ -129,7 +129,7 @@ pub async fn join_room_by_id_or_alias_route(
let join_room_response = join_room_by_id_helper(
&db,
Some(sender_user),
body.sender_user.as_ref(),
&room_id,
&servers,
body.third_party_signed.as_ref(),
@ -204,7 +204,7 @@ pub async fn kick_user_route( @@ -204,7 +204,7 @@ pub async fn kick_user_route(
) -> ConduitResult<kick_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event: RoomMemberEventContent = serde_json::from_str(
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
db.rooms
.room_state_get(
&body.room_id,
@ -216,11 +216,13 @@ pub async fn kick_user_route( @@ -216,11 +216,13 @@ pub async fn kick_user_route(
"Cannot kick member that's not in the room.",
))?
.content
.get(),
.clone(),
)
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = MembershipState::Leave;
event.membership = ruma::events::room::member::MembershipState::Leave;
// TODO: reason
let mutex_state = Arc::clone(
@ -236,7 +238,7 @@ pub async fn kick_user_route( @@ -236,7 +238,7 @@ pub async fn kick_user_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
content: serde_json::to_value(event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(body.user_id.to_string()),
redacts: None,
@ -278,23 +280,24 @@ pub async fn ban_user_route( @@ -278,23 +280,24 @@ pub async fn ban_user_route(
&body.user_id.to_string(),
)?
.map_or(
Ok(RoomMemberEventContent {
membership: MembershipState::Ban,
Ok::<_, Error>(member::MemberEventContent {
membership: member::MembershipState::Ban,
displayname: db.users.displayname(&body.user_id)?,
avatar_url: db.users.avatar_url(&body.user_id)?,
is_direct: None,
third_party_invite: None,
blurhash: db.users.blurhash(&body.user_id)?,
reason: None,
join_authorized_via_users_server: None,
}),
|event| {
serde_json::from_str(event.content.get())
.map(|event: RoomMemberEventContent| RoomMemberEventContent {
membership: MembershipState::Ban,
..event
})
.map_err(|_| Error::bad_database("Invalid member event in database."))
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
event.content.clone(),
)
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Ban;
Ok(event)
},
)?;
@ -311,7 +314,7 @@ pub async fn ban_user_route( @@ -311,7 +314,7 @@ pub async fn ban_user_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
content: serde_json::to_value(event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(body.user_id.to_string()),
redacts: None,
@ -343,7 +346,7 @@ pub async fn unban_user_route( @@ -343,7 +346,7 @@ pub async fn unban_user_route(
) -> ConduitResult<unban_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event: RoomMemberEventContent = serde_json::from_str(
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
db.rooms
.room_state_get(
&body.room_id,
@ -355,11 +358,13 @@ pub async fn unban_user_route( @@ -355,11 +358,13 @@ pub async fn unban_user_route(
"Cannot unban a user who is not banned.",
))?
.content
.get(),
.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = MembershipState::Leave;
event.membership = ruma::events::room::member::MembershipState::Leave;
let mutex_state = Arc::clone(
db.globals
@ -374,7 +379,7 @@ pub async fn unban_user_route( @@ -374,7 +379,7 @@ pub async fn unban_user_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
content: serde_json::to_value(event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(body.user_id.to_string()),
redacts: None,
@ -534,7 +539,7 @@ async fn join_room_by_id_helper( @@ -534,7 +539,7 @@ async fn join_room_by_id_helper(
.roomid_mutex_state
.write()
.unwrap()
.entry(room_id.to_owned())
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
@ -554,7 +559,7 @@ async fn join_room_by_id_helper( @@ -554,7 +559,7 @@ async fn join_room_by_id_helper(
federation::membership::create_join_event_template::v1::Request {
room_id,
user_id: sender_user,
ver: &[RoomVersionId::V5, RoomVersionId::V6],
ver: &[RoomVersionId::Version5, RoomVersionId::Version6],
},
)
.await;
@ -570,15 +575,17 @@ async fn join_room_by_id_helper( @@ -570,15 +575,17 @@ async fn join_room_by_id_helper(
let room_version = match make_join_response.room_version {
Some(room_version)
if room_version == RoomVersionId::V5 || room_version == RoomVersionId::V6 =>
if room_version == RoomVersionId::Version5
|| room_version == RoomVersionId::Version6 =>
{
room_version
}
_ => return Err(Error::BadServerResponse("Room version is not supported")),
};
let mut join_event_stub: CanonicalJsonObject =
serde_json::from_str(make_join_response.event.get()).map_err(|_| {
let mut join_event_stub =
serde_json::from_str::<CanonicalJsonObject>(make_join_response.event.json().get())
.map_err(|_| {
Error::BadServerResponse("Invalid make_join event json received from server.")
})?;
@ -597,15 +604,14 @@ async fn join_room_by_id_helper( @@ -597,15 +604,14 @@ async fn join_room_by_id_helper(
);
join_event_stub.insert(
"content".to_owned(),
to_canonical_value(RoomMemberEventContent {
membership: MembershipState::Join,
to_canonical_value(member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None,
third_party_invite: None,
blurhash: db.users.blurhash(sender_user)?,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
);
@ -623,12 +629,11 @@ async fn join_room_by_id_helper( @@ -623,12 +629,11 @@ async fn join_room_by_id_helper(
.expect("event is valid, we just created it");
// Generate event id
let event_id = format!(
let event_id = EventId::try_from(&*format!(
"${}",
ruma::signatures::reference_hash(&join_event_stub, &room_version)
.expect("ruma can calculate reference hashes")
);
let event_id = <&EventId>::try_from(event_id.as_str())
))
.expect("ruma's reference hashes are valid event ids");
// Add event_id back
@ -647,15 +652,15 @@ async fn join_room_by_id_helper( @@ -647,15 +652,15 @@ async fn join_room_by_id_helper(
remote_server,
federation::membership::create_join_event::v2::Request {
room_id,
event_id,
pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
event_id: &event_id,
pdu: PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
},
)
.await?;
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."))?;
let mut state = HashMap::new();
@ -718,7 +723,7 @@ async fn join_room_by_id_helper( @@ -718,7 +723,7 @@ async fn join_room_by_id_helper(
state
.into_iter()
.map(|(k, id)| db.rooms.compress_state_event(k, &id, &db.globals))
.collect::<Result<_>>()?,
.collect::<Result<HashSet<_>>>()?,
db,
)?;
@ -743,7 +748,7 @@ async fn join_room_by_id_helper( @@ -743,7 +748,7 @@ async fn join_room_by_id_helper(
db.rooms.append_pdu(
&pdu,
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
iter::once(&*pdu.event_id),
&[pdu.event_id.clone()],
db,
)?;
@ -751,21 +756,20 @@ async fn join_room_by_id_helper( @@ -751,21 +756,20 @@ async fn join_room_by_id_helper(
// where events in the current room state do not exist
db.rooms.set_room_state(room_id, statehashid)?;
} else {
let event = RoomMemberEventContent {
membership: MembershipState::Join,
let event = member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None,
third_party_invite: None,
blurhash: db.users.blurhash(sender_user)?,
reason: None,
join_authorized_via_users_server: None,
};
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
content: serde_json::to_value(event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(sender_user.to_string()),
redacts: None,
@ -781,20 +785,20 @@ async fn join_room_by_id_helper( @@ -781,20 +785,20 @@ async fn join_room_by_id_helper(
db.flush()?;
Ok(join_room_by_id::Response::new(room_id.to_owned()).into())
Ok(join_room_by_id::Response::new(room_id.clone()).into())
}
fn validate_and_add_event_id(
pdu: &RawJsonValue,
pdu: &Raw<Pdu>,
room_version: &RoomVersionId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
db: &Database,
) -> Result<(Box<EventId>, CanonicalJsonObject)> {
let mut value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
) -> Result<(EventId, CanonicalJsonObject)> {
let mut value = serde_json::from_str::<CanonicalJsonObject>(pdu.json().get()).map_err(|e| {
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response")
})?;
let event_id = EventId::parse(format!(
let event_id = EventId::try_from(&*format!(
"${}",
ruma::signatures::reference_hash(&value, room_version)
.expect("ruma can calculate reference hashes")
@ -861,26 +865,29 @@ pub(crate) async fn invite_helper<'a>( @@ -861,26 +865,29 @@ pub(crate) async fn invite_helper<'a>(
.roomid_mutex_state
.write()
.unwrap()
.entry(room_id.to_owned())
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
let prev_events: Vec<_> = db
let prev_events = db
.rooms
.get_pdu_leaves(room_id)?
.into_iter()
.take(20)
.collect();
.collect::<Vec<_>>();
let create_event = db
.rooms
.room_state_get(room_id, &EventType::RoomCreate, "")?;
let create_event_content: Option<RoomCreateEventContent> = create_event
let create_event_content = create_event
.as_ref()
.map(|create_event| {
serde_json::from_str(create_event.content.get()).map_err(|e| {
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone())
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|e| {
warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.")
})
@ -897,11 +904,13 @@ pub(crate) async fn invite_helper<'a>( @@ -897,11 +904,13 @@ pub(crate) async fn invite_helper<'a>(
// If there was no create event yet, assume we are creating a version 6 room right now
let room_version_id = create_event_content
.map_or(RoomVersionId::V6, |create_event| create_event.room_version);
.map_or(RoomVersionId::Version6, |create_event| {
create_event.room_version
});
let room_version =
RoomVersion::new(&room_version_id).expect("room version is supported");
let content = to_raw_value(&RoomMemberEventContent {
let content = serde_json::to_value(MemberEventContent {
avatar_url: None,
displayname: None,
is_direct: Some(is_direct),
@ -909,7 +918,6 @@ pub(crate) async fn invite_helper<'a>( @@ -909,7 +918,6 @@ pub(crate) async fn invite_helper<'a>(
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("member event is valid value");
@ -938,14 +946,14 @@ pub(crate) async fn invite_helper<'a>( @@ -938,14 +946,14 @@ pub(crate) async fn invite_helper<'a>(
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
unsigned.insert(
"prev_sender".to_owned(),
to_raw_value(&prev_pdu.sender).expect("UserId is valid"),
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
);
}
let pdu = PduEvent {
event_id: ruma::event_id!("$thiswillbefilledinlater").into(),
room_id: room_id.to_owned(),
sender: sender_user.to_owned(),
event_id: ruma::event_id!("$thiswillbefilledinlater"),
room_id: room_id.clone(),
sender: sender_user.clone(),
origin_server_ts: utils::millis_since_unix_epoch()
.try_into()
.expect("time is valid"),
@ -959,15 +967,11 @@ pub(crate) async fn invite_helper<'a>( @@ -959,15 +967,11 @@ pub(crate) async fn invite_helper<'a>(
.map(|(_, pdu)| pdu.event_id.clone())
.collect(),
redacts: None,
unsigned: if unsigned.is_empty() {
None
} else {
Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
},
hashes: EventHash {
unsigned,
hashes: ruma::events::pdu::EventHash {
sha256: "aaa".to_owned(),
},
signatures: None,
signatures: BTreeMap::new(),
};
let auth_check = state_res::auth_check(
@ -1018,12 +1022,11 @@ pub(crate) async fn invite_helper<'a>( @@ -1018,12 +1022,11 @@ pub(crate) async fn invite_helper<'a>(
};
// Generate event id
let expected_event_id = format!(
let expected_event_id = EventId::try_from(&*format!(
"${}",
ruma::signatures::reference_hash(&pdu_json, &room_version_id)
.expect("ruma can calculate reference hashes")
);
let expected_event_id = <&EventId>::try_from(expected_event_id.as_str())
))
.expect("ruma's reference hashes are valid event ids");
let response = db
@ -1032,11 +1035,11 @@ pub(crate) async fn invite_helper<'a>( @@ -1032,11 +1035,11 @@ pub(crate) async fn invite_helper<'a>(
&db.globals,
user_id.server_name(),
create_invite::v2::Request {
room_id,
event_id: expected_event_id,
room_version: &room_version_id,
event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
invite_room_state: &invite_room_state,
room_id: room_id.clone(),
event_id: expected_event_id.clone(),
room_version: room_version_id,
event: PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
invite_room_state,
},
)
.await?;
@ -1059,7 +1062,7 @@ pub(crate) async fn invite_helper<'a>( @@ -1059,7 +1062,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);
}
let origin: Box<ServerName> = serde_json::from_value(
let origin = serde_json::from_value::<Box<ServerName>>(
serde_json::to_value(value.get("origin").ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Event needs an origin field.",
@ -1105,7 +1108,7 @@ pub(crate) async fn invite_helper<'a>( @@ -1105,7 +1108,7 @@ pub(crate) async fn invite_helper<'a>(
.roomid_mutex_state
.write()
.unwrap()
.entry(room_id.to_owned())
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
@ -1113,15 +1116,14 @@ pub(crate) async fn invite_helper<'a>( @@ -1113,15 +1116,14 @@ pub(crate) async fn invite_helper<'a>(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Invite,
displayname: db.users.displayname(user_id)?,
avatar_url: db.users.avatar_url(user_id)?,
is_direct: Some(is_direct),
third_party_invite: None,
blurhash: db.users.blurhash(user_id)?,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,

45
src/client_server/message.rs

@ -5,8 +5,13 @@ use ruma::{ @@ -5,8 +5,13 @@ use ruma::{
r0::message::{get_message_events, send_message_event},
},
events::EventType,
EventId,
};
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
sync::Arc,
};
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
#[cfg(feature = "conduit_bin")]
use rocket::{get, put};
@ -40,14 +45,6 @@ pub async fn send_message_event_route( @@ -40,14 +45,6 @@ pub async fn send_message_event_route(
);
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
if let Some(response) =
db.transaction_ids
@ -62,9 +59,10 @@ pub async fn send_message_event_route( @@ -62,9 +59,10 @@ pub async fn send_message_event_route(
));
}
let event_id = utils::string_from_bytes(&response)
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?
.try_into()
let event_id = EventId::try_from(
utils::string_from_bytes(&response)
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?,
)
.map_err(|_| Error::bad_database("Invalid event id in txnid data."))?;
return Ok(send_message_event::Response { event_id }.into());
}
@ -98,7 +96,7 @@ pub async fn send_message_event_route( @@ -98,7 +96,7 @@ pub async fn send_message_event_route(
db.flush()?;
Ok(send_message_event::Response::new((*event_id).to_owned()).into())
Ok(send_message_event::Response::new(event_id).into())
}
/// # `GET /_matrix/client/r0/rooms/{roomId}/messages`
@ -134,11 +132,14 @@ pub async fn get_message_events_route( @@ -134,11 +132,14 @@ pub async fn get_message_events_route(
let to = body.to.as_ref().map(|t| t.parse());
// Use limit or else 10
let limit = body.limit.try_into().map_or(10_usize, |l: u32| l as usize);
let limit = body
.limit
.try_into()
.map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?;
match body.dir {
get_message_events::Direction::Forward => {
let events_after: Vec<_> = db
let events_after = db
.rooms
.pdus_after(sender_user, &body.room_id, from)?
.take(limit)
@ -150,14 +151,14 @@ pub async fn get_message_events_route( @@ -150,14 +151,14 @@ pub async fn get_message_events_route(
.ok()
})
.take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to`
.collect();
.collect::<Vec<_>>();
let end_token = events_after.last().map(|(count, _)| count.to_string());
let events_after: Vec<_> = events_after
let events_after = events_after
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect();
.collect::<Vec<_>>();
let mut resp = get_message_events::Response::new();
resp.start = Some(body.from.to_owned());
@ -168,7 +169,7 @@ pub async fn get_message_events_route( @@ -168,7 +169,7 @@ pub async fn get_message_events_route(
Ok(resp.into())
}
get_message_events::Direction::Backward => {
let events_before: Vec<_> = db
let events_before = db
.rooms
.pdus_until(sender_user, &body.room_id, from)?
.take(limit)
@ -180,14 +181,14 @@ pub async fn get_message_events_route( @@ -180,14 +181,14 @@ pub async fn get_message_events_route(
.ok()
})
.take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to`
.collect();
.collect::<Vec<_>>();
let start_token = events_before.last().map(|(count, _)| count.to_string());
let events_before: Vec<_> = events_before
let events_before = events_before
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect();
.collect::<Vec<_>>();
let mut resp = get_message_events::Response::new();
resp.start = Some(body.from.to_owned());

2
src/client_server/mod.rs

@ -16,7 +16,6 @@ mod profile; @@ -16,7 +16,6 @@ mod profile;
mod push;
mod read_marker;
mod redact;
mod report;
mod room;
mod search;
mod session;
@ -48,7 +47,6 @@ pub use profile::*; @@ -48,7 +47,6 @@ pub use profile::*;
pub use push::*;
pub use read_marker::*;
pub use redact::*;
pub use report::*;
pub use room::*;
pub use search::*;
pub use session::*;

20
src/client_server/profile.rs

@ -9,9 +9,9 @@ use ruma::{ @@ -9,9 +9,9 @@ use ruma::{
},
federation::{self, query::get_profile_information::v1::ProfileField},
},
events::{room::member::RoomMemberEventContent, EventType},
events::EventType,
serde::Raw,
};
use serde_json::value::to_raw_value;
use std::{convert::TryInto, sync::Arc};
#[cfg(feature = "conduit_bin")]
@ -45,9 +45,9 @@ pub async fn set_displayname_route( @@ -45,9 +45,9 @@ pub async fn set_displayname_route(
Ok::<_, Error>((
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
content: serde_json::to_value(ruma::events::room::member::MemberEventContent {
displayname: body.displayname.clone(),
..serde_json::from_str(
..serde_json::from_value::<Raw<_>>(
db.rooms
.room_state_get(
&room_id,
@ -61,8 +61,10 @@ pub async fn set_displayname_route( @@ -61,8 +61,10 @@ pub async fn set_displayname_route(
)
})?
.content
.get(),
.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Database contains invalid PDU."))?
})
.expect("event is valid, we just created it"),
@ -188,9 +190,9 @@ pub async fn set_avatar_url_route( @@ -188,9 +190,9 @@ pub async fn set_avatar_url_route(
Ok::<_, Error>((
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
content: serde_json::to_value(ruma::events::room::member::MemberEventContent {
avatar_url: body.avatar_url.clone(),
..serde_json::from_str(
..serde_json::from_value::<Raw<_>>(
db.rooms
.room_state_get(
&room_id,
@ -204,8 +206,10 @@ pub async fn set_avatar_url_route( @@ -204,8 +206,10 @@ pub async fn set_avatar_url_route(
)
})?
.content
.get(),
.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Database contains invalid PDU."))?
})
.expect("event is valid, we just created it"),

44
src/client_server/push.rs

@ -8,7 +8,7 @@ use ruma::{ @@ -8,7 +8,7 @@ use ruma::{
set_pushrule_enabled, RuleKind,
},
},
events::{push_rules::PushRulesEvent, EventType},
events::{push_rules, EventType},
push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit},
};
@ -29,9 +29,9 @@ pub async fn get_pushrules_all_route( @@ -29,9 +29,9 @@ pub async fn get_pushrules_all_route(
) -> ConduitResult<get_pushrules_all::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: PushRulesEvent = db
let event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -57,9 +57,9 @@ pub async fn get_pushrule_route( @@ -57,9 +57,9 @@ pub async fn get_pushrule_route(
) -> ConduitResult<get_pushrule::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: PushRulesEvent = db
let event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -105,15 +105,15 @@ pub async fn get_pushrule_route( @@ -105,15 +105,15 @@ pub async fn get_pushrule_route(
/// Creates a single specified push rule for this user.
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<req>")
)]
#[tracing::instrument(skip(db, body))]
#[tracing::instrument(skip(db, req))]
pub async fn set_pushrule_route(
db: DatabaseGuard,
body: Ruma<set_pushrule::Request<'_>>,
req: Ruma<set_pushrule::Request<'_>>,
) -> ConduitResult<set_pushrule::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let body = body.body;
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
let body = req.body;
if body.scope != "global" {
return Err(Error::BadRequest(
@ -122,9 +122,9 @@ pub async fn set_pushrule_route( @@ -122,9 +122,9 @@ pub async fn set_pushrule_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -222,9 +222,9 @@ pub async fn get_pushrule_actions_route( @@ -222,9 +222,9 @@ pub async fn get_pushrule_actions_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -284,9 +284,9 @@ pub async fn set_pushrule_actions_route( @@ -284,9 +284,9 @@ pub async fn set_pushrule_actions_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -356,9 +356,9 @@ pub async fn get_pushrule_enabled_route( @@ -356,9 +356,9 @@ pub async fn get_pushrule_enabled_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -420,9 +420,9 @@ pub async fn set_pushrule_enabled_route( @@ -420,9 +420,9 @@ pub async fn set_pushrule_enabled_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<ruma::events::push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",
@ -497,9 +497,9 @@ pub async fn delete_pushrule_route( @@ -497,9 +497,9 @@ pub async fn delete_pushrule_route(
));
}
let mut event: PushRulesEvent = db
let mut event = db
.account_data
.get(None, sender_user, EventType::PushRules)?
.get::<push_rules::PushRulesEvent>(None, sender_user, EventType::PushRules)?
.ok_or(Error::BadRequest(
ErrorKind::NotFound,
"PushRules event not found.",

9
src/client_server/redact.rs

@ -3,12 +3,11 @@ use std::sync::Arc; @@ -3,12 +3,11 @@ use std::sync::Arc;
use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma};
use ruma::{
api::client::r0::redact::redact_event,
events::{room::redaction::RoomRedactionEventContent, EventType},
events::{room::redaction, EventType},
};
#[cfg(feature = "conduit_bin")]
use rocket::put;
use serde_json::value::to_raw_value;
/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
///
@ -25,7 +24,6 @@ pub async fn redact_event_route( @@ -25,7 +24,6 @@ pub async fn redact_event_route(
body: Ruma<redact_event::Request<'_>>,
) -> ConduitResult<redact_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let body = body.body;
let mutex_state = Arc::clone(
db.globals
@ -40,13 +38,13 @@ pub async fn redact_event_route( @@ -40,13 +38,13 @@ pub async fn redact_event_route(
let event_id = db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomRedaction,
content: to_raw_value(&RoomRedactionEventContent {
content: serde_json::to_value(redaction::RedactionEventContent {
reason: body.reason.clone(),
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: Some(body.event_id.into()),
redacts: Some(body.event_id.clone()),
},
sender_user,
&body.room_id,
@ -58,6 +56,5 @@ pub async fn redact_event_route( @@ -58,6 +56,5 @@ pub async fn redact_event_route(
db.flush()?;
let event_id = (*event_id).to_owned();
Ok(redact_event::Response { event_id }.into())
}

84
src/client_server/report.rs

@ -1,84 +0,0 @@ @@ -1,84 +0,0 @@
use crate::{
database::{admin::AdminCommand, DatabaseGuard},
ConduitResult, Error, Ruma,
};
use ruma::{
api::client::{error::ErrorKind, r0::room::report_content},
events::room::message,
int,
};
#[cfg(feature = "conduit_bin")]
use rocket::{http::RawStr, post};
/// # `POST /_matrix/client/r0/rooms/{roomId}/report/{eventId}`
///
/// Reports an inappropriate event to homeserver admins
///
#[cfg_attr(
feature = "conduit_bin",
post("/_matrix/client/r0/rooms/<_>/report/<_>", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn report_event_route(
db: DatabaseGuard,
body: Ruma<report_content::Request<'_>>,
) -> ConduitResult<report_content::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let pdu = match db.rooms.get_pdu(&body.event_id)? {
Some(pdu) => pdu,
_ => {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid Event ID",
))
}
};
if body.score > int!(0) || body.score < int!(-100) {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid score, must be within 0 to -100",
));
};
if body.reason.chars().count() > 250 {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Reason too long, should be 250 characters or fewer",
));
};
db.admin.send(AdminCommand::SendMessage(
message::RoomMessageEventContent::text_html(
format!(
"Report received from: {}\n\n\
Event ID: {}\n\
Room ID: {}\n\
Sent By: {}\n\n\
Report Score: {}\n\
Report Reason: {}",
sender_user, pdu.event_id, pdu.room_id, pdu.sender, body.score, body.reason
),
format!(
"<details><summary>Report received from: <a href=\"https://matrix.to/#/{0}\">{0}\
</a></summary><ul><li>Event Info<ul><li>Event ID: <code>{1}</code>\
<a href=\"https://matrix.to/#/{2}/{1}\">🔗</a></li><li>Room ID: <code>{2}</code>\
</li><li>Sent By: <a href=\"https://matrix.to/#/{3}\">{3}</a></li></ul></li><li>\
Report Info<ul><li>Report Score: {4}</li><li>Report Reason: {5}</li></ul></li>\
</ul></details>",
sender_user,
pdu.event_id,
pdu.room_id,
pdu.sender,
body.score,
RawStr::new(&body.reason).html_escape()
),
),
));
db.flush()?;
Ok(report_content::Response {}.into())
}

243
src/client_server/room.rs

@ -8,26 +8,13 @@ use ruma::{ @@ -8,26 +8,13 @@ use ruma::{
r0::room::{self, aliases, create_room, get_room_event, upgrade_room},
},
events::{
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,
},
room::{guest_access, history_visibility, join_rules, member, name, topic},
EventType,
},
int,
serde::{CanonicalJsonObject, JsonObject},
serde::Raw,
RoomAliasId, RoomId, RoomVersionId,
};
use serde_json::{json, value::to_raw_value};
use std::{cmp::max, collections::BTreeMap, convert::TryInto, sync::Arc};
use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc};
use tracing::{info, warn};
#[cfg(feature = "conduit_bin")]
@ -74,26 +61,14 @@ pub async fn create_room_route( @@ -74,26 +61,14 @@ pub async fn create_room_route(
);
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<Box<RoomAliasId>> =
let alias: Option<RoomAliasId> =
body.room_alias_name
.as_ref()
.map_or(Ok(None), |localpart| {
// TODO: Check for invalid characters and maximum length
let alias =
RoomAliasId::parse(format!("#{}:{}", localpart, db.globals.server_name()))
.map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias.")
})?;
RoomAliasId::try_from(format!("#{}:{}", localpart, db.globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
if db.rooms.id_from_alias(&alias)?.is_some() {
Err(Error::BadRequest(
@ -105,9 +80,12 @@ pub async fn create_room_route( @@ -105,9 +80,12 @@ pub async fn create_room_route(
}
})?;
let room_version = match body.room_version.clone() {
let mut content = ruma::events::room::create::CreateEventContent::new(sender_user.clone());
content.federate = body.creation_content.federate;
content.predecessor = body.creation_content.predecessor.clone();
content.room_version = match body.room_version.clone() {
Some(room_version) => {
if room_version == RoomVersionId::V5 || room_version == RoomVersionId::V6 {
if room_version == RoomVersionId::Version5 || room_version == RoomVersionId::Version6 {
room_version
} else {
return Err(Error::BadRequest(
@ -116,64 +94,14 @@ pub async fn create_room_route( @@ -116,64 +94,14 @@ pub async fn create_room_route(
));
}
}
None => RoomVersionId::V6,
None => RoomVersionId::Version6,
};
let content = match &body.creation_content {
Some(content) => {
let mut content = content
.deserialize_as::<CanonicalJsonObject>()
.expect("Invalid creation content");
content.insert(
"creator".into(),
json!(&sender_user).try_into().map_err(|_| {
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
})?,
);
content.insert(
"room_version".into(),
json!(room_version.as_str()).try_into().map_err(|_| {
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
})?,
);
content
}
None => {
let mut content = serde_json::from_str::<CanonicalJsonObject>(
to_raw_value(&RoomCreateEventContent::new(sender_user.clone()))
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid creation content"))?
.get(),
)
.unwrap();
content.insert(
"room_version".into(),
json!(room_version.as_str()).try_into().map_err(|_| {
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
})?,
);
content
}
};
// Validate creation content
let de_result = serde_json::from_str::<CanonicalJsonObject>(
to_raw_value(&content)
.expect("Invalid creation content")
.get(),
);
if de_result.is_err() {
return Err(Error::BadRequest(
ErrorKind::BadJson,
"Invalid creation content",
));
}
// 1. The room create event
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomCreate,
content: to_raw_value(&content).expect("event is valid, we just created it"),
content: serde_json::to_value(content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
redacts: None,
@ -188,15 +116,14 @@ pub async fn create_room_route( @@ -188,15 +116,14 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?,
is_direct: Some(body.is_direct),
third_party_invite: None,
blurhash: db.users.blurhash(sender_user)?,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
@ -222,22 +149,25 @@ pub async fn create_room_route( @@ -222,22 +149,25 @@ pub async fn create_room_route(
});
let mut users = BTreeMap::new();
users.insert(sender_user.clone(), int!(100));
users.insert(sender_user.clone(), 100.into());
if preset == create_room::RoomPreset::TrustedPrivateChat {
for invite_ in &body.invite {
users.insert(invite_.clone(), int!(100));
users.insert(invite_.clone(), 100.into());
}
}
let mut power_levels_content = serde_json::to_value(RoomPowerLevelsEventContent {
let mut power_levels_content =
serde_json::to_value(ruma::events::room::power_levels::PowerLevelsEventContent {
users,
..Default::default()
})
.expect("event is valid, we just created it");
if let Some(power_level_content_override) = &body.power_level_content_override {
let json: JsonObject = serde_json::from_str(power_level_content_override.json().get())
let json = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(
power_level_content_override.json().get(),
)
.map_err(|_| {
Error::BadRequest(ErrorKind::BadJson, "Invalid power_level_content_override.")
})?;
@ -250,8 +180,7 @@ pub async fn create_room_route( @@ -250,8 +180,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content)
.expect("to_raw_value always works on serde_json::Value"),
content: power_levels_content,
unsigned: None,
state_key: Some("".to_owned()),
redacts: None,
@ -267,10 +196,12 @@ pub async fn create_room_route( @@ -267,10 +196,12 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomCanonicalAlias,
content: to_raw_value(&RoomCanonicalAliasEventContent {
alias: Some(room_alias_id.to_owned()),
content: serde_json::to_value(
ruma::events::room::canonical_alias::CanonicalAliasEventContent {
alias: Some(room_alias_id.clone()),
alt_aliases: vec![],
})
},
)
.expect("We checked that alias earlier, it must be fine"),
unsigned: None,
state_key: Some("".to_owned()),
@ -289,12 +220,17 @@ pub async fn create_room_route( @@ -289,12 +220,17 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomJoinRules,
content: to_raw_value(&RoomJoinRulesEventContent::new(match preset {
create_room::RoomPreset::PublicChat => JoinRule::Public,
content: match preset {
create_room::RoomPreset::PublicChat => serde_json::to_value(
join_rules::JoinRulesEventContent::new(join_rules::JoinRule::Public),
)
.expect("event is valid, we just created it"),
// according to spec "invite" is the default
_ => JoinRule::Invite,
}))
_ => serde_json::to_value(join_rules::JoinRulesEventContent::new(
join_rules::JoinRule::Invite,
))
.expect("event is valid, we just created it"),
},
unsigned: None,
state_key: Some("".to_owned()),
redacts: None,
@ -309,8 +245,8 @@ pub async fn create_room_route( @@ -309,8 +245,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomHistoryVisibility,
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
HistoryVisibility::Shared,
content: serde_json::to_value(history_visibility::HistoryVisibilityEventContent::new(
history_visibility::HistoryVisibility::Shared,
))
.expect("event is valid, we just created it"),
unsigned: None,
@ -327,11 +263,18 @@ pub async fn create_room_route( @@ -327,11 +263,18 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomGuestAccess,
content: to_raw_value(&RoomGuestAccessEventContent::new(match preset {
create_room::RoomPreset::PublicChat => GuestAccess::Forbidden,
_ => GuestAccess::CanJoin,
}))
content: match preset {
create_room::RoomPreset::PublicChat => {
serde_json::to_value(guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::Forbidden,
))
.expect("event is valid, we just created it")
}
_ => serde_json::to_value(guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::CanJoin,
))
.expect("event is valid, we just created it"),
},
unsigned: None,
state_key: Some("".to_owned()),
redacts: None,
@ -363,7 +306,7 @@ pub async fn create_room_route( @@ -363,7 +306,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(Some(name.clone())))
content: serde_json::to_value(name::NameEventContent::new(Some(name.clone())))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -380,7 +323,7 @@ pub async fn create_room_route( @@ -380,7 +323,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomTopic,
content: to_raw_value(&RoomTopicEventContent {
content: serde_json::to_value(topic::TopicEventContent {
topic: topic.clone(),
})
.expect("event is valid, we just created it"),
@ -483,7 +426,7 @@ pub async fn get_room_aliases_route( @@ -483,7 +426,7 @@ pub async fn get_room_aliases_route(
.into())
}
/// # `POST /_matrix/client/r0/rooms/{roomId}/upgrade`
/// # `GET /_matrix/client/r0/rooms/{roomId}/upgrade`
///
/// Upgrades the room.
///
@ -504,7 +447,10 @@ pub async fn upgrade_room_route( @@ -504,7 +447,10 @@ pub async fn upgrade_room_route(
) -> ConduitResult<upgrade_room::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !matches!(body.new_version, RoomVersionId::V5 | RoomVersionId::V6) {
if !matches!(
body.new_version,
RoomVersionId::Version5 | RoomVersionId::Version6
) {
return Err(Error::BadRequest(
ErrorKind::UnsupportedRoomVersion,
"This server does not support that room version.",
@ -531,8 +477,8 @@ pub async fn upgrade_room_route( @@ -531,8 +477,8 @@ pub async fn upgrade_room_route(
let tombstone_event_id = db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomTombstone,
content: to_raw_value(&RoomTombstoneEventContent {
body: "This room has been replaced".to_owned(),
content: serde_json::to_value(ruma::events::room::tombstone::TombstoneEventContent {
body: "This room has been replaced".to_string(),
replacement_room: replacement_room.clone(),
})
.expect("event is valid, we just created it"),
@ -558,60 +504,36 @@ pub async fn upgrade_room_route( @@ -558,60 +504,36 @@ pub async fn upgrade_room_route(
);
let state_lock = mutex_state.lock().await;
// Get the old room creation event
let mut create_event_content = serde_json::from_str::<CanonicalJsonObject>(
// Get the old room federations status
let federate = serde_json::from_value::<Raw<ruma::events::room::create::CreateEventContent>>(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomCreate, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
.content
.get(),
.clone(),
)
.map_err(|_| Error::bad_database("Invalid room event in database."))?;
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid room event in database."))?
.federate;
// Use the m.room.tombstone event as the predecessor
let predecessor = Some(ruma::events::room::create::PreviousRoom::new(
body.room_id.clone(),
(*tombstone_event_id).to_owned(),
tombstone_event_id,
));
// Send a m.room.create event containing a predecessor field and the applicable room_version
create_event_content.insert(
"creator".into(),
json!(&sender_user)
.try_into()
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
);
create_event_content.insert(
"room_version".into(),
json!(&body.new_version)
.try_into()
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
);
create_event_content.insert(
"predecessor".into(),
json!(predecessor)
.try_into()
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
);
// Validate creation event content
let de_result = serde_json::from_str::<CanonicalJsonObject>(
to_raw_value(&create_event_content)
.expect("Error forming creation event")
.get(),
);
if de_result.is_err() {
return Err(Error::BadRequest(
ErrorKind::BadJson,
"Error forming creation event",
));
}
let mut create_event_content =
ruma::events::room::create::CreateEventContent::new(sender_user.clone());
create_event_content.federate = federate;
create_event_content.room_version = body.new_version.clone();
create_event_content.predecessor = predecessor;
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomCreate,
content: to_raw_value(&create_event_content)
content: serde_json::to_value(create_event_content)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),
@ -627,15 +549,14 @@ pub async fn upgrade_room_route( @@ -627,15 +549,14 @@ pub async fn upgrade_room_route(
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
content: serde_json::to_value(member::MemberEventContent {
membership: member::MembershipState::Join,
displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None,
third_party_invite: None,
blurhash: db.users.blurhash(sender_user)?,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
@ -690,17 +611,23 @@ pub async fn upgrade_room_route( @@ -690,17 +611,23 @@ pub async fn upgrade_room_route(
}
// Get the old room power levels
let mut power_levels_event_content: RoomPowerLevelsEventContent = serde_json::from_str(
let mut power_levels_event_content =
serde_json::from_value::<Raw<ruma::events::room::power_levels::PowerLevelsEventContent>>(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomPowerLevels, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
.content
.get(),
.clone(),
)
.expect("database contains invalid PDU")
.deserialize()
.map_err(|_| Error::bad_database("Invalid room event in database."))?;
// Setting events_default and invite to the greater of 50 and users_default + 1
let new_level = max(int!(50), power_levels_event_content.users_default + int!(1));
let new_level = max(
50.into(),
power_levels_event_content.users_default + 1.into(),
);
power_levels_event_content.events_default = new_level;
power_levels_event_content.invite = new_level;
@ -708,7 +635,7 @@ pub async fn upgrade_room_route( @@ -708,7 +635,7 @@ pub async fn upgrade_room_route(
let _ = db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomPowerLevels,
content: to_raw_value(&power_levels_event_content)
content: serde_json::to_value(power_levels_event_content)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some("".to_owned()),

6
src/client_server/search.rs

@ -74,7 +74,7 @@ pub async fn search_events_route( @@ -74,7 +74,7 @@ pub async fn search_events_route(
}
}
let results: Vec<_> = results
let results = results
.iter()
.map(|result| {
Ok::<_, Error>(SearchResult {
@ -95,7 +95,7 @@ pub async fn search_events_route( @@ -95,7 +95,7 @@ pub async fn search_events_route(
.filter_map(|r| r.ok())
.skip(skip)
.take(limit)
.collect();
.collect::<Vec<_>>();
let next_batch = if results.len() < limit as usize {
None
@ -114,7 +114,7 @@ pub async fn search_events_route( @@ -114,7 +114,7 @@ pub async fn search_events_route(
.search_term
.split_terminator(|c: char| !c.is_alphanumeric())
.map(str::to_lowercase)
.collect(),
.collect::<Vec<_>>(),
},
})
.into())

12
src/client_server/session.rs

@ -60,10 +60,10 @@ pub async fn login_route( @@ -60,10 +60,10 @@ pub async fn login_route(
// Validate login method
// TODO: Other login methods
let user_id = match &body.login_info {
login::IncomingLoginInfo::Password(login::IncomingPassword {
login::IncomingLoginInfo::Password {
identifier,
password,
}) => {
} => {
let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
matrix_id
} else {
@ -97,7 +97,7 @@ pub async fn login_route( @@ -97,7 +97,7 @@ pub async fn login_route(
user_id
}
login::IncomingLoginInfo::Token(login::IncomingToken { token }) => {
login::IncomingLoginInfo::Token { token } => {
if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() {
let token = jsonwebtoken::decode::<Claims>(
token,
@ -116,12 +116,6 @@ pub async fn login_route( @@ -116,12 +116,6 @@ 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

36
src/client_server/state.rs

@ -10,8 +10,8 @@ use ruma::{ @@ -10,8 +10,8 @@ use ruma::{
},
events::{
room::{
canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
canonical_alias::CanonicalAliasEventContent,
history_visibility::{HistoryVisibility, HistoryVisibilityEventContent},
},
AnyStateEventContent, EventType,
},
@ -52,7 +52,6 @@ pub async fn send_state_event_for_key_route( @@ -52,7 +52,6 @@ pub async fn send_state_event_for_key_route(
db.flush()?;
let event_id = (*event_id).to_owned();
Ok(send_state_event::Response { event_id }.into())
}
@ -74,14 +73,6 @@ pub async fn send_state_event_for_empty_key_route( @@ -74,14 +73,6 @@ pub async fn send_state_event_for_empty_key_route(
) -> ConduitResult<send_state_event::Response> {
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(
&db,
sender_user,
@ -94,7 +85,6 @@ pub async fn send_state_event_for_empty_key_route( @@ -94,7 +85,6 @@ pub async fn send_state_event_for_empty_key_route(
db.flush()?;
let event_id = (*event_id).to_owned();
Ok(send_state_event::Response { event_id }.into())
}
@ -122,13 +112,13 @@ pub async fn get_state_events_route( @@ -122,13 +112,13 @@ pub async fn get_state_events_route(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone())
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
.map(|e| e.history_visibility)
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
@ -174,13 +164,13 @@ pub async fn get_state_events_for_key_route( @@ -174,13 +164,13 @@ pub async fn get_state_events_for_key_route(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone())
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
.map(|e| e.history_visibility)
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
@ -200,7 +190,7 @@ pub async fn get_state_events_for_key_route( @@ -200,7 +190,7 @@ pub async fn get_state_events_for_key_route(
))?;
Ok(get_state_events_for_key::Response {
content: serde_json::from_str(event.content.get())
content: serde_json::from_value(event.content.clone())
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}
.into())
@ -230,13 +220,13 @@ pub async fn get_state_events_for_empty_key_route( @@ -230,13 +220,13 @@ pub async fn get_state_events_for_empty_key_route(
db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| {
serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone())
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})
.map(|e| e.history_visibility)
}),
Some(Ok(HistoryVisibility::WorldReadable))
)
@ -256,7 +246,7 @@ pub async fn get_state_events_for_empty_key_route( @@ -256,7 +246,7 @@ pub async fn get_state_events_for_empty_key_route(
))?;
Ok(get_state_events_for_key::Response {
content: serde_json::from_str(event.content.get())
content: serde_json::from_value(event.content.clone())
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}
.into())
@ -269,13 +259,13 @@ async fn send_state_event_for_key_helper( @@ -269,13 +259,13 @@ async fn send_state_event_for_key_helper(
event_type: EventType,
json: &Raw<AnyStateEventContent>,
state_key: String,
) -> Result<Arc<EventId>> {
) -> Result<EventId> {
let sender_user = sender;
// TODO: Review this check, error if event is unparsable, use event type, allow alias if it
// previously existed
if let Ok(canonical_alias) =
serde_json::from_str::<RoomCanonicalAliasEventContent>(json.json().get())
serde_json::from_str::<CanonicalAliasEventContent>(json.json().get())
{
let mut aliases = canonical_alias.alt_aliases.clone();
@ -305,7 +295,7 @@ async fn send_state_event_for_key_helper( @@ -305,7 +295,7 @@ async fn send_state_event_for_key_helper(
.roomid_mutex_state
.write()
.unwrap()
.entry(room_id.to_owned())
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;

85
src/client_server/sync.rs

@ -1,16 +1,13 @@ @@ -1,16 +1,13 @@
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
use ruma::{
api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
events::{
room::member::{MembershipState, RoomMemberEventContent},
AnySyncEphemeralRoomEvent, EventType,
},
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType},
serde::Raw,
DeviceId, RoomId, UserId,
};
use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::TryInto,
convert::{TryFrom, TryInto},
sync::Arc,
time::Duration,
};
@ -60,10 +57,9 @@ use rocket::{get, tokio}; @@ -60,10 +57,9 @@ use rocket::{get, tokio};
pub async fn sync_events_route(
db: DatabaseGuard,
body: Ruma<sync_events::Request<'_>>,
) -> Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> {
let sender_user = body.sender_user.expect("user is authenticated");
let sender_device = body.sender_device.expect("user is authenticated");
let body = body.body;
) -> std::result::Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> {
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 arc_db = Arc::new(db);
@ -133,7 +129,7 @@ pub async fn sync_events_route( @@ -133,7 +129,7 @@ pub async fn sync_events_route(
async fn sync_helper_wrapper(
db: Arc<DatabaseGuard>,
sender_user: Box<UserId>,
sender_user: UserId,
sender_device: Box<DeviceId>,
since: Option<String>,
full_state: bool,
@ -177,13 +173,13 @@ async fn sync_helper_wrapper( @@ -177,13 +173,13 @@ async fn sync_helper_wrapper(
async fn sync_helper(
db: Arc<DatabaseGuard>,
sender_user: Box<UserId>,
sender_user: UserId,
sender_device: Box<DeviceId>,
since: Option<String>,
full_state: bool,
timeout: Option<Duration>,
// bool = caching allowed
) -> Result<(sync_events::Response, bool), Error> {
) -> std::result::Result<(sync_events::Response, bool), Error> {
// TODO: match body.set_presence {
db.rooms.edus.ping_presence(&sender_user)?;
@ -245,13 +241,13 @@ async fn sync_helper( @@ -245,13 +241,13 @@ async fn sync_helper(
});
// Take the last 10 events for the timeline
let timeline_pdus: Vec<_> = non_timeline_pdus
let timeline_pdus = non_timeline_pdus
.by_ref()
.take(10)
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
.collect::<Vec<_>>();
let send_notification_counts = !timeline_pdus.is_empty()
|| db
@ -291,13 +287,13 @@ async fn sync_helper( @@ -291,13 +287,13 @@ async fn sync_helper(
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
.map(|(_, pdu)| {
let content: RoomMemberEventContent =
serde_json::from_str(pdu.content.get()).map_err(|_| {
Error::bad_database("Invalid member event in database.")
})?;
let content = serde_json::from_value::<
ruma::events::room::member::MemberEventContent,
>(pdu.content.clone())
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
if let Some(state_key) = &pdu.state_key {
let user_id = UserId::parse(state_key.clone()).map_err(|_| {
let user_id = UserId::try_from(state_key.clone()).map_err(|_| {
Error::bad_database("Invalid UserId in member PDU.")
})?;
@ -347,11 +343,11 @@ async fn sync_helper( @@ -347,11 +343,11 @@ async fn sync_helper(
let (joined_member_count, invited_member_count, heroes) = calculate_counts()?;
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
let state_events: Vec<_> = current_state_ids
let state_events = current_state_ids
.iter()
.map(|(_, id)| db.rooms.get_pdu(id))
.filter_map(|r| r.ok().flatten())
.collect();
.collect::<Vec<_>>();
(
heroes,
@ -367,7 +363,7 @@ async fn sync_helper( @@ -367,7 +363,7 @@ async fn sync_helper(
// Incremental /sync
let since_shortstatehash = since_shortstatehash.unwrap();
let since_sender_member: Option<RoomMemberEventContent> = db
let since_sender_member = db
.rooms
.state_get(
since_shortstatehash,
@ -375,7 +371,11 @@ async fn sync_helper( @@ -375,7 +371,11 @@ async fn sync_helper(
sender_user.as_str(),
)?
.and_then(|pdu| {
serde_json::from_str(pdu.content.get())
serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
pdu.content.clone(),
)
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid PDU in database."))
.ok()
});
@ -425,16 +425,18 @@ async fn sync_helper( @@ -425,16 +425,18 @@ async fn sync_helper(
}
if let Some(state_key) = &state_event.state_key {
let user_id = UserId::parse(state_key.clone())
let user_id = UserId::try_from(state_key.clone())
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
if user_id == sender_user {
continue;
}
let new_membership = serde_json::from_str::<RoomMemberEventContent>(
state_event.content.get(),
)
let new_membership = serde_json::from_value::<
Raw<ruma::events::room::member::MemberEventContent>,
>(state_event.content.clone())
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid PDU in database."))?
.membership;
@ -523,18 +525,18 @@ async fn sync_helper( @@ -523,18 +525,18 @@ async fn sync_helper(
Ok(Some(db.rooms.pdu_count(pdu_id)?.to_string()))
})?;
let room_events: Vec<_> = timeline_pdus
let room_events = timeline_pdus
.iter()
.map(|(_, pdu)| pdu.to_sync_room_event())
.collect();
.collect::<Vec<_>>();
let mut edus: Vec<_> = db
let mut edus = db
.rooms
.edus
.readreceipts_since(&room_id, since)
.filter_map(|r| r.ok()) // Filter out buggy events
.map(|(_, _, v)| v)
.collect();
.collect::<Vec<_>>();
if db.rooms.edus.last_typing_update(&room_id, &db.globals)? > since {
edus.push(
@ -563,7 +565,7 @@ async fn sync_helper( @@ -563,7 +565,7 @@ async fn sync_helper(
.map_err(|_| Error::bad_database("Invalid account event in database."))
.ok()
})
.collect(),
.collect::<Vec<_>>(),
},
summary: sync_events::RoomSummary {
heroes,
@ -628,7 +630,7 @@ async fn sync_helper( @@ -628,7 +630,7 @@ async fn sync_helper(
}
let mut left_rooms = BTreeMap::new();
let all_left_rooms: Vec<_> = db.rooms.rooms_left(&sender_user).collect();
let all_left_rooms = db.rooms.rooms_left(&sender_user).collect::<Vec<_>>();
for result in all_left_rooms {
let (room_id, left_state_events) = result?;
@ -668,7 +670,7 @@ async fn sync_helper( @@ -668,7 +670,7 @@ async fn sync_helper(
}
let mut invited_rooms = BTreeMap::new();
let all_invited_rooms: Vec<_> = db.rooms.rooms_invited(&sender_user).collect();
let all_invited_rooms = db.rooms.rooms_invited(&sender_user).collect::<Vec<_>>();
for result in all_invited_rooms {
let (room_id, invite_state_events) = result?;
@ -737,7 +739,7 @@ async fn sync_helper( @@ -737,7 +739,7 @@ async fn sync_helper(
presence: sync_events::Presence {
events: presence_updates
.into_iter()
.map(|(_, v)| Raw::new(&v).expect("PresenceEvent always serializes successfully"))
.map(|(_, v)| Raw::from(v))
.collect(),
},
account_data: sync_events::GlobalAccountData {
@ -750,13 +752,19 @@ async fn sync_helper( @@ -750,13 +752,19 @@ async fn sync_helper(
.map_err(|_| Error::bad_database("Invalid account event in database."))
.ok()
})
.collect(),
.collect::<Vec<_>>(),
},
device_lists: sync_events::DeviceLists {
changed: device_list_updates.into_iter().collect(),
left: device_list_left.into_iter().collect(),
},
device_one_time_keys_count: db.users.count_one_time_keys(&sender_user, &sender_device)?,
device_one_time_keys_count: if db.users.last_one_time_keys_update(&sender_user)? > since
|| since == 0
{
db.users.count_one_time_keys(&sender_user, &sender_device)?
} else {
BTreeMap::new()
},
to_device: sync_events::ToDevice {
events: db
.users
@ -770,6 +778,7 @@ async fn sync_helper( @@ -770,6 +778,7 @@ async fn sync_helper(
&& response.presence.is_empty()
&& response.account_data.is_empty()
&& response.device_lists.is_empty()
&& response.device_one_time_keys_count.is_empty()
&& response.to_device.is_empty()
{
// Hang a few seconds so requests are not spammed
@ -794,7 +803,7 @@ fn share_encrypted_room( @@ -794,7 +803,7 @@ fn share_encrypted_room(
) -> Result<bool> {
Ok(db
.rooms
.get_shared_rooms(vec![sender_user.to_owned(), user_id.to_owned()])?
.get_shared_rooms(vec![sender_user.clone(), user_id.clone()])?
.filter_map(|r| r.ok())
.filter(|room_id| room_id != ignore_room)
.filter_map(|other_room_id| {

23
src/client_server/tag.rs

@ -1,10 +1,7 @@ @@ -1,10 +1,7 @@
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
use ruma::{
api::client::r0::tag::{create_tag, delete_tag, get_tags},
events::{
tag::{TagEvent, TagEventContent},
EventType,
},
events::EventType,
};
use std::collections::BTreeMap;
@ -29,9 +26,9 @@ pub async fn update_tag_route( @@ -29,9 +26,9 @@ pub async fn update_tag_route(
let mut tags_event = db
.account_data
.get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| TagEvent {
content: TagEventContent {
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent {
content: ruma::events::tag::TagEventContent {
tags: BTreeMap::new(),
},
});
@ -71,9 +68,9 @@ pub async fn delete_tag_route( @@ -71,9 +68,9 @@ pub async fn delete_tag_route(
let mut tags_event = db
.account_data
.get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| TagEvent {
content: TagEventContent {
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent {
content: ruma::events::tag::TagEventContent {
tags: BTreeMap::new(),
},
});
@ -111,9 +108,9 @@ pub async fn get_tags_route( @@ -111,9 +108,9 @@ pub async fn get_tags_route(
Ok(get_tags::Response {
tags: db
.account_data
.get(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| TagEvent {
content: TagEventContent {
.get::<ruma::events::tag::TagEvent>(Some(&body.room_id), sender_user, EventType::Tag)?
.unwrap_or_else(|| ruma::events::tag::TagEvent {
content: ruma::events::tag::TagEventContent {
tags: BTreeMap::new(),
},
})

55
src/client_server/voip.rs

@ -1,11 +1,6 @@ @@ -1,11 +1,6 @@
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
use hmac::{Hmac, Mac, NewMac};
use crate::ConduitResult;
use ruma::api::client::r0::voip::get_turn_server_info;
use ruma::SecondsSinceUnixEpoch;
use sha1::Sha1;
use std::time::{Duration, SystemTime};
type HmacSha1 = Hmac<Sha1>;
use std::time::Duration;
#[cfg(feature = "conduit_bin")]
use rocket::get;
@ -13,46 +8,14 @@ use rocket::get; @@ -13,46 +8,14 @@ use rocket::get;
/// # `GET /_matrix/client/r0/voip/turnServer`
///
/// TODO: Returns information about the recommended turn server.
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/client/r0/voip/turnServer", data = "<body>")
)]
#[tracing::instrument(skip(body, db))]
pub async fn turn_server_route(
body: Ruma<get_turn_server_info::Request>,
db: DatabaseGuard,
) -> ConduitResult<get_turn_server_info::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let turn_secret = db.globals.turn_secret();
let (username, password) = if !turn_secret.is_empty() {
let expiry = SecondsSinceUnixEpoch::from_system_time(
SystemTime::now() + Duration::from_secs(db.globals.turn_ttl()),
)
.expect("time is valid");
let username: String = format!("{}:{}", expiry.get(), sender_user);
let mut mac = HmacSha1::new_from_slice(turn_secret.as_bytes())
.expect("HMAC can take key of any size");
mac.update(username.as_bytes());
let password: String = base64::encode_config(mac.finalize().into_bytes(), base64::STANDARD);
(username, password)
} else {
(
db.globals.turn_username().clone(),
db.globals.turn_password().clone(),
)
};
#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/voip/turnServer"))]
#[tracing::instrument]
pub async fn turn_server_route() -> ConduitResult<get_turn_server_info::Response> {
Ok(get_turn_server_info::Response {
username,
password,
uris: db.globals.turn_uris().to_vec(),
ttl: Duration::from_secs(db.globals.turn_ttl()),
username: "".to_owned(),
password: "".to_owned(),
uris: Vec::new(),
ttl: Duration::from_secs(60 * 60 * 24),
}
.into())
}

44
src/database.rs

@ -61,8 +61,6 @@ pub struct Config { @@ -61,8 +61,6 @@ pub struct Config {
allow_encryption: bool,
#[serde(default = "false_fn")]
allow_federation: bool,
#[serde(default = "true_fn")]
allow_room_creation: bool,
#[serde(default = "false_fn")]
pub allow_jaeger: bool,
#[serde(default = "false_fn")]
@ -74,16 +72,6 @@ pub struct Config { @@ -74,16 +72,6 @@ pub struct Config {
trusted_servers: Vec<Box<ServerName>>,
#[serde(default = "default_log")]
pub log: String,
#[serde(default)]
turn_username: String,
#[serde(default)]
turn_password: String,
#[serde(default = "Vec::new")]
turn_uris: Vec<String>,
#[serde(default)]
turn_secret: String,
#[serde(default = "default_turn_ttl")]
turn_ttl: u64,
#[serde(flatten)]
catchall: BTreeMap<String, IgnoredAny>,
@ -141,10 +129,6 @@ fn default_log() -> String { @@ -141,10 +129,6 @@ fn default_log() -> String {
"info,state_res=warn,rocket=off,_=off,sled=off".to_owned()
}
fn default_turn_ttl() -> u64 {
60 * 60 * 24
}
#[cfg(feature = "sled")]
pub type Engine = abstraction::sled::Engine;
@ -476,9 +460,10 @@ impl Database { @@ -476,9 +460,10 @@ impl Database {
if db.globals.database_version()? < 6 {
// Set room member count
for (roomid, _) in db.rooms.roomid_shortstatehash.iter() {
let string = utils::string_from_bytes(&roomid).unwrap();
let room_id = <&RoomId>::try_from(string.as_str()).unwrap();
db.rooms.update_joined_count(room_id, &db)?;
let room_id =
RoomId::try_from(utils::string_from_bytes(&roomid).unwrap()).unwrap();
db.rooms.update_joined_count(&room_id, &db)?;
}
db.globals.bump_database_version(6)?;
@ -488,7 +473,7 @@ impl Database { @@ -488,7 +473,7 @@ impl Database {
if db.globals.database_version()? < 7 {
// Upgrade state store
let mut last_roomstates: HashMap<Box<RoomId>, u64> = HashMap::new();
let mut last_roomstates: HashMap<RoomId, u64> = HashMap::new();
let mut current_sstatehash: Option<u64> = None;
let mut current_room = None;
let mut current_state = HashSet::new();
@ -514,13 +499,13 @@ impl Database { @@ -514,13 +499,13 @@ impl Database {
if let Some(parent_stateinfo) = states_parents.last() {
let statediffnew = current_state
.difference(&parent_stateinfo.1)
.copied()
.cloned()
.collect::<HashSet<_>>();
let statediffremoved = parent_stateinfo
.1
.difference(&current_state)
.copied()
.cloned()
.collect::<HashSet<_>>();
(statediffnew, statediffremoved)
@ -569,7 +554,7 @@ impl Database { @@ -569,7 +554,7 @@ impl Database {
if let Some(current_sstatehash) = current_sstatehash {
handle_state(
current_sstatehash,
current_room.as_deref().unwrap(),
current_room.as_ref().unwrap(),
current_state,
&mut last_roomstates,
)?;
@ -585,9 +570,10 @@ impl Database { @@ -585,9 +570,10 @@ impl Database {
.get(&seventid)
.unwrap()
.unwrap();
let string = utils::string_from_bytes(&event_id).unwrap();
let event_id = <&EventId>::try_from(string.as_str()).unwrap();
let pdu = db.rooms.get_pdu(event_id).unwrap().unwrap();
let event_id =
EventId::try_from(utils::string_from_bytes(&event_id).unwrap())
.unwrap();
let pdu = db.rooms.get_pdu(&event_id).unwrap().unwrap();
if Some(&pdu.room_id) != current_room.as_ref() {
current_room = Some(pdu.room_id.clone());
@ -602,7 +588,7 @@ impl Database { @@ -602,7 +588,7 @@ impl Database {
if let Some(current_sstatehash) = current_sstatehash {
handle_state(
current_sstatehash,
current_room.as_deref().unwrap(),
current_room.as_ref().unwrap(),
current_state,
&mut last_roomstates,
)?;
@ -713,7 +699,7 @@ impl Database { @@ -713,7 +699,7 @@ impl Database {
println!("Deleting starts");
let batch2: Vec<_> = db
let batch2 = db
.rooms
.tokenids
.iter()
@ -725,7 +711,7 @@ impl Database { @@ -725,7 +711,7 @@ impl Database {
None
}
})
.collect();
.collect::<Vec<_>>();
for key in batch2 {
println!("del");

4
src/database/abstraction.rs

@ -22,7 +22,7 @@ pub trait Tree: Send + Sync { @@ -22,7 +22,7 @@ pub trait Tree: Send + Sync {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>;
fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>;
fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>;
fn remove(&self, key: &[u8]) -> Result<()>;
@ -35,7 +35,7 @@ pub trait Tree: Send + Sync { @@ -35,7 +35,7 @@ pub trait Tree: Send + Sync {
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
fn increment(&self, key: &[u8]) -> Result<Vec<u8>>;
fn increment_batch(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>;
fn increment_batch<'a>(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>;
fn scan_prefix<'a>(
&'a self,

24
src/database/admin.rs

@ -1,19 +1,21 @@ @@ -1,19 +1,21 @@
use std::{convert::TryInto, sync::Arc};
use std::{
convert::{TryFrom, TryInto},
sync::Arc,
};
use crate::{pdu::PduBuilder, Database};
use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{
events::{room::message::RoomMessageEventContent, EventType},
events::{room::message, EventType},
UserId,
};
use serde_json::value::to_raw_value;
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
use tracing::warn;
pub enum AdminCommand {
RegisterAppservice(serde_yaml::Value),
ListAppservices,
SendMessage(RoomMessageEventContent),
SendMessage(message::MessageEventContent),
}
#[derive(Clone)]
@ -33,14 +35,14 @@ impl Admin { @@ -33,14 +35,14 @@ impl Admin {
let guard = db.read().await;
let conduit_user = UserId::parse(format!("@conduit:{}", guard.globals.server_name()))
let conduit_user =
UserId::try_from(format!("@conduit:{}", guard.globals.server_name()))
.expect("@conduit:server_name is valid");
let conduit_room = guard
.rooms
.id_from_alias(
format!("#admins:{}", guard.globals.server_name())
.as_str()
&format!("#admins:{}", guard.globals.server_name())
.try_into()
.expect("#admins:server_name is a valid room alias"),
)
@ -56,7 +58,7 @@ impl Admin { @@ -56,7 +58,7 @@ impl Admin {
drop(guard);
let send_message = |message: RoomMessageEventContent,
let send_message = |message: message::MessageEventContent,
guard: RwLockReadGuard<'_, Database>,
mutex_lock: &MutexGuard<'_, ()>| {
guard
@ -64,7 +66,7 @@ impl Admin { @@ -64,7 +66,7 @@ impl Admin {
.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMessage,
content: to_raw_value(&message)
content: serde_json::to_value(message)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
@ -104,9 +106,9 @@ impl Admin { @@ -104,9 +106,9 @@ impl Admin {
count,
appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ")
);
send_message(RoomMessageEventContent::text_plain(output), guard, &state_lock);
send_message(message::MessageEventContent::text_plain(output), guard, &state_lock);
} else {
send_message(RoomMessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
send_message(message::MessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
}
}
AdminCommand::SendMessage(message) => {

45
src/database/globals.rs

@ -40,13 +40,13 @@ pub struct Globals { @@ -40,13 +40,13 @@ pub struct Globals {
dns_resolver: TokioAsyncResolver,
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
pub(super) server_signingkeys: Arc<dyn Tree>,
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
pub bad_event_ratelimiter: Arc<RwLock<HashMap<EventId, RateLimitState>>>,
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
pub servername_ratelimiter: Arc<RwLock<HashMap<Box<ServerName>, Arc<Semaphore>>>>,
pub sync_receivers: RwLock<HashMap<(Box<UserId>, Box<DeviceId>), SyncHandle>>,
pub roomid_mutex_insert: RwLock<HashMap<Box<RoomId>, Arc<Mutex<()>>>>,
pub roomid_mutex_state: RwLock<HashMap<Box<RoomId>, Arc<TokioMutex<()>>>>,
pub roomid_mutex_federation: RwLock<HashMap<Box<RoomId>, Arc<TokioMutex<()>>>>, // this lock will be held longer
pub sync_receivers: RwLock<HashMap<(UserId, Box<DeviceId>), SyncHandle>>,
pub roomid_mutex_insert: RwLock<HashMap<RoomId, Arc<Mutex<()>>>>,
pub roomid_mutex_state: RwLock<HashMap<RoomId, Arc<TokioMutex<()>>>>,
pub roomid_mutex_federation: RwLock<HashMap<RoomId, Arc<TokioMutex<()>>>>, // this lock will be held longer
pub rotate: RotationHandler,
}
@ -57,7 +57,8 @@ pub struct RotationHandler(broadcast::Sender<()>, broadcast::Receiver<()>); @@ -57,7 +57,8 @@ pub struct RotationHandler(broadcast::Sender<()>, broadcast::Receiver<()>);
impl RotationHandler {
pub fn new() -> Self {
let (s, r) = broadcast::channel(1);
let (s, r) = broadcast::channel::<()>(1);
Self(s, r)
}
@ -210,10 +211,6 @@ impl Globals { @@ -210,10 +211,6 @@ impl Globals {
self.config.allow_federation
}
pub fn allow_room_creation(&self) -> bool {
self.config.allow_room_creation
}
pub fn trusted_servers(&self) -> &[Box<ServerName>] {
&self.config.trusted_servers
}
@ -226,26 +223,6 @@ impl Globals { @@ -226,26 +223,6 @@ impl Globals {
self.jwt_decoding_key.as_ref()
}
pub fn turn_password(&self) -> &String {
&self.config.turn_password
}
pub fn turn_ttl(&self) -> u64 {
self.config.turn_ttl
}
pub fn turn_uris(&self) -> &[String] {
&self.config.turn_uris
}
pub fn turn_username(&self) -> &String {
&self.config.turn_username
}
pub fn turn_secret(&self) -> &String {
&self.config.turn_secret
}
/// TODO: the key valid until timestamp is only honored in room version > 4
/// Remove the outdated keys and insert the new ones.
///
@ -254,7 +231,7 @@ impl Globals { @@ -254,7 +231,7 @@ impl Globals {
&self,
origin: &ServerName,
new_keys: ServerSigningKeys,
) -> Result<BTreeMap<Box<ServerSigningKeyId>, VerifyKey>> {
) -> Result<BTreeMap<ServerSigningKeyId, VerifyKey>> {
// Not atomic, but this is not critical
let signingkeys = self.server_signingkeys.get(origin.as_bytes())?;
@ -293,12 +270,12 @@ impl Globals { @@ -293,12 +270,12 @@ impl Globals {
pub fn signing_keys_for(
&self,
origin: &ServerName,
) -> Result<BTreeMap<Box<ServerSigningKeyId>, VerifyKey>> {
) -> Result<BTreeMap<ServerSigningKeyId, VerifyKey>> {
let signingkeys = self
.server_signingkeys
.get(origin.as_bytes())?
.and_then(|bytes| serde_json::from_slice(&bytes).ok())
.map(|keys: ServerSigningKeys| {
.and_then(|bytes| serde_json::from_slice::<ServerSigningKeys>(&bytes).ok())
.map(|keys| {
let mut tree = keys.verify_keys;
tree.extend(
keys.old_verify_keys

21
src/database/key_backups.rs

@ -6,7 +6,7 @@ use ruma::{ @@ -6,7 +6,7 @@ use ruma::{
},
RoomId, UserId,
};
use std::{collections::BTreeMap, sync::Arc};
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc};
use super::abstraction::Tree;
@ -81,7 +81,7 @@ impl KeyBackups { @@ -81,7 +81,7 @@ impl KeyBackups {
)?;
self.backupid_etag
.insert(&key, &globals.next_count()?.to_be_bytes())?;
Ok(version.to_owned())
Ok(version.to_string())
}
pub fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>> {
@ -94,15 +94,15 @@ impl KeyBackups { @@ -94,15 +94,15 @@ impl KeyBackups {
.iter_from(&last_possible_key, true)
.take_while(move |(k, _)| k.starts_with(&prefix))
.next()
.map(|(key, _)| {
.map_or(Ok(None), |(key, _)| {
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
.expect("rsplit always returns an element"),
)
.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)>> {
@ -115,7 +115,7 @@ impl KeyBackups { @@ -115,7 +115,7 @@ impl KeyBackups {
.iter_from(&last_possible_key, true)
.take_while(move |(k, _)| k.starts_with(&prefix))
.next()
.map(|(key, value)| {
.map_or(Ok(None), |(key, value)| {
let version = utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -123,14 +123,13 @@ impl KeyBackups { @@ -123,14 +123,13 @@ impl KeyBackups {
)
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))?;
Ok((
Ok(Some((
version,
serde_json::from_slice(&value).map_err(|_| {
Error::bad_database("Algorithm in backupid_algorithm is invalid.")
})?,
))
)))
})
.transpose()
}
pub fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<BackupAlgorithm>> {
@ -209,13 +208,13 @@ impl KeyBackups { @@ -209,13 +208,13 @@ impl KeyBackups {
&self,
user_id: &UserId,
version: &str,
) -> Result<BTreeMap<Box<RoomId>, RoomKeyBackup>> {
) -> Result<BTreeMap<RoomId, RoomKeyBackup>> {
let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(version.as_bytes());
prefix.push(0xff);
let mut rooms = BTreeMap::<Box<RoomId>, RoomKeyBackup>::new();
let mut rooms = BTreeMap::<RoomId, RoomKeyBackup>::new();
for result in self
.backupkeyid_backup
@ -231,7 +230,7 @@ impl KeyBackups { @@ -231,7 +230,7 @@ impl KeyBackups {
Error::bad_database("backupkeyid_backup session_id is invalid.")
})?;
let room_id = RoomId::parse(
let room_id = RoomId::try_from(
utils::string_from_bytes(parts.next().ok_or_else(|| {
Error::bad_database("backupkeyid_backup key is invalid.")
})?)

6
src/database/proxy.rs

@ -125,7 +125,7 @@ impl WildCardedDomain { @@ -125,7 +125,7 @@ impl WildCardedDomain {
}
impl std::str::FromStr for WildCardedDomain {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
// maybe do some domain validation?
Ok(if s.starts_with("*.") {
WildCardedDomain::WildCarded(s[1..].to_owned())
@ -136,8 +136,8 @@ impl std::str::FromStr for WildCardedDomain { @@ -136,8 +136,8 @@ impl std::str::FromStr for WildCardedDomain {
})
}
}
impl<'de> Deserialize<'de> for WildCardedDomain {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
impl<'de> serde::de::Deserialize<'de> for WildCardedDomain {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{

36
src/database/pusher.rs

@ -9,10 +9,8 @@ use ruma::{ @@ -9,10 +9,8 @@ use ruma::{
},
IncomingResponse, OutgoingRequest, SendAccessToken,
},
events::{
room::{name::RoomNameEventContent, power_levels::RoomPowerLevelsEventContent},
AnySyncRoomEvent, EventType,
},
events::{room::power_levels::PowerLevelsEventContent, AnySyncRoomEvent, EventType},
identifiers::RoomName,
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
serde::Raw,
uint, RoomId, UInt, UserId,
@ -179,11 +177,11 @@ pub async fn send_push_notice( @@ -179,11 +177,11 @@ pub async fn send_push_notice(
let mut notify = None;
let mut tweaks = Vec::new();
let power_levels: RoomPowerLevelsEventContent = db
let power_levels: PowerLevelsEventContent = db
.rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| {
serde_json::from_str(ev.content.get())
serde_json::from_value(ev.content.clone())
.map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
})
.transpose()?
@ -228,13 +226,13 @@ pub async fn send_push_notice( @@ -228,13 +226,13 @@ pub async fn send_push_notice(
pub fn get_actions<'a>(
user: &UserId,
ruleset: &'a Ruleset,
power_levels: &RoomPowerLevelsEventContent,
power_levels: &PowerLevelsEventContent,
pdu: &Raw<AnySyncRoomEvent>,
room_id: &RoomId,
db: &Database,
) -> Result<&'a [Action]> {
let ctx = PushConditionRoomCtx {
room_id: room_id.to_owned(),
room_id: room_id.clone(),
member_count: 10_u32.into(), // TODO: get member count efficiently
user_display_name: db
.users
@ -277,7 +275,7 @@ async fn send_notice( @@ -277,7 +275,7 @@ async fn send_notice(
let mut data_minus_url = pusher.data.clone();
// The url must be stripped off according to spec
data_minus_url.url = None;
device.data = data_minus_url;
device.data = Some(data_minus_url);
// Tweaks are only added if the format is NOT event_id_only
if !event_id_only {
@ -320,18 +318,16 @@ async fn send_notice( @@ -320,18 +318,16 @@ async fn send_notice(
let user_name = db.users.displayname(&event.sender)?;
notifi.sender_display_name = user_name.as_deref();
let room_name = if let Some(room_name_pdu) =
db.rooms
let room_name = db
.rooms
.room_state_get(&event.room_id, &EventType::RoomName, "")?
{
serde_json::from_str::<RoomNameEventContent>(room_name_pdu.content.get())
.map_err(|_| Error::bad_database("Invalid room name event in database."))?
.name
} else {
None
};
.map(|pdu| match pdu.content.get("name") {
Some(serde_json::Value::String(s)) => {
Some(Box::<RoomName>::try_from(&**s).expect("room name is valid"))
}
_ => None,
})
.flatten();
notifi.room_name = room_name.as_deref();
send_request(

542
src/database/rooms.rs

File diff suppressed because it is too large Load Diff

59
src/database/rooms/edus.rs

@ -11,7 +11,7 @@ use ruma::{ @@ -11,7 +11,7 @@ use ruma::{
};
use std::{
collections::{HashMap, HashSet},
convert::TryInto,
convert::{TryFrom, TryInto},
mem,
sync::Arc,
};
@ -76,13 +76,8 @@ impl RoomEdus { @@ -76,13 +76,8 @@ impl RoomEdus {
&'a self,
room_id: &RoomId,
since: u64,
) -> impl Iterator<
Item = Result<(
Box<UserId>,
u64,
Raw<ruma::events::AnySyncEphemeralRoomEvent>,
)>,
> + 'a {
) -> impl Iterator<Item = Result<(UserId, u64, Raw<ruma::events::AnySyncEphemeralRoomEvent>)>> + 'a
{
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let prefix2 = prefix.clone();
@ -97,7 +92,7 @@ impl RoomEdus { @@ -97,7 +92,7 @@ impl RoomEdus {
let count =
utils::u64_from_bytes(&k[prefix.len()..prefix.len() + mem::size_of::<u64>()])
.map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?;
let user_id = UserId::parse(
let user_id = UserId::try_from(
utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..])
.map_err(|_| {
Error::bad_database("Invalid readreceiptid userid bytes in db.")
@ -167,12 +162,11 @@ impl RoomEdus { @@ -167,12 +162,11 @@ impl RoomEdus {
Ok(self
.roomuserid_lastprivatereadupdate
.get(&key)?
.map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| {
.map_or(Ok::<_, Error>(None), |bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.")
})
})
.transpose()?
})?))
})?
.unwrap_or(0))
}
@ -292,12 +286,11 @@ impl RoomEdus { @@ -292,12 +286,11 @@ impl RoomEdus {
Ok(self
.roomid_lasttypingupdate
.get(room_id.as_bytes())?
.map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| {
.map_or(Ok::<_, Error>(None), |bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
})
})
.transpose()?
})?))
})?
.unwrap_or(0))
}
@ -310,13 +303,17 @@ impl RoomEdus { @@ -310,13 +303,17 @@ impl RoomEdus {
let mut user_ids = HashSet::new();
for (_, user_id) in self.typingid_userid.scan_prefix(prefix) {
let user_id = UserId::parse(utils::string_from_bytes(&user_id).map_err(|_| {
for user_id in self
.typingid_userid
.scan_prefix(prefix)
.map(|(_, user_id)| {
UserId::try_from(utils::string_from_bytes(&user_id).map_err(|_| {
Error::bad_database("User ID in typingid_userid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))?;
user_ids.insert(user_id);
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))
})
{
user_ids.insert(user_id?);
}
Ok(SyncEphemeralRoomEvent {
@ -334,7 +331,7 @@ impl RoomEdus { @@ -334,7 +331,7 @@ impl RoomEdus {
&self,
user_id: &UserId,
room_id: &RoomId,
presence: PresenceEvent,
presence: ruma::events::presence::PresenceEvent,
globals: &super::super::globals::Globals,
) -> Result<()> {
// TODO: Remove old entry? Or maybe just wipe completely from time to time?
@ -402,7 +399,7 @@ impl RoomEdus { @@ -402,7 +399,7 @@ impl RoomEdus {
self.presenceid_presence
.get(&presence_id)?
.map(|value| {
let mut presence: PresenceEvent = serde_json::from_slice(&value)
let mut presence = serde_json::from_slice::<PresenceEvent>(&value)
.map_err(|_| Error::bad_database("Invalid presence event in db."))?;
let current_timestamp: UInt = utils::millis_since_unix_epoch()
.try_into()
@ -450,7 +447,7 @@ impl RoomEdus { @@ -450,7 +447,7 @@ impl RoomEdus {
{
// Send new presence events to set the user offline
let count = globals.next_count()?.to_be_bytes();
let user_id: Box<_> = utils::string_from_bytes(&user_id_bytes)
let user_id = utils::string_from_bytes(&user_id_bytes)
.map_err(|_| {
Error::bad_database("Invalid UserId bytes in userid_lastpresenceupdate.")
})?
@ -476,7 +473,7 @@ impl RoomEdus { @@ -476,7 +473,7 @@ impl RoomEdus {
presence: PresenceState::Offline,
status_msg: None,
},
sender: user_id.to_owned(),
sender: user_id.clone(),
})
.expect("PresenceEvent can be serialized"),
)?;
@ -499,7 +496,7 @@ impl RoomEdus { @@ -499,7 +496,7 @@ impl RoomEdus {
since: u64,
_rooms: &super::Rooms,
_globals: &super::super::globals::Globals,
) -> Result<HashMap<Box<UserId>, PresenceEvent>> {
) -> Result<HashMap<UserId, PresenceEvent>> {
//self.presence_maintain(rooms, globals)?;
let mut prefix = room_id.as_bytes().to_vec();
@ -514,7 +511,7 @@ impl RoomEdus { @@ -514,7 +511,7 @@ impl RoomEdus {
.iter_from(&*first_possible_edu, false)
.take_while(|(key, _)| key.starts_with(&prefix))
{
let user_id = UserId::parse(
let user_id = UserId::try_from(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -524,7 +521,7 @@ impl RoomEdus { @@ -524,7 +521,7 @@ impl RoomEdus {
)
.map_err(|_| Error::bad_database("Invalid UserId in presenceid_presence."))?;
let mut presence: PresenceEvent = serde_json::from_slice(&value)
let mut presence = serde_json::from_slice::<PresenceEvent>(&value)
.map_err(|_| Error::bad_database("Invalid presence event in db."))?;
let current_timestamp: UInt = utils::millis_since_unix_epoch()

37
src/database/sending.rs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
convert::TryInto,
convert::{TryFrom, TryInto},
fmt::Debug,
sync::Arc,
time::{Duration, Instant},
@ -27,7 +27,7 @@ use ruma::{ @@ -27,7 +27,7 @@ use ruma::{
OutgoingRequest,
},
device_id,
events::{push_rules::PushRulesEvent, AnySyncEphemeralRoomEvent, EventType},
events::{push_rules, AnySyncEphemeralRoomEvent, EventType},
push,
receipt::ReceiptType,
uint, MilliSecondsSinceUnixEpoch, ServerName, UInt, UserId,
@ -165,13 +165,13 @@ impl Sending { @@ -165,13 +165,13 @@ impl Sending {
}
// Find events that have been added since starting the last request
let new_events: Vec<_> = guard.sending.servernameevent_data
let new_events = guard.sending.servernameevent_data
.scan_prefix(prefix.clone())
.filter_map(|(k, v)| {
Self::parse_servercurrentevent(&k, v).ok().map(|ev| (ev, k))
})
.take(30)
.collect::<>();
.collect::<Vec<_>>();
// TODO: find edus
@ -344,8 +344,8 @@ impl Sending { @@ -344,8 +344,8 @@ impl Sending {
continue;
}
let event: AnySyncEphemeralRoomEvent =
serde_json::from_str(read_receipt.json().get())
let event =
serde_json::from_str::<AnySyncEphemeralRoomEvent>(read_receipt.json().get())
.map_err(|_| Error::bad_database("Invalid edu event in read_receipts."))?;
let federation_event = match event {
AnySyncEphemeralRoomEvent::Receipt(r) => {
@ -397,8 +397,8 @@ impl Sending { @@ -397,8 +397,8 @@ impl Sending {
// Because synapse resyncs, we can just insert dummy data
let edu = Edu::DeviceListUpdate(DeviceListUpdateContent {
user_id,
device_id: device_id!("dummy").to_owned(),
device_display_name: Some("Dummy".to_owned()),
device_id: device_id!("dummy"),
device_display_name: "Dummy".to_owned(),
stream_id: uint!(1),
prev_id: Vec::new(),
deleted: None,
@ -485,7 +485,7 @@ impl Sending { @@ -485,7 +485,7 @@ impl Sending {
kind: OutgoingKind,
events: Vec<SendingEventType>,
db: Arc<RwLock<Database>>,
) -> Result<OutgoingKind, (OutgoingKind, Error)> {
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> {
let db = db.read().await;
match &kind {
@ -573,17 +573,12 @@ impl Sending { @@ -573,17 +573,12 @@ impl Sending {
for pdu in pdus {
// Redacted events are not notification targets (we don't send push for them)
if let Some(unsigned) = &pdu.unsigned {
if let Ok(unsigned) =
serde_json::from_str::<serde_json::Value>(unsigned.get())
{
if unsigned.get("redacted_because").is_some() {
if pdu.unsigned.get("redacted_because").is_some() {
continue;
}
}
}
let userid = UserId::parse(utils::string_from_bytes(user).map_err(|_| {
let userid =
UserId::try_from(utils::string_from_bytes(user).map_err(|_| {
(
kind.clone(),
Error::bad_database("Invalid push user string in db."),
@ -611,9 +606,9 @@ impl Sending { @@ -611,9 +606,9 @@ impl Sending {
let rules_for_user = db
.account_data
.get(None, &userid, EventType::PushRules)
.get::<push_rules::PushRulesEvent>(None, &userid, EventType::PushRules)
.unwrap_or_default()
.map(|ev: PushRulesEvent| ev.content.global)
.map(|ev| ev.content.global)
.unwrap_or_else(|| push::Ruleset::server_default(&userid));
let unread: UInt = db
@ -731,7 +726,7 @@ impl Sending { @@ -731,7 +726,7 @@ impl Sending {
})?;
(
OutgoingKind::Appservice(ServerName::parse(server).map_err(|_| {
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction")
})?),
if value.is_empty() {
@ -770,7 +765,7 @@ impl Sending { @@ -770,7 +765,7 @@ impl Sending {
})?;
(
OutgoingKind::Normal(ServerName::parse(server).map_err(|_| {
OutgoingKind::Normal(Box::<ServerName>::try_from(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction")
})?),
if value.is_empty() {

25
src/database/uiaa.rs

@ -5,8 +5,7 @@ use ruma::{ @@ -5,8 +5,7 @@ use ruma::{
api::client::{
error::ErrorKind,
r0::uiaa::{
AuthType, IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId,
UiaaInfo,
IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId, UiaaInfo,
},
},
signatures::CanonicalJsonValue,
@ -100,10 +99,10 @@ impl Uiaa { @@ -100,10 +99,10 @@ impl Uiaa {
}
// Password was correct! Let's add it to `completed`
uiaainfo.completed.push(AuthType::Password);
uiaainfo.completed.push("m.login.password".to_owned());
}
IncomingAuthData::Dummy(_) => {
uiaainfo.completed.push(AuthType::Dummy);
uiaainfo.completed.push("m.login.dummy".to_owned());
}
k => error!("type not supported: {:?}", k),
}
@ -175,14 +174,16 @@ impl Uiaa { @@ -175,14 +174,16 @@ impl Uiaa {
self.userdevicesessionid_uiaarequest
.get(&userdevicesessionid)?
.map(|bytes| {
.map_or(Ok(None), |bytes| {
Ok::<_, Error>(Some(
serde_json::from_str::<CanonicalJsonValue>(
&utils::string_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Invalid uiaa request bytes in db."))?,
&utils::string_from_bytes(&bytes).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(
@ -223,7 +224,7 @@ impl Uiaa { @@ -223,7 +224,7 @@ impl Uiaa {
userdevicesessionid.push(0xff);
userdevicesessionid.extend_from_slice(session.as_bytes());
serde_json::from_slice(
let uiaainfo = serde_json::from_slice::<UiaaInfo>(
&self
.userdevicesessionid_uiaainfo
.get(&userdevicesessionid)?
@ -232,6 +233,8 @@ impl Uiaa { @@ -232,6 +233,8 @@ impl Uiaa {
"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)
}
}

54
src/database/users.rs

@ -5,10 +5,9 @@ use ruma::{ @@ -5,10 +5,9 @@ use ruma::{
events::{AnyToDeviceEvent, EventType},
identifiers::MxcUri,
serde::Raw,
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, RoomAliasId, UInt,
UserId,
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, UInt, UserId,
};
use std::{collections::BTreeMap, convert::TryInto, mem, sync::Arc};
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc};
use tracing::warn;
use super::abstraction::Tree;
@ -54,21 +53,6 @@ impl Users { @@ -54,21 +53,6 @@ impl Users {
.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::parse(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();
rooms.is_joined(user_id, &admin_room_id)
}
/// Create a new user account on this homeserver.
#[tracing::instrument(skip(self, user_id, password))]
pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
@ -84,7 +68,7 @@ impl Users { @@ -84,7 +68,7 @@ impl Users {
/// Find out which user an access token belongs to.
#[tracing::instrument(skip(self, token))]
pub fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> {
pub fn find_from_token(&self, token: &str) -> Result<Option<(UserId, String)>> {
self.token_userdeviceid
.get(token.as_bytes())?
.map_or(Ok(None), |bytes| {
@ -97,7 +81,7 @@ impl Users { @@ -97,7 +81,7 @@ impl Users {
})?;
Ok(Some((
UserId::parse(utils::string_from_bytes(user_bytes).map_err(|_| {
UserId::try_from(utils::string_from_bytes(user_bytes).map_err(|_| {
Error::bad_database("User ID in token_userdeviceid is invalid unicode.")
})?)
.map_err(|_| {
@ -112,9 +96,9 @@ impl Users { @@ -112,9 +96,9 @@ impl Users {
/// Returns an iterator over all users on this homeserver.
#[tracing::instrument(skip(self))]
pub fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ {
pub fn iter(&self) -> impl Iterator<Item = Result<UserId>> + '_ {
self.userid_password.iter().map(|(bytes, _)| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in userid_password is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in userid_password is invalid."))
@ -180,21 +164,20 @@ impl Users { @@ -180,21 +164,20 @@ impl Users {
/// Get the avatar_url of a user.
#[tracing::instrument(skip(self, user_id))]
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<Box<MxcUri>>> {
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<MxcUri>> {
self.userid_avatarurl
.get(user_id.as_bytes())?
.map(|bytes| {
let s = utils::string_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))?;
s.try_into()
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))
MxcUri::try_from(s).map_err(|_| Error::bad_database("Avatar URL in db is invalid."))
})
.transpose()
}
/// Sets a new avatar_url or removes it if avatar_url is None.
#[tracing::instrument(skip(self, user_id, avatar_url))]
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<Box<MxcUri>>) -> Result<()> {
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<MxcUri>) -> Result<()> {
if let Some(avatar_url) = avatar_url {
self.userid_avatarurl
.insert(user_id.as_bytes(), avatar_url.to_string().as_bytes())?;
@ -409,7 +392,7 @@ impl Users { @@ -409,7 +392,7 @@ impl Users {
device_id: &DeviceId,
key_algorithm: &DeviceKeyAlgorithm,
globals: &super::globals::Globals,
) -> Result<Option<(Box<DeviceKeyId>, OneTimeKey)>> {
) -> Result<Option<(DeviceKeyId, OneTimeKey)>> {
let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(device_id.as_bytes());
@ -459,7 +442,7 @@ impl Users { @@ -459,7 +442,7 @@ impl Users {
.scan_prefix(userdeviceid)
.map(|(bytes, _)| {
Ok::<_, Error>(
serde_json::from_slice::<Box<DeviceKeyId>>(
serde_json::from_slice::<DeviceKeyId>(
&*bytes.rsplit(|&b| b == 0xff).next().ok_or_else(|| {
Error::bad_database("OneTimeKey ID in db is invalid.")
})?,
@ -620,11 +603,10 @@ impl Users { @@ -620,11 +603,10 @@ impl Users {
key.push(0xff);
key.extend_from_slice(key_id.as_bytes());
let mut cross_signing_key: serde_json::Value =
serde_json::from_slice(&self.keyid_key.get(&key)?.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Tried to sign nonexistent key.",
))?)
let mut cross_signing_key =
serde_json::from_slice::<serde_json::Value>(&self.keyid_key.get(&key)?.ok_or(
Error::BadRequest(ErrorKind::InvalidParam, "Tried to sign nonexistent key."),
)?)
.map_err(|_| Error::bad_database("key in keyid_key is invalid."))?;
let signatures = cross_signing_key
@ -632,7 +614,7 @@ impl Users { @@ -632,7 +614,7 @@ impl Users {
.ok_or_else(|| Error::bad_database("key in keyid_key has no signatures field."))?
.as_object_mut()
.ok_or_else(|| Error::bad_database("key in keyid_key has invalid signatures field."))?
.entry(sender_id.to_owned())
.entry(sender_id.clone())
.or_insert_with(|| serde_json::Map::new().into());
signatures
@ -657,7 +639,7 @@ impl Users { @@ -657,7 +639,7 @@ impl Users {
user_or_room_id: &str,
from: u64,
to: Option<u64>,
) -> impl Iterator<Item = Result<Box<UserId>>> + 'a {
) -> impl Iterator<Item = Result<UserId>> + 'a {
let mut prefix = user_or_room_id.as_bytes().to_vec();
prefix.push(0xff);
@ -683,7 +665,7 @@ impl Users { @@ -683,7 +665,7 @@ impl Users {
}
})
.map(|(_, bytes)| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in devicekeychangeid_userid is invalid."))

2
src/error.rs

@ -20,7 +20,7 @@ use { @@ -20,7 +20,7 @@ use {
tracing::error,
};
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {

6
src/lib.rs

@ -1,9 +1,3 @@ @@ -1,9 +1,3 @@
#![warn(
rust_2018_idioms,
unused_qualifications,
clippy::cloned_instead_of_copied,
clippy::str_to_string
)]
#![allow(clippy::suspicious_else_formatting)]
#![deny(clippy::dbg_macro)]

8
src/main.rs

@ -1,9 +1,4 @@ @@ -1,9 +1,4 @@
#![warn(
rust_2018_idioms,
unused_qualifications,
clippy::cloned_instead_of_copied,
clippy::str_to_string
)]
#![warn(rust_2018_idioms)]
#![allow(clippy::suspicious_else_formatting)]
#![deny(clippy::dbg_macro)]
@ -101,7 +96,6 @@ fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket< @@ -101,7 +96,6 @@ fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<
client_server::create_typing_event_route,
client_server::create_room_route,
client_server::redact_event_route,
client_server::report_event_route,
client_server::create_alias_route,
client_server::delete_alias_route,
client_server::get_alias_route,

132
src/pdu.rs

@ -1,55 +1,45 @@ @@ -1,55 +1,45 @@
use crate::Error;
use ruma::{
events::{
room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyInitialStateEvent,
AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent,
EventType, StateEvent,
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent,
AnyInitialStateEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent,
AnySyncStateEvent, EventType, StateEvent,
},
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UInt, UserId,
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, UInt, UserId,
};
use serde::{Deserialize, Serialize};
use serde_json::{
json,
value::{to_raw_value, RawValue as RawJsonValue},
};
use std::{cmp::Ordering, collections::BTreeMap, convert::TryInto, sync::Arc};
use serde_json::json;
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
use tracing::warn;
/// 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 event_id: Arc<EventId>,
pub room_id: Box<RoomId>,
pub sender: Box<UserId>,
pub event_id: EventId,
pub room_id: RoomId,
pub sender: UserId,
pub origin_server_ts: UInt,
#[serde(rename = "type")]
pub kind: EventType,
pub content: Box<RawJsonValue>,
pub content: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>,
pub prev_events: Vec<Arc<EventId>>,
pub prev_events: Vec<EventId>,
pub depth: UInt,
pub auth_events: Vec<Arc<EventId>>,
pub auth_events: Vec<EventId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<Arc<EventId>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unsigned: Option<Box<RawJsonValue>>,
pub redacts: Option<EventId>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub unsigned: BTreeMap<String, serde_json::Value>,
pub hashes: EventHash,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<Box<RawJsonValue>>, // BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
pub signatures: BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>,
}
impl PduEvent {
#[tracing::instrument(skip(self))]
pub fn redact(&mut self, reason: &PduEvent) -> crate::Result<()> {
self.unsigned = None;
self.unsigned.clear();
let allowed: &[&str] = match self.kind {
EventType::RoomMember => &["membership"],
@ -69,9 +59,10 @@ impl PduEvent { @@ -69,9 +59,10 @@ impl PduEvent {
_ => &[],
};
let mut old_content: BTreeMap<String, serde_json::Value> =
serde_json::from_str(self.content.get())
.map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
let old_content = self
.content
.as_object_mut()
.ok_or_else(|| Error::bad_database("PDU in db has invalid content."))?;
let mut new_content = serde_json::Map::new();
@ -81,23 +72,12 @@ impl PduEvent { @@ -81,23 +72,12 @@ impl PduEvent {
}
}
self.unsigned = Some(to_raw_value(&json!({
"redacted_because": serde_json::to_value(reason).expect("to_value(PduEvent) always works")
})).expect("to string always works"));
self.content = to_raw_value(&new_content).expect("to string always works");
Ok(())
}
self.unsigned.insert(
"redacted_because".to_owned(),
serde_json::to_value(reason).expect("to_value(PduEvent) always works"),
);
pub fn remove_transaction_id(&mut self) -> crate::Result<()> {
if let Some(unsigned) = &self.unsigned {
let mut unsigned: BTreeMap<String, Box<RawJsonValue>> =
serde_json::from_str(unsigned.get())
.map_err(|_| Error::bad_database("Invalid unsigned in pdu event"))?;
unsigned.remove("transaction_id");
self.unsigned = Some(to_raw_value(&unsigned).expect("unsigned is valid"));
}
self.content = new_content.into();
Ok(())
}
@ -212,7 +192,7 @@ impl PduEvent { @@ -212,7 +192,7 @@ impl PduEvent {
}
#[tracing::instrument(skip(self))]
pub fn to_member_event(&self) -> Raw<StateEvent<RoomMemberEventContent>> {
pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> {
let json = json!({
"content": self.content,
"type": self.kind,
@ -232,7 +212,7 @@ impl PduEvent { @@ -232,7 +212,7 @@ impl PduEvent {
#[tracing::instrument]
pub fn convert_to_outgoing_federation_event(
mut pdu_json: CanonicalJsonObject,
) -> Box<RawJsonValue> {
) -> Raw<ruma::events::pdu::Pdu> {
if let Some(unsigned) = pdu_json
.get_mut("unsigned")
.and_then(|val| val.as_object_mut())
@ -249,7 +229,10 @@ impl PduEvent { @@ -249,7 +229,10 @@ impl PduEvent {
// )
// .expect("Raw::from_value always works")
to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
serde_json::from_value::<Raw<_>>(
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
)
.expect("Raw::from_value always works")
}
pub fn from_id_val(
@ -257,7 +240,7 @@ impl PduEvent { @@ -257,7 +240,7 @@ impl PduEvent {
mut json: CanonicalJsonObject,
) -> Result<Self, serde_json::Error> {
json.insert(
"event_id".to_owned(),
"event_id".to_string(),
CanonicalJsonValue::String(event_id.as_str().to_owned()),
);
@ -266,9 +249,7 @@ impl PduEvent { @@ -266,9 +249,7 @@ impl PduEvent {
}
impl state_res::Event for PduEvent {
type Id = Arc<EventId>;
fn event_id(&self) -> &Self::Id {
fn event_id(&self) -> &EventId {
&self.event_id
}
@ -284,7 +265,7 @@ impl state_res::Event for PduEvent { @@ -284,7 +265,7 @@ impl state_res::Event for PduEvent {
&self.kind
}
fn content(&self) -> &RawJsonValue {
fn content(&self) -> &serde_json::Value {
&self.content
}
@ -296,17 +277,33 @@ impl state_res::Event for PduEvent { @@ -296,17 +277,33 @@ impl state_res::Event for PduEvent {
self.state_key.as_deref()
}
fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> {
Box::new(self.prev_events.iter())
}
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
fn depth(&self) -> &UInt {
&self.depth
}
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> {
Box::new(self.auth_events.iter())
}
fn redacts(&self) -> Option<&Self::Id> {
fn redacts(&self) -> Option<&EventId> {
self.redacts.as_ref()
}
fn hashes(&self) -> &EventHash {
&self.hashes
}
fn signatures(&self) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
self.signatures.clone()
}
fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
&self.unsigned
}
}
// These impl's allow us to dedup state snapshots when resolving state
@ -332,20 +329,19 @@ impl Ord for PduEvent { @@ -332,20 +329,19 @@ impl Ord for PduEvent {
///
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json(
pdu: &RawJsonValue,
) -> crate::Result<(Box<EventId>, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.get()).map_err(|e| {
pdu: &Raw<ruma::events::pdu::Pdu>,
) -> crate::Result<(EventId, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.json().get()).map_err(|e| {
warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response")
})?;
let event_id = format!(
let event_id = EventId::try_from(&*format!(
"${}",
// Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
ruma::signatures::reference_hash(&value, &RoomVersionId::Version6)
.expect("ruma can calculate reference hashes")
)
.try_into()
))
.expect("ruma's reference hashes are valid event ids");
Ok((event_id, value))
@ -356,10 +352,10 @@ pub(crate) fn gen_event_id_canonical_json( @@ -356,10 +352,10 @@ pub(crate) fn gen_event_id_canonical_json(
pub struct PduBuilder {
#[serde(rename = "type")]
pub event_type: EventType,
pub content: Box<RawJsonValue>,
pub content: serde_json::Value,
pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
pub state_key: Option<String>,
pub redacts: Option<Arc<EventId>>,
pub redacts: Option<EventId>,
}
/// Direct conversion prevents loss of the empty `state_key` that ruma requires.
@ -367,7 +363,7 @@ impl From<AnyInitialStateEvent> for PduBuilder { @@ -367,7 +363,7 @@ impl From<AnyInitialStateEvent> for PduBuilder {
fn from(event: AnyInitialStateEvent) -> Self {
Self {
event_type: EventType::from(event.event_type()),
content: to_raw_value(&event.content())
content: serde_json::value::to_value(event.content())
.expect("AnyStateEventContent came from JSON and can thus turn back into JSON."),
unsigned: None,
state_key: Some(event.state_key().to_owned()),

12
src/ruma_wrapper.rs

@ -20,6 +20,7 @@ use { @@ -20,6 +20,7 @@ use {
},
ruma::api::{AuthScheme, IncomingRequest},
std::collections::BTreeMap,
std::convert::TryFrom,
std::io::Cursor,
tracing::{debug, warn},
};
@ -28,7 +29,7 @@ use { @@ -28,7 +29,7 @@ use {
/// first.
pub struct Ruma<T: Outgoing> {
pub body: T::Incoming,
pub sender_user: Option<Box<UserId>>,
pub sender_user: Option<UserId>,
pub sender_device: Option<Box<DeviceId>>,
pub sender_servername: Option<Box<ServerName>>,
// This is None when body is not a valid string
@ -85,7 +86,7 @@ where @@ -85,7 +86,7 @@ where
registration
.get("as_token")
.and_then(|as_token| as_token.as_str())
.map_or(false, |as_token| token == Some(as_token))
.map_or(false, |as_token| token.as_deref() == Some(as_token))
}) {
match metadata.authentication {
AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken => {
@ -102,7 +103,8 @@ where @@ -102,7 +103,8 @@ where
.unwrap()
},
|string| {
UserId::parse(string.expect("parsing to string always works")).unwrap()
UserId::try_from(string.expect("parsing to string always works"))
.unwrap()
},
);
@ -169,7 +171,7 @@ where @@ -169,7 +171,7 @@ where
}
};
let origin = match ServerName::parse(origin_str) {
let origin = match Box::<ServerName>::try_from(origin_str) {
Ok(s) => s,
_ => {
warn!(
@ -342,7 +344,7 @@ impl<T: Outgoing> Deref for Ruma<T> { @@ -342,7 +344,7 @@ impl<T: Outgoing> Deref for Ruma<T> {
}
/// This struct converts ruma responses into rocket http responses.
pub type ConduitResult<T> = Result<RumaResponse<T>, Error>;
pub type ConduitResult<T> = std::result::Result<RumaResponse<T>, Error>;
pub fn response<T: OutgoingResponse>(response: RumaResponse<T>) -> response::Result<'static> {
let http_response = response

346
src/server_server.rs

File diff suppressed because it is too large Load Diff

2
src/utils.rs

@ -123,7 +123,7 @@ pub fn deserialize_from_str< @@ -123,7 +123,7 @@ pub fn deserialize_from_str<
E: std::fmt::Display,
>(
deserializer: D,
) -> Result<T, D::Error> {
) -> std::result::Result<T, D::Error> {
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>
for Visitor<T, Err>

1
tests/sytest/sytest-whitelist

@ -510,4 +510,3 @@ remote user can join room with version 5 @@ -510,4 +510,3 @@ remote user can join room with version 5
remote user can join room with version 6
setting 'm.room.name' respects room powerlevel
setting 'm.room.power_levels' respects room powerlevel
Federation publicRoom Name/topic keys are correct

Loading…
Cancel
Save