Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73b764322f | ||
|
|
a1f51440e2 | ||
|
|
0d6ae2e1e7 | ||
|
|
d0baca44f8 | ||
|
|
5536bfdca5 | ||
|
|
3357bbec1e | ||
|
|
102ff574ea | ||
|
|
2770ce2cf6 | ||
|
|
7cd7f1923f | ||
|
|
43d693ca19 | ||
|
|
875b345a5f | ||
|
|
17adf096e9 | ||
|
|
487601a249 | ||
|
|
27788af022 | ||
|
|
4b39d7cb64 | ||
|
|
c53d79e287 | ||
|
|
984ad5ecd6 | ||
|
|
b546a5bf15 | ||
|
|
a87519fb71 | ||
|
|
9c3f1a9272 | ||
|
|
8518d59a0b | ||
|
|
4e68c22457 | ||
|
|
73d876643c | ||
|
|
22779c21d3 | ||
|
|
8c584887c9 | ||
|
|
33738dbbc2 | ||
|
|
a6bb9bbe68 | ||
|
|
b9eb39a9c6 | ||
|
|
364820aae4 | ||
|
|
ec38411620 | ||
|
|
9ec8b7f2b3 | ||
|
|
a08ea15695 | ||
|
|
49dd3567c6 | ||
|
|
69df9a0145 | ||
|
|
d0d47675d8 | ||
|
|
bd4ea14a29 | ||
|
|
9033cc86aa | ||
|
|
1b25e78e3a | ||
|
|
d57c19802d | ||
|
|
9ded40e983 | ||
|
|
a469ca0461 | ||
|
|
632a1343eb | ||
|
|
00c9ad12bd | ||
|
|
1601027605 | ||
|
|
afca61fe7c | ||
|
|
9bff276fa9 | ||
|
|
19b89ab91f | ||
|
|
0330d3e270 | ||
|
|
a1e8a99db5 | ||
|
|
33172a70e6 | ||
|
|
1b56a7bbfd | ||
|
|
d485eb5a24 | ||
|
|
41d07be97b | ||
|
|
bef4fe50ce | ||
|
|
9152b877a7 | ||
|
|
9f8c45c763 | ||
|
|
5800e9b797 | ||
|
|
5aa56b92ee | ||
|
|
dd87066546 | ||
|
|
30b309b708 | ||
|
|
d5695fb9fe | ||
|
|
81e056417c | ||
|
|
72dd95f500 | ||
|
|
b6e755f67e | ||
|
|
4f8cf3bed4 | ||
|
|
b3ea6adc9c | ||
|
|
6d83954c40 | ||
|
|
667ffb4239 | ||
|
|
48494c9464 | ||
|
|
2ef23b213a | ||
|
|
33481ec062 | ||
|
|
3b78e43a18 | ||
|
|
4956fb9fba | ||
|
|
b09499c2df | ||
|
|
46d8a46e1f | ||
|
|
cefca2d0d1 | ||
|
|
bf7e019a68 | ||
|
|
75ba8bb565 | ||
|
|
0823506d05 | ||
|
|
2c3bee34a0 | ||
|
|
a4310f840e | ||
|
|
5bd5b41c70 | ||
|
|
f9a2edc0dd | ||
|
|
ecd1e45a44 | ||
|
|
1d46569929 | ||
|
|
1e3a8ca35d | ||
|
|
0cb22996be | ||
|
|
9c3a8edcae | ||
|
|
38effda799 | ||
|
|
3cf0145bc5 | ||
|
|
3eabaa2a95 | ||
|
|
31f60ad6fd | ||
|
|
41dd620d74 | ||
|
|
9410d3ef9c | ||
|
|
665aee11c0 | ||
|
|
5173d0deb5 | ||
|
|
c2c6a8673e | ||
|
|
096e0971f1 | ||
|
|
8a5dbef474 | ||
|
|
4cf3c432af | ||
|
|
dd3c60fb5a | ||
|
|
e83c226b1b | ||
|
|
8335f44bfe | ||
|
|
3745a7cbe6 | ||
|
|
610b4f9ad1 | ||
|
|
879aeafa9e | ||
|
|
a7b6af7caf | ||
|
|
260db9fcc7 | ||
|
|
1cbc61c558 | ||
|
|
d2f406e0e8 | ||
|
|
a816e3decd | ||
|
|
989a20e99c | ||
|
|
1cf9da26bf | ||
|
|
55a2ff025f | ||
|
|
76660bf189 | ||
|
|
df727688ef | ||
|
|
902404d48d | ||
|
|
b813c34642 | ||
|
|
ab7835dedb | ||
|
|
9bb4c3cd01 | ||
|
|
fce22362d4 | ||
|
|
41ec7cf5d0 | ||
|
|
49ade0cfbd | ||
|
|
0eeba86b32 | ||
|
|
6b06fc9707 | ||
|
|
bd63797213 | ||
|
|
2c4f966d60 | ||
|
|
8174b16c38 | ||
|
|
6666aae5a5 | ||
|
|
0f045890eb | ||
|
|
9cb99a05e0 | ||
|
|
dc85a8fafd | ||
|
|
fcd127aadc | ||
|
|
5df6b8cd5f | ||
|
|
5c776e9ba7 | ||
|
|
c209775abd | ||
|
|
5e924227b6 | ||
|
|
734c4637f0 | ||
|
|
e0072eff63 | ||
|
|
cae2b200cc | ||
|
|
918df9ca6d | ||
|
|
387e864659 | ||
|
|
c84960df91 | ||
|
|
32db4c09a0 | ||
|
|
0d33cc4acd | ||
|
|
666e1d30a3 | ||
|
|
f43fd6a475 | ||
|
|
1587f2cd52 | ||
|
|
f121978897 | ||
|
|
80533bfab2 | ||
|
|
08a3326431 | ||
|
|
fe3b5d32a7 | ||
|
|
e20f55970e | ||
|
|
728e176a06 | ||
|
|
7d14a46607 | ||
|
|
68cc743f2a | ||
|
|
56e5d5e3d2 | ||
|
|
f0ac7ed933 | ||
|
|
0f8ecdc625 | ||
|
|
cfaa900e83 | ||
|
|
f5273f7eb1 | ||
|
|
0fcefa4125 | ||
|
|
d07762f596 | ||
|
|
d253f9236a | ||
|
|
ec44f3d568 | ||
|
|
167e903a56 | ||
|
|
0a8dadb797 | ||
|
|
678ce0abc6 | ||
|
|
e7a51c07d0 | ||
|
|
79bf7fc597 | ||
|
|
7e579f8d34 | ||
|
|
0f2dc9a239 | ||
|
|
59d538206f | ||
|
|
faa283d35b | ||
|
|
86160597ea | ||
|
|
d6b37480e7 | ||
|
|
3ce75d1f02 | ||
|
|
a50abeedad | ||
|
|
130b9841da | ||
|
|
ffa10a68b9 | ||
|
|
124b5484b5 | ||
|
|
2babff1e41 | ||
|
|
cdd01262d2 | ||
|
|
092221ca3f | ||
|
|
5d8ad4fd72 | ||
|
|
7054f74783 | ||
|
|
82a4ec9cbd | ||
|
|
f924ebf8a6 | ||
|
|
b1993421c2 | ||
|
|
8e2d6f7e43 | ||
|
|
d76e95e8fc | ||
|
|
661101c9ae | ||
|
|
f7ecf83ac3 | ||
|
|
9a4789efdc | ||
|
|
eaa4c77641 | ||
|
|
9268b33c3f | ||
|
|
9de32ae12f | ||
|
|
952fb75795 | ||
|
|
e12b1ff863 | ||
|
|
e15e6d4405 | ||
|
|
1c25492a7e | ||
|
|
8c44312485 | ||
|
|
ac0027756e | ||
|
|
f3e806096b | ||
|
|
e1b89c1248 | ||
|
|
be6b6c3cf0 | ||
|
|
15471d9ac4 | ||
|
|
3c236fb671 | ||
|
|
9d4fa9a220 | ||
|
|
bd4bd58612 | ||
|
|
0eec1cc696 | ||
|
|
3fa09ff57d | ||
|
|
0080932aef | ||
|
|
888a2f7fa1 | ||
|
|
dcb5e5900f | ||
|
|
4732aa6782 | ||
|
|
699f77671f | ||
|
|
fcfb9b51de | ||
|
|
36681dd3ac | ||
|
|
13c0beafa6 | ||
|
|
a313a93ff5 | ||
|
|
6a96cfaac1 | ||
|
|
09a8737f24 |
@@ -0,0 +1,11 @@
|
||||
---
|
||||
name: "Issue with / Feature Request for Conduit"
|
||||
about: "Please file issues on GitLab: https://gitlab.com/famedly/conduit/-/issues/new"
|
||||
title: "CLOSE ME"
|
||||
---
|
||||
|
||||
|
||||
|
||||
**⚠️ Conduit development does not happen on GitHub. Issues opened here will not be addressed**
|
||||
|
||||
Please open issues on GitLab: https://gitlab.com/famedly/conduit/-/issues/new
|
||||
@@ -59,6 +59,7 @@ $RECYCLE.BIN/
|
||||
# Conduit
|
||||
Rocket.toml
|
||||
conduit.toml
|
||||
conduit.db
|
||||
|
||||
# Etc.
|
||||
**/*.rs.bk
|
||||
|
||||
+281
-74
@@ -1,8 +1,8 @@
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
- publish artifacts
|
||||
- release nightly
|
||||
- build docker image
|
||||
- test
|
||||
- upload artifacts
|
||||
|
||||
variables:
|
||||
GIT_SUBMODULE_STRATEGY: recursive
|
||||
@@ -10,29 +10,6 @@ variables:
|
||||
CACHE_COMPRESSION_LEVEL: fastest
|
||||
|
||||
|
||||
test:cargo:
|
||||
stage: "test"
|
||||
needs: []
|
||||
image: "rust:latest"
|
||||
variables:
|
||||
CARGO_HOME: "cargohome"
|
||||
cache:
|
||||
paths:
|
||||
- target
|
||||
- cargohome
|
||||
key: test_cache
|
||||
interruptible: true
|
||||
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
|
||||
- rustup component add clippy rustfmt
|
||||
script:
|
||||
- rustc --version && cargo --version # Print version info for debugging
|
||||
- cargo test --workspace --verbose --locked
|
||||
- cargo fmt --all -- --check
|
||||
- cargo clippy
|
||||
|
||||
# --------------------------------------------------------------------- #
|
||||
# Cargo: Compiling for different architectures #
|
||||
# --------------------------------------------------------------------- #
|
||||
@@ -41,14 +18,20 @@ test:cargo:
|
||||
stage: "build"
|
||||
needs: []
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
|
||||
- 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-$TARGET"
|
||||
key: "build_cache-$TARGET-release"
|
||||
variables:
|
||||
CARGO_PROFILE_RELEASE_LTO=true
|
||||
CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
|
||||
before_script:
|
||||
- 'echo "Building for target $TARGET"'
|
||||
- 'mkdir -p cargohome && CARGOHOME="cargohome"'
|
||||
@@ -61,17 +44,20 @@ test:cargo:
|
||||
- time cargo build --target $TARGET --release
|
||||
- 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"'
|
||||
artifacts:
|
||||
name: "conduit-$TARGET"
|
||||
expose_as: "Binary"
|
||||
paths:
|
||||
- "conduit-$TARGET"
|
||||
expire_in: never
|
||||
|
||||
build:cargo:x86_64-unknown-linux-gnu:
|
||||
|
||||
build:release:cargo:x86_64-unknown-linux-gnu:
|
||||
extends: .build-cargo-shared-settings
|
||||
variables:
|
||||
TARGET: "x86_64-unknown-linux-gnu"
|
||||
artifacts:
|
||||
name: "conduit-x86_64-unknown-linux-gnu"
|
||||
paths:
|
||||
- "conduit-x86_64-unknown-linux-gnu"
|
||||
expose_as: "Conduit for x86_64-unknown-linux-gnu"
|
||||
|
||||
build:cargo:armv7-unknown-linux-gnueabihf:
|
||||
build:release:cargo:armv7-unknown-linux-gnueabihf:
|
||||
extends: .build-cargo-shared-settings
|
||||
variables:
|
||||
TARGET: "armv7-unknown-linux-gnueabihf"
|
||||
@@ -79,8 +65,13 @@ build:cargo:armv7-unknown-linux-gnueabihf:
|
||||
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-armv7-unknown-linux-gnueabihf"
|
||||
paths:
|
||||
- "conduit-armv7-unknown-linux-gnueabihf"
|
||||
expose_as: "Conduit for armv7-unknown-linux-gnueabihf"
|
||||
|
||||
build:cargo:aarch64-unknown-linux-gnu:
|
||||
build:release:cargo:aarch64-unknown-linux-gnu:
|
||||
extends: .build-cargo-shared-settings
|
||||
variables:
|
||||
TARGET: "aarch64-unknown-linux-gnu"
|
||||
@@ -90,6 +81,71 @@ build:cargo:aarch64-unknown-linux-gnu:
|
||||
CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++
|
||||
TARGET_CC: "/usr/bin/aarch64-linux-gnu-gcc-8"
|
||||
TARGET_AR: "/usr/bin/aarch64-linux-gnu-gcc-ar-8"
|
||||
artifacts:
|
||||
name: "conduit-aarch64-unknown-linux-gnu"
|
||||
paths:
|
||||
- "conduit-aarch64-unknown-linux-gnu"
|
||||
expose_as: "Conduit for aarch64-unknown-linux-gnu"
|
||||
|
||||
build:release:cargo:x86_64-unknown-linux-musl:
|
||||
extends: .build-cargo-shared-settings
|
||||
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-x86_64-unknown-linux-musl"
|
||||
paths:
|
||||
- "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'
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
cache:
|
||||
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: "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"
|
||||
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- #
|
||||
@@ -99,11 +155,14 @@ build:cargo:aarch64-unknown-linux-gnu:
|
||||
|
||||
.build-cargo-deb-shared-settings:
|
||||
stage: "build"
|
||||
needs: []
|
||||
needs: [ ]
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
|
||||
- if: '$CI_COMMIT_BRANCH == "master"'
|
||||
- if: '$CI_COMMIT_BRANCH == "next"'
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
interruptible: true
|
||||
image: "rust:latest"
|
||||
tags: ["docker"]
|
||||
cache:
|
||||
paths:
|
||||
- cargohome
|
||||
@@ -121,35 +180,208 @@ build:cargo:aarch64-unknown-linux-gnu:
|
||||
script:
|
||||
- time cargo deb --target $TARGET
|
||||
- 'mv target/$TARGET/debian/*.deb "conduit-$TARGET.deb"'
|
||||
artifacts:
|
||||
name: "conduit-$TARGET.deb"
|
||||
expose_as: "Debian Package"
|
||||
paths:
|
||||
- "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"
|
||||
needs: []
|
||||
interruptible: true
|
||||
image:
|
||||
name: "gcr.io/kaniko-project/executor:debug"
|
||||
entrypoint: [""]
|
||||
tags: ["docker"]
|
||||
variables:
|
||||
# Configure Kaniko Caching: https://cloud.google.com/build/docs/kaniko-cache
|
||||
KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=120h --cache-repo $CI_REGISTRY_IMAGE/kaniko-ci-cache"
|
||||
before_script:
|
||||
- "mkdir -p /kaniko/.docker"
|
||||
- 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"},\"$DOCKER_HUB\":{\"username\":\"$DOCKER_HUB_USER\",\"password\":\"$DOCKER_HUB_PASSWORD\"}}}" > /kaniko/.docker/config.json'
|
||||
|
||||
|
||||
build:docker:next:
|
||||
extends: .docker-shared-settings
|
||||
needs:
|
||||
- "build:release:cargo:x86_64-unknown-linux-musl"
|
||||
script:
|
||||
- >
|
||||
/kaniko/executor
|
||||
$KANIKO_CACHE_ARGS
|
||||
--context $CI_PROJECT_DIR
|
||||
--build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||
--build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
|
||||
--build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
|
||||
--dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile"
|
||||
--destination "$CI_REGISTRY_IMAGE/conduit:next"
|
||||
--destination "$CI_REGISTRY_IMAGE/conduit:next-alpine"
|
||||
--destination "$CI_REGISTRY_IMAGE/conduit:commit-$CI_COMMIT_SHORT_SHA"
|
||||
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"
|
||||
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next-alpine"
|
||||
--destination "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:commit-$CI_COMMIT_SHORT_SHA"
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "next"'
|
||||
|
||||
|
||||
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"'
|
||||
|
||||
|
||||
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_TAG'
|
||||
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- #
|
||||
# Storing and releasing compiled binaries #
|
||||
# Run tests #
|
||||
# --------------------------------------------------------------------- #
|
||||
|
||||
test:cargo:
|
||||
stage: "test"
|
||||
needs: [ ]
|
||||
image: "rust:latest"
|
||||
tags: [ "docker" ]
|
||||
variables:
|
||||
CARGO_HOME: "cargohome"
|
||||
cache:
|
||||
paths:
|
||||
- target
|
||||
- cargohome
|
||||
key: test_cache
|
||||
interruptible: true
|
||||
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
|
||||
- rustup component add clippy rustfmt
|
||||
script:
|
||||
- rustc --version && cargo --version # Print version info for debugging
|
||||
- cargo fmt --all -- --check
|
||||
- cargo test --workspace --verbose --locked
|
||||
- cargo clippy
|
||||
|
||||
test:sytest:
|
||||
stage: "test"
|
||||
allow_failure: true
|
||||
needs:
|
||||
- "build:debug:cargo:x86_64-unknown-linux-musl"
|
||||
image:
|
||||
name: "valkum/sytest-conduit:latest"
|
||||
entrypoint: [ "" ]
|
||||
tags: [ "docker" ]
|
||||
variables:
|
||||
PLUGINS: "https://github.com/valkum/sytest_conduit/archive/master.tar.gz"
|
||||
before_script:
|
||||
- "mkdir -p /app"
|
||||
- "cp ./conduit-debug-x86_64-unknown-linux-musl /app/conduit"
|
||||
- "chmod +x /app/conduit"
|
||||
- "rm -rf /src && ln -s $CI_PROJECT_DIR/ /src"
|
||||
- "mkdir -p /work/server-0/database/ && mkdir -p /work/server-1/database/ && mkdir -p /work/server-2/database/"
|
||||
- "cd /"
|
||||
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"
|
||||
- "exit $SYTEST_EXIT_CODE"
|
||||
artifacts:
|
||||
when: always
|
||||
paths:
|
||||
- "$CI_PROJECT_DIR/sytest.xml"
|
||||
- "$CI_PROJECT_DIR/results.tap"
|
||||
reports:
|
||||
junit: "$CI_PROJECT_DIR/sytest.xml"
|
||||
|
||||
|
||||
test:register:element-web-stable:
|
||||
stage: "test"
|
||||
needs:
|
||||
- "build:debug:cargo:x86_64-unknown-linux-gnu"
|
||||
image: "buildkite/puppeteer:latest"
|
||||
tags: [ "docker" ]
|
||||
interruptible: true
|
||||
script:
|
||||
- "CONDUIT_CONFIG=tests/test-config.toml ./conduit-debug-x86_64-unknown-linux-gnu > conduit.log &"
|
||||
- "cd tests/client-element-web/"
|
||||
- "npm install puppeteer"
|
||||
- "node test-element-web-registration.js \"https://app.element.io/\" \"http://localhost:6167\""
|
||||
- "killall --regexp \"conduit\""
|
||||
- "cd ../.."
|
||||
- "cat conduit.log"
|
||||
artifacts:
|
||||
paths:
|
||||
- "tests/client-element-web/*.png"
|
||||
- "*.log"
|
||||
expire_in: 1 week
|
||||
when: always
|
||||
retry: 1
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- #
|
||||
# Store binaries as package so they have download urls #
|
||||
# --------------------------------------------------------------------- #
|
||||
|
||||
publish:package:
|
||||
stage: "publish artifacts"
|
||||
stage: "upload artifacts"
|
||||
needs:
|
||||
- "build:cargo:x86_64-unknown-linux-gnu"
|
||||
- "build:cargo:armv7-unknown-linux-gnueabihf"
|
||||
- "build:cargo:aarch64-unknown-linux-gnu"
|
||||
- "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:cargo-deb:x86_64-unknown-linux-gnu"
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
|
||||
- if: '$CI_COMMIT_BRANCH == "master"'
|
||||
- if: '$CI_COMMIT_BRANCH == "next"'
|
||||
- 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:
|
||||
@@ -157,32 +389,7 @@ publish:package:
|
||||
- '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-x86_64-unknown-linux-gnu.deb "${BASE_URL}/conduit-x86_64-unknown-linux-gnu.deb"'
|
||||
|
||||
|
||||
|
||||
publish:nightly:
|
||||
stage: "release nightly"
|
||||
image: registry.gitlab.com/gitlab-org/release-cli:latest
|
||||
needs:
|
||||
- job: "publish:package"
|
||||
artifacts: false
|
||||
variables:
|
||||
GIT_STRATEGY: "none" # Don't need a clean copy of the code, we just operate on artifacts
|
||||
script: "echo 'Releasing current state as release'"
|
||||
rules:
|
||||
- if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH" # Run this job on the main branch
|
||||
release:
|
||||
name: 'Nightly'
|
||||
description: '⚠️ The latest development version of the day, fresh from the repository. Use at your own risk!'
|
||||
tag_name: 'nightly'
|
||||
assets:
|
||||
links:
|
||||
- name: 'conduit-x86_64-unknown-linux-gnu'
|
||||
url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}/conduit-x86_64-unknown-linux-gnu"
|
||||
- name: 'conduit-armv7-unknown-linux-gnueabihf'
|
||||
url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}/conduit-armv7-unknown-linux-gnueabihf"
|
||||
- name: 'conduit-aarch64-unknown-linux-gnu'
|
||||
url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}/conduit-aarch64-unknown-linux-gnu"
|
||||
- name: 'conduit-x86_64-unknown-linux-gnu.deb'
|
||||
url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}/conduit-x86_64-unknown-linux-gnu.deb"
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<!--
|
||||
If you're requesting a new feature, that isn't part of this project yet,
|
||||
then please consider filling out a "Feature Request" instead!
|
||||
|
||||
If you need a hand setting up your conduit server, feel free to ask for help in the
|
||||
Conduit Matrix Chat: https://matrix.to/#/#conduit:fachschaften.org.
|
||||
-->
|
||||
|
||||
### Description
|
||||
<!-- What did you do and what happened? Why is that bad? -->
|
||||
|
||||
### System Configuration
|
||||
<!-- Other data that might help us debug this issue, like os, conduit version, database backend -->
|
||||
|
||||
Conduit Version:
|
||||
Database backend (default is sqlite): sqlite
|
||||
|
||||
|
||||
/label ~conduit
|
||||
@@ -0,0 +1,17 @@
|
||||
<!--
|
||||
If you want to report a bug or an error,
|
||||
then please consider filling out a "Bug Report" instead!
|
||||
-->
|
||||
|
||||
|
||||
### Is your feature request related to a problem? Please describe.
|
||||
|
||||
<!-- Eg. I'm always frustrated when [...] -->
|
||||
|
||||
|
||||
### Describe the solution you'd like
|
||||
|
||||
|
||||
|
||||
|
||||
/label ~conduit
|
||||
@@ -1,15 +0,0 @@
|
||||
# Headline
|
||||
|
||||
### Description
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/label ~conduit
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
<!-- Please describe your changes here -->
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
- [ ] I ran `cargo fmt` and `cargo test`
|
||||
- [ ] I agree to release my code and all other changes of this MR under the Apache-2.0 license
|
||||
|
||||
Generated
+648
-460
File diff suppressed because it is too large
Load Diff
+54
-30
@@ -6,7 +6,7 @@ authors = ["timokoesters <timo@koesters.xyz>"]
|
||||
homepage = "https://conduit.rs"
|
||||
repository = "https://gitlab.com/famedly/conduit"
|
||||
readme = "README.md"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -14,45 +14,45 @@ edition = "2018"
|
||||
[dependencies]
|
||||
# Used to handle requests
|
||||
# TODO: This can become optional as soon as proper configs are supported
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "801e04bd5369eb39e126c75f6d11e1e9597304d8", features = ["tls"] } # Used to handle requests
|
||||
# rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "801e04bd5369eb39e126c75f6d11e1e9597304d8", features = ["tls"] } # Used to handle requests
|
||||
rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle requests
|
||||
|
||||
# Used for matrix spec type definitions and helpers
|
||||
ruma = { git = "https://github.com/ruma/ruma", rev = "174555857ef90d49e4b9a672be9e2fe0acdc2687", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
|
||||
ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
|
||||
#ruma = { git = "https://github.com/ruma/ruma", rev = "f5ab038e22421ed338396ece977b6b2844772ced", 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"] }
|
||||
|
||||
# Used for long polling and federation sender, should be the same as rocket::tokio
|
||||
tokio = "1.2.0"
|
||||
tokio = "1.11.0"
|
||||
# Used for storing data permanently
|
||||
sled = { version = "0.34.6", features = ["compression", "no_metrics"], optional = true }
|
||||
rocksdb = { version = "0.16.0", features = ["multi-threaded-cf"], optional = true }
|
||||
#sled = { git = "https://github.com/spacejam/sled.git", rev = "e4640e0773595229f398438886f19bca6f7326a2", features = ["compression"] }
|
||||
|
||||
# Used for the http request / response body type for Ruma endpoints used with reqwest
|
||||
bytes = "1.0.1"
|
||||
# Used for emitting log entries
|
||||
log = "0.4.14"
|
||||
bytes = "1.1.0"
|
||||
# Used for rocket<->ruma conversions
|
||||
http = "0.2.3"
|
||||
http = "0.2.4"
|
||||
# Used to find data directory for default db path
|
||||
directories = "3.0.1"
|
||||
directories = "3.0.2"
|
||||
# Used for ruma wrapper
|
||||
serde_json = { version = "1.0.64", features = ["raw_value"] }
|
||||
serde_json = { version = "1.0.67", features = ["raw_value"] }
|
||||
# Used for appservice registration files
|
||||
serde_yaml = "0.8.17"
|
||||
serde_yaml = "0.8.20"
|
||||
# Used for pdu definition
|
||||
serde = "1.0.123"
|
||||
serde = "1.0.130"
|
||||
# Used for secure identifiers
|
||||
rand = "0.8.3"
|
||||
rand = "0.8.4"
|
||||
# Used to hash passwords
|
||||
rust-argon2 = "0.8.3"
|
||||
# Used to send requests
|
||||
reqwest = { version = "0.11.3", default-features = false, features = ["rustls-tls-native-roots", "socks"] }
|
||||
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls-native-roots", "socks"] }
|
||||
# Custom TLS verifier
|
||||
rustls = { version = "0.19", features = ["dangerous_configuration"] }
|
||||
rustls = { version = "0.19.1", features = ["dangerous_configuration"] }
|
||||
rustls-native-certs = "0.5.0"
|
||||
webpki = "0.21.0"
|
||||
webpki = "0.22.0"
|
||||
# Used for conduit::Error type
|
||||
thiserror = "1.0.24"
|
||||
thiserror = "1.0.28"
|
||||
# Used to generate thumbnails for images
|
||||
image = { version = "0.23.14", default-features = false, features = ["jpeg", "png", "gif"] }
|
||||
# Used to encode server public key
|
||||
@@ -60,24 +60,34 @@ base64 = "0.13.0"
|
||||
# Used when hashing the state
|
||||
ring = "0.16.20"
|
||||
# Used when querying the SRV record of other servers
|
||||
trust-dns-resolver = "0.20.0"
|
||||
trust-dns-resolver = "0.20.3"
|
||||
# Used to find matching events for appservices
|
||||
regex = "1.4.3"
|
||||
regex = "1.5.4"
|
||||
# jwt jsonwebtokens
|
||||
jsonwebtoken = "7.2.0"
|
||||
# Performance measurements
|
||||
tracing = "0.1.25"
|
||||
opentelemetry = "0.12.0"
|
||||
tracing-subscriber = "0.2.16"
|
||||
tracing-opentelemetry = "0.11.0"
|
||||
opentelemetry-jaeger = "0.11.0"
|
||||
tracing = { version = "0.1.26", features = ["release_max_level_warn"] }
|
||||
tracing-subscriber = "0.2.20"
|
||||
tracing-opentelemetry = "0.15.0"
|
||||
tracing-flame = "0.1.0"
|
||||
opentelemetry = { version = "0.16.0", features = ["rt-tokio"] }
|
||||
opentelemetry-jaeger = { version = "0.15.0", features = ["rt-tokio"] }
|
||||
pretty_env_logger = "0.4.0"
|
||||
lru-cache = "0.1.2"
|
||||
rusqlite = { version = "0.25.3", optional = true, features = ["bundled"] }
|
||||
parking_lot = { version = "0.11.2", optional = true }
|
||||
crossbeam = { version = "0.8.1", optional = true }
|
||||
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"
|
||||
|
||||
[features]
|
||||
default = ["conduit_bin", "backend_sled"]
|
||||
default = ["conduit_bin", "backend_sqlite"]
|
||||
backend_sled = ["sled"]
|
||||
backend_rocksdb = ["rocksdb"]
|
||||
backend_sqlite = ["sqlite"]
|
||||
backend_heed = ["heed", "crossbeam"]
|
||||
sqlite = ["rusqlite", "parking_lot", "crossbeam", "tokio/signal"]
|
||||
conduit_bin = [] # TODO: add rocket to this when it is optional
|
||||
|
||||
[[bin]]
|
||||
@@ -111,6 +121,20 @@ conf-files = [
|
||||
maintainer-scripts = "debian/"
|
||||
systemd-units = { unit-name = "matrix-conduit" }
|
||||
|
||||
# For flamegraphs:
|
||||
#[profile.release]
|
||||
#debug = true
|
||||
[profile.dev]
|
||||
lto = 'thin'
|
||||
incremental = true
|
||||
|
||||
[profile.release]
|
||||
lto = 'thin'
|
||||
incremental = true
|
||||
|
||||
codegen-units=32
|
||||
# If you want to make flamegraphs, enable debug info:
|
||||
# debug = true
|
||||
|
||||
# For releases also try to max optimizations for dependencies:
|
||||
[profile.release.build-override]
|
||||
opt-level = 3
|
||||
[profile.release.package."*"]
|
||||
opt-level = 3
|
||||
|
||||
@@ -6,12 +6,21 @@ If you run into any problems while setting up Conduit, write an email to `timo@k
|
||||
|
||||
## Installing Conduit
|
||||
|
||||
You may simply download the binary that fits your machine. Run `uname -m` to see
|
||||
what you need. Now copy the right url:
|
||||
- x84_64: `https://conduit.rs/master/x86_64/conduit-bin`
|
||||
- armv7: `https://conduit.rs/master/armv7/conduit-bin`
|
||||
- armv8: `https://conduit.rs/master/armv8/conduit-bin`
|
||||
- arm: `https://conduit.rs/master/arm/conduit-bin`
|
||||
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 | 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
|
||||
|
||||
[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>
|
||||
@@ -19,6 +28,7 @@ $ sudo chmod +x /usr/local/bin/matrix-conduit
|
||||
```
|
||||
|
||||
Alternatively, you may compile the binary yourself using
|
||||
|
||||
```bash
|
||||
$ cargo build --release
|
||||
```
|
||||
@@ -26,6 +36,7 @@ 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.
|
||||
@@ -33,7 +44,7 @@ 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:
|
||||
|
||||
```
|
||||
```bash
|
||||
sudo adduser --system conduit --no-create-home
|
||||
```
|
||||
|
||||
@@ -96,8 +107,8 @@ port = 6167
|
||||
# Max size for uploads
|
||||
max_request_size = 20_000_000 # in bytes
|
||||
|
||||
# Disabling registration means no new users will be able to register on this server
|
||||
allow_registration = false
|
||||
# Enables registration. If set to false, no users can register on this server.
|
||||
allow_registration = true
|
||||
|
||||
# Disable encryption, so no new encrypted rooms can be created
|
||||
# Note: existing rooms will continue to work
|
||||
@@ -106,11 +117,13 @@ allow_federation = true
|
||||
|
||||
trusted_servers = ["matrix.org"]
|
||||
|
||||
#cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024
|
||||
#max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time
|
||||
#workers = 4 # default: cpu core count * 2
|
||||
|
||||
address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy
|
||||
|
||||
# The total amount of memory that the database will use.
|
||||
#db_cache_capacity_mb = 200
|
||||
```
|
||||
|
||||
## Setting the correct file permissions
|
||||
@@ -118,13 +131,13 @@ address = "127.0.0.1" # This makes sure Conduit can only be reached using the re
|
||||
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
|
||||
```
|
||||
|
||||
If you use the default database path you also need to run this:
|
||||
|
||||
```
|
||||
```bash
|
||||
sudo mkdir -p /var/lib/matrix-conduit/conduit_db
|
||||
sudo chown -R conduit:nogroup /var/lib/matrix-conduit/conduit_db
|
||||
```
|
||||
@@ -138,7 +151,7 @@ This depends on whether you use Apache, Nginx or another web server.
|
||||
|
||||
Create `/etc/apache2/sites-enabled/050-conduit.conf` and copy-and-paste this:
|
||||
|
||||
```
|
||||
```apache
|
||||
Listen 8448
|
||||
|
||||
<VirtualHost *:443 *:8448>
|
||||
@@ -167,7 +180,7 @@ $ sudo systemctl reload apache2
|
||||
If you use Nginx and not Apache, add the following server section inside the
|
||||
http section of `/etc/nginx/nginx.conf`
|
||||
|
||||
```
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen [::]:443 ssl http2;
|
||||
@@ -218,4 +231,15 @@ Set it to start automatically when your system boots with:
|
||||
$ sudo systemctl enable conduit
|
||||
```
|
||||
|
||||
## How do I know it works?
|
||||
|
||||
You can open <https://app.element.io>, enter your homeserver and try to register.
|
||||
|
||||
You can also use these commands as a quick health check.
|
||||
|
||||
```bash
|
||||
$ curl https://your.server.name/_matrix/client/versions
|
||||
$ curl https://your.server.name:8448/_matrix/client/versions
|
||||
```
|
||||
|
||||
If you want to set up an appservice, take a look at the [Appservice Guide](APPSERVICES.md).
|
||||
|
||||
+17
-20
@@ -5,7 +5,12 @@
|
||||
|
||||
########################## BUILD IMAGE ##########################
|
||||
# Alpine build image to build Conduit's statically compiled binary
|
||||
FROM alpine:3.12 as builder
|
||||
FROM alpine:3.14 as builder
|
||||
|
||||
# Install packages needed for building all crates
|
||||
RUN apk add --no-cache \
|
||||
cargo \
|
||||
openssl-dev
|
||||
|
||||
# Specifies if the local project is build or if Conduit gets build
|
||||
# from the official git repository. Defaults to the git repo.
|
||||
@@ -13,30 +18,24 @@ ARG LOCAL=false
|
||||
# Specifies which revision/commit is build. Defaults to HEAD
|
||||
ARG GIT_REF=origin/master
|
||||
|
||||
# Add 'edge'-repository to get Rust 1.45
|
||||
RUN sed -i \
|
||||
-e 's|v3\.12|edge|' \
|
||||
/etc/apk/repositories
|
||||
|
||||
# Install packages needed for building all crates
|
||||
RUN apk add --no-cache \
|
||||
cargo \
|
||||
openssl-dev
|
||||
|
||||
|
||||
# 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 \
|
||||
cargo install --git "https://gitlab.com/famedly/conduit.git" --rev ${GIT_REF}; \
|
||||
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.12
|
||||
FROM alpine:3.14
|
||||
|
||||
ARG CREATED
|
||||
ARG VERSION
|
||||
@@ -61,14 +60,15 @@ LABEL org.opencontainers.image.created=${CREATED} \
|
||||
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 Rocket launches
|
||||
EXPOSE 8000
|
||||
# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose.
|
||||
EXPOSE 6167
|
||||
|
||||
# Copy config files from context and the binary from
|
||||
# the "builder" stage to the current stage into folder
|
||||
# /srv/conduit and create data folder for database
|
||||
RUN mkdir -p /srv/conduit/.local/share/conduit
|
||||
COPY --from=builder /root/.cargo/bin/conduit /srv/conduit/
|
||||
COPY --from=builder ./healthcheck.sh /srv/conduit/
|
||||
|
||||
# Add www-data user and group with UID 82, as used by alpine
|
||||
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
|
||||
@@ -86,11 +86,8 @@ RUN apk add --no-cache \
|
||||
curl \
|
||||
libgcc
|
||||
|
||||
# Create a volume for the database, to persist its contents
|
||||
VOLUME ["/srv/conduit/.local/share/conduit"]
|
||||
|
||||
# Test if Conduit is still alive, uses the same endpoint as Element
|
||||
HEALTHCHECK --start-period=2s CMD curl --fail -s http://localhost:8000/_matrix/client/versions || curl -k --fail -s https://localhost:8000/_matrix/client/versions || exit 1
|
||||
HEALTHCHECK --start-period=5s --interval=60s CMD ./healthcheck.sh
|
||||
|
||||
# Set user to www-data
|
||||
USER www-data
|
||||
|
||||
@@ -3,90 +3,42 @@
|
||||
|
||||
#### What is the goal?
|
||||
|
||||
A fast Matrix homeserver that's easy to set up and just works. You can install
|
||||
An efficient Matrix homeserver that's easy to set up and just works. You can install
|
||||
it on a mini-computer like the Raspberry Pi to host Matrix for your family,
|
||||
friends or company.
|
||||
|
||||
|
||||
#### Can I try it out?
|
||||
|
||||
Yes! Just open a Matrix client (<https://app.element.io> or Element Android for
|
||||
example) and register on the `https://conduit.koesters.xyz` homeserver.
|
||||
Yes! You can test our Conduit instance by opening a Matrix client (<https://app.element.io> or Element Android for
|
||||
example) and registering on the `conduit.rs` homeserver.
|
||||
|
||||
|
||||
#### What is it built on?
|
||||
|
||||
- [Ruma](https://www.ruma.io): Useful structures for endpoint requests and
|
||||
responses that can be (de)serialized
|
||||
- [Sled](https://github.com/spacejam/sled): A simple (key, value) database with
|
||||
good performance
|
||||
- [Rocket](https://rocket.rs): A flexible web framework
|
||||
It is hosted on a ODROID HC 2 with 2GB RAM and a SAMSUNG Exynos 5422 CPU, which
|
||||
was used in the Samsung Galaxy S5. It joined many big rooms including Matrix
|
||||
HQ.
|
||||
|
||||
|
||||
#### What is the current status?
|
||||
|
||||
Conduit can already be used chat with other users on Conduit, chat with users
|
||||
from other Matrix servers and even to chat with users on other platforms using
|
||||
appservices. When chatting with users on the same Conduit server, everything
|
||||
should work assuming you use a compatible client.
|
||||
|
||||
**You should not join Matrix rooms without asking the admins first.** We do not
|
||||
know whether Conduit is safe for general use yet, so you should assume there is
|
||||
some chance that it breaks rooms permanently for all participating users. We
|
||||
are not aware of such a bug today, but we would like to do more testing.
|
||||
As of 2021-09-01, Conduit is Beta, meaning 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.
|
||||
|
||||
There are still a few important features missing:
|
||||
|
||||
- Database stability (currently you might have to do manual upgrades or even wipe the db for new versions)
|
||||
- End-to-end encrypted chats over federation
|
||||
- Typing, presence, read receipts etc. over federation
|
||||
- Lots of testing
|
||||
- E2EE verification over federation
|
||||
- Outgoing read receipts, typing, presence over federation
|
||||
|
||||
Check out the [Conduit 1.0 Release Milestone](https://gitlab.com/famedly/conduit/-/milestones/3).
|
||||
|
||||
|
||||
#### How can I deploy my own?
|
||||
|
||||
##### Deploy
|
||||
Simple install (this was tested the most): [DEPLOY.md](DEPLOY.md)\
|
||||
Debian package: [debian/README.Debian](debian/README.Debian)\
|
||||
Docker: [docker/README.md](docker/README.md)
|
||||
|
||||
Download or compile a Conduit binary, set up the config and call it from somewhere like a systemd script. [Read
|
||||
more](DEPLOY.md)
|
||||
|
||||
If you want to connect an Appservice to Conduit, take a look at the [Appservice Guide](APPSERVICES.md).
|
||||
|
||||
##### Deploy using a Debian package
|
||||
|
||||
You need to have the `deb` helper command installed that creates Debian packages from Cargo projects (see [cargo-deb](https://github.com/mmstick/cargo-deb/) for more info):
|
||||
|
||||
```shell
|
||||
$ cargo install cargo-deb
|
||||
```
|
||||
|
||||
Then, you can create and install a Debian package at a whim:
|
||||
|
||||
```shell
|
||||
$ cargo deb
|
||||
$ dpkg -i target/debian/matrix-conduit_0.1.0_amd64.deb
|
||||
```
|
||||
|
||||
This will build, package, install, configure and start Conduit. [Read more](debian/README.Debian).
|
||||
|
||||
Note that `cargo deb` supports [cross-compilation](https://github.com/mmstick/cargo-deb/#cross-compilation) too!
|
||||
Official Debian packages will follow once Conduit starts to have stable releases.
|
||||
|
||||
##### Deploy using Docker
|
||||
|
||||
Pull and run the docker image with
|
||||
|
||||
``` bash
|
||||
docker pull matrixconduit/matrix-conduit:latest
|
||||
docker run -d -p 8448:8000 -v ~/conduit.toml:/srv/conduit/conduit.toml -v db:/srv/conduit/.local/share/conduit matrixconduit/matrix-conduit:latest
|
||||
```
|
||||
|
||||
> <b>Note:</b> You also need to supply a `conduit.toml` config file, you can find an example [here](./conduit-example.toml).
|
||||
> Or you can pass in `-e CONDUIT_CONFIG=""` and configure Conduit purely with env vars.
|
||||
|
||||
Or build and run it with docker or docker-compose. [Read more](docker/README.md)
|
||||
If you want to connect an Appservice to Conduit, take a look at [APPSERVICES.md](APPSERVICES.md).
|
||||
|
||||
|
||||
#### How can I contribute?
|
||||
@@ -98,6 +50,17 @@ Or build and run it with docker or docker-compose. [Read more](docker/README.md)
|
||||
3. Fork the repo and work on the issue. #conduit:nordgedanken.dev is happy to help :)
|
||||
4. Submit a MR
|
||||
|
||||
|
||||
#### Thanks to
|
||||
|
||||
Thanks to Famedly, Prototype Fund (DLR and German BMBF) and all other individuals for financially supporting this project.
|
||||
|
||||
Thanks to the contributors to Conduit and all libraries we use, for example:
|
||||
|
||||
- Ruma: A clean library for the Matrix Spec in Rust
|
||||
- Rocket: A flexible web framework
|
||||
|
||||
|
||||
#### Donate
|
||||
|
||||
Liberapay: <https://liberapay.com/timokoesters/>\
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#server_name = "your.server.name"
|
||||
|
||||
# This is the only directory where Conduit will save its data
|
||||
database_path = "/var/lib/conduit/conduit.db"
|
||||
database_path = "/var/lib/conduit/"
|
||||
|
||||
# The port Conduit will be running on. You need to set up a reverse proxy in
|
||||
# your web server (e.g. apache or nginx), so all requests to /_matrix on port
|
||||
@@ -22,8 +22,8 @@ port = 6167
|
||||
# Max size for uploads
|
||||
max_request_size = 20_000_000 # in bytes
|
||||
|
||||
# Disable registration. No new users will be able to register on this server
|
||||
#allow_registration = false
|
||||
# Enables registration. If set to false, no users can register on this server.
|
||||
allow_registration = true
|
||||
|
||||
# Disable encryption, so no new encrypted rooms can be created
|
||||
# Note: existing rooms will continue to work
|
||||
@@ -35,7 +35,6 @@ max_request_size = 20_000_000 # in bytes
|
||||
|
||||
trusted_servers = ["matrix.org"]
|
||||
|
||||
#cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024
|
||||
#max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time
|
||||
#log = "info,state_res=warn,rocket=off,_=off,sled=off"
|
||||
#workers = 4 # default: cpu core count * 2
|
||||
@@ -43,3 +42,6 @@ trusted_servers = ["matrix.org"]
|
||||
address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy
|
||||
|
||||
proxy = "none" # more examples can be found at src/database/proxy.rs:6
|
||||
|
||||
# The total amount of memory that the database will use.
|
||||
#db_cache_capacity_mb = 200
|
||||
|
||||
Vendored
+5
-3
@@ -62,8 +62,8 @@ port = ${CONDUIT_PORT}
|
||||
# Max size for uploads
|
||||
max_request_size = 20_000_000 # in bytes
|
||||
|
||||
# Disable registration. No new users will be able to register on this server.
|
||||
#allow_registration = false
|
||||
# Enables registration. If set to false, no users can register on this server.
|
||||
allow_registration = true
|
||||
|
||||
# Disable encryption, so no new encrypted rooms can be created.
|
||||
# Note: Existing rooms will continue to work.
|
||||
@@ -73,10 +73,12 @@ max_request_size = 20_000_000 # in bytes
|
||||
# Enable jaeger to support monitoring and troubleshooting through jaeger.
|
||||
#allow_jaeger = false
|
||||
|
||||
#cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024
|
||||
#max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time
|
||||
#log = "info,state_res=warn,rocket=off,_=off,sled=off"
|
||||
#workers = 4 # default: cpu core count * 2
|
||||
|
||||
# The total amount of memory that the database will use.
|
||||
#db_cache_capacity_mb = 200
|
||||
EOF
|
||||
fi
|
||||
;;
|
||||
|
||||
+15
-15
@@ -3,38 +3,38 @@ version: '3'
|
||||
|
||||
services:
|
||||
homeserver:
|
||||
### If you already built the Conduit image with 'docker build' or want to use the Docker Hub image,
|
||||
### If you already built the Conduit image with 'docker build' or want to use a registry image,
|
||||
### then you are ready to go.
|
||||
#image: matrixconduit/matrix-conduit:latest
|
||||
image: matrixconduit/matrix-conduit:latest
|
||||
### If you want to build a fresh image from the sources, then comment the image line and uncomment the
|
||||
### build lines. If you want meaningful labels in your built Conduit image, you should run docker-compose like this:
|
||||
### CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ') VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml) docker-compose up -d
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
CREATED: '2021-03-16T08:18:27Z'
|
||||
VERSION: '0.1.0'
|
||||
LOCAL: 'false'
|
||||
GIT_REF: origin/master
|
||||
# build:
|
||||
# context: .
|
||||
# args:
|
||||
# CREATED: '2021-03-16T08:18:27Z'
|
||||
# VERSION: '0.1.0'
|
||||
# LOCAL: 'false'
|
||||
# GIT_REF: origin/master
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- 8448:8000
|
||||
- 8448:6167
|
||||
volumes:
|
||||
- db:/srv/conduit/.local/share/conduit
|
||||
### Uncomment if you want to use conduit.toml to configure Conduit
|
||||
### Note: Set env vars will override conduit.toml values
|
||||
# - ./conduit.toml:/srv/conduit/conduit.toml
|
||||
environment:
|
||||
CONDUIT_SERVER_NAME: localhost:8000 # replace with your own name
|
||||
CONDUIT_SERVER_NAME: localhost:6167 # replace with your own name
|
||||
CONDUIT_TRUSTED_SERVERS: '["matrix.org"]'
|
||||
CONDUIT_ALLOW_REGISTRATION: 'true'
|
||||
### Uncomment and change values as desired
|
||||
# CONDUIT_ADDRESS: 127.0.0.1
|
||||
# CONDUIT_PORT: 8000
|
||||
# CONDUIT_ADDRESS: 0.0.0.0
|
||||
# CONDUIT_PORT: 6167
|
||||
# CONDUIT_CONFIG: '/srv/conduit/conduit.toml' # if you want to configure purely by env vars, set this to an empty string ''
|
||||
# Available levels are: error, warn, info, debug, trace - more info at: https://docs.rs/env_logger/*/env_logger/#enabling-logging
|
||||
# CONDUIT_LOG: info # default is: "info,rocket=off,_=off,sled=off"
|
||||
# CONDUIT_ALLOW_JAEGER: 'false'
|
||||
# CONDUIT_ALLOW_REGISTRATION : 'false'
|
||||
# CONDUIT_ALLOW_ENCRYPTION: 'false'
|
||||
# CONDUIT_ALLOW_FEDERATION: 'false'
|
||||
# CONDUIT_DATABASE_PATH: /srv/conduit/.local/share/conduit
|
||||
@@ -56,4 +56,4 @@ services:
|
||||
# - homeserver
|
||||
|
||||
volumes:
|
||||
db:
|
||||
db:
|
||||
|
||||
+63
-5
@@ -40,18 +40,27 @@ which also will tag the resulting image as `matrixconduit/matrix-conduit:latest`
|
||||
After building the image you can simply run it with
|
||||
|
||||
``` bash
|
||||
docker run -d -p 8448:8000 -v ~/conduit.toml:/srv/conduit/conduit.toml -v db:/srv/conduit/.local/share/conduit matrixconduit/matrix-conduit:latest
|
||||
docker run -d -p 8448:6167 -v ~/conduit.toml:/srv/conduit/conduit.toml -v db:/srv/conduit/.local/share/conduit matrixconduit/matrix-conduit:latest
|
||||
```
|
||||
|
||||
For detached mode, you also need to use the `-d` flag. You also need to supply a `conduit.toml` config file, you can find an example [here](../conduit-example.toml).
|
||||
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](https://hub.docker.com/r/matrixconduit/matrix-conduit) |  |
|
||||
| GitLab Registry | [registry.gitlab.com/famedly/conduit/conduit:latest](https://gitlab.com/famedly/conduit/container_registry/2134341) |  |
|
||||
|
||||
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
|
||||
too pass `-e CONDUIT_CONFIG=""` into your container. For an overview of possible values, please take a look at the `docker-compose.yml` file.
|
||||
to pass `-e CONDUIT_CONFIG=""` into your container. For an overview of possible values, please take a look at the `docker-compose.yml` file.
|
||||
|
||||
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) including [`docker-compose.override.traefik.yml`](docker-compose.override.traefik.yml) or the normal [`docker-compose.yml`](../docker-compose.yml) for every other reverse proxy.
|
||||
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
|
||||
@@ -67,8 +76,57 @@ This will also start the container right afterwards, so if want it to run in det
|
||||
|
||||
### Run
|
||||
|
||||
If you already have built the image, you can just start the container and everything else in the compose file in detached mode with:
|
||||
If you already have built the image or want to use one from the registries, you can just start the container and everything else in the compose file in detached mode with:
|
||||
|
||||
``` bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
> **Note:** Don't forget to modify and adjust the compose file to your needs.
|
||||
|
||||
### Use Traefik as Proxy
|
||||
|
||||
As a container user, you probably know about Traefik. It is a easy to use reverse proxy for making containerized app and services available through the web. With the
|
||||
two provided files, [`docker-compose.traefik.yml`](docker-compose.traefik.yml) and [`docker-compose.override.traefik.yml`](docker-compose.override.traefik.yml), it is
|
||||
equally easy to deploy and use Conduit, with a little caveat. If you already took a look at the files, then you should have seen the `well-known` service, and that is
|
||||
the little caveat. Traefik is simply a proxy and loadbalancer and is not able to serve any kind of content, but for Conduit to federate, we need to either expose ports
|
||||
`443` and `8448` or serve two endpoints `.well-known/matrix/client` and `.well-known/matrix/server`.
|
||||
|
||||
With the service `well-known` we use a single `nginx` container that will serve those two files.
|
||||
|
||||
So...step by step:
|
||||
|
||||
1. Copy [`docker-compose.traefik.yml`](docker-compose.traefik.yml) and [`docker-compose.override.traefik.yml`](docker-compose.override.traefik.yml) from the repository and remove `.traefik` from the filenames.
|
||||
2. Open both files and modify/adjust them to your needs. Meaning, change the `CONDUIT_SERVER_NAME` and the volume host mappings according to your needs.
|
||||
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>;
|
||||
listen 80 default_server;
|
||||
|
||||
location /.well-known/matrix/ {
|
||||
root /var/www;
|
||||
default_type application/json;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
}
|
||||
}
|
||||
```
|
||||
- `./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
|
||||
{
|
||||
"m.homeserver": {
|
||||
"base_url": "https://<SUBDOMAIN>.<DOMAIN>"
|
||||
}
|
||||
}
|
||||
```
|
||||
- `./nginx/www/.well-known/matrix/server` (relative to the compose file, you can change this, but then also need to change the volume mapping)
|
||||
```json
|
||||
{
|
||||
"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.
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
# ---------------------------------------------------------------------------------------------------------
|
||||
# This Dockerfile is intended to be built as part of Conduit's CI pipeline.
|
||||
# It does not build Conduit in Docker, but just copies the matching build artifact from the build job.
|
||||
# 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 alpine:3.14
|
||||
|
||||
# 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} \
|
||||
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=""
|
||||
|
||||
# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose.
|
||||
EXPOSE 6167
|
||||
|
||||
# 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/
|
||||
|
||||
# Add www-data user and group with UID 82, as used by alpine
|
||||
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
|
||||
RUN set -x ; \
|
||||
addgroup -Sg 82 www-data 2>/dev/null ; \
|
||||
adduser -S -D -H -h /srv/conduit -G www-data -g www-data www-data 2>/dev/null ; \
|
||||
addgroup www-data www-data 2>/dev/null && exit 0 ; exit 1
|
||||
|
||||
# Change ownership of Conduit files to www-data user and group
|
||||
RUN chown -cR www-data:www-data /srv/conduit
|
||||
RUN chmod +x /srv/conduit/healthcheck.sh
|
||||
|
||||
|
||||
# Test if Conduit is still alive, uses the same endpoint as Element
|
||||
HEALTHCHECK --start-period=5s --interval=60s CMD ./healthcheck.sh
|
||||
|
||||
# Set user to www-data
|
||||
USER www-data
|
||||
# Set container home directory
|
||||
WORKDIR /srv/conduit
|
||||
# Run Conduit
|
||||
ENTRYPOINT [ "/srv/conduit/conduit" ]
|
||||
@@ -10,6 +10,29 @@ services:
|
||||
- "traefik.http.routers.to-conduit.rule=Host(`<SUBDOMAIN>.<DOMAIN>`)" # Change to the address on which Conduit is hosted
|
||||
- "traefik.http.routers.to-conduit.tls=true"
|
||||
- "traefik.http.routers.to-conduit.tls.certresolver=letsencrypt"
|
||||
- "traefik.http.routers.to-conduit.middlewares=cors-headers@docker"
|
||||
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowOrigin=*"
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowHeaders=Origin, X-Requested-With, Content-Type, Accept, Authorization"
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowMethods=GET, POST, PUT, DELETE, OPTIONS"
|
||||
|
||||
# We need some way to server the client and server .well-known json. The simplest way is to use a nginx container
|
||||
# to serve those two as static files. If you want to use a different way, delete or comment the below service, here
|
||||
# and in the docker-compose file.
|
||||
well-known:
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.docker.network=proxy"
|
||||
|
||||
- "traefik.http.routers.to-matrix-wellknown.rule=Host(`<SUBDOMAIN>.<DOMAIN>`) && PathPrefix(`/.well-known/matrix`)"
|
||||
- "traefik.http.routers.to-matrix-wellknown.tls=true"
|
||||
- "traefik.http.routers.to-matrix-wellknown.tls.certresolver=letsencrypt"
|
||||
- "traefik.http.routers.to-matrix-wellknown.middlewares=cors-headers@docker"
|
||||
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowOrigin=*"
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowHeaders=Origin, X-Requested-With, Content-Type, Accept, Authorization"
|
||||
- "traefik.http.middlewares.cors-headers.headers.accessControlAllowMethods=GET, POST, PUT, DELETE, OPTIONS"
|
||||
|
||||
|
||||
### Uncomment this if you uncommented Element-Web App in the docker-compose.yml
|
||||
# element-web:
|
||||
|
||||
@@ -12,8 +12,8 @@ services:
|
||||
# build:
|
||||
# context: .
|
||||
# args:
|
||||
# CREATED:
|
||||
# VERSION:
|
||||
# CREATED: '2021-03-16T08:18:27Z'
|
||||
# VERSION: '0.1.0'
|
||||
# LOCAL: 'false'
|
||||
# GIT_REF: origin/master
|
||||
restart: unless-stopped
|
||||
@@ -25,22 +25,32 @@ services:
|
||||
networks:
|
||||
- proxy
|
||||
environment:
|
||||
CONDUIT_SERVER_NAME: localhost:8000 # replace with your own name
|
||||
CONDUIT_SERVER_NAME: localhost:6167 # replace with your own name
|
||||
CONDUIT_TRUSTED_SERVERS: '["matrix.org"]'
|
||||
CONDUIT_ALLOW_REGISTRATION : 'true'
|
||||
### Uncomment and change values as desired
|
||||
# CONDUIT_ADDRESS: 127.0.0.1
|
||||
# CONDUIT_PORT: 8000
|
||||
# CONDUIT_CONFIG: '/srv/conduit/conduit.toml' # if want to configure purely by env vars, set this to an empty string ''
|
||||
# CONDUIT_ADDRESS: 0.0.0.0
|
||||
# CONDUIT_PORT: 6167
|
||||
# CONDUIT_CONFIG: '/srv/conduit/conduit.toml' # if you want to configure purely by env vars, set this to an empty string ''
|
||||
# Available levels are: error, warn, info, debug, trace - more info at: https://docs.rs/env_logger/*/env_logger/#enabling-logging
|
||||
# CONDUIT_LOG: info # default is: "info,rocket=off,_=off,sled=off"
|
||||
# CONDUIT_ALLOW_JAEGER: 'false'
|
||||
# CONDUIT_ALLOW_REGISTRATION : 'false'
|
||||
# CONDUIT_ALLOW_ENCRYPTION: 'false'
|
||||
# CONDUIT_ALLOW_FEDERATION: 'false'
|
||||
# CONDUIT_DATABASE_PATH: /srv/conduit/.local/share/conduit
|
||||
# CONDUIT_WORKERS: 10
|
||||
# CONDUIT_MAX_REQUEST_SIZE: 20_000_000 # in bytes, ~20 MB
|
||||
|
||||
# We need some way to server the client and server .well-known json. The simplest way is to use a nginx container
|
||||
# to serve those two as static files. If you want to use a different way, delete or comment the below service, here
|
||||
# and in the docker-compose override file.
|
||||
well-known:
|
||||
image: nginx:latest
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./nginx/matrix.conf:/etc/nginx/conf.d/matrix.conf # the config to serve the .well-known/matrix files
|
||||
- ./nginx/www:/var/www/ # location of the client and server .well-known-files
|
||||
|
||||
### Uncomment if you want to use your own Element-Web App.
|
||||
### Note: You need to provide a config.json for Element and you also need a second
|
||||
### Domain or Subdomain for the communication between Element and Conduit
|
||||
@@ -59,7 +69,7 @@ volumes:
|
||||
db:
|
||||
|
||||
networks:
|
||||
# This is the network Traefik listens to, if you network has a different
|
||||
# This is the network Traefik listens to, if your network has a different
|
||||
# name, don't forget to change it here and in the docker-compose.override.yml
|
||||
proxy:
|
||||
external: true
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
# If the port is not specified as env var, take it from the config file
|
||||
if [ -z ${CONDUIT_PORT} ]; then
|
||||
CONDUIT_PORT=$(grep -m1 -o 'port\s=\s[0-9]*' conduit.toml | grep -m1 -o '[0-9]*')
|
||||
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 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
|
||||
+1
-1
@@ -1 +1 @@
|
||||
1.51.0
|
||||
1.52
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::{utils, Error, Result};
|
||||
use bytes::BytesMut;
|
||||
use log::warn;
|
||||
use ruma::api::{IncomingResponse, OutgoingRequest, SendAccessToken};
|
||||
use std::{
|
||||
convert::{TryFrom, TryInto},
|
||||
@@ -8,8 +7,9 @@ use std::{
|
||||
mem,
|
||||
time::Duration,
|
||||
};
|
||||
use tracing::warn;
|
||||
|
||||
pub async fn send_request<T: OutgoingRequest>(
|
||||
pub(crate) async fn send_request<T: OutgoingRequest>(
|
||||
globals: &crate::database::globals::Globals,
|
||||
registration: serde_yaml::Value,
|
||||
request: T,
|
||||
@@ -46,7 +46,11 @@ where
|
||||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let mut response = globals.reqwest_client().execute(reqwest_request).await?;
|
||||
let mut response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await?;
|
||||
|
||||
// reqwest::Response -> http::Response conversion
|
||||
let status = response.status();
|
||||
|
||||
+145
-66
@@ -1,8 +1,11 @@
|
||||
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use super::{State, DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma};
|
||||
use log::info;
|
||||
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -11,6 +14,7 @@ use ruma::{
|
||||
change_password, deactivate, get_username_availability, register, whoami,
|
||||
ThirdPartyIdRemovalStatus,
|
||||
},
|
||||
contact::get_contacts,
|
||||
uiaa::{AuthFlow, UiaaInfo},
|
||||
},
|
||||
},
|
||||
@@ -21,8 +25,10 @@ use ruma::{
|
||||
},
|
||||
EventType,
|
||||
},
|
||||
identifiers::RoomName,
|
||||
push, RoomAliasId, RoomId, RoomVersionId, UserId,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
use register::RegistrationKind;
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
@@ -34,15 +40,19 @@ const GUEST_NAME_LENGTH: usize = 10;
|
||||
///
|
||||
/// Checks if a username is valid and available on this server.
|
||||
///
|
||||
/// - Returns true if no user or appservice on this server claimed this username
|
||||
/// - This will not reserve the username, so the username might become invalid when trying to register
|
||||
/// Conditions for returning true:
|
||||
/// - The user id is not historical
|
||||
/// - The server name of the user id matches this server
|
||||
/// - No user or appservice on this server already claimed this username
|
||||
///
|
||||
/// Note: This will not reserve the username, so the username might become invalid when trying to register
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/register/available", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_register_available_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_username_availability::Request<'_>>,
|
||||
) -> ConduitResult<get_username_availability::Response> {
|
||||
// Validate user id
|
||||
@@ -74,18 +84,22 @@ pub async fn get_register_available_route(
|
||||
///
|
||||
/// Register an account on this homeserver.
|
||||
///
|
||||
/// - Returns the device id and access_token unless `inhibit_login` is true
|
||||
/// - When registering a guest account, all parameters except initial_device_display_name will be
|
||||
/// ignored
|
||||
/// - Creates a new account and a device for it
|
||||
/// - The account will be populated with default account data
|
||||
/// You can use [`GET /_matrix/client/r0/register/available`](fn.get_register_available_route.html)
|
||||
/// to check if the user id is valid and available.
|
||||
///
|
||||
/// - Only works if registration is enabled
|
||||
/// - If type is guest: ignores all parameters except initial_device_display_name
|
||||
/// - If sender is not appservice: Requires UIAA (but we only use a dummy stage)
|
||||
/// - If type is not guest and no username is given: Always fails after UIAA check
|
||||
/// - Creates a new account and populates it with default account data
|
||||
/// - If `inhibit_login` is false: Creates a device and returns device id and access_token
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/register", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn register_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<register::Request<'_>>,
|
||||
) -> ConduitResult<register::Response> {
|
||||
if !db.globals.allow_registration() && !body.from_appservice {
|
||||
@@ -123,7 +137,7 @@ pub async fn register_route(
|
||||
))?;
|
||||
|
||||
// Check if username is creative enough
|
||||
if !missing_username && db.users.exists(&user_id)? {
|
||||
if db.users.exists(&user_id)? {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::UserInUse,
|
||||
"Desired user ID is already taken.",
|
||||
@@ -156,16 +170,18 @@ pub async fn register_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&UserId::parse_with_server_name("", db.globals.server_name())
|
||||
.expect("we know this is valid"),
|
||||
"".into(),
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
&json,
|
||||
)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +201,12 @@ pub async fn register_route(
|
||||
// Create user
|
||||
db.users.create(&user_id, password)?;
|
||||
|
||||
// Initial data
|
||||
// Default to pretty displayname
|
||||
let displayname = format!("{} ⚡️", user_id.localpart());
|
||||
db.users
|
||||
.set_displayname(&user_id, Some(displayname.clone()))?;
|
||||
|
||||
// Initial account data
|
||||
db.account_data.update(
|
||||
None,
|
||||
&user_id,
|
||||
@@ -198,6 +219,7 @@ pub async fn register_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
// Inhibit login does not work for guests
|
||||
if !is_guest && body.inhibit_login {
|
||||
return Ok(register::Response {
|
||||
access_token: None,
|
||||
@@ -218,7 +240,7 @@ pub async fn register_route(
|
||||
// Generate new token for the device
|
||||
let token = utils::random_string(TOKEN_LENGTH);
|
||||
|
||||
// Add device
|
||||
// Create device for this account
|
||||
db.users.create_device(
|
||||
&user_id,
|
||||
&device_id,
|
||||
@@ -226,7 +248,7 @@ pub async fn register_route(
|
||||
body.initial_device_display_name.clone(),
|
||||
)?;
|
||||
|
||||
// If this is the first user on this server, create the admins room
|
||||
// If this is the first user on this server, create the admin room
|
||||
if db.users.count()? == 1 {
|
||||
// Create a user for the server
|
||||
let conduit_user = UserId::parse_with_server_name("conduit", db.globals.server_name())
|
||||
@@ -236,6 +258,18 @@ pub async fn register_route(
|
||||
|
||||
let room_id = RoomId::new(db.globals.server_name());
|
||||
|
||||
db.rooms.get_or_create_shortroomid(&room_id, &db.globals)?;
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let mut content = ruma::events::room::create::CreateEventContent::new(conduit_user.clone());
|
||||
content.federate = true;
|
||||
content.predecessor = None;
|
||||
@@ -253,6 +287,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 2. Make conduit bot join
|
||||
@@ -265,6 +300,8 @@ pub async fn register_route(
|
||||
avatar_url: None,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -274,6 +311,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 3. Power levels
|
||||
@@ -298,6 +336,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4.1 Join Rules
|
||||
@@ -315,6 +354,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4.2 History Visibility
|
||||
@@ -334,6 +374,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4.3 Guest Access
|
||||
@@ -351,18 +392,18 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 6. Events implied by name and topic
|
||||
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: serde_json::to_value(
|
||||
name::NameEventContent::new("Admin Room".to_owned()).map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Name is invalid.")
|
||||
})?,
|
||||
)
|
||||
.expect("event is valid, we just created it"),
|
||||
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()),
|
||||
redacts: None,
|
||||
@@ -370,6 +411,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
@@ -386,6 +428,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// Room alias
|
||||
@@ -408,6 +451,7 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.rooms.set_alias(&alias, Some(&room_id), &db.globals)?;
|
||||
@@ -422,6 +466,8 @@ pub async fn register_route(
|
||||
avatar_url: None,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -431,16 +477,19 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
content: serde_json::to_value(member::MemberEventContent {
|
||||
membership: member::MembershipState::Join,
|
||||
displayname: None,
|
||||
displayname: Some(displayname),
|
||||
avatar_url: None,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -450,6 +499,7 @@ pub async fn register_route(
|
||||
&user_id,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// Send welcome message
|
||||
@@ -457,8 +507,8 @@ pub async fn register_route(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMessage,
|
||||
content: serde_json::to_value(message::MessageEventContent::text_html(
|
||||
"Thanks for trying out Conduit! This software is still in development, so expect many bugs and missing features. If you have federation enabled, you can join the Conduit chat room by typing `/join #conduit:matrix.org`. **Important: Please don't join any other Matrix rooms over federation without permission from the room's admins.** Some actions might trigger bugs in other server implementations, breaking the chat for everyone else.".to_owned(),
|
||||
"Thanks for trying out Conduit! This software is still in development, so expect many bugs and missing features. If you have federation enabled, you can join the Conduit chat room by typing <code>/join #conduit:matrix.org</code>. <strong>Important: Please don't join any other Matrix rooms over federation without permission from the room's admins.</strong> Some actions might trigger bugs in other server implementations, breaking the chat for everyone else.".to_owned(),
|
||||
"## Thank you for trying out Conduit!\n\nConduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Website: https://conduit.rs\n> Git and Documentation: https://gitlab.com/famedly/conduit\n> Report issues: https://gitlab.com/famedly/conduit/-/issues\n\nHere are some rooms you can join (by typing the command):\n\nConduit room (Ask questions and get notified on updates):\n`/join #conduit:fachschaften.org`\n\nConduit lounge (Off-topic, only Conduit users are allowed to join)\n`/join #conduit-lounge:conduit.rs`".to_owned(),
|
||||
"<h2>Thank you for trying out Conduit!</h2>\n<p>Conduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Website: https://conduit.rs<br>Git and Documentation: https://gitlab.com/famedly/conduit<br>Report issues: https://gitlab.com/famedly/conduit/-/issues</p>\n</blockquote>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>Conduit room (Ask questions and get notified on updates):<br><code>/join #conduit:fachschaften.org</code></p>\n<p>Conduit lounge (Off-topic, only Conduit users are allowed to join)<br><code>/join #conduit-lounge:conduit.rs</code></p>\n".to_owned(),
|
||||
))
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -468,12 +518,13 @@ pub async fn register_route(
|
||||
&conduit_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
info!("{} registered on this server", user_id);
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(register::Response {
|
||||
access_token: Some(token),
|
||||
@@ -487,16 +538,23 @@ pub async fn register_route(
|
||||
///
|
||||
/// Changes the password of this account.
|
||||
///
|
||||
/// - Invalidates all other access tokens if logout_devices is true
|
||||
/// - Deletes all other devices and most of their data (to-device events, last seen, etc.) if
|
||||
/// logout_devices is true
|
||||
/// - Requires UIAA to verify user password
|
||||
/// - Changes the password of the sender user
|
||||
/// - The password hash is calculated using argon2 with 32 character salt, the plain password is
|
||||
/// not saved
|
||||
///
|
||||
/// If logout_devices is true it does the following for each device except the sender device:
|
||||
/// - Invalidates access token
|
||||
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets to-device events
|
||||
/// - Triggers device list updates
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/account/password", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn change_password_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<change_password::Request<'_>>,
|
||||
) -> ConduitResult<change_password::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -525,15 +583,13 @@ pub async fn change_password_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&sender_user,
|
||||
&sender_device,
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
)?;
|
||||
db.uiaa
|
||||
.create(&sender_user, &sender_device, &uiaainfo, &json)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
|
||||
db.users
|
||||
@@ -551,16 +607,16 @@ pub async fn change_password_route(
|
||||
}
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(change_password::Response.into())
|
||||
Ok(change_password::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET _matrix/client/r0/account/whoami`
|
||||
///
|
||||
/// Get user_id of this account.
|
||||
/// Get user_id of the sender user.
|
||||
///
|
||||
/// - Also works for Application Services
|
||||
/// Note: Also works for Application Services
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/account/whoami", data = "<body>")
|
||||
@@ -576,11 +632,13 @@ pub async fn whoami_route(body: Ruma<whoami::Request>) -> ConduitResult<whoami::
|
||||
|
||||
/// # `POST /_matrix/client/r0/account/deactivate`
|
||||
///
|
||||
/// Deactivate this user's account
|
||||
/// Deactivate sender user account.
|
||||
///
|
||||
/// - Leaves all rooms and rejects all invitations
|
||||
/// - Invalidates all access tokens
|
||||
/// - Deletes all devices
|
||||
/// - Deletes all device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets all to-device events
|
||||
/// - Triggers device list updates
|
||||
/// - Removes ability to log in again
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
@@ -588,7 +646,7 @@ pub async fn whoami_route(body: Ruma<whoami::Request>) -> ConduitResult<whoami::
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn deactivate_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<deactivate::Request<'_>>,
|
||||
) -> ConduitResult<deactivate::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -617,23 +675,28 @@ pub async fn deactivate_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&sender_user,
|
||||
&sender_device,
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
)?;
|
||||
db.uiaa
|
||||
.create(&sender_user, &sender_device, &uiaainfo, &json)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
|
||||
// Leave all joined rooms and reject all invitations
|
||||
for room_id in db.rooms.rooms_joined(&sender_user).chain(
|
||||
db.rooms
|
||||
.rooms_invited(&sender_user)
|
||||
.map(|t| t.map(|(r, _)| r)),
|
||||
) {
|
||||
// TODO: work over federation invites
|
||||
let all_rooms = db
|
||||
.rooms
|
||||
.rooms_joined(&sender_user)
|
||||
.chain(
|
||||
db.rooms
|
||||
.rooms_invited(&sender_user)
|
||||
.map(|t| t.map(|(r, _)| r)),
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for room_id in all_rooms {
|
||||
let room_id = room_id?;
|
||||
let event = member::MemberEventContent {
|
||||
membership: member::MembershipState::Leave,
|
||||
@@ -641,8 +704,20 @@ pub async fn deactivate_route(
|
||||
avatar_url: None,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason: None,
|
||||
};
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
@@ -654,6 +729,7 @@ pub async fn deactivate_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -662,7 +738,7 @@ pub async fn deactivate_route(
|
||||
|
||||
info!("{} deactivated their account", sender_user);
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(deactivate::Response {
|
||||
id_server_unbind_result: ThirdPartyIdRemovalStatus::NoSupport,
|
||||
@@ -670,16 +746,19 @@ pub async fn deactivate_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/*/
|
||||
/// # `GET _matrix/client/r0/account/3pid`
|
||||
///
|
||||
/// Get a list of third party identifiers associated with this account.
|
||||
///
|
||||
/// - Currently always returns empty list
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/account/3pid", data = "<body>")
|
||||
)]
|
||||
pub async fn third_party_route(
|
||||
body: Ruma<account::add_3pid::Request<'_>>,
|
||||
) -> ConduitResult<account::add_3pid::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
body: Ruma<get_contacts::Request>,
|
||||
) -> ConduitResult<get_contacts::Response> {
|
||||
let _sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
Ok(account::add_3pid::Response::default().into())
|
||||
Ok(get_contacts::Response::new(Vec::new()).into())
|
||||
}
|
||||
*/
|
||||
|
||||
+38
-12
@@ -1,7 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Ruma};
|
||||
use regex::Regex;
|
||||
use ruma::{
|
||||
api::{
|
||||
@@ -18,15 +15,25 @@ use ruma::{
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{delete, get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/directory/room/{roomAlias}`
|
||||
///
|
||||
/// Creates a new room alias on this server.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/directory/room/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn create_alias_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_alias::Request<'_>>,
|
||||
) -> ConduitResult<create_alias::Response> {
|
||||
if body.room_alias.server_name() != db.globals.server_name() {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Alias is from another server.",
|
||||
));
|
||||
}
|
||||
|
||||
if db.rooms.id_from_alias(&body.room_alias)?.is_some() {
|
||||
return Err(Error::Conflict("Alias already exists."));
|
||||
}
|
||||
@@ -34,40 +41,60 @@ pub async fn create_alias_route(
|
||||
db.rooms
|
||||
.set_alias(&body.room_alias, Some(&body.room_id), &db.globals)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_alias::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/directory/room/{roomAlias}`
|
||||
///
|
||||
/// Deletes a room alias from this server.
|
||||
///
|
||||
/// - TODO: additional access control checks
|
||||
/// - TODO: Update canonical alias event
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/r0/directory/room/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_alias_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_alias::Request<'_>>,
|
||||
) -> ConduitResult<delete_alias::Response> {
|
||||
if body.room_alias.server_name() != db.globals.server_name() {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Alias is from another server.",
|
||||
));
|
||||
}
|
||||
|
||||
db.rooms.set_alias(&body.room_alias, None, &db.globals)?;
|
||||
|
||||
db.flush().await?;
|
||||
// TODO: update alt_aliases?
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_alias::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/directory/room/{roomAlias}`
|
||||
///
|
||||
/// Resolve an alias locally or over federation.
|
||||
///
|
||||
/// - TODO: Suggest more servers to join via
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/directory/room/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_alias_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_alias::Request<'_>>,
|
||||
) -> ConduitResult<get_alias::Response> {
|
||||
get_alias_helper(&db, &body.room_alias).await
|
||||
}
|
||||
|
||||
pub async fn get_alias_helper(
|
||||
pub(crate) async fn get_alias_helper(
|
||||
db: &Database,
|
||||
room_alias: &RoomAliasId,
|
||||
) -> ConduitResult<get_alias::Response> {
|
||||
@@ -88,8 +115,7 @@ pub async fn get_alias_helper(
|
||||
match db.rooms.id_from_alias(&room_alias)? {
|
||||
Some(r) => room_id = Some(r),
|
||||
None => {
|
||||
let iter = db.appservice.iter_all()?;
|
||||
for (_id, registration) in iter.filter_map(|r| r.ok()) {
|
||||
for (_id, registration) in db.appservice.all()? {
|
||||
let aliases = registration
|
||||
.get("namespaces")
|
||||
.and_then(|ns| ns.get("aliases"))
|
||||
|
||||
+115
-29
@@ -1,7 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
r0::backup::{
|
||||
@@ -15,13 +12,16 @@ use ruma::api::client::{
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{delete, get, post, put};
|
||||
|
||||
/// # `POST /_matrix/client/r0/room_keys/version`
|
||||
///
|
||||
/// Creates a new backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/unstable/room_keys/version", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn create_backup_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_backup::Request>,
|
||||
) -> ConduitResult<create_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -29,36 +29,42 @@ pub async fn create_backup_route(
|
||||
.key_backups
|
||||
.create_backup(&sender_user, &body.algorithm, &db.globals)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_backup::Response { version }.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/room_keys/version/{version}`
|
||||
///
|
||||
/// Update information about an existing backup. Only `auth_data` can be modified.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/unstable/room_keys/version/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn update_backup_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<update_backup::Request<'_>>,
|
||||
) -> ConduitResult<update_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
db.key_backups
|
||||
.update_backup(&sender_user, &body.version, &body.algorithm, &db.globals)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(update_backup::Response.into())
|
||||
Ok(update_backup::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/room_keys/version`
|
||||
///
|
||||
/// Get information about the latest backup version.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/unstable/room_keys/version", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_latest_backup_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_latest_backup::Request>,
|
||||
) -> ConduitResult<get_latest_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -80,13 +86,16 @@ pub async fn get_latest_backup_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/room_keys/version`
|
||||
///
|
||||
/// Get information about an existing backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/unstable/room_keys/version/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_backup_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_backup::Request<'_>>,
|
||||
) -> ConduitResult<get_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -107,36 +116,59 @@ pub async fn get_backup_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/room_keys/version/{version}`
|
||||
///
|
||||
/// Delete an existing key backup.
|
||||
///
|
||||
/// - Deletes both information about the backup, as well as all key data related to the backup
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/unstable/room_keys/version/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_backup_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_backup::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
db.key_backups.delete_backup(&sender_user, &body.version)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_backup::Response.into())
|
||||
Ok(delete_backup::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/room_keys/keys`
|
||||
///
|
||||
/// Add the received backup keys to the database.
|
||||
///
|
||||
/// - Only manipulating the most recently created version of the backup is allowed
|
||||
/// - Adds the keys to the backup
|
||||
/// - Returns the new number of keys in this backup and the etag
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/unstable/room_keys/keys", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn add_backup_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<add_backup_keys::Request<'_>>,
|
||||
) -> ConduitResult<add_backup_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if Some(&body.version)
|
||||
!= db
|
||||
.key_backups
|
||||
.get_latest_backup_version(sender_user)?
|
||||
.as_ref()
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"You may only manipulate the most recently created version of the backup.",
|
||||
));
|
||||
}
|
||||
|
||||
for (room_id, room) in &body.rooms {
|
||||
for (session_id, key_data) in &room.sessions {
|
||||
db.key_backups.add_key(
|
||||
@@ -150,7 +182,7 @@ pub async fn add_backup_keys_route(
|
||||
}
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(add_backup_keys::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
@@ -159,18 +191,36 @@ pub async fn add_backup_keys_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/room_keys/keys/{roomId}`
|
||||
///
|
||||
/// Add the received backup keys to the database.
|
||||
///
|
||||
/// - Only manipulating the most recently created version of the backup is allowed
|
||||
/// - Adds the keys to the backup
|
||||
/// - Returns the new number of keys in this backup and the etag
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/unstable/room_keys/keys/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn add_backup_key_sessions_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<add_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<add_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if Some(&body.version)
|
||||
!= db
|
||||
.key_backups
|
||||
.get_latest_backup_version(sender_user)?
|
||||
.as_ref()
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"You may only manipulate the most recently created version of the backup.",
|
||||
));
|
||||
}
|
||||
|
||||
for (session_id, key_data) in &body.sessions {
|
||||
db.key_backups.add_key(
|
||||
&sender_user,
|
||||
@@ -182,7 +232,7 @@ pub async fn add_backup_key_sessions_route(
|
||||
)?
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(add_backup_key_sessions::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
@@ -191,18 +241,36 @@ pub async fn add_backup_key_sessions_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}`
|
||||
///
|
||||
/// Add the received backup key to the database.
|
||||
///
|
||||
/// - Only manipulating the most recently created version of the backup is allowed
|
||||
/// - Adds the keys to the backup
|
||||
/// - Returns the new number of keys in this backup and the etag
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/unstable/room_keys/keys/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn add_backup_key_session_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<add_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<add_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if Some(&body.version)
|
||||
!= db
|
||||
.key_backups
|
||||
.get_latest_backup_version(sender_user)?
|
||||
.as_ref()
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"You may only manipulate the most recently created version of the backup.",
|
||||
));
|
||||
}
|
||||
|
||||
db.key_backups.add_key(
|
||||
&sender_user,
|
||||
&body.version,
|
||||
@@ -212,7 +280,7 @@ pub async fn add_backup_key_session_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(add_backup_key_session::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
@@ -221,13 +289,16 @@ pub async fn add_backup_key_session_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/room_keys/keys`
|
||||
///
|
||||
/// Retrieves all keys from the backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/unstable/room_keys/keys", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_backup_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_backup_keys::Request<'_>>,
|
||||
) -> ConduitResult<get_backup_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -237,13 +308,16 @@ pub async fn get_backup_keys_route(
|
||||
Ok(get_backup_keys::Response { rooms }.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/room_keys/keys/{roomId}`
|
||||
///
|
||||
/// Retrieves all keys from the backup for a given room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/unstable/room_keys/keys/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_backup_key_sessions_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<get_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -255,13 +329,16 @@ pub async fn get_backup_key_sessions_route(
|
||||
Ok(get_backup_key_sessions::Response { sessions }.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}`
|
||||
///
|
||||
/// Retrieves a key from the backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/unstable/room_keys/keys/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_backup_key_session_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<get_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -277,13 +354,16 @@ pub async fn get_backup_key_session_route(
|
||||
Ok(get_backup_key_session::Response { key_data }.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/room_keys/keys`
|
||||
///
|
||||
/// Delete the keys from the backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/unstable/room_keys/keys", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_backup_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_backup_keys::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -291,7 +371,7 @@ pub async fn delete_backup_keys_route(
|
||||
db.key_backups
|
||||
.delete_all_keys(&sender_user, &body.version)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_backup_keys::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
@@ -300,13 +380,16 @@ pub async fn delete_backup_keys_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/room_keys/keys/{roomId}`
|
||||
///
|
||||
/// Delete the keys from the backup for a given room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/unstable/room_keys/keys/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_backup_key_sessions_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -314,7 +397,7 @@ pub async fn delete_backup_key_sessions_route(
|
||||
db.key_backups
|
||||
.delete_room_keys(&sender_user, &body.version, &body.room_id)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_backup_key_sessions::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
@@ -323,13 +406,16 @@ pub async fn delete_backup_key_sessions_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}`
|
||||
///
|
||||
/// Delete a key from the backup.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/unstable/room_keys/keys/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_backup_key_session_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -337,7 +423,7 @@ pub async fn delete_backup_key_session_route(
|
||||
db.key_backups
|
||||
.delete_room_key(&sender_user, &body.version, &body.room_id, &body.session_id)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_backup_key_session::Response {
|
||||
count: (db.key_backups.count_keys(sender_user, &body.version)? as u32).into(),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::ConduitResult;
|
||||
use crate::Ruma;
|
||||
use ruma::{
|
||||
api::client::r0::capabilities::{
|
||||
get_capabilities, Capabilities, RoomVersionStability, RoomVersionsCapability,
|
||||
@@ -12,11 +13,17 @@ use rocket::get;
|
||||
|
||||
/// # `GET /_matrix/client/r0/capabilities`
|
||||
///
|
||||
/// Get information on this server's supported feature set and other relevent capabilities.
|
||||
#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/capabilities"))]
|
||||
#[tracing::instrument]
|
||||
pub async fn get_capabilities_route() -> ConduitResult<get_capabilities::Response> {
|
||||
/// Get information on the supported feature set and other relevent capabilities of this server.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/capabilities", data = "<_body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(_body))]
|
||||
pub async fn get_capabilities_route(
|
||||
_body: Ruma<get_capabilities::Request>,
|
||||
) -> ConduitResult<get_capabilities::Response> {
|
||||
let mut available = BTreeMap::new();
|
||||
available.insert(RoomVersionId::Version5, RoomVersionStability::Stable);
|
||||
available.insert(RoomVersionId::Version6, RoomVersionStability::Stable);
|
||||
|
||||
let mut capabilities = Capabilities::new();
|
||||
|
||||
+21
-14
@@ -1,7 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -19,13 +16,16 @@ use serde_json::{json, value::RawValue as RawJsonValue};
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}`
|
||||
///
|
||||
/// Sets some account data for the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_global_account_data_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_global_account_data::Request<'_>>,
|
||||
) -> ConduitResult<set_global_account_data::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -46,11 +46,14 @@ pub async fn set_global_account_data_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_global_account_data::Response.into())
|
||||
Ok(set_global_account_data::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
|
||||
///
|
||||
/// Sets some room account data for the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put(
|
||||
@@ -60,7 +63,7 @@ pub async fn set_global_account_data_route(
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_room_account_data_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_room_account_data::Request<'_>>,
|
||||
) -> ConduitResult<set_room_account_data::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -81,18 +84,21 @@ pub async fn set_room_account_data_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_room_account_data::Response.into())
|
||||
Ok(set_room_account_data::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/user/{userId}/account_data/{type}`
|
||||
///
|
||||
/// Gets some account data for the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_global_account_data_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_global_account_data::Request<'_>>,
|
||||
) -> ConduitResult<get_global_account_data::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -101,7 +107,6 @@ pub async fn get_global_account_data_route(
|
||||
.account_data
|
||||
.get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
db.flush().await?;
|
||||
|
||||
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
@@ -110,6 +115,9 @@ pub async fn get_global_account_data_route(
|
||||
Ok(get_global_account_data::Response { account_data }.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
|
||||
///
|
||||
/// Gets some room account data for the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get(
|
||||
@@ -119,7 +127,7 @@ pub async fn get_global_account_data_route(
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_room_account_data_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_room_account_data::Request<'_>>,
|
||||
) -> ConduitResult<get_room_account_data::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -132,7 +140,6 @@ pub async fn get_room_account_data_route(
|
||||
body.event_type.clone().into(),
|
||||
)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
db.flush().await?;
|
||||
|
||||
let account_data = serde_json::from_str::<ExtractRoomEventContent>(event.get())
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::api::client::{error::ErrorKind, r0::context::get_context};
|
||||
use std::{convert::TryFrom, sync::Arc};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::get;
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/context`
|
||||
///
|
||||
/// Allows loading room history around an event.
|
||||
///
|
||||
/// - Only works if the user is joined (TODO: always allow, but only show events if the user was
|
||||
/// joined, depending on history_visibility)
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/context/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_context_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_context::Request<'_>>,
|
||||
) -> ConduitResult<get_context::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -45,7 +50,7 @@ pub async fn get_context_route(
|
||||
|
||||
let events_before = db
|
||||
.rooms
|
||||
.pdus_until(&sender_user, &body.room_id, base_token)
|
||||
.pdus_until(&sender_user, &body.room_id, base_token)?
|
||||
.take(
|
||||
u32::try_from(body.limit).map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
|
||||
@@ -67,7 +72,7 @@ pub async fn get_context_route(
|
||||
|
||||
let events_after = db
|
||||
.rooms
|
||||
.pdus_after(&sender_user, &body.room_id, base_token)
|
||||
.pdus_after(&sender_user, &body.room_id, base_token)?
|
||||
.take(
|
||||
u32::try_from(body.limit).map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
|
||||
|
||||
+51
-29
@@ -1,7 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{utils, ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Error, Ruma};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
r0::{
|
||||
@@ -14,13 +11,16 @@ use super::SESSION_ID_LENGTH;
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{delete, get, post, put};
|
||||
|
||||
/// # `GET /_matrix/client/r0/devices`
|
||||
///
|
||||
/// Get metadata on all devices of the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/devices", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_devices_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_devices::Request>,
|
||||
) -> ConduitResult<get_devices::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -34,13 +34,16 @@ pub async fn get_devices_route(
|
||||
Ok(get_devices::Response { devices }.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/devices/{deviceId}`
|
||||
///
|
||||
/// Get metadata on a single device of the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/devices/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_device_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_device::Request<'_>>,
|
||||
) -> ConduitResult<get_device::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -53,13 +56,16 @@ pub async fn get_device_route(
|
||||
Ok(get_device::Response { device }.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/devices/{deviceId}`
|
||||
///
|
||||
/// Updates the metadata on a given device of the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/devices/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn update_device_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<update_device::Request<'_>>,
|
||||
) -> ConduitResult<update_device::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -74,18 +80,27 @@ pub async fn update_device_route(
|
||||
db.users
|
||||
.update_device_metadata(&sender_user, &body.device_id, &device)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(update_device::Response.into())
|
||||
Ok(update_device::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/devices/{deviceId}`
|
||||
///
|
||||
/// Deletes the given device.
|
||||
///
|
||||
/// - Requires UIAA to verify user password
|
||||
/// - Invalidates access token
|
||||
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets to-device events
|
||||
/// - Triggers device list updates
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/r0/devices/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_device_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_device::Request<'_>>,
|
||||
) -> ConduitResult<delete_device::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -115,31 +130,40 @@ pub async fn delete_device_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&sender_user,
|
||||
&sender_device,
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
)?;
|
||||
db.uiaa
|
||||
.create(&sender_user, &sender_device, &uiaainfo, &json)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
|
||||
db.users.remove_device(&sender_user, &body.device_id)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_device::Response.into())
|
||||
Ok(delete_device::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/devices/{deviceId}`
|
||||
///
|
||||
/// Deletes the given device.
|
||||
///
|
||||
/// - Requires UIAA to verify user password
|
||||
///
|
||||
/// For each device:
|
||||
/// - Invalidates access token
|
||||
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets to-device events
|
||||
/// - Triggers device list updates
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/delete_devices", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_devices_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_devices::Request<'_>>,
|
||||
) -> ConduitResult<delete_devices::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -169,22 +193,20 @@ pub async fn delete_devices_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&sender_user,
|
||||
&sender_device,
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
)?;
|
||||
db.uiaa
|
||||
.create(&sender_user, &sender_device, &uiaainfo, &json)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
|
||||
for device_id in &body.devices {
|
||||
db.users.remove_device(&sender_user, &device_id)?
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_devices::Response.into())
|
||||
Ok(delete_devices::Response {}.into())
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::convert::TryInto;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Result, Ruma};
|
||||
use log::info;
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::{
|
||||
@@ -25,17 +23,23 @@ use ruma::{
|
||||
serde::Raw,
|
||||
ServerName, UInt,
|
||||
};
|
||||
use tracing::{info, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post, put};
|
||||
|
||||
/// # `POST /_matrix/client/r0/publicRooms`
|
||||
///
|
||||
/// Lists the public rooms on this server.
|
||||
///
|
||||
/// - Rooms are ordered by the number of joined members
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/publicRooms", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_public_rooms_filtered_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_public_rooms_filtered::Request<'_>>,
|
||||
) -> ConduitResult<get_public_rooms_filtered::Response> {
|
||||
get_public_rooms_filtered_helper(
|
||||
@@ -49,13 +53,18 @@ pub async fn get_public_rooms_filtered_route(
|
||||
.await
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/publicRooms`
|
||||
///
|
||||
/// Lists the public rooms on this server.
|
||||
///
|
||||
/// - Rooms are ordered by the number of joined members
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/publicRooms", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_public_rooms_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_public_rooms::Request<'_>>,
|
||||
) -> ConduitResult<get_public_rooms::Response> {
|
||||
let response = get_public_rooms_filtered_helper(
|
||||
@@ -78,43 +87,51 @@ pub async fn get_public_rooms_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/directory/list/room/{roomId}`
|
||||
///
|
||||
/// Sets the visibility of a given room in the room directory.
|
||||
///
|
||||
/// - TODO: Access control checks
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/directory/list/room/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_room_visibility_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_room_visibility::Request<'_>>,
|
||||
) -> ConduitResult<set_room_visibility::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
match &body.visibility {
|
||||
room::Visibility::_Custom(_s) => {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Room visibility type is not supported.",
|
||||
));
|
||||
}
|
||||
room::Visibility::Public => {
|
||||
db.rooms.set_public(&body.room_id, true)?;
|
||||
info!("{} made {} public", sender_user, body.room_id);
|
||||
}
|
||||
room::Visibility::Private => db.rooms.set_public(&body.room_id, false)?,
|
||||
_ => {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Room visibility type is not supported.",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_room_visibility::Response.into())
|
||||
Ok(set_room_visibility::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/directory/list/room/{roomId}`
|
||||
///
|
||||
/// Gets the visibility of a given room in the room directory.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/directory/list/room/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_room_visibility_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_room_visibility::Request<'_>>,
|
||||
) -> ConduitResult<get_room_visibility::Response> {
|
||||
Ok(get_room_visibility::Response {
|
||||
@@ -127,7 +144,7 @@ pub async fn get_room_visibility_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
pub async fn get_public_rooms_filtered_helper(
|
||||
pub(crate) async fn get_public_rooms_filtered_helper(
|
||||
db: &Database,
|
||||
server: Option<&ServerName>,
|
||||
limit: Option<UInt>,
|
||||
@@ -234,10 +251,17 @@ pub async fn get_public_rooms_filtered_helper(
|
||||
.map_err(|_| {
|
||||
Error::bad_database("Invalid room name event in database.")
|
||||
})?
|
||||
.name()
|
||||
.map(|n| n.to_owned()))
|
||||
.name)
|
||||
})?,
|
||||
num_joined_members: (db.rooms.room_members(&room_id).count() as u32).into(),
|
||||
num_joined_members: db
|
||||
.rooms
|
||||
.room_joined_count(&room_id)?
|
||||
.unwrap_or_else(|| {
|
||||
warn!("Room {} has no member count", room_id);
|
||||
0
|
||||
})
|
||||
.try_into()
|
||||
.expect("user count should not be that big"),
|
||||
topic: db
|
||||
.rooms
|
||||
.room_state_get(&room_id, &EventType::RoomTopic, "")?
|
||||
@@ -319,7 +343,7 @@ pub async fn get_public_rooms_filtered_helper(
|
||||
.map(|q| q.to_lowercase())
|
||||
{
|
||||
if let Some(name) = &chunk.name {
|
||||
if name.to_lowercase().contains(&query) {
|
||||
if name.as_str().to_lowercase().contains(&query) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ use ruma::api::client::r0::filter::{self, create_filter, get_filter};
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
||||
/// # `GET /_matrix/client/r0/user/{userId}/filter/{filterId}`
|
||||
///
|
||||
/// TODO: Loads a filter that was previously created.
|
||||
#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/user/<_>/filter/<_>"))]
|
||||
#[tracing::instrument]
|
||||
pub async fn get_filter_route() -> ConduitResult<get_filter::Response> {
|
||||
@@ -18,6 +21,9 @@ pub async fn get_filter_route() -> ConduitResult<get_filter::Response> {
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/user/{userId}/filter`
|
||||
///
|
||||
/// TODO: Creates a new filter to be used by other endpoints.
|
||||
#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/user/<_>/filter"))]
|
||||
#[tracing::instrument]
|
||||
pub async fn create_filter_route() -> ConduitResult<create_filter::Response> {
|
||||
|
||||
+151
-38
@@ -1,34 +1,42 @@
|
||||
use super::{State, SESSION_ID_LENGTH};
|
||||
use crate::{utils, ConduitResult, Database, Error, Result, Ruma};
|
||||
use super::SESSION_ID_LENGTH;
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Database, Error, Result, Ruma};
|
||||
use rocket::futures::{prelude::*, stream::FuturesUnordered};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::{
|
||||
keys::{
|
||||
claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures,
|
||||
upload_signing_keys,
|
||||
api::{
|
||||
client::{
|
||||
error::ErrorKind,
|
||||
r0::{
|
||||
keys::{
|
||||
claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures,
|
||||
upload_signing_keys,
|
||||
},
|
||||
uiaa::{AuthFlow, UiaaInfo},
|
||||
},
|
||||
uiaa::{AuthFlow, UiaaInfo},
|
||||
},
|
||||
federation,
|
||||
},
|
||||
encryption::UnsignedDeviceInfo,
|
||||
DeviceId, DeviceKeyAlgorithm, UserId,
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashSet},
|
||||
sync::Arc,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/upload`
|
||||
///
|
||||
/// Publish end-to-end encryption keys for the sender device.
|
||||
///
|
||||
/// - Adds one time keys
|
||||
/// - If there are no device keys yet: Adds device keys (TODO: merge with existing keys?)
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/keys/upload", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn upload_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<upload_keys::Request>,
|
||||
) -> ConduitResult<upload_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -47,6 +55,7 @@ pub async fn upload_keys_route(
|
||||
}
|
||||
|
||||
if let Some(device_keys) = &body.device_keys {
|
||||
// TODO: merge this and the existing event?
|
||||
// This check is needed to assure that signatures are kept
|
||||
if db
|
||||
.users
|
||||
@@ -63,7 +72,7 @@ pub async fn upload_keys_route(
|
||||
}
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(upload_keys::Response {
|
||||
one_time_key_counts: db.users.count_one_time_keys(sender_user, sender_device)?,
|
||||
@@ -71,13 +80,20 @@ pub async fn upload_keys_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/query`
|
||||
///
|
||||
/// Get end-to-end encryption keys for the given users.
|
||||
///
|
||||
/// - Always fetches users from other servers over federation
|
||||
/// - Gets master keys, self-signing keys, user signing keys and device keys.
|
||||
/// - The master and self-signing keys contain signatures that the user is allowed to see
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/keys/query", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_keys::Request<'_>>,
|
||||
) -> ConduitResult<get_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -87,34 +103,43 @@ pub async fn get_keys_route(
|
||||
&body.device_keys,
|
||||
|u| u == sender_user,
|
||||
&db,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(response.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/claim`
|
||||
///
|
||||
/// Claims one-time keys
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/keys/claim", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn claim_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<claim_keys::Request>,
|
||||
) -> ConduitResult<claim_keys::Response> {
|
||||
let response = claim_keys_helper(&body.one_time_keys, &db)?;
|
||||
let response = claim_keys_helper(&body.one_time_keys, &db).await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(response.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/device_signing/upload`
|
||||
///
|
||||
/// Uploads end-to-end key information for the sender user.
|
||||
///
|
||||
/// - Requires UIAA to verify password
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/unstable/keys/device_signing/upload", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn upload_signing_keys_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<upload_signing_keys::Request<'_>>,
|
||||
) -> ConduitResult<upload_signing_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -144,15 +169,13 @@ pub async fn upload_signing_keys_route(
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
}
|
||||
// Success!
|
||||
} else {
|
||||
} else if let Some(json) = body.json_body {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
db.uiaa.create(
|
||||
&sender_user,
|
||||
&sender_device,
|
||||
&uiaainfo,
|
||||
&body.json_body.expect("body is json"),
|
||||
)?;
|
||||
db.uiaa
|
||||
.create(&sender_user, &sender_device, &uiaainfo, &json)?;
|
||||
return Err(Error::Uiaa(uiaainfo));
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
|
||||
}
|
||||
|
||||
if let Some(master_key) = &body.master_key {
|
||||
@@ -166,18 +189,21 @@ pub async fn upload_signing_keys_route(
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(upload_signing_keys::Response.into())
|
||||
Ok(upload_signing_keys::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/signatures/upload`
|
||||
///
|
||||
/// Uploads end-to-end key signatures from the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/unstable/keys/signatures/upload", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn upload_signatures_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<upload_signatures::Request>,
|
||||
) -> ConduitResult<upload_signatures::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -227,18 +253,23 @@ pub async fn upload_signatures_route(
|
||||
}
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(upload_signatures::Response.into())
|
||||
Ok(upload_signatures::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/keys/changes`
|
||||
///
|
||||
/// Gets a list of users who have updated their device identity keys since the previous sync token.
|
||||
///
|
||||
/// - TODO: left users
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/keys/changes", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_key_changes_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_key_changes::Request<'_>>,
|
||||
) -> ConduitResult<get_key_changes::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -283,7 +314,7 @@ pub async fn get_key_changes_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
pub fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
sender_user: Option<&UserId>,
|
||||
device_keys_input: &BTreeMap<UserId, Vec<Box<DeviceId>>>,
|
||||
allowed_signatures: F,
|
||||
@@ -294,7 +325,17 @@ pub fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
let mut user_signing_keys = BTreeMap::new();
|
||||
let mut device_keys = BTreeMap::new();
|
||||
|
||||
let mut get_over_federation = HashMap::new();
|
||||
|
||||
for (user_id, device_ids) in device_keys_input {
|
||||
if user_id.server_name() != db.globals.server_name() {
|
||||
get_over_federation
|
||||
.entry(user_id.server_name())
|
||||
.or_insert_with(Vec::new)
|
||||
.push((user_id, device_ids));
|
||||
continue;
|
||||
}
|
||||
|
||||
if device_ids.is_empty() {
|
||||
let mut container = BTreeMap::new();
|
||||
for device_id in db.users.all_device_ids(user_id) {
|
||||
@@ -352,21 +393,68 @@ pub fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
}
|
||||
}
|
||||
|
||||
let mut failures = BTreeMap::new();
|
||||
|
||||
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.clone(), keys.clone());
|
||||
}
|
||||
(
|
||||
server,
|
||||
db.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
server,
|
||||
federation::keys::get_keys::v1::Request {
|
||||
device_keys: device_keys_input_fed,
|
||||
},
|
||||
)
|
||||
.await,
|
||||
)
|
||||
})
|
||||
.collect::<FuturesUnordered<_>>();
|
||||
|
||||
while let Some((server, response)) = futures.next().await {
|
||||
match response {
|
||||
Ok(response) => {
|
||||
master_keys.extend(response.master_keys);
|
||||
self_signing_keys.extend(response.self_signing_keys);
|
||||
device_keys.extend(response.device_keys);
|
||||
}
|
||||
Err(_e) => {
|
||||
failures.insert(server.to_string(), json!({}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(get_keys::Response {
|
||||
master_keys,
|
||||
self_signing_keys,
|
||||
user_signing_keys,
|
||||
device_keys,
|
||||
failures: BTreeMap::new(),
|
||||
failures,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn claim_keys_helper(
|
||||
pub(crate) async fn claim_keys_helper(
|
||||
one_time_keys_input: &BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>,
|
||||
db: &Database,
|
||||
) -> Result<claim_keys::Response> {
|
||||
let mut one_time_keys = BTreeMap::new();
|
||||
|
||||
let mut get_over_federation = BTreeMap::new();
|
||||
|
||||
for (user_id, map) in one_time_keys_input {
|
||||
if user_id.server_name() != db.globals.server_name() {
|
||||
get_over_federation
|
||||
.entry(user_id.server_name())
|
||||
.or_insert_with(Vec::new)
|
||||
.push((user_id, map));
|
||||
}
|
||||
|
||||
let mut container = BTreeMap::new();
|
||||
for (device_id, key_algorithm) in map {
|
||||
if let Some(one_time_keys) =
|
||||
@@ -381,8 +469,33 @@ pub fn claim_keys_helper(
|
||||
one_time_keys.insert(user_id.clone(), container);
|
||||
}
|
||||
|
||||
let mut failures = BTreeMap::new();
|
||||
|
||||
for (server, vec) in get_over_federation {
|
||||
let mut one_time_keys_input_fed = BTreeMap::new();
|
||||
for (user_id, keys) in vec {
|
||||
one_time_keys_input_fed.insert(user_id.clone(), keys.clone());
|
||||
}
|
||||
// Ignore failures
|
||||
if let Ok(keys) = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
server,
|
||||
federation::keys::claim_keys::v1::Request {
|
||||
one_time_keys: one_time_keys_input_fed,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
one_time_keys.extend(keys.one_time_keys);
|
||||
} else {
|
||||
failures.insert(server.to_string(), json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(claim_keys::Response {
|
||||
failures: BTreeMap::new(),
|
||||
failures,
|
||||
one_time_keys,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
use super::State;
|
||||
use crate::{database::media::FileMeta, utils, ConduitResult, Database, Error, Ruma};
|
||||
use crate::{
|
||||
database::media::FileMeta, database::DatabaseGuard, utils, ConduitResult, Error, Ruma,
|
||||
};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
r0::media::{create_content, get_content, get_content_thumbnail, get_media_config},
|
||||
};
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
const MXC_LENGTH: usize = 32;
|
||||
|
||||
/// # `GET /_matrix/media/r0/config`
|
||||
///
|
||||
/// Returns max upload size.
|
||||
#[cfg_attr(feature = "conduit_bin", get("/_matrix/media/r0/config"))]
|
||||
#[tracing::instrument(skip(db))]
|
||||
pub async fn get_media_config_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
) -> ConduitResult<get_media_config::Response> {
|
||||
Ok(get_media_config::Response {
|
||||
upload_size: db.globals.max_request_size().into(),
|
||||
@@ -22,13 +26,19 @@ pub async fn get_media_config_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/media/r0/upload`
|
||||
///
|
||||
/// Permanently save media in the server.
|
||||
///
|
||||
/// - Some metadata will be saved in the database
|
||||
/// - Media will be saved in the media/ directory
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/media/r0/upload", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn create_content_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_content::Request<'_>>,
|
||||
) -> ConduitResult<create_content::Response> {
|
||||
let mxc = format!(
|
||||
@@ -51,7 +61,7 @@ pub async fn create_content_route(
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_content::Response {
|
||||
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
|
||||
@@ -60,13 +70,18 @@ pub async fn create_content_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/media/r0/download/{serverName}/{mediaId}`
|
||||
///
|
||||
/// Load media from our server or over federation.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/media/r0/download/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_content_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_content::Request<'_>>,
|
||||
) -> ConduitResult<get_content::Response> {
|
||||
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
||||
@@ -113,13 +128,18 @@ pub async fn get_content_route(
|
||||
}
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
|
||||
///
|
||||
/// Load media thumbnail from our server or over federation.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/media/r0/thumbnail/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_content_thumbnail_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_content_thumbnail::Request<'_>>,
|
||||
) -> ConduitResult<get_content_thumbnail::Response> {
|
||||
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
||||
|
||||
+402
-214
@@ -1,12 +1,10 @@
|
||||
use super::State;
|
||||
use crate::{
|
||||
client_server,
|
||||
database::DatabaseGuard,
|
||||
pdu::{PduBuilder, PduEvent},
|
||||
server_server, utils, ConduitResult, Database, Error, Result, Ruma,
|
||||
};
|
||||
use log::{debug, error, warn};
|
||||
use member::{MemberEventContent, MembershipState};
|
||||
use rocket::futures;
|
||||
use ruma::{
|
||||
api::{
|
||||
client::{
|
||||
@@ -29,22 +27,29 @@ use ruma::{
|
||||
uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
|
||||
};
|
||||
use std::{
|
||||
collections::{btree_map::Entry, BTreeMap, HashSet},
|
||||
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::{Arc, RwLock},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/join`
|
||||
///
|
||||
/// Tries to join the sender user into a room.
|
||||
///
|
||||
/// - If the server knowns about this room: creates the join event and does auth rules locally
|
||||
/// - If the server does not know about the room: asks other servers over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/join", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn join_room_by_id_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<join_room_by_id::Request<'_>>,
|
||||
) -> ConduitResult<join_room_by_id::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -65,23 +70,33 @@ pub async fn join_room_by_id_route(
|
||||
|
||||
servers.insert(body.room_id.server_name().to_owned());
|
||||
|
||||
join_room_by_id_helper(
|
||||
let ret = join_room_by_id_helper(
|
||||
&db,
|
||||
body.sender_user.as_ref(),
|
||||
&body.room_id,
|
||||
&servers,
|
||||
body.third_party_signed.as_ref(),
|
||||
)
|
||||
.await
|
||||
.await;
|
||||
|
||||
db.flush()?;
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/join/{roomIdOrAlias}`
|
||||
///
|
||||
/// Tries to join the sender user into a room.
|
||||
///
|
||||
/// - If the server knowns about this room: creates the join event and does auth rules locally
|
||||
/// - If the server does not know about the room: asks other servers over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/join/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn join_room_by_id_or_alias_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
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_ref().expect("user is authenticated");
|
||||
@@ -121,7 +136,7 @@ pub async fn join_room_by_id_or_alias_route(
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(join_room_by_id_or_alias::Response {
|
||||
room_id: join_room_response.0.room_id,
|
||||
@@ -129,51 +144,62 @@ pub async fn join_room_by_id_or_alias_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/leave`
|
||||
///
|
||||
/// Tries to leave the sender user from a room.
|
||||
///
|
||||
/// - This should always work if the user is currently joined.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/leave", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn leave_room_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<leave_room::Request<'_>>,
|
||||
) -> ConduitResult<leave_room::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
db.rooms.leave_room(sender_user, &body.room_id, &db).await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(leave_room::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/invite`
|
||||
///
|
||||
/// Tries to send an invite event into the room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/invite", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn invite_user_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<invite_user::Request<'_>>,
|
||||
) -> ConduitResult<invite_user::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if let invite_user::IncomingInvitationRecipient::UserId { user_id } = &body.recipient {
|
||||
invite_helper(sender_user, user_id, &body.room_id, &db, false).await?;
|
||||
db.flush().await?;
|
||||
Ok(invite_user::Response.into())
|
||||
db.flush()?;
|
||||
Ok(invite_user::Response {}.into())
|
||||
} else {
|
||||
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))
|
||||
}
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/kick`
|
||||
///
|
||||
/// Tries to send a kick event into the room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/kick", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn kick_user_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<kick_user::Request<'_>>,
|
||||
) -> ConduitResult<kick_user::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -199,6 +225,16 @@ pub async fn kick_user_route(
|
||||
event.membership = ruma::events::room::member::MembershipState::Leave;
|
||||
// TODO: reason
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
@@ -210,20 +246,26 @@ pub async fn kick_user_route(
|
||||
&sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(kick_user::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/ban`
|
||||
///
|
||||
/// Tries to send a ban event into the room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/ban", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn ban_user_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<ban_user::Request<'_>>,
|
||||
) -> ConduitResult<ban_user::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -244,6 +286,8 @@ pub async fn ban_user_route(
|
||||
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,
|
||||
}),
|
||||
|event| {
|
||||
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
|
||||
@@ -257,6 +301,16 @@ pub async fn ban_user_route(
|
||||
},
|
||||
)?;
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
@@ -268,20 +322,26 @@ pub async fn ban_user_route(
|
||||
&sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(ban_user::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/unban`
|
||||
///
|
||||
/// Tries to send an unban event into the room.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/unban", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn unban_user_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<unban_user::Request<'_>>,
|
||||
) -> ConduitResult<unban_user::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -306,6 +366,16 @@ pub async fn unban_user_route(
|
||||
|
||||
event.membership = ruma::events::room::member::MembershipState::Leave;
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
@@ -317,38 +387,52 @@ pub async fn unban_user_route(
|
||||
&sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(unban_user::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/forget`
|
||||
///
|
||||
/// Forgets about a room.
|
||||
///
|
||||
/// - If the sender user currently left the room: Stops sender user from receiving information about the room
|
||||
///
|
||||
/// Note: Other devices of the user have no way of knowing the room was forgotten, so this has to
|
||||
/// be called from every device
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/forget", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn forget_room_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<forget_room::Request<'_>>,
|
||||
) -> ConduitResult<forget_room::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
db.rooms.forget(&body.room_id, &sender_user)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(forget_room::Response::new().into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/joined_rooms`
|
||||
///
|
||||
/// Lists all rooms the user has joined.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/joined_rooms", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn joined_rooms_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<joined_rooms::Request>,
|
||||
) -> ConduitResult<joined_rooms::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -363,17 +447,23 @@ pub async fn joined_rooms_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/members`
|
||||
///
|
||||
/// Lists all joined users in a room (TODO: at a specific point in time, with a specific membership).
|
||||
///
|
||||
/// - Only works if the user is currently joined
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/members", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_member_events_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_member_events::Request<'_>>,
|
||||
) -> ConduitResult<get_member_events::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
// TODO: check history visibility?
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
@@ -393,22 +483,24 @@ pub async fn get_member_events_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/joined_members`
|
||||
///
|
||||
/// Lists all members of a room.
|
||||
///
|
||||
/// - The sender user must be in the room
|
||||
/// - TODO: An appservice just needs a puppet joined
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/joined_members", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn joined_members_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<joined_members::Request<'_>>,
|
||||
) -> ConduitResult<joined_members::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if !db
|
||||
.rooms
|
||||
.is_joined(&sender_user, &body.room_id)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
if !db.rooms.is_joined(&sender_user, &body.room_id)? {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You aren't a member of the room.",
|
||||
@@ -442,6 +534,16 @@ async fn join_room_by_id_helper(
|
||||
) -> ConduitResult<join_room_by_id::Response> {
|
||||
let sender_user = sender_user.expect("user is authenticated");
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
// Ask a remote server if we don't have this room
|
||||
if !db.rooms.exists(&room_id)? && room_id.server_name() != db.globals.server_name() {
|
||||
let mut make_join_response_and_server = Err(Error::BadServerResponse(
|
||||
@@ -457,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::Version6],
|
||||
ver: &[RoomVersionId::Version5, RoomVersionId::Version6],
|
||||
},
|
||||
)
|
||||
.await;
|
||||
@@ -472,7 +574,12 @@ async fn join_room_by_id_helper(
|
||||
let (make_join_response, remote_server) = make_join_response_and_server?;
|
||||
|
||||
let room_version = match make_join_response.room_version {
|
||||
Some(room_version) if room_version == RoomVersionId::Version6 => room_version,
|
||||
Some(room_version)
|
||||
if room_version == RoomVersionId::Version5
|
||||
|| room_version == RoomVersionId::Version6 =>
|
||||
{
|
||||
room_version
|
||||
}
|
||||
_ => return Err(Error::BadServerResponse("Room version is not supported")),
|
||||
};
|
||||
|
||||
@@ -503,6 +610,8 @@ async fn join_room_by_id_helper(
|
||||
avatar_url: db.users.avatar_url(&sender_user)?,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&sender_user)?,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
);
|
||||
@@ -549,20 +658,27 @@ async fn join_room_by_id_helper(
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.rooms.get_or_create_shortroomid(&room_id, &db.globals)?;
|
||||
|
||||
let pdu = PduEvent::from_id_val(&event_id, join_event.clone())
|
||||
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
|
||||
|
||||
let mut state = BTreeMap::new();
|
||||
let mut state = HashMap::new();
|
||||
let pub_key_map = RwLock::new(BTreeMap::new());
|
||||
|
||||
for result in futures::future::join_all(
|
||||
send_join_response
|
||||
.room_state
|
||||
.state
|
||||
.iter()
|
||||
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db)),
|
||||
server_server::fetch_join_signing_keys(
|
||||
&send_join_response,
|
||||
&room_version,
|
||||
&pub_key_map,
|
||||
&db,
|
||||
)
|
||||
.await
|
||||
.await?;
|
||||
|
||||
for result in send_join_response
|
||||
.room_state
|
||||
.state
|
||||
.iter()
|
||||
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db))
|
||||
{
|
||||
let (event_id, value) = match result {
|
||||
Ok(t) => t,
|
||||
@@ -576,32 +692,46 @@ async fn join_room_by_id_helper(
|
||||
|
||||
db.rooms.add_pdu_outlier(&event_id, &value)?;
|
||||
if let Some(state_key) = &pdu.state_key {
|
||||
state.insert((pdu.kind.clone(), state_key.clone()), pdu.event_id.clone());
|
||||
let shortstatekey =
|
||||
db.rooms
|
||||
.get_or_create_shortstatekey(&pdu.kind, state_key, &db.globals)?;
|
||||
state.insert(shortstatekey, pdu.event_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
state.insert(
|
||||
(
|
||||
pdu.kind.clone(),
|
||||
pdu.state_key.clone().expect("join event has state key"),
|
||||
),
|
||||
pdu.event_id.clone(),
|
||||
);
|
||||
let incoming_shortstatekey = db.rooms.get_or_create_shortstatekey(
|
||||
&pdu.kind,
|
||||
pdu.state_key
|
||||
.as_ref()
|
||||
.expect("Pdu is a membership state event"),
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
if state.get(&(EventType::RoomCreate, "".to_owned())).is_none() {
|
||||
state.insert(incoming_shortstatekey, pdu.event_id.clone());
|
||||
|
||||
let create_shortstatekey = db
|
||||
.rooms
|
||||
.get_shortstatekey(&EventType::RoomCreate, "")?
|
||||
.expect("Room exists");
|
||||
|
||||
if state.get(&create_shortstatekey).is_none() {
|
||||
return Err(Error::BadServerResponse("State contained no create event."));
|
||||
}
|
||||
|
||||
db.rooms.force_state(room_id, state, &db)?;
|
||||
db.rooms.force_state(
|
||||
room_id,
|
||||
state
|
||||
.into_iter()
|
||||
.map(|(k, id)| db.rooms.compress_state_event(k, &id, &db.globals))
|
||||
.collect::<Result<HashSet<_>>>()?,
|
||||
&db,
|
||||
)?;
|
||||
|
||||
for result in futures::future::join_all(
|
||||
send_join_response
|
||||
.room_state
|
||||
.auth_chain
|
||||
.iter()
|
||||
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db)),
|
||||
)
|
||||
.await
|
||||
for result in send_join_response
|
||||
.room_state
|
||||
.auth_chain
|
||||
.iter()
|
||||
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db))
|
||||
{
|
||||
let (event_id, value) = match result {
|
||||
Ok(t) => t,
|
||||
@@ -615,16 +745,9 @@ async fn join_room_by_id_helper(
|
||||
// pdu without it's state. This is okay because append_pdu can't fail.
|
||||
let statehashid = db.rooms.append_to_state(&pdu, &db.globals)?;
|
||||
|
||||
let count = db.globals.next_count()?;
|
||||
let mut pdu_id = room_id.as_bytes().to_vec();
|
||||
pdu_id.push(0xff);
|
||||
pdu_id.extend_from_slice(&count.to_be_bytes());
|
||||
|
||||
db.rooms.append_pdu(
|
||||
&pdu,
|
||||
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
|
||||
count,
|
||||
&pdu_id,
|
||||
&[pdu.event_id.clone()],
|
||||
db,
|
||||
)?;
|
||||
@@ -639,6 +762,8 @@ async fn join_room_by_id_helper(
|
||||
avatar_url: db.users.avatar_url(&sender_user)?,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&sender_user)?,
|
||||
reason: None,
|
||||
};
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
@@ -652,15 +777,18 @@ async fn join_room_by_id_helper(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(join_room_by_id::Response::new(room_id.clone()).into())
|
||||
}
|
||||
|
||||
async fn validate_and_add_event_id(
|
||||
fn validate_and_add_event_id(
|
||||
pdu: &Raw<Pdu>,
|
||||
room_version: &RoomVersionId,
|
||||
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
|
||||
@@ -703,7 +831,6 @@ async fn validate_and_add_event_id(
|
||||
}
|
||||
}
|
||||
|
||||
server_server::fetch_required_signing_keys(&value, pub_key_map, db).await?;
|
||||
if let Err(e) = ruma::signatures::verify_event(
|
||||
&*pub_key_map
|
||||
.read()
|
||||
@@ -724,7 +851,7 @@ async fn validate_and_add_event_id(
|
||||
Ok((event_id, value))
|
||||
}
|
||||
|
||||
pub async fn invite_helper(
|
||||
pub(crate) async fn invite_helper<'a>(
|
||||
sender_user: &UserId,
|
||||
user_id: &UserId,
|
||||
room_id: &RoomId,
|
||||
@@ -732,141 +859,176 @@ pub async fn invite_helper(
|
||||
is_direct: bool,
|
||||
) -> Result<()> {
|
||||
if user_id.server_name() != db.globals.server_name() {
|
||||
let prev_events = db
|
||||
.rooms
|
||||
.get_pdu_leaves(room_id)?
|
||||
.into_iter()
|
||||
.take(20)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let create_event = db
|
||||
.rooms
|
||||
.room_state_get(room_id, &EventType::RoomCreate, "")?;
|
||||
|
||||
let create_event_content = create_event
|
||||
.as_ref()
|
||||
.map(|create_event| {
|
||||
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone())
|
||||
.expect("Raw::from_value always works.")
|
||||
.deserialize()
|
||||
.map_err(|_| Error::bad_database("Invalid PowerLevels event in db."))
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let create_prev_event = if prev_events.len() == 1
|
||||
&& Some(&prev_events[0]) == create_event.as_ref().map(|c| &c.event_id)
|
||||
{
|
||||
create_event
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// If there was no create event yet, assume we are creating a version 6 room right now
|
||||
let room_version_id = create_event_content
|
||||
.map_or(RoomVersionId::Version6, |create_event| {
|
||||
create_event.room_version
|
||||
});
|
||||
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");
|
||||
|
||||
let content = serde_json::to_value(MemberEventContent {
|
||||
avatar_url: None,
|
||||
displayname: None,
|
||||
is_direct: Some(is_direct),
|
||||
membership: MembershipState::Invite,
|
||||
third_party_invite: None,
|
||||
})
|
||||
.expect("member event is valid value");
|
||||
|
||||
let state_key = user_id.to_string();
|
||||
let kind = EventType::RoomMember;
|
||||
|
||||
let auth_events =
|
||||
db.rooms
|
||||
.get_auth_events(room_id, &kind, &sender_user, Some(&state_key), &content)?;
|
||||
|
||||
// Our depth is the maximum depth of prev_events + 1
|
||||
let depth = prev_events
|
||||
.iter()
|
||||
.filter_map(|event_id| Some(db.rooms.get_pdu(event_id).ok()??.depth))
|
||||
.max()
|
||||
.unwrap_or_else(|| uint!(0))
|
||||
+ uint!(1);
|
||||
|
||||
let mut unsigned = BTreeMap::new();
|
||||
|
||||
if let Some(prev_pdu) = db.rooms.room_state_get(room_id, &kind, &state_key)? {
|
||||
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
||||
unsigned.insert(
|
||||
"prev_sender".to_owned(),
|
||||
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
|
||||
let (room_version_id, pdu_json, invite_room_state) = {
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
}
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let pdu = PduEvent {
|
||||
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"),
|
||||
kind,
|
||||
content,
|
||||
state_key: Some(state_key),
|
||||
prev_events,
|
||||
depth,
|
||||
auth_events: auth_events
|
||||
let prev_events = db
|
||||
.rooms
|
||||
.get_pdu_leaves(room_id)?
|
||||
.into_iter()
|
||||
.take(20)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let create_event = db
|
||||
.rooms
|
||||
.room_state_get(room_id, &EventType::RoomCreate, "")?;
|
||||
|
||||
let create_event_content = create_event
|
||||
.as_ref()
|
||||
.map(|create_event| {
|
||||
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.")
|
||||
})
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let create_prev_event = if prev_events.len() == 1
|
||||
&& Some(&prev_events[0]) == create_event.as_ref().map(|c| &c.event_id)
|
||||
{
|
||||
create_event
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// If there was no create event yet, assume we are creating a version 6 room right now
|
||||
let room_version_id = create_event_content
|
||||
.map_or(RoomVersionId::Version6, |create_event| {
|
||||
create_event.room_version
|
||||
});
|
||||
let room_version =
|
||||
RoomVersion::new(&room_version_id).expect("room version is supported");
|
||||
|
||||
let content = serde_json::to_value(MemberEventContent {
|
||||
avatar_url: None,
|
||||
displayname: None,
|
||||
is_direct: Some(is_direct),
|
||||
membership: MembershipState::Invite,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason: None,
|
||||
})
|
||||
.expect("member event is valid value");
|
||||
|
||||
let state_key = user_id.to_string();
|
||||
let kind = EventType::RoomMember;
|
||||
|
||||
let auth_events = db.rooms.get_auth_events(
|
||||
room_id,
|
||||
&kind,
|
||||
&sender_user,
|
||||
Some(&state_key),
|
||||
&content,
|
||||
)?;
|
||||
|
||||
// Our depth is the maximum depth of prev_events + 1
|
||||
let depth = prev_events
|
||||
.iter()
|
||||
.map(|(_, pdu)| pdu.event_id.clone())
|
||||
.collect(),
|
||||
redacts: None,
|
||||
unsigned,
|
||||
hashes: ruma::events::pdu::EventHash {
|
||||
sha256: "aaa".to_owned(),
|
||||
},
|
||||
signatures: BTreeMap::new(),
|
||||
.filter_map(|event_id| Some(db.rooms.get_pdu(event_id).ok()??.depth))
|
||||
.max()
|
||||
.unwrap_or_else(|| uint!(0))
|
||||
+ uint!(1);
|
||||
|
||||
let mut unsigned = BTreeMap::new();
|
||||
|
||||
if let Some(prev_pdu) = db.rooms.room_state_get(room_id, &kind, &state_key)? {
|
||||
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
||||
unsigned.insert(
|
||||
"prev_sender".to_owned(),
|
||||
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
|
||||
);
|
||||
}
|
||||
|
||||
let pdu = PduEvent {
|
||||
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"),
|
||||
kind,
|
||||
content,
|
||||
state_key: Some(state_key),
|
||||
prev_events,
|
||||
depth,
|
||||
auth_events: auth_events
|
||||
.iter()
|
||||
.map(|(_, pdu)| pdu.event_id.clone())
|
||||
.collect(),
|
||||
redacts: None,
|
||||
unsigned,
|
||||
hashes: ruma::events::pdu::EventHash {
|
||||
sha256: "aaa".to_owned(),
|
||||
},
|
||||
signatures: BTreeMap::new(),
|
||||
};
|
||||
|
||||
let auth_check = state_res::auth_check(
|
||||
&room_version,
|
||||
&Arc::new(pdu.clone()),
|
||||
create_prev_event,
|
||||
None, // TODO: third_party_invite
|
||||
|k, s| auth_events.get(&(k.clone(), s.to_owned())).map(Arc::clone),
|
||||
)
|
||||
.map_err(|e| {
|
||||
error!("{:?}", e);
|
||||
Error::bad_database("Auth check failed.")
|
||||
})?;
|
||||
|
||||
if !auth_check {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"Event is not authorized.",
|
||||
));
|
||||
}
|
||||
|
||||
// Hash and sign
|
||||
let mut pdu_json =
|
||||
utils::to_canonical_object(&pdu).expect("event is valid, we just created it");
|
||||
|
||||
pdu_json.remove("event_id");
|
||||
|
||||
// Add origin because synapse likes that (and it's required in the spec)
|
||||
pdu_json.insert(
|
||||
"origin".to_owned(),
|
||||
to_canonical_value(db.globals.server_name())
|
||||
.expect("server name is a valid CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
ruma::signatures::hash_and_sign_event(
|
||||
db.globals.server_name().as_str(),
|
||||
db.globals.keypair(),
|
||||
&mut pdu_json,
|
||||
&room_version_id,
|
||||
)
|
||||
.expect("event is valid, we just created it");
|
||||
|
||||
let invite_room_state = db.rooms.calculate_invite_state(&pdu)?;
|
||||
|
||||
drop(state_lock);
|
||||
|
||||
(room_version_id, pdu_json, invite_room_state)
|
||||
};
|
||||
|
||||
let auth_check = state_res::auth_check(
|
||||
&room_version,
|
||||
&Arc::new(pdu.clone()),
|
||||
create_prev_event,
|
||||
&auth_events,
|
||||
None, // TODO: third_party_invite
|
||||
)
|
||||
.map_err(|e| {
|
||||
error!("{:?}", e);
|
||||
Error::bad_database("Auth check failed.")
|
||||
})?;
|
||||
// Generate event id
|
||||
let expected_event_id = EventId::try_from(&*format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&pdu_json, &room_version_id)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
if !auth_check {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"Event is not authorized.",
|
||||
));
|
||||
}
|
||||
|
||||
// Hash and sign
|
||||
let mut pdu_json =
|
||||
utils::to_canonical_object(&pdu).expect("event is valid, we just created it");
|
||||
|
||||
pdu_json.remove("event_id");
|
||||
|
||||
// Add origin because synapse likes that (and it's required in the spec)
|
||||
pdu_json.insert(
|
||||
"origin".to_owned(),
|
||||
to_canonical_value(db.globals.server_name())
|
||||
.expect("server name is a valid CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
ruma::signatures::hash_and_sign_event(
|
||||
db.globals.server_name().as_str(),
|
||||
db.globals.keypair(),
|
||||
&mut pdu_json,
|
||||
&room_version_id,
|
||||
)
|
||||
.expect("event is valid, we just created it");
|
||||
|
||||
let invite_room_state = db.rooms.calculate_invite_state(&pdu)?;
|
||||
let response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
@@ -874,9 +1036,9 @@ pub async fn invite_helper(
|
||||
user_id.server_name(),
|
||||
create_invite::v2::Request {
|
||||
room_id: room_id.clone(),
|
||||
event_id: ruma::event_id!("$receivingservershouldsetthis"),
|
||||
room_version: RoomVersionId::Version6,
|
||||
event: PduEvent::convert_to_outgoing_federation_event(pdu_json),
|
||||
event_id: expected_event_id.clone(),
|
||||
room_version: room_version_id,
|
||||
event: PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
|
||||
invite_room_state,
|
||||
},
|
||||
)
|
||||
@@ -896,6 +1058,10 @@ pub async fn invite_helper(
|
||||
}
|
||||
};
|
||||
|
||||
if expected_event_id != event_id {
|
||||
warn!("Server {} changed invite event, that's not allowed in the spec: ours: {:?}, theirs: {:?}", user_id.server_name(), pdu_json, value);
|
||||
}
|
||||
|
||||
let origin = serde_json::from_value::<Box<ServerName>>(
|
||||
serde_json::to_value(value.get("origin").ok_or(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
@@ -905,19 +1071,26 @@ pub async fn invite_helper(
|
||||
)
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
|
||||
|
||||
let pdu_id =
|
||||
server_server::handle_incoming_pdu(&origin, &event_id, value, true, &db, &pub_key_map)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Error while handling incoming PDU.",
|
||||
)
|
||||
})?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Could not accept incoming PDU as timeline event.",
|
||||
))?;
|
||||
let pdu_id = server_server::handle_incoming_pdu(
|
||||
&origin,
|
||||
&event_id,
|
||||
&room_id,
|
||||
value,
|
||||
true,
|
||||
&db,
|
||||
&pub_key_map,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Error while handling incoming PDU.",
|
||||
)
|
||||
})?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Could not accept incoming PDU as timeline event.",
|
||||
))?;
|
||||
|
||||
for server in db
|
||||
.rooms
|
||||
@@ -931,6 +1104,16 @@ pub async fn invite_helper(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMember,
|
||||
@@ -940,6 +1123,8 @@ pub async fn invite_helper(
|
||||
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,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -949,7 +1134,10 @@ pub async fn invite_helper(
|
||||
&sender_user,
|
||||
room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
drop(state_lock);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use super::State;
|
||||
use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -17,18 +16,35 @@ use std::{
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}`
|
||||
///
|
||||
/// Send a message event into the room.
|
||||
///
|
||||
/// - Is a NOOP if the txn id was already used before and returns the same event id again
|
||||
/// - The only requirement for the content is that it has to be valid json
|
||||
/// - Tries to send the event into the room, auth rules will determine if it is allowed
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/rooms/<_>/send/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn send_message_event_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<send_message_event::Request<'_>>,
|
||||
) -> ConduitResult<send_message_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
let sender_device = body.sender_device.as_deref();
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
// Check if this is a new transaction id
|
||||
if let Some(response) =
|
||||
db.transaction_ids
|
||||
@@ -66,6 +82,7 @@ pub async fn send_message_event_route(
|
||||
&sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.transaction_ids.add_txnid(
|
||||
@@ -75,18 +92,26 @@ pub async fn send_message_event_route(
|
||||
event_id.as_bytes(),
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(send_message_event::Response::new(event_id).into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/messages`
|
||||
///
|
||||
/// Allows paginating through room history.
|
||||
///
|
||||
/// - Only works if the user is joined (TODO: always allow, but only show events where the user was
|
||||
/// joined, depending on history_visibility)
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/messages", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_message_events_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_message_events::Request<'_>>,
|
||||
) -> ConduitResult<get_message_events::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -116,7 +141,7 @@ pub async fn get_message_events_route(
|
||||
get_message_events::Direction::Forward => {
|
||||
let events_after = db
|
||||
.rooms
|
||||
.pdus_after(&sender_user, &body.room_id, from)
|
||||
.pdus_after(&sender_user, &body.room_id, from)?
|
||||
.take(limit)
|
||||
.filter_map(|r| r.ok()) // Filter out buggy events
|
||||
.filter_map(|(pdu_id, pdu)| {
|
||||
@@ -146,7 +171,7 @@ pub async fn get_message_events_route(
|
||||
get_message_events::Direction::Backward => {
|
||||
let events_before = db
|
||||
.rooms
|
||||
.pdus_until(&sender_user, &body.room_id, from)
|
||||
.pdus_until(&sender_user, &body.room_id, from)?
|
||||
.take(limit)
|
||||
.filter_map(|r| r.ok()) // Filter out buggy events
|
||||
.filter_map(|(pdu_id, pdu)| {
|
||||
|
||||
@@ -64,18 +64,19 @@ pub use voip::*;
|
||||
use super::State;
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::ConduitResult,
|
||||
rocket::{options, State},
|
||||
ruma::api::client::r0::to_device::send_event_to_device,
|
||||
crate::ConduitResult, rocket::options, ruma::api::client::r0::to_device::send_event_to_device,
|
||||
};
|
||||
|
||||
pub const DEVICE_ID_LENGTH: usize = 10;
|
||||
pub const TOKEN_LENGTH: usize = 256;
|
||||
pub const SESSION_ID_LENGTH: usize = 256;
|
||||
|
||||
/// # `OPTIONS`
|
||||
///
|
||||
/// Web clients use this to get CORS headers.
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
#[options("/<_..>")]
|
||||
#[tracing::instrument]
|
||||
pub async fn options_route() -> ConduitResult<send_event_to_device::Response> {
|
||||
Ok(send_event_to_device::Response.into())
|
||||
Ok(send_event_to_device::Response {}.into())
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
use super::State;
|
||||
use crate::{utils, ConduitResult, Database, Ruma};
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Ruma};
|
||||
use ruma::api::client::r0::presence::{get_presence, set_presence};
|
||||
use std::{convert::TryInto, sync::Arc, time::Duration};
|
||||
use std::{convert::TryInto, time::Duration};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/presence/{userId}/status`
|
||||
///
|
||||
/// Sets the presence state of the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/presence/<_>/status", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_presence_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_presence::Request<'_>>,
|
||||
) -> ConduitResult<set_presence::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -42,18 +44,23 @@ pub async fn set_presence_route(
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_presence::Response.into())
|
||||
Ok(set_presence::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/presence/{userId}/status`
|
||||
///
|
||||
/// Gets the presence state of the given user.
|
||||
///
|
||||
/// - Only works if you share a room with the user
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/presence/<_>/status", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_presence_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_presence::Request<'_>>,
|
||||
) -> ConduitResult<get_presence::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -72,6 +79,7 @@ pub async fn get_presence_route(
|
||||
.get_last_presence_event(&sender_user, &room_id)?
|
||||
{
|
||||
presence_event = Some(presence);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+143
-28
@@ -1,27 +1,34 @@
|
||||
use super::State;
|
||||
use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::profile::{
|
||||
get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
|
||||
api::{
|
||||
client::{
|
||||
error::ErrorKind,
|
||||
r0::profile::{
|
||||
get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
|
||||
},
|
||||
},
|
||||
federation::{self, query::get_profile_information::v1::ProfileField},
|
||||
},
|
||||
events::EventType,
|
||||
serde::Raw,
|
||||
};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/profile/{userId}/displayname`
|
||||
///
|
||||
/// Updates the displayname.
|
||||
///
|
||||
/// - Also makes sure other users receive the update using presence EDUs
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/profile/<_>/displayname", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_displayname_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_display_name::Request<'_>>,
|
||||
) -> ConduitResult<set_display_name::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -30,7 +37,7 @@ pub async fn set_displayname_route(
|
||||
.set_displayname(&sender_user, body.displayname.clone())?;
|
||||
|
||||
// Send a new membership event and presence update into all joined rooms
|
||||
for (pdu_builder, room_id) in db
|
||||
let all_rooms_joined: Vec<_> = db
|
||||
.rooms
|
||||
.rooms_joined(&sender_user)
|
||||
.filter_map(|r| r.ok())
|
||||
@@ -50,7 +57,7 @@ pub async fn set_displayname_route(
|
||||
.ok_or_else(|| {
|
||||
Error::bad_database(
|
||||
"Tried to send displayname update for user not in the \
|
||||
room.",
|
||||
room.",
|
||||
)
|
||||
})?
|
||||
.content
|
||||
@@ -69,10 +76,22 @@ pub async fn set_displayname_route(
|
||||
))
|
||||
})
|
||||
.filter_map(|r| r.ok())
|
||||
{
|
||||
let _ = db
|
||||
.rooms
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db);
|
||||
.collect();
|
||||
|
||||
for (pdu_builder, room_id) in all_rooms_joined {
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let _ =
|
||||
db.rooms
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db, &state_lock);
|
||||
|
||||
// Presence update
|
||||
db.rooms.edus.update_presence(
|
||||
@@ -97,33 +116,62 @@ pub async fn set_displayname_route(
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_display_name::Response.into())
|
||||
Ok(set_display_name::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/profile/{userId}/displayname`
|
||||
///
|
||||
/// Returns the displayname of the user.
|
||||
///
|
||||
/// - If user is on another server: Fetches displayname over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/profile/<_>/displayname", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_displayname_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_display_name::Request<'_>>,
|
||||
) -> ConduitResult<get_display_name::Response> {
|
||||
if body.user_id.server_name() != db.globals.server_name() {
|
||||
let response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
body.user_id.server_name(),
|
||||
federation::query::get_profile_information::v1::Request {
|
||||
user_id: &body.user_id,
|
||||
field: Some(&ProfileField::DisplayName),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(get_display_name::Response {
|
||||
displayname: response.displayname,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
|
||||
Ok(get_display_name::Response {
|
||||
displayname: db.users.displayname(&body.user_id)?,
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/profile/{userId}/avatar_url`
|
||||
///
|
||||
/// Updates the avatar_url and blurhash.
|
||||
///
|
||||
/// - Also makes sure other users receive the update using presence EDUs
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/profile/<_>/avatar_url", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_avatar_url_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_avatar_url::Request<'_>>,
|
||||
) -> ConduitResult<set_avatar_url::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -131,8 +179,10 @@ pub async fn set_avatar_url_route(
|
||||
db.users
|
||||
.set_avatar_url(&sender_user, body.avatar_url.clone())?;
|
||||
|
||||
db.users.set_blurhash(&sender_user, body.blurhash.clone())?;
|
||||
|
||||
// Send a new membership event and presence update into all joined rooms
|
||||
for (pdu_builder, room_id) in db
|
||||
let all_joined_rooms: Vec<_> = db
|
||||
.rooms
|
||||
.rooms_joined(&sender_user)
|
||||
.filter_map(|r| r.ok())
|
||||
@@ -152,7 +202,7 @@ pub async fn set_avatar_url_route(
|
||||
.ok_or_else(|| {
|
||||
Error::bad_database(
|
||||
"Tried to send displayname update for user not in the \
|
||||
room.",
|
||||
room.",
|
||||
)
|
||||
})?
|
||||
.content
|
||||
@@ -171,10 +221,22 @@ pub async fn set_avatar_url_route(
|
||||
))
|
||||
})
|
||||
.filter_map(|r| r.ok())
|
||||
{
|
||||
let _ = db
|
||||
.rooms
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db);
|
||||
.collect();
|
||||
|
||||
for (pdu_builder, room_id) in all_joined_rooms {
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let _ =
|
||||
db.rooms
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db, &state_lock);
|
||||
|
||||
// Presence update
|
||||
db.rooms.edus.update_presence(
|
||||
@@ -199,35 +261,87 @@ pub async fn set_avatar_url_route(
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_avatar_url::Response.into())
|
||||
Ok(set_avatar_url::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/profile/{userId}/avatar_url`
|
||||
///
|
||||
/// Returns the avatar_url and blurhash of the user.
|
||||
///
|
||||
/// - If user is on another server: Fetches avatar_url and blurhash over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/profile/<_>/avatar_url", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_avatar_url_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_avatar_url::Request<'_>>,
|
||||
) -> ConduitResult<get_avatar_url::Response> {
|
||||
if body.user_id.server_name() != db.globals.server_name() {
|
||||
let response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
body.user_id.server_name(),
|
||||
federation::query::get_profile_information::v1::Request {
|
||||
user_id: &body.user_id,
|
||||
field: Some(&ProfileField::AvatarUrl),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(get_avatar_url::Response {
|
||||
avatar_url: response.avatar_url,
|
||||
blurhash: response.blurhash,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
|
||||
Ok(get_avatar_url::Response {
|
||||
avatar_url: db.users.avatar_url(&body.user_id)?,
|
||||
blurhash: db.users.blurhash(&body.user_id)?,
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/profile/{userId}`
|
||||
///
|
||||
/// Returns the displayname, avatar_url and blurhash of the user.
|
||||
///
|
||||
/// - If user is on another server: Fetches profile over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/profile/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_profile_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_profile::Request<'_>>,
|
||||
) -> ConduitResult<get_profile::Response> {
|
||||
if body.user_id.server_name() != db.globals.server_name() {
|
||||
let response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
body.user_id.server_name(),
|
||||
federation::query::get_profile_information::v1::Request {
|
||||
user_id: &body.user_id,
|
||||
field: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(get_profile::Response {
|
||||
displayname: response.displayname,
|
||||
avatar_url: response.avatar_url,
|
||||
blurhash: response.blurhash,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
|
||||
if !db.users.exists(&body.user_id)? {
|
||||
// Return 404 if this user doesn't exist
|
||||
return Err(Error::BadRequest(
|
||||
@@ -238,6 +352,7 @@ pub async fn get_profile_route(
|
||||
|
||||
Ok(get_profile::Response {
|
||||
avatar_url: db.users.avatar_url(&body.user_id)?,
|
||||
blurhash: db.users.blurhash(&body.user_id)?,
|
||||
displayname: db.users.displayname(&body.user_id)?,
|
||||
}
|
||||
.into())
|
||||
|
||||
+61
-32
@@ -1,7 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -18,13 +15,16 @@ use ruma::{
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{delete, get, post, put};
|
||||
|
||||
/// # `GET /_matrix/client/r0/pushrules`
|
||||
///
|
||||
/// Retrieves the push rules event for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/pushrules", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_pushrules_all_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_pushrules_all::Request>,
|
||||
) -> ConduitResult<get_pushrules_all::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -43,13 +43,16 @@ pub async fn get_pushrules_all_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}`
|
||||
///
|
||||
/// Retrieves a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_pushrule_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_pushrule::Request<'_>>,
|
||||
) -> ConduitResult<get_pushrule::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -84,7 +87,7 @@ pub async fn get_pushrule_route(
|
||||
.content
|
||||
.get(body.rule_id.as_str())
|
||||
.map(|rule| rule.clone().into()),
|
||||
RuleKind::_Custom(_) => None,
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(rule) = rule {
|
||||
@@ -97,13 +100,16 @@ pub async fn get_pushrule_route(
|
||||
}
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}`
|
||||
///
|
||||
/// Creates a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<req>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, req))]
|
||||
pub async fn set_pushrule_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
req: Ruma<set_pushrule::Request<'_>>,
|
||||
) -> ConduitResult<set_pushrule::Response> {
|
||||
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -184,7 +190,7 @@ pub async fn set_pushrule_route(
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
RuleKind::_Custom(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
db.account_data.update(
|
||||
@@ -195,18 +201,21 @@ pub async fn set_pushrule_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_pushrule::Response.into())
|
||||
Ok(set_pushrule::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/actions`
|
||||
///
|
||||
/// Gets the actions of a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>/actions", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_pushrule_actions_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_pushrule_actions::Request<'_>>,
|
||||
) -> ConduitResult<get_pushrule_actions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -248,10 +257,10 @@ pub async fn get_pushrule_actions_route(
|
||||
.content
|
||||
.get(body.rule_id.as_str())
|
||||
.map(|rule| rule.actions.clone()),
|
||||
RuleKind::_Custom(_) => None,
|
||||
_ => None,
|
||||
};
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(get_pushrule_actions::Response {
|
||||
actions: actions.unwrap_or_default(),
|
||||
@@ -259,13 +268,16 @@ pub async fn get_pushrule_actions_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/actions`
|
||||
///
|
||||
/// Sets the actions of a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/actions", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_pushrule_actions_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_pushrule_actions::Request<'_>>,
|
||||
) -> ConduitResult<set_pushrule_actions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -317,7 +329,7 @@ pub async fn set_pushrule_actions_route(
|
||||
global.content.replace(rule);
|
||||
}
|
||||
}
|
||||
RuleKind::_Custom(_) => {}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
db.account_data.update(
|
||||
@@ -328,18 +340,21 @@ pub async fn set_pushrule_actions_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_pushrule_actions::Response.into())
|
||||
Ok(set_pushrule_actions::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/enabled`
|
||||
///
|
||||
/// Gets the enabled status of a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_pushrule_enabled_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_pushrule_enabled::Request<'_>>,
|
||||
) -> ConduitResult<get_pushrule_enabled::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -386,21 +401,24 @@ pub async fn get_pushrule_enabled_route(
|
||||
.iter()
|
||||
.find(|rule| rule.rule_id == body.rule_id)
|
||||
.map_or(false, |rule| rule.enabled),
|
||||
RuleKind::_Custom(_) => false,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(get_pushrule_enabled::Response { enabled }.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/enabled`
|
||||
///
|
||||
/// Sets the enabled status of a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_pushrule_enabled_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_pushrule_enabled::Request<'_>>,
|
||||
) -> ConduitResult<set_pushrule_enabled::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -457,7 +475,7 @@ pub async fn set_pushrule_enabled_route(
|
||||
global.content.insert(rule);
|
||||
}
|
||||
}
|
||||
RuleKind::_Custom(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
db.account_data.update(
|
||||
@@ -468,18 +486,21 @@ pub async fn set_pushrule_enabled_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_pushrule_enabled::Response.into())
|
||||
Ok(set_pushrule_enabled::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}`
|
||||
///
|
||||
/// Deletes a single specified push rule for this user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_pushrule_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_pushrule::Request<'_>>,
|
||||
) -> ConduitResult<delete_pushrule::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -526,7 +547,7 @@ pub async fn delete_pushrule_route(
|
||||
global.content.remove(&rule);
|
||||
}
|
||||
}
|
||||
RuleKind::_Custom(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
db.account_data.update(
|
||||
@@ -537,18 +558,21 @@ pub async fn delete_pushrule_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_pushrule::Response.into())
|
||||
Ok(delete_pushrule::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/pushers`
|
||||
///
|
||||
/// Gets all currently active pushers for the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/pushers", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_pushers_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_pushers::Request>,
|
||||
) -> ConduitResult<get_pushers::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -559,13 +583,18 @@ pub async fn get_pushers_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/pushers/set`
|
||||
///
|
||||
/// Adds a pusher for the sender user.
|
||||
///
|
||||
/// - TODO: Handle `append`
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/pushers/set", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_pushers_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_pusher::Request>,
|
||||
) -> ConduitResult<set_pusher::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -573,7 +602,7 @@ pub async fn set_pushers_route(
|
||||
|
||||
db.pusher.set_pusher(sender_user, pusher)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_pusher::Response::default().into())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -9,18 +8,24 @@ use ruma::{
|
||||
receipt::ReceiptType,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::post;
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/read_markers`
|
||||
///
|
||||
/// Sets different types of read markers.
|
||||
///
|
||||
/// - Updates fully-read account data event to `fully_read`
|
||||
/// - If `read_receipt` is set: Update private marker and public read receipt EDU
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/read_markers", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn set_read_marker_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<set_read_marker::Request<'_>>,
|
||||
) -> ConduitResult<set_read_marker::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -76,18 +81,21 @@ pub async fn set_read_marker_route(
|
||||
)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(set_read_marker::Response.into())
|
||||
Ok(set_read_marker::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}`
|
||||
///
|
||||
/// Sets private read marker and public read receipt EDU.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/receipt/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn create_receipt_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_receipt::Request<'_>>,
|
||||
) -> ConduitResult<create_receipt::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -129,7 +137,7 @@ pub async fn create_receipt_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_receipt::Response.into())
|
||||
Ok(create_receipt::Response {}.into())
|
||||
}
|
||||
|
||||
@@ -1,25 +1,40 @@
|
||||
use super::State;
|
||||
use crate::{pdu::PduBuilder, ConduitResult, Database, Ruma};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma};
|
||||
use ruma::{
|
||||
api::client::r0::redact::redact_event,
|
||||
events::{room::redaction, EventType},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::put;
|
||||
|
||||
/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
|
||||
///
|
||||
/// Tries to send a redaction event into the room.
|
||||
///
|
||||
/// - TODO: Handle txn id
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/rooms/<_>/redact/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn redact_event_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<redact_event::Request<'_>>,
|
||||
) -> ConduitResult<redact_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let event_id = db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomRedaction,
|
||||
@@ -34,9 +49,12 @@ pub async fn redact_event_route(
|
||||
&sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
Ok(redact_event::Response { event_id }.into())
|
||||
}
|
||||
|
||||
+204
-49
@@ -1,10 +1,11 @@
|
||||
use super::State;
|
||||
use crate::{client_server::invite_helper, pdu::PduBuilder, ConduitResult, Database, Error, Ruma};
|
||||
use log::info;
|
||||
use crate::{
|
||||
client_server::invite_helper, database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Error,
|
||||
Ruma,
|
||||
};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::room::{self, create_room, get_room_event, upgrade_room},
|
||||
r0::room::{self, aliases, create_room, get_room_event, upgrade_room},
|
||||
},
|
||||
events::{
|
||||
room::{guest_access, history_visibility, join_rules, member, name, topic},
|
||||
@@ -14,46 +15,87 @@ use ruma::{
|
||||
RoomAliasId, RoomId, RoomVersionId,
|
||||
};
|
||||
use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc};
|
||||
use tracing::{info, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
||||
/// # `POST /_matrix/client/r0/createRoom`
|
||||
///
|
||||
/// Creates a new room.
|
||||
///
|
||||
/// - Room ID is randomly generated
|
||||
/// - Create alias if room_alias_name is set
|
||||
/// - Send create event
|
||||
/// - Join sender user
|
||||
/// - Send power levels event
|
||||
/// - Send canonical room alias
|
||||
/// - Send join rules
|
||||
/// - Send history visibility
|
||||
/// - Send guest access
|
||||
/// - Send events listed in initial state
|
||||
/// - Send events implied by `name` and `topic`
|
||||
/// - Send invite events
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/createRoom", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn create_room_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_room::Request<'_>>,
|
||||
) -> ConduitResult<create_room::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let room_id = RoomId::new(db.globals.server_name());
|
||||
|
||||
let alias = body
|
||||
.room_alias_name
|
||||
.as_ref()
|
||||
.map_or(Ok(None), |localpart| {
|
||||
// TODO: Check for invalid characters and maximum length
|
||||
let alias =
|
||||
RoomAliasId::try_from(format!("#{}:{}", localpart, db.globals.server_name()))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
|
||||
db.rooms.get_or_create_shortroomid(&room_id, &db.globals)?;
|
||||
|
||||
if db.rooms.id_from_alias(&alias)?.is_some() {
|
||||
Err(Error::BadRequest(
|
||||
ErrorKind::RoomInUse,
|
||||
"Room alias already exists.",
|
||||
))
|
||||
} else {
|
||||
Ok(Some(alias))
|
||||
}
|
||||
})?;
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
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::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(
|
||||
ErrorKind::RoomInUse,
|
||||
"Room alias already exists.",
|
||||
))
|
||||
} else {
|
||||
Ok(Some(alias))
|
||||
}
|
||||
})?;
|
||||
|
||||
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 = RoomVersionId::Version6;
|
||||
content.room_version = match body.room_version.clone() {
|
||||
Some(room_version) => {
|
||||
if room_version == RoomVersionId::Version5 || room_version == RoomVersionId::Version6 {
|
||||
room_version
|
||||
} else {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::UnsupportedRoomVersion,
|
||||
"This server does not support that room version.",
|
||||
));
|
||||
}
|
||||
}
|
||||
None => RoomVersionId::Version6,
|
||||
};
|
||||
|
||||
// 1. The room create event
|
||||
db.rooms.build_and_append_pdu(
|
||||
@@ -67,6 +109,7 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 2. Let the room creator join
|
||||
@@ -79,6 +122,8 @@ pub async fn create_room_route(
|
||||
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,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -88,6 +133,7 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 3. Power levels
|
||||
@@ -99,7 +145,7 @@ pub async fn create_room_route(
|
||||
.unwrap_or_else(|| match &body.visibility {
|
||||
room::Visibility::Private => create_room::RoomPreset::PrivateChat,
|
||||
room::Visibility::Public => create_room::RoomPreset::PublicChat,
|
||||
room::Visibility::_Custom(_) => create_room::RoomPreset::PrivateChat, // Room visibility should not be custom
|
||||
_ => create_room::RoomPreset::PrivateChat, // Room visibility should not be custom
|
||||
});
|
||||
|
||||
let mut users = BTreeMap::new();
|
||||
@@ -142,11 +188,35 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4. Events set by preset
|
||||
// 4. Canonical room alias
|
||||
if let Some(room_alias_id) = &alias {
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomCanonicalAlias,
|
||||
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()),
|
||||
redacts: None,
|
||||
},
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
// 4.1 Join Rules
|
||||
// 5. Events set by preset
|
||||
|
||||
// 5.1 Join Rules
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomJoinRules,
|
||||
@@ -168,9 +238,10 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4.2 History Visibility
|
||||
// 5.2 History Visibility
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomHistoryVisibility,
|
||||
@@ -185,9 +256,10 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 4.3 Guest Access
|
||||
// 5.3 Guest Access
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomGuestAccess,
|
||||
@@ -210,14 +282,15 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// 5. Events listed in initial_state
|
||||
// 6. Events listed in initial_state
|
||||
for event in &body.initial_state {
|
||||
let pdu_builder = serde_json::from_str::<PduBuilder>(
|
||||
&serde_json::to_string(&event).expect("AnyInitialStateEvent::to_string always works"),
|
||||
)
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid initial state event."))?;
|
||||
let pdu_builder = PduBuilder::from(event.deserialize().map_err(|e| {
|
||||
warn!("Invalid initial state event: {:?}", e);
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Invalid initial state event.")
|
||||
})?);
|
||||
|
||||
// Silently skip encryption events if they are not allowed
|
||||
if pdu_builder.event_type == EventType::RoomEncryption && !db.globals.allow_encryption() {
|
||||
@@ -225,20 +298,16 @@ pub async fn create_room_route(
|
||||
}
|
||||
|
||||
db.rooms
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db)?;
|
||||
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db, &state_lock)?;
|
||||
}
|
||||
|
||||
// 6. Events implied by name and topic
|
||||
// 7. Events implied by name and topic
|
||||
if let Some(name) = &body.name {
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomName,
|
||||
content: serde_json::to_value(
|
||||
name::NameEventContent::new(name.clone()).map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Name is invalid.")
|
||||
})?,
|
||||
)
|
||||
.expect("event is valid, we just created it"),
|
||||
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()),
|
||||
redacts: None,
|
||||
@@ -246,6 +315,7 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -264,10 +334,12 @@ pub async fn create_room_route(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
// 7. Events implied by invite (and TODO: invite_3pid)
|
||||
// 8. Events implied by invite (and TODO: invite_3pid)
|
||||
drop(state_lock);
|
||||
for user_id in &body.invite {
|
||||
let _ = invite_helper(sender_user, user_id, &room_id, &db, body.is_direct).await;
|
||||
}
|
||||
@@ -283,18 +355,23 @@ pub async fn create_room_route(
|
||||
|
||||
info!("{} created a room", sender_user);
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_room::Response::new(room_id).into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/event/{eventId}`
|
||||
///
|
||||
/// Gets a single event.
|
||||
///
|
||||
/// - You have to currently be joined to the room (TODO: Respect history visibility)
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/event/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_room_event_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_room_event::Request<'_>>,
|
||||
) -> ConduitResult<get_room_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -316,19 +393,64 @@ pub async fn get_room_event_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/aliases`
|
||||
///
|
||||
/// Lists all aliases of the room.
|
||||
///
|
||||
/// - Only users joined to the room are allowed to call this TODO: Allow any user to call it if history_visibility is world readable
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_room_id>/upgrade", data = "<body>")
|
||||
get("/_matrix/client/r0/rooms/<_>/aliases", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_room_aliases_route(
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<aliases::Request<'_>>,
|
||||
) -> ConduitResult<aliases::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view this room.",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(aliases::Response {
|
||||
aliases: db
|
||||
.rooms
|
||||
.room_aliases(&body.room_id)
|
||||
.filter_map(|a| a.ok())
|
||||
.collect(),
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/upgrade`
|
||||
///
|
||||
/// Upgrades the room.
|
||||
///
|
||||
/// - Creates a replacement room
|
||||
/// - Sends a tombstone event into the current room
|
||||
/// - Sender user joins the room
|
||||
/// - Transfers some state events
|
||||
/// - Moves local aliases
|
||||
/// - Modifies old room power levels to prevent users from speaking
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/rooms/<_>/upgrade", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn upgrade_room_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<upgrade_room::Request<'_>>,
|
||||
_room_id: String,
|
||||
) -> ConduitResult<upgrade_room::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if !matches!(body.new_version, RoomVersionId::Version6) {
|
||||
if !matches!(
|
||||
body.new_version,
|
||||
RoomVersionId::Version5 | RoomVersionId::Version6
|
||||
) {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::UnsupportedRoomVersion,
|
||||
"This server does not support that room version.",
|
||||
@@ -337,6 +459,18 @@ pub async fn upgrade_room_route(
|
||||
|
||||
// Create a replacement room
|
||||
let replacement_room = RoomId::new(db.globals.server_name());
|
||||
db.rooms
|
||||
.get_or_create_shortroomid(&replacement_room, &db.globals)?;
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(body.room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
// Send a m.room.tombstone event to the old room to indicate that it is not intended to be used any further
|
||||
// Fail if the sender does not have the required permissions
|
||||
@@ -355,8 +489,21 @@ pub async fn upgrade_room_route(
|
||||
sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// Change lock to replacement room
|
||||
drop(state_lock);
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(replacement_room.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
// Get the old room federations status
|
||||
let federate = serde_json::from_value::<Raw<ruma::events::room::create::CreateEventContent>>(
|
||||
db.rooms
|
||||
@@ -395,6 +542,7 @@ pub async fn upgrade_room_route(
|
||||
sender_user,
|
||||
&replacement_room,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// Join the new room
|
||||
@@ -407,6 +555,8 @@ pub async fn upgrade_room_route(
|
||||
avatar_url: db.users.avatar_url(&sender_user)?,
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&sender_user)?,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
@@ -416,6 +566,7 @@ pub async fn upgrade_room_route(
|
||||
sender_user,
|
||||
&replacement_room,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
// Recommended transferable state events list from the specs
|
||||
@@ -449,6 +600,7 @@ pub async fn upgrade_room_route(
|
||||
sender_user,
|
||||
&replacement_room,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -492,9 +644,12 @@ pub async fn upgrade_room_route(
|
||||
sender_user,
|
||||
&body.room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
drop(state_lock);
|
||||
|
||||
db.flush()?;
|
||||
|
||||
// Return the replacement room id
|
||||
Ok(upgrade_room::Response { replacement_room }.into())
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::api::client::{error::ErrorKind, r0::search::search_events};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::post;
|
||||
use search_events::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// # `POST /_matrix/client/r0/search`
|
||||
///
|
||||
/// Searches rooms for messages.
|
||||
///
|
||||
/// - Only works if the user is currently joined to the room (TODO: Respect history visibility)
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/search", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn search_events_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<search_events::Request<'_>>,
|
||||
) -> ConduitResult<search_events::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::{State, DEVICE_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{utils, ConduitResult, Database, Error, Ruma};
|
||||
use log::info;
|
||||
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::session::{get_login_types, login, logout, logout_all},
|
||||
r0::{
|
||||
session::{get_login_types, login, logout, logout_all},
|
||||
uiaa::IncomingUserIdentifier,
|
||||
},
|
||||
},
|
||||
UserId,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Claims {
|
||||
@@ -23,7 +24,7 @@ use rocket::{get, post};
|
||||
|
||||
/// # `GET /_matrix/client/r0/login`
|
||||
///
|
||||
/// Get the homeserver's supported login types. One of these should be used as the `type` field
|
||||
/// Get the supported login types of this server. One of these should be used as the `type` field
|
||||
/// when logging in.
|
||||
#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/login"))]
|
||||
#[tracing::instrument]
|
||||
@@ -40,9 +41,10 @@ pub async fn get_login_types_route() -> ConduitResult<get_login_types::Response>
|
||||
///
|
||||
/// Authenticates the user and returns an access token it can use in subsequent requests.
|
||||
///
|
||||
/// - The returned access token is associated with the user and device
|
||||
/// - Old access tokens of that device should be invalidated
|
||||
/// - If `device_id` is unknown, a new device will be created
|
||||
/// - The user needs to authenticate using their password (or if enabled using a json web token)
|
||||
/// - If `device_id` is known: invalidates old access token of that device
|
||||
/// - If `device_id` is unknown: creates a new device
|
||||
/// - Returns access token that is associated with the user and device
|
||||
///
|
||||
/// Note: You can use [`GET /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see
|
||||
/// supported login types.
|
||||
@@ -52,7 +54,7 @@ pub async fn get_login_types_route() -> ConduitResult<get_login_types::Response>
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn login_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<login::Request<'_>>,
|
||||
) -> ConduitResult<login::Response> {
|
||||
// Validate login method
|
||||
@@ -62,7 +64,7 @@ pub async fn login_route(
|
||||
identifier,
|
||||
password,
|
||||
} => {
|
||||
let username = if let login::IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
|
||||
let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
|
||||
matrix_id
|
||||
} else {
|
||||
return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type."));
|
||||
@@ -145,7 +147,7 @@ pub async fn login_route(
|
||||
|
||||
info!("{} logged in", user_id);
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(login::Response {
|
||||
user_id,
|
||||
@@ -161,15 +163,17 @@ pub async fn login_route(
|
||||
///
|
||||
/// Log out the current device.
|
||||
///
|
||||
/// - Invalidates the access token
|
||||
/// - Deletes the device and most of it's data (to-device events, last seen, etc.)
|
||||
/// - Invalidates access token
|
||||
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets to-device events
|
||||
/// - Triggers device list updates
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/logout", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn logout_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<logout::Request>,
|
||||
) -> ConduitResult<logout::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -177,7 +181,7 @@ pub async fn logout_route(
|
||||
|
||||
db.users.remove_device(&sender_user, sender_device)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(logout::Response::new().into())
|
||||
}
|
||||
@@ -187,7 +191,9 @@ pub async fn logout_route(
|
||||
/// Log out all devices of this user.
|
||||
///
|
||||
/// - Invalidates all access tokens
|
||||
/// - Deletes devices and most of their data (to-device events, last seen, etc.)
|
||||
/// - Deletes all device metadata (device id, device display name, last seen ip, last seen ts)
|
||||
/// - Forgets all to-device events
|
||||
/// - Triggers device list updates
|
||||
///
|
||||
/// Note: This is equivalent to calling [`GET /_matrix/client/r0/logout`](fn.logout_route.html)
|
||||
/// from each device of this user.
|
||||
@@ -197,7 +203,7 @@ pub async fn logout_route(
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn logout_all_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<logout_all::Request>,
|
||||
) -> ConduitResult<logout_all::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -206,7 +212,7 @@ pub async fn logout_all_route(
|
||||
db.users.remove_device(&sender_user, &device_id)?;
|
||||
}
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(logout_all::Response::new().into())
|
||||
}
|
||||
|
||||
+53
-10
@@ -1,7 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma};
|
||||
use crate::{
|
||||
database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma,
|
||||
};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
@@ -21,13 +22,20 @@ use ruma::{
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}`
|
||||
///
|
||||
/// Sends a state event into the room.
|
||||
///
|
||||
/// - The only requirement for the content is that it has to be valid json
|
||||
/// - Tries to send the event into the room, auth rules will determine if it is allowed
|
||||
/// - If event is new canonical_alias: Rejects if alias is incorrect
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn send_state_event_for_key_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<send_state_event::Request<'_>>,
|
||||
) -> ConduitResult<send_state_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -42,18 +50,25 @@ pub async fn send_state_event_for_key_route(
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(send_state_event::Response { event_id }.into())
|
||||
}
|
||||
|
||||
/// # `PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}`
|
||||
///
|
||||
/// Sends a state event into the room.
|
||||
///
|
||||
/// - The only requirement for the content is that it has to be valid json
|
||||
/// - Tries to send the event into the room, auth rules will determine if it is allowed
|
||||
/// - If event is new canonical_alias: Rejects if alias is incorrect
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn send_state_event_for_empty_key_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<send_state_event::Request<'_>>,
|
||||
) -> ConduitResult<send_state_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -68,18 +83,23 @@ pub async fn send_state_event_for_empty_key_route(
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(send_state_event::Response { event_id }.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomid}/state`
|
||||
///
|
||||
/// Get all state events for a room.
|
||||
///
|
||||
/// - If not joined: Only works if current room history visibility is world readable
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/state", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_state_events_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_state_events::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -120,13 +140,18 @@ pub async fn get_state_events_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}/{stateKey}`
|
||||
///
|
||||
/// Get single state event of a room.
|
||||
///
|
||||
/// - If not joined: Only works if current room history visibility is world readable
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_state_events_for_key_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_state_events_for_key::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events_for_key::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -171,13 +196,18 @@ pub async fn get_state_events_for_key_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}`
|
||||
///
|
||||
/// Get single state event of a room.
|
||||
///
|
||||
/// - If not joined: Only works if current room history visibility is world readable
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_state_events_for_empty_key_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_state_events_for_key::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events_for_key::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -222,7 +252,7 @@ pub async fn get_state_events_for_empty_key_route(
|
||||
.into())
|
||||
}
|
||||
|
||||
pub async fn send_state_event_for_key_helper(
|
||||
async fn send_state_event_for_key_helper(
|
||||
db: &Database,
|
||||
sender: &UserId,
|
||||
room_id: &RoomId,
|
||||
@@ -232,6 +262,8 @@ pub async fn send_state_event_for_key_helper(
|
||||
) -> 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::<CanonicalAliasEventContent>(json.json().get())
|
||||
{
|
||||
@@ -258,6 +290,16 @@ pub async fn send_state_event_for_key_helper(
|
||||
}
|
||||
}
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
let event_id = db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type,
|
||||
@@ -269,6 +311,7 @@ pub async fn send_state_event_for_key_helper(
|
||||
&sender_user,
|
||||
&room_id,
|
||||
&db,
|
||||
&state_lock,
|
||||
)?;
|
||||
|
||||
Ok(event_id)
|
||||
|
||||
+273
-234
@@ -1,6 +1,4 @@
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Result, Ruma, RumaResponse};
|
||||
use log::error;
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
|
||||
use ruma::{
|
||||
api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
|
||||
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType},
|
||||
@@ -8,12 +6,13 @@ use ruma::{
|
||||
DeviceId, RoomId, UserId,
|
||||
};
|
||||
use std::{
|
||||
collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet},
|
||||
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::sync::watch::Sender;
|
||||
use tracing::error;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, tokio};
|
||||
@@ -23,25 +22,48 @@ use rocket::{get, tokio};
|
||||
/// Synchronize the client's state with the latest state on the server.
|
||||
///
|
||||
/// - This endpoint takes a `since` parameter which should be the `next_batch` value from a
|
||||
/// previous request.
|
||||
/// - Calling this endpoint without a `since` parameter will return all recent events, the state
|
||||
/// of all rooms and more data. This should only be called on the initial login of the device.
|
||||
/// - To get incremental updates, you can call this endpoint with a `since` parameter. This will
|
||||
/// return all recent events, state updates and more data that happened since the last /sync
|
||||
/// request.
|
||||
/// previous request for incremental syncs.
|
||||
///
|
||||
/// Calling this endpoint without a `since` parameter returns:
|
||||
/// - Some of the most recent events of each timeline
|
||||
/// - Notification counts for each room
|
||||
/// - Joined and invited member counts, heroes
|
||||
/// - All state events
|
||||
///
|
||||
/// Calling this endpoint with a `since` parameter from a previous `next_batch` returns:
|
||||
/// For joined rooms:
|
||||
/// - Some of the most recent events of each timeline that happened after since
|
||||
/// - If user joined the room after since: All state events and device list updates in that room
|
||||
/// - If the user was already in the room: A list of all events that are in the state now, but were
|
||||
/// not in the state at `since`
|
||||
/// - If the state we send contains a member event: Joined and invited member counts, heroes
|
||||
/// - Device list updates that happened after `since`
|
||||
/// - If there are events in the timeline we send or the user send updated his read mark: Notification counts
|
||||
/// - EDUs that are active now (read receipts, typing updates, presence)
|
||||
///
|
||||
/// For invited rooms:
|
||||
/// - If the user was invited after `since`: A subset of the state of the room at the point of the invite
|
||||
///
|
||||
/// For left rooms:
|
||||
/// - If the user left after `since`: prev_batch token, empty state (TODO: subset of the state at the point of the leave)
|
||||
///
|
||||
/// - Sync is handled in an async task, multiple requests from the same device with the same
|
||||
/// `since` will be cached
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/sync", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn sync_events_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<sync_events::Request<'_>>,
|
||||
) -> 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 mut rx = match db
|
||||
let arc_db = Arc::new(db);
|
||||
|
||||
let mut rx = match arc_db
|
||||
.globals
|
||||
.sync_receivers
|
||||
.write()
|
||||
@@ -52,7 +74,7 @@ pub async fn sync_events_route(
|
||||
let (tx, rx) = tokio::sync::watch::channel(None);
|
||||
|
||||
tokio::spawn(sync_helper_wrapper(
|
||||
Arc::clone(&db),
|
||||
Arc::clone(&arc_db),
|
||||
sender_user.clone(),
|
||||
sender_device.clone(),
|
||||
body.since.clone(),
|
||||
@@ -68,7 +90,7 @@ pub async fn sync_events_route(
|
||||
let (tx, rx) = tokio::sync::watch::channel(None);
|
||||
|
||||
tokio::spawn(sync_helper_wrapper(
|
||||
Arc::clone(&db),
|
||||
Arc::clone(&arc_db),
|
||||
sender_user.clone(),
|
||||
sender_device.clone(),
|
||||
body.since.clone(),
|
||||
@@ -88,7 +110,9 @@ pub async fn sync_events_route(
|
||||
|
||||
let we_have_to_wait = rx.borrow().is_none();
|
||||
if we_have_to_wait {
|
||||
let _ = rx.changed().await;
|
||||
if let Err(e) = rx.changed().await {
|
||||
error!("Error waiting for sync: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
let result = match rx
|
||||
@@ -103,8 +127,8 @@ pub async fn sync_events_route(
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn sync_helper_wrapper(
|
||||
db: Arc<Database>,
|
||||
async fn sync_helper_wrapper(
|
||||
db: Arc<DatabaseGuard>,
|
||||
sender_user: UserId,
|
||||
sender_device: Box<DeviceId>,
|
||||
since: Option<String>,
|
||||
@@ -142,11 +166,13 @@ pub async fn sync_helper_wrapper(
|
||||
}
|
||||
}
|
||||
|
||||
drop(db);
|
||||
|
||||
let _ = tx.send(Some(r.map(|(r, _)| r.into())));
|
||||
}
|
||||
|
||||
async fn sync_helper(
|
||||
db: Arc<Database>,
|
||||
db: Arc<DatabaseGuard>,
|
||||
sender_user: UserId,
|
||||
sender_device: Box<DeviceId>,
|
||||
since: Option<String>,
|
||||
@@ -181,12 +207,26 @@ async fn sync_helper(
|
||||
.filter_map(|r| r.ok()),
|
||||
);
|
||||
|
||||
for room_id in db.rooms.rooms_joined(&sender_user) {
|
||||
let all_joined_rooms = db.rooms.rooms_joined(&sender_user).collect::<Vec<_>>();
|
||||
for room_id in all_joined_rooms {
|
||||
let room_id = room_id?;
|
||||
|
||||
// Get and drop the lock to wait for remaining operations to finish
|
||||
// This will make sure the we have all events until next_batch
|
||||
let mutex_insert = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_insert
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let insert_lock = mutex_insert.lock().unwrap();
|
||||
drop(insert_lock);
|
||||
|
||||
let mut non_timeline_pdus = db
|
||||
.rooms
|
||||
.pdus_until(&sender_user, &room_id, u64::MAX)
|
||||
.pdus_until(&sender_user, &room_id, u64::MAX)?
|
||||
.filter_map(|r| {
|
||||
// Filter out buggy events
|
||||
if r.is_err() {
|
||||
@@ -222,25 +262,75 @@ async fn sync_helper(
|
||||
|
||||
// Database queries:
|
||||
|
||||
let current_shortstatehash = db.rooms.current_shortstatehash(&room_id)?;
|
||||
|
||||
// These type is Option<Option<_>>. The outer Option is None when there is no event between
|
||||
// since and the current room state, meaning there should be no updates.
|
||||
// The inner Option is None when there is an event, but there is no state hash associated
|
||||
// with it. This can happen for the RoomCreate event, so all updates should arrive.
|
||||
let first_pdu_before_since = db.rooms.pdus_until(&sender_user, &room_id, since).next();
|
||||
|
||||
let pdus_after_since = db
|
||||
let current_shortstatehash = db
|
||||
.rooms
|
||||
.pdus_after(&sender_user, &room_id, since)
|
||||
.next()
|
||||
.is_some();
|
||||
.current_shortstatehash(&room_id)?
|
||||
.expect("All rooms have state");
|
||||
|
||||
let since_shortstatehash = first_pdu_before_since.as_ref().map(|pdu| {
|
||||
db.rooms
|
||||
.pdu_shortstatehash(&pdu.as_ref().ok()?.1.event_id)
|
||||
.ok()?
|
||||
});
|
||||
let since_shortstatehash = db.rooms.get_token_shortstatehash(&room_id, since)?;
|
||||
|
||||
// Calculates joined_member_count, invited_member_count and heroes
|
||||
let calculate_counts = || {
|
||||
let joined_member_count = db.rooms.room_joined_count(&room_id)?.unwrap_or(0);
|
||||
let invited_member_count = db.rooms.room_invited_count(&room_id)?.unwrap_or(0);
|
||||
|
||||
// Recalculate heroes (first 5 members)
|
||||
let mut heroes = Vec::new();
|
||||
|
||||
if joined_member_count + invited_member_count <= 5 {
|
||||
// Go through all PDUs and for each member event, check if the user is still joined or
|
||||
// invited until we have 5 or we reach the end
|
||||
|
||||
for hero in db
|
||||
.rooms
|
||||
.all_pdus(&sender_user, &room_id)?
|
||||
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
|
||||
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
|
||||
.map(|(_, pdu)| {
|
||||
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::try_from(state_key.clone()).map_err(|_| {
|
||||
Error::bad_database("Invalid UserId in member PDU.")
|
||||
})?;
|
||||
|
||||
// The membership was and still is invite or join
|
||||
if matches!(
|
||||
content.membership,
|
||||
MembershipState::Join | MembershipState::Invite
|
||||
) && (db.rooms.is_joined(&user_id, &room_id)?
|
||||
|| db.rooms.is_invited(&user_id, &room_id)?)
|
||||
{
|
||||
Ok::<_, Error>(Some(state_key.clone()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
})
|
||||
// Filter out buggy users
|
||||
.filter_map(|u| u.ok())
|
||||
// Filter for possible heroes
|
||||
.flatten()
|
||||
{
|
||||
if heroes.contains(&hero) || hero == sender_user.as_str() {
|
||||
continue;
|
||||
}
|
||||
|
||||
heroes.push(hero);
|
||||
}
|
||||
}
|
||||
|
||||
Ok::<_, Error>((
|
||||
Some(joined_member_count),
|
||||
Some(invited_member_count),
|
||||
heroes,
|
||||
))
|
||||
};
|
||||
|
||||
let (
|
||||
heroes,
|
||||
@@ -248,121 +338,125 @@ async fn sync_helper(
|
||||
invited_member_count,
|
||||
joined_since_last_sync,
|
||||
state_events,
|
||||
) = if pdus_after_since && Some(current_shortstatehash) != since_shortstatehash {
|
||||
let current_state = db.rooms.room_state_full(&room_id)?;
|
||||
let current_members = current_state
|
||||
) = if since_shortstatehash.is_none() {
|
||||
// Probably since = 0, we will do an initial sync
|
||||
let (joined_member_count, invited_member_count, heroes) = calculate_counts()?;
|
||||
|
||||
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
|
||||
let state_events = current_state_ids
|
||||
.iter()
|
||||
.filter(|(key, _)| key.0 == EventType::RoomMember)
|
||||
.map(|(key, value)| (&key.1, value)) // Only keep state key
|
||||
.map(|(_, id)| db.rooms.get_pdu(id))
|
||||
.filter_map(|r| r.ok().flatten())
|
||||
.collect::<Vec<_>>();
|
||||
let encrypted_room = current_state
|
||||
.get(&(EventType::RoomEncryption, "".to_owned()))
|
||||
.is_some();
|
||||
let since_state = since_shortstatehash
|
||||
.as_ref()
|
||||
.map(|since_shortstatehash| {
|
||||
since_shortstatehash
|
||||
.map(|since_shortstatehash| db.rooms.state_full(since_shortstatehash))
|
||||
.transpose()
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let since_encryption = since_state.as_ref().map(|state| {
|
||||
state
|
||||
.as_ref()
|
||||
.map(|state| state.get(&(EventType::RoomEncryption, "".to_owned())))
|
||||
});
|
||||
(
|
||||
heroes,
|
||||
joined_member_count,
|
||||
invited_member_count,
|
||||
true,
|
||||
state_events,
|
||||
)
|
||||
} else if timeline_pdus.is_empty() && since_shortstatehash == Some(current_shortstatehash) {
|
||||
// No state changes
|
||||
(Vec::new(), None, None, false, Vec::new())
|
||||
} else {
|
||||
// Incremental /sync
|
||||
let since_shortstatehash = since_shortstatehash.unwrap();
|
||||
|
||||
// Calculations:
|
||||
let new_encrypted_room =
|
||||
encrypted_room && since_encryption.map_or(true, |encryption| encryption.is_none());
|
||||
|
||||
let send_member_count = since_state.as_ref().map_or(true, |since_state| {
|
||||
since_state.as_ref().map_or(true, |since_state| {
|
||||
current_members.len()
|
||||
!= since_state
|
||||
.iter()
|
||||
.filter(|(key, _)| key.0 == EventType::RoomMember)
|
||||
.count()
|
||||
})
|
||||
});
|
||||
|
||||
let since_sender_member = since_state.as_ref().map(|since_state| {
|
||||
since_state.as_ref().and_then(|state| {
|
||||
state
|
||||
.get(&(EventType::RoomMember, sender_user.as_str().to_owned()))
|
||||
.and_then(|pdu| {
|
||||
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()
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
if encrypted_room {
|
||||
for (user_id, current_member) in current_members {
|
||||
let current_membership = serde_json::from_value::<
|
||||
Raw<ruma::events::room::member::MemberEventContent>,
|
||||
>(current_member.content.clone())
|
||||
let since_sender_member = db
|
||||
.rooms
|
||||
.state_get(
|
||||
since_shortstatehash,
|
||||
&EventType::RoomMember,
|
||||
sender_user.as_str(),
|
||||
)?
|
||||
.and_then(|pdu| {
|
||||
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."))?
|
||||
.membership;
|
||||
.map_err(|_| Error::bad_database("Invalid PDU in database."))
|
||||
.ok()
|
||||
});
|
||||
|
||||
let since_membership =
|
||||
since_state
|
||||
.as_ref()
|
||||
.map_or(MembershipState::Leave, |since_state| {
|
||||
since_state
|
||||
.as_ref()
|
||||
.and_then(|since_state| {
|
||||
since_state
|
||||
.get(&(EventType::RoomMember, user_id.clone()))
|
||||
.and_then(|since_member| {
|
||||
serde_json::from_value::<
|
||||
Raw<ruma::events::room::member::MemberEventContent>,
|
||||
>(
|
||||
since_member.content.clone()
|
||||
)
|
||||
.expect("Raw::from_value always works")
|
||||
.deserialize()
|
||||
.map_err(|_| {
|
||||
Error::bad_database("Invalid PDU in database.")
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
})
|
||||
.map_or(MembershipState::Leave, |member| member.membership)
|
||||
});
|
||||
let joined_since_last_sync = since_sender_member
|
||||
.map_or(true, |member| member.membership != MembershipState::Join);
|
||||
|
||||
let user_id = UserId::try_from(user_id.clone())
|
||||
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
|
||||
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
|
||||
|
||||
match (since_membership, current_membership) {
|
||||
(MembershipState::Leave, MembershipState::Join) => {
|
||||
// A new user joined an encrypted room
|
||||
if !share_encrypted_room(&db, &sender_user, &user_id, &room_id)? {
|
||||
device_list_updates.insert(user_id);
|
||||
let since_state_ids = db.rooms.state_full_ids(since_shortstatehash)?;
|
||||
|
||||
let state_events = if joined_since_last_sync {
|
||||
current_state_ids
|
||||
.iter()
|
||||
.map(|(_, id)| db.rooms.get_pdu(id))
|
||||
.filter_map(|r| r.ok().flatten())
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
current_state_ids
|
||||
.iter()
|
||||
.filter(|(key, id)| since_state_ids.get(key) != Some(id))
|
||||
.map(|(_, id)| db.rooms.get_pdu(id))
|
||||
.filter_map(|r| r.ok().flatten())
|
||||
.collect()
|
||||
};
|
||||
|
||||
let encrypted_room = db
|
||||
.rooms
|
||||
.state_get(current_shortstatehash, &EventType::RoomEncryption, "")?
|
||||
.is_some();
|
||||
|
||||
let since_encryption =
|
||||
db.rooms
|
||||
.state_get(since_shortstatehash, &EventType::RoomEncryption, "")?;
|
||||
|
||||
// Calculations:
|
||||
let new_encrypted_room = encrypted_room && since_encryption.is_none();
|
||||
|
||||
let send_member_count = state_events
|
||||
.iter()
|
||||
.any(|event| event.kind == EventType::RoomMember);
|
||||
|
||||
if encrypted_room {
|
||||
for state_event in &state_events {
|
||||
if state_event.kind != EventType::RoomMember {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(state_key) = &state_event.state_key {
|
||||
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_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;
|
||||
|
||||
match new_membership {
|
||||
MembershipState::Join => {
|
||||
// A new user joined an encrypted room
|
||||
if !share_encrypted_room(&db, &sender_user, &user_id, &room_id)? {
|
||||
device_list_updates.insert(user_id);
|
||||
}
|
||||
}
|
||||
MembershipState::Leave => {
|
||||
// Write down users that have left encrypted rooms we are in
|
||||
left_encrypted_users.insert(user_id);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// TODO: Remove, this should never happen here, right?
|
||||
(MembershipState::Join, MembershipState::Leave) => {
|
||||
// Write down users that have left encrypted rooms we are in
|
||||
left_encrypted_users.insert(user_id);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let joined_since_last_sync = since_sender_member.map_or(true, |member| {
|
||||
member.map_or(true, |member| member.membership != MembershipState::Join)
|
||||
});
|
||||
|
||||
if joined_since_last_sync && encrypted_room || new_encrypted_room {
|
||||
// If the user is in a new encrypted room, give them all joined users
|
||||
device_list_updates.extend(
|
||||
@@ -382,100 +476,11 @@ async fn sync_helper(
|
||||
}
|
||||
|
||||
let (joined_member_count, invited_member_count, heroes) = if send_member_count {
|
||||
let joined_member_count = db.rooms.room_members(&room_id).count();
|
||||
let invited_member_count = db.rooms.room_members_invited(&room_id).count();
|
||||
|
||||
// Recalculate heroes (first 5 members)
|
||||
let mut heroes = Vec::new();
|
||||
|
||||
if joined_member_count + invited_member_count <= 5 {
|
||||
// Go through all PDUs and for each member event, check if the user is still joined or
|
||||
// invited until we have 5 or we reach the end
|
||||
|
||||
for hero in db
|
||||
.rooms
|
||||
.all_pdus(&sender_user, &room_id)
|
||||
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
|
||||
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
|
||||
.map(|(_, pdu)| {
|
||||
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::try_from(state_key.clone()).map_err(|_| {
|
||||
Error::bad_database("Invalid UserId in member PDU.")
|
||||
})?;
|
||||
|
||||
// The membership was and still is invite or join
|
||||
if matches!(
|
||||
content.membership,
|
||||
MembershipState::Join | MembershipState::Invite
|
||||
) && (db.rooms.is_joined(&user_id, &room_id)?
|
||||
|| db.rooms.is_invited(&user_id, &room_id)?)
|
||||
{
|
||||
Ok::<_, Error>(Some(state_key.clone()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
})
|
||||
// Filter out buggy users
|
||||
.filter_map(|u| u.ok())
|
||||
// Filter for possible heroes
|
||||
.flatten()
|
||||
{
|
||||
if heroes.contains(&hero) || hero == sender_user.as_str() {
|
||||
continue;
|
||||
}
|
||||
|
||||
heroes.push(hero);
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
Some(joined_member_count),
|
||||
Some(invited_member_count),
|
||||
heroes,
|
||||
)
|
||||
calculate_counts()?
|
||||
} else {
|
||||
(None, None, Vec::new())
|
||||
};
|
||||
|
||||
let state_events = if joined_since_last_sync {
|
||||
current_state
|
||||
.iter()
|
||||
.map(|(_, pdu)| pdu.to_sync_state_event())
|
||||
.collect()
|
||||
} else {
|
||||
match since_state {
|
||||
None => Vec::new(),
|
||||
Some(Some(since_state)) => current_state
|
||||
.iter()
|
||||
.filter(|(key, value)| {
|
||||
since_state.get(key).map(|e| &e.event_id) != Some(&value.event_id)
|
||||
})
|
||||
.filter(|(_, value)| {
|
||||
!timeline_pdus.iter().any(|(_, timeline_pdu)| {
|
||||
timeline_pdu.kind == value.kind
|
||||
&& timeline_pdu.state_key == value.state_key
|
||||
})
|
||||
})
|
||||
.map(|(_, pdu)| pdu.to_sync_state_event())
|
||||
.collect(),
|
||||
Some(None) => current_state
|
||||
.iter()
|
||||
.map(|(_, pdu)| pdu.to_sync_state_event())
|
||||
.collect(),
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
heroes,
|
||||
joined_member_count,
|
||||
@@ -483,8 +488,6 @@ async fn sync_helper(
|
||||
joined_since_last_sync,
|
||||
state_events,
|
||||
)
|
||||
} else {
|
||||
(Vec::new(), None, None, false, Vec::new())
|
||||
};
|
||||
|
||||
// Look for device list updates in this room
|
||||
@@ -547,6 +550,10 @@ async fn sync_helper(
|
||||
);
|
||||
}
|
||||
|
||||
// Save the state after this sync so we can send the correct state diff next sync
|
||||
db.rooms
|
||||
.associate_token_shortstatehash(&room_id, next_batch, current_shortstatehash)?;
|
||||
|
||||
let joined_room = sync_events::JoinedRoom {
|
||||
account_data: sync_events::RoomAccountData {
|
||||
events: db
|
||||
@@ -575,7 +582,10 @@ async fn sync_helper(
|
||||
events: room_events,
|
||||
},
|
||||
state: sync_events::State {
|
||||
events: state_events,
|
||||
events: state_events
|
||||
.iter()
|
||||
.map(|pdu| pdu.to_sync_state_event())
|
||||
.collect(),
|
||||
},
|
||||
ephemeral: sync_events::Ephemeral { events: edus },
|
||||
};
|
||||
@@ -591,10 +601,10 @@ async fn sync_helper(
|
||||
.presence_since(&room_id, since, &db.rooms, &db.globals)?
|
||||
{
|
||||
match presence_updates.entry(user_id) {
|
||||
hash_map::Entry::Vacant(v) => {
|
||||
Entry::Vacant(v) => {
|
||||
v.insert(presence);
|
||||
}
|
||||
hash_map::Entry::Occupied(mut o) => {
|
||||
Entry::Occupied(mut o) => {
|
||||
let p = o.get_mut();
|
||||
|
||||
// Update existing presence event with more info
|
||||
@@ -620,8 +630,22 @@ async fn sync_helper(
|
||||
}
|
||||
|
||||
let mut left_rooms = BTreeMap::new();
|
||||
for result in db.rooms.rooms_left(&sender_user) {
|
||||
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?;
|
||||
|
||||
// Get and drop the lock to wait for remaining operations to finish
|
||||
let mutex_insert = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_insert
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let insert_lock = mutex_insert.lock().unwrap();
|
||||
drop(insert_lock);
|
||||
|
||||
let left_count = db.rooms.get_left_count(&room_id, &sender_user)?;
|
||||
|
||||
// Left before last sync
|
||||
@@ -646,8 +670,22 @@ async fn sync_helper(
|
||||
}
|
||||
|
||||
let mut invited_rooms = BTreeMap::new();
|
||||
for result in db.rooms.rooms_invited(&sender_user) {
|
||||
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?;
|
||||
|
||||
// Get and drop the lock to wait for remaining operations to finish
|
||||
let mutex_insert = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex_insert
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let insert_lock = mutex_insert.lock().unwrap();
|
||||
drop(insert_lock);
|
||||
|
||||
let invite_count = db.rooms.get_invite_count(&room_id, &sender_user)?;
|
||||
|
||||
// Invited before last sync
|
||||
@@ -696,6 +734,7 @@ async fn sync_helper(
|
||||
leave: left_rooms,
|
||||
join: joined_rooms,
|
||||
invite: invited_rooms,
|
||||
knock: BTreeMap::new(), // TODO
|
||||
},
|
||||
presence: sync_events::Presence {
|
||||
events: presence_updates
|
||||
|
||||
+24
-10
@@ -1,21 +1,25 @@
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
|
||||
use ruma::{
|
||||
api::client::r0::tag::{create_tag, delete_tag, get_tags},
|
||||
events::EventType,
|
||||
};
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{delete, get, put};
|
||||
|
||||
/// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}`
|
||||
///
|
||||
/// Adds a tag to the room.
|
||||
///
|
||||
/// - Inserts the tag into the tag event of the room account data.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn update_tag_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_tag::Request<'_>>,
|
||||
) -> ConduitResult<create_tag::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -41,18 +45,23 @@ pub async fn update_tag_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(create_tag::Response.into())
|
||||
Ok(create_tag::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `DELETE /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}`
|
||||
///
|
||||
/// Deletes a tag from the room.
|
||||
///
|
||||
/// - Removes the tag from the tag event of the room account data.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
delete("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn delete_tag_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<delete_tag::Request<'_>>,
|
||||
) -> ConduitResult<delete_tag::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -75,18 +84,23 @@ pub async fn delete_tag_route(
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(delete_tag::Response.into())
|
||||
Ok(delete_tag::Response {}.into())
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags`
|
||||
///
|
||||
/// Returns tags on the room.
|
||||
///
|
||||
/// - Gets the tag event of the room account data.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/user/<_>/rooms/<_>/tags", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_tags_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_tags::Request<'_>>,
|
||||
) -> ConduitResult<get_tags::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
@@ -5,6 +5,9 @@ use ruma::api::client::r0::thirdparty::get_protocols;
|
||||
use rocket::get;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// # `GET /_matrix/client/r0/thirdparty/protocols`
|
||||
///
|
||||
/// TODO: Fetches all metadata about protocols supported by the homeserver.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/client/r0/thirdparty/protocols")
|
||||
|
||||
@@ -1,28 +1,36 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Error, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{error::ErrorKind, r0::to_device::send_event_to_device},
|
||||
api::{
|
||||
client::{error::ErrorKind, r0::to_device::send_event_to_device},
|
||||
federation::{self, transactions::edu::DirectDeviceContent},
|
||||
},
|
||||
events::EventType,
|
||||
to_device::DeviceIdOrAllDevices,
|
||||
};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::put;
|
||||
|
||||
/// # `PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId}`
|
||||
///
|
||||
/// Send a to-device event to a set of client devices.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn send_event_to_device_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<send_event_to_device::Request<'_>>,
|
||||
) -> ConduitResult<send_event_to_device::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
let sender_device = body.sender_device.as_deref();
|
||||
|
||||
// TODO: uncomment when https://github.com/vector-im/element-android/issues/3589 is solved
|
||||
// Check if this is a new transaction id
|
||||
/*
|
||||
if db
|
||||
.transaction_ids
|
||||
.existing_txnid(sender_user, sender_device, &body.txn_id)?
|
||||
@@ -30,9 +38,33 @@ pub async fn send_event_to_device_route(
|
||||
{
|
||||
return Ok(send_event_to_device::Response.into());
|
||||
}
|
||||
*/
|
||||
|
||||
for (target_user_id, map) in &body.messages {
|
||||
for (target_device_id_maybe, event) in map {
|
||||
if target_user_id.server_name() != db.globals.server_name() {
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert(target_device_id_maybe.clone(), event.clone());
|
||||
let mut messages = BTreeMap::new();
|
||||
messages.insert(target_user_id.clone(), map);
|
||||
|
||||
db.sending.send_reliable_edu(
|
||||
target_user_id.server_name(),
|
||||
serde_json::to_vec(&federation::transactions::edu::Edu::DirectToDevice(
|
||||
DirectDeviceContent {
|
||||
sender: sender_user.clone(),
|
||||
ev_type: EventType::from(&body.event_type),
|
||||
message_id: body.txn_id.clone(),
|
||||
messages,
|
||||
},
|
||||
))
|
||||
.expect("DirectToDevice EDU can be serialized"),
|
||||
db.globals.next_count()?,
|
||||
)?;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
match target_device_id_maybe {
|
||||
DeviceIdOrAllDevices::DeviceId(target_device_id) => db.users.add_to_device_event(
|
||||
sender_user,
|
||||
@@ -67,7 +99,7 @@ pub async fn send_event_to_device_route(
|
||||
db.transaction_ids
|
||||
.add_txnid(sender_user, sender_device, &body.txn_id, &[])?;
|
||||
|
||||
db.flush().await?;
|
||||
db.flush()?;
|
||||
|
||||
Ok(send_event_to_device::Response.into())
|
||||
Ok(send_event_to_device::Response {}.into())
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{utils, ConduitResult, Database, Ruma};
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Ruma};
|
||||
use create_typing_event::Typing;
|
||||
use ruma::api::client::r0::typing::create_typing_event;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::put;
|
||||
|
||||
/// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}`
|
||||
///
|
||||
/// Sets the typing state of the sender user.
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
put("/_matrix/client/r0/rooms/<_>/typing/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub fn create_typing_event_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<create_typing_event::Request<'_>>,
|
||||
) -> ConduitResult<create_typing_event::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
@@ -32,5 +32,5 @@ pub fn create_typing_event_route(
|
||||
.typing_remove(&sender_user, &body.room_id, &db.globals)?;
|
||||
}
|
||||
|
||||
Ok(create_typing_event::Response.into())
|
||||
Ok(create_typing_event::Response {}.into())
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use rocket::get;
|
||||
///
|
||||
/// - Versions take the form MAJOR.MINOR.PATCH
|
||||
/// - Only the latest PATCH release will be reported for each MAJOR.MINOR value
|
||||
/// - Unstable features should be namespaced and may include version information in their name
|
||||
/// - Unstable features are namespaced and may include version information in their name
|
||||
///
|
||||
/// Note: Unstable features are used while developing new features. Clients should avoid using
|
||||
/// unstable features in their stable releases
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::State;
|
||||
use crate::{ConduitResult, Database, Ruma};
|
||||
use crate::{database::DatabaseGuard, ConduitResult, Ruma};
|
||||
use ruma::api::client::r0::user_directory::search_users;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::post;
|
||||
|
||||
/// # `POST /_matrix/client/r0/user_directory/search`
|
||||
///
|
||||
/// Searches all known users for a match.
|
||||
///
|
||||
/// - TODO: Hide users that are not in any public rooms?
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/user_directory/search", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn search_users_route(
|
||||
db: State<'_, Arc<Database>>,
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<search_users::Request<'_>>,
|
||||
) -> ConduitResult<search_users::Response> {
|
||||
let limit = u64::from(body.limit) as usize;
|
||||
@@ -28,20 +30,22 @@ pub async fn search_users_route(
|
||||
avatar_url: db.users.avatar_url(&user_id).ok()?,
|
||||
};
|
||||
|
||||
if !user
|
||||
let user_id_matches = user
|
||||
.user_id
|
||||
.to_string()
|
||||
.to_lowercase()
|
||||
.contains(&body.search_term.to_lowercase())
|
||||
&& user
|
||||
.display_name
|
||||
.as_ref()
|
||||
.filter(|name| {
|
||||
name.to_lowercase()
|
||||
.contains(&body.search_term.to_lowercase())
|
||||
})
|
||||
.is_none()
|
||||
{
|
||||
.contains(&body.search_term.to_lowercase());
|
||||
|
||||
let user_displayname_matches = user
|
||||
.display_name
|
||||
.as_ref()
|
||||
.filter(|name| {
|
||||
name.to_lowercase()
|
||||
.contains(&body.search_term.to_lowercase())
|
||||
})
|
||||
.is_some();
|
||||
|
||||
if !user_id_matches && !user_displayname_matches {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ use std::time::Duration;
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
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"))]
|
||||
#[tracing::instrument]
|
||||
pub async fn turn_server_route() -> ConduitResult<get_turn_server_info::Response> {
|
||||
|
||||
+640
-104
@@ -17,18 +17,27 @@ pub mod users;
|
||||
use crate::{utils, Error, Result};
|
||||
use abstraction::DatabaseEngine;
|
||||
use directories::ProjectDirs;
|
||||
use log::error;
|
||||
use lru_cache::LruCache;
|
||||
use rocket::futures::{channel::mpsc, stream::FuturesUnordered, StreamExt};
|
||||
use ruma::{DeviceId, ServerName, UserId};
|
||||
use serde::Deserialize;
|
||||
use rocket::{
|
||||
futures::{channel::mpsc, stream::FuturesUnordered, StreamExt},
|
||||
outcome::{try_outcome, IntoOutcome},
|
||||
request::{FromRequest, Request},
|
||||
Shutdown, State,
|
||||
};
|
||||
use ruma::{DeviceId, EventId, RoomId, ServerName, UserId};
|
||||
use serde::{de::IgnoredAny, Deserialize};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
fs::{self, remove_dir_all},
|
||||
io::Write,
|
||||
sync::{Arc, RwLock},
|
||||
mem::size_of,
|
||||
ops::Deref,
|
||||
path::Path,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::sync::{OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use self::proxy::ProxyConfig;
|
||||
|
||||
@@ -36,13 +45,17 @@ use self::proxy::ProxyConfig;
|
||||
pub struct Config {
|
||||
server_name: Box<ServerName>,
|
||||
database_path: String,
|
||||
#[serde(default = "default_cache_capacity")]
|
||||
cache_capacity: u32,
|
||||
#[serde(default = "default_db_cache_capacity_mb")]
|
||||
db_cache_capacity_mb: f64,
|
||||
#[serde(default = "default_pdu_cache_capacity")]
|
||||
pdu_cache_capacity: u32,
|
||||
#[serde(default = "default_sqlite_wal_clean_second_interval")]
|
||||
sqlite_wal_clean_second_interval: u32,
|
||||
#[serde(default = "default_max_request_size")]
|
||||
max_request_size: u32,
|
||||
#[serde(default = "default_max_concurrent_requests")]
|
||||
max_concurrent_requests: u16,
|
||||
#[serde(default = "true_fn")]
|
||||
#[serde(default = "false_fn")]
|
||||
allow_registration: bool,
|
||||
#[serde(default = "true_fn")]
|
||||
allow_encryption: bool,
|
||||
@@ -50,6 +63,8 @@ pub struct Config {
|
||||
allow_federation: bool,
|
||||
#[serde(default = "false_fn")]
|
||||
pub allow_jaeger: bool,
|
||||
#[serde(default = "false_fn")]
|
||||
pub tracing_flame: bool,
|
||||
#[serde(default)]
|
||||
proxy: ProxyConfig,
|
||||
jwt_secret: Option<String>,
|
||||
@@ -57,6 +72,29 @@ pub struct Config {
|
||||
trusted_servers: Vec<Box<ServerName>>,
|
||||
#[serde(default = "default_log")]
|
||||
pub log: String,
|
||||
|
||||
#[serde(flatten)]
|
||||
catchall: BTreeMap<String, IgnoredAny>,
|
||||
}
|
||||
|
||||
const DEPRECATED_KEYS: &[&str] = &["cache_capacity"];
|
||||
|
||||
impl Config {
|
||||
pub fn warn_deprecated(&self) {
|
||||
let mut was_deprecated = false;
|
||||
for key in self
|
||||
.catchall
|
||||
.keys()
|
||||
.filter(|key| DEPRECATED_KEYS.iter().any(|s| s == key))
|
||||
{
|
||||
warn!("Config parameter {} is deprecated", key);
|
||||
was_deprecated = true;
|
||||
}
|
||||
|
||||
if was_deprecated {
|
||||
warn!("Read conduit documentation and check your configuration if any new configuration parameters should be adjusted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn false_fn() -> bool {
|
||||
@@ -67,8 +105,16 @@ fn true_fn() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_cache_capacity() -> u32 {
|
||||
1024 * 1024 * 1024
|
||||
fn default_db_cache_capacity_mb() -> f64 {
|
||||
200.0
|
||||
}
|
||||
|
||||
fn default_pdu_cache_capacity() -> u32 {
|
||||
100_000
|
||||
}
|
||||
|
||||
fn default_sqlite_wal_clean_second_interval() -> u32 {
|
||||
1 * 60 // every minute
|
||||
}
|
||||
|
||||
fn default_max_request_size() -> u32 {
|
||||
@@ -84,12 +130,16 @@ fn default_log() -> String {
|
||||
}
|
||||
|
||||
#[cfg(feature = "sled")]
|
||||
pub type Engine = abstraction::SledEngine;
|
||||
pub type Engine = abstraction::sled::Engine;
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
pub type Engine = abstraction::RocksDbEngine;
|
||||
#[cfg(feature = "sqlite")]
|
||||
pub type Engine = abstraction::sqlite::Engine;
|
||||
|
||||
#[cfg(feature = "heed")]
|
||||
pub type Engine = abstraction::heed::Engine;
|
||||
|
||||
pub struct Database {
|
||||
_db: Arc<Engine>,
|
||||
pub globals: globals::Globals,
|
||||
pub users: users::Users,
|
||||
pub uiaa: uiaa::Uiaa,
|
||||
@@ -117,8 +167,37 @@ impl Database {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_sled_or_sqlite_db(config: &Config) -> Result<()> {
|
||||
#[cfg(feature = "backend_sqlite")]
|
||||
{
|
||||
let path = Path::new(&config.database_path);
|
||||
|
||||
let sled_exists = path.join("db").exists();
|
||||
let sqlite_exists = path.join("conduit.db").exists();
|
||||
if sled_exists {
|
||||
if sqlite_exists {
|
||||
// most likely an in-place directory, only warn
|
||||
warn!("Both sled and sqlite databases are detected in database directory");
|
||||
warn!("Currently running from the sqlite database, but consider removing sled database files to free up space")
|
||||
} else {
|
||||
error!(
|
||||
"Sled database detected, conduit now uses sqlite for database operations"
|
||||
);
|
||||
error!("This database must be converted to sqlite, go to https://github.com/ShadowJonathan/conduit_toolbox#conduit_sled_to_sqlite");
|
||||
return Err(Error::bad_config(
|
||||
"sled database detected, migrate to sqlite",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Load an existing database or create a new one.
|
||||
pub async fn load_or_create(config: Config) -> Result<Arc<Self>> {
|
||||
pub async fn load_or_create(config: &Config) -> Result<Arc<TokioRwLock<Self>>> {
|
||||
Self::check_sled_or_sqlite_db(&config)?;
|
||||
|
||||
let builder = Engine::open(&config)?;
|
||||
|
||||
if config.max_request_size < 1024 {
|
||||
@@ -128,11 +207,13 @@ impl Database {
|
||||
let (admin_sender, admin_receiver) = mpsc::unbounded();
|
||||
let (sending_sender, sending_receiver) = mpsc::unbounded();
|
||||
|
||||
let db = Arc::new(Self {
|
||||
let db = Arc::new(TokioRwLock::from(Self {
|
||||
_db: builder.clone(),
|
||||
users: users::Users {
|
||||
userid_password: builder.open_tree("userid_password")?,
|
||||
userid_displayname: builder.open_tree("userid_displayname")?,
|
||||
userid_avatarurl: builder.open_tree("userid_avatarurl")?,
|
||||
userid_blurhash: builder.open_tree("userid_blurhash")?,
|
||||
userdeviceid_token: builder.open_tree("userdeviceid_token")?,
|
||||
userdeviceid_metadata: builder.open_tree("userdeviceid_metadata")?,
|
||||
userid_devicelistversion: builder.open_tree("userid_devicelistversion")?,
|
||||
@@ -176,6 +257,8 @@ impl Database {
|
||||
serverroomids: builder.open_tree("serverroomids")?,
|
||||
userroomid_joined: builder.open_tree("userroomid_joined")?,
|
||||
roomuserid_joined: builder.open_tree("roomuserid_joined")?,
|
||||
roomid_joinedcount: builder.open_tree("roomid_joinedcount")?,
|
||||
roomid_invitedcount: builder.open_tree("roomid_invitedcount")?,
|
||||
roomuseroncejoinedids: builder.open_tree("roomuseroncejoinedids")?,
|
||||
userroomid_invitestate: builder.open_tree("userroomid_invitestate")?,
|
||||
roomuserid_invitecount: builder.open_tree("roomuserid_invitecount")?,
|
||||
@@ -186,19 +269,42 @@ impl Database {
|
||||
userroomid_highlightcount: builder.open_tree("userroomid_highlightcount")?,
|
||||
|
||||
statekey_shortstatekey: builder.open_tree("statekey_shortstatekey")?,
|
||||
stateid_shorteventid: builder.open_tree("stateid_shorteventid")?,
|
||||
shortstatekey_statekey: builder.open_tree("shortstatekey_statekey")?,
|
||||
|
||||
shorteventid_authchain: builder.open_tree("shorteventid_authchain")?,
|
||||
|
||||
roomid_shortroomid: builder.open_tree("roomid_shortroomid")?,
|
||||
|
||||
shortstatehash_statediff: builder.open_tree("shortstatehash_statediff")?,
|
||||
eventid_shorteventid: builder.open_tree("eventid_shorteventid")?,
|
||||
shorteventid_eventid: builder.open_tree("shorteventid_eventid")?,
|
||||
shorteventid_shortstatehash: builder.open_tree("shorteventid_shortstatehash")?,
|
||||
roomid_shortstatehash: builder.open_tree("roomid_shortstatehash")?,
|
||||
roomsynctoken_shortstatehash: builder.open_tree("roomsynctoken_shortstatehash")?,
|
||||
statehash_shortstatehash: builder.open_tree("statehash_shortstatehash")?,
|
||||
|
||||
eventid_outlierpdu: builder.open_tree("eventid_outlierpdu")?,
|
||||
prevevent_parent: builder.open_tree("prevevent_parent")?,
|
||||
pdu_cache: RwLock::new(LruCache::new(1_000_000)),
|
||||
softfailedeventids: builder.open_tree("softfailedeventids")?,
|
||||
|
||||
referencedevents: builder.open_tree("referencedevents")?,
|
||||
pdu_cache: Mutex::new(LruCache::new(
|
||||
config
|
||||
.pdu_cache_capacity
|
||||
.try_into()
|
||||
.expect("pdu cache capacity fits into usize"),
|
||||
)),
|
||||
auth_chain_cache: Mutex::new(LruCache::new(1_000_000)),
|
||||
shorteventid_cache: Mutex::new(LruCache::new(1_000_000)),
|
||||
eventidshort_cache: Mutex::new(LruCache::new(1_000_000)),
|
||||
shortstatekey_cache: Mutex::new(LruCache::new(1_000_000)),
|
||||
statekeyshort_cache: Mutex::new(LruCache::new(1_000_000)),
|
||||
our_real_users_cache: RwLock::new(HashMap::new()),
|
||||
appservice_in_room_cache: RwLock::new(HashMap::new()),
|
||||
stateinfo_cache: Mutex::new(LruCache::new(1000)),
|
||||
},
|
||||
account_data: account_data::AccountData {
|
||||
roomuserdataid_accountdata: builder.open_tree("roomuserdataid_accountdata")?,
|
||||
roomusertype_roomuserdataid: builder.open_tree("roomusertype_roomuserdataid")?,
|
||||
},
|
||||
media: media::Media {
|
||||
mediaid_file: builder.open_tree("mediaid_file")?,
|
||||
@@ -213,8 +319,8 @@ impl Database {
|
||||
},
|
||||
sending: sending::Sending {
|
||||
servername_educount: builder.open_tree("servername_educount")?,
|
||||
servernamepduids: builder.open_tree("servernamepduids")?,
|
||||
servercurrentevents: builder.open_tree("servercurrentevents")?,
|
||||
servernameevent_data: builder.open_tree("servernameevent_data")?,
|
||||
servercurrentevent_data: builder.open_tree("servercurrentevent_data")?,
|
||||
maximum_requests: Arc::new(Semaphore::new(config.max_concurrent_requests as usize)),
|
||||
sender: sending_sender,
|
||||
},
|
||||
@@ -231,104 +337,440 @@ impl Database {
|
||||
globals: globals::Globals::load(
|
||||
builder.open_tree("global")?,
|
||||
builder.open_tree("server_signingkeys")?,
|
||||
config,
|
||||
config.clone(),
|
||||
)?,
|
||||
});
|
||||
}));
|
||||
|
||||
// MIGRATIONS
|
||||
// TODO: database versions of new dbs should probably not be 0
|
||||
if db.globals.database_version()? < 1 {
|
||||
for (roomserverid, _) in db.rooms.roomserverids.iter() {
|
||||
let mut parts = roomserverid.split(|&b| b == 0xff);
|
||||
let room_id = parts.next().expect("split always returns one element");
|
||||
let servername = match parts.next() {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
error!("Migration: Invalid roomserverid in db.");
|
||||
{
|
||||
let db = db.read().await;
|
||||
// MIGRATIONS
|
||||
// TODO: database versions of new dbs should probably not be 0
|
||||
if db.globals.database_version()? < 1 {
|
||||
for (roomserverid, _) in db.rooms.roomserverids.iter() {
|
||||
let mut parts = roomserverid.split(|&b| b == 0xff);
|
||||
let room_id = parts.next().expect("split always returns one element");
|
||||
let servername = match parts.next() {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
error!("Migration: Invalid roomserverid in db.");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let mut serverroomid = servername.to_vec();
|
||||
serverroomid.push(0xff);
|
||||
serverroomid.extend_from_slice(room_id);
|
||||
|
||||
db.rooms.serverroomids.insert(&serverroomid, &[])?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(1)?;
|
||||
|
||||
println!("Migration: 0 -> 1 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 2 {
|
||||
// We accidentally inserted hashed versions of "" into the db instead of just ""
|
||||
for (userid, password) in db.users.userid_password.iter() {
|
||||
let password = utils::string_from_bytes(&password);
|
||||
|
||||
let empty_hashed_password = password.map_or(false, |password| {
|
||||
argon2::verify_encoded(&password, b"").unwrap_or(false)
|
||||
});
|
||||
|
||||
if empty_hashed_password {
|
||||
db.users.userid_password.insert(&userid, b"")?;
|
||||
}
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(2)?;
|
||||
|
||||
println!("Migration: 1 -> 2 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 3 {
|
||||
// Move media to filesystem
|
||||
for (key, content) in db.media.mediaid_file.iter() {
|
||||
if content.is_empty() {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let mut serverroomid = servername.to_vec();
|
||||
serverroomid.push(0xff);
|
||||
serverroomid.extend_from_slice(room_id);
|
||||
|
||||
db.rooms.serverroomids.insert(&serverroomid, &[])?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(1)?;
|
||||
|
||||
println!("Migration: 0 -> 1 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 2 {
|
||||
// We accidentally inserted hashed versions of "" into the db instead of just ""
|
||||
for (userid, password) in db.users.userid_password.iter() {
|
||||
let password = utils::string_from_bytes(&password);
|
||||
|
||||
let empty_hashed_password = password.map_or(false, |password| {
|
||||
argon2::verify_encoded(&password, b"").unwrap_or(false)
|
||||
});
|
||||
|
||||
if empty_hashed_password {
|
||||
db.users.userid_password.insert(&userid, b"")?;
|
||||
}
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(2)?;
|
||||
|
||||
println!("Migration: 1 -> 2 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 3 {
|
||||
// Move media to filesystem
|
||||
for (key, content) in db.media.mediaid_file.iter() {
|
||||
if content.len() == 0 {
|
||||
continue;
|
||||
let path = db.globals.get_media_file(&key);
|
||||
let mut file = fs::File::create(path)?;
|
||||
file.write_all(&content)?;
|
||||
db.media.mediaid_file.insert(&key, &[])?;
|
||||
}
|
||||
|
||||
let path = db.globals.get_media_file(&key);
|
||||
let mut file = fs::File::create(path)?;
|
||||
file.write_all(&content)?;
|
||||
db.media.mediaid_file.insert(&key, &[])?;
|
||||
db.globals.bump_database_version(3)?;
|
||||
|
||||
println!("Migration: 2 -> 3 finished");
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(3)?;
|
||||
|
||||
println!("Migration: 2 -> 3 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 4 {
|
||||
// Add federated users to db as deactivated
|
||||
for our_user in db.users.iter() {
|
||||
let our_user = our_user?;
|
||||
if db.users.is_deactivated(&our_user)? {
|
||||
continue;
|
||||
}
|
||||
for room in db.rooms.rooms_joined(&our_user) {
|
||||
for user in db.rooms.room_members(&room?) {
|
||||
let user = user?;
|
||||
if user.server_name() != db.globals.server_name() {
|
||||
println!("Migration: Creating user {}", user);
|
||||
db.users.create(&user, None)?;
|
||||
if db.globals.database_version()? < 4 {
|
||||
// Add federated users to db as deactivated
|
||||
for our_user in db.users.iter() {
|
||||
let our_user = our_user?;
|
||||
if db.users.is_deactivated(&our_user)? {
|
||||
continue;
|
||||
}
|
||||
for room in db.rooms.rooms_joined(&our_user) {
|
||||
for user in db.rooms.room_members(&room?) {
|
||||
let user = user?;
|
||||
if user.server_name() != db.globals.server_name() {
|
||||
println!("Migration: Creating user {}", user);
|
||||
db.users.create(&user, None)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(4)?;
|
||||
|
||||
println!("Migration: 3 -> 4 finished");
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(4)?;
|
||||
if db.globals.database_version()? < 5 {
|
||||
// Upgrade user data store
|
||||
for (roomuserdataid, _) in db.account_data.roomuserdataid_accountdata.iter() {
|
||||
let mut parts = roomuserdataid.split(|&b| b == 0xff);
|
||||
let room_id = parts.next().unwrap();
|
||||
let user_id = parts.next().unwrap();
|
||||
let event_type = roomuserdataid.rsplit(|&b| b == 0xff).next().unwrap();
|
||||
|
||||
println!("Migration: 3 -> 4 finished");
|
||||
let mut key = room_id.to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(user_id);
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(event_type);
|
||||
|
||||
db.account_data
|
||||
.roomusertype_roomuserdataid
|
||||
.insert(&key, &roomuserdataid)?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(5)?;
|
||||
|
||||
println!("Migration: 4 -> 5 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 6 {
|
||||
// Set room member count
|
||||
for (roomid, _) in db.rooms.roomid_shortstatehash.iter() {
|
||||
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)?;
|
||||
|
||||
println!("Migration: 5 -> 6 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 7 {
|
||||
// Upgrade state store
|
||||
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();
|
||||
let mut counter = 0;
|
||||
|
||||
let mut handle_state =
|
||||
|current_sstatehash: u64,
|
||||
current_room: &RoomId,
|
||||
current_state: HashSet<_>,
|
||||
last_roomstates: &mut HashMap<_, _>| {
|
||||
counter += 1;
|
||||
println!("counter: {}", counter);
|
||||
let last_roomsstatehash = last_roomstates.get(current_room);
|
||||
|
||||
let states_parents = last_roomsstatehash.map_or_else(
|
||||
|| Ok(Vec::new()),
|
||||
|&last_roomsstatehash| {
|
||||
db.rooms.load_shortstatehash_info(dbg!(last_roomsstatehash))
|
||||
},
|
||||
)?;
|
||||
|
||||
let (statediffnew, statediffremoved) =
|
||||
if let Some(parent_stateinfo) = states_parents.last() {
|
||||
let statediffnew = current_state
|
||||
.difference(&parent_stateinfo.1)
|
||||
.cloned()
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let statediffremoved = parent_stateinfo
|
||||
.1
|
||||
.difference(¤t_state)
|
||||
.cloned()
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
(statediffnew, statediffremoved)
|
||||
} else {
|
||||
(current_state, HashSet::new())
|
||||
};
|
||||
|
||||
db.rooms.save_state_from_diff(
|
||||
dbg!(current_sstatehash),
|
||||
statediffnew,
|
||||
statediffremoved,
|
||||
2, // every state change is 2 event changes on average
|
||||
states_parents,
|
||||
)?;
|
||||
|
||||
/*
|
||||
let mut tmp = db.rooms.load_shortstatehash_info(¤t_sstatehash, &db)?;
|
||||
let state = tmp.pop().unwrap();
|
||||
println!(
|
||||
"{}\t{}{:?}: {:?} + {:?} - {:?}",
|
||||
current_room,
|
||||
" ".repeat(tmp.len()),
|
||||
utils::u64_from_bytes(¤t_sstatehash).unwrap(),
|
||||
tmp.last().map(|b| utils::u64_from_bytes(&b.0).unwrap()),
|
||||
state
|
||||
.2
|
||||
.iter()
|
||||
.map(|b| utils::u64_from_bytes(&b[size_of::<u64>()..]).unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
state
|
||||
.3
|
||||
.iter()
|
||||
.map(|b| utils::u64_from_bytes(&b[size_of::<u64>()..]).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
*/
|
||||
|
||||
Ok::<_, Error>(())
|
||||
};
|
||||
|
||||
for (k, seventid) in db._db.open_tree("stateid_shorteventid")?.iter() {
|
||||
let sstatehash = utils::u64_from_bytes(&k[0..size_of::<u64>()])
|
||||
.expect("number of bytes is correct");
|
||||
let sstatekey = k[size_of::<u64>()..].to_vec();
|
||||
if Some(sstatehash) != current_sstatehash {
|
||||
if let Some(current_sstatehash) = current_sstatehash {
|
||||
handle_state(
|
||||
current_sstatehash,
|
||||
current_room.as_ref().unwrap(),
|
||||
current_state,
|
||||
&mut last_roomstates,
|
||||
)?;
|
||||
last_roomstates
|
||||
.insert(current_room.clone().unwrap(), current_sstatehash);
|
||||
}
|
||||
current_state = HashSet::new();
|
||||
current_sstatehash = Some(sstatehash);
|
||||
|
||||
let event_id = db
|
||||
.rooms
|
||||
.shorteventid_eventid
|
||||
.get(&seventid)
|
||||
.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());
|
||||
}
|
||||
}
|
||||
|
||||
let mut val = sstatekey;
|
||||
val.extend_from_slice(&seventid);
|
||||
current_state.insert(val.try_into().expect("size is correct"));
|
||||
}
|
||||
|
||||
if let Some(current_sstatehash) = current_sstatehash {
|
||||
handle_state(
|
||||
current_sstatehash,
|
||||
current_room.as_ref().unwrap(),
|
||||
current_state,
|
||||
&mut last_roomstates,
|
||||
)?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(7)?;
|
||||
|
||||
println!("Migration: 6 -> 7 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 8 {
|
||||
// Generate short room ids for all rooms
|
||||
for (room_id, _) in db.rooms.roomid_shortstatehash.iter() {
|
||||
let shortroomid = db.globals.next_count()?.to_be_bytes();
|
||||
db.rooms.roomid_shortroomid.insert(&room_id, &shortroomid)?;
|
||||
println!("Migration: 8");
|
||||
}
|
||||
// Update pduids db layout
|
||||
let mut batch = db.rooms.pduid_pdu.iter().filter_map(|(key, v)| {
|
||||
if !key.starts_with(b"!") {
|
||||
return None;
|
||||
}
|
||||
let mut parts = key.splitn(2, |&b| b == 0xff);
|
||||
let room_id = parts.next().unwrap();
|
||||
let count = parts.next().unwrap();
|
||||
|
||||
let short_room_id = db
|
||||
.rooms
|
||||
.roomid_shortroomid
|
||||
.get(&room_id)
|
||||
.unwrap()
|
||||
.expect("shortroomid should exist");
|
||||
|
||||
let mut new_key = short_room_id;
|
||||
new_key.extend_from_slice(count);
|
||||
|
||||
Some((new_key, v))
|
||||
});
|
||||
|
||||
db.rooms.pduid_pdu.insert_batch(&mut batch)?;
|
||||
|
||||
let mut batch2 = db.rooms.eventid_pduid.iter().filter_map(|(k, value)| {
|
||||
if !value.starts_with(b"!") {
|
||||
return None;
|
||||
}
|
||||
let mut parts = value.splitn(2, |&b| b == 0xff);
|
||||
let room_id = parts.next().unwrap();
|
||||
let count = parts.next().unwrap();
|
||||
|
||||
let short_room_id = db
|
||||
.rooms
|
||||
.roomid_shortroomid
|
||||
.get(&room_id)
|
||||
.unwrap()
|
||||
.expect("shortroomid should exist");
|
||||
|
||||
let mut new_value = short_room_id;
|
||||
new_value.extend_from_slice(count);
|
||||
|
||||
Some((k, new_value))
|
||||
});
|
||||
|
||||
db.rooms.eventid_pduid.insert_batch(&mut batch2)?;
|
||||
|
||||
db.globals.bump_database_version(8)?;
|
||||
|
||||
println!("Migration: 7 -> 8 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 9 {
|
||||
// Update tokenids db layout
|
||||
let mut iter = db
|
||||
.rooms
|
||||
.tokenids
|
||||
.iter()
|
||||
.filter_map(|(key, _)| {
|
||||
if !key.starts_with(b"!") {
|
||||
return None;
|
||||
}
|
||||
let mut parts = key.splitn(4, |&b| b == 0xff);
|
||||
let room_id = parts.next().unwrap();
|
||||
let word = parts.next().unwrap();
|
||||
let _pdu_id_room = parts.next().unwrap();
|
||||
let pdu_id_count = parts.next().unwrap();
|
||||
|
||||
let short_room_id = db
|
||||
.rooms
|
||||
.roomid_shortroomid
|
||||
.get(&room_id)
|
||||
.unwrap()
|
||||
.expect("shortroomid should exist");
|
||||
let mut new_key = short_room_id;
|
||||
new_key.extend_from_slice(word);
|
||||
new_key.push(0xff);
|
||||
new_key.extend_from_slice(pdu_id_count);
|
||||
println!("old {:?}", key);
|
||||
println!("new {:?}", new_key);
|
||||
Some((new_key, Vec::new()))
|
||||
})
|
||||
.peekable();
|
||||
|
||||
while iter.peek().is_some() {
|
||||
db.rooms
|
||||
.tokenids
|
||||
.insert_batch(&mut iter.by_ref().take(1000))?;
|
||||
println!("smaller batch done");
|
||||
}
|
||||
|
||||
println!("Deleting starts");
|
||||
|
||||
let batch2 = db
|
||||
.rooms
|
||||
.tokenids
|
||||
.iter()
|
||||
.filter_map(|(key, _)| {
|
||||
if key.starts_with(b"!") {
|
||||
println!("del {:?}", key);
|
||||
Some(key)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for key in batch2 {
|
||||
println!("del");
|
||||
db.rooms.tokenids.remove(&key)?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(9)?;
|
||||
|
||||
println!("Migration: 8 -> 9 finished");
|
||||
}
|
||||
|
||||
if db.globals.database_version()? < 10 {
|
||||
// Add other direction for shortstatekeys
|
||||
for (statekey, shortstatekey) in db.rooms.statekey_shortstatekey.iter() {
|
||||
db.rooms
|
||||
.shortstatekey_statekey
|
||||
.insert(&shortstatekey, &statekey)?;
|
||||
}
|
||||
|
||||
// Force E2EE device list updates so we can send them over federation
|
||||
for user_id in db.users.iter().filter_map(|r| r.ok()) {
|
||||
db.users
|
||||
.mark_device_key_update(&user_id, &db.rooms, &db.globals)?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(10)?;
|
||||
|
||||
println!("Migration: 9 -> 10 finished");
|
||||
}
|
||||
}
|
||||
|
||||
// This data is probably outdated
|
||||
db.rooms.edus.presenceid_presence.clear()?;
|
||||
let guard = db.read().await;
|
||||
|
||||
db.admin.start_handler(Arc::clone(&db), admin_receiver);
|
||||
db.sending.start_handler(Arc::clone(&db), sending_receiver);
|
||||
// This data is probably outdated
|
||||
guard.rooms.edus.presenceid_presence.clear()?;
|
||||
|
||||
guard.admin.start_handler(Arc::clone(&db), admin_receiver);
|
||||
guard
|
||||
.sending
|
||||
.start_handler(Arc::clone(&db), sending_receiver);
|
||||
|
||||
drop(guard);
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
{
|
||||
Self::start_wal_clean_task(Arc::clone(&db), &config).await;
|
||||
}
|
||||
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
pub async fn start_on_shutdown_tasks(db: Arc<TokioRwLock<Self>>, shutdown: Shutdown) {
|
||||
use tracing::info;
|
||||
|
||||
tokio::spawn(async move {
|
||||
shutdown.await;
|
||||
|
||||
info!(target: "shutdown-sync", "Received shutdown notification, notifying sync helpers...");
|
||||
|
||||
db.read().await.globals.rotate.fire();
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn watch(&self, user_id: &UserId, device_id: &DeviceId) {
|
||||
let userid_bytes = user_id.as_bytes().to_vec();
|
||||
let mut userid_prefix = userid_bytes.clone();
|
||||
@@ -355,6 +797,16 @@ impl Database {
|
||||
.watch_prefix(&userid_prefix),
|
||||
);
|
||||
futures.push(self.rooms.userroomid_leftstate.watch_prefix(&userid_prefix));
|
||||
futures.push(
|
||||
self.rooms
|
||||
.userroomid_notificationcount
|
||||
.watch_prefix(&userid_prefix),
|
||||
);
|
||||
futures.push(
|
||||
self.rooms
|
||||
.userroomid_highlightcount
|
||||
.watch_prefix(&userid_prefix),
|
||||
);
|
||||
|
||||
// Events for rooms we are in
|
||||
for room_id in self.rooms.rooms_joined(user_id).filter_map(|r| r.ok()) {
|
||||
@@ -389,7 +841,7 @@ impl Database {
|
||||
|
||||
futures.push(
|
||||
self.account_data
|
||||
.roomuserdataid_accountdata
|
||||
.roomusertype_roomuserdataid
|
||||
.watch_prefix(&roomuser_prefix),
|
||||
);
|
||||
}
|
||||
@@ -399,7 +851,7 @@ impl Database {
|
||||
|
||||
futures.push(
|
||||
self.account_data
|
||||
.roomuserdataid_accountdata
|
||||
.roomusertype_roomuserdataid
|
||||
.watch_prefix(&globaluserdata_prefix),
|
||||
);
|
||||
|
||||
@@ -413,13 +865,97 @@ impl Database {
|
||||
.watch_prefix(&userid_bytes),
|
||||
);
|
||||
|
||||
futures.push(Box::pin(self.globals.rotate.watch()));
|
||||
|
||||
// Wait until one of them finds something
|
||||
futures.next().await;
|
||||
}
|
||||
|
||||
pub async fn flush(&self) -> Result<()> {
|
||||
// noop while we don't use sled 1.0
|
||||
//self._db.flush_async().await?;
|
||||
Ok(())
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn flush(&self) -> Result<()> {
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let res = self._db.flush();
|
||||
|
||||
debug!("flush: took {:?}", start.elapsed());
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn flush_wal(&self) -> Result<()> {
|
||||
self._db.flush_wal()
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[tracing::instrument(skip(db, config))]
|
||||
pub async fn start_wal_clean_task(db: Arc<TokioRwLock<Self>>, config: &Config) {
|
||||
use tokio::time::interval;
|
||||
|
||||
#[cfg(unix)]
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
use tracing::info;
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
let timer_interval = Duration::from_secs(config.sqlite_wal_clean_second_interval as u64);
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut i = interval(timer_interval);
|
||||
#[cfg(unix)]
|
||||
let mut s = signal(SignalKind::hangup()).unwrap();
|
||||
|
||||
loop {
|
||||
#[cfg(unix)]
|
||||
tokio::select! {
|
||||
_ = i.tick() => {
|
||||
info!("wal-trunc: Timer ticked");
|
||||
}
|
||||
_ = s.recv() => {
|
||||
info!("wal-trunc: Received SIGHUP");
|
||||
}
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
i.tick().await;
|
||||
info!("wal-trunc: Timer ticked")
|
||||
}
|
||||
|
||||
let start = Instant::now();
|
||||
if let Err(e) = db.read().await.flush_wal() {
|
||||
error!("wal-trunc: Errored: {}", e);
|
||||
} else {
|
||||
info!("wal-trunc: Flushed in {:?}", start.elapsed());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DatabaseGuard(OwnedRwLockReadGuard<Database>);
|
||||
|
||||
impl Deref for DatabaseGuard {
|
||||
type Target = OwnedRwLockReadGuard<Database>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for DatabaseGuard {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> rocket::request::Outcome<Self, ()> {
|
||||
let db = try_outcome!(req.guard::<&State<Arc<TokioRwLock<Database>>>>().await);
|
||||
|
||||
Ok(DatabaseGuard(Arc::clone(&db).read_owned().await)).or_forward(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OwnedRwLockReadGuard<Database>> for DatabaseGuard {
|
||||
fn from(val: OwnedRwLockReadGuard<Database>) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
}
|
||||
|
||||
+14
-289
@@ -1,51 +1,46 @@
|
||||
use super::Config;
|
||||
use crate::{utils, Result};
|
||||
use log::warn;
|
||||
use crate::Result;
|
||||
|
||||
use std::{future::Future, pin::Pin, sync::Arc};
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
use std::{collections::BTreeMap, sync::RwLock};
|
||||
|
||||
#[cfg(feature = "sled")]
|
||||
pub struct SledEngine(sled::Db);
|
||||
#[cfg(feature = "sled")]
|
||||
pub struct SledEngineTree(sled::Tree);
|
||||
pub mod sled;
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
pub struct RocksDbEngine(rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>);
|
||||
#[cfg(feature = "rocksdb")]
|
||||
pub struct RocksDbEngineTree<'a> {
|
||||
db: Arc<RocksDbEngine>,
|
||||
name: &'a str,
|
||||
watchers: RwLock<BTreeMap<Vec<u8>, Vec<tokio::sync::oneshot::Sender<()>>>>,
|
||||
}
|
||||
#[cfg(feature = "sqlite")]
|
||||
pub mod sqlite;
|
||||
|
||||
#[cfg(feature = "heed")]
|
||||
pub mod heed;
|
||||
|
||||
pub trait DatabaseEngine: Sized {
|
||||
fn open(config: &Config) -> Result<Arc<Self>>;
|
||||
fn open_tree(self: &Arc<Self>, name: &'static str) -> Result<Arc<dyn Tree>>;
|
||||
fn flush(self: &Arc<Self>) -> Result<()>;
|
||||
}
|
||||
|
||||
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<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>;
|
||||
|
||||
fn remove(&self, key: &[u8]) -> Result<()>;
|
||||
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + Sync + 'a>;
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
|
||||
|
||||
fn iter_from<'a>(
|
||||
&'a self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a>;
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
|
||||
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>>;
|
||||
fn increment_batch<'a>(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>;
|
||||
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + 'a>;
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
|
||||
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
|
||||
@@ -57,273 +52,3 @@ pub trait Tree: Send + Sync {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "sled")]
|
||||
impl DatabaseEngine for SledEngine {
|
||||
fn open(config: &Config) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(SledEngine(
|
||||
sled::Config::default()
|
||||
.path(&config.database_path)
|
||||
.cache_capacity(config.cache_capacity as u64)
|
||||
.use_compression(true)
|
||||
.open()?,
|
||||
)))
|
||||
}
|
||||
|
||||
fn open_tree(self: &Arc<Self>, name: &'static str) -> Result<Arc<dyn Tree>> {
|
||||
Ok(Arc::new(SledEngineTree(self.0.open_tree(name)?)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "sled")]
|
||||
impl Tree for SledEngineTree {
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
Ok(self.0.get(key)?.map(|v| v.to_vec()))
|
||||
}
|
||||
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
self.0.insert(key, value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
self.0.remove(key)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + Sync + 'a> {
|
||||
Box::new(
|
||||
self.0
|
||||
.iter()
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into())),
|
||||
)
|
||||
}
|
||||
|
||||
fn iter_from(
|
||||
&self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)>> {
|
||||
let iter = if backwards {
|
||||
self.0.range(..from)
|
||||
} else {
|
||||
self.0.range(from..)
|
||||
};
|
||||
|
||||
let iter = iter
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into()));
|
||||
|
||||
if backwards {
|
||||
Box::new(iter.rev())
|
||||
} else {
|
||||
Box::new(iter)
|
||||
}
|
||||
}
|
||||
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
Ok(self
|
||||
.0
|
||||
.update_and_fetch(key, utils::increment)
|
||||
.map(|o| o.expect("increment always sets a value").to_vec())?)
|
||||
}
|
||||
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + 'a> {
|
||||
let iter = self
|
||||
.0
|
||||
.scan_prefix(prefix)
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into()));
|
||||
|
||||
Box::new(iter)
|
||||
}
|
||||
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let prefix = prefix.to_vec();
|
||||
Box::pin(async move {
|
||||
self.0.watch_prefix(prefix).await;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
impl DatabaseEngine for RocksDbEngine {
|
||||
fn open(config: &Config) -> Result<Arc<Self>> {
|
||||
let mut db_opts = rocksdb::Options::default();
|
||||
db_opts.create_if_missing(true);
|
||||
db_opts.set_max_open_files(16);
|
||||
db_opts.set_compaction_style(rocksdb::DBCompactionStyle::Level);
|
||||
db_opts.set_compression_type(rocksdb::DBCompressionType::Snappy);
|
||||
db_opts.set_target_file_size_base(256 << 20);
|
||||
db_opts.set_write_buffer_size(256 << 20);
|
||||
|
||||
let mut block_based_options = rocksdb::BlockBasedOptions::default();
|
||||
block_based_options.set_block_size(512 << 10);
|
||||
db_opts.set_block_based_table_factory(&block_based_options);
|
||||
|
||||
let cfs = rocksdb::DBWithThreadMode::<rocksdb::MultiThreaded>::list_cf(
|
||||
&db_opts,
|
||||
&config.database_path,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut options = rocksdb::Options::default();
|
||||
options.set_merge_operator_associative("increment", utils::increment_rocksdb);
|
||||
|
||||
let db = rocksdb::DBWithThreadMode::<rocksdb::MultiThreaded>::open_cf_descriptors(
|
||||
&db_opts,
|
||||
&config.database_path,
|
||||
cfs.iter()
|
||||
.map(|name| rocksdb::ColumnFamilyDescriptor::new(name, options.clone())),
|
||||
)?;
|
||||
|
||||
Ok(Arc::new(RocksDbEngine(db)))
|
||||
}
|
||||
|
||||
fn open_tree(self: &Arc<Self>, name: &'static str) -> Result<Arc<dyn Tree>> {
|
||||
let mut options = rocksdb::Options::default();
|
||||
options.set_merge_operator_associative("increment", utils::increment_rocksdb);
|
||||
|
||||
// Create if it doesn't exist
|
||||
let _ = self.0.create_cf(name, &options);
|
||||
|
||||
Ok(Arc::new(RocksDbEngineTree {
|
||||
name,
|
||||
db: Arc::clone(self),
|
||||
watchers: RwLock::new(BTreeMap::new()),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
impl RocksDbEngineTree<'_> {
|
||||
fn cf(&self) -> rocksdb::BoundColumnFamily<'_> {
|
||||
self.db.0.cf_handle(self.name).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
impl Tree for RocksDbEngineTree<'_> {
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
Ok(self.db.0.get_cf(self.cf(), key)?)
|
||||
}
|
||||
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
let watchers = self.watchers.read().unwrap();
|
||||
let mut triggered = Vec::new();
|
||||
|
||||
for length in 0..=key.len() {
|
||||
if watchers.contains_key(&key[..length]) {
|
||||
triggered.push(&key[..length]);
|
||||
}
|
||||
}
|
||||
|
||||
drop(watchers);
|
||||
|
||||
if !triggered.is_empty() {
|
||||
let mut watchers = self.watchers.write().unwrap();
|
||||
for prefix in triggered {
|
||||
if let Some(txs) = watchers.remove(prefix) {
|
||||
for tx in txs {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.db.0.put_cf(self.cf(), key, value)?)
|
||||
}
|
||||
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
Ok(self.db.0.delete_cf(self.cf(), key)?)
|
||||
}
|
||||
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + Sync + 'a> {
|
||||
Box::new(
|
||||
self.db
|
||||
.0
|
||||
.iterator_cf(self.cf(), rocksdb::IteratorMode::Start),
|
||||
)
|
||||
}
|
||||
|
||||
fn iter_from<'a>(
|
||||
&'a self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a> {
|
||||
Box::new(self.db.0.iterator_cf(
|
||||
self.cf(),
|
||||
rocksdb::IteratorMode::From(
|
||||
from,
|
||||
if backwards {
|
||||
rocksdb::Direction::Reverse
|
||||
} else {
|
||||
rocksdb::Direction::Forward
|
||||
},
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
let stats = rocksdb::perf::get_memory_usage_stats(Some(&[&self.db.0]), None).unwrap();
|
||||
dbg!(stats.mem_table_total);
|
||||
dbg!(stats.mem_table_unflushed);
|
||||
dbg!(stats.mem_table_readers_total);
|
||||
dbg!(stats.cache_total);
|
||||
// TODO: atomic?
|
||||
let old = self.get(key)?;
|
||||
let new = utils::increment(old.as_deref()).unwrap();
|
||||
self.insert(key, &new)?;
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + Send + 'a> {
|
||||
Box::new(
|
||||
self.db
|
||||
.0
|
||||
.iterator_cf(
|
||||
self.cf(),
|
||||
rocksdb::IteratorMode::From(&prefix, rocksdb::Direction::Forward),
|
||||
)
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix)),
|
||||
)
|
||||
}
|
||||
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
self.watchers
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(prefix.to_vec())
|
||||
.or_default()
|
||||
.push(tx);
|
||||
|
||||
Box::pin(async move {
|
||||
// Tx is never destroyed
|
||||
rx.await.unwrap();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
use super::super::Config;
|
||||
use crossbeam::channel::{bounded, Sender as ChannelSender};
|
||||
use threadpool::ThreadPool;
|
||||
|
||||
use crate::{Error, Result};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
use tokio::sync::oneshot::Sender;
|
||||
|
||||
use super::{DatabaseEngine, Tree};
|
||||
|
||||
type TupleOfBytes = (Vec<u8>, Vec<u8>);
|
||||
|
||||
pub struct Engine {
|
||||
env: heed::Env,
|
||||
iter_pool: Mutex<ThreadPool>,
|
||||
}
|
||||
|
||||
pub struct EngineTree {
|
||||
engine: Arc<Engine>,
|
||||
tree: Arc<heed::UntypedDatabase>,
|
||||
watchers: RwLock<HashMap<Vec<u8>, Vec<Sender<()>>>>,
|
||||
}
|
||||
|
||||
fn convert_error(error: heed::Error) -> Error {
|
||||
Error::HeedError {
|
||||
error: error.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
impl DatabaseEngine for Engine {
|
||||
fn open(config: &Config) -> Result<Arc<Self>> {
|
||||
let mut env_builder = heed::EnvOpenOptions::new();
|
||||
env_builder.map_size(1024 * 1024 * 1024 * 1024); // 1 Terabyte
|
||||
env_builder.max_readers(126);
|
||||
env_builder.max_dbs(128);
|
||||
unsafe {
|
||||
env_builder.flag(heed::flags::Flags::MdbWriteMap);
|
||||
env_builder.flag(heed::flags::Flags::MdbMapAsync);
|
||||
}
|
||||
|
||||
Ok(Arc::new(Engine {
|
||||
env: env_builder
|
||||
.open(&config.database_path)
|
||||
.map_err(convert_error)?,
|
||||
iter_pool: Mutex::new(ThreadPool::new(10)),
|
||||
}))
|
||||
}
|
||||
|
||||
fn open_tree(self: &Arc<Self>, name: &'static str) -> Result<Arc<dyn Tree>> {
|
||||
// Creates the db if it doesn't exist already
|
||||
Ok(Arc::new(EngineTree {
|
||||
engine: Arc::clone(self),
|
||||
tree: Arc::new(
|
||||
self.env
|
||||
.create_database(Some(name))
|
||||
.map_err(convert_error)?,
|
||||
),
|
||||
watchers: RwLock::new(HashMap::new()),
|
||||
}))
|
||||
}
|
||||
|
||||
fn flush(self: &Arc<Self>) -> Result<()> {
|
||||
self.env.force_sync().map_err(convert_error)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl EngineTree {
|
||||
#[tracing::instrument(skip(self, tree, from, backwards))]
|
||||
fn iter_from_thread(
|
||||
&self,
|
||||
tree: Arc<heed::UntypedDatabase>,
|
||||
from: Vec<u8>,
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = TupleOfBytes> + Send + Sync> {
|
||||
let (s, r) = bounded::<TupleOfBytes>(100);
|
||||
let engine = Arc::clone(&self.engine);
|
||||
|
||||
let lock = self.engine.iter_pool.lock().await;
|
||||
if lock.active_count() < lock.max_count() {
|
||||
lock.execute(move || {
|
||||
iter_from_thread_work(tree, &engine.env.read_txn().unwrap(), from, backwards, &s);
|
||||
});
|
||||
} else {
|
||||
std::thread::spawn(move || {
|
||||
iter_from_thread_work(tree, &engine.env.read_txn().unwrap(), from, backwards, &s);
|
||||
});
|
||||
}
|
||||
|
||||
Box::new(r.into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(tree, txn, from, backwards))]
|
||||
fn iter_from_thread_work(
|
||||
tree: Arc<heed::UntypedDatabase>,
|
||||
txn: &heed::RoTxn<'_>,
|
||||
from: Vec<u8>,
|
||||
backwards: bool,
|
||||
s: &ChannelSender<(Vec<u8>, Vec<u8>)>,
|
||||
) {
|
||||
if backwards {
|
||||
for (k, v) in tree.rev_range(txn, ..=&*from).unwrap().map(|r| r.unwrap()) {
|
||||
if s.send((k.to_vec(), v.to_vec())).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if from.is_empty() {
|
||||
for (k, v) in tree.iter(txn).unwrap().map(|r| r.unwrap()) {
|
||||
if s.send((k.to_vec(), v.to_vec())).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (k, v) in tree.range(txn, &*from..).unwrap().map(|r| r.unwrap()) {
|
||||
if s.send((k.to_vec(), v.to_vec())).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree for EngineTree {
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
let txn = self.engine.env.read_txn().map_err(convert_error)?;
|
||||
Ok(self
|
||||
.tree
|
||||
.get(&txn, &key)
|
||||
.map_err(convert_error)?
|
||||
.map(|s| s.to_vec()))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key, value))]
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
let mut txn = self.engine.env.write_txn().map_err(convert_error)?;
|
||||
self.tree
|
||||
.put(&mut txn, &key, &value)
|
||||
.map_err(convert_error)?;
|
||||
txn.commit().map_err(convert_error)?;
|
||||
|
||||
let watchers = self.watchers.read().unwrap();
|
||||
let mut triggered = Vec::new();
|
||||
|
||||
for length in 0..=key.len() {
|
||||
if watchers.contains_key(&key[..length]) {
|
||||
triggered.push(&key[..length]);
|
||||
}
|
||||
}
|
||||
|
||||
drop(watchers);
|
||||
|
||||
if !triggered.is_empty() {
|
||||
let mut watchers = self.watchers.write().unwrap();
|
||||
for prefix in triggered {
|
||||
if let Some(txs) = watchers.remove(prefix) {
|
||||
for tx in txs {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
let mut txn = self.engine.env.write_txn().map_err(convert_error)?;
|
||||
self.tree.delete(&mut txn, &key).map_err(convert_error)?;
|
||||
txn.commit().map_err(convert_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + Send + 'a> {
|
||||
self.iter_from(&[], false)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, from, backwards))]
|
||||
fn iter_from(
|
||||
&self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + Send> {
|
||||
self.iter_from_thread(Arc::clone(&self.tree), from.to_vec(), backwards)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut txn = self.engine.env.write_txn().map_err(convert_error)?;
|
||||
|
||||
let old = self.tree.get(&txn, &key).map_err(convert_error)?;
|
||||
let new =
|
||||
crate::utils::increment(old.as_deref()).expect("utils::increment always returns Some");
|
||||
|
||||
self.tree
|
||||
.put(&mut txn, &key, &&*new)
|
||||
.map_err(convert_error)?;
|
||||
|
||||
txn.commit().map_err(convert_error)?;
|
||||
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + Send + 'a> {
|
||||
Box::new(
|
||||
self.iter_from(&prefix, false)
|
||||
.take_while(move |(key, _)| key.starts_with(&prefix)),
|
||||
)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
self.watchers
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(prefix.to_vec())
|
||||
.or_default()
|
||||
.push(tx);
|
||||
|
||||
Box::pin(async move {
|
||||
// Tx is never destroyed
|
||||
rx.await.unwrap();
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
use super::super::Config;
|
||||
use crate::{utils, Result};
|
||||
use std::{future::Future, pin::Pin, sync::Arc};
|
||||
use tracing::warn;
|
||||
|
||||
use super::{DatabaseEngine, Tree};
|
||||
|
||||
pub struct Engine(sled::Db);
|
||||
|
||||
pub struct SledEngineTree(sled::Tree);
|
||||
|
||||
impl DatabaseEngine for Engine {
|
||||
fn open(config: &Config) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Engine(
|
||||
sled::Config::default()
|
||||
.path(&config.database_path)
|
||||
.cache_capacity((config.db_cache_capacity_mb * 1024.0 * 1024.0) as u64)
|
||||
.use_compression(true)
|
||||
.open()?,
|
||||
)))
|
||||
}
|
||||
|
||||
fn open_tree(self: &Arc<Self>, name: &'static str) -> Result<Arc<dyn Tree>> {
|
||||
Ok(Arc::new(SledEngineTree(self.0.open_tree(name)?)))
|
||||
}
|
||||
|
||||
fn flush(self: &Arc<Self>) -> Result<()> {
|
||||
Ok(()) // noop
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree for SledEngineTree {
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
Ok(self.0.get(key)?.map(|v| v.to_vec()))
|
||||
}
|
||||
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
self.0.insert(key, value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, iter))]
|
||||
fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
|
||||
for (key, value) in iter {
|
||||
self.0.insert(key, value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
self.0.remove(key)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> {
|
||||
Box::new(
|
||||
self.0
|
||||
.iter()
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into())),
|
||||
)
|
||||
}
|
||||
|
||||
fn iter_from(
|
||||
&self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)>> {
|
||||
let iter = if backwards {
|
||||
self.0.range(..=from)
|
||||
} else {
|
||||
self.0.range(from..)
|
||||
};
|
||||
|
||||
let iter = iter
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into()));
|
||||
|
||||
if backwards {
|
||||
Box::new(iter.rev())
|
||||
} else {
|
||||
Box::new(iter)
|
||||
}
|
||||
}
|
||||
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
Ok(self
|
||||
.0
|
||||
.update_and_fetch(key, utils::increment)
|
||||
.map(|o| o.expect("increment always sets a value").to_vec())?)
|
||||
}
|
||||
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> {
|
||||
let iter = self
|
||||
.0
|
||||
.scan_prefix(prefix)
|
||||
.filter_map(|r| {
|
||||
if let Err(e) = &r {
|
||||
warn!("Error: {}", e);
|
||||
}
|
||||
r.ok()
|
||||
})
|
||||
.map(|(k, v)| (k.to_vec().into(), v.to_vec().into()));
|
||||
|
||||
Box::new(iter)
|
||||
}
|
||||
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let prefix = prefix.to_vec();
|
||||
Box::pin(async move {
|
||||
self.0.watch_prefix(prefix).await;
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
use super::{DatabaseEngine, Tree};
|
||||
use crate::{database::Config, Result};
|
||||
use parking_lot::{Mutex, MutexGuard, RwLock};
|
||||
use rusqlite::{Connection, DatabaseName::Main, OptionalExtension};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
path::{Path, PathBuf},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
};
|
||||
use thread_local::ThreadLocal;
|
||||
use tokio::sync::oneshot::Sender;
|
||||
use tracing::debug;
|
||||
|
||||
thread_local! {
|
||||
static READ_CONNECTION: RefCell<Option<&'static Connection>> = RefCell::new(None);
|
||||
static READ_CONNECTION_ITERATOR: RefCell<Option<&'static Connection>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
struct PreparedStatementIterator<'a> {
|
||||
pub iterator: Box<dyn Iterator<Item = TupleOfBytes> + 'a>,
|
||||
pub statement_ref: NonAliasingBox<rusqlite::Statement<'a>>,
|
||||
}
|
||||
|
||||
impl Iterator for PreparedStatementIterator<'_> {
|
||||
type Item = TupleOfBytes;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iterator.next()
|
||||
}
|
||||
}
|
||||
|
||||
struct NonAliasingBox<T>(*mut T);
|
||||
impl<T> Drop for NonAliasingBox<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe { Box::from_raw(self.0) };
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
writer: Mutex<Connection>,
|
||||
read_conn_tls: ThreadLocal<Connection>,
|
||||
read_iterator_conn_tls: ThreadLocal<Connection>,
|
||||
|
||||
path: PathBuf,
|
||||
cache_size_per_thread: u32,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
fn prepare_conn(path: &Path, cache_size_kb: u32) -> Result<Connection> {
|
||||
let conn = Connection::open(&path)?;
|
||||
|
||||
conn.pragma_update(Some(Main), "page_size", &2048)?;
|
||||
conn.pragma_update(Some(Main), "journal_mode", &"WAL")?;
|
||||
conn.pragma_update(Some(Main), "synchronous", &"NORMAL")?;
|
||||
conn.pragma_update(Some(Main), "cache_size", &(-i64::from(cache_size_kb)))?;
|
||||
conn.pragma_update(Some(Main), "wal_autocheckpoint", &2000)?;
|
||||
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
fn write_lock(&self) -> MutexGuard<'_, Connection> {
|
||||
self.writer.lock()
|
||||
}
|
||||
|
||||
fn read_lock(&self) -> &Connection {
|
||||
self.read_conn_tls
|
||||
.get_or(|| Self::prepare_conn(&self.path, self.cache_size_per_thread).unwrap())
|
||||
}
|
||||
|
||||
fn read_lock_iterator(&self) -> &Connection {
|
||||
self.read_iterator_conn_tls
|
||||
.get_or(|| Self::prepare_conn(&self.path, self.cache_size_per_thread).unwrap())
|
||||
}
|
||||
|
||||
pub fn flush_wal(self: &Arc<Self>) -> Result<()> {
|
||||
self.write_lock()
|
||||
.pragma_update(Some(Main), "wal_checkpoint", &"TRUNCATE")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl DatabaseEngine for Engine {
|
||||
fn open(config: &Config) -> Result<Arc<Self>> {
|
||||
let path = Path::new(&config.database_path).join("conduit.db");
|
||||
|
||||
// calculates cache-size per permanent connection
|
||||
// 1. convert MB to KiB
|
||||
// 2. divide by permanent connections + permanent iter connections + write connection
|
||||
// 3. round down to nearest integer
|
||||
let cache_size_per_thread: u32 = ((config.db_cache_capacity_mb * 1024.0)
|
||||
/ ((num_cpus::get().max(1) * 2) + 1) as f64)
|
||||
as u32;
|
||||
|
||||
let writer = Mutex::new(Self::prepare_conn(&path, cache_size_per_thread)?);
|
||||
|
||||
let arc = Arc::new(Engine {
|
||||
writer,
|
||||
read_conn_tls: ThreadLocal::new(),
|
||||
read_iterator_conn_tls: ThreadLocal::new(),
|
||||
path,
|
||||
cache_size_per_thread,
|
||||
});
|
||||
|
||||
Ok(arc)
|
||||
}
|
||||
|
||||
fn open_tree(self: &Arc<Self>, name: &str) -> Result<Arc<dyn Tree>> {
|
||||
self.write_lock().execute(&format!("CREATE TABLE IF NOT EXISTS {} ( \"key\" BLOB PRIMARY KEY, \"value\" BLOB NOT NULL )", name), [])?;
|
||||
|
||||
Ok(Arc::new(SqliteTable {
|
||||
engine: Arc::clone(self),
|
||||
name: name.to_owned(),
|
||||
watchers: RwLock::new(HashMap::new()),
|
||||
}))
|
||||
}
|
||||
|
||||
fn flush(self: &Arc<Self>) -> Result<()> {
|
||||
// we enabled PRAGMA synchronous=normal, so this should not be necessary
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SqliteTable {
|
||||
engine: Arc<Engine>,
|
||||
name: String,
|
||||
watchers: RwLock<HashMap<Vec<u8>, Vec<Sender<()>>>>,
|
||||
}
|
||||
|
||||
type TupleOfBytes = (Vec<u8>, Vec<u8>);
|
||||
|
||||
impl SqliteTable {
|
||||
#[tracing::instrument(skip(self, guard, key))]
|
||||
fn get_with_guard(&self, guard: &Connection, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
//dbg!(&self.name);
|
||||
Ok(guard
|
||||
.prepare(format!("SELECT value FROM {} WHERE key = ?", self.name).as_str())?
|
||||
.query_row([key], |row| row.get(0))
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, guard, key, value))]
|
||||
fn insert_with_guard(&self, guard: &Connection, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
//dbg!(&self.name);
|
||||
guard.execute(
|
||||
format!(
|
||||
"INSERT OR REPLACE INTO {} (key, value) VALUES (?, ?)",
|
||||
self.name
|
||||
)
|
||||
.as_str(),
|
||||
[key, value],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn iter_with_guard<'a>(
|
||||
&'a self,
|
||||
guard: &'a Connection,
|
||||
) -> Box<dyn Iterator<Item = TupleOfBytes> + 'a> {
|
||||
let statement = Box::leak(Box::new(
|
||||
guard
|
||||
.prepare(&format!(
|
||||
"SELECT key, value FROM {} ORDER BY key ASC",
|
||||
&self.name
|
||||
))
|
||||
.unwrap(),
|
||||
));
|
||||
|
||||
let statement_ref = NonAliasingBox(statement);
|
||||
|
||||
//let name = self.name.clone();
|
||||
|
||||
let iterator = Box::new(
|
||||
statement
|
||||
.query_map([], |row| Ok((row.get_unwrap(0), row.get_unwrap(1))))
|
||||
.unwrap()
|
||||
.map(move |r| {
|
||||
//dbg!(&name);
|
||||
r.unwrap()
|
||||
}),
|
||||
);
|
||||
|
||||
Box::new(PreparedStatementIterator {
|
||||
iterator,
|
||||
statement_ref,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree for SqliteTable {
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
self.get_with_guard(&self.engine.read_lock(), key)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key, value))]
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
let guard = self.engine.write_lock();
|
||||
self.insert_with_guard(&guard, key, value)?;
|
||||
drop(guard);
|
||||
|
||||
let watchers = self.watchers.read();
|
||||
let mut triggered = Vec::new();
|
||||
|
||||
for length in 0..=key.len() {
|
||||
if watchers.contains_key(&key[..length]) {
|
||||
triggered.push(&key[..length]);
|
||||
}
|
||||
}
|
||||
|
||||
drop(watchers);
|
||||
|
||||
if !triggered.is_empty() {
|
||||
let mut watchers = self.watchers.write();
|
||||
for prefix in triggered {
|
||||
if let Some(txs) = watchers.remove(prefix) {
|
||||
for tx in txs {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, iter))]
|
||||
fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
|
||||
let guard = self.engine.write_lock();
|
||||
|
||||
guard.execute("BEGIN", [])?;
|
||||
for (key, value) in iter {
|
||||
self.insert_with_guard(&guard, &key, &value)?;
|
||||
}
|
||||
guard.execute("COMMIT", [])?;
|
||||
|
||||
drop(guard);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, iter))]
|
||||
fn increment_batch<'a>(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()> {
|
||||
let guard = self.engine.write_lock();
|
||||
|
||||
guard.execute("BEGIN", [])?;
|
||||
for key in iter {
|
||||
let old = self.get_with_guard(&guard, &key)?;
|
||||
let new = crate::utils::increment(old.as_deref())
|
||||
.expect("utils::increment always returns Some");
|
||||
self.insert_with_guard(&guard, &key, &new)?;
|
||||
}
|
||||
guard.execute("COMMIT", [])?;
|
||||
|
||||
drop(guard);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
let guard = self.engine.write_lock();
|
||||
|
||||
guard.execute(
|
||||
format!("DELETE FROM {} WHERE key = ?", self.name).as_str(),
|
||||
[key],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = TupleOfBytes> + 'a> {
|
||||
let guard = self.engine.read_lock_iterator();
|
||||
|
||||
self.iter_with_guard(&guard)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, from, backwards))]
|
||||
fn iter_from<'a>(
|
||||
&'a self,
|
||||
from: &[u8],
|
||||
backwards: bool,
|
||||
) -> Box<dyn Iterator<Item = TupleOfBytes> + 'a> {
|
||||
let guard = self.engine.read_lock_iterator();
|
||||
let from = from.to_vec(); // TODO change interface?
|
||||
|
||||
//let name = self.name.clone();
|
||||
|
||||
if backwards {
|
||||
let statement = Box::leak(Box::new(
|
||||
guard
|
||||
.prepare(&format!(
|
||||
"SELECT key, value FROM {} WHERE key <= ? ORDER BY key DESC",
|
||||
&self.name
|
||||
))
|
||||
.unwrap(),
|
||||
));
|
||||
|
||||
let statement_ref = NonAliasingBox(statement);
|
||||
|
||||
let iterator = Box::new(
|
||||
statement
|
||||
.query_map([from], |row| Ok((row.get_unwrap(0), row.get_unwrap(1))))
|
||||
.unwrap()
|
||||
.map(move |r| {
|
||||
//dbg!(&name);
|
||||
r.unwrap()
|
||||
}),
|
||||
);
|
||||
Box::new(PreparedStatementIterator {
|
||||
iterator,
|
||||
statement_ref,
|
||||
})
|
||||
} else {
|
||||
let statement = Box::leak(Box::new(
|
||||
guard
|
||||
.prepare(&format!(
|
||||
"SELECT key, value FROM {} WHERE key >= ? ORDER BY key ASC",
|
||||
&self.name
|
||||
))
|
||||
.unwrap(),
|
||||
));
|
||||
|
||||
let statement_ref = NonAliasingBox(statement);
|
||||
|
||||
let iterator = Box::new(
|
||||
statement
|
||||
.query_map([from], |row| Ok((row.get_unwrap(0), row.get_unwrap(1))))
|
||||
.unwrap()
|
||||
.map(move |r| {
|
||||
//dbg!(&name);
|
||||
r.unwrap()
|
||||
}),
|
||||
);
|
||||
|
||||
Box::new(PreparedStatementIterator {
|
||||
iterator,
|
||||
statement_ref,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
let guard = self.engine.write_lock();
|
||||
|
||||
let old = self.get_with_guard(&guard, key)?;
|
||||
|
||||
let new =
|
||||
crate::utils::increment(old.as_deref()).expect("utils::increment always returns Some");
|
||||
|
||||
self.insert_with_guard(&guard, key, &new)?;
|
||||
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn scan_prefix<'a>(&'a self, prefix: Vec<u8>) -> Box<dyn Iterator<Item = TupleOfBytes> + 'a> {
|
||||
Box::new(
|
||||
self.iter_from(&prefix, false)
|
||||
.take_while(move |(key, _)| key.starts_with(&prefix)),
|
||||
)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
self.watchers
|
||||
.write()
|
||||
.entry(prefix.to_vec())
|
||||
.or_default()
|
||||
.push(tx);
|
||||
|
||||
Box::pin(async move {
|
||||
// Tx is never destroyed
|
||||
rx.await.unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn clear(&self) -> Result<()> {
|
||||
debug!("clear: running");
|
||||
self.engine
|
||||
.write_lock()
|
||||
.execute(format!("DELETE FROM {}", self.name).as_str(), [])?;
|
||||
debug!("clear: ran");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,12 @@ use super::abstraction::Tree;
|
||||
|
||||
pub struct AccountData {
|
||||
pub(super) roomuserdataid_accountdata: Arc<dyn Tree>, // RoomUserDataId = Room + User + Count + Type
|
||||
pub(super) roomusertype_roomuserdataid: Arc<dyn Tree>, // RoomUserType = Room + User + Type
|
||||
}
|
||||
|
||||
impl AccountData {
|
||||
/// Places one event in the account data of the user and removes the previous entry.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, event_type, data, globals))]
|
||||
pub fn update<T: Serialize>(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
@@ -33,15 +35,13 @@ impl AccountData {
|
||||
prefix.extend_from_slice(&user_id.as_bytes());
|
||||
prefix.push(0xff);
|
||||
|
||||
// Remove old entry
|
||||
if let Some((old_key, _)) = self.find_event(room_id, user_id, &event_type)? {
|
||||
self.roomuserdataid_accountdata.remove(&old_key)?;
|
||||
}
|
||||
let mut roomuserdataid = prefix.clone();
|
||||
roomuserdataid.extend_from_slice(&globals.next_count()?.to_be_bytes());
|
||||
roomuserdataid.push(0xff);
|
||||
roomuserdataid.extend_from_slice(&event_type.as_bytes());
|
||||
|
||||
let mut key = prefix;
|
||||
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(event_type.as_ref().as_bytes());
|
||||
key.extend_from_slice(event_type.as_bytes());
|
||||
|
||||
let json = serde_json::to_value(data).expect("all types here can be serialized"); // TODO: maybe add error handling
|
||||
if json.get("type").is_none() || json.get("content").is_none() {
|
||||
@@ -52,29 +52,58 @@ impl AccountData {
|
||||
}
|
||||
|
||||
self.roomuserdataid_accountdata.insert(
|
||||
&key,
|
||||
&roomuserdataid,
|
||||
&serde_json::to_vec(&json).expect("to_vec always works on json values"),
|
||||
)?;
|
||||
|
||||
let prev = self.roomusertype_roomuserdataid.get(&key)?;
|
||||
|
||||
self.roomusertype_roomuserdataid
|
||||
.insert(&key, &roomuserdataid)?;
|
||||
|
||||
// Remove old entry
|
||||
if let Some(prev) = prev {
|
||||
self.roomuserdataid_accountdata.remove(&prev)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Searches the account data for a specific kind.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, kind))]
|
||||
pub fn get<T: DeserializeOwned>(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
kind: EventType,
|
||||
) -> Result<Option<T>> {
|
||||
self.find_event(room_id, user_id, &kind)?
|
||||
.map(|(_, v)| {
|
||||
serde_json::from_slice(&v).map_err(|_| Error::bad_database("could not deserialize"))
|
||||
let mut key = room_id
|
||||
.map(|r| r.to_string())
|
||||
.unwrap_or_default()
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&user_id.as_bytes());
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(kind.as_ref().as_bytes());
|
||||
|
||||
self.roomusertype_roomuserdataid
|
||||
.get(&key)?
|
||||
.and_then(|roomuserdataid| {
|
||||
self.roomuserdataid_accountdata
|
||||
.get(&roomuserdataid)
|
||||
.transpose()
|
||||
})
|
||||
.transpose()?
|
||||
.map(|data| {
|
||||
serde_json::from_slice(&data)
|
||||
.map_err(|_| Error::bad_database("could not deserialize"))
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Returns all changes to the account data that happened after `since`.
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, room_id, user_id, since))]
|
||||
pub fn changes_since(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
@@ -121,36 +150,4 @@ impl AccountData {
|
||||
|
||||
Ok(userdata)
|
||||
}
|
||||
|
||||
fn find_event(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
kind: &EventType,
|
||||
) -> Result<Option<(Box<[u8]>, Box<[u8]>)>> {
|
||||
let mut prefix = room_id
|
||||
.map(|r| r.to_string())
|
||||
.unwrap_or_default()
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
prefix.push(0xff);
|
||||
prefix.extend_from_slice(&user_id.as_bytes());
|
||||
prefix.push(0xff);
|
||||
|
||||
let mut last_possible_key = prefix.clone();
|
||||
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
|
||||
|
||||
let kind = kind.clone();
|
||||
|
||||
Ok(self
|
||||
.roomuserdataid_accountdata
|
||||
.iter_from(&last_possible_key, true)
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.find(move |(k, _)| {
|
||||
k.rsplit(|&b| b == 0xff)
|
||||
.next()
|
||||
.map(|current_event_type| current_event_type == kind.as_ref().as_bytes())
|
||||
.unwrap_or(false)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
+57
-32
@@ -4,12 +4,13 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{pdu::PduBuilder, Database};
|
||||
use log::warn;
|
||||
use rocket::futures::{channel::mpsc, stream::StreamExt};
|
||||
use ruma::{
|
||||
events::{room::message, EventType},
|
||||
UserId,
|
||||
};
|
||||
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
||||
use tracing::warn;
|
||||
|
||||
pub enum AdminCommand {
|
||||
RegisterAppservice(serde_yaml::Value),
|
||||
@@ -25,73 +26,97 @@ pub struct Admin {
|
||||
impl Admin {
|
||||
pub fn start_handler(
|
||||
&self,
|
||||
db: Arc<Database>,
|
||||
db: Arc<RwLock<Database>>,
|
||||
mut receiver: mpsc::UnboundedReceiver<AdminCommand>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
// TODO: Use futures when we have long admin commands
|
||||
//let mut futures = FuturesUnordered::new();
|
||||
|
||||
let conduit_user = UserId::try_from(format!("@conduit:{}", db.globals.server_name()))
|
||||
.expect("@conduit:server_name is valid");
|
||||
let guard = db.read().await;
|
||||
|
||||
let conduit_room = db
|
||||
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:{}", db.globals.server_name())
|
||||
&format!("#admins:{}", guard.globals.server_name())
|
||||
.try_into()
|
||||
.expect("#admins:server_name is a valid room alias"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if conduit_room.is_none() {
|
||||
warn!("Conduit instance does not have an #admins room. Logging to that room will not work. Restart Conduit after creating a user to fix this.");
|
||||
}
|
||||
|
||||
let send_message = |message: message::MessageEventContent| {
|
||||
if let Some(conduit_room) = &conduit_room {
|
||||
db.rooms
|
||||
.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMessage,
|
||||
content: serde_json::to_value(message)
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
state_key: None,
|
||||
redacts: None,
|
||||
},
|
||||
&conduit_user,
|
||||
&conduit_room,
|
||||
&db,
|
||||
)
|
||||
.unwrap();
|
||||
let conduit_room = match conduit_room {
|
||||
None => {
|
||||
warn!("Conduit instance does not have an #admins room. Logging to that room will not work. Restart Conduit after creating a user to fix this.");
|
||||
return;
|
||||
}
|
||||
Some(r) => r,
|
||||
};
|
||||
|
||||
drop(guard);
|
||||
|
||||
let send_message = |message: message::MessageEventContent,
|
||||
guard: RwLockReadGuard<'_, Database>,
|
||||
mutex_lock: &MutexGuard<'_, ()>| {
|
||||
guard
|
||||
.rooms
|
||||
.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
event_type: EventType::RoomMessage,
|
||||
content: serde_json::to_value(message)
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
state_key: None,
|
||||
redacts: None,
|
||||
},
|
||||
&conduit_user,
|
||||
&conduit_room,
|
||||
&guard,
|
||||
mutex_lock,
|
||||
)
|
||||
.unwrap();
|
||||
};
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some(event) = receiver.next() => {
|
||||
let guard = db.read().await;
|
||||
let mutex_state = Arc::clone(
|
||||
guard.globals
|
||||
.roomid_mutex_state
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(conduit_room.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
match event {
|
||||
AdminCommand::RegisterAppservice(yaml) => {
|
||||
db.appservice.register_appservice(yaml).unwrap(); // TODO handle error
|
||||
guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error
|
||||
}
|
||||
AdminCommand::ListAppservices => {
|
||||
if let Ok(appservices) = db.appservice.iter_ids().map(|ids| ids.collect::<Vec<_>>()) {
|
||||
if let Ok(appservices) = guard.appservice.iter_ids().map(|ids| ids.collect::<Vec<_>>()) {
|
||||
let count = appservices.len();
|
||||
let output = format!(
|
||||
"Appservices ({}): {}",
|
||||
count,
|
||||
appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ")
|
||||
);
|
||||
send_message(message::MessageEventContent::text_plain(output));
|
||||
send_message(message::MessageEventContent::text_plain(output), guard, &state_lock);
|
||||
} else {
|
||||
send_message(message::MessageEventContent::text_plain("Failed to get appservices."));
|
||||
send_message(message::MessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
|
||||
}
|
||||
}
|
||||
AdminCommand::SendMessage(message) => {
|
||||
send_message(message);
|
||||
send_message(message, guard, &state_lock);
|
||||
}
|
||||
}
|
||||
|
||||
drop(state_lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-11
@@ -49,22 +49,23 @@ impl Appservice {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn iter_ids(&self) -> Result<impl Iterator<Item = Result<String>> + Send + Sync + '_> {
|
||||
pub fn iter_ids(&self) -> Result<impl Iterator<Item = Result<String>> + '_> {
|
||||
Ok(self.id_appserviceregistrations.iter().map(|(id, _)| {
|
||||
utils::string_from_bytes(&id)
|
||||
.map_err(|_| Error::bad_database("Invalid id bytes in id_appserviceregistrations."))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn iter_all(
|
||||
&self,
|
||||
) -> Result<impl Iterator<Item = Result<(String, serde_yaml::Value)>> + '_ + Send + Sync> {
|
||||
Ok(self.iter_ids()?.filter_map(|id| id.ok()).map(move |id| {
|
||||
Ok((
|
||||
id.clone(),
|
||||
self.get_registration(&id)?
|
||||
.expect("iter_ids only returns appservices that exist"),
|
||||
))
|
||||
}))
|
||||
pub fn all(&self) -> Result<Vec<(String, serde_yaml::Value)>> {
|
||||
self.iter_ids()?
|
||||
.filter_map(|id| id.ok())
|
||||
.map(move |id| {
|
||||
Ok((
|
||||
id.clone(),
|
||||
self.get_registration(&id)?
|
||||
.expect("iter_ids only returns appservices that exist"),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
+81
-76
@@ -1,84 +1,83 @@
|
||||
use crate::{database::Config, utils, ConduitResult, Error, Result};
|
||||
use log::{error, info};
|
||||
use crate::{database::Config, server_server::FedDest, utils, ConduitResult, Error, Result};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::r0::sync::sync_events,
|
||||
federation::discovery::{ServerSigningKeys, VerifyKey},
|
||||
},
|
||||
DeviceId, EventId, MilliSecondsSinceUnixEpoch, ServerName, ServerSigningKeyId, UserId,
|
||||
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, UserId,
|
||||
};
|
||||
use rustls::{ServerCertVerifier, WebPKIVerifier};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
fs,
|
||||
future::Future,
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
sync::{Arc, RwLock},
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::sync::{broadcast, watch::Receiver, Mutex as TokioMutex, Semaphore};
|
||||
use tracing::error;
|
||||
use trust_dns_resolver::TokioAsyncResolver;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
pub const COUNTER: &[u8] = b"c";
|
||||
|
||||
type WellKnownMap = HashMap<Box<ServerName>, (String, String)>;
|
||||
type TlsNameMap = HashMap<String, webpki::DNSName>;
|
||||
type WellKnownMap = HashMap<Box<ServerName>, (FedDest, String)>;
|
||||
type TlsNameMap = HashMap<String, (Vec<IpAddr>, u16)>;
|
||||
type RateLimitState = (Instant, u32); // Time if last failed try, number of failed tries
|
||||
type SyncHandle = (
|
||||
Option<String>, // since
|
||||
Receiver<Option<ConduitResult<sync_events::Response>>>, // rx
|
||||
);
|
||||
|
||||
pub struct Globals {
|
||||
pub actual_destination_cache: Arc<RwLock<WellKnownMap>>, // actual_destination, host
|
||||
pub tls_name_override: Arc<RwLock<TlsNameMap>>,
|
||||
pub(super) globals: Arc<dyn Tree>,
|
||||
config: Config,
|
||||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||
reqwest_client: reqwest::Client,
|
||||
dns_resolver: TokioAsyncResolver,
|
||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
||||
pub(super) server_signingkeys: Arc<dyn Tree>,
|
||||
pub bad_event_ratelimiter: Arc<RwLock<BTreeMap<EventId, RateLimitState>>>,
|
||||
pub bad_signature_ratelimiter: Arc<RwLock<BTreeMap<Vec<String>, RateLimitState>>>,
|
||||
pub servername_ratelimiter: Arc<RwLock<BTreeMap<Box<ServerName>, Arc<Semaphore>>>>,
|
||||
pub sync_receivers: RwLock<
|
||||
BTreeMap<
|
||||
(UserId, Box<DeviceId>),
|
||||
(
|
||||
Option<String>,
|
||||
tokio::sync::watch::Receiver<Option<ConduitResult<sync_events::Response>>>,
|
||||
), // since, rx
|
||||
>,
|
||||
>,
|
||||
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<(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,
|
||||
}
|
||||
|
||||
struct MatrixServerVerifier {
|
||||
inner: WebPKIVerifier,
|
||||
tls_name_override: Arc<RwLock<TlsNameMap>>,
|
||||
}
|
||||
/// Handles "rotation" of long-polling requests. "Rotation" in this context is similar to "rotation" of log files and the like.
|
||||
///
|
||||
/// This is utilized to have sync workers return early and release read locks on the database.
|
||||
pub struct RotationHandler(broadcast::Sender<()>, broadcast::Receiver<()>);
|
||||
|
||||
impl ServerCertVerifier for MatrixServerVerifier {
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
roots: &rustls::RootCertStore,
|
||||
presented_certs: &[rustls::Certificate],
|
||||
dns_name: webpki::DNSNameRef<'_>,
|
||||
ocsp_response: &[u8],
|
||||
) -> std::result::Result<rustls::ServerCertVerified, rustls::TLSError> {
|
||||
if let Some(override_name) = self.tls_name_override.read().unwrap().get(dns_name.into()) {
|
||||
let result = self.inner.verify_server_cert(
|
||||
roots,
|
||||
presented_certs,
|
||||
override_name.as_ref(),
|
||||
ocsp_response,
|
||||
);
|
||||
if result.is_ok() {
|
||||
return result;
|
||||
}
|
||||
info!(
|
||||
"Server {:?} is non-compliant, retrying TLS verification with original name",
|
||||
dns_name
|
||||
);
|
||||
impl RotationHandler {
|
||||
pub fn new() -> Self {
|
||||
let (s, r) = broadcast::channel::<()>(1);
|
||||
|
||||
Self(s, r)
|
||||
}
|
||||
|
||||
pub fn watch(&self) -> impl Future<Output = ()> {
|
||||
let mut r = self.0.subscribe();
|
||||
|
||||
async move {
|
||||
let _ = r.recv().await;
|
||||
}
|
||||
self.inner
|
||||
.verify_server_cert(roots, presented_certs, dns_name, ocsp_response)
|
||||
}
|
||||
|
||||
pub fn fire(&self) {
|
||||
let _ = self.0.send(());
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RotationHandler {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,24 +127,6 @@ impl Globals {
|
||||
};
|
||||
|
||||
let tls_name_override = Arc::new(RwLock::new(TlsNameMap::new()));
|
||||
let verifier = Arc::new(MatrixServerVerifier {
|
||||
inner: WebPKIVerifier::new(),
|
||||
tls_name_override: tls_name_override.clone(),
|
||||
});
|
||||
let mut tlsconfig = rustls::ClientConfig::new();
|
||||
tlsconfig.dangerous().set_certificate_verifier(verifier);
|
||||
tlsconfig.root_store =
|
||||
rustls_native_certs::load_native_certs().expect("Error loading system certificates");
|
||||
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3))
|
||||
.pool_max_idle_per_host(1)
|
||||
.use_preconfigured_tls(tlsconfig);
|
||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
let reqwest_client = reqwest_client_builder.build().unwrap();
|
||||
|
||||
let jwt_decoding_key = config
|
||||
.jwt_secret
|
||||
@@ -156,7 +137,6 @@ impl Globals {
|
||||
globals,
|
||||
config,
|
||||
keypair: Arc::new(keypair),
|
||||
reqwest_client,
|
||||
dns_resolver: TokioAsyncResolver::tokio_from_system_conf().map_err(|_| {
|
||||
Error::bad_config("Failed to set up trust dns resolver with system config.")
|
||||
})?,
|
||||
@@ -164,10 +144,14 @@ impl Globals {
|
||||
tls_name_override,
|
||||
server_signingkeys,
|
||||
jwt_decoding_key,
|
||||
bad_event_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
|
||||
bad_signature_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
|
||||
servername_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
|
||||
sync_receivers: RwLock::new(BTreeMap::new()),
|
||||
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
||||
bad_signature_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
||||
servername_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
||||
roomid_mutex_state: RwLock::new(HashMap::new()),
|
||||
roomid_mutex_insert: RwLock::new(HashMap::new()),
|
||||
roomid_mutex_federation: RwLock::new(HashMap::new()),
|
||||
sync_receivers: RwLock::new(HashMap::new()),
|
||||
rotate: RotationHandler::new(),
|
||||
};
|
||||
|
||||
fs::create_dir_all(s.get_media_folder())?;
|
||||
@@ -181,15 +165,25 @@ impl Globals {
|
||||
}
|
||||
|
||||
/// Returns a reqwest client which can be used to send requests.
|
||||
pub fn reqwest_client(&self) -> &reqwest::Client {
|
||||
&self.reqwest_client
|
||||
pub fn reqwest_client(&self) -> Result<reqwest::ClientBuilder> {
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3))
|
||||
.pool_max_idle_per_host(1);
|
||||
if let Some(proxy) = self.config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
|
||||
Ok(reqwest_client_builder)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn next_count(&self) -> Result<u64> {
|
||||
utils::u64_from_bytes(&self.globals.increment(COUNTER)?)
|
||||
.map_err(|_| Error::bad_database("Count has invalid bytes."))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn current_count(&self) -> Result<u64> {
|
||||
self.globals.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
|
||||
utils::u64_from_bytes(&bytes)
|
||||
@@ -233,7 +227,11 @@ impl Globals {
|
||||
/// Remove the outdated keys and insert the new ones.
|
||||
///
|
||||
/// This doesn't actually check that the keys provided are newer than the old set.
|
||||
pub fn add_signing_key(&self, origin: &ServerName, new_keys: ServerSigningKeys) -> Result<()> {
|
||||
pub fn add_signing_key(
|
||||
&self,
|
||||
origin: &ServerName,
|
||||
new_keys: ServerSigningKeys,
|
||||
) -> Result<BTreeMap<ServerSigningKeyId, VerifyKey>> {
|
||||
// Not atomic, but this is not critical
|
||||
let signingkeys = self.server_signingkeys.get(origin.as_bytes())?;
|
||||
|
||||
@@ -258,7 +256,14 @@ impl Globals {
|
||||
&serde_json::to_vec(&keys).expect("serversigningkeys can be serialized"),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
let mut tree = keys.verify_keys;
|
||||
tree.extend(
|
||||
keys.old_verify_keys
|
||||
.into_iter()
|
||||
.map(|old| (old.0, VerifyKey::new(old.1.key))),
|
||||
);
|
||||
|
||||
Ok(tree)
|
||||
}
|
||||
|
||||
/// This returns an empty `Ok(BTreeMap<..>)` when there are no keys found for the server.
|
||||
|
||||
@@ -84,6 +84,27 @@ impl KeyBackups {
|
||||
Ok(version.to_string())
|
||||
}
|
||||
|
||||
pub fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
let mut prefix = user_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
let mut last_possible_key = prefix.clone();
|
||||
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
|
||||
|
||||
self.backupid_algorithm
|
||||
.iter_from(&last_possible_key, true)
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.next()
|
||||
.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)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_latest_backup(&self, user_id: &UserId) -> Result<Option<(String, BackupAlgorithm)>> {
|
||||
let mut prefix = user_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
||||
@@ -54,6 +54,7 @@ impl Media {
|
||||
}
|
||||
|
||||
/// Uploads or replaces a file thumbnail.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn upload_thumbnail(
|
||||
&self,
|
||||
mxc: String,
|
||||
@@ -100,8 +101,8 @@ impl Media {
|
||||
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
||||
prefix.push(0xff);
|
||||
|
||||
let mut iter = self.mediaid_file.scan_prefix(prefix);
|
||||
if let Some((key, _)) = iter.next() {
|
||||
let first = self.mediaid_file.scan_prefix(prefix).next();
|
||||
if let Some((key, _)) = first {
|
||||
let path = globals.get_media_file(&key);
|
||||
let mut file = Vec::new();
|
||||
File::open(path).await?.read_to_end(&mut file).await?;
|
||||
@@ -189,7 +190,9 @@ impl Media {
|
||||
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
||||
original_prefix.push(0xff);
|
||||
|
||||
if let Some((key, _)) = self.mediaid_file.scan_prefix(thumbnail_prefix).next() {
|
||||
let first_thumbnailprefix = self.mediaid_file.scan_prefix(thumbnail_prefix).next();
|
||||
let first_originalprefix = self.mediaid_file.scan_prefix(original_prefix).next();
|
||||
if let Some((key, _)) = first_thumbnailprefix {
|
||||
// Using saved thumbnail
|
||||
let path = globals.get_media_file(&key);
|
||||
let mut file = Vec::new();
|
||||
@@ -224,7 +227,7 @@ impl Media {
|
||||
content_type,
|
||||
file: file.to_vec(),
|
||||
}))
|
||||
} else if let Some((key, _)) = self.mediaid_file.scan_prefix(original_prefix).next() {
|
||||
} else if let Some((key, _)) = first_originalprefix {
|
||||
// Generate a thumbnail
|
||||
let path = globals.get_media_file(&key);
|
||||
let mut file = Vec::new();
|
||||
|
||||
+47
-22
@@ -1,6 +1,5 @@
|
||||
use crate::{Database, Error, PduEvent, Result};
|
||||
use bytes::BytesMut;
|
||||
use log::{error, info, warn};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::r0::push::{get_pushers, set_pusher, PusherKind},
|
||||
@@ -10,10 +9,13 @@ use ruma::{
|
||||
},
|
||||
IncomingResponse, OutgoingRequest, SendAccessToken,
|
||||
},
|
||||
events::{room::power_levels::PowerLevelsEventContent, EventType},
|
||||
events::{room::power_levels::PowerLevelsEventContent, AnySyncRoomEvent, EventType},
|
||||
identifiers::RoomName,
|
||||
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
|
||||
uint, UInt, UserId,
|
||||
serde::Raw,
|
||||
uint, RoomId, UInt, UserId,
|
||||
};
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use std::{convert::TryFrom, fmt::Debug, mem, sync::Arc};
|
||||
|
||||
@@ -25,6 +27,7 @@ pub struct PushData {
|
||||
}
|
||||
|
||||
impl PushData {
|
||||
#[tracing::instrument(skip(self, sender, pusher))]
|
||||
pub fn set_pusher(&self, sender: &UserId, pusher: set_pusher::Pusher) -> Result<()> {
|
||||
let mut key = sender.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
@@ -47,6 +50,7 @@ impl PushData {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, senderkey))]
|
||||
pub fn get_pusher(&self, senderkey: &[u8]) -> Result<Option<get_pushers::Pusher>> {
|
||||
self.senderkey_pusher
|
||||
.get(senderkey)?
|
||||
@@ -57,6 +61,7 @@ impl PushData {
|
||||
.transpose()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, sender))]
|
||||
pub fn get_pushers(&self, sender: &UserId) -> Result<Vec<get_pushers::Pusher>> {
|
||||
let mut prefix = sender.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
@@ -70,10 +75,11 @@ impl PushData {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, sender))]
|
||||
pub fn get_pusher_senderkeys<'a>(
|
||||
&'a self,
|
||||
sender: &UserId,
|
||||
) -> impl Iterator<Item = Box<[u8]>> + 'a {
|
||||
) -> impl Iterator<Item = Vec<u8>> + 'a {
|
||||
let mut prefix = sender.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
||||
@@ -81,6 +87,7 @@ impl PushData {
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(globals, destination, request))]
|
||||
pub async fn send_request<T: OutgoingRequest>(
|
||||
globals: &crate::database::globals::Globals,
|
||||
destination: &str,
|
||||
@@ -106,7 +113,11 @@ where
|
||||
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let response = globals.reqwest_client().execute(reqwest_request).await;
|
||||
let response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
@@ -154,6 +165,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(user, unread, pusher, ruleset, pdu, db))]
|
||||
pub async fn send_push_notice(
|
||||
user: &UserId,
|
||||
unread: UInt,
|
||||
@@ -165,7 +177,24 @@ pub async fn send_push_notice(
|
||||
let mut notify = None;
|
||||
let mut tweaks = Vec::new();
|
||||
|
||||
for action in get_actions(user, &ruleset, pdu, db)? {
|
||||
let power_levels: PowerLevelsEventContent = db
|
||||
.rooms
|
||||
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
|
||||
.map(|ev| {
|
||||
serde_json::from_value(ev.content.clone())
|
||||
.map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
|
||||
for action in get_actions(
|
||||
user,
|
||||
&ruleset,
|
||||
&power_levels,
|
||||
&pdu.to_sync_room_event(),
|
||||
&pdu.room_id,
|
||||
db,
|
||||
)? {
|
||||
let n = match action {
|
||||
Action::DontNotify => false,
|
||||
// TODO: Implement proper support for coalesce
|
||||
@@ -193,37 +222,31 @@ pub async fn send_push_notice(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(user, ruleset, pdu, db))]
|
||||
pub fn get_actions<'a>(
|
||||
user: &UserId,
|
||||
ruleset: &'a Ruleset,
|
||||
pdu: &PduEvent,
|
||||
power_levels: &PowerLevelsEventContent,
|
||||
pdu: &Raw<AnySyncRoomEvent>,
|
||||
room_id: &RoomId,
|
||||
db: &Database,
|
||||
) -> Result<&'a [Action]> {
|
||||
let power_levels: PowerLevelsEventContent = db
|
||||
.rooms
|
||||
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
|
||||
.map(|ev| {
|
||||
serde_json::from_value(ev.content.clone())
|
||||
.map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
|
||||
let ctx = PushConditionRoomCtx {
|
||||
room_id: pdu.room_id.clone(),
|
||||
room_id: room_id.clone(),
|
||||
member_count: 10_u32.into(), // TODO: get member count efficiently
|
||||
user_display_name: db
|
||||
.users
|
||||
.displayname(&user)?
|
||||
.unwrap_or_else(|| user.localpart().to_owned()),
|
||||
users_power_levels: power_levels.users,
|
||||
users_power_levels: power_levels.users.clone(),
|
||||
default_power_level: power_levels.users_default,
|
||||
notification_power_levels: power_levels.notifications,
|
||||
notification_power_levels: power_levels.notifications.clone(),
|
||||
};
|
||||
|
||||
Ok(ruleset.get_actions(&pdu.to_sync_room_event(), &ctx))
|
||||
Ok(ruleset.get_actions(pdu, &ctx))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(unread, pusher, tweaks, event, db))]
|
||||
async fn send_notice(
|
||||
unread: UInt,
|
||||
pusher: &get_pushers::Pusher,
|
||||
@@ -299,7 +322,9 @@ async fn send_notice(
|
||||
.rooms
|
||||
.room_state_get(&event.room_id, &EventType::RoomName, "")?
|
||||
.map(|pdu| match pdu.content.get("name") {
|
||||
Some(serde_json::Value::String(s)) => Some(s.to_string()),
|
||||
Some(serde_json::Value::String(s)) => {
|
||||
Some(Box::<RoomName>::try_from(&**s).expect("room name is valid"))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.flatten();
|
||||
|
||||
+1468
-425
File diff suppressed because it is too large
Load Diff
@@ -116,6 +116,7 @@ impl RoomEdus {
|
||||
}
|
||||
|
||||
/// Sets a private read marker at `count`.
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
pub fn private_read_set(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
@@ -421,7 +422,7 @@ impl RoomEdus {
|
||||
}
|
||||
|
||||
/// Sets all users to offline who have been quiet for too long.
|
||||
pub fn presence_maintain(
|
||||
fn _presence_maintain(
|
||||
&self,
|
||||
rooms: &super::Rooms,
|
||||
globals: &super::super::globals::Globals,
|
||||
@@ -488,15 +489,15 @@ impl RoomEdus {
|
||||
}
|
||||
|
||||
/// Returns an iterator over the most recent presence updates that happened after the event with id `since`.
|
||||
#[tracing::instrument(skip(self, globals, rooms))]
|
||||
#[tracing::instrument(skip(self, since, _rooms, _globals))]
|
||||
pub fn presence_since(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
since: u64,
|
||||
rooms: &super::Rooms,
|
||||
globals: &super::super::globals::Globals,
|
||||
_rooms: &super::Rooms,
|
||||
_globals: &super::super::globals::Globals,
|
||||
) -> Result<HashMap<UserId, PresenceEvent>> {
|
||||
self.presence_maintain(rooms, globals)?;
|
||||
//self.presence_maintain(rooms, globals)?;
|
||||
|
||||
let mut prefix = room_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
||||
+171
-98
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
fmt::Debug,
|
||||
sync::Arc,
|
||||
@@ -10,7 +10,6 @@ use crate::{
|
||||
appservice_server, database::pusher, server_server, utils, Database, Error, PduEvent, Result,
|
||||
};
|
||||
use federation::transactions::send_transaction_message;
|
||||
use log::{error, warn};
|
||||
use ring::digest;
|
||||
use rocket::futures::{
|
||||
channel::mpsc,
|
||||
@@ -21,16 +20,23 @@ use ruma::{
|
||||
appservice,
|
||||
federation::{
|
||||
self,
|
||||
transactions::edu::{Edu, ReceiptContent, ReceiptData, ReceiptMap},
|
||||
transactions::edu::{
|
||||
DeviceListUpdateContent, Edu, ReceiptContent, ReceiptData, ReceiptMap,
|
||||
},
|
||||
},
|
||||
OutgoingRequest,
|
||||
},
|
||||
device_id,
|
||||
events::{push_rules, AnySyncEphemeralRoomEvent, EventType},
|
||||
push,
|
||||
receipt::ReceiptType,
|
||||
MilliSecondsSinceUnixEpoch, ServerName, UInt, UserId,
|
||||
uint, MilliSecondsSinceUnixEpoch, ServerName, UInt, UserId,
|
||||
};
|
||||
use tokio::{select, sync::Semaphore};
|
||||
use tokio::{
|
||||
select,
|
||||
sync::{RwLock, Semaphore},
|
||||
};
|
||||
use tracing::{error, warn};
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
@@ -42,6 +48,7 @@ pub enum OutgoingKind {
|
||||
}
|
||||
|
||||
impl OutgoingKind {
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn get_prefix(&self) -> Vec<u8> {
|
||||
let mut prefix = match self {
|
||||
OutgoingKind::Appservice(server) => {
|
||||
@@ -77,10 +84,10 @@ pub enum SendingEventType {
|
||||
pub struct Sending {
|
||||
/// The state for a given state hash.
|
||||
pub(super) servername_educount: Arc<dyn Tree>, // EduCount: Count of last EDU sync
|
||||
pub(super) servernamepduids: Arc<dyn Tree>, // ServernamePduId = (+ / $)SenderKey / ServerName / UserId + PduId
|
||||
pub(super) servercurrentevents: Arc<dyn Tree>, // ServerCurrentEvents = (+ / $)ServerName / UserId + PduId / (*)EduEvent
|
||||
pub(super) servernameevent_data: Arc<dyn Tree>, // ServernamEvent = (+ / $)SenderKey / ServerName / UserId + PduId / Id (for edus), Data = EDU content
|
||||
pub(super) servercurrentevent_data: Arc<dyn Tree>, // ServerCurrentEvents = (+ / $)ServerName / UserId + PduId / Id (for edus), Data = EDU content
|
||||
pub(super) maximum_requests: Arc<Semaphore>,
|
||||
pub sender: mpsc::UnboundedSender<Vec<u8>>,
|
||||
pub sender: mpsc::UnboundedSender<(Vec<u8>, Vec<u8>)>,
|
||||
}
|
||||
|
||||
enum TransactionStatus {
|
||||
@@ -90,7 +97,11 @@ enum TransactionStatus {
|
||||
}
|
||||
|
||||
impl Sending {
|
||||
pub fn start_handler(&self, db: Arc<Database>, mut receiver: mpsc::UnboundedReceiver<Vec<u8>>) {
|
||||
pub fn start_handler(
|
||||
&self,
|
||||
db: Arc<RwLock<Database>>,
|
||||
mut receiver: mpsc::UnboundedReceiver<(Vec<u8>, Vec<u8>)>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
let mut futures = FuturesUnordered::new();
|
||||
|
||||
@@ -98,15 +109,18 @@ impl Sending {
|
||||
|
||||
// Retry requests we could not finish yet
|
||||
let mut initial_transactions = HashMap::<OutgoingKind, Vec<SendingEventType>>::new();
|
||||
for (key, outgoing_kind, event) in
|
||||
db.sending
|
||||
.servercurrentevents
|
||||
.iter()
|
||||
.filter_map(|(key, _)| {
|
||||
Self::parse_servercurrentevent(&key)
|
||||
.ok()
|
||||
.map(|(k, e)| (key, k, e))
|
||||
})
|
||||
|
||||
let guard = db.read().await;
|
||||
|
||||
for (key, outgoing_kind, event) in guard
|
||||
.sending
|
||||
.servercurrentevent_data
|
||||
.iter()
|
||||
.filter_map(|(key, v)| {
|
||||
Self::parse_servercurrentevent(&key, v)
|
||||
.ok()
|
||||
.map(|(k, e)| (key, k, e))
|
||||
})
|
||||
{
|
||||
let entry = initial_transactions
|
||||
.entry(outgoing_kind.clone())
|
||||
@@ -117,17 +131,23 @@ impl Sending {
|
||||
"Dropping some current events: {:?} {:?} {:?}",
|
||||
key, outgoing_kind, event
|
||||
);
|
||||
db.sending.servercurrentevents.remove(&key).unwrap();
|
||||
guard.sending.servercurrentevent_data.remove(&key).unwrap();
|
||||
continue;
|
||||
}
|
||||
|
||||
entry.push(event);
|
||||
}
|
||||
|
||||
drop(guard);
|
||||
|
||||
for (outgoing_kind, events) in initial_transactions {
|
||||
current_transaction_status
|
||||
.insert(outgoing_kind.get_prefix(), TransactionStatus::Running);
|
||||
futures.push(Self::handle_events(outgoing_kind.clone(), events, &db));
|
||||
futures.push(Self::handle_events(
|
||||
outgoing_kind.clone(),
|
||||
events,
|
||||
Arc::clone(&db),
|
||||
));
|
||||
}
|
||||
|
||||
loop {
|
||||
@@ -135,18 +155,20 @@ impl Sending {
|
||||
Some(response) = futures.next() => {
|
||||
match response {
|
||||
Ok(outgoing_kind) => {
|
||||
let guard = db.read().await;
|
||||
|
||||
let prefix = outgoing_kind.get_prefix();
|
||||
for (key, _) in db.sending.servercurrentevents
|
||||
for (key, _) in guard.sending.servercurrentevent_data
|
||||
.scan_prefix(prefix.clone())
|
||||
{
|
||||
db.sending.servercurrentevents.remove(&key).unwrap();
|
||||
guard.sending.servercurrentevent_data.remove(&key).unwrap();
|
||||
}
|
||||
|
||||
// Find events that have been added since starting the last request
|
||||
let new_events = db.sending.servernamepduids
|
||||
let new_events = guard.sending.servernameevent_data
|
||||
.scan_prefix(prefix.clone())
|
||||
.map(|(k, _)| {
|
||||
SendingEventType::Pdu(k[prefix.len()..].to_vec())
|
||||
.filter_map(|(k, v)| {
|
||||
Self::parse_servercurrentevent(&k, v).ok().map(|ev| (ev, k))
|
||||
})
|
||||
.take(30)
|
||||
.collect::<Vec<_>>();
|
||||
@@ -155,23 +177,19 @@ impl Sending {
|
||||
|
||||
if !new_events.is_empty() {
|
||||
// Insert pdus we found
|
||||
for event in &new_events {
|
||||
let mut current_key = prefix.clone();
|
||||
match event {
|
||||
SendingEventType::Pdu(b) |
|
||||
SendingEventType::Edu(b) => {
|
||||
current_key.extend_from_slice(&b);
|
||||
db.sending.servercurrentevents.insert(¤t_key, &[]).unwrap();
|
||||
db.sending.servernamepduids.remove(¤t_key).unwrap();
|
||||
}
|
||||
}
|
||||
for (e, key) in &new_events {
|
||||
let value = if let SendingEventType::Edu(value) = &e.1 { &**value } else { &[] };
|
||||
guard.sending.servercurrentevent_data.insert(&key, value).unwrap();
|
||||
guard.sending.servernameevent_data.remove(&key).unwrap();
|
||||
}
|
||||
|
||||
drop(guard);
|
||||
|
||||
futures.push(
|
||||
Self::handle_events(
|
||||
outgoing_kind.clone(),
|
||||
new_events,
|
||||
&db,
|
||||
new_events.into_iter().map(|(event, _)| event.1).collect(),
|
||||
Arc::clone(&db),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
@@ -190,15 +208,17 @@ impl Sending {
|
||||
}
|
||||
};
|
||||
},
|
||||
Some(key) = receiver.next() => {
|
||||
if let Ok((outgoing_kind, event)) = Self::parse_servercurrentevent(&key) {
|
||||
Some((key, value)) = receiver.next() => {
|
||||
if let Ok((outgoing_kind, event)) = Self::parse_servercurrentevent(&key, value) {
|
||||
let guard = db.read().await;
|
||||
|
||||
if let Ok(Some(events)) = Self::select_events(
|
||||
&outgoing_kind,
|
||||
vec![(event, key)],
|
||||
&mut current_transaction_status,
|
||||
&db
|
||||
&guard
|
||||
) {
|
||||
futures.push(Self::handle_events(outgoing_kind, events, &db));
|
||||
futures.push(Self::handle_events(outgoing_kind, events, Arc::clone(&db)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,6 +227,7 @@ impl Sending {
|
||||
});
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(outgoing_kind, new_events, current_transaction_status, db))]
|
||||
fn select_events(
|
||||
outgoing_kind: &OutgoingKind,
|
||||
new_events: Vec<(SendingEventType, Vec<u8>)>, // Events we want to send: event and full key
|
||||
@@ -249,25 +270,33 @@ impl Sending {
|
||||
|
||||
if retry {
|
||||
// We retry the previous transaction
|
||||
for (key, _) in db.sending.servercurrentevents.scan_prefix(prefix) {
|
||||
if let Ok((_, e)) = Self::parse_servercurrentevent(&key) {
|
||||
for (key, value) in db.sending.servercurrentevent_data.scan_prefix(prefix) {
|
||||
if let Ok((_, e)) = Self::parse_servercurrentevent(&key, value) {
|
||||
events.push(e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (e, full_key) in new_events {
|
||||
db.sending.servercurrentevents.insert(&full_key, &[])?;
|
||||
let value = if let SendingEventType::Edu(value) = &e {
|
||||
&**value
|
||||
} else {
|
||||
&[][..]
|
||||
};
|
||||
db.sending
|
||||
.servercurrentevent_data
|
||||
.insert(&full_key, value)?;
|
||||
|
||||
// If it was a PDU we have to unqueue it
|
||||
// TODO: don't try to unqueue EDUs
|
||||
db.sending.servernamepduids.remove(&full_key)?;
|
||||
db.sending.servernameevent_data.remove(&full_key)?;
|
||||
|
||||
events.push(e);
|
||||
}
|
||||
|
||||
if let OutgoingKind::Normal(server_name) = outgoing_kind {
|
||||
if let Ok((select_edus, last_count)) = Self::select_edus(db, server_name) {
|
||||
events.extend_from_slice(&select_edus);
|
||||
events.extend(select_edus.into_iter().map(SendingEventType::Edu));
|
||||
|
||||
db.sending
|
||||
.servername_educount
|
||||
.insert(server_name.as_bytes(), &last_count.to_be_bytes())?;
|
||||
@@ -278,7 +307,8 @@ impl Sending {
|
||||
Ok(Some(events))
|
||||
}
|
||||
|
||||
pub fn select_edus(db: &Database, server: &ServerName) -> Result<(Vec<SendingEventType>, u64)> {
|
||||
#[tracing::instrument(skip(db, server))]
|
||||
pub fn select_edus(db: &Database, server: &ServerName) -> Result<(Vec<Vec<u8>>, u64)> {
|
||||
// u64: count of last edu
|
||||
let since = db
|
||||
.sending
|
||||
@@ -290,8 +320,19 @@ impl Sending {
|
||||
})?;
|
||||
let mut events = Vec::new();
|
||||
let mut max_edu_count = since;
|
||||
let mut device_list_changes = HashSet::new();
|
||||
|
||||
'outer: for room_id in db.rooms.server_rooms(server) {
|
||||
let room_id = room_id?;
|
||||
// Look for device list updates in this room
|
||||
device_list_changes.extend(
|
||||
db.users
|
||||
.keys_changed(&room_id.to_string(), since, None)
|
||||
.filter_map(|r| r.ok())
|
||||
.filter(|user_id| user_id.server_name() == db.globals.server_name()),
|
||||
);
|
||||
|
||||
// Look for read receipts in this room
|
||||
for r in db.rooms.edus.readreceipts_since(&room_id, since) {
|
||||
let (user_id, count, read_receipt) = r?;
|
||||
|
||||
@@ -343,9 +384,7 @@ impl Sending {
|
||||
}
|
||||
};
|
||||
|
||||
events.push(SendingEventType::Edu(
|
||||
serde_json::to_vec(&federation_event).expect("json can be serialized"),
|
||||
));
|
||||
events.push(serde_json::to_vec(&federation_event).expect("json can be serialized"));
|
||||
|
||||
if events.len() >= 20 {
|
||||
break 'outer;
|
||||
@@ -353,28 +392,60 @@ impl Sending {
|
||||
}
|
||||
}
|
||||
|
||||
for user_id in device_list_changes {
|
||||
// Empty prev id forces synapse to resync: https://github.com/matrix-org/synapse/blob/98aec1cc9da2bd6b8e34ffb282c85abf9b8b42ca/synapse/handlers/device.py#L767
|
||||
// Because synapse resyncs, we can just insert dummy data
|
||||
let edu = Edu::DeviceListUpdate(DeviceListUpdateContent {
|
||||
user_id,
|
||||
device_id: device_id!("dummy"),
|
||||
device_display_name: "Dummy".to_owned(),
|
||||
stream_id: uint!(1),
|
||||
prev_id: Vec::new(),
|
||||
deleted: None,
|
||||
keys: None,
|
||||
});
|
||||
|
||||
events.push(serde_json::to_vec(&edu).expect("json can be serialized"));
|
||||
}
|
||||
|
||||
Ok((events, max_edu_count))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn send_push_pdu(&self, pdu_id: &[u8], senderkey: Box<[u8]>) -> Result<()> {
|
||||
#[tracing::instrument(skip(self, pdu_id, senderkey))]
|
||||
pub fn send_push_pdu(&self, pdu_id: &[u8], senderkey: Vec<u8>) -> Result<()> {
|
||||
let mut key = b"$".to_vec();
|
||||
key.extend_from_slice(&senderkey);
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(pdu_id);
|
||||
self.servernamepduids.insert(&key, b"")?;
|
||||
self.sender.unbounded_send(key).unwrap();
|
||||
self.servernameevent_data.insert(&key, &[])?;
|
||||
self.sender.unbounded_send((key, vec![])).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, server, pdu_id))]
|
||||
pub fn send_pdu(&self, server: &ServerName, pdu_id: &[u8]) -> Result<()> {
|
||||
let mut key = server.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(pdu_id);
|
||||
self.servernamepduids.insert(&key, b"")?;
|
||||
self.sender.unbounded_send(key).unwrap();
|
||||
self.servernameevent_data.insert(&key, &[])?;
|
||||
self.sender.unbounded_send((key, vec![])).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, server, serialized))]
|
||||
pub fn send_reliable_edu(
|
||||
&self,
|
||||
server: &ServerName,
|
||||
serialized: Vec<u8>,
|
||||
id: u64,
|
||||
) -> Result<()> {
|
||||
let mut key = server.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&id.to_be_bytes());
|
||||
self.servernameevent_data.insert(&key, &serialized)?;
|
||||
self.sender.unbounded_send((key, serialized)).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -385,13 +456,13 @@ impl Sending {
|
||||
key.extend_from_slice(appservice_id.as_bytes());
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(pdu_id);
|
||||
self.servernamepduids.insert(&key, b"")?;
|
||||
self.sender.unbounded_send(key).unwrap();
|
||||
self.servernameevent_data.insert(&key, &[])?;
|
||||
self.sender.unbounded_send((key, vec![])).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
#[tracing::instrument(skip(keys))]
|
||||
fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> {
|
||||
// We only hash the pdu's event ids, not the whole pdu
|
||||
let bytes = keys.join(&0xff);
|
||||
@@ -399,12 +470,14 @@ impl Sending {
|
||||
hash.as_ref().to_owned()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(db))]
|
||||
#[tracing::instrument(skip(db, events, kind))]
|
||||
async fn handle_events(
|
||||
kind: OutgoingKind,
|
||||
events: Vec<SendingEventType>,
|
||||
db: &Database,
|
||||
db: Arc<RwLock<Database>>,
|
||||
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> {
|
||||
let db = db.read().await;
|
||||
|
||||
match &kind {
|
||||
OutgoingKind::Appservice(server) => {
|
||||
let mut pdu_jsons = Vec::new();
|
||||
@@ -419,7 +492,7 @@ impl Sending {
|
||||
(
|
||||
kind.clone(),
|
||||
Error::bad_database(
|
||||
"[Appservice] Event in servernamepduids not found in db.",
|
||||
"[Appservice] Event in servernameevent_data not found in db.",
|
||||
),
|
||||
)
|
||||
})?
|
||||
@@ -476,7 +549,7 @@ impl Sending {
|
||||
(
|
||||
kind.clone(),
|
||||
Error::bad_database(
|
||||
"[Push] Event in servernamepduids not found in db.",
|
||||
"[Push] Event in servernamevent_datas not found in db.",
|
||||
),
|
||||
)
|
||||
})?,
|
||||
@@ -543,7 +616,7 @@ impl Sending {
|
||||
&pusher,
|
||||
rules_for_user,
|
||||
&pdu,
|
||||
db,
|
||||
&db,
|
||||
)
|
||||
.await
|
||||
.map(|_response| kind.clone())
|
||||
@@ -561,29 +634,25 @@ impl Sending {
|
||||
match event {
|
||||
SendingEventType::Pdu(pdu_id) => {
|
||||
// TODO: check room version and remove event_id if needed
|
||||
pdu_jsons.push(serde_json::from_str(
|
||||
PduEvent::convert_to_outgoing_federation_event(
|
||||
db.rooms
|
||||
.get_pdu_json_from_id(&pdu_id)
|
||||
.map_err(|e| (OutgoingKind::Normal(server.clone()), e))?
|
||||
.ok_or_else(|| {
|
||||
(
|
||||
OutgoingKind::Normal(server.clone()),
|
||||
Error::bad_database(
|
||||
"[Normal] Event in servernamepduids not found in db.",
|
||||
),
|
||||
)
|
||||
})?,
|
||||
)
|
||||
.json()
|
||||
.get(),
|
||||
)
|
||||
.expect("Raw<..> is always valid"));
|
||||
let raw = PduEvent::convert_to_outgoing_federation_event(
|
||||
db.rooms
|
||||
.get_pdu_json_from_id(&pdu_id)
|
||||
.map_err(|e| (OutgoingKind::Normal(server.clone()), e))?
|
||||
.ok_or_else(|| {
|
||||
(
|
||||
OutgoingKind::Normal(server.clone()),
|
||||
Error::bad_database(
|
||||
"[Normal] Event in servernamevent_datas not found in db.",
|
||||
),
|
||||
)
|
||||
})?,
|
||||
);
|
||||
pdu_jsons.push(raw);
|
||||
}
|
||||
SendingEventType::Edu(edu) => {
|
||||
edu_jsons.push(
|
||||
serde_json::from_slice(edu).expect("Raw<..> is always valid"),
|
||||
);
|
||||
if let Ok(raw) = serde_json::from_slice(edu) {
|
||||
edu_jsons.push(raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -629,7 +698,11 @@ impl Sending {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_servercurrentevent(key: &[u8]) -> Result<(OutgoingKind, SendingEventType)> {
|
||||
#[tracing::instrument(skip(key))]
|
||||
fn parse_servercurrentevent(
|
||||
key: &[u8],
|
||||
value: Vec<u8>,
|
||||
) -> Result<(OutgoingKind, SendingEventType)> {
|
||||
// Appservices start with a plus
|
||||
Ok::<_, Error>(if key.starts_with(b"+") {
|
||||
let mut parts = key[1..].splitn(2, |&b| b == 0xff);
|
||||
@@ -646,10 +719,10 @@ impl Sending {
|
||||
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| {
|
||||
Error::bad_database("Invalid server string in server_currenttransaction")
|
||||
})?),
|
||||
if event.starts_with(b"*") {
|
||||
SendingEventType::Edu(event[1..].to_vec())
|
||||
} else {
|
||||
if value.is_empty() {
|
||||
SendingEventType::Pdu(event.to_vec())
|
||||
} else {
|
||||
SendingEventType::Edu(value)
|
||||
},
|
||||
)
|
||||
} else if key.starts_with(b"$") {
|
||||
@@ -664,10 +737,10 @@ impl Sending {
|
||||
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
||||
(
|
||||
OutgoingKind::Push(user.to_vec(), pushkey.to_vec()),
|
||||
if event.starts_with(b"*") {
|
||||
SendingEventType::Edu(event[1..].to_vec())
|
||||
} else {
|
||||
if value.is_empty() {
|
||||
SendingEventType::Pdu(event.to_vec())
|
||||
} else {
|
||||
SendingEventType::Edu(value)
|
||||
},
|
||||
)
|
||||
} else {
|
||||
@@ -685,16 +758,16 @@ impl Sending {
|
||||
OutgoingKind::Normal(Box::<ServerName>::try_from(server).map_err(|_| {
|
||||
Error::bad_database("Invalid server string in server_currenttransaction")
|
||||
})?),
|
||||
if event.starts_with(b"*") {
|
||||
SendingEventType::Edu(event[1..].to_vec())
|
||||
} else {
|
||||
if value.is_empty() {
|
||||
SendingEventType::Pdu(event.to_vec())
|
||||
} else {
|
||||
SendingEventType::Edu(value)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
#[tracing::instrument(skip(self, globals, destination, request))]
|
||||
pub async fn send_federation_request<T: OutgoingRequest>(
|
||||
&self,
|
||||
globals: &crate::database::globals::Globals,
|
||||
@@ -711,7 +784,7 @@ impl Sending {
|
||||
response
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
#[tracing::instrument(skip(self, globals, registration, request))]
|
||||
pub async fn send_appservice_request<T: OutgoingRequest>(
|
||||
&self,
|
||||
globals: &crate::database::globals::Globals,
|
||||
|
||||
+67
-99
@@ -4,11 +4,14 @@ use crate::{client_server::SESSION_ID_LENGTH, utils, Error, Result};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::uiaa::{IncomingAuthData, UiaaInfo},
|
||||
r0::uiaa::{
|
||||
IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId, UiaaInfo,
|
||||
},
|
||||
},
|
||||
signatures::CanonicalJsonValue,
|
||||
DeviceId, UserId,
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
@@ -49,126 +52,91 @@ impl Uiaa {
|
||||
users: &super::users::Users,
|
||||
globals: &super::globals::Globals,
|
||||
) -> Result<(bool, UiaaInfo)> {
|
||||
if let IncomingAuthData::DirectRequest {
|
||||
kind,
|
||||
session,
|
||||
auth_parameters,
|
||||
} = &auth
|
||||
{
|
||||
let mut uiaainfo = session
|
||||
.as_ref()
|
||||
.map(|session| self.get_uiaa_session(&user_id, &device_id, session))
|
||||
.unwrap_or_else(|| Ok(uiaainfo.clone()))?;
|
||||
let mut uiaainfo = auth
|
||||
.session()
|
||||
.map(|session| self.get_uiaa_session(&user_id, &device_id, session))
|
||||
.unwrap_or_else(|| Ok(uiaainfo.clone()))?;
|
||||
|
||||
if uiaainfo.session.is_none() {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
}
|
||||
if uiaainfo.session.is_none() {
|
||||
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
|
||||
}
|
||||
|
||||
match auth {
|
||||
// Find out what the user completed
|
||||
match &**kind {
|
||||
"m.login.password" => {
|
||||
let identifier = auth_parameters.get("identifier").ok_or(Error::BadRequest(
|
||||
ErrorKind::MissingParam,
|
||||
"m.login.password needs identifier.",
|
||||
))?;
|
||||
|
||||
let identifier_type = identifier.get("type").ok_or(Error::BadRequest(
|
||||
ErrorKind::MissingParam,
|
||||
"Identifier needs a type.",
|
||||
))?;
|
||||
|
||||
if identifier_type != "m.id.user" {
|
||||
IncomingAuthData::Password(IncomingPassword {
|
||||
identifier,
|
||||
password,
|
||||
..
|
||||
}) => {
|
||||
let username = match identifier {
|
||||
MatrixId(username) => username,
|
||||
_ => {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Unrecognized,
|
||||
"Identifier type not recognized.",
|
||||
));
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let username = identifier
|
||||
.get("user")
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::MissingParam,
|
||||
"Identifier needs user field.",
|
||||
))?
|
||||
.as_str()
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::BadJson,
|
||||
"User is not a string.",
|
||||
))?;
|
||||
|
||||
let user_id = UserId::parse_with_server_name(username, globals.server_name())
|
||||
let user_id =
|
||||
UserId::parse_with_server_name(username.clone(), globals.server_name())
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "User ID is invalid.")
|
||||
})?;
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "User ID is invalid.")
|
||||
})?;
|
||||
|
||||
let password = auth_parameters
|
||||
.get("password")
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::MissingParam,
|
||||
"Password is missing.",
|
||||
))?
|
||||
.as_str()
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::BadJson,
|
||||
"Password is not a string.",
|
||||
))?;
|
||||
// Check if password is correct
|
||||
if let Some(hash) = users.password_hash(&user_id)? {
|
||||
let hash_matches =
|
||||
argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false);
|
||||
|
||||
// Check if password is correct
|
||||
if let Some(hash) = users.password_hash(&user_id)? {
|
||||
let hash_matches =
|
||||
argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false);
|
||||
|
||||
if !hash_matches {
|
||||
uiaainfo.auth_error = Some(ruma::api::client::error::ErrorBody {
|
||||
kind: ErrorKind::Forbidden,
|
||||
message: "Invalid username or password.".to_owned(),
|
||||
});
|
||||
return Ok((false, uiaainfo));
|
||||
}
|
||||
}
|
||||
|
||||
// Password was correct! Let's add it to `completed`
|
||||
uiaainfo.completed.push("m.login.password".to_owned());
|
||||
}
|
||||
"m.login.dummy" => {
|
||||
uiaainfo.completed.push("m.login.dummy".to_owned());
|
||||
}
|
||||
k => panic!("type not supported: {}", k),
|
||||
}
|
||||
|
||||
// Check if a flow now succeeds
|
||||
let mut completed = false;
|
||||
'flows: for flow in &mut uiaainfo.flows {
|
||||
for stage in &flow.stages {
|
||||
if !uiaainfo.completed.contains(stage) {
|
||||
continue 'flows;
|
||||
if !hash_matches {
|
||||
uiaainfo.auth_error = Some(ruma::api::client::error::ErrorBody {
|
||||
kind: ErrorKind::Forbidden,
|
||||
message: "Invalid username or password.".to_owned(),
|
||||
});
|
||||
return Ok((false, uiaainfo));
|
||||
}
|
||||
}
|
||||
// We didn't break, so this flow succeeded!
|
||||
completed = true;
|
||||
}
|
||||
|
||||
if !completed {
|
||||
self.update_uiaa_session(
|
||||
user_id,
|
||||
device_id,
|
||||
uiaainfo.session.as_ref().expect("session is always set"),
|
||||
Some(&uiaainfo),
|
||||
)?;
|
||||
return Ok((false, uiaainfo));
|
||||
// Password was correct! Let's add it to `completed`
|
||||
uiaainfo.completed.push("m.login.password".to_owned());
|
||||
}
|
||||
IncomingAuthData::Dummy(_) => {
|
||||
uiaainfo.completed.push("m.login.dummy".to_owned());
|
||||
}
|
||||
k => error!("type not supported: {:?}", k),
|
||||
}
|
||||
|
||||
// UIAA was successful! Remove this session and return true
|
||||
// Check if a flow now succeeds
|
||||
let mut completed = false;
|
||||
'flows: for flow in &mut uiaainfo.flows {
|
||||
for stage in &flow.stages {
|
||||
if !uiaainfo.completed.contains(stage) {
|
||||
continue 'flows;
|
||||
}
|
||||
}
|
||||
// We didn't break, so this flow succeeded!
|
||||
completed = true;
|
||||
}
|
||||
|
||||
if !completed {
|
||||
self.update_uiaa_session(
|
||||
user_id,
|
||||
device_id,
|
||||
uiaainfo.session.as_ref().expect("session is always set"),
|
||||
None,
|
||||
Some(&uiaainfo),
|
||||
)?;
|
||||
Ok((true, uiaainfo))
|
||||
} else {
|
||||
panic!("FallbackAcknowledgement is not supported yet");
|
||||
return Ok((false, uiaainfo));
|
||||
}
|
||||
|
||||
// UIAA was successful! Remove this session and return true
|
||||
self.update_uiaa_session(
|
||||
user_id,
|
||||
device_id,
|
||||
uiaainfo.session.as_ref().expect("session is always set"),
|
||||
None,
|
||||
)?;
|
||||
Ok((true, uiaainfo))
|
||||
}
|
||||
|
||||
fn set_uiaa_request(
|
||||
|
||||
+110
-13
@@ -8,6 +8,7 @@ use ruma::{
|
||||
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, UInt, UserId,
|
||||
};
|
||||
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc};
|
||||
use tracing::warn;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
@@ -15,6 +16,7 @@ pub struct Users {
|
||||
pub(super) userid_password: Arc<dyn Tree>,
|
||||
pub(super) userid_displayname: Arc<dyn Tree>,
|
||||
pub(super) userid_avatarurl: Arc<dyn Tree>,
|
||||
pub(super) userid_blurhash: Arc<dyn Tree>,
|
||||
pub(super) userdeviceid_token: Arc<dyn Tree>,
|
||||
pub(super) userdeviceid_metadata: Arc<dyn Tree>, // This is also used to check if a device exists
|
||||
pub(super) userid_devicelistversion: Arc<dyn Tree>, // DevicelistVersion = u64
|
||||
@@ -33,11 +35,13 @@ pub struct Users {
|
||||
|
||||
impl Users {
|
||||
/// Check if a user has an account on this homeserver.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn exists(&self, user_id: &UserId) -> Result<bool> {
|
||||
Ok(self.userid_password.get(user_id.as_bytes())?.is_some())
|
||||
}
|
||||
|
||||
/// Check if account is deactivated
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn is_deactivated(&self, user_id: &UserId) -> Result<bool> {
|
||||
Ok(self
|
||||
.userid_password
|
||||
@@ -50,17 +54,20 @@ impl Users {
|
||||
}
|
||||
|
||||
/// 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<()> {
|
||||
self.set_password(user_id, password)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of users registered on this server.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn count(&self) -> Result<usize> {
|
||||
Ok(self.userid_password.iter().count())
|
||||
}
|
||||
|
||||
/// Find out which user an access token belongs to.
|
||||
#[tracing::instrument(skip(self, token))]
|
||||
pub fn find_from_token(&self, token: &str) -> Result<Option<(UserId, String)>> {
|
||||
self.token_userdeviceid
|
||||
.get(token.as_bytes())?
|
||||
@@ -88,6 +95,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Returns an iterator over all users on this homeserver.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn iter(&self) -> impl Iterator<Item = Result<UserId>> + '_ {
|
||||
self.userid_password.iter().map(|(bytes, _)| {
|
||||
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||
@@ -98,6 +106,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Returns the password hash for the given user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_password
|
||||
.get(user_id.as_bytes())?
|
||||
@@ -109,6 +118,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Hash and set the user's password to the Argon2 hash
|
||||
#[tracing::instrument(skip(self, user_id, password))]
|
||||
pub fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
|
||||
if let Some(password) = password {
|
||||
if let Ok(hash) = utils::calculate_hash(&password) {
|
||||
@@ -128,6 +138,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Returns the displayname of a user on this homeserver.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn displayname(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_displayname
|
||||
.get(user_id.as_bytes())?
|
||||
@@ -139,6 +150,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Sets a new displayname or removes it if displayname is None. You still need to nofify all rooms of this change.
|
||||
#[tracing::instrument(skip(self, user_id, displayname))]
|
||||
pub fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
|
||||
if let Some(displayname) = displayname {
|
||||
self.userid_displayname
|
||||
@@ -150,7 +162,8 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a the avatar_url of a user.
|
||||
/// Get the avatar_url of a user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<MxcUri>> {
|
||||
self.userid_avatarurl
|
||||
.get(user_id.as_bytes())?
|
||||
@@ -163,6 +176,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// 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<MxcUri>) -> Result<()> {
|
||||
if let Some(avatar_url) = avatar_url {
|
||||
self.userid_avatarurl
|
||||
@@ -174,7 +188,35 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the blurhash of a user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_blurhash
|
||||
.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."))?;
|
||||
|
||||
Ok(s)
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Sets a new avatar_url or removes it if avatar_url is None.
|
||||
#[tracing::instrument(skip(self, user_id, blurhash))]
|
||||
pub fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
|
||||
if let Some(blurhash) = blurhash {
|
||||
self.userid_blurhash
|
||||
.insert(user_id.as_bytes(), blurhash.as_bytes())?;
|
||||
} else {
|
||||
self.userid_blurhash.remove(user_id.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adds a new device to a user.
|
||||
#[tracing::instrument(skip(self, user_id, device_id, token, initial_device_display_name))]
|
||||
pub fn create_device(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -209,6 +251,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Removes a device from a user.
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
|
||||
let mut userdeviceid = user_id.as_bytes().to_vec();
|
||||
userdeviceid.push(0xff);
|
||||
@@ -239,6 +282,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Returns an iterator over all device ids of this user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn all_device_ids<'a>(
|
||||
&'a self,
|
||||
user_id: &UserId,
|
||||
@@ -261,6 +305,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Replaces the access token of one device.
|
||||
#[tracing::instrument(skip(self, user_id, device_id, token))]
|
||||
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
|
||||
let mut userdeviceid = user_id.as_bytes().to_vec();
|
||||
userdeviceid.push(0xff);
|
||||
@@ -284,6 +329,14 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
user_id,
|
||||
device_id,
|
||||
one_time_key_key,
|
||||
one_time_key_value,
|
||||
globals
|
||||
))]
|
||||
pub fn add_one_time_key(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -320,7 +373,7 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
|
||||
self.userid_lastonetimekeyupdate
|
||||
.get(&user_id.as_bytes())?
|
||||
@@ -332,6 +385,7 @@ impl Users {
|
||||
.unwrap_or(Ok(0))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, key_algorithm, globals))]
|
||||
pub fn take_one_time_key(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -371,7 +425,7 @@ impl Users {
|
||||
.transpose()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn count_one_time_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -404,6 +458,7 @@ impl Users {
|
||||
Ok(counts)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, device_keys, rooms, globals))]
|
||||
pub fn add_device_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -426,6 +481,14 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
master_key,
|
||||
self_signing_key,
|
||||
user_signing_key,
|
||||
rooms,
|
||||
globals
|
||||
))]
|
||||
pub fn add_cross_signing_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -526,6 +589,7 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, target_id, key_id, signature, sender_id, rooms, globals))]
|
||||
pub fn sign_key(
|
||||
&self,
|
||||
target_id: &UserId,
|
||||
@@ -569,7 +633,7 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_or_room_id, from, to))]
|
||||
pub fn keys_changed<'a>(
|
||||
&'a self,
|
||||
user_or_room_id: &str,
|
||||
@@ -582,9 +646,24 @@ impl Users {
|
||||
let mut start = prefix.clone();
|
||||
start.extend_from_slice(&(from + 1).to_be_bytes());
|
||||
|
||||
let to = to.unwrap_or(u64::MAX);
|
||||
|
||||
self.keychangeid_userid
|
||||
.iter_from(&start, false)
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.take_while(move |(k, _)| {
|
||||
k.starts_with(&prefix)
|
||||
&& if let Some(current) = k.splitn(2, |&b| b == 0xff).nth(1) {
|
||||
if let Ok(c) = utils::u64_from_bytes(current) {
|
||||
c <= to
|
||||
} else {
|
||||
warn!("BadDatabase: Could not parse keychangeid_userid bytes");
|
||||
false
|
||||
}
|
||||
} else {
|
||||
warn!("BadDatabase: Could not parse keychangeid_userid");
|
||||
false
|
||||
}
|
||||
})
|
||||
.map(|(_, bytes)| {
|
||||
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.")
|
||||
@@ -593,7 +672,8 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
fn mark_device_key_update(
|
||||
#[tracing::instrument(skip(self, user_id, rooms, globals))]
|
||||
pub fn mark_device_key_update(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
rooms: &super::rooms::Rooms,
|
||||
@@ -624,6 +704,7 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_device_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -640,6 +721,7 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, allowed_signatures))]
|
||||
pub fn get_master_key<F: Fn(&UserId) -> bool>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -667,6 +749,7 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, allowed_signatures))]
|
||||
pub fn get_self_signing_key<F: Fn(&UserId) -> bool>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -694,6 +777,7 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<CrossSigningKey>> {
|
||||
self.userid_usersigningkeyid
|
||||
.get(user_id.as_bytes())?
|
||||
@@ -706,6 +790,15 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
sender,
|
||||
target_user_id,
|
||||
target_device_id,
|
||||
event_type,
|
||||
content,
|
||||
globals
|
||||
))]
|
||||
pub fn add_to_device_event(
|
||||
&self,
|
||||
sender: &UserId,
|
||||
@@ -726,15 +819,14 @@ impl Users {
|
||||
json.insert("sender".to_owned(), sender.to_string().into());
|
||||
json.insert("content".to_owned(), content);
|
||||
|
||||
self.todeviceid_events.insert(
|
||||
&key,
|
||||
&serde_json::to_vec(&json).expect("Map::to_vec always works"),
|
||||
)?;
|
||||
let value = serde_json::to_vec(&json).expect("Map::to_vec always works");
|
||||
|
||||
self.todeviceid_events.insert(&key, &value)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_to_device_events(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -757,7 +849,7 @@ impl Users {
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id, until))]
|
||||
pub fn remove_to_device_events(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -774,7 +866,7 @@ impl Users {
|
||||
|
||||
for (key, _) in self
|
||||
.todeviceid_events
|
||||
.iter_from(&last, true)
|
||||
.iter_from(&last, true) // this includes last
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.map(|(key, _)| {
|
||||
Ok::<_, Error>((
|
||||
@@ -792,6 +884,7 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, device))]
|
||||
pub fn update_device_metadata(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -817,6 +910,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Get device metadata.
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_device_metadata(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
@@ -835,6 +929,7 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
|
||||
self.userid_devicelistversion
|
||||
.get(user_id.as_bytes())?
|
||||
@@ -845,6 +940,7 @@ impl Users {
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn all_devices_metadata<'a>(
|
||||
&'a self,
|
||||
user_id: &UserId,
|
||||
@@ -861,6 +957,7 @@ impl Users {
|
||||
}
|
||||
|
||||
/// Deactivate account
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn deactivate_account(&self, user_id: &UserId) -> Result<()> {
|
||||
// Remove all associated devices
|
||||
for device_id in self.all_device_ids(user_id) {
|
||||
|
||||
+9
-5
@@ -1,4 +1,3 @@
|
||||
use log::{error, warn};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::{Error as RumaError, ErrorKind},
|
||||
@@ -7,6 +6,7 @@ use ruma::{
|
||||
ServerName,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use tracing::warn;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
@@ -17,6 +17,7 @@ use {
|
||||
Request,
|
||||
},
|
||||
ruma::api::client::r0::uiaa::UiaaResponse,
|
||||
tracing::error,
|
||||
};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -29,12 +30,15 @@ pub enum Error {
|
||||
#[from]
|
||||
source: sled::Error,
|
||||
},
|
||||
#[cfg(feature = "rocksdb")]
|
||||
#[error("There was a problem with the connection to the rocksdb database: {source}")]
|
||||
RocksDbError {
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[error("There was a problem with the connection to the sqlite database: {source}")]
|
||||
SqliteError {
|
||||
#[from]
|
||||
source: rocksdb::Error,
|
||||
source: rusqlite::Error,
|
||||
},
|
||||
#[cfg(feature = "heed")]
|
||||
#[error("There was a problem with the connection to the heed database: {error}")]
|
||||
HeedError { error: String },
|
||||
#[error("Could not generate an image.")]
|
||||
ImageError {
|
||||
#[from]
|
||||
|
||||
+2
-2
@@ -10,10 +10,10 @@ mod ruma_wrapper;
|
||||
pub mod server_server;
|
||||
mod utils;
|
||||
|
||||
pub use database::Database;
|
||||
pub use database::{Config, Database};
|
||||
pub use error::{Error, Result};
|
||||
pub use pdu::PduEvent;
|
||||
pub use rocket::Config;
|
||||
pub use rocket::Config as RocketConfig;
|
||||
pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse};
|
||||
use std::ops::Deref;
|
||||
|
||||
|
||||
+84
-21
@@ -17,6 +17,7 @@ use std::sync::Arc;
|
||||
use database::Config;
|
||||
pub use database::Database;
|
||||
pub use error::{Error, Result};
|
||||
use opentelemetry::trace::{FutureExt, Tracer};
|
||||
pub use pdu::PduEvent;
|
||||
pub use rocket::State;
|
||||
use ruma::api::client::error::ErrorKind;
|
||||
@@ -30,10 +31,10 @@ use rocket::{
|
||||
},
|
||||
routes, Request,
|
||||
};
|
||||
use tracing::span;
|
||||
use tracing_subscriber::{prelude::*, Registry};
|
||||
use tokio::sync::RwLock;
|
||||
use tracing_subscriber::{prelude::*, EnvFilter};
|
||||
|
||||
fn setup_rocket(config: Figment, data: Arc<Database>) -> rocket::Rocket<rocket::Build> {
|
||||
fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<rocket::Build> {
|
||||
rocket::custom(config)
|
||||
.manage(data)
|
||||
.mount(
|
||||
@@ -49,6 +50,7 @@ fn setup_rocket(config: Figment, data: Arc<Database>) -> rocket::Rocket<rocket::
|
||||
client_server::logout_all_route,
|
||||
client_server::change_password_route,
|
||||
client_server::deactivate_route,
|
||||
client_server::third_party_route,
|
||||
client_server::get_capabilities_route,
|
||||
client_server::get_pushrules_all_route,
|
||||
client_server::set_pushrule_route,
|
||||
@@ -59,6 +61,7 @@ fn setup_rocket(config: Figment, data: Arc<Database>) -> rocket::Rocket<rocket::
|
||||
client_server::set_pushrule_actions_route,
|
||||
client_server::delete_pushrule_route,
|
||||
client_server::get_room_event_route,
|
||||
client_server::get_room_aliases_route,
|
||||
client_server::get_filter_route,
|
||||
client_server::create_filter_route,
|
||||
client_server::set_global_account_data_route,
|
||||
@@ -157,7 +160,8 @@ fn setup_rocket(config: Figment, data: Arc<Database>) -> rocket::Rocket<rocket::
|
||||
server_server::get_room_state_route,
|
||||
server_server::get_room_state_ids_route,
|
||||
server_server::create_join_event_template_route,
|
||||
server_server::create_join_event_route,
|
||||
server_server::create_join_event_v1_route,
|
||||
server_server::create_join_event_v2_route,
|
||||
server_server::create_invite_route,
|
||||
server_server::get_devices_route,
|
||||
server_server::get_room_information_route,
|
||||
@@ -184,7 +188,7 @@ async fn main() {
|
||||
std::env::set_var("CONDUIT_LOG_LEVEL", "off");
|
||||
|
||||
let raw_config =
|
||||
Figment::from(rocket::Config::release_default())
|
||||
Figment::from(default_config())
|
||||
.merge(
|
||||
Toml::file(Env::var("CONDUIT_CONFIG").expect(
|
||||
"The CONDUIT_CONFIG env var needs to be set. Example: /etc/conduit.toml",
|
||||
@@ -193,33 +197,65 @@ async fn main() {
|
||||
)
|
||||
.merge(Env::prefixed("CONDUIT_").global());
|
||||
|
||||
std::env::set_var("RUST_LOG", "warn");
|
||||
|
||||
let config = raw_config
|
||||
.extract::<Config>()
|
||||
.expect("It looks like your config is invalid. Please take a look at the error");
|
||||
|
||||
let db = Database::load_or_create(config.clone())
|
||||
.await
|
||||
.expect("config is valid");
|
||||
let start = async {
|
||||
config.warn_deprecated();
|
||||
|
||||
let db = Database::load_or_create(&config)
|
||||
.await
|
||||
.expect("config is valid");
|
||||
|
||||
let rocket = setup_rocket(raw_config, Arc::clone(&db))
|
||||
.ignite()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Database::start_on_shutdown_tasks(db, rocket.shutdown()).await;
|
||||
|
||||
rocket.launch().await.unwrap();
|
||||
};
|
||||
|
||||
if config.allow_jaeger {
|
||||
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
|
||||
.with_service_name("conduit")
|
||||
.install()
|
||||
opentelemetry::global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());
|
||||
let tracer = opentelemetry_jaeger::new_pipeline()
|
||||
.install_batch(opentelemetry::runtime::Tokio)
|
||||
.unwrap();
|
||||
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
|
||||
Registry::default().with(telemetry).try_init().unwrap();
|
||||
|
||||
let root = span!(tracing::Level::INFO, "app_start", work_units = 2);
|
||||
let _enter = root.enter();
|
||||
let span = tracer.start("conduit");
|
||||
start.with_current_context().await;
|
||||
drop(span);
|
||||
|
||||
let rocket = setup_rocket(raw_config, db);
|
||||
rocket.launch().await.unwrap();
|
||||
println!("exporting");
|
||||
opentelemetry::global::shutdown_tracer_provider();
|
||||
} else {
|
||||
std::env::set_var("RUST_LOG", config.log);
|
||||
tracing_subscriber::fmt::init();
|
||||
std::env::set_var("RUST_LOG", &config.log);
|
||||
|
||||
let rocket = setup_rocket(raw_config, db);
|
||||
rocket.launch().await.unwrap();
|
||||
let registry = tracing_subscriber::Registry::default();
|
||||
if config.tracing_flame {
|
||||
let (flame_layer, _guard) =
|
||||
tracing_flame::FlameLayer::with_file("./tracing.folded").unwrap();
|
||||
let flame_layer = flame_layer.with_empty_samples(false);
|
||||
|
||||
let filter_layer = EnvFilter::new("trace,h2=off");
|
||||
|
||||
let subscriber = registry.with(filter_layer).with(flame_layer);
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
start.await;
|
||||
} else {
|
||||
let fmt_layer = tracing_subscriber::fmt::Layer::new();
|
||||
let filter_layer = EnvFilter::try_from_default_env()
|
||||
.or_else(|_| EnvFilter::try_new("info"))
|
||||
.unwrap();
|
||||
|
||||
let subscriber = registry.with(filter_layer).with(fmt_layer);
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
start.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,3 +286,30 @@ fn missing_token_catcher() -> Result<()> {
|
||||
fn bad_json_catcher() -> Result<()> {
|
||||
Err(Error::BadRequest(ErrorKind::BadJson, "Bad json."))
|
||||
}
|
||||
|
||||
fn default_config() -> rocket::Config {
|
||||
let mut config = rocket::Config::release_default();
|
||||
|
||||
{
|
||||
let mut shutdown = &mut config.shutdown;
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use rocket::config::Sig;
|
||||
|
||||
shutdown.signals.insert(Sig::Term);
|
||||
shutdown.signals.insert(Sig::Int);
|
||||
}
|
||||
|
||||
// Once shutdown is triggered, this is the amount of seconds before rocket
|
||||
// will forcefully start shutting down connections, this gives enough time to /sync
|
||||
// requests and the like (which havent gotten the memo, somehow) to still complete gracefully.
|
||||
shutdown.grace = 35;
|
||||
|
||||
// After the grace period, rocket starts shutting down connections, and waits at least this
|
||||
// many seconds before forcefully shutting all of them down.
|
||||
shutdown.mercy = 10;
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
+20
-5
@@ -1,10 +1,9 @@
|
||||
use crate::Error;
|
||||
use log::error;
|
||||
use ruma::{
|
||||
events::{
|
||||
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent, 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, ServerName,
|
||||
@@ -13,6 +12,7 @@ use ruma::{
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
|
||||
use tracing::warn;
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||
pub struct PduEvent {
|
||||
@@ -322,12 +322,13 @@ pub(crate) fn gen_event_id_canonical_json(
|
||||
pdu: &Raw<ruma::events::pdu::Pdu>,
|
||||
) -> crate::Result<(EventId, CanonicalJsonObject)> {
|
||||
let value = serde_json::from_str(pdu.json().get()).map_err(|e| {
|
||||
error!("{:?}: {:?}", pdu, e);
|
||||
warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
|
||||
Error::BadServerResponse("Invalid PDU in server response")
|
||||
})?;
|
||||
|
||||
let event_id = EventId::try_from(&*format!(
|
||||
"${}",
|
||||
// Anything higher than version3 behaves the same
|
||||
ruma::signatures::reference_hash(&value, &RoomVersionId::Version6)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
@@ -346,3 +347,17 @@ pub struct PduBuilder {
|
||||
pub state_key: Option<String>,
|
||||
pub redacts: Option<EventId>,
|
||||
}
|
||||
|
||||
/// Direct conversion prevents loss of the empty `state_key` that ruma requires.
|
||||
impl From<AnyInitialStateEvent> for PduBuilder {
|
||||
fn from(event: AnyInitialStateEvent) -> Self {
|
||||
Self {
|
||||
event_type: EventType::from(event.event_type()),
|
||||
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()),
|
||||
redacts: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+34
-23
@@ -1,4 +1,4 @@
|
||||
use crate::Error;
|
||||
use crate::{database::DatabaseGuard, Error};
|
||||
use ruma::{
|
||||
api::{client::r0::uiaa::UiaaResponse, OutgoingResponse},
|
||||
identifiers::{DeviceId, UserId},
|
||||
@@ -9,21 +9,20 @@ use std::ops::Deref;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::{server_server, Database},
|
||||
log::{debug, warn},
|
||||
crate::server_server,
|
||||
rocket::{
|
||||
data::{self, ByteUnit, Data, FromData},
|
||||
http::Status,
|
||||
outcome::Outcome::*,
|
||||
response::{self, Responder},
|
||||
tokio::io::AsyncReadExt,
|
||||
Request, State,
|
||||
Request,
|
||||
},
|
||||
ruma::api::{AuthScheme, IncomingRequest},
|
||||
std::collections::BTreeMap,
|
||||
std::convert::TryFrom,
|
||||
std::io::Cursor,
|
||||
std::sync::Arc,
|
||||
tracing::{debug, warn},
|
||||
};
|
||||
|
||||
/// This struct converts rocket requests into ruma structs by converting them into http requests
|
||||
@@ -46,10 +45,14 @@ where
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
async fn from_data(request: &'a Request<'_>, data: Data) -> data::Outcome<Self, Self::Error> {
|
||||
#[tracing::instrument(skip(request, data))]
|
||||
async fn from_data(
|
||||
request: &'a Request<'_>,
|
||||
data: Data<'a>,
|
||||
) -> data::Outcome<'a, Self, Self::Error> {
|
||||
let metadata = T::Incoming::METADATA;
|
||||
let db = request
|
||||
.guard::<State<'_, Arc<Database>>>()
|
||||
.guard::<DatabaseGuard>()
|
||||
.await
|
||||
.expect("database was loaded");
|
||||
|
||||
@@ -63,7 +66,11 @@ where
|
||||
let limit = db.globals.max_request_size();
|
||||
let mut handle = data.open(ByteUnit::Byte(limit.into()));
|
||||
let mut body = Vec::new();
|
||||
handle.read_to_end(&mut body).await.unwrap();
|
||||
if let Err(_) = handle.read_to_end(&mut body).await {
|
||||
// Client disconnected
|
||||
// Missing Token
|
||||
return Failure((Status::new(582), ()));
|
||||
}
|
||||
|
||||
let mut json_body = serde_json::from_slice::<CanonicalJsonValue>(&body).ok();
|
||||
|
||||
@@ -72,9 +79,9 @@ where
|
||||
registration,
|
||||
)) = db
|
||||
.appservice
|
||||
.iter_all()
|
||||
.all()
|
||||
.unwrap()
|
||||
.filter_map(|r| r.ok())
|
||||
.iter()
|
||||
.find(|(_id, registration)| {
|
||||
registration
|
||||
.get("as_token")
|
||||
@@ -103,7 +110,7 @@ where
|
||||
|
||||
if !db.users.exists(&user_id).unwrap() {
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
|
||||
// TODO: Check if appservice is allowed to be that user
|
||||
@@ -118,7 +125,7 @@ where
|
||||
if let Some(token) = token {
|
||||
match db.users.find_from_token(&token).unwrap() {
|
||||
// Unknown Token
|
||||
None => return Failure((Status::raw(581), ())),
|
||||
None => return Failure((Status::new(581), ())),
|
||||
Some((user_id, device_id)) => (
|
||||
Some(user_id),
|
||||
Some(Box::<DeviceId>::from(device_id)),
|
||||
@@ -128,7 +135,7 @@ where
|
||||
}
|
||||
} else {
|
||||
// Missing Token
|
||||
return Failure((Status::raw(582), ()));
|
||||
return Failure((Status::new(582), ()));
|
||||
}
|
||||
}
|
||||
AuthScheme::ServerSignatures => {
|
||||
@@ -150,7 +157,7 @@ where
|
||||
warn!("No Authorization header");
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -160,7 +167,7 @@ where
|
||||
warn!("Invalid X-Matrix header origin field: {:?}", x_matrix);
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -173,7 +180,7 @@ where
|
||||
);
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -183,7 +190,7 @@ where
|
||||
warn!("Invalid X-Matrix header key field: {:?}", x_matrix);
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -193,7 +200,7 @@ where
|
||||
warn!("Invalid X-Matrix header sig field: {:?}", x_matrix);
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -244,7 +251,7 @@ where
|
||||
warn!("Failed to fetch signing keys: {}", e);
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -254,14 +261,17 @@ where
|
||||
match ruma::signatures::verify_json(&pub_key_map, &request_map) {
|
||||
Ok(()) => (None, None, Some(origin), false),
|
||||
Err(e) => {
|
||||
warn!("Failed to verify json request from {}: {}", origin, e);
|
||||
warn!(
|
||||
"Failed to verify json request from {}: {}\n{:?}",
|
||||
origin, e, request_map
|
||||
);
|
||||
|
||||
if request.uri().to_string().contains('@') {
|
||||
warn!("Request uri contained '@' character. Make sure your reverse proxy gives Conduit the raw uri (apache: use nocanon)");
|
||||
}
|
||||
|
||||
// Forbidden
|
||||
return Failure((Status::raw(580), ()));
|
||||
return Failure((Status::new(580), ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -318,7 +328,8 @@ where
|
||||
}),
|
||||
Err(e) => {
|
||||
warn!("{:?}", e);
|
||||
Failure((Status::raw(583), ()))
|
||||
// Bad Json
|
||||
Failure((Status::new(583), ()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -344,7 +355,7 @@ pub fn response<T: OutgoingResponse>(response: RumaResponse<T>) -> response::Res
|
||||
let mut response = rocket::response::Response::build();
|
||||
|
||||
let status = http_response.status();
|
||||
response.raw_status(status.into(), "");
|
||||
response.status(Status::new(status.as_u16()));
|
||||
|
||||
for header in http_response.headers() {
|
||||
response.raw_header(header.0.to_string(), header.1.to_str().unwrap().to_owned());
|
||||
|
||||
+1645
-645
File diff suppressed because it is too large
Load Diff
+9
-10
@@ -9,6 +9,7 @@ use std::{
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn millis_since_unix_epoch() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
@@ -16,15 +17,6 @@ pub fn millis_since_unix_epoch() -> u64 {
|
||||
.as_millis() as u64
|
||||
}
|
||||
|
||||
#[cfg(feature = "rocksdb")]
|
||||
pub fn increment_rocksdb(
|
||||
_new_key: &[u8],
|
||||
old: Option<&[u8]>,
|
||||
_operands: &mut rocksdb::MergeOperands,
|
||||
) -> Option<Vec<u8>> {
|
||||
increment(old)
|
||||
}
|
||||
|
||||
pub fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
|
||||
let number = match old.map(|bytes| bytes.try_into()) {
|
||||
Some(Ok(bytes)) => {
|
||||
@@ -48,16 +40,19 @@ pub fn generate_keypair() -> Vec<u8> {
|
||||
}
|
||||
|
||||
/// Parses the bytes into an u64.
|
||||
#[tracing::instrument(skip(bytes))]
|
||||
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
|
||||
let array: [u8; 8] = bytes.try_into()?;
|
||||
Ok(u64::from_be_bytes(array))
|
||||
}
|
||||
|
||||
/// Parses the bytes into a string.
|
||||
#[tracing::instrument(skip(bytes))]
|
||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
|
||||
String::from_utf8(bytes.to_vec())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(length))]
|
||||
pub fn random_string(length: usize) -> String {
|
||||
thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
@@ -67,6 +62,7 @@ pub fn random_string(length: usize) -> String {
|
||||
}
|
||||
|
||||
/// Calculate a new hash for the given password
|
||||
#[tracing::instrument(skip(password))]
|
||||
pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
|
||||
let hashing_config = Config {
|
||||
variant: Variant::Argon2id,
|
||||
@@ -77,6 +73,7 @@ pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
|
||||
argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(iterators, check_order))]
|
||||
pub fn common_elements(
|
||||
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>,
|
||||
check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
||||
@@ -104,6 +101,7 @@ pub fn common_elements(
|
||||
/// Fallible conversion from any value that implements `Serialize` to a `CanonicalJsonObject`.
|
||||
///
|
||||
/// `value` must serialize to an `serde_json::Value::Object`.
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn to_canonical_object<T: serde::Serialize>(
|
||||
value: T,
|
||||
) -> Result<CanonicalJsonObject, CanonicalJsonError> {
|
||||
@@ -117,6 +115,7 @@ pub fn to_canonical_object<T: serde::Serialize>(
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(deserializer))]
|
||||
pub fn deserialize_from_str<
|
||||
'de,
|
||||
D: serde::de::Deserializer<'de>,
|
||||
@@ -137,7 +136,7 @@ pub fn deserialize_from_str<
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
v.parse().map_err(|e| serde::de::Error::custom(e))
|
||||
v.parse().map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
deserializer.deserialize_str(Visitor(std::marker::PhantomData))
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
const puppeteer = require('puppeteer');
|
||||
|
||||
run().then(() => console.log('Done')).catch(error => {
|
||||
console.error("Registration test failed.");
|
||||
console.error("There might be a screenshot of the failure in the artifacts.\n");
|
||||
console.error(error);
|
||||
process.exit(111);
|
||||
});
|
||||
|
||||
async function run() {
|
||||
|
||||
const elementUrl = process.argv[process.argv.length - 2];
|
||||
console.debug("Testing registration with ElementWeb hosted at "+ elementUrl);
|
||||
|
||||
const homeserverUrl = process.argv[process.argv.length - 1];
|
||||
console.debug("Homeserver url: "+ homeserverUrl);
|
||||
|
||||
const username = "testuser" + String(Math.floor(Math.random() * 100000));
|
||||
const password = "testpassword" + String(Math.floor(Math.random() * 100000));
|
||||
console.debug("Testuser for this run:\n User: " + username + "\n Password: " + password);
|
||||
|
||||
const browser = await puppeteer.launch({
|
||||
headless: true, args: [
|
||||
"--no-sandbox"
|
||||
]
|
||||
});
|
||||
|
||||
const page = await browser.newPage();
|
||||
await page.goto(elementUrl);
|
||||
|
||||
await page.screenshot({ path: '01-element-web-opened.png' });
|
||||
|
||||
console.debug("Click [Create Account] button");
|
||||
await page.waitForSelector('a.mx_ButtonCreateAccount');
|
||||
await page.click('a.mx_ButtonCreateAccount');
|
||||
|
||||
await page.screenshot({ path: '02-clicked-create-account-button.png' });
|
||||
|
||||
// The webapp should have loaded right now, if anything takes more than 5 seconds, something probably broke
|
||||
page.setDefaultTimeout(5000);
|
||||
|
||||
console.debug("Click [Edit] to switch homeserver");
|
||||
await page.waitForSelector('div.mx_ServerPicker_change');
|
||||
await page.click('div.mx_ServerPicker_change');
|
||||
|
||||
await page.screenshot({ path: '03-clicked-edit-homeserver-button.png' });
|
||||
|
||||
console.debug("Type in local homeserver url");
|
||||
await page.waitForSelector('input#mx_homeserverInput');
|
||||
await page.click('input#mx_homeserverInput');
|
||||
await page.click('input#mx_homeserverInput');
|
||||
await page.keyboard.type(homeserverUrl);
|
||||
|
||||
await page.screenshot({ path: '04-typed-in-homeserver.png' });
|
||||
|
||||
console.debug("[Continue] with changed homeserver");
|
||||
await page.waitForSelector("div.mx_ServerPickerDialog_continue");
|
||||
await page.click('div.mx_ServerPickerDialog_continue');
|
||||
|
||||
await page.screenshot({ path: '05-back-to-enter-user-credentials.png' });
|
||||
|
||||
console.debug("Type in username");
|
||||
await page.waitForSelector("input#mx_RegistrationForm_username");
|
||||
await page.click('input#mx_RegistrationForm_username');
|
||||
await page.keyboard.type(username);
|
||||
|
||||
await page.screenshot({ path: '06-typed-in-username.png' });
|
||||
|
||||
console.debug("Type in password");
|
||||
await page.waitForSelector("input#mx_RegistrationForm_password");
|
||||
await page.click('input#mx_RegistrationForm_password');
|
||||
await page.keyboard.type(password);
|
||||
|
||||
await page.screenshot({ path: '07-typed-in-password-once.png' });
|
||||
|
||||
console.debug("Type in password again");
|
||||
await page.waitForSelector("input#mx_RegistrationForm_passwordConfirm");
|
||||
await page.click('input#mx_RegistrationForm_passwordConfirm');
|
||||
await page.keyboard.type(password);
|
||||
|
||||
await page.screenshot({ path: '08-typed-in-password-twice.png' });
|
||||
|
||||
console.debug("Click on [Register] to finish the account creation");
|
||||
await page.waitForSelector("input.mx_Login_submit");
|
||||
await page.click('input.mx_Login_submit');
|
||||
|
||||
await page.screenshot({ path: '09-clicked-on-register-button.png' });
|
||||
|
||||
// Waiting for the app to login can take some time, so be patient.
|
||||
page.setDefaultTimeout(10000);
|
||||
|
||||
console.debug("Wait for chat window to show up");
|
||||
await page.waitForSelector("div.mx_HomePage_default_buttons");
|
||||
console.debug("Apparently the registration worked.");
|
||||
|
||||
await page.screenshot({ path: '10-logged-in-homescreen.png' });
|
||||
|
||||
|
||||
// Close the browser and exit the script
|
||||
await browser.close();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
[global]
|
||||
|
||||
# Server runs in same container as tests do, so localhost is fine
|
||||
server_name = "localhost"
|
||||
|
||||
# With a bit of luck /tmp is a RAM disk, so that the file system does not become the bottleneck while testing
|
||||
database_path = "/tmp"
|
||||
|
||||
# All the other settings are left at their defaults:
|
||||
port = 6167
|
||||
max_request_size = 20_000_000
|
||||
allow_registration = true
|
||||
trusted_servers = ["matrix.org"]
|
||||
address = "127.0.0.1"
|
||||
proxy = "none"
|
||||
Reference in New Issue
Block a user