mirror of https://gitlab.com/famedly/conduit
Merge remote-tracking branch 'origin/next'
commit
53f14a2c4c
@ -0,0 +1,5 @@
|
|||||||
|
# Nix things
|
||||||
|
.envrc @CobaltCause
|
||||||
|
flake.lock @CobaltCause
|
||||||
|
flake.nix @CobaltCause
|
||||||
|
nix/ @CobaltCause
|
@ -0,0 +1,37 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eux
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------- #
|
||||||
|
# #
|
||||||
|
# Configures docker buildx to use a remote server for arm building. #
|
||||||
|
# Expects $SSH_PRIVATE_KEY to be a valid ssh ed25519 private key with #
|
||||||
|
# access to the server $ARM_SERVER_USER@$ARM_SERVER_IP #
|
||||||
|
# #
|
||||||
|
# This is expected to only be used in the official CI/CD pipeline! #
|
||||||
|
# #
|
||||||
|
# Requirements: openssh-client, docker buildx #
|
||||||
|
# Inspired by: https://depot.dev/blog/building-arm-containers #
|
||||||
|
# #
|
||||||
|
# --------------------------------------------------------------------- #
|
||||||
|
|
||||||
|
cat "$BUILD_SERVER_SSH_PRIVATE_KEY" | ssh-add -
|
||||||
|
|
||||||
|
# Test server connections:
|
||||||
|
ssh "$ARM_SERVER_USER@$ARM_SERVER_IP" "uname -a"
|
||||||
|
ssh "$AMD_SERVER_USER@$AMD_SERVER_IP" "uname -a"
|
||||||
|
|
||||||
|
# Connect remote arm64 server for all arm builds:
|
||||||
|
docker buildx create \
|
||||||
|
--name "multi" \
|
||||||
|
--driver "docker-container" \
|
||||||
|
--platform "linux/arm64,linux/arm/v7" \
|
||||||
|
"ssh://$ARM_SERVER_USER@$ARM_SERVER_IP"
|
||||||
|
|
||||||
|
# Connect remote amd64 server for adm64 builds:
|
||||||
|
docker buildx create --append \
|
||||||
|
--name "multi" \
|
||||||
|
--driver "docker-container" \
|
||||||
|
--platform "linux/amd64" \
|
||||||
|
"ssh://$AMD_SERVER_USER@$AMD_SERVER_IP"
|
||||||
|
|
||||||
|
docker buildx use multi
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"rust-analyzer.procMacro.enable": true,
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,102 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"fenix": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
],
|
||||||
|
"rust-analyzer-src": "rust-analyzer-src"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1665815894,
|
||||||
|
"narHash": "sha256-Vboo1L4NMGLKZKVLnOPi9OHlae7uoNyfgvyIUm+SVXE=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "fenix",
|
||||||
|
"rev": "2348450241a5f945f0ba07e44ecbfac2f541d7f4",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "fenix",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-utils": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1659877975,
|
||||||
|
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"naersk": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1662220400,
|
||||||
|
"narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "naersk",
|
||||||
|
"rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "naersk",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1665856037,
|
||||||
|
"narHash": "sha256-/RvIWnGKdTSoIq5Xc2HwPIL0TzRslzU6Rqk4Img6UNg=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "c95ebc5125ffffcd431df0ad8620f0926b8125b8",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"fenix": "fenix",
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"naersk": "naersk",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-analyzer-src": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1665765556,
|
||||||
|
"narHash": "sha256-w9L5j0TIB5ay4aRwzGCp8mgvGsu5dVJQvbEFutwr6xE=",
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"rev": "018b8429cf3fa9d8aed916704e41dfedeb0f4f78",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"ref": "nightly",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
|
||||||
|
fenix = {
|
||||||
|
url = "github:nix-community/fenix";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
naersk = {
|
||||||
|
url = "github:nix-community/naersk";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{ self
|
||||||
|
, nixpkgs
|
||||||
|
, flake-utils
|
||||||
|
|
||||||
|
, fenix
|
||||||
|
, naersk
|
||||||
|
}: flake-utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
|
||||||
|
# Nix-accessible `Cargo.toml`
|
||||||
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
||||||
|
|
||||||
|
# The Rust toolchain to use
|
||||||
|
toolchain = fenix.packages.${system}.toolchainOf {
|
||||||
|
# Use the Rust version defined in `Cargo.toml`
|
||||||
|
channel = cargoToml.package.rust-version;
|
||||||
|
|
||||||
|
# This will need to be updated when `package.rust-version` is changed in
|
||||||
|
# `Cargo.toml`
|
||||||
|
sha256 = "sha256-KXx+ID0y4mg2B3LHp7IyaiMrdexF6octADnAtFIOjrY=";
|
||||||
|
};
|
||||||
|
|
||||||
|
builder = (pkgs.callPackage naersk {
|
||||||
|
inherit (toolchain) rustc cargo;
|
||||||
|
}).buildPackage;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
packages.default = builder {
|
||||||
|
src = ./.;
|
||||||
|
|
||||||
|
nativeBuildInputs = (with pkgs.rustPlatform; [
|
||||||
|
bindgenHook
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
# Rust Analyzer needs to be able to find the path to default crate
|
||||||
|
# sources, and it can read this environment variable to do so
|
||||||
|
RUST_SRC_PATH = "${toolchain.rust-src}/lib/rustlib/src/rust/library";
|
||||||
|
|
||||||
|
# Development tools
|
||||||
|
nativeBuildInputs = (with pkgs.rustPlatform; [
|
||||||
|
bindgenHook
|
||||||
|
]) ++ (with toolchain; [
|
||||||
|
cargo
|
||||||
|
clippy
|
||||||
|
rust-src
|
||||||
|
rustc
|
||||||
|
rustfmt
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
checks = {
|
||||||
|
packagesDefault = self.packages.${system}.default;
|
||||||
|
devShellsDefault = self.devShells.${system}.default;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,188 @@
|
|||||||
|
# Conduit for Nix/NixOS
|
||||||
|
|
||||||
|
This guide assumes you have a recent version of Nix (^2.4) installed.
|
||||||
|
|
||||||
|
Since Conduit ships as a Nix flake, you'll first need to [enable
|
||||||
|
flakes][enable_flakes].
|
||||||
|
|
||||||
|
You can now use the usual Nix commands to interact with Conduit's flake. For
|
||||||
|
example, `nix run gitlab:famedly/conduit` will run Conduit (though you'll need
|
||||||
|
to provide configuration and such manually as usual).
|
||||||
|
|
||||||
|
If your NixOS configuration is defined as a flake, you can depend on this flake
|
||||||
|
to provide a more up-to-date version than provided by `nixpkgs`. In your flake,
|
||||||
|
add the following to your `inputs`:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
conduit = {
|
||||||
|
url = "gitlab:famedly/conduit";
|
||||||
|
|
||||||
|
# Assuming you have an input for nixpkgs called `nixpkgs`. If you experience
|
||||||
|
# build failures while using this, try commenting/deleting this line. This
|
||||||
|
# will probably also require you to always build from source.
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Next, make sure you're passing your flake inputs to the `specialArgs` argument
|
||||||
|
of `nixpkgs.lib.nixosSystem` [as explained here][specialargs]. This guide will
|
||||||
|
assume you've named the group `flake-inputs`.
|
||||||
|
|
||||||
|
Now you can configure Conduit and a reverse proxy for it. Add the following to
|
||||||
|
a new Nix file and include it in your configuration:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
{ config
|
||||||
|
, pkgs
|
||||||
|
, flake-inputs
|
||||||
|
, ...
|
||||||
|
}:
|
||||||
|
|
||||||
|
let
|
||||||
|
# You'll need to edit these values
|
||||||
|
|
||||||
|
# The hostname that will appear in your user and room IDs
|
||||||
|
server_name = "example.com";
|
||||||
|
|
||||||
|
# The hostname that Conduit actually runs on
|
||||||
|
#
|
||||||
|
# This can be the same as `server_name` if you want. This is only necessary
|
||||||
|
# when Conduit is running on a different machine than the one hosting your
|
||||||
|
# root domain. This configuration also assumes this is all running on a single
|
||||||
|
# machine, some tweaks will need to be made if this is not the case.
|
||||||
|
matrix_hostname = "matrix.${server_name}";
|
||||||
|
|
||||||
|
# An admin email for TLS certificate notifications
|
||||||
|
admin_email = "admin@${server_name}";
|
||||||
|
|
||||||
|
# These ones you can leave alone
|
||||||
|
|
||||||
|
# Build a dervation that stores the content of `${server_name}/.well-known/matrix/server`
|
||||||
|
well_known_server = pkgs.writeText "well-known-matrix-server" ''
|
||||||
|
{
|
||||||
|
"m.server": "${matrix_hostname}"
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
|
||||||
|
# Build a dervation that stores the content of `${server_name}/.well-known/matrix/client`
|
||||||
|
well_known_client = pkgs.writeText "well-known-matrix-client" ''
|
||||||
|
{
|
||||||
|
"m.homeserver": {
|
||||||
|
"base_url": "https://${matrix_hostname}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
in
|
||||||
|
|
||||||
|
{
|
||||||
|
# Configure Conduit itself
|
||||||
|
services.matrix-conduit = {
|
||||||
|
enable = true;
|
||||||
|
|
||||||
|
# This causes NixOS to use the flake defined in this repository instead of
|
||||||
|
# the build of Conduit built into nixpkgs.
|
||||||
|
package = flake-inputs.conduit.packages.${pkgs.system}.default;
|
||||||
|
|
||||||
|
settings.global = {
|
||||||
|
inherit server_name;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
# Configure automated TLS acquisition/renewal
|
||||||
|
security.acme = {
|
||||||
|
acceptTerms = true;
|
||||||
|
defaults = {
|
||||||
|
email = admin_email;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
# ACME data must be readable by the NGINX user
|
||||||
|
users.users.nginx.extraGroups = [
|
||||||
|
"acme"
|
||||||
|
];
|
||||||
|
|
||||||
|
# Configure NGINX as a reverse proxy
|
||||||
|
services.nginx = {
|
||||||
|
enable = true;
|
||||||
|
recommendedProxySettings = true;
|
||||||
|
|
||||||
|
virtualHosts = {
|
||||||
|
"${server_name}" = {
|
||||||
|
forceSSL = true;
|
||||||
|
enableACME = true;
|
||||||
|
|
||||||
|
listen = [
|
||||||
|
{
|
||||||
|
addr = "0.0.0.0";
|
||||||
|
port = 443;
|
||||||
|
ssl = true;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
addr = "0.0.0.0";
|
||||||
|
port = 8448;
|
||||||
|
ssl = true;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
extraConfig = ''
|
||||||
|
merge_slashes off;
|
||||||
|
'';
|
||||||
|
|
||||||
|
"${matrix_hostname}" = {
|
||||||
|
forceSSL = true;
|
||||||
|
enableACME = true;
|
||||||
|
|
||||||
|
locations."/_matrix/" = {
|
||||||
|
proxyPass = "http://backend_conduit$request_uri";
|
||||||
|
proxyWebsockets = true;
|
||||||
|
extraConfig = ''
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_buffering off;
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
locations."=/.well-known/matrix/server" = {
|
||||||
|
# Use the contents of the derivation built previously
|
||||||
|
alias = "${well_known_server}";
|
||||||
|
|
||||||
|
extraConfig = ''
|
||||||
|
# Set the header since by default NGINX thinks it's just bytes
|
||||||
|
default_type application/json;
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
locations."=/.well-known/matrix/client" = {
|
||||||
|
# Use the contents of the derivation built previously
|
||||||
|
alias = "${well_known_client}";
|
||||||
|
|
||||||
|
extraConfig = ''
|
||||||
|
# Set the header since by default NGINX thinks it's just bytes
|
||||||
|
default_type application/json;
|
||||||
|
|
||||||
|
# https://matrix.org/docs/spec/client_server/r0.4.0#web-browser-clients
|
||||||
|
add_header Access-Control-Allow-Origin "*";
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
upstreams = {
|
||||||
|
"backend_conduit" = {
|
||||||
|
servers = {
|
||||||
|
"localhost:${toString config.services.matrix-conduit.settings.global.port}" = { };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
# Open firewall ports for HTTP, HTTPS, and Matrix federation
|
||||||
|
networking.firewall.allowedTCPPorts = [ 80 443 8448 ];
|
||||||
|
networking.firewall.allowedUDPPorts = [ 80 443 8448 ];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Now you can rebuild your system configuration and you should be good to go!
|
||||||
|
|
||||||
|
[enable_flakes]: https://nixos.wiki/wiki/Flakes#Enable_flakes
|
||||||
|
|
||||||
|
[specialargs]: https://nixos.wiki/wiki/Flakes#Using_nix_flakes_with_NixOS
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,162 @@
|
|||||||
|
use crate::{services, Error, Result, Ruma};
|
||||||
|
use ruma::{
|
||||||
|
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
|
||||||
|
events::{
|
||||||
|
receipt::{ReceiptThread, ReceiptType},
|
||||||
|
RoomAccountDataEventType,
|
||||||
|
},
|
||||||
|
MilliSecondsSinceUnixEpoch,
|
||||||
|
};
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
/// # `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
|
||||||
|
pub async fn set_read_marker_route(
|
||||||
|
body: Ruma<set_read_marker::v3::Request>,
|
||||||
|
) -> Result<set_read_marker::v3::Response> {
|
||||||
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
if let Some(fully_read) = &body.fully_read {
|
||||||
|
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
||||||
|
content: ruma::events::fully_read::FullyReadEventContent {
|
||||||
|
event_id: fully_read.clone(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
services().account_data.update(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::FullyRead,
|
||||||
|
&serde_json::to_value(fully_read_event).expect("to json value always works"),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if body.private_read_receipt.is_some() || body.read_receipt.is_some() {
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.user
|
||||||
|
.reset_notification_counts(sender_user, &body.room_id)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(event) = &body.private_read_receipt {
|
||||||
|
services().rooms.edus.read_receipt.private_read_set(
|
||||||
|
&body.room_id,
|
||||||
|
sender_user,
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.get_pdu_count(event)?
|
||||||
|
.ok_or(Error::BadRequest(
|
||||||
|
ErrorKind::InvalidParam,
|
||||||
|
"Event does not exist.",
|
||||||
|
))?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(event) = &body.read_receipt {
|
||||||
|
let mut user_receipts = BTreeMap::new();
|
||||||
|
user_receipts.insert(
|
||||||
|
sender_user.clone(),
|
||||||
|
ruma::events::receipt::Receipt {
|
||||||
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
||||||
|
thread: ReceiptThread::Unthreaded,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut receipts = BTreeMap::new();
|
||||||
|
receipts.insert(ReceiptType::Read, user_receipts);
|
||||||
|
|
||||||
|
let mut receipt_content = BTreeMap::new();
|
||||||
|
receipt_content.insert(event.to_owned(), receipts);
|
||||||
|
|
||||||
|
services().rooms.edus.read_receipt.readreceipt_update(
|
||||||
|
sender_user,
|
||||||
|
&body.room_id,
|
||||||
|
ruma::events::receipt::ReceiptEvent {
|
||||||
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
||||||
|
room_id: body.room_id.clone(),
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(set_read_marker::v3::Response {})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}`
|
||||||
|
///
|
||||||
|
/// Sets private read marker and public read receipt EDU.
|
||||||
|
pub async fn create_receipt_route(
|
||||||
|
body: Ruma<create_receipt::v3::Request>,
|
||||||
|
) -> Result<create_receipt::v3::Response> {
|
||||||
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
if matches!(
|
||||||
|
&body.receipt_type,
|
||||||
|
create_receipt::v3::ReceiptType::Read | create_receipt::v3::ReceiptType::ReadPrivate
|
||||||
|
) {
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.user
|
||||||
|
.reset_notification_counts(sender_user, &body.room_id)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
match body.receipt_type {
|
||||||
|
create_receipt::v3::ReceiptType::FullyRead => {
|
||||||
|
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
||||||
|
content: ruma::events::fully_read::FullyReadEventContent {
|
||||||
|
event_id: body.event_id.clone(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
services().account_data.update(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::FullyRead,
|
||||||
|
&serde_json::to_value(fully_read_event).expect("to json value always works"),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
create_receipt::v3::ReceiptType::Read => {
|
||||||
|
let mut user_receipts = BTreeMap::new();
|
||||||
|
user_receipts.insert(
|
||||||
|
sender_user.clone(),
|
||||||
|
ruma::events::receipt::Receipt {
|
||||||
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
||||||
|
thread: ReceiptThread::Unthreaded,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let mut receipts = BTreeMap::new();
|
||||||
|
receipts.insert(ReceiptType::Read, user_receipts);
|
||||||
|
|
||||||
|
let mut receipt_content = BTreeMap::new();
|
||||||
|
receipt_content.insert(body.event_id.to_owned(), receipts);
|
||||||
|
|
||||||
|
services().rooms.edus.read_receipt.readreceipt_update(
|
||||||
|
sender_user,
|
||||||
|
&body.room_id,
|
||||||
|
ruma::events::receipt::ReceiptEvent {
|
||||||
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
||||||
|
room_id: body.room_id.clone(),
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
create_receipt::v3::ReceiptType::ReadPrivate => {
|
||||||
|
services().rooms.edus.read_receipt.private_read_set(
|
||||||
|
&body.room_id,
|
||||||
|
sender_user,
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.get_pdu_count(&body.event_id)?
|
||||||
|
.ok_or(Error::BadRequest(
|
||||||
|
ErrorKind::InvalidParam,
|
||||||
|
"Event does not exist.",
|
||||||
|
))?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
_ => return Err(Error::bad_database("Unsupported receipt type")),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(create_receipt::v3::Response {})
|
||||||
|
}
|
@ -0,0 +1,126 @@
|
|||||||
|
use crate::{services, Error, Result, Ruma};
|
||||||
|
use ruma::{
|
||||||
|
api::client::tag::{create_tag, delete_tag, get_tags},
|
||||||
|
events::{
|
||||||
|
tag::{TagEvent, TagEventContent},
|
||||||
|
RoomAccountDataEventType,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
/// # `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.
|
||||||
|
pub async fn update_tag_route(
|
||||||
|
body: Ruma<create_tag::v3::Request>,
|
||||||
|
) -> Result<create_tag::v3::Response> {
|
||||||
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
let event = services().account_data.get(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::Tag,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut tags_event = event
|
||||||
|
.map(|e| {
|
||||||
|
serde_json::from_str(e.get())
|
||||||
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
Ok(TagEvent {
|
||||||
|
content: TagEventContent {
|
||||||
|
tags: BTreeMap::new(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
tags_event
|
||||||
|
.content
|
||||||
|
.tags
|
||||||
|
.insert(body.tag.clone().into(), body.tag_info.clone());
|
||||||
|
|
||||||
|
services().account_data.update(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::Tag,
|
||||||
|
&serde_json::to_value(tags_event).expect("to json value always works"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(create_tag::v3::Response {})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # `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.
|
||||||
|
pub async fn delete_tag_route(
|
||||||
|
body: Ruma<delete_tag::v3::Request>,
|
||||||
|
) -> Result<delete_tag::v3::Response> {
|
||||||
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
let event = services().account_data.get(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::Tag,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut tags_event = event
|
||||||
|
.map(|e| {
|
||||||
|
serde_json::from_str(e.get())
|
||||||
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
Ok(TagEvent {
|
||||||
|
content: TagEventContent {
|
||||||
|
tags: BTreeMap::new(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
tags_event.content.tags.remove(&body.tag.clone().into());
|
||||||
|
|
||||||
|
services().account_data.update(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::Tag,
|
||||||
|
&serde_json::to_value(tags_event).expect("to json value always works"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(delete_tag::v3::Response {})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags`
|
||||||
|
///
|
||||||
|
/// Returns tags on the room.
|
||||||
|
///
|
||||||
|
/// - Gets the tag event of the room account data.
|
||||||
|
pub async fn get_tags_route(body: Ruma<get_tags::v3::Request>) -> Result<get_tags::v3::Response> {
|
||||||
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
let event = services().account_data.get(
|
||||||
|
Some(&body.room_id),
|
||||||
|
sender_user,
|
||||||
|
RoomAccountDataEventType::Tag,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let tags_event = event
|
||||||
|
.map(|e| {
|
||||||
|
serde_json::from_str(e.get())
|
||||||
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
Ok(TagEvent {
|
||||||
|
content: TagEventContent {
|
||||||
|
tags: BTreeMap::new(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(get_tags::v3::Response {
|
||||||
|
tags: tags_event.content.tags,
|
||||||
|
})
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
pub mod appservice_server;
|
||||||
|
pub mod client_server;
|
||||||
|
pub mod ruma_wrapper;
|
||||||
|
pub mod server_server;
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,127 +0,0 @@
|
|||||||
use crate::{database::DatabaseGuard, Error, Result, Ruma};
|
|
||||||
use ruma::{
|
|
||||||
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
|
|
||||||
events::RoomAccountDataEventType,
|
|
||||||
receipt::ReceiptType,
|
|
||||||
MilliSecondsSinceUnixEpoch,
|
|
||||||
};
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
/// # `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
|
|
||||||
pub async fn set_read_marker_route(
|
|
||||||
db: DatabaseGuard,
|
|
||||||
body: Ruma<set_read_marker::v3::IncomingRequest>,
|
|
||||||
) -> Result<set_read_marker::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
|
||||||
content: ruma::events::fully_read::FullyReadEventContent {
|
|
||||||
event_id: body.fully_read.clone(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
db.account_data.update(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::FullyRead,
|
|
||||||
&fully_read_event,
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if let Some(event) = &body.read_receipt {
|
|
||||||
db.rooms.edus.private_read_set(
|
|
||||||
&body.room_id,
|
|
||||||
sender_user,
|
|
||||||
db.rooms.get_pdu_count(event)?.ok_or(Error::BadRequest(
|
|
||||||
ErrorKind::InvalidParam,
|
|
||||||
"Event does not exist.",
|
|
||||||
))?,
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
db.rooms
|
|
||||||
.reset_notification_counts(sender_user, &body.room_id)?;
|
|
||||||
|
|
||||||
let mut user_receipts = BTreeMap::new();
|
|
||||||
user_receipts.insert(
|
|
||||||
sender_user.clone(),
|
|
||||||
ruma::events::receipt::Receipt {
|
|
||||||
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut receipts = BTreeMap::new();
|
|
||||||
receipts.insert(ReceiptType::Read, user_receipts);
|
|
||||||
|
|
||||||
let mut receipt_content = BTreeMap::new();
|
|
||||||
receipt_content.insert(event.to_owned(), receipts);
|
|
||||||
|
|
||||||
db.rooms.edus.readreceipt_update(
|
|
||||||
sender_user,
|
|
||||||
&body.room_id,
|
|
||||||
ruma::events::receipt::ReceiptEvent {
|
|
||||||
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
||||||
room_id: body.room_id.clone(),
|
|
||||||
},
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.flush()?;
|
|
||||||
|
|
||||||
Ok(set_read_marker::v3::Response {})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}`
|
|
||||||
///
|
|
||||||
/// Sets private read marker and public read receipt EDU.
|
|
||||||
pub async fn create_receipt_route(
|
|
||||||
db: DatabaseGuard,
|
|
||||||
body: Ruma<create_receipt::v3::IncomingRequest>,
|
|
||||||
) -> Result<create_receipt::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
db.rooms.edus.private_read_set(
|
|
||||||
&body.room_id,
|
|
||||||
sender_user,
|
|
||||||
db.rooms
|
|
||||||
.get_pdu_count(&body.event_id)?
|
|
||||||
.ok_or(Error::BadRequest(
|
|
||||||
ErrorKind::InvalidParam,
|
|
||||||
"Event does not exist.",
|
|
||||||
))?,
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
db.rooms
|
|
||||||
.reset_notification_counts(sender_user, &body.room_id)?;
|
|
||||||
|
|
||||||
let mut user_receipts = BTreeMap::new();
|
|
||||||
user_receipts.insert(
|
|
||||||
sender_user.clone(),
|
|
||||||
ruma::events::receipt::Receipt {
|
|
||||||
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
let mut receipts = BTreeMap::new();
|
|
||||||
receipts.insert(ReceiptType::Read, user_receipts);
|
|
||||||
|
|
||||||
let mut receipt_content = BTreeMap::new();
|
|
||||||
receipt_content.insert(body.event_id.to_owned(), receipts);
|
|
||||||
|
|
||||||
db.rooms.edus.readreceipt_update(
|
|
||||||
sender_user,
|
|
||||||
&body.room_id,
|
|
||||||
ruma::events::receipt::ReceiptEvent {
|
|
||||||
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
||||||
room_id: body.room_id.clone(),
|
|
||||||
},
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
db.flush()?;
|
|
||||||
|
|
||||||
Ok(create_receipt::v3::Response {})
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
use crate::{database::DatabaseGuard, Result, Ruma};
|
|
||||||
use ruma::{
|
|
||||||
api::client::tag::{create_tag, delete_tag, get_tags},
|
|
||||||
events::{
|
|
||||||
tag::{TagEvent, TagEventContent},
|
|
||||||
RoomAccountDataEventType,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
/// # `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.
|
|
||||||
pub async fn update_tag_route(
|
|
||||||
db: DatabaseGuard,
|
|
||||||
body: Ruma<create_tag::v3::IncomingRequest>,
|
|
||||||
) -> Result<create_tag::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
let mut tags_event = db
|
|
||||||
.account_data
|
|
||||||
.get(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::Tag,
|
|
||||||
)?
|
|
||||||
.unwrap_or_else(|| TagEvent {
|
|
||||||
content: TagEventContent {
|
|
||||||
tags: BTreeMap::new(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
tags_event
|
|
||||||
.content
|
|
||||||
.tags
|
|
||||||
.insert(body.tag.clone().into(), body.tag_info.clone());
|
|
||||||
|
|
||||||
db.account_data.update(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::Tag,
|
|
||||||
&tags_event,
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
db.flush()?;
|
|
||||||
|
|
||||||
Ok(create_tag::v3::Response {})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # `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.
|
|
||||||
pub async fn delete_tag_route(
|
|
||||||
db: DatabaseGuard,
|
|
||||||
body: Ruma<delete_tag::v3::IncomingRequest>,
|
|
||||||
) -> Result<delete_tag::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
let mut tags_event = db
|
|
||||||
.account_data
|
|
||||||
.get(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::Tag,
|
|
||||||
)?
|
|
||||||
.unwrap_or_else(|| TagEvent {
|
|
||||||
content: TagEventContent {
|
|
||||||
tags: BTreeMap::new(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
tags_event.content.tags.remove(&body.tag.clone().into());
|
|
||||||
|
|
||||||
db.account_data.update(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::Tag,
|
|
||||||
&tags_event,
|
|
||||||
&db.globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
db.flush()?;
|
|
||||||
|
|
||||||
Ok(delete_tag::v3::Response {})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags`
|
|
||||||
///
|
|
||||||
/// Returns tags on the room.
|
|
||||||
///
|
|
||||||
/// - Gets the tag event of the room account data.
|
|
||||||
pub async fn get_tags_route(
|
|
||||||
db: DatabaseGuard,
|
|
||||||
body: Ruma<get_tags::v3::IncomingRequest>,
|
|
||||||
) -> Result<get_tags::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
Ok(get_tags::v3::Response {
|
|
||||||
tags: db
|
|
||||||
.account_data
|
|
||||||
.get(
|
|
||||||
Some(&body.room_id),
|
|
||||||
sender_user,
|
|
||||||
RoomAccountDataEventType::Tag,
|
|
||||||
)?
|
|
||||||
.unwrap_or_else(|| TagEvent {
|
|
||||||
content: TagEventContent {
|
|
||||||
tags: BTreeMap::new(),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.content
|
|
||||||
.tags,
|
|
||||||
})
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,233 @@
|
|||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use futures_util::{stream::FuturesUnordered, StreamExt};
|
||||||
|
use ruma::{
|
||||||
|
api::federation::discovery::{ServerSigningKeys, VerifyKey},
|
||||||
|
signatures::Ed25519KeyPair,
|
||||||
|
DeviceId, MilliSecondsSinceUnixEpoch, OwnedServerSigningKeyId, ServerName, UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
pub const COUNTER: &[u8] = b"c";
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl service::globals::Data for KeyValueDatabase {
|
||||||
|
fn next_count(&self) -> Result<u64> {
|
||||||
|
utils::u64_from_bytes(&self.global.increment(COUNTER)?)
|
||||||
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_count(&self) -> Result<u64> {
|
||||||
|
self.global.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn watch(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
|
||||||
|
let userid_bytes = user_id.as_bytes().to_vec();
|
||||||
|
let mut userid_prefix = userid_bytes.clone();
|
||||||
|
userid_prefix.push(0xff);
|
||||||
|
|
||||||
|
let mut userdeviceid_prefix = userid_prefix.clone();
|
||||||
|
userdeviceid_prefix.extend_from_slice(device_id.as_bytes());
|
||||||
|
userdeviceid_prefix.push(0xff);
|
||||||
|
|
||||||
|
let mut futures = FuturesUnordered::new();
|
||||||
|
|
||||||
|
// Return when *any* user changed his key
|
||||||
|
// TODO: only send for user they share a room with
|
||||||
|
futures.push(self.todeviceid_events.watch_prefix(&userdeviceid_prefix));
|
||||||
|
|
||||||
|
futures.push(self.userroomid_joined.watch_prefix(&userid_prefix));
|
||||||
|
futures.push(self.userroomid_invitestate.watch_prefix(&userid_prefix));
|
||||||
|
futures.push(self.userroomid_leftstate.watch_prefix(&userid_prefix));
|
||||||
|
futures.push(
|
||||||
|
self.userroomid_notificationcount
|
||||||
|
.watch_prefix(&userid_prefix),
|
||||||
|
);
|
||||||
|
futures.push(self.userroomid_highlightcount.watch_prefix(&userid_prefix));
|
||||||
|
|
||||||
|
// Events for rooms we are in
|
||||||
|
for room_id in services()
|
||||||
|
.rooms
|
||||||
|
.state_cache
|
||||||
|
.rooms_joined(user_id)
|
||||||
|
.filter_map(|r| r.ok())
|
||||||
|
{
|
||||||
|
let short_roomid = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(&room_id)
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
|
||||||
|
let roomid_bytes = room_id.as_bytes().to_vec();
|
||||||
|
let mut roomid_prefix = roomid_bytes.clone();
|
||||||
|
roomid_prefix.push(0xff);
|
||||||
|
|
||||||
|
// PDUs
|
||||||
|
futures.push(self.pduid_pdu.watch_prefix(&short_roomid));
|
||||||
|
|
||||||
|
// EDUs
|
||||||
|
futures.push(self.roomid_lasttypingupdate.watch_prefix(&roomid_bytes));
|
||||||
|
|
||||||
|
futures.push(self.readreceiptid_readreceipt.watch_prefix(&roomid_prefix));
|
||||||
|
|
||||||
|
// Key changes
|
||||||
|
futures.push(self.keychangeid_userid.watch_prefix(&roomid_prefix));
|
||||||
|
|
||||||
|
// Room account data
|
||||||
|
let mut roomuser_prefix = roomid_prefix.clone();
|
||||||
|
roomuser_prefix.extend_from_slice(&userid_prefix);
|
||||||
|
|
||||||
|
futures.push(
|
||||||
|
self.roomusertype_roomuserdataid
|
||||||
|
.watch_prefix(&roomuser_prefix),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut globaluserdata_prefix = vec![0xff];
|
||||||
|
globaluserdata_prefix.extend_from_slice(&userid_prefix);
|
||||||
|
|
||||||
|
futures.push(
|
||||||
|
self.roomusertype_roomuserdataid
|
||||||
|
.watch_prefix(&globaluserdata_prefix),
|
||||||
|
);
|
||||||
|
|
||||||
|
// More key changes (used when user is not joined to any rooms)
|
||||||
|
futures.push(self.keychangeid_userid.watch_prefix(&userid_prefix));
|
||||||
|
|
||||||
|
// One time keys
|
||||||
|
futures.push(self.userid_lastonetimekeyupdate.watch_prefix(&userid_bytes));
|
||||||
|
|
||||||
|
futures.push(Box::pin(services().globals.rotate.watch()));
|
||||||
|
|
||||||
|
// Wait until one of them finds something
|
||||||
|
futures.next().await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cleanup(&self) -> Result<()> {
|
||||||
|
self._db.cleanup()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn memory_usage(&self) -> Result<String> {
|
||||||
|
self._db.memory_usage()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_keypair(&self) -> Result<Ed25519KeyPair> {
|
||||||
|
let keypair_bytes = self.global.get(b"keypair")?.map_or_else(
|
||||||
|
|| {
|
||||||
|
let keypair = utils::generate_keypair();
|
||||||
|
self.global.insert(b"keypair", &keypair)?;
|
||||||
|
Ok::<_, Error>(keypair)
|
||||||
|
},
|
||||||
|
|s| Ok(s.to_vec()),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut parts = keypair_bytes.splitn(2, |&b| b == 0xff);
|
||||||
|
|
||||||
|
utils::string_from_bytes(
|
||||||
|
// 1. version
|
||||||
|
parts
|
||||||
|
.next()
|
||||||
|
.expect("splitn always returns at least one element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid version bytes in keypair."))
|
||||||
|
.and_then(|version| {
|
||||||
|
// 2. key
|
||||||
|
parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid keypair format in database."))
|
||||||
|
.map(|key| (version, key))
|
||||||
|
})
|
||||||
|
.and_then(|(version, key)| {
|
||||||
|
Ed25519KeyPair::from_der(key, version)
|
||||||
|
.map_err(|_| Error::bad_database("Private or public keys are invalid."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn remove_keypair(&self) -> Result<()> {
|
||||||
|
self.global.remove(b"keypair")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_signing_key(
|
||||||
|
&self,
|
||||||
|
origin: &ServerName,
|
||||||
|
new_keys: ServerSigningKeys,
|
||||||
|
) -> Result<BTreeMap<OwnedServerSigningKeyId, VerifyKey>> {
|
||||||
|
// Not atomic, but this is not critical
|
||||||
|
let signingkeys = self.server_signingkeys.get(origin.as_bytes())?;
|
||||||
|
|
||||||
|
let mut keys = signingkeys
|
||||||
|
.and_then(|keys| serde_json::from_slice(&keys).ok())
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
// Just insert "now", it doesn't matter
|
||||||
|
ServerSigningKeys::new(origin.to_owned(), MilliSecondsSinceUnixEpoch::now())
|
||||||
|
});
|
||||||
|
|
||||||
|
let ServerSigningKeys {
|
||||||
|
verify_keys,
|
||||||
|
old_verify_keys,
|
||||||
|
..
|
||||||
|
} = new_keys;
|
||||||
|
|
||||||
|
keys.verify_keys.extend(verify_keys.into_iter());
|
||||||
|
keys.old_verify_keys.extend(old_verify_keys.into_iter());
|
||||||
|
|
||||||
|
self.server_signingkeys.insert(
|
||||||
|
origin.as_bytes(),
|
||||||
|
&serde_json::to_vec(&keys).expect("serversigningkeys can be serialized"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
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.
|
||||||
|
fn signing_keys_for(
|
||||||
|
&self,
|
||||||
|
origin: &ServerName,
|
||||||
|
) -> Result<BTreeMap<OwnedServerSigningKeyId, VerifyKey>> {
|
||||||
|
let signingkeys = self
|
||||||
|
.server_signingkeys
|
||||||
|
.get(origin.as_bytes())?
|
||||||
|
.and_then(|bytes| serde_json::from_slice(&bytes).ok())
|
||||||
|
.map(|keys: ServerSigningKeys| {
|
||||||
|
let mut tree = keys.verify_keys;
|
||||||
|
tree.extend(
|
||||||
|
keys.old_verify_keys
|
||||||
|
.into_iter()
|
||||||
|
.map(|old| (old.0, VerifyKey::new(old.1.key))),
|
||||||
|
);
|
||||||
|
tree
|
||||||
|
})
|
||||||
|
.unwrap_or_else(BTreeMap::new);
|
||||||
|
|
||||||
|
Ok(signingkeys)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn database_version(&self) -> Result<u64> {
|
||||||
|
self.global.get(b"version")?.map_or(Ok(0), |version| {
|
||||||
|
utils::u64_from_bytes(&version)
|
||||||
|
.map_err(|_| Error::bad_database("Database version id is invalid."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bump_database_version(&self, new_version: u64) -> Result<()> {
|
||||||
|
self.global.insert(b"version", &new_version.to_be_bytes())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
use ruma::api::client::error::ErrorKind;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::media::Data for KeyValueDatabase {
|
||||||
|
fn create_file_metadata(
|
||||||
|
&self,
|
||||||
|
mxc: String,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
content_disposition: Option<&str>,
|
||||||
|
content_type: Option<&str>,
|
||||||
|
) -> Result<Vec<u8>> {
|
||||||
|
let mut key = mxc.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(&width.to_be_bytes());
|
||||||
|
key.extend_from_slice(&height.to_be_bytes());
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(
|
||||||
|
content_disposition
|
||||||
|
.as_ref()
|
||||||
|
.map(|f| f.as_bytes())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(
|
||||||
|
content_type
|
||||||
|
.as_ref()
|
||||||
|
.map(|c| c.as_bytes())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.mediaid_file.insert(&key, &[])?;
|
||||||
|
|
||||||
|
Ok(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_file_metadata(
|
||||||
|
&self,
|
||||||
|
mxc: String,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
) -> Result<(Option<String>, Option<String>, Vec<u8>)> {
|
||||||
|
let mut prefix = mxc.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
prefix.extend_from_slice(&width.to_be_bytes());
|
||||||
|
prefix.extend_from_slice(&height.to_be_bytes());
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let (key, _) = self
|
||||||
|
.mediaid_file
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.next()
|
||||||
|
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?;
|
||||||
|
|
||||||
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
||||||
|
|
||||||
|
let content_type = parts
|
||||||
|
.next()
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::string_from_bytes(bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Content type in mediaid_file is invalid unicode.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
let content_disposition_bytes = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
||||||
|
|
||||||
|
let content_disposition = if content_disposition_bytes.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(
|
||||||
|
utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Content Disposition in mediaid_file is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
Ok((content_disposition, content_type, key))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
mod account_data;
|
||||||
|
//mod admin;
|
||||||
|
mod appservice;
|
||||||
|
mod globals;
|
||||||
|
mod key_backups;
|
||||||
|
mod media;
|
||||||
|
//mod pdu;
|
||||||
|
mod pusher;
|
||||||
|
mod rooms;
|
||||||
|
mod sending;
|
||||||
|
mod transaction_ids;
|
||||||
|
mod uiaa;
|
||||||
|
mod users;
|
@ -0,0 +1,79 @@
|
|||||||
|
use ruma::{
|
||||||
|
api::client::push::{set_pusher, Pusher},
|
||||||
|
UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::pusher::Data for KeyValueDatabase {
|
||||||
|
fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::PusherAction) -> Result<()> {
|
||||||
|
match &pusher {
|
||||||
|
set_pusher::v3::PusherAction::Post(data) => {
|
||||||
|
let mut key = sender.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(data.pusher.ids.pushkey.as_bytes());
|
||||||
|
self.senderkey_pusher.insert(
|
||||||
|
&key,
|
||||||
|
&serde_json::to_vec(&pusher).expect("Pusher is valid JSON value"),
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
set_pusher::v3::PusherAction::Delete(ids) => {
|
||||||
|
let mut key = sender.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(ids.pushkey.as_bytes());
|
||||||
|
self.senderkey_pusher
|
||||||
|
.remove(&key)
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_pusher(&self, sender: &UserId, pushkey: &str) -> Result<Option<Pusher>> {
|
||||||
|
let mut senderkey = sender.as_bytes().to_vec();
|
||||||
|
senderkey.push(0xff);
|
||||||
|
senderkey.extend_from_slice(pushkey.as_bytes());
|
||||||
|
|
||||||
|
self.senderkey_pusher
|
||||||
|
.get(&senderkey)?
|
||||||
|
.map(|push| {
|
||||||
|
serde_json::from_slice(&push)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_pushers(&self, sender: &UserId) -> Result<Vec<Pusher>> {
|
||||||
|
let mut prefix = sender.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
self.senderkey_pusher
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(_, push)| {
|
||||||
|
serde_json::from_slice(&push)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_pushkeys<'a>(
|
||||||
|
&'a self,
|
||||||
|
sender: &UserId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<String>> + 'a> {
|
||||||
|
let mut prefix = sender.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(self.senderkey_pusher.scan_prefix(prefix).map(|(k, _)| {
|
||||||
|
let mut parts = k.splitn(2, |&b| b == 0xff);
|
||||||
|
let _senderkey = parts.next();
|
||||||
|
let push_key = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid senderkey_pusher in db"))?;
|
||||||
|
let push_key_string = utils::string_from_bytes(push_key)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid pusher bytes in senderkey_pusher"))?;
|
||||||
|
|
||||||
|
Ok(push_key_string)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
use ruma::{api::client::error::ErrorKind, OwnedRoomAliasId, OwnedRoomId, RoomAliasId, RoomId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::alias::Data for KeyValueDatabase {
|
||||||
|
fn set_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> Result<()> {
|
||||||
|
self.alias_roomid
|
||||||
|
.insert(alias.alias().as_bytes(), room_id.as_bytes())?;
|
||||||
|
let mut aliasid = room_id.as_bytes().to_vec();
|
||||||
|
aliasid.push(0xff);
|
||||||
|
aliasid.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
|
||||||
|
self.aliasid_alias.insert(&aliasid, alias.as_bytes())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_alias(&self, alias: &RoomAliasId) -> Result<()> {
|
||||||
|
if let Some(room_id) = self.alias_roomid.get(alias.alias().as_bytes())? {
|
||||||
|
let mut prefix = room_id.to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
for (key, _) in self.aliasid_alias.scan_prefix(prefix) {
|
||||||
|
self.aliasid_alias.remove(&key)?;
|
||||||
|
}
|
||||||
|
self.alias_roomid.remove(alias.alias().as_bytes())?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::BadRequest(
|
||||||
|
ErrorKind::NotFound,
|
||||||
|
"Alias does not exist.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_local_alias(&self, alias: &RoomAliasId) -> Result<Option<OwnedRoomId>> {
|
||||||
|
self.alias_roomid
|
||||||
|
.get(alias.alias().as_bytes())?
|
||||||
|
.map(|bytes| {
|
||||||
|
RoomId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in alias_roomid is invalid unicode.")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn local_aliases_for_room<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedRoomAliasId>> + 'a> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(self.aliasid_alias.scan_prefix(prefix).map(|(_, bytes)| {
|
||||||
|
utils::string_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid alias bytes in aliasid_alias."))?
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| Error::bad_database("Invalid alias in aliasid_alias."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
use std::{collections::HashSet, mem::size_of, sync::Arc};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, utils, Result};
|
||||||
|
|
||||||
|
impl service::rooms::auth_chain::Data for KeyValueDatabase {
|
||||||
|
fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Option<Arc<HashSet<u64>>>> {
|
||||||
|
// Check RAM cache
|
||||||
|
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key) {
|
||||||
|
return Ok(Some(Arc::clone(result)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only save auth chains for single events in the db
|
||||||
|
if key.len() == 1 {
|
||||||
|
// Check DB cache
|
||||||
|
let chain = self
|
||||||
|
.shorteventid_authchain
|
||||||
|
.get(&key[0].to_be_bytes())?
|
||||||
|
.map(|chain| {
|
||||||
|
chain
|
||||||
|
.chunks_exact(size_of::<u64>())
|
||||||
|
.map(|chunk| utils::u64_from_bytes(chunk).expect("byte length is correct"))
|
||||||
|
.collect()
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(chain) = chain {
|
||||||
|
let chain = Arc::new(chain);
|
||||||
|
|
||||||
|
// Cache in RAM
|
||||||
|
self.auth_chain_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(vec![key[0]], Arc::clone(&chain));
|
||||||
|
|
||||||
|
return Ok(Some(chain));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()> {
|
||||||
|
// Only persist single events in db
|
||||||
|
if key.len() == 1 {
|
||||||
|
self.shorteventid_authchain.insert(
|
||||||
|
&key[0].to_be_bytes(),
|
||||||
|
&auth_chain
|
||||||
|
.iter()
|
||||||
|
.flat_map(|s| s.to_be_bytes().to_vec())
|
||||||
|
.collect::<Vec<u8>>(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache in RAM
|
||||||
|
self.auth_chain_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(key, auth_chain);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
use ruma::{OwnedRoomId, RoomId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::directory::Data for KeyValueDatabase {
|
||||||
|
fn set_public(&self, room_id: &RoomId) -> Result<()> {
|
||||||
|
self.publicroomids.insert(room_id.as_bytes(), &[])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_not_public(&self, room_id: &RoomId) -> Result<()> {
|
||||||
|
self.publicroomids.remove(room_id.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_public_room(&self, room_id: &RoomId) -> Result<bool> {
|
||||||
|
Ok(self.publicroomids.get(room_id.as_bytes())?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn public_rooms<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
|
||||||
|
Box::new(self.publicroomids.iter().map(|(bytes, _)| {
|
||||||
|
RoomId::parse(
|
||||||
|
utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in publicroomids is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Room ID in publicroomids is invalid."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
mod presence;
|
||||||
|
mod read_receipt;
|
||||||
|
mod typing;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service};
|
||||||
|
|
||||||
|
impl service::rooms::edus::Data for KeyValueDatabase {}
|
@ -0,0 +1,152 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use ruma::{
|
||||||
|
events::presence::PresenceEvent, presence::PresenceState, OwnedUserId, RoomId, UInt, UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::edus::presence::Data for KeyValueDatabase {
|
||||||
|
fn update_presence(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
presence: PresenceEvent,
|
||||||
|
) -> Result<()> {
|
||||||
|
// TODO: Remove old entry? Or maybe just wipe completely from time to time?
|
||||||
|
|
||||||
|
let count = services().globals.next_count()?.to_be_bytes();
|
||||||
|
|
||||||
|
let mut presence_id = room_id.as_bytes().to_vec();
|
||||||
|
presence_id.push(0xff);
|
||||||
|
presence_id.extend_from_slice(&count);
|
||||||
|
presence_id.push(0xff);
|
||||||
|
presence_id.extend_from_slice(presence.sender.as_bytes());
|
||||||
|
|
||||||
|
self.presenceid_presence.insert(
|
||||||
|
&presence_id,
|
||||||
|
&serde_json::to_vec(&presence).expect("PresenceEvent can be serialized"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.userid_lastpresenceupdate.insert(
|
||||||
|
user_id.as_bytes(),
|
||||||
|
&utils::millis_since_unix_epoch().to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ping_presence(&self, user_id: &UserId) -> Result<()> {
|
||||||
|
self.userid_lastpresenceupdate.insert(
|
||||||
|
user_id.as_bytes(),
|
||||||
|
&utils::millis_since_unix_epoch().to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_presence_update(&self, user_id: &UserId) -> Result<Option<u64>> {
|
||||||
|
self.userid_lastpresenceupdate
|
||||||
|
.get(user_id.as_bytes())?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid timestamp in userid_lastpresenceupdate.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_presence_event(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
user_id: &UserId,
|
||||||
|
count: u64,
|
||||||
|
) -> Result<Option<PresenceEvent>> {
|
||||||
|
let mut presence_id = room_id.as_bytes().to_vec();
|
||||||
|
presence_id.push(0xff);
|
||||||
|
presence_id.extend_from_slice(&count.to_be_bytes());
|
||||||
|
presence_id.push(0xff);
|
||||||
|
presence_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.presenceid_presence
|
||||||
|
.get(&presence_id)?
|
||||||
|
.map(|value| parse_presence_event(&value))
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn presence_since(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
since: u64,
|
||||||
|
) -> Result<HashMap<OwnedUserId, PresenceEvent>> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let mut first_possible_edu = prefix.clone();
|
||||||
|
first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
|
||||||
|
let mut hashmap = HashMap::new();
|
||||||
|
|
||||||
|
for (key, value) in self
|
||||||
|
.presenceid_presence
|
||||||
|
.iter_from(&first_possible_edu, false)
|
||||||
|
.take_while(|(key, _)| key.starts_with(&prefix))
|
||||||
|
{
|
||||||
|
let user_id = UserId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid UserId bytes in presenceid_presence."))?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid UserId in presenceid_presence."))?;
|
||||||
|
|
||||||
|
let presence = parse_presence_event(&value)?;
|
||||||
|
|
||||||
|
hashmap.insert(user_id, presence);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(hashmap)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn presence_maintain(&self, db: Arc<TokioRwLock<Database>>) {
|
||||||
|
// TODO @M0dEx: move this to a timed tasks module
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
select! {
|
||||||
|
Some(user_id) = self.presence_timers.next() {
|
||||||
|
// TODO @M0dEx: would it be better to acquire the lock outside the loop?
|
||||||
|
let guard = db.read().await;
|
||||||
|
|
||||||
|
// TODO @M0dEx: add self.presence_timers
|
||||||
|
// TODO @M0dEx: maintain presence
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_presence_event(bytes: &[u8]) -> Result<PresenceEvent> {
|
||||||
|
let mut presence: PresenceEvent = serde_json::from_slice(bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid presence event in db."))?;
|
||||||
|
|
||||||
|
let current_timestamp: UInt = utils::millis_since_unix_epoch()
|
||||||
|
.try_into()
|
||||||
|
.expect("time is valid");
|
||||||
|
|
||||||
|
if presence.content.presence == PresenceState::Online {
|
||||||
|
// Don't set last_active_ago when the user is online
|
||||||
|
presence.content.last_active_ago = None;
|
||||||
|
} else {
|
||||||
|
// Convert from timestamp to duration
|
||||||
|
presence.content.last_active_ago = presence
|
||||||
|
.content
|
||||||
|
.last_active_ago
|
||||||
|
.map(|timestamp| current_timestamp - timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(presence)
|
||||||
|
}
|
@ -0,0 +1,150 @@
|
|||||||
|
use std::mem;
|
||||||
|
|
||||||
|
use ruma::{
|
||||||
|
events::receipt::ReceiptEvent, serde::Raw, CanonicalJsonObject, OwnedUserId, RoomId, UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
|
||||||
|
fn readreceipt_update(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
event: ReceiptEvent,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut prefix = room_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());
|
||||||
|
|
||||||
|
// Remove old entry
|
||||||
|
if let Some((old, _)) = self
|
||||||
|
.readreceiptid_readreceipt
|
||||||
|
.iter_from(&last_possible_key, true)
|
||||||
|
.take_while(|(key, _)| key.starts_with(&prefix))
|
||||||
|
.find(|(key, _)| {
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element")
|
||||||
|
== user_id.as_bytes()
|
||||||
|
})
|
||||||
|
{
|
||||||
|
// This is the old room_latest
|
||||||
|
self.readreceiptid_readreceipt.remove(&old)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut room_latest_id = prefix;
|
||||||
|
room_latest_id.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
|
||||||
|
room_latest_id.push(0xff);
|
||||||
|
room_latest_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.readreceiptid_readreceipt.insert(
|
||||||
|
&room_latest_id,
|
||||||
|
&serde_json::to_vec(&event).expect("EduEvent::to_string always works"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readreceipts_since<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
since: u64,
|
||||||
|
) -> Box<
|
||||||
|
dyn Iterator<
|
||||||
|
Item = Result<(
|
||||||
|
OwnedUserId,
|
||||||
|
u64,
|
||||||
|
Raw<ruma::events::AnySyncEphemeralRoomEvent>,
|
||||||
|
)>,
|
||||||
|
> + 'a,
|
||||||
|
> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
let prefix2 = prefix.clone();
|
||||||
|
|
||||||
|
let mut first_possible_edu = prefix.clone();
|
||||||
|
first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
|
||||||
|
|
||||||
|
Box::new(
|
||||||
|
self.readreceiptid_readreceipt
|
||||||
|
.iter_from(&first_possible_edu, false)
|
||||||
|
.take_while(move |(k, _)| k.starts_with(&prefix2))
|
||||||
|
.map(move |(k, v)| {
|
||||||
|
let count = utils::u64_from_bytes(
|
||||||
|
&k[prefix.len()..prefix.len() + mem::size_of::<u64>()],
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?;
|
||||||
|
let user_id = UserId::parse(
|
||||||
|
utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..])
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Invalid readreceiptid userid bytes in db.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid readreceiptid userid in db."))?;
|
||||||
|
|
||||||
|
let mut json =
|
||||||
|
serde_json::from_slice::<CanonicalJsonObject>(&v).map_err(|_| {
|
||||||
|
Error::bad_database(
|
||||||
|
"Read receipt in roomlatestid_roomlatest is invalid json.",
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
json.remove("room_id");
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
user_id,
|
||||||
|
count,
|
||||||
|
Raw::from_json(
|
||||||
|
serde_json::value::to_raw_value(&json)
|
||||||
|
.expect("json is valid raw value"),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) -> Result<()> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomuserid_privateread
|
||||||
|
.insert(&key, &count.to_be_bytes())?;
|
||||||
|
|
||||||
|
self.roomuserid_lastprivatereadupdate
|
||||||
|
.insert(&key, &services().globals.next_count()?.to_be_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn private_read_get(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomuserid_privateread
|
||||||
|
.get(&key)?
|
||||||
|
.map_or(Ok(None), |v| {
|
||||||
|
Ok(Some(utils::u64_from_bytes(&v).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid private read marker bytes")
|
||||||
|
})?))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_privateread_update(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self
|
||||||
|
.roomuserid_lastprivatereadupdate
|
||||||
|
.get(&key)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(0))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,127 @@
|
|||||||
|
use std::{collections::HashSet, mem};
|
||||||
|
|
||||||
|
use ruma::{OwnedUserId, RoomId, UserId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::edus::typing::Data for KeyValueDatabase {
|
||||||
|
fn typing_add(&self, user_id: &UserId, room_id: &RoomId, timeout: u64) -> Result<()> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let count = services().globals.next_count()?.to_be_bytes();
|
||||||
|
|
||||||
|
let mut room_typing_id = prefix;
|
||||||
|
room_typing_id.extend_from_slice(&timeout.to_be_bytes());
|
||||||
|
room_typing_id.push(0xff);
|
||||||
|
room_typing_id.extend_from_slice(&count);
|
||||||
|
|
||||||
|
self.typingid_userid
|
||||||
|
.insert(&room_typing_id, user_id.as_bytes())?;
|
||||||
|
|
||||||
|
self.roomid_lasttypingupdate
|
||||||
|
.insert(room_id.as_bytes(), &count)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn typing_remove(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let user_id = user_id.to_string();
|
||||||
|
|
||||||
|
let mut found_outdated = false;
|
||||||
|
|
||||||
|
// Maybe there are multiple ones from calling roomtyping_add multiple times
|
||||||
|
for outdated_edu in self
|
||||||
|
.typingid_userid
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.filter(|(_, v)| &**v == user_id.as_bytes())
|
||||||
|
{
|
||||||
|
self.typingid_userid.remove(&outdated_edu.0)?;
|
||||||
|
found_outdated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if found_outdated {
|
||||||
|
self.roomid_lasttypingupdate.insert(
|
||||||
|
room_id.as_bytes(),
|
||||||
|
&services().globals.next_count()?.to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn typings_maintain(&self, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let current_timestamp = utils::millis_since_unix_epoch();
|
||||||
|
|
||||||
|
let mut found_outdated = false;
|
||||||
|
|
||||||
|
// Find all outdated edus before inserting a new one
|
||||||
|
for outdated_edu in self
|
||||||
|
.typingid_userid
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, _)| {
|
||||||
|
Ok::<_, Error>((
|
||||||
|
key.clone(),
|
||||||
|
utils::u64_from_bytes(
|
||||||
|
&key.splitn(2, |&b| b == 0xff).nth(1).ok_or_else(|| {
|
||||||
|
Error::bad_database("RoomTyping has invalid timestamp or delimiters.")
|
||||||
|
})?[0..mem::size_of::<u64>()],
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("RoomTyping has invalid timestamp bytes."))?,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.filter_map(|r| r.ok())
|
||||||
|
.take_while(|&(_, timestamp)| timestamp < current_timestamp)
|
||||||
|
{
|
||||||
|
// This is an outdated edu (time > timestamp)
|
||||||
|
self.typingid_userid.remove(&outdated_edu.0)?;
|
||||||
|
found_outdated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if found_outdated {
|
||||||
|
self.roomid_lasttypingupdate.insert(
|
||||||
|
room_id.as_bytes(),
|
||||||
|
&services().globals.next_count()?.to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> {
|
||||||
|
Ok(self
|
||||||
|
.roomid_lasttypingupdate
|
||||||
|
.get(room_id.as_bytes())?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn typings_all(&self, room_id: &RoomId) -> Result<HashSet<OwnedUserId>> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
let mut user_ids = HashSet::new();
|
||||||
|
|
||||||
|
for (_, user_id) in self.typingid_userid.scan_prefix(prefix) {
|
||||||
|
let user_id = UserId::parse(utils::string_from_bytes(&user_id).map_err(|_| {
|
||||||
|
Error::bad_database("User ID in typingid_userid is invalid unicode.")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))?;
|
||||||
|
|
||||||
|
user_ids.insert(user_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(user_ids)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
use ruma::{DeviceId, RoomId, UserId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, Result};
|
||||||
|
|
||||||
|
impl service::rooms::lazy_loading::Data for KeyValueDatabase {
|
||||||
|
fn lazy_load_was_sent_before(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
ll_user: &UserId,
|
||||||
|
) -> Result<bool> {
|
||||||
|
let mut key = user_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(device_id.as_bytes());
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(room_id.as_bytes());
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(ll_user.as_bytes());
|
||||||
|
Ok(self.lazyloadedids.get(&key)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lazy_load_confirm_delivery(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
confirmed_user_ids: &mut dyn Iterator<Item = &UserId>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut prefix = user_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
prefix.extend_from_slice(device_id.as_bytes());
|
||||||
|
prefix.push(0xff);
|
||||||
|
prefix.extend_from_slice(room_id.as_bytes());
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
for ll_id in confirmed_user_ids {
|
||||||
|
let mut key = prefix.clone();
|
||||||
|
key.extend_from_slice(ll_id.as_bytes());
|
||||||
|
self.lazyloadedids.insert(&key, &[])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lazy_load_reset(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut prefix = user_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
prefix.extend_from_slice(device_id.as_bytes());
|
||||||
|
prefix.push(0xff);
|
||||||
|
prefix.extend_from_slice(room_id.as_bytes());
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
for (key, _) in self.lazyloadedids.scan_prefix(prefix) {
|
||||||
|
self.lazyloadedids.remove(&key)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
use ruma::{OwnedRoomId, RoomId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::metadata::Data for KeyValueDatabase {
|
||||||
|
fn exists(&self, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let prefix = match services().rooms.short.get_shortroomid(room_id)? {
|
||||||
|
Some(b) => b.to_be_bytes().to_vec(),
|
||||||
|
None => return Ok(false),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Look for PDUs in that room.
|
||||||
|
Ok(self
|
||||||
|
.pduid_pdu
|
||||||
|
.iter_from(&prefix, false)
|
||||||
|
.next()
|
||||||
|
.filter(|(k, _)| k.starts_with(&prefix))
|
||||||
|
.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter_ids<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
|
||||||
|
Box::new(self.roomid_shortroomid.iter().map(|(bytes, _)| {
|
||||||
|
RoomId::parse(
|
||||||
|
utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in publicroomids is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Room ID in roomid_shortroomid is invalid."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_disabled(&self, room_id: &RoomId) -> Result<bool> {
|
||||||
|
Ok(self.disabledroomids.get(room_id.as_bytes())?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disable_room(&self, room_id: &RoomId, disabled: bool) -> Result<()> {
|
||||||
|
if disabled {
|
||||||
|
self.disabledroomids.insert(room_id.as_bytes(), &[])?;
|
||||||
|
} else {
|
||||||
|
self.disabledroomids.remove(room_id.as_bytes())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
mod alias;
|
||||||
|
mod auth_chain;
|
||||||
|
mod directory;
|
||||||
|
mod edus;
|
||||||
|
mod lazy_load;
|
||||||
|
mod metadata;
|
||||||
|
mod outlier;
|
||||||
|
mod pdu_metadata;
|
||||||
|
mod search;
|
||||||
|
mod short;
|
||||||
|
mod state;
|
||||||
|
mod state_accessor;
|
||||||
|
mod state_cache;
|
||||||
|
mod state_compressor;
|
||||||
|
mod timeline;
|
||||||
|
mod user;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service};
|
||||||
|
|
||||||
|
impl service::rooms::Data for KeyValueDatabase {}
|
@ -0,0 +1,28 @@
|
|||||||
|
use ruma::{CanonicalJsonObject, EventId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, Error, PduEvent, Result};
|
||||||
|
|
||||||
|
impl service::rooms::outlier::Data for KeyValueDatabase {
|
||||||
|
fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
|
||||||
|
self.eventid_outlierpdu
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map_or(Ok(None), |pdu| {
|
||||||
|
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
|
||||||
|
self.eventid_outlierpdu
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map_or(Ok(None), |pdu| {
|
||||||
|
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_pdu_outlier(&self, event_id: &EventId, pdu: &CanonicalJsonObject) -> Result<()> {
|
||||||
|
self.eventid_outlierpdu.insert(
|
||||||
|
event_id.as_bytes(),
|
||||||
|
&serde_json::to_vec(&pdu).expect("CanonicalJsonObject is valid"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use ruma::{EventId, RoomId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, Result};
|
||||||
|
|
||||||
|
impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
|
||||||
|
fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()> {
|
||||||
|
for prev in event_ids {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.extend_from_slice(prev.as_bytes());
|
||||||
|
self.referencedevents.insert(&key, &[])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_event_referenced(&self, room_id: &RoomId, event_id: &EventId) -> Result<bool> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.extend_from_slice(event_id.as_bytes());
|
||||||
|
Ok(self.referencedevents.get(&key)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_event_soft_failed(&self, event_id: &EventId) -> Result<()> {
|
||||||
|
self.softfailedeventids.insert(event_id.as_bytes(), &[])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_event_soft_failed(&self, event_id: &EventId) -> Result<bool> {
|
||||||
|
self.softfailedeventids
|
||||||
|
.get(event_id.as_bytes())
|
||||||
|
.map(|o| o.is_some())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
use std::mem::size_of;
|
||||||
|
|
||||||
|
use ruma::RoomId;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Result};
|
||||||
|
|
||||||
|
impl service::rooms::search::Data for KeyValueDatabase {
|
||||||
|
fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: &str) -> Result<()> {
|
||||||
|
let mut batch = message_body
|
||||||
|
.split_terminator(|c: char| !c.is_alphanumeric())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.filter(|word| word.len() <= 50)
|
||||||
|
.map(str::to_lowercase)
|
||||||
|
.map(|word| {
|
||||||
|
let mut key = shortroomid.to_be_bytes().to_vec();
|
||||||
|
key.extend_from_slice(word.as_bytes());
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(pdu_id);
|
||||||
|
(key, Vec::new())
|
||||||
|
});
|
||||||
|
|
||||||
|
self.tokenids.insert_batch(&mut batch)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_pdus<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
search_string: &str,
|
||||||
|
) -> Result<Option<(Box<dyn Iterator<Item = Vec<u8>> + 'a>, Vec<String>)>> {
|
||||||
|
let prefix = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
let prefix_clone = prefix.clone();
|
||||||
|
|
||||||
|
let words: Vec<_> = search_string
|
||||||
|
.split_terminator(|c: char| !c.is_alphanumeric())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(str::to_lowercase)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let iterators = words.clone().into_iter().map(move |word| {
|
||||||
|
let mut prefix2 = prefix.clone();
|
||||||
|
prefix2.extend_from_slice(word.as_bytes());
|
||||||
|
prefix2.push(0xff);
|
||||||
|
|
||||||
|
let mut last_possible_id = prefix2.clone();
|
||||||
|
last_possible_id.extend_from_slice(&u64::MAX.to_be_bytes());
|
||||||
|
|
||||||
|
self.tokenids
|
||||||
|
.iter_from(&last_possible_id, true) // Newest pdus first
|
||||||
|
.take_while(move |(k, _)| k.starts_with(&prefix2))
|
||||||
|
.map(|(key, _)| key[key.len() - size_of::<u64>()..].to_vec())
|
||||||
|
});
|
||||||
|
|
||||||
|
let common_elements = match utils::common_elements(iterators, |a, b| {
|
||||||
|
// We compare b with a because we reversed the iterator earlier
|
||||||
|
b.cmp(a)
|
||||||
|
}) {
|
||||||
|
Some(it) => it,
|
||||||
|
None => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mapped = common_elements.map(move |id| {
|
||||||
|
let mut pduid = prefix_clone.clone();
|
||||||
|
pduid.extend_from_slice(&id);
|
||||||
|
pduid
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(Some((Box::new(mapped), words)))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,218 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use ruma::{events::StateEventType, EventId, RoomId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::short::Data for KeyValueDatabase {
|
||||||
|
fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64> {
|
||||||
|
if let Some(short) = self.eventidshort_cache.lock().unwrap().get_mut(event_id) {
|
||||||
|
return Ok(*short);
|
||||||
|
}
|
||||||
|
|
||||||
|
let short = match self.eventid_shorteventid.get(event_id.as_bytes())? {
|
||||||
|
Some(shorteventid) => utils::u64_from_bytes(&shorteventid)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shorteventid in db."))?,
|
||||||
|
None => {
|
||||||
|
let shorteventid = services().globals.next_count()?;
|
||||||
|
self.eventid_shorteventid
|
||||||
|
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
|
||||||
|
self.shorteventid_eventid
|
||||||
|
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
|
||||||
|
shorteventid
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.eventidshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(event_id.to_owned(), short);
|
||||||
|
|
||||||
|
Ok(short)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_shortstatekey(
|
||||||
|
&self,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<Option<u64>> {
|
||||||
|
if let Some(short) = self
|
||||||
|
.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||||
|
{
|
||||||
|
return Ok(Some(*short));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut statekey = event_type.to_string().as_bytes().to_vec();
|
||||||
|
statekey.push(0xff);
|
||||||
|
statekey.extend_from_slice(state_key.as_bytes());
|
||||||
|
|
||||||
|
let short = self
|
||||||
|
.statekey_shortstatekey
|
||||||
|
.get(&statekey)?
|
||||||
|
.map(|shortstatekey| {
|
||||||
|
utils::u64_from_bytes(&shortstatekey)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))
|
||||||
|
})
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
if let Some(s) = short {
|
||||||
|
self.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert((event_type.clone(), state_key.to_owned()), s);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(short)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_or_create_shortstatekey(
|
||||||
|
&self,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<u64> {
|
||||||
|
if let Some(short) = self
|
||||||
|
.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||||
|
{
|
||||||
|
return Ok(*short);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut statekey = event_type.to_string().as_bytes().to_vec();
|
||||||
|
statekey.push(0xff);
|
||||||
|
statekey.extend_from_slice(state_key.as_bytes());
|
||||||
|
|
||||||
|
let short = match self.statekey_shortstatekey.get(&statekey)? {
|
||||||
|
Some(shortstatekey) => utils::u64_from_bytes(&shortstatekey)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))?,
|
||||||
|
None => {
|
||||||
|
let shortstatekey = services().globals.next_count()?;
|
||||||
|
self.statekey_shortstatekey
|
||||||
|
.insert(&statekey, &shortstatekey.to_be_bytes())?;
|
||||||
|
self.shortstatekey_statekey
|
||||||
|
.insert(&shortstatekey.to_be_bytes(), &statekey)?;
|
||||||
|
shortstatekey
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||||
|
|
||||||
|
Ok(short)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<EventId>> {
|
||||||
|
if let Some(id) = self
|
||||||
|
.shorteventid_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&shorteventid)
|
||||||
|
{
|
||||||
|
return Ok(Arc::clone(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = self
|
||||||
|
.shorteventid_eventid
|
||||||
|
.get(&shorteventid.to_be_bytes())?
|
||||||
|
.ok_or_else(|| Error::bad_database("Shorteventid does not exist"))?;
|
||||||
|
|
||||||
|
let event_id = EventId::parse_arc(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?;
|
||||||
|
|
||||||
|
self.shorteventid_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(shorteventid, Arc::clone(&event_id));
|
||||||
|
|
||||||
|
Ok(event_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)> {
|
||||||
|
if let Some(id) = self
|
||||||
|
.shortstatekey_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&shortstatekey)
|
||||||
|
{
|
||||||
|
return Ok(id.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = self
|
||||||
|
.shortstatekey_statekey
|
||||||
|
.get(&shortstatekey.to_be_bytes())?
|
||||||
|
.ok_or_else(|| Error::bad_database("Shortstatekey does not exist"))?;
|
||||||
|
|
||||||
|
let mut parts = bytes.splitn(2, |&b| b == 0xff);
|
||||||
|
let eventtype_bytes = parts.next().expect("split always returns one entry");
|
||||||
|
let statekey_bytes = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid statekey in shortstatekey_statekey."))?;
|
||||||
|
|
||||||
|
let event_type =
|
||||||
|
StateEventType::try_from(utils::string_from_bytes(eventtype_bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Event type in shortstatekey_statekey is invalid unicode.")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("Event type in shortstatekey_statekey is invalid."))?;
|
||||||
|
|
||||||
|
let state_key = utils::string_from_bytes(statekey_bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Statekey in shortstatekey_statekey is invalid unicode.")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let result = (event_type, state_key);
|
||||||
|
|
||||||
|
self.shortstatekey_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(shortstatekey, result.clone());
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns (shortstatehash, already_existed)
|
||||||
|
fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> {
|
||||||
|
Ok(match self.statehash_shortstatehash.get(state_hash)? {
|
||||||
|
Some(shortstatehash) => (
|
||||||
|
utils::u64_from_bytes(&shortstatehash)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?,
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
None => {
|
||||||
|
let shortstatehash = services().globals.next_count()?;
|
||||||
|
self.statehash_shortstatehash
|
||||||
|
.insert(state_hash, &shortstatehash.to_be_bytes())?;
|
||||||
|
(shortstatehash, false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
||||||
|
self.roomid_shortroomid
|
||||||
|
.get(room_id.as_bytes())?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
|
||||||
|
Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? {
|
||||||
|
Some(short) => utils::u64_from_bytes(&short)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))?,
|
||||||
|
None => {
|
||||||
|
let short = services().globals.next_count()?;
|
||||||
|
self.roomid_shortroomid
|
||||||
|
.insert(room_id.as_bytes(), &short.to_be_bytes())?;
|
||||||
|
short
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
use ruma::{EventId, OwnedEventId, RoomId};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::MutexGuard;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::state::Data for KeyValueDatabase {
|
||||||
|
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
||||||
|
self.roomid_shortstatehash
|
||||||
|
.get(room_id.as_bytes())?
|
||||||
|
.map_or(Ok(None), |bytes| {
|
||||||
|
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid shortstatehash in roomid_shortstatehash")
|
||||||
|
})?))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_room_state(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
new_shortstatehash: u64,
|
||||||
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
||||||
|
) -> Result<()> {
|
||||||
|
self.roomid_shortstatehash
|
||||||
|
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) -> Result<()> {
|
||||||
|
self.shorteventid_shortstatehash
|
||||||
|
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
self.roomid_pduleaves
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(_, bytes)| {
|
||||||
|
EventId::parse_arc(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("EventID in roomid_pduleaves is invalid unicode.")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_forward_extremities<'a>(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
event_ids: Vec<OwnedEventId>,
|
||||||
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
for (key, _) in self.roomid_pduleaves.scan_prefix(prefix.clone()) {
|
||||||
|
self.roomid_pduleaves.remove(&key)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for event_id in event_ids {
|
||||||
|
let mut key = prefix.to_owned();
|
||||||
|
key.extend_from_slice(event_id.as_bytes());
|
||||||
|
self.roomid_pduleaves.insert(&key, event_id.as_bytes())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,186 @@
|
|||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, PduEvent, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use ruma::{events::StateEventType, EventId, RoomId};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl service::rooms::state_accessor::Data for KeyValueDatabase {
|
||||||
|
async fn state_full_ids(&self, shortstatehash: u64) -> Result<HashMap<u64, Arc<EventId>>> {
|
||||||
|
let full_state = services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.load_shortstatehash_info(shortstatehash)?
|
||||||
|
.pop()
|
||||||
|
.expect("there is always one layer")
|
||||||
|
.1;
|
||||||
|
let mut result = HashMap::new();
|
||||||
|
let mut i = 0;
|
||||||
|
for compressed in full_state.into_iter() {
|
||||||
|
let parsed = services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.parse_compressed_state_event(&compressed)?;
|
||||||
|
result.insert(parsed.0, parsed.1);
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
if i % 100 == 0 {
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn state_full(
|
||||||
|
&self,
|
||||||
|
shortstatehash: u64,
|
||||||
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
||||||
|
let full_state = services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.load_shortstatehash_info(shortstatehash)?
|
||||||
|
.pop()
|
||||||
|
.expect("there is always one layer")
|
||||||
|
.1;
|
||||||
|
|
||||||
|
let mut result = HashMap::new();
|
||||||
|
let mut i = 0;
|
||||||
|
for compressed in full_state {
|
||||||
|
let (_, eventid) = services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.parse_compressed_state_event(&compressed)?;
|
||||||
|
if let Some(pdu) = services().rooms.timeline.get_pdu(&eventid)? {
|
||||||
|
result.insert(
|
||||||
|
(
|
||||||
|
pdu.kind.to_string().into(),
|
||||||
|
pdu.state_key
|
||||||
|
.as_ref()
|
||||||
|
.ok_or_else(|| Error::bad_database("State event has no state key."))?
|
||||||
|
.clone(),
|
||||||
|
),
|
||||||
|
pdu,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
if i % 100 == 0 {
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
|
fn state_get_id(
|
||||||
|
&self,
|
||||||
|
shortstatehash: u64,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<Option<Arc<EventId>>> {
|
||||||
|
let shortstatekey = match services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortstatekey(event_type, state_key)?
|
||||||
|
{
|
||||||
|
Some(s) => s,
|
||||||
|
None => return Ok(None),
|
||||||
|
};
|
||||||
|
let full_state = services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.load_shortstatehash_info(shortstatehash)?
|
||||||
|
.pop()
|
||||||
|
.expect("there is always one layer")
|
||||||
|
.1;
|
||||||
|
Ok(full_state
|
||||||
|
.into_iter()
|
||||||
|
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes()))
|
||||||
|
.and_then(|compressed| {
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.state_compressor
|
||||||
|
.parse_compressed_state_event(&compressed)
|
||||||
|
.ok()
|
||||||
|
.map(|(_, id)| id)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
|
fn state_get(
|
||||||
|
&self,
|
||||||
|
shortstatehash: u64,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<Option<Arc<PduEvent>>> {
|
||||||
|
self.state_get_id(shortstatehash, event_type, state_key)?
|
||||||
|
.map_or(Ok(None), |event_id| {
|
||||||
|
services().rooms.timeline.get_pdu(&event_id)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the state hash for this pdu.
|
||||||
|
fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
||||||
|
self.eventid_shorteventid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map_or(Ok(None), |shorteventid| {
|
||||||
|
self.shorteventid_shortstatehash
|
||||||
|
.get(&shorteventid)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database(
|
||||||
|
"Invalid shortstatehash bytes in shorteventid_shortstatehash",
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the full room state.
|
||||||
|
async fn room_state_full(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
||||||
|
if let Some(current_shortstatehash) =
|
||||||
|
services().rooms.state.get_room_shortstatehash(room_id)?
|
||||||
|
{
|
||||||
|
self.state_full(current_shortstatehash).await
|
||||||
|
} else {
|
||||||
|
Ok(HashMap::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
|
fn room_state_get_id(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<Option<Arc<EventId>>> {
|
||||||
|
if let Some(current_shortstatehash) =
|
||||||
|
services().rooms.state.get_room_shortstatehash(room_id)?
|
||||||
|
{
|
||||||
|
self.state_get_id(current_shortstatehash, event_type, state_key)
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
|
fn room_state_get(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
event_type: &StateEventType,
|
||||||
|
state_key: &str,
|
||||||
|
) -> Result<Option<Arc<PduEvent>>> {
|
||||||
|
if let Some(current_shortstatehash) =
|
||||||
|
services().rooms.state.get_room_shortstatehash(room_id)?
|
||||||
|
{
|
||||||
|
self.state_get(current_shortstatehash, event_type, state_key)
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,622 @@
|
|||||||
|
use std::{collections::HashSet, sync::Arc};
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use ruma::{
|
||||||
|
events::{AnyStrippedStateEvent, AnySyncStateEvent},
|
||||||
|
serde::Raw,
|
||||||
|
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::state_cache::Data for KeyValueDatabase {
|
||||||
|
fn mark_as_once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
self.roomuseroncejoinedids.insert(&userroom_id, &[])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_as_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut roomuser_id = room_id.as_bytes().to_vec();
|
||||||
|
roomuser_id.push(0xff);
|
||||||
|
roomuser_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_joined.insert(&userroom_id, &[])?;
|
||||||
|
self.roomuserid_joined.insert(&roomuser_id, &[])?;
|
||||||
|
self.userroomid_invitestate.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_invitecount.remove(&roomuser_id)?;
|
||||||
|
self.userroomid_leftstate.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_leftcount.remove(&roomuser_id)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_as_invited(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut roomuser_id = room_id.as_bytes().to_vec();
|
||||||
|
roomuser_id.push(0xff);
|
||||||
|
roomuser_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_invitestate.insert(
|
||||||
|
&userroom_id,
|
||||||
|
&serde_json::to_vec(&last_state.unwrap_or_default())
|
||||||
|
.expect("state to bytes always works"),
|
||||||
|
)?;
|
||||||
|
self.roomuserid_invitecount.insert(
|
||||||
|
&roomuser_id,
|
||||||
|
&services().globals.next_count()?.to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
self.userroomid_joined.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_joined.remove(&roomuser_id)?;
|
||||||
|
self.userroomid_leftstate.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_leftcount.remove(&roomuser_id)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_as_left(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut roomuser_id = room_id.as_bytes().to_vec();
|
||||||
|
roomuser_id.push(0xff);
|
||||||
|
roomuser_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_leftstate.insert(
|
||||||
|
&userroom_id,
|
||||||
|
&serde_json::to_vec(&Vec::<Raw<AnySyncStateEvent>>::new()).unwrap(),
|
||||||
|
)?; // TODO
|
||||||
|
self.roomuserid_leftcount.insert(
|
||||||
|
&roomuser_id,
|
||||||
|
&services().globals.next_count()?.to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
self.userroomid_joined.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_joined.remove(&roomuser_id)?;
|
||||||
|
self.userroomid_invitestate.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_invitecount.remove(&roomuser_id)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_joined_count(&self, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut joinedcount = 0_u64;
|
||||||
|
let mut invitedcount = 0_u64;
|
||||||
|
let mut joined_servers = HashSet::new();
|
||||||
|
let mut real_users = HashSet::new();
|
||||||
|
|
||||||
|
for joined in self.room_members(room_id).filter_map(|r| r.ok()) {
|
||||||
|
joined_servers.insert(joined.server_name().to_owned());
|
||||||
|
if joined.server_name() == services().globals.server_name()
|
||||||
|
&& !services().users.is_deactivated(&joined).unwrap_or(true)
|
||||||
|
{
|
||||||
|
real_users.insert(joined);
|
||||||
|
}
|
||||||
|
joinedcount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for _invited in self.room_members_invited(room_id).filter_map(|r| r.ok()) {
|
||||||
|
invitedcount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.roomid_joinedcount
|
||||||
|
.insert(room_id.as_bytes(), &joinedcount.to_be_bytes())?;
|
||||||
|
|
||||||
|
self.roomid_invitedcount
|
||||||
|
.insert(room_id.as_bytes(), &invitedcount.to_be_bytes())?;
|
||||||
|
|
||||||
|
self.our_real_users_cache
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(room_id.to_owned(), Arc::new(real_users));
|
||||||
|
|
||||||
|
for old_joined_server in self.room_servers(room_id).filter_map(|r| r.ok()) {
|
||||||
|
if !joined_servers.remove(&old_joined_server) {
|
||||||
|
// Server not in room anymore
|
||||||
|
let mut roomserver_id = room_id.as_bytes().to_vec();
|
||||||
|
roomserver_id.push(0xff);
|
||||||
|
roomserver_id.extend_from_slice(old_joined_server.as_bytes());
|
||||||
|
|
||||||
|
let mut serverroom_id = old_joined_server.as_bytes().to_vec();
|
||||||
|
serverroom_id.push(0xff);
|
||||||
|
serverroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomserverids.remove(&roomserver_id)?;
|
||||||
|
self.serverroomids.remove(&serverroom_id)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now only new servers are in joined_servers anymore
|
||||||
|
for server in joined_servers {
|
||||||
|
let mut roomserver_id = room_id.as_bytes().to_vec();
|
||||||
|
roomserver_id.push(0xff);
|
||||||
|
roomserver_id.extend_from_slice(server.as_bytes());
|
||||||
|
|
||||||
|
let mut serverroom_id = server.as_bytes().to_vec();
|
||||||
|
serverroom_id.push(0xff);
|
||||||
|
serverroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomserverids.insert(&roomserver_id, &[])?;
|
||||||
|
self.serverroomids.insert(&serverroom_id, &[])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.appservice_in_room_cache
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.remove(room_id);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self, room_id))]
|
||||||
|
fn get_our_real_users(&self, room_id: &RoomId) -> Result<Arc<HashSet<OwnedUserId>>> {
|
||||||
|
let maybe = self
|
||||||
|
.our_real_users_cache
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.get(room_id)
|
||||||
|
.cloned();
|
||||||
|
if let Some(users) = maybe {
|
||||||
|
Ok(users)
|
||||||
|
} else {
|
||||||
|
self.update_joined_count(room_id)?;
|
||||||
|
Ok(Arc::clone(
|
||||||
|
self.our_real_users_cache
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.get(room_id)
|
||||||
|
.unwrap(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self, room_id, appservice))]
|
||||||
|
fn appservice_in_room(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
appservice: &(String, serde_yaml::Value),
|
||||||
|
) -> Result<bool> {
|
||||||
|
let maybe = self
|
||||||
|
.appservice_in_room_cache
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.get(room_id)
|
||||||
|
.and_then(|map| map.get(&appservice.0))
|
||||||
|
.copied();
|
||||||
|
|
||||||
|
if let Some(b) = maybe {
|
||||||
|
Ok(b)
|
||||||
|
} else if let Some(namespaces) = appservice.1.get("namespaces") {
|
||||||
|
let users = namespaces
|
||||||
|
.get("users")
|
||||||
|
.and_then(|users| users.as_sequence())
|
||||||
|
.map_or_else(Vec::new, |users| {
|
||||||
|
users
|
||||||
|
.iter()
|
||||||
|
.filter_map(|users| Regex::new(users.get("regex")?.as_str()?).ok())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
});
|
||||||
|
|
||||||
|
let bridge_user_id = appservice
|
||||||
|
.1
|
||||||
|
.get("sender_localpart")
|
||||||
|
.and_then(|string| string.as_str())
|
||||||
|
.and_then(|string| {
|
||||||
|
UserId::parse_with_server_name(string, services().globals.server_name()).ok()
|
||||||
|
});
|
||||||
|
|
||||||
|
let in_room = bridge_user_id
|
||||||
|
.map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false))
|
||||||
|
|| self.room_members(room_id).any(|userid| {
|
||||||
|
userid.map_or(false, |userid| {
|
||||||
|
users.iter().any(|r| r.is_match(userid.as_str()))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
self.appservice_in_room_cache
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.entry(room_id.to_owned())
|
||||||
|
.or_default()
|
||||||
|
.insert(appservice.0.clone(), in_room);
|
||||||
|
|
||||||
|
Ok(in_room)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Makes a user forget a room.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn forget(&self, room_id: &RoomId, user_id: &UserId) -> Result<()> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
let mut roomuser_id = room_id.as_bytes().to_vec();
|
||||||
|
roomuser_id.push(0xff);
|
||||||
|
roomuser_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_leftstate.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_leftcount.remove(&roomuser_id)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator of all servers participating in this room.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_servers<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedServerName>> + 'a> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(self.roomserverids.scan_prefix(prefix).map(|(key, _)| {
|
||||||
|
ServerName::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Server name in roomserverids is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Server name in roomserverids is invalid."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn server_in_room<'a>(&'a self, server: &ServerName, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut key = server.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.serverroomids.get(&key).map(|o| o.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator of all rooms a server participates in (as far as we know).
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn server_rooms<'a>(
|
||||||
|
&'a self,
|
||||||
|
server: &ServerName,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
|
||||||
|
let mut prefix = server.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(self.serverroomids.scan_prefix(prefix).map(|(key, _)| {
|
||||||
|
RoomId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("RoomId in serverroomids is invalid unicode."))?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("RoomId in serverroomids is invalid."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all joined members of a room.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_members<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(self.roomuserid_joined.scan_prefix(prefix).map(|(key, _)| {
|
||||||
|
UserId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("User ID in roomuserid_joined is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("User ID in roomuserid_joined is invalid."))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_joined_count(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
||||||
|
self.roomid_joinedcount
|
||||||
|
.get(room_id.as_bytes())?
|
||||||
|
.map(|b| {
|
||||||
|
utils::u64_from_bytes(&b)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid joinedcount in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_invited_count(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
||||||
|
self.roomid_invitedcount
|
||||||
|
.get(room_id.as_bytes())?
|
||||||
|
.map(|b| {
|
||||||
|
utils::u64_from_bytes(&b)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid joinedcount in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all User IDs who ever joined a room.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_useroncejoined<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(
|
||||||
|
self.roomuseroncejoinedids
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, _)| {
|
||||||
|
UserId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database(
|
||||||
|
"User ID in room_useroncejoined is invalid unicode.",
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("User ID in room_useroncejoined is invalid."))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all invited members of a room.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn room_members_invited<'a>(
|
||||||
|
&'a self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a> {
|
||||||
|
let mut prefix = room_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(
|
||||||
|
self.roomuserid_invitecount
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, _)| {
|
||||||
|
UserId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("User ID in roomuserid_invited is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("User ID in roomuserid_invited is invalid."))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn get_invite_count(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomuserid_invitecount
|
||||||
|
.get(&key)?
|
||||||
|
.map_or(Ok(None), |bytes| {
|
||||||
|
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid invitecount in db.")
|
||||||
|
})?))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn get_left_count(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.roomuserid_leftcount
|
||||||
|
.get(&key)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid leftcount in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all rooms this user joined.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn rooms_joined<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
|
||||||
|
Box::new(
|
||||||
|
self.userroomid_joined
|
||||||
|
.scan_prefix(user_id.as_bytes().to_vec())
|
||||||
|
.map(|(key, _)| {
|
||||||
|
RoomId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in userroomid_joined is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("Room ID in userroomid_joined is invalid."))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all rooms a user was invited to.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn rooms_invited<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<(OwnedRoomId, Vec<Raw<AnyStrippedStateEvent>>)>> + 'a> {
|
||||||
|
let mut prefix = user_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(
|
||||||
|
self.userroomid_invitestate
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, state)| {
|
||||||
|
let room_id = RoomId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in userroomid_invited is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in userroomid_invited is invalid.")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let state = serde_json::from_slice(&state).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid state in userroomid_invitestate.")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok((room_id, state))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn invite_state(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Result<Option<Vec<Raw<AnyStrippedStateEvent>>>> {
|
||||||
|
let mut key = user_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_invitestate
|
||||||
|
.get(&key)?
|
||||||
|
.map(|state| {
|
||||||
|
let state = serde_json::from_slice(&state)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid state in userroomid_invitestate."))?;
|
||||||
|
|
||||||
|
Ok(state)
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn left_state(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Result<Option<Vec<Raw<AnyStrippedStateEvent>>>> {
|
||||||
|
let mut key = user_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_leftstate
|
||||||
|
.get(&key)?
|
||||||
|
.map(|state| {
|
||||||
|
let state = serde_json::from_slice(&state)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid state in userroomid_leftstate."))?;
|
||||||
|
|
||||||
|
Ok(state)
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all rooms a user left.
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn rooms_left<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<(OwnedRoomId, Vec<Raw<AnySyncStateEvent>>)>> + 'a> {
|
||||||
|
let mut prefix = user_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
Box::new(
|
||||||
|
self.userroomid_leftstate
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, state)| {
|
||||||
|
let room_id = RoomId::parse(
|
||||||
|
utils::string_from_bytes(
|
||||||
|
key.rsplit(|&b| b == 0xff)
|
||||||
|
.next()
|
||||||
|
.expect("rsplit always returns an element"),
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in userroomid_invited is invalid unicode.")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
Error::bad_database("Room ID in userroomid_invited is invalid.")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let state = serde_json::from_slice(&state).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid state in userroomid_leftstate.")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok((room_id, state))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self.roomuseroncejoinedids.get(&userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn is_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_joined.get(&userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn is_invited(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_invitestate.get(&userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
fn is_left(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_leftstate.get(&userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
use std::{collections::HashSet, mem::size_of};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::KeyValueDatabase,
|
||||||
|
service::{self, rooms::state_compressor::data::StateDiff},
|
||||||
|
utils, Error, Result,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl service::rooms::state_compressor::Data for KeyValueDatabase {
|
||||||
|
fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> {
|
||||||
|
let value = self
|
||||||
|
.shortstatehash_statediff
|
||||||
|
.get(&shortstatehash.to_be_bytes())?
|
||||||
|
.ok_or_else(|| Error::bad_database("State hash does not exist"))?;
|
||||||
|
let parent =
|
||||||
|
utils::u64_from_bytes(&value[0..size_of::<u64>()]).expect("bytes have right length");
|
||||||
|
let parent = if parent != 0 { Some(parent) } else { None };
|
||||||
|
|
||||||
|
let mut add_mode = true;
|
||||||
|
let mut added = HashSet::new();
|
||||||
|
let mut removed = HashSet::new();
|
||||||
|
|
||||||
|
let mut i = size_of::<u64>();
|
||||||
|
while let Some(v) = value.get(i..i + 2 * size_of::<u64>()) {
|
||||||
|
if add_mode && v.starts_with(&0_u64.to_be_bytes()) {
|
||||||
|
add_mode = false;
|
||||||
|
i += size_of::<u64>();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if add_mode {
|
||||||
|
added.insert(v.try_into().expect("we checked the size above"));
|
||||||
|
} else {
|
||||||
|
removed.insert(v.try_into().expect("we checked the size above"));
|
||||||
|
}
|
||||||
|
i += 2 * size_of::<u64>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(StateDiff {
|
||||||
|
parent,
|
||||||
|
added,
|
||||||
|
removed,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_statediff(&self, shortstatehash: u64, diff: StateDiff) -> Result<()> {
|
||||||
|
let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec();
|
||||||
|
for new in &diff.added {
|
||||||
|
value.extend_from_slice(&new[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !diff.removed.is_empty() {
|
||||||
|
value.extend_from_slice(&0_u64.to_be_bytes());
|
||||||
|
for removed in &diff.removed {
|
||||||
|
value.extend_from_slice(&removed[..]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.shortstatehash_statediff
|
||||||
|
.insert(&shortstatehash.to_be_bytes(), &value)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,370 @@
|
|||||||
|
use std::{collections::hash_map, mem::size_of, sync::Arc};
|
||||||
|
|
||||||
|
use ruma::{
|
||||||
|
api::client::error::ErrorKind, CanonicalJsonObject, EventId, OwnedUserId, RoomId, UserId,
|
||||||
|
};
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, PduEvent, Result};
|
||||||
|
|
||||||
|
impl service::rooms::timeline::Data for KeyValueDatabase {
|
||||||
|
fn first_pdu_in_room(&self, room_id: &RoomId) -> Result<Option<Arc<PduEvent>>> {
|
||||||
|
let prefix = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
|
||||||
|
// Look for PDUs in that room.
|
||||||
|
self.pduid_pdu
|
||||||
|
.iter_from(&prefix, false)
|
||||||
|
.filter(|(k, _)| k.starts_with(&prefix))
|
||||||
|
.map(|(_, pdu)| {
|
||||||
|
serde_json::from_slice(&pdu)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid first PDU in db."))
|
||||||
|
.map(Arc::new)
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_timeline_count(&self, sender_user: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||||
|
match self
|
||||||
|
.lasttimelinecount_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.entry(room_id.to_owned())
|
||||||
|
{
|
||||||
|
hash_map::Entry::Vacant(v) => {
|
||||||
|
if let Some(last_count) = self
|
||||||
|
.pdus_until(sender_user, room_id, u64::MAX)?
|
||||||
|
.filter_map(|r| {
|
||||||
|
// Filter out buggy events
|
||||||
|
if r.is_err() {
|
||||||
|
error!("Bad pdu in pdus_since: {:?}", r);
|
||||||
|
}
|
||||||
|
r.ok()
|
||||||
|
})
|
||||||
|
.map(|(pduid, _)| self.pdu_count(&pduid))
|
||||||
|
.next()
|
||||||
|
{
|
||||||
|
Ok(*v.insert(last_count?))
|
||||||
|
} else {
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hash_map::Entry::Occupied(o) => Ok(*o.get()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the `count` of this pdu's id.
|
||||||
|
fn get_pdu_count(&self, event_id: &EventId) -> Result<Option<u64>> {
|
||||||
|
self.eventid_pduid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map(|pdu_id| self.pdu_count(&pdu_id))
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the json of a pdu.
|
||||||
|
fn get_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
|
||||||
|
self.eventid_pduid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map_or_else(
|
||||||
|
|| self.eventid_outlierpdu.get(event_id.as_bytes()),
|
||||||
|
|pduid| {
|
||||||
|
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| {
|
||||||
|
Error::bad_database("Invalid pduid in eventid_pduid.")
|
||||||
|
})?))
|
||||||
|
},
|
||||||
|
)?
|
||||||
|
.map(|pdu| {
|
||||||
|
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the json of a pdu.
|
||||||
|
fn get_non_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
|
||||||
|
self.eventid_pduid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map(|pduid| {
|
||||||
|
self.pduid_pdu
|
||||||
|
.get(&pduid)?
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid pduid in eventid_pduid."))
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.map(|pdu| {
|
||||||
|
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu's id.
|
||||||
|
fn get_pdu_id(&self, event_id: &EventId) -> Result<Option<Vec<u8>>> {
|
||||||
|
self.eventid_pduid.get(event_id.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
///
|
||||||
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
|
fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
|
||||||
|
self.eventid_pduid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map(|pduid| {
|
||||||
|
self.pduid_pdu
|
||||||
|
.get(&pduid)?
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid pduid in eventid_pduid."))
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.map(|pdu| {
|
||||||
|
serde_json::from_slice(&pdu).map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
///
|
||||||
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
|
fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
|
||||||
|
if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) {
|
||||||
|
return Ok(Some(Arc::clone(p)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(pdu) = self
|
||||||
|
.eventid_pduid
|
||||||
|
.get(event_id.as_bytes())?
|
||||||
|
.map_or_else(
|
||||||
|
|| self.eventid_outlierpdu.get(event_id.as_bytes()),
|
||||||
|
|pduid| {
|
||||||
|
Ok(Some(self.pduid_pdu.get(&pduid)?.ok_or_else(|| {
|
||||||
|
Error::bad_database("Invalid pduid in eventid_pduid.")
|
||||||
|
})?))
|
||||||
|
},
|
||||||
|
)?
|
||||||
|
.map(|pdu| {
|
||||||
|
serde_json::from_slice(&pdu)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid PDU in db."))
|
||||||
|
.map(Arc::new)
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
{
|
||||||
|
self.pdu_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(event_id.to_owned(), Arc::clone(&pdu));
|
||||||
|
Ok(Some(pdu))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
///
|
||||||
|
/// This does __NOT__ check the outliers `Tree`.
|
||||||
|
fn get_pdu_from_id(&self, pdu_id: &[u8]) -> Result<Option<PduEvent>> {
|
||||||
|
self.pduid_pdu.get(pdu_id)?.map_or(Ok(None), |pdu| {
|
||||||
|
Ok(Some(
|
||||||
|
serde_json::from_slice(&pdu)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid PDU in db."))?,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`.
|
||||||
|
fn get_pdu_json_from_id(&self, pdu_id: &[u8]) -> Result<Option<CanonicalJsonObject>> {
|
||||||
|
self.pduid_pdu.get(pdu_id)?.map_or(Ok(None), |pdu| {
|
||||||
|
Ok(Some(
|
||||||
|
serde_json::from_slice(&pdu)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid PDU in db."))?,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the `count` of this pdu's id.
|
||||||
|
fn pdu_count(&self, pdu_id: &[u8]) -> Result<u64> {
|
||||||
|
utils::u64_from_bytes(&pdu_id[pdu_id.len() - size_of::<u64>()..])
|
||||||
|
.map_err(|_| Error::bad_database("PDU has invalid count bytes."))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_pdu(
|
||||||
|
&self,
|
||||||
|
pdu_id: &[u8],
|
||||||
|
pdu: &PduEvent,
|
||||||
|
json: &CanonicalJsonObject,
|
||||||
|
count: u64,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.pduid_pdu.insert(
|
||||||
|
pdu_id,
|
||||||
|
&serde_json::to_vec(json).expect("CanonicalJsonObject is always a valid"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.lasttimelinecount_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(pdu.room_id.clone(), count);
|
||||||
|
|
||||||
|
self.eventid_pduid.insert(pdu.event_id.as_bytes(), pdu_id)?;
|
||||||
|
self.eventid_outlierpdu.remove(pdu.event_id.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes a pdu and creates a new one with the same id.
|
||||||
|
fn replace_pdu(&self, pdu_id: &[u8], pdu: &PduEvent) -> Result<()> {
|
||||||
|
if self.pduid_pdu.get(pdu_id)?.is_some() {
|
||||||
|
self.pduid_pdu.insert(
|
||||||
|
pdu_id,
|
||||||
|
&serde_json::to_vec(pdu).expect("CanonicalJsonObject is always a valid"),
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::BadRequest(
|
||||||
|
ErrorKind::NotFound,
|
||||||
|
"PDU does not exist.",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all events in a room that happened after the event with id `since`
|
||||||
|
/// in chronological order.
|
||||||
|
fn pdus_since<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
since: u64,
|
||||||
|
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a>> {
|
||||||
|
let prefix = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
|
||||||
|
// Skip the first pdu if it's exactly at since, because we sent that last time
|
||||||
|
let mut first_pdu_id = prefix.clone();
|
||||||
|
first_pdu_id.extend_from_slice(&(since + 1).to_be_bytes());
|
||||||
|
|
||||||
|
let user_id = user_id.to_owned();
|
||||||
|
|
||||||
|
Ok(Box::new(
|
||||||
|
self.pduid_pdu
|
||||||
|
.iter_from(&first_pdu_id, false)
|
||||||
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||||
|
.map(move |(pdu_id, v)| {
|
||||||
|
let mut pdu = serde_json::from_slice::<PduEvent>(&v)
|
||||||
|
.map_err(|_| Error::bad_database("PDU in db is invalid."))?;
|
||||||
|
if pdu.sender != user_id {
|
||||||
|
pdu.remove_transaction_id()?;
|
||||||
|
}
|
||||||
|
Ok((pdu_id, pdu))
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all events and their tokens in a room that happened before the
|
||||||
|
/// event with id `until` in reverse-chronological order.
|
||||||
|
fn pdus_until<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
until: u64,
|
||||||
|
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a>> {
|
||||||
|
// Create the first part of the full pdu id
|
||||||
|
let prefix = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
|
||||||
|
let mut current = prefix.clone();
|
||||||
|
current.extend_from_slice(&(until.saturating_sub(1)).to_be_bytes()); // -1 because we don't want event at `until`
|
||||||
|
|
||||||
|
let current: &[u8] = ¤t;
|
||||||
|
|
||||||
|
let user_id = user_id.to_owned();
|
||||||
|
|
||||||
|
Ok(Box::new(
|
||||||
|
self.pduid_pdu
|
||||||
|
.iter_from(current, true)
|
||||||
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||||
|
.map(move |(pdu_id, v)| {
|
||||||
|
let mut pdu = serde_json::from_slice::<PduEvent>(&v)
|
||||||
|
.map_err(|_| Error::bad_database("PDU in db is invalid."))?;
|
||||||
|
if pdu.sender != user_id {
|
||||||
|
pdu.remove_transaction_id()?;
|
||||||
|
}
|
||||||
|
Ok((pdu_id, pdu))
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pdus_after<'a>(
|
||||||
|
&'a self,
|
||||||
|
user_id: &UserId,
|
||||||
|
room_id: &RoomId,
|
||||||
|
from: u64,
|
||||||
|
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a>> {
|
||||||
|
// Create the first part of the full pdu id
|
||||||
|
let prefix = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists")
|
||||||
|
.to_be_bytes()
|
||||||
|
.to_vec();
|
||||||
|
|
||||||
|
let mut current = prefix.clone();
|
||||||
|
current.extend_from_slice(&(from + 1).to_be_bytes()); // +1 so we don't send the base event
|
||||||
|
|
||||||
|
let current: &[u8] = ¤t;
|
||||||
|
|
||||||
|
let user_id = user_id.to_owned();
|
||||||
|
|
||||||
|
Ok(Box::new(
|
||||||
|
self.pduid_pdu
|
||||||
|
.iter_from(current, false)
|
||||||
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||||
|
.map(move |(pdu_id, v)| {
|
||||||
|
let mut pdu = serde_json::from_slice::<PduEvent>(&v)
|
||||||
|
.map_err(|_| Error::bad_database("PDU in db is invalid."))?;
|
||||||
|
if pdu.sender != user_id {
|
||||||
|
pdu.remove_transaction_id()?;
|
||||||
|
}
|
||||||
|
Ok((pdu_id, pdu))
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment_notification_counts(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
notifies: Vec<OwnedUserId>,
|
||||||
|
highlights: Vec<OwnedUserId>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut notifies_batch = Vec::new();
|
||||||
|
let mut highlights_batch = Vec::new();
|
||||||
|
for user in notifies {
|
||||||
|
let mut userroom_id = user.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
notifies_batch.push(userroom_id);
|
||||||
|
}
|
||||||
|
for user in highlights {
|
||||||
|
let mut userroom_id = user.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
highlights_batch.push(userroom_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.userroomid_notificationcount
|
||||||
|
.increment_batch(&mut notifies_batch.into_iter())?;
|
||||||
|
self.userroomid_highlightcount
|
||||||
|
.increment_batch(&mut highlights_batch.into_iter())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,149 @@
|
|||||||
|
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
|
||||||
|
|
||||||
|
impl service::rooms::user::Data for KeyValueDatabase {
|
||||||
|
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
let mut roomuser_id = room_id.as_bytes().to_vec();
|
||||||
|
roomuser_id.push(0xff);
|
||||||
|
roomuser_id.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_notificationcount
|
||||||
|
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
||||||
|
self.userroomid_highlightcount
|
||||||
|
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
||||||
|
|
||||||
|
self.roomuserid_lastnotificationread.insert(
|
||||||
|
&roomuser_id,
|
||||||
|
&services().globals.next_count()?.to_be_bytes(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_notificationcount
|
||||||
|
.get(&userroom_id)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid notification count in db."))
|
||||||
|
})
|
||||||
|
.unwrap_or(Ok(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
|
||||||
|
self.userroomid_highlightcount
|
||||||
|
.get(&userroom_id)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid highlight count in db."))
|
||||||
|
})
|
||||||
|
.unwrap_or(Ok(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_notification_read(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||||
|
let mut key = room_id.as_bytes().to_vec();
|
||||||
|
key.push(0xff);
|
||||||
|
key.extend_from_slice(user_id.as_bytes());
|
||||||
|
|
||||||
|
Ok(self
|
||||||
|
.roomuserid_lastnotificationread
|
||||||
|
.get(&key)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn associate_token_shortstatehash(
|
||||||
|
&self,
|
||||||
|
room_id: &RoomId,
|
||||||
|
token: u64,
|
||||||
|
shortstatehash: u64,
|
||||||
|
) -> Result<()> {
|
||||||
|
let shortroomid = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists");
|
||||||
|
|
||||||
|
let mut key = shortroomid.to_be_bytes().to_vec();
|
||||||
|
key.extend_from_slice(&token.to_be_bytes());
|
||||||
|
|
||||||
|
self.roomsynctoken_shortstatehash
|
||||||
|
.insert(&key, &shortstatehash.to_be_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>> {
|
||||||
|
let shortroomid = services()
|
||||||
|
.rooms
|
||||||
|
.short
|
||||||
|
.get_shortroomid(room_id)?
|
||||||
|
.expect("room exists");
|
||||||
|
|
||||||
|
let mut key = shortroomid.to_be_bytes().to_vec();
|
||||||
|
key.extend_from_slice(&token.to_be_bytes());
|
||||||
|
|
||||||
|
self.roomsynctoken_shortstatehash
|
||||||
|
.get(&key)?
|
||||||
|
.map(|bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid shortstatehash in roomsynctoken_shortstatehash")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_shared_rooms<'a>(
|
||||||
|
&'a self,
|
||||||
|
users: Vec<OwnedUserId>,
|
||||||
|
) -> Result<Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a>> {
|
||||||
|
let iterators = users.into_iter().map(move |user_id| {
|
||||||
|
let mut prefix = user_id.as_bytes().to_vec();
|
||||||
|
prefix.push(0xff);
|
||||||
|
|
||||||
|
self.userroomid_joined
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, _)| {
|
||||||
|
let roomid_index = key
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, &b)| b == 0xff)
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid userroomid_joined in db."))?
|
||||||
|
.0
|
||||||
|
+ 1; // +1 because the room id starts AFTER the separator
|
||||||
|
|
||||||
|
let room_id = key[roomid_index..].to_vec();
|
||||||
|
|
||||||
|
Ok::<_, Error>(room_id)
|
||||||
|
})
|
||||||
|
.filter_map(|r| r.ok())
|
||||||
|
});
|
||||||
|
|
||||||
|
// We use the default compare function because keys are sorted correctly (not reversed)
|
||||||
|
Ok(Box::new(
|
||||||
|
utils::common_elements(iterators, Ord::cmp)
|
||||||
|
.expect("users is not empty")
|
||||||
|
.map(|bytes| {
|
||||||
|
RoomId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid RoomId bytes in userroomid_joined")
|
||||||
|
})?)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,205 @@
|
|||||||
|
use ruma::{ServerName, UserId};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::KeyValueDatabase,
|
||||||
|
service::{
|
||||||
|
self,
|
||||||
|
sending::{OutgoingKind, SendingEventType},
|
||||||
|
},
|
||||||
|
services, utils, Error, Result,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl service::sending::Data for KeyValueDatabase {
|
||||||
|
fn active_requests<'a>(
|
||||||
|
&'a self,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<(Vec<u8>, OutgoingKind, SendingEventType)>> + 'a> {
|
||||||
|
Box::new(
|
||||||
|
self.servercurrentevent_data
|
||||||
|
.iter()
|
||||||
|
.map(|(key, v)| parse_servercurrentevent(&key, v).map(|(k, e)| (key, k, e))),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn active_requests_for<'a>(
|
||||||
|
&'a self,
|
||||||
|
outgoing_kind: &OutgoingKind,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<(Vec<u8>, SendingEventType)>> + 'a> {
|
||||||
|
let prefix = outgoing_kind.get_prefix();
|
||||||
|
Box::new(
|
||||||
|
self.servercurrentevent_data
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(key, v)| parse_servercurrentevent(&key, v).map(|(_, e)| (key, e))),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_active_request(&self, key: Vec<u8>) -> Result<()> {
|
||||||
|
self.servercurrentevent_data.remove(&key)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_all_active_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()> {
|
||||||
|
let prefix = outgoing_kind.get_prefix();
|
||||||
|
for (key, _) in self.servercurrentevent_data.scan_prefix(prefix) {
|
||||||
|
self.servercurrentevent_data.remove(&key)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_all_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()> {
|
||||||
|
let prefix = outgoing_kind.get_prefix();
|
||||||
|
for (key, _) in self.servercurrentevent_data.scan_prefix(prefix.clone()) {
|
||||||
|
self.servercurrentevent_data.remove(&key).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (key, _) in self.servernameevent_data.scan_prefix(prefix) {
|
||||||
|
self.servernameevent_data.remove(&key).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn queue_requests(
|
||||||
|
&self,
|
||||||
|
requests: &[(&OutgoingKind, SendingEventType)],
|
||||||
|
) -> Result<Vec<Vec<u8>>> {
|
||||||
|
let mut batch = Vec::new();
|
||||||
|
let mut keys = Vec::new();
|
||||||
|
for (outgoing_kind, event) in requests {
|
||||||
|
let mut key = outgoing_kind.get_prefix();
|
||||||
|
if let SendingEventType::Pdu(value) = &event {
|
||||||
|
key.extend_from_slice(value)
|
||||||
|
} else {
|
||||||
|
key.extend_from_slice(&services().globals.next_count()?.to_be_bytes())
|
||||||
|
}
|
||||||
|
let value = if let SendingEventType::Edu(value) = &event {
|
||||||
|
&**value
|
||||||
|
} else {
|
||||||
|
&[]
|
||||||
|
};
|
||||||
|
batch.push((key.clone(), value.to_owned()));
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
self.servernameevent_data
|
||||||
|
.insert_batch(&mut batch.into_iter())?;
|
||||||
|
Ok(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn queued_requests<'a>(
|
||||||
|
&'a self,
|
||||||
|
outgoing_kind: &OutgoingKind,
|
||||||
|
) -> Box<dyn Iterator<Item = Result<(SendingEventType, Vec<u8>)>> + 'a> {
|
||||||
|
let prefix = outgoing_kind.get_prefix();
|
||||||
|
return Box::new(
|
||||||
|
self.servernameevent_data
|
||||||
|
.scan_prefix(prefix)
|
||||||
|
.map(|(k, v)| parse_servercurrentevent(&k, v).map(|(_, ev)| (ev, k))),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_as_active(&self, events: &[(SendingEventType, Vec<u8>)]) -> Result<()> {
|
||||||
|
for (e, key) in events {
|
||||||
|
let value = if let SendingEventType::Edu(value) = &e {
|
||||||
|
&**value
|
||||||
|
} else {
|
||||||
|
&[]
|
||||||
|
};
|
||||||
|
self.servercurrentevent_data.insert(key, value)?;
|
||||||
|
self.servernameevent_data.remove(key)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_latest_educount(&self, server_name: &ServerName, last_count: u64) -> Result<()> {
|
||||||
|
self.servername_educount
|
||||||
|
.insert(server_name.as_bytes(), &last_count.to_be_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_latest_educount(&self, server_name: &ServerName) -> Result<u64> {
|
||||||
|
self.servername_educount
|
||||||
|
.get(server_name.as_bytes())?
|
||||||
|
.map_or(Ok(0), |bytes| {
|
||||||
|
utils::u64_from_bytes(&bytes)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid u64 in servername_educount."))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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);
|
||||||
|
|
||||||
|
let server = parts.next().expect("splitn always returns one element");
|
||||||
|
let event = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
||||||
|
|
||||||
|
let server = utils::string_from_bytes(server).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid server bytes in server_currenttransaction")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
(
|
||||||
|
OutgoingKind::Appservice(server),
|
||||||
|
if value.is_empty() {
|
||||||
|
SendingEventType::Pdu(event.to_vec())
|
||||||
|
} else {
|
||||||
|
SendingEventType::Edu(value)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else if key.starts_with(b"$") {
|
||||||
|
let mut parts = key[1..].splitn(3, |&b| b == 0xff);
|
||||||
|
|
||||||
|
let user = parts.next().expect("splitn always returns one element");
|
||||||
|
let user_string = utils::string_from_bytes(user)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid user string in servercurrentevent"))?;
|
||||||
|
let user_id = UserId::parse(user_string)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid user id in servercurrentevent"))?;
|
||||||
|
|
||||||
|
let pushkey = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
||||||
|
let pushkey_string = utils::string_from_bytes(pushkey)
|
||||||
|
.map_err(|_| Error::bad_database("Invalid pushkey in servercurrentevent"))?;
|
||||||
|
|
||||||
|
let event = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
||||||
|
|
||||||
|
(
|
||||||
|
OutgoingKind::Push(user_id, pushkey_string),
|
||||||
|
if value.is_empty() {
|
||||||
|
SendingEventType::Pdu(event.to_vec())
|
||||||
|
} else {
|
||||||
|
// I'm pretty sure this should never be called
|
||||||
|
SendingEventType::Edu(value)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let mut parts = key.splitn(2, |&b| b == 0xff);
|
||||||
|
|
||||||
|
let server = parts.next().expect("splitn always returns one element");
|
||||||
|
let event = parts
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
||||||
|
|
||||||
|
let server = utils::string_from_bytes(server).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid server bytes in server_currenttransaction")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
(
|
||||||
|
OutgoingKind::Normal(ServerName::parse(server).map_err(|_| {
|
||||||
|
Error::bad_database("Invalid server string in server_currenttransaction")
|
||||||
|
})?),
|
||||||
|
if value.is_empty() {
|
||||||
|
SendingEventType::Pdu(event.to_vec())
|
||||||
|
} else {
|
||||||
|
SendingEventType::Edu(value)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
@ -0,0 +1,89 @@
|
|||||||
|
use ruma::{
|
||||||
|
api::client::{error::ErrorKind, uiaa::UiaaInfo},
|
||||||
|
CanonicalJsonValue, DeviceId, UserId,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{database::KeyValueDatabase, service, Error, Result};
|
||||||
|
|
||||||
|
impl service::uiaa::Data for KeyValueDatabase {
|
||||||
|
fn set_uiaa_request(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
session: &str,
|
||||||
|
request: &CanonicalJsonValue,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.userdevicesessionid_uiaarequest
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(
|
||||||
|
(user_id.to_owned(), device_id.to_owned(), session.to_owned()),
|
||||||
|
request.to_owned(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_uiaa_request(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
session: &str,
|
||||||
|
) -> Option<CanonicalJsonValue> {
|
||||||
|
self.userdevicesessionid_uiaarequest
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.get(&(user_id.to_owned(), device_id.to_owned(), session.to_owned()))
|
||||||
|
.map(|j| j.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_uiaa_session(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
session: &str,
|
||||||
|
uiaainfo: Option<&UiaaInfo>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut userdevicesessionid = user_id.as_bytes().to_vec();
|
||||||
|
userdevicesessionid.push(0xff);
|
||||||
|
userdevicesessionid.extend_from_slice(device_id.as_bytes());
|
||||||
|
userdevicesessionid.push(0xff);
|
||||||
|
userdevicesessionid.extend_from_slice(session.as_bytes());
|
||||||
|
|
||||||
|
if let Some(uiaainfo) = uiaainfo {
|
||||||
|
self.userdevicesessionid_uiaainfo.insert(
|
||||||
|
&userdevicesessionid,
|
||||||
|
&serde_json::to_vec(&uiaainfo).expect("UiaaInfo::to_vec always works"),
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
self.userdevicesessionid_uiaainfo
|
||||||
|
.remove(&userdevicesessionid)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_uiaa_session(
|
||||||
|
&self,
|
||||||
|
user_id: &UserId,
|
||||||
|
device_id: &DeviceId,
|
||||||
|
session: &str,
|
||||||
|
) -> Result<UiaaInfo> {
|
||||||
|
let mut userdevicesessionid = user_id.as_bytes().to_vec();
|
||||||
|
userdevicesessionid.push(0xff);
|
||||||
|
userdevicesessionid.extend_from_slice(device_id.as_bytes());
|
||||||
|
userdevicesessionid.push(0xff);
|
||||||
|
userdevicesessionid.extend_from_slice(session.as_bytes());
|
||||||
|
|
||||||
|
serde_json::from_slice(
|
||||||
|
&self
|
||||||
|
.userdevicesessionid_uiaainfo
|
||||||
|
.get(&userdevicesessionid)?
|
||||||
|
.ok_or(Error::BadRequest(
|
||||||
|
ErrorKind::Forbidden,
|
||||||
|
"UIAA session does not exist.",
|
||||||
|
))?,
|
||||||
|
)
|
||||||
|
.map_err(|_| Error::bad_database("UiaaInfo in userdeviceid_uiaainfo is invalid."))
|
||||||
|
}
|
||||||
|
}
|
@ -1,358 +0,0 @@
|
|||||||
use crate::database::globals::Globals;
|
|
||||||
use image::{imageops::FilterType, GenericImageView};
|
|
||||||
|
|
||||||
use super::abstraction::Tree;
|
|
||||||
use crate::{utils, Error, Result};
|
|
||||||
use std::{mem, sync::Arc};
|
|
||||||
use tokio::{
|
|
||||||
fs::File,
|
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct FileMeta {
|
|
||||||
pub content_disposition: Option<String>,
|
|
||||||
pub content_type: Option<String>,
|
|
||||||
pub file: Vec<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Media {
|
|
||||||
pub(super) mediaid_file: Arc<dyn Tree>, // MediaId = MXC + WidthHeight + ContentDisposition + ContentType
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Media {
|
|
||||||
/// Uploads a file.
|
|
||||||
pub async fn create(
|
|
||||||
&self,
|
|
||||||
mxc: String,
|
|
||||||
globals: &Globals,
|
|
||||||
content_disposition: &Option<&str>,
|
|
||||||
content_type: &Option<&str>,
|
|
||||||
file: &[u8],
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut key = mxc.as_bytes().to_vec();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
||||||
key.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(
|
|
||||||
content_disposition
|
|
||||||
.as_ref()
|
|
||||||
.map(|f| f.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(
|
|
||||||
content_type
|
|
||||||
.as_ref()
|
|
||||||
.map(|c| c.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let path = globals.get_media_file(&key);
|
|
||||||
let mut f = File::create(path).await?;
|
|
||||||
f.write_all(file).await?;
|
|
||||||
|
|
||||||
self.mediaid_file.insert(&key, &[])?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Uploads or replaces a file thumbnail.
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub async fn upload_thumbnail(
|
|
||||||
&self,
|
|
||||||
mxc: String,
|
|
||||||
globals: &Globals,
|
|
||||||
content_disposition: &Option<String>,
|
|
||||||
content_type: &Option<String>,
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
file: &[u8],
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut key = mxc.as_bytes().to_vec();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&width.to_be_bytes());
|
|
||||||
key.extend_from_slice(&height.to_be_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(
|
|
||||||
content_disposition
|
|
||||||
.as_ref()
|
|
||||||
.map(|f| f.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(
|
|
||||||
content_type
|
|
||||||
.as_ref()
|
|
||||||
.map(|c| c.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let path = globals.get_media_file(&key);
|
|
||||||
let mut f = File::create(path).await?;
|
|
||||||
f.write_all(file).await?;
|
|
||||||
|
|
||||||
self.mediaid_file.insert(&key, &[])?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Downloads a file.
|
|
||||||
pub async fn get(&self, globals: &Globals, mxc: &str) -> Result<Option<FileMeta>> {
|
|
||||||
let mut prefix = mxc.as_bytes().to_vec();
|
|
||||||
prefix.push(0xff);
|
|
||||||
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
||||||
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
||||||
prefix.push(0xff);
|
|
||||||
|
|
||||||
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?;
|
|
||||||
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
||||||
|
|
||||||
let content_type = parts
|
|
||||||
.next()
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::string_from_bytes(bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Content type in mediaid_file is invalid unicode.")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
let content_disposition_bytes = parts
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
|
||||||
|
|
||||||
let content_disposition = if content_disposition_bytes.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(
|
|
||||||
utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
|
|
||||||
Error::bad_database(
|
|
||||||
"Content Disposition in mediaid_file is invalid unicode.",
|
|
||||||
)
|
|
||||||
})?,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file,
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns width, height of the thumbnail and whether it should be cropped. Returns None when
|
|
||||||
/// the server should send the original file.
|
|
||||||
pub fn thumbnail_properties(&self, width: u32, height: u32) -> Option<(u32, u32, bool)> {
|
|
||||||
match (width, height) {
|
|
||||||
(0..=32, 0..=32) => Some((32, 32, true)),
|
|
||||||
(0..=96, 0..=96) => Some((96, 96, true)),
|
|
||||||
(0..=320, 0..=240) => Some((320, 240, false)),
|
|
||||||
(0..=640, 0..=480) => Some((640, 480, false)),
|
|
||||||
(0..=800, 0..=600) => Some((800, 600, false)),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Downloads a file's thumbnail.
|
|
||||||
///
|
|
||||||
/// Here's an example on how it works:
|
|
||||||
///
|
|
||||||
/// - Client requests an image with width=567, height=567
|
|
||||||
/// - Server rounds that up to (800, 600), so it doesn't have to save too many thumbnails
|
|
||||||
/// - Server rounds that up again to (958, 600) to fix the aspect ratio (only for width,height>96)
|
|
||||||
/// - Server creates the thumbnail and sends it to the user
|
|
||||||
///
|
|
||||||
/// For width,height <= 96 the server uses another thumbnailing algorithm which crops the image afterwards.
|
|
||||||
pub async fn get_thumbnail(
|
|
||||||
&self,
|
|
||||||
mxc: &str,
|
|
||||||
globals: &Globals,
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
) -> Result<Option<FileMeta>> {
|
|
||||||
let (width, height, crop) = self
|
|
||||||
.thumbnail_properties(width, height)
|
|
||||||
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file
|
|
||||||
|
|
||||||
let mut main_prefix = mxc.as_bytes().to_vec();
|
|
||||||
main_prefix.push(0xff);
|
|
||||||
|
|
||||||
let mut thumbnail_prefix = main_prefix.clone();
|
|
||||||
thumbnail_prefix.extend_from_slice(&width.to_be_bytes());
|
|
||||||
thumbnail_prefix.extend_from_slice(&height.to_be_bytes());
|
|
||||||
thumbnail_prefix.push(0xff);
|
|
||||||
|
|
||||||
let mut original_prefix = main_prefix;
|
|
||||||
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
||||||
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
||||||
original_prefix.push(0xff);
|
|
||||||
|
|
||||||
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();
|
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
|
||||||
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
||||||
|
|
||||||
let content_type = parts
|
|
||||||
.next()
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::string_from_bytes(bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Content type in mediaid_file is invalid unicode.")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
let content_disposition_bytes = parts
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
|
||||||
|
|
||||||
let content_disposition = if content_disposition_bytes.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(
|
|
||||||
utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Content Disposition in db is invalid.")
|
|
||||||
})?,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file: file.to_vec(),
|
|
||||||
}))
|
|
||||||
} else if let Some((key, _)) = first_originalprefix {
|
|
||||||
// Generate a thumbnail
|
|
||||||
let path = globals.get_media_file(&key);
|
|
||||||
let mut file = Vec::new();
|
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
|
||||||
|
|
||||||
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
||||||
|
|
||||||
let content_type = parts
|
|
||||||
.next()
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::string_from_bytes(bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Content type in mediaid_file is invalid unicode.")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
let content_disposition_bytes = parts
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
|
||||||
|
|
||||||
let content_disposition = if content_disposition_bytes.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(
|
|
||||||
utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
|
|
||||||
Error::bad_database(
|
|
||||||
"Content Disposition in mediaid_file is invalid unicode.",
|
|
||||||
)
|
|
||||||
})?,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Ok(image) = image::load_from_memory(&file) {
|
|
||||||
let original_width = image.width();
|
|
||||||
let original_height = image.height();
|
|
||||||
if width > original_width || height > original_height {
|
|
||||||
return Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file: file.to_vec(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
let thumbnail = if crop {
|
|
||||||
image.resize_to_fill(width, height, FilterType::CatmullRom)
|
|
||||||
} else {
|
|
||||||
let (exact_width, exact_height) = {
|
|
||||||
// Copied from image::dynimage::resize_dimensions
|
|
||||||
let ratio = u64::from(original_width) * u64::from(height);
|
|
||||||
let nratio = u64::from(width) * u64::from(original_height);
|
|
||||||
|
|
||||||
let use_width = nratio <= ratio;
|
|
||||||
let intermediate = if use_width {
|
|
||||||
u64::from(original_height) * u64::from(width)
|
|
||||||
/ u64::from(original_width)
|
|
||||||
} else {
|
|
||||||
u64::from(original_width) * u64::from(height)
|
|
||||||
/ u64::from(original_height)
|
|
||||||
};
|
|
||||||
if use_width {
|
|
||||||
if intermediate <= u64::from(::std::u32::MAX) {
|
|
||||||
(width, intermediate as u32)
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
(u64::from(width) * u64::from(::std::u32::MAX) / intermediate)
|
|
||||||
as u32,
|
|
||||||
::std::u32::MAX,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else if intermediate <= u64::from(::std::u32::MAX) {
|
|
||||||
(intermediate as u32, height)
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
::std::u32::MAX,
|
|
||||||
(u64::from(height) * u64::from(::std::u32::MAX) / intermediate)
|
|
||||||
as u32,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
image.thumbnail_exact(exact_width, exact_height)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut thumbnail_bytes = Vec::new();
|
|
||||||
thumbnail.write_to(&mut thumbnail_bytes, image::ImageOutputFormat::Png)?;
|
|
||||||
|
|
||||||
// Save thumbnail in database so we don't have to generate it again next time
|
|
||||||
let mut thumbnail_key = key.to_vec();
|
|
||||||
let width_index = thumbnail_key
|
|
||||||
.iter()
|
|
||||||
.position(|&b| b == 0xff)
|
|
||||||
.ok_or_else(|| Error::bad_database("Media in db is invalid."))?
|
|
||||||
+ 1;
|
|
||||||
let mut widthheight = width.to_be_bytes().to_vec();
|
|
||||||
widthheight.extend_from_slice(&height.to_be_bytes());
|
|
||||||
|
|
||||||
thumbnail_key.splice(
|
|
||||||
width_index..width_index + 2 * mem::size_of::<u32>(),
|
|
||||||
widthheight,
|
|
||||||
);
|
|
||||||
|
|
||||||
let path = globals.get_media_file(&thumbnail_key);
|
|
||||||
let mut f = File::create(path).await?;
|
|
||||||
f.write_all(&thumbnail_bytes).await?;
|
|
||||||
|
|
||||||
self.mediaid_file.insert(&thumbnail_key, &[])?;
|
|
||||||
|
|
||||||
Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file: thumbnail_bytes.to_vec(),
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
// Couldn't parse file to generate thumbnail, send original
|
|
||||||
Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file: file.to_vec(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue