Compare commits

..

20 Commits

Author SHA1 Message Date
Zomatree
553437e1df chore: update es code to lapin
Signed-off-by: Zomatree <me@zomatree.live>
2026-06-25 06:20:24 +01:00
Zomatree
6a421dcd37 Merge remote-tracking branch 'origin/main' into feat/elasticsearch
Signed-off-by: Zomatree <me@zomatree.live>
2026-06-25 06:19:17 +01:00
Zomatree
f3a64200cf Merge branch 'main' into feat/elasticsearch
Signed-off-by: Zomatree <me@zomatree.live>
2026-04-16 22:58:18 +01:00
Zomatree
9c62568132 feat: search for links and author type
Signed-off-by: Zomatree <me@zomatree.live>
2026-04-09 03:54:14 +01:00
Zomatree
b7ed543156 Merge branch 'main' into feat/elasticsearch
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 08:13:30 +01:00
Zomatree
7ec3b07f6c Merge remote-tracking branch 'origin/feat/elasticsearch' into feat/elasticsearch
Signed-off-by: Zomatree <me@zomatree.live>

# Conflicts:
#	Cargo.lock
#	compose.yml
#	crates/core/config/src/lib.rs
#	crates/core/database/src/models/messages/ops.rs
#	crates/core/database/src/models/messages/ops/mongodb.rs
#	crates/core/database/src/models/messages/ops/reference.rs
#	crates/delta/src/routes/servers/server_delete.rs
2026-03-29 06:07:10 +01:00
Zomatree
fb487130c8 feat: use bulk indexing for existing messages
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:44:29 +01:00
Zomatree
3fd170a7de fix: switch to hashset
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:44:28 +01:00
Zomatree
ab159ccaa3 fix: disable search for bots
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:44:28 +01:00
Zomatree
d97260f2fe feat: search by message components
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:44:28 +01:00
Zomatree
f977432e14 fix: add missing consumer
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:43:58 +01:00
Zomatree
8009b3bf53 feat: initial work on elasticsearch integration
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:43:58 +01:00
Zomatree
d9643ebd8d feat: initiate mongo replset by default
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-29 05:37:43 +01:00
Zomatree
d568b4369e feat: use bulk indexing for existing messages
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-21 06:59:56 +00:00
Zomatree
3b5491a438 fix: switch to hashset
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-21 02:57:59 +00:00
Zomatree
083f071c30 fix: disable search for bots
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-21 02:57:13 +00:00
Zomatree
19df5c5ef5 feat: search by message components
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-20 15:24:56 +00:00
Zomatree
1745826d2f Merge remote-tracking branch 'origin' into feat/elasticsearch
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-19 23:40:09 +00:00
Zomatree
61a9fab37f fix: add missing consumer
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-19 23:38:31 +00:00
Zomatree
96efc76866 feat: initial work on elasticsearch integration
Signed-off-by: Zomatree <me@zomatree.live>
2026-03-19 23:04:37 +00:00
85 changed files with 2286 additions and 499 deletions

View File

@@ -12,7 +12,7 @@ permissions:
packages: write
concurrency:
group: docker-test-${{ github.head_ref || github.ref }}
group: ${{ github.head_ref || github.ref }}
cancel-in-progress: true
jobs:

View File

@@ -6,7 +6,7 @@ on:
pull_request:
concurrency:
group: rust-build-test-${{ github.head_ref || github.ref }}
group: ${{ github.head_ref || github.ref }}
cancel-in-progress: true
env:

View File

@@ -2,7 +2,7 @@
node = "25.4.0"
pnpm = "10.28.1"
gh = "2.25.0"
gh = "2.95.0"
rust = "1.92.0"
"cargo:cargo-nextest" = "0.9.122"

226
Cargo.lock generated
View File

@@ -300,6 +300,18 @@ dependencies = [
"tokio",
]
[[package]]
name = "async-compression"
version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e79b3f8a79cccc2898f31920fc69f304859b3bd567490f75ebf51ae1c792a9ac"
dependencies = [
"compression-codecs",
"compression-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "async-executor"
version = "1.14.0"
@@ -1325,6 +1337,15 @@ dependencies = [
"alloc-stdlib",
]
[[package]]
name = "bs58"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4"
dependencies = [
"tinyvec",
]
[[package]]
name = "bson"
version = "2.15.0"
@@ -1491,9 +1512,9 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chacha20"
version = "0.10.0"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601"
checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81"
dependencies = [
"cfg-if",
"cpufeatures 0.3.0",
@@ -1509,6 +1530,7 @@ dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link",
]
@@ -1581,6 +1603,23 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "compression-codecs"
version = "0.4.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce2548391e9c1929c21bf6aa2680af86fe4c1b33e6cea9ac1cfeec0bd11218cf"
dependencies = [
"compression-core",
"flate2",
"memchr",
]
[[package]]
name = "compression-core"
version = "0.4.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789"
[[package]]
name = "concurrent-queue"
version = "2.5.0"
@@ -2501,6 +2540,41 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
[[package]]
name = "elasticsearch"
version = "9.1.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12bb303aa6e1d28c0c86b6fbfe484fd0fd3f512629aeed1ac4f6b85f81d9834a"
dependencies = [
"base64 0.22.1",
"bytes",
"dyn-clone",
"flate2",
"lazy_static",
"parking_lot",
"percent-encoding",
"reqwest 0.12.28",
"rustc_version",
"serde",
"serde_json",
"serde_with",
"tokio",
"url",
"void",
]
[[package]]
name = "elasticsearch-dsl"
version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9445aba099fdf52dea22534cf953b918d1e4cbb799c99507c9cc96188d9c230"
dependencies = [
"chrono",
"num-traits",
"serde",
"serde_json",
]
[[package]]
name = "elliptic-curve"
version = "0.13.8"
@@ -3759,6 +3833,22 @@ dependencies = [
"tokio-native-tls",
]
[[package]]
name = "hyper-tls"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper 1.10.1",
"hyper-util",
"native-tls",
"tokio",
"tokio-native-tls",
"tower-service",
]
[[package]]
name = "hyper-util"
version = "0.1.20"
@@ -4141,7 +4231,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d4e5d712dd664b11e778d1cfc06c79ba2700d6bc1771e44fb7b6a4656b487d"
dependencies = [
"generic-array 1.4.3",
"schemars",
"schemars 0.8.22",
"serde",
"time",
]
@@ -4265,9 +4355,9 @@ dependencies = [
[[package]]
name = "js-sys"
version = "0.3.102"
version = "0.3.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d04c30968dffe80775bd4d7fb676131cd04a1fb46d2686dbffbaec2d9dfd31"
checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102"
dependencies = [
"cfg-if",
"futures-util",
@@ -7035,7 +7125,7 @@ dependencies = [
"http 0.2.12",
"http-body 0.4.6",
"hyper 0.14.32",
"hyper-tls",
"hyper-tls 0.5.0",
"ipnet",
"js-sys",
"log",
@@ -7074,9 +7164,11 @@ dependencies = [
"http-body-util",
"hyper 1.10.1",
"hyper-rustls 0.27.9",
"hyper-tls 0.6.0",
"hyper-util",
"js-sys",
"log",
"native-tls",
"percent-encoding",
"pin-project-lite",
"quinn",
@@ -7088,6 +7180,7 @@ dependencies = [
"serde_urlencoded",
"sync_wrapper 1.0.2",
"tokio",
"tokio-native-tls",
"tokio-rustls 0.26.4",
"tower",
"tower-http 0.6.11",
@@ -7284,6 +7377,7 @@ dependencies = [
name = "revolt-database"
version = "0.13.7"
dependencies = [
"anyhow",
"async-lock 2.8.0",
"async-recursion",
"async-trait",
@@ -7325,7 +7419,7 @@ dependencies = [
"revolt_rocket_okapi",
"rocket",
"rust-argon2",
"schemars",
"schemars 0.8.22",
"serde",
"serde_json",
"sha1 0.10.6",
@@ -7370,12 +7464,13 @@ dependencies = [
"revolt-presence",
"revolt-ratelimits",
"revolt-result",
"revolt-search",
"revolt_rocket_okapi",
"rocket",
"rocket_cors",
"rocket_empty",
"rocket_prometheus",
"schemars",
"schemars 0.8.22",
"serde",
"serde_json",
"tokio",
@@ -7479,7 +7574,7 @@ dependencies = [
"revolt-permissions",
"revolt_optional_struct",
"rocket",
"schemars",
"schemars 0.8.22",
"serde",
"serde_json",
"utoipa",
@@ -7503,7 +7598,7 @@ dependencies = [
"num_enum",
"once_cell",
"revolt-result",
"schemars",
"schemars 0.8.22",
"serde",
"tokio",
]
@@ -7575,13 +7670,45 @@ dependencies = [
"revolt_okapi",
"revolt_rocket_okapi",
"rocket",
"schemars",
"schemars 0.8.22",
"sentry",
"serde",
"serde_json",
"utoipa",
]
[[package]]
name = "revolt-search"
version = "0.13.7"
dependencies = [
"elasticsearch",
"elasticsearch-dsl",
"futures",
"iso8601-timestamp",
"linkify",
"revolt-config",
"revolt-database",
"revolt-models",
"serde",
"serde_json",
"ulid",
]
[[package]]
name = "revolt-searchd"
version = "0.13.7"
dependencies = [
"anyhow",
"async-trait",
"lapin",
"log",
"revolt-config",
"revolt-database",
"revolt-search",
"serde_json",
"tokio",
]
[[package]]
name = "revolt-voice-ingress"
version = "0.13.7"
@@ -7646,7 +7773,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23bfdf7ae769c3042fe727f6e5c17363b02a64b4b33ad60c3e5f73b26df7835b"
dependencies = [
"log",
"schemars",
"schemars 0.8.22",
"serde",
"serde_json",
]
@@ -7672,7 +7799,7 @@ dependencies = [
"revolt_okapi",
"revolt_rocket_okapi_codegen",
"rocket",
"schemars",
"schemars 0.8.22",
"serde",
"serde_json",
]
@@ -8247,6 +8374,30 @@ dependencies = [
"serde_json",
]
[[package]]
name = "schemars"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd191f9397d57d581cddd31014772520aa448f65ef991055d7f61582c65165f"
dependencies = [
"dyn-clone",
"ref-cast",
"serde",
"serde_json",
]
[[package]]
name = "schemars"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2b42f36aa1cd011945615b92222f6bf73c599a102a300334cd7f8dbeec726cc"
dependencies = [
"dyn-clone",
"ref-cast",
"serde",
"serde_json",
]
[[package]]
name = "schemars_derive"
version = "0.8.22"
@@ -8661,8 +8812,18 @@ version = "3.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a5c54c7310e7b8b9577c286d7e399ddd876c3e12b3ed917a8aabc4b96e9e8c"
dependencies = [
"base64 0.22.1",
"bs58",
"chrono",
"hex",
"indexmap 1.9.3",
"indexmap 2.14.0",
"schemars 0.9.0",
"schemars 1.2.1",
"serde_core",
"serde_json",
"serde_with_macros",
"time",
]
[[package]]
@@ -9634,12 +9795,17 @@ version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840"
dependencies = [
"async-compression",
"bitflags 2.13.0",
"bytes",
"futures-core",
"futures-util",
"http 1.4.2",
"http-body 1.0.1",
"http-body-util",
"pin-project-lite",
"tokio",
"tokio-util",
"tower",
"tower-layer",
"tower-service",
@@ -10080,9 +10246,9 @@ dependencies = [
[[package]]
name = "uuid"
version = "1.23.3"
version = "1.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "144d6b123cef80b301b8f72a9e2ca4370ddec21950d0a103dd22c437006d2db7"
checksum = "bf80a72845275afea99e7f2b434723d3bc7e38470fcd1c7ed39a599c73319a53"
dependencies = [
"getrandom 0.4.3",
"js-sys",
@@ -10179,6 +10345,12 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]]
name = "vsimd"
version = "0.8.0"
@@ -10236,9 +10408,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen"
version = "0.2.125"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ddb3f79143bced6de84270411622a2699cee572fc0875aeaf1e7867cf9fca1a"
checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4"
dependencies = [
"cfg-if",
"once_cell",
@@ -10249,9 +10421,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.75"
version = "0.4.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "503b14d284f2c8dac03b819967e155ea753f573586193b2b2c95990cb5d69280"
checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d"
dependencies = [
"js-sys",
"wasm-bindgen",
@@ -10259,9 +10431,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.125"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e21a184b13fb19e157296e2c46056aec9092264fab83e4ba59e68c61b323c3d"
checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1"
dependencies = [
"quote 1.0.46",
"wasm-bindgen-macro-support",
@@ -10269,9 +10441,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.125"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fecefd9c35bd935a20fc3fc344b5f29138961e4f47fb03297d88f2587afb5ebd"
checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e"
dependencies = [
"bumpalo",
"proc-macro2",
@@ -10282,9 +10454,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.125"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23939e44bb9a5d7576fa2b563dc2e136628f1224e88a8deed09e04858b77871f"
checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24"
dependencies = [
"unicode-ident",
]
@@ -10313,9 +10485,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.102"
version = "0.3.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6430a72df5eb332242960fe84b3002a241163998241eb596d4f739b9757061d"
checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141"
dependencies = [
"js-sys",
"wasm-bindgen",

View File

@@ -164,6 +164,10 @@ livekit-api = "=0.4.23"
livekit-protocol = "=0.7.7"
livekit-runtime = "0.4.0"
# Elasticsearch
elasticsearch = "9.1.0-alpha.1"
elasticsearch-dsl = "0.4"
# Other Utilities
once_cell = "1.9.0"
config = "0.13.3"
@@ -203,3 +207,4 @@ revolt-permissions = { version = "0.13.7", path = "crates/core/permissions" }
revolt-presence = { version = "0.13.7", path = "crates/core/presence" }
revolt-ratelimits = { version = "0.13.7", path = "crates/core/ratelimits" }
revolt-result = { version = "0.13.7", path = "crates/core/result" }
revolt-search = { version = "0.13.7", path = "crates/core/search" }

View File

@@ -4,7 +4,7 @@
[database]
# MongoDB connection URL
# Defaults to the container name specified in self-hosted
mongodb = "mongodb://127.0.0.1:27017"
mongodb = "mongodb://127.0.0.1:27017?directConnection=true&replicaSet=rs0"
# Redis connection URL
# Defaults to the container name specified in self-hosted
redis = "redis://127.0.0.1:6379/"
@@ -68,3 +68,6 @@ access_key_id = "minioautumn"
secret_access_key = "minioautumn"
# Bucket to upload to by default
default_bucket = "revolt-uploads"
[elasticsearch]
api_key = ""

View File

@@ -86,3 +86,34 @@ services:
network_mode: "host"
volumes:
- ./livekit.yml:/etc/livekit.yml
elasticsearch:
image: elasticsearch:9.3.0
volumes:
- ./.data/elasticsearch:/usr/share/elasticsearch/data
ports:
- 9200:9200
extra_hosts:
- host.docker.internal:host-gateway
- model-runner.docker.internal:host-gateway
environment:
- discovery.type=single-node
- xpack.security.enabled=false
- xpack.security.http.ssl.enabled=false
- indices.id_field_data.enabled=true
healthcheck:
test:
[
"CMD-SHELL",
"curl --output /dev/null --silent --head --fail http://elasticsearch:9200",
]
interval: 10s
timeout: 10s
retries: 30
kibana:
image: kibana:9.3.1
ports:
- 5601:5601
environment:
ELASTICSEARCH_HOSTS: '["http://elasticsearch:9200"]'

View File

@@ -1,7 +1,7 @@
environment = "test"
[database]
mongodb = "mongodb://localhost"
mongodb = "mongodb://localhost?directConnection=true&replicaSet=rs0"
redis = "redis://localhost/"
[rabbit]

View File

@@ -5,7 +5,7 @@ environment = "dev"
[database]
# MongoDB connection URL
# Defaults to the container name specified in self-hosted
mongodb = "mongodb://database"
mongodb = "mongodb://database?directConnection=true&replicaSet=rs0"
# Redis connection URL
# Defaults to the container name specified in self-hosted
redis = "redis://redis/"
@@ -346,3 +346,14 @@ proxy = ""
pushd = ""
crond = ""
gifbox = ""
[elasticsearch]
host = "http://elasticsearch"
port = 9200
api_key = ""
exchange = "revolt.messages"
message_queue = "messages.message"
message_edit_queue = "messages.message_edit"
message_delete_queue = "messages.message_delete"
channel_delete_queue = "messages.channel_delete"

View File

@@ -456,6 +456,19 @@ pub struct Sentry {
pub gifbox: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Elasticsearch {
pub host: String,
pub port: u16,
pub api_key: String,
pub exchange: String,
pub message_queue: String,
pub message_edit_queue: String,
pub message_delete_queue: String,
pub channel_delete_queue: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
pub database: Database,
@@ -470,6 +483,7 @@ pub struct Settings {
pub production: bool,
pub environment: String,
pub disable_events_dont_use: bool,
pub elasticsearch: Elasticsearch,
}
impl Settings {

View File

@@ -31,7 +31,7 @@ default = ["mongodb", "tokio-runtime", "tasks"]
[dependencies]
# Core
revolt-config = { workspace = true, features = ["report-macros"] }
revolt-config = { workspace = true, features = ["report-macros", "anyhow"] }
revolt-result = { workspace = true }
revolt-models = { workspace = true, features = ["validator"] }
revolt-presence = { workspace = true }
@@ -56,6 +56,7 @@ validator = { workspace = true, features = ["derive"] }
isahc = { workspace = true, features = ["json"], optional = true }
base32 = { workspace = true }
sha1 = { workspace = true }
anyhow = { workspace = true }
# Serialisation
serde_json = { workspace = true }

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::sync::Arc;
use crate::events::rabbit::*;
use crate::{Message, events::rabbit::*};
use crate::User;
use lapin::{
options::BasicPublishOptions,
@@ -25,6 +25,10 @@ pub struct AMQP {
ack_notification_message: Arc<Channel>,
dm_call_updated: Arc<Channel>,
process_ack: Arc<Channel>,
message_search: Arc<Channel>,
edit_message_search: Arc<Channel>,
delete_message_search: Arc<Channel>,
delete_channel_search: Arc<Channel>,
#[allow(unused)]
connection: Arc<Connection>,
}
@@ -40,6 +44,10 @@ impl AMQP {
ack_notification_message: Self::create_channel(&connection).await,
dm_call_updated: Self::create_channel(&connection).await,
process_ack: Self::create_channel(&connection).await,
message_search: Self::create_channel(&connection).await,
edit_message_search: Self::create_channel(&connection).await,
delete_message_search: Self::create_channel(&connection).await,
delete_channel_search: Self::create_channel(&connection).await,
connection,
}
}
@@ -379,4 +387,112 @@ impl AMQP {
Ok(())
}
pub async fn new_message_search(
&self,
message: Message,
user: Option<User>,
) -> Result<(), AMQPError> {
let config = revolt_config::config().await;
let payload = to_string(&MessageCreatePayload { message, user }).unwrap();
debug!(
"Sending new message search payload on channel {}: {}",
config.elasticsearch.message_queue, payload
);
self.message_search
.basic_publish(
config.elasticsearch.exchange.clone().into(),
config.elasticsearch.message_queue.into(),
BasicPublishOptions::default(),
payload.as_bytes(),
AMQPProperties::default()
.with_content_type("application/json".into())
.with_delivery_mode(2),
)
.await?;
Ok(())
}
pub async fn edit_message_search(
&self,
message: Message,
user: Option<User>,
) -> Result<(), AMQPError> {
let config = revolt_config::config().await;
let payload = to_string(&MessageEditPayload { message, user }).unwrap();
debug!(
"Sending edit message search payload on channel {}: {}",
config.elasticsearch.message_edit_queue, payload
);
self.edit_message_search
.basic_publish(
config.elasticsearch.exchange.clone().into(),
config.elasticsearch.message_edit_queue.into(),
BasicPublishOptions::default(),
payload.as_bytes(),
AMQPProperties::default()
.with_content_type("application/json".into())
.with_delivery_mode(2),
)
.await?;
Ok(())
}
pub async fn delete_message_search(&self, message_id: String) -> Result<(), AMQPError> {
let config = revolt_config::config().await;
let payload = to_string(&MessageDeletePayload { message_id }).unwrap();
debug!(
"Sending delete message search payload on channel {}: {}",
config.elasticsearch.message_delete_queue, payload
);
self.delete_message_search
.basic_publish(
config.elasticsearch.exchange.clone().into(),
config.elasticsearch.message_delete_queue.into(),
BasicPublishOptions::default(),
payload.as_bytes(),
AMQPProperties::default()
.with_content_type("application/json".into())
.with_delivery_mode(2),
)
.await?;
Ok(())
}
pub async fn delete_channel_search(&self, channel_id: String) -> Result<(), AMQPError> {
let config = revolt_config::config().await;
let payload = to_string(&ChannelDeletePayload { channel_id }).unwrap();
debug!(
"Sending delete channel search payload on channel {}: {}",
config.elasticsearch.channel_delete_queue, payload
);
self.delete_channel_search
.basic_publish(
config.elasticsearch.exchange.clone().into(),
config.elasticsearch.channel_delete_queue.into(),
BasicPublishOptions::default(),
payload.as_bytes(),
AMQPProperties::default()
.with_content_type("application/json".into())
.with_delivery_mode(2),
)
.await?;
Ok(())
}
}

View File

@@ -1,7 +1,5 @@
use std::{
future::{ready, Future},
pin::Pin,
sync::Arc,
future::{Future, ready}, marker::PhantomData, pin::Pin, sync::Arc
};
use anyhow::Result;
@@ -12,14 +10,15 @@ use lapin::{
BasicProperties, Channel, Connection, ConsumerDelegate, Error as AMQPError,
};
use log::debug;
use revolt_database::Database;
use crate::Database;
#[async_trait]
pub trait Consumer: Clone + Send + Sync + 'static {
pub trait Consumer<T: Clone = ()>: Clone + Send + Sync + 'static {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
data: T,
) -> Self;
fn channel(&self) -> &Arc<Channel>;
async fn consume(&self, delivery: Delivery) -> Result<()>;
@@ -65,9 +64,15 @@ pub trait Consumer: Clone + Send + Sync + 'static {
}
}
pub struct Delegate<C: Consumer>(pub C);
pub struct Delegate<C: Consumer<D>, D: Clone>(C, PhantomData<D>);
impl<C: Consumer> ConsumerDelegate for Delegate<C> {
impl<C: Consumer<D>, D: Clone> Delegate<C, D> {
pub fn new(consumer: C) -> Self {
Self(consumer, PhantomData)
}
}
impl<C: Consumer<D>, D: Clone + Send + Sync> ConsumerDelegate for Delegate<C, D> {
fn on_new_delivery(
&self,
delivery: DeliveryResult,

View File

@@ -1,2 +1,3 @@
#[allow(clippy::module_inception)]
pub mod amqp;
pub mod consumer;

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use revolt_models::v0::PushNotification;
use serde::{Deserialize, Serialize};
use crate::User;
use crate::{Message, User};
#[derive(Serialize, Deserialize)]
pub struct MessageSentPayload {
@@ -79,6 +79,28 @@ pub struct AckPayload {
pub message_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageDeletePayload {
pub message_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelDeletePayload {
pub channel_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageCreatePayload {
pub message: Message,
pub user: Option<User>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageEditPayload {
pub message: Message,
pub user: Option<User>,
}
/// This is not the same as the AckPayload above, as the state for this event is stored in redis to allow for state updates while the event is queued.
#[derive(Serialize, Deserialize, Debug)]
pub struct AckEventPayload {

View File

@@ -109,7 +109,7 @@ pub mod events;
#[cfg(feature = "tasks")]
pub mod tasks;
mod amqp;
pub mod amqp;
pub use amqp::amqp::AMQP;
#[cfg(feature = "voice")]

View File

@@ -161,7 +161,6 @@ auto_derived!(
Icon,
DefaultPermissions,
Voice,
Slowmode,
}
);
@@ -341,7 +340,7 @@ impl Channel {
pub async fn add_user_to_group(
&mut self,
db: &Database,
amqp: &AMQP,
amqp: Option<&AMQP>,
user: &User,
by_id: &str,
) -> Result<()> {
@@ -378,7 +377,7 @@ impl Channel {
.into_message(id.to_string())
.send(
db,
Some(amqp),
amqp,
MessageAuthor::System {
username: &user.username,
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
@@ -555,12 +554,6 @@ impl Channel {
}
_ => {}
},
FieldsChannel::Slowmode => match self {
Self::TextChannel { slowmode, .. } => {
slowmode.take();
}
_ => {}
}
}
}
@@ -672,7 +665,7 @@ impl Channel {
pub async fn remove_user_from_group(
&self,
db: &Database,
amqp: &AMQP,
amqp: Option<&AMQP>,
user: &User,
by_id: Option<&str>,
silent: bool,
@@ -704,7 +697,7 @@ impl Channel {
.into_message(id.to_string())
.send(
db,
Some(amqp),
amqp,
MessageAuthor::System {
username: name,
avatar: None,
@@ -717,7 +710,7 @@ impl Channel {
.await
.ok();
} else {
return self.delete(db).await;
return self.delete(db, amqp).await;
}
}
@@ -744,7 +737,7 @@ impl Channel {
.into_message(id.to_string())
.send(
db,
Some(amqp),
amqp,
MessageAuthor::System {
username: &user.username,
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
@@ -766,13 +759,22 @@ impl Channel {
}
/// Delete a channel
pub async fn delete(&self, db: &Database) -> Result<()> {
pub async fn delete(&self, db: &Database, amqp: Option<&AMQP>) -> Result<()> {
let id = self.id().to_string();
EventV1::ChannelDelete { id: id.clone() }.p(id).await;
// TODO: missing functionality:
// - group invites
// - channels list / categories list on server
db.delete_channel(self).await
db.delete_channel(self).await?;
if let Some(amqp) = amqp {
if let Err(e) = amqp.delete_channel_search(self.id().to_string()).await {
log::error!("Error pushing message to RabbitMQ: {e}");
revolt_config::capture_error(&e);
}
}
Ok(())
}
}
@@ -784,7 +786,6 @@ impl IntoDocumentPath for FieldsChannel {
FieldsChannel::Icon => "icon",
FieldsChannel::DefaultPermissions => "default_permissions",
FieldsChannel::Voice => "voice",
FieldsChannel::Slowmode => "slowmode",
})
}
}

View File

@@ -1,6 +1,6 @@
use indexmap::{IndexMap, IndexSet};
use iso8601_timestamp::Timestamp;
use revolt_config::{config, FeaturesLimits};
use revolt_config::{capture_error, config, FeaturesLimits};
use revolt_models::v0::{
self, BulkMessageResponse, DataMessageSend, Embed, MessageAuthor, MessageFlags, MessageSort,
MessageWebhook, PushNotification, ReplyIntent, SendableEmbed, Text,
@@ -18,7 +18,7 @@ use crate::{
bulk_permissions::BulkDatabasePermissionQuery, idempotency::IdempotencyKey,
permissions::DatabasePermissionQuery,
},
Channel, Database, Emoji, File, User, AMQP,
Channel, Database, Emoji, File, Member, User, AMQP,
};
#[cfg(feature = "tasks")]
@@ -210,6 +210,13 @@ auto_derived!(
pub enum FieldsMessage {
Pinned,
}
/// Message along with the user for the author
pub struct MessageWithUser {
#[serde(flatten)]
pub message: Message,
pub user: Option<User>,
}
);
pub struct MessageFlagsValue(pub u32);
@@ -272,8 +279,8 @@ impl Message {
channel: Channel,
data: DataMessageSend,
author: MessageAuthor<'_>,
user: Option<v0::User>,
member: Option<v0::Member>,
user: Option<User>,
member: Option<Member>,
limits: FeaturesLimits,
mut idempotency: IdempotencyKey,
generate_embeds: bool,
@@ -610,10 +617,10 @@ impl Message {
/// Send a message without any notifications
pub async fn send_without_notifications(
&mut self,
&self,
db: &Database,
user: Option<v0::User>,
member: Option<v0::Member>,
user: Option<User>,
member: Option<Member>,
is_dm: bool,
generate_embeds: bool,
// This determines if this function should queue the mentions task or if somewhere else will.
@@ -623,9 +630,18 @@ impl Message {
db.insert_message(self).await?;
// Fan out events
EventV1::Message(self.clone().into_model(user, member))
.p(self.channel.to_string())
.await;
EventV1::Message(self.clone().into_model(
match user {
Some(user) => {
let is_online = revolt_presence::is_online(&user.id).await;
Some(user.into_known_static(is_online).await)
}
None => None,
},
member.map(Into::into),
))
.p(self.channel.to_string())
.await;
// Update last_message_id
#[cfg(feature = "tasks")]
@@ -671,10 +687,10 @@ impl Message {
pub async fn send(
&mut self,
db: &Database,
_amqp: Option<&AMQP>, // this is optional mostly for tests.
amqp: Option<&AMQP>, // this is optional mostly for tests.
author: MessageAuthor<'_>,
user: Option<v0::User>,
member: Option<v0::Member>,
user: Option<User>,
member: Option<Member>,
channel: &Channel,
generate_embeds: bool,
) -> Result<()> {
@@ -704,7 +720,17 @@ impl Message {
messages: vec![(
Some(
PushNotification::from(
self.clone().into_model(user, member),
self.clone().into_model(
match user.clone() {
Some(user) => {
let is_online =
revolt_presence::is_online(&user.id).await;
Some(user.into_known_static(is_online).await)
}
None => None,
},
member.map(Into::into),
),
Some(author.clone()),
channel.to_owned().into(),
)
@@ -730,6 +756,13 @@ impl Message {
.await;
}
if let Some(amqp) = amqp {
if let Err(e) = amqp.new_message_search(self.clone(), user).await {
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
}
Ok(())
}
@@ -782,6 +815,7 @@ impl Message {
pub async fn update(
&mut self,
db: &Database,
amqp: Option<&AMQP>,
partial: PartialMessage,
remove: Vec<FieldsMessage>,
) -> Result<()> {
@@ -803,9 +837,74 @@ impl Message {
.p(self.channel.clone())
.await;
if let Some(amqp) = amqp {
if let Err(e) = amqp
.edit_message_search(self.clone(), self.fetch_author(db).await)
.await
{
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
}
Ok(())
}
pub async fn fetch_users(
db: &Database,
messages: &[Message],
server_id: Option<&str>,
) -> Result<(Vec<User>, Vec<Member>)> {
let user_ids = messages
.iter()
.flat_map(|m| {
let mut users = vec![m.author.clone()];
if let Some(system) = &m.system {
match system {
SystemMessage::ChannelDescriptionChanged { by } => users.push(by.clone()),
SystemMessage::ChannelIconChanged { by } => users.push(by.clone()),
SystemMessage::ChannelOwnershipChanged { from, to, .. } => {
users.push(from.clone());
users.push(to.clone())
}
SystemMessage::ChannelRenamed { by, .. } => users.push(by.clone()),
SystemMessage::UserAdded { by, id, .. }
| SystemMessage::UserRemove { by, id, .. } => {
users.push(by.clone());
users.push(id.clone());
}
SystemMessage::UserBanned { id, .. }
| SystemMessage::UserKicked { id, .. }
| SystemMessage::UserJoined { id, .. }
| SystemMessage::UserLeft { id, .. } => {
users.push(id.clone());
}
SystemMessage::Text { .. } => {}
SystemMessage::MessagePinned { by, .. } => {
users.push(by.clone());
}
SystemMessage::MessageUnpinned { by, .. } => {
users.push(by.clone());
}
SystemMessage::CallStarted { by, .. } => users.push(by.clone()),
}
}
users
})
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<String>>();
let users = db.fetch_users(&user_ids).await?;
let members = if let Some(server_id) = server_id {
db.fetch_members(server_id, &user_ids).await?
} else {
Vec::new()
};
Ok((users, members))
}
/// Helper function to fetch many messages with users
pub async fn fetch_with_users(
db: &Database,
@@ -814,93 +913,55 @@ impl Message {
include_users: Option<bool>,
server_id: Option<&str>,
) -> Result<BulkMessageResponse> {
let messages: Vec<v0::Message> = db
.fetch_messages(query)
.await?
.into_iter()
.map(|msg| msg.into_model(None, None))
.collect();
let messages = db.fetch_messages(query).await?;
if let Some(true) = include_users {
let user_ids = messages
.iter()
.flat_map(|m| {
let mut users = vec![m.author.clone()];
if let Some(system) = &m.system {
match system {
v0::SystemMessage::ChannelDescriptionChanged { by } => {
users.push(by.clone())
}
v0::SystemMessage::ChannelIconChanged { by } => users.push(by.clone()),
v0::SystemMessage::ChannelOwnershipChanged { from, to, .. } => {
users.push(from.clone());
users.push(to.clone())
}
v0::SystemMessage::ChannelRenamed { by, .. } => users.push(by.clone()),
v0::SystemMessage::UserAdded { by, id, .. }
| v0::SystemMessage::UserRemove { by, id, .. } => {
users.push(by.clone());
users.push(id.clone());
}
v0::SystemMessage::UserBanned { id, .. }
| v0::SystemMessage::UserKicked { id, .. }
| v0::SystemMessage::UserJoined { id, .. }
| v0::SystemMessage::UserLeft { id, .. } => {
users.push(id.clone());
}
v0::SystemMessage::Text { .. } => {}
v0::SystemMessage::MessagePinned { by, .. } => {
users.push(by.clone());
}
v0::SystemMessage::MessageUnpinned { by, .. } => {
users.push(by.clone());
}
v0::SystemMessage::CallStarted { by, .. } => users.push(by.clone()),
}
}
users
})
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<String>>();
let users = User::fetch_many_ids_as_mutuals(db, perspective, &user_ids).await?;
let (users, members) = Message::fetch_users(db, &messages, server_id).await?;
Ok(BulkMessageResponse::MessagesAndUsers {
messages,
users,
members: if let Some(server_id) = server_id {
Some(
db.fetch_members(server_id, &user_ids)
.await?
.into_iter()
.map(Into::into)
.collect(),
)
} else {
None
},
messages: messages
.into_iter()
.map(|msg| msg.into_model(None, None))
.collect(),
users: User::into_mutuals(perspective, users).await,
members: Some(members.into_iter().map(Into::into).collect()),
})
} else {
Ok(BulkMessageResponse::JustMessages(messages))
Ok(BulkMessageResponse::JustMessages(
messages
.into_iter()
.map(|msg| msg.into_model(None, None))
.collect(),
))
}
}
/// Append content to message
pub async fn append(
db: &Database,
amqp: Option<&AMQP>,
id: String,
channel: String,
append: AppendMessage,
) -> Result<()> {
db.append_message(&id, &append).await?;
if let Some(message) = db.append_message(&id, &append).await? {
if let Some(amqp) = amqp {
let author = message.fetch_author(db).await;
EventV1::MessageAppend {
id,
channel: channel.to_string(),
append: append.into(),
if let Err(e) = amqp.edit_message_search(message, author).await {
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
}
EventV1::MessageAppend {
id,
channel: channel.to_string(),
append: append.into(),
}
.p(channel)
.await;
}
.p(channel)
.await;
Ok(())
}
@@ -998,9 +1059,10 @@ impl Message {
}
/// Delete a message
pub async fn delete(self, db: &Database) -> Result<()> {
pub async fn delete(&self, db: &Database, amqp: Option<&AMQP>) -> Result<()> {
let file_ids: Vec<String> = self
.attachments
.as_ref()
.map(|files| files.iter().map(|file| file.id.to_string()).collect())
.unwrap_or_default();
@@ -1010,12 +1072,20 @@ impl Message {
db.delete_message(&self.id).await?;
if let Some(amqp) = amqp {
if let Err(e) = amqp.delete_message_search(self.id.clone()).await {
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
}
EventV1::MessageDelete {
id: self.id,
id: self.id.clone(),
channel: self.channel.clone(),
}
.p(self.channel)
.p(self.channel.clone())
.await;
Ok(())
}
@@ -1116,6 +1186,14 @@ impl Message {
FieldsMessage::Pinned => self.pinned = None,
}
}
pub async fn fetch_author(&self, db: &Database) -> Option<User> {
if self.webhook.is_some() {
None
} else {
db.fetch_user(&self.author).await.ok()
}
}
}
impl SystemMessage {

View File

@@ -1,8 +1,11 @@
use revolt_result::Result;
use std::collections::HashMap;
use std::time::SystemTime;
use revolt_result::Result;
use crate::{AppendMessage, FieldsMessage, Message, MessageQuery, PartialMessage};
use crate::{
util::ChunkedDatabaseGenerator, AppendMessage, FieldsMessage, Message, MessageQuery,
MessageWithUser, PartialMessage,
};
#[cfg(feature = "mongodb")]
mod mongodb;
@@ -23,10 +26,15 @@ pub trait AbstractMessages: Sync + Send {
async fn fetch_messages_by_id(&self, ids: &[String]) -> Result<Vec<Message>>;
/// Update a given message with new information
async fn update_message(&self, id: &str, message: &PartialMessage, remove: Vec<FieldsMessage>) -> Result<()>;
async fn update_message(
&self,
id: &str,
message: &PartialMessage,
remove: Vec<FieldsMessage>,
) -> Result<()>;
/// Append information to a given message
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()>;
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<Option<Message>>;
/// Add a new reaction to a message
async fn add_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()>;
@@ -48,8 +56,11 @@ pub trait AbstractMessages: Sync + Send {
&self,
channels: &[String],
author: &str,
since: SystemTime
since: SystemTime,
) -> Result<HashMap<String, Vec<String>>>;
/// Fetches all messages along with their author from every message in decending order
async fn fetch_all_messages(&self) -> Result<ChunkedDatabaseGenerator<MessageWithUser>>;
async fn delete_messages_by_user(&self, user_id: &str) -> Result<()>;
}

View File

@@ -1,7 +1,7 @@
use bson::{to_bson, Document};
use futures::try_join;
use futures::StreamExt;
use mongodb::options::FindOptions;
use mongodb::options::{FindOptions, ReadConcern, ReturnDocument};
use revolt_models::v0::MessageSort;
use revolt_result::Result;
use std::collections::{HashMap, HashSet};
@@ -9,8 +9,8 @@ use std::time::SystemTime;
use ulid::Ulid;
use crate::{
AppendMessage, DocumentId, FieldsMessage, IntoDocumentPath, Message, MessageQuery,
MessageTimePeriod, MongoDb, PartialMessage,
util::ChunkedDatabaseGenerator, AppendMessage, DocumentId, FieldsMessage, IntoDocumentPath,
Message, MessageQuery, MessageTimePeriod, MessageWithUser, MongoDb, PartialMessage,
};
use super::AbstractMessages;
@@ -205,7 +205,7 @@ impl AbstractMessages for MongoDb {
}
/// Append information to a given message
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<Option<Message>> {
let mut query = doc! {};
if let Some(embeds) = &append.embeds {
@@ -223,18 +223,18 @@ impl AbstractMessages for MongoDb {
}
if query.is_empty() {
return Ok(());
return Ok(None);
}
self.col::<Document>(COL)
.update_one(
self.col::<Message>(COL)
.find_one_and_update(
doc! {
"_id": id
},
query,
)
.return_document(ReturnDocument::After)
.await
.map(|_| ())
.map_err(|_| create_database_error!("update_one", COL))
}
@@ -417,6 +417,53 @@ impl AbstractMessages for MongoDb {
Ok(deleted_messages)
}
/// Fetches all messages along with their author from every message in decending order
async fn fetch_all_messages(&self) -> Result<ChunkedDatabaseGenerator<MessageWithUser>> {
let mut session = self
.start_session()
.await
.map_err(|_| create_database_error!("start_session", COL))?;
session
.start_transaction()
.read_concern(ReadConcern::snapshot())
.await
.map_err(|_| create_database_error!("start_transaction", COL))?;
let cursor = self
.col::<Message>(COL)
.aggregate([
doc! {
"$lookup": {
"from": "users",
"localField": "author",
"foreignField": "_id",
"as": "user"
}
},
doc! {
"$set": {
"user": {
"$first": "$user"
}
}
},
doc! {
"$sort": {
"_id": -1
}
},
])
.with_type::<MessageWithUser>()
.session(&mut session)
.batch_size(1000)
.await
.inspect_err(|e| log::error!("{e}"))
.map_err(|_| create_database_error!("aggregate", COL))?;
Ok(ChunkedDatabaseGenerator::new_mongo(session, cursor))
}
async fn delete_messages_by_user(&self, user_id: &str) -> Result<()> {
self.delete_bulk_messages(doc! {
"author": user_id,

View File

@@ -1,6 +1,6 @@
use crate::{
AppendMessage, FieldsMessage, Message, MessageQuery,
PartialMessage, ReferenceDb,
util::ChunkedDatabaseGenerator, AppendMessage, FieldsMessage, Message, MessageQuery,
MessageWithUser, PartialMessage, ReferenceDb,
};
use futures::future::try_join_all;
use indexmap::IndexSet;
@@ -215,7 +215,7 @@ impl AbstractMessages for ReferenceDb {
}
/// Append information to a given message
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<Option<Message>> {
let mut messages = self.messages.lock().await;
if let Some(message_data) = messages.get_mut(id) {
if let Some(embeds) = &append.embeds {
@@ -226,9 +226,11 @@ impl AbstractMessages for ReferenceDb {
message_data.embeds = Some(embeds.clone());
}
}
}
Ok(())
Ok(Some(message_data.clone()))
} else {
Ok(None)
}
} else {
Err(create_error!(NotFound))
}
@@ -353,6 +355,24 @@ impl AbstractMessages for ReferenceDb {
Ok(deleted_messages)
}
/// Fetches all messages along with their author from every message in decending order
async fn fetch_all_messages(&self) -> Result<ChunkedDatabaseGenerator<MessageWithUser>> {
let users = self.users.lock().await;
Ok(ChunkedDatabaseGenerator::new_reference(
self.messages
.lock()
.await
.values()
.cloned()
.map(|message| MessageWithUser {
user: users.get(&message.author).cloned(),
message,
})
.collect(),
))
}
async fn delete_messages_by_user(&self, user_id: &str) -> Result<()> {
let mut messages = self.messages.lock().await;

View File

@@ -1,11 +1,12 @@
use std::collections::{HashMap, HashSet};
use revolt_config::capture_error;
use revolt_models::v0::{self, DataCreateServerChannel};
use revolt_permissions::{OverrideField, DEFAULT_PERMISSION_SERVER};
use revolt_result::Result;
use ulid::Ulid;
use crate::{events::client::EventV1, Channel, Database, File, User};
use crate::{AMQP, Channel, Database, File, User, events::client::EventV1};
auto_derived_partial!(
/// Server
@@ -214,7 +215,16 @@ impl Server {
}
/// Delete a server
pub async fn delete(self, db: &Database) -> Result<()> {
pub async fn delete(self, db: &Database, amqp: Option<&AMQP>) -> Result<()> {
if let Some(amqp) = amqp {
for channel_id in self.channels {
if let Err(e) = amqp.delete_channel_search(channel_id).await {
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
}
}
EventV1::ServerDelete {
id: self.id.clone(),
}

View File

@@ -381,6 +381,17 @@ impl User {
)
}
pub async fn into_mutuals(perspective: &User, users: Vec<User>) -> Vec<v0::User> {
let online_ids =
filter_online(&users.iter().map(|user| user.id.clone()).collect::<Vec<_>>()).await;
join_all(users.into_iter().map(|user| async {
let is_online = online_ids.contains(&user.id);
user.into_known(perspective, is_online).await
}))
.await
}
/// Find a free discriminator for a given username
pub async fn find_discriminator(
db: &Database,
@@ -531,7 +542,7 @@ impl User {
pub async fn add_friend(
&mut self,
db: &Database,
amqp: &AMQP,
amqp: Option<&AMQP>,
target: &mut User,
) -> Result<()> {
match self.relationship_with(&target.id) {
@@ -541,8 +552,10 @@ impl User {
RelationshipStatus::Blocked => Err(create_error!(Blocked)),
RelationshipStatus::BlockedOther => Err(create_error!(BlockedByOther)),
RelationshipStatus::Incoming => {
// Accept incoming friend request
_ = amqp.friend_request_accepted(self, target).await;
if let Some(amqp) = amqp {
// Accept incoming friend request
_ = amqp.friend_request_accepted(self, target).await;
};
self.apply_relationship(
db,
@@ -572,7 +585,9 @@ impl User {
}));
}
_ = amqp.friend_request_received(target, self).await;
if let Some(amqp) = amqp {
_ = amqp.friend_request_received(target, self).await;
};
// Send the friend request
self.apply_relationship(
@@ -867,13 +882,13 @@ impl User {
/// - deletes owned bots, servers and messages
/// - removes user from all groups
/// - clears relationships
pub async fn delete(&mut self, db: &Database) -> Result<()> {
pub async fn delete(&mut self, db: &Database, amqp: Option<&AMQP>) -> Result<()> {
for bot in db.fetch_bots_by_user(&self.id).await? {
bot.delete(db).await?;
}
for server in db.fetch_owned_servers(&self.id).await? {
server.delete(db).await?;
server.delete(db, amqp).await?;
}
self.remove_from_all_groups(db).await?;

View File

@@ -16,7 +16,7 @@ pub fn start_workers(db: Database, amqp: AMQP) {
for _ in 0..WORKER_COUNT {
task::spawn(ack::worker(db.clone(), amqp.clone()));
task::spawn(last_message_id::worker(db.clone()));
task::spawn(process_embeds::worker(db.clone()));
task::spawn(process_embeds::worker(db.clone(), amqp.clone()));
}
}

View File

@@ -1,4 +1,4 @@
use crate::{models::Message, AppendMessage, Database};
use crate::{AMQP, AppendMessage, Database, models::Message};
use futures::future::join_all;
use linkify::{LinkFinder, LinkKind};
@@ -41,7 +41,7 @@ pub async fn queue(channel: String, id: String, content: String) {
}
/// Start a new worker
pub async fn worker(db: Database) {
pub async fn worker(db: Database, amqp: AMQP) {
let semaphore = Arc::new(Semaphore::new(
config().await.api.workers.max_concurrent_connections,
));
@@ -49,6 +49,7 @@ pub async fn worker(db: Database) {
loop {
let task = Q.pop().await;
let db = db.clone();
let amqp = amqp.clone();
let semaphore = semaphore.clone();
spawn(async move {
@@ -64,6 +65,7 @@ pub async fn worker(db: Database) {
if let Ok(embeds) = embeds {
if let Err(err) = Message::append(
&db,
Some(&amqp),
task.id,
task.channel,
AppendMessage {

View File

@@ -319,7 +319,6 @@ impl From<FieldsChannel> for crate::FieldsChannel {
FieldsChannel::Icon => crate::FieldsChannel::Icon,
FieldsChannel::DefaultPermissions => crate::FieldsChannel::DefaultPermissions,
FieldsChannel::Voice => crate::FieldsChannel::Voice,
FieldsChannel::Slowmode => crate::FieldsChannel::Slowmode,
}
}
}
@@ -331,7 +330,6 @@ impl From<crate::FieldsChannel> for FieldsChannel {
crate::FieldsChannel::Icon => FieldsChannel::Icon,
crate::FieldsChannel::DefaultPermissions => FieldsChannel::DefaultPermissions,
crate::FieldsChannel::Voice => FieldsChannel::Voice,
crate::FieldsChannel::Slowmode => FieldsChannel::Slowmode,
}
}
}

View File

@@ -13,5 +13,5 @@ pub mod reference;
pub mod shield;
pub mod test_fixtures;
pub use chunked::ChunkedDatabaseGenerator;
pub use funcs::*;
pub use chunked::ChunkedDatabaseGenerator;

View File

@@ -159,17 +159,4 @@ impl VoiceClient {
.await
.to_internal_error()
}
pub async fn get_room_participants(
&self,
node: &str,
channel_id: &str,
) -> Result<Vec<ParticipantInfo>> {
let room = self.get_node(node)?;
room.client
.list_participants(channel_id)
.await
.to_internal_error()
}
}

View File

@@ -164,7 +164,6 @@ auto_derived!(
Icon,
DefaultPermissions,
Voice,
Slowmode,
}
/// New webhook information

View File

@@ -14,6 +14,7 @@ mod server_members;
mod servers;
mod user_settings;
mod users;
mod search;
mod accounts;
mod mfa_tickets;
mod sessions;
@@ -34,6 +35,7 @@ pub use server_members::*;
pub use servers::*;
pub use user_settings::*;
pub use users::*;
pub use search::*;
pub use accounts::*;
pub use mfa_tickets::*;
pub use sessions::*;
pub use sessions::*;

View File

@@ -0,0 +1,74 @@
use iso8601_timestamp::Timestamp;
use std::collections::HashSet;
auto_derived!(
/// Options for searching messages in a server or channel
pub struct DataChannelMessagesSearch {
/// Channel to search in
pub channel: Option<String>,
/// Server to search in
pub server: Option<String>,
/// Filter options
pub filters: Option<DataChannelMessagesSearchFilters>,
/// What index to start the search at
pub offset: Option<u64>,
/// Max amount of messages to return
pub limit: Option<u64>,
/// Sort order
pub sort: Option<SortOrder>,
}
/// Message search filters
pub struct DataChannelMessagesSearchFilters {
/// Message content
pub content: Option<String>,
/// Specific user
pub author: Option<HashSet<String>>,
/// Mentions a user
pub mentions: Option<HashSet<String>>,
/// Mentions a role
pub role_mentions: Option<HashSet<String>>,
/// Send before a specific date
pub before_date: Option<Timestamp>,
/// Sent after a specific date
pub after_date: Option<Timestamp>,
/// What type of user sent the message
pub author_type: Option<HashSet<AuthorType>>,
/// Whether the message is pinned or not
pub pinned: Option<bool>,
/// Require message to have a specific component type
pub components: Option<HashSet<MessageComponent>>,
}
/// Message author type
#[derive(Copy, Hash)]
pub enum AuthorType {
User,
Bot,
Webhook,
}
/// Message component
#[derive(Copy, Hash)]
pub enum MessageComponent {
Image,
Video,
Link,
File,
Embed,
}
/// Message sort order
#[derive(Copy, Default)]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum SortOrder {
Asc,
#[default]
Desc,
}
);

View File

@@ -0,0 +1,19 @@
[package]
name = "revolt-search"
version = "0.13.7"
edition = "2024"
license = "MIT"
publish = false
[dependencies]
revolt-config = { workspace = true }
revolt-database = { workspace = true }
revolt-models = { workspace = true }
elasticsearch = { workspace = true }
serde_json = { workspace = true }
ulid = { workspace = true }
iso8601-timestamp = { workspace = true, features = ["serde", "bson"] }
futures = { workspace = true }
elasticsearch-dsl = { workspace = true }
serde = { workspace = true }
linkify = { workspace = true }

View File

@@ -0,0 +1,413 @@
use std::fmt::Display;
use elasticsearch::{
BulkOperation, BulkParts, CreateParts, DeleteByQueryParts, DeleteParts, Elasticsearch,
IndexParts, SearchParts,
auth::Credentials,
http::{
response::Exception,
transport::{SingleNodeConnectionPool, TransportBuilder},
},
indices::{IndicesCreateParts, IndicesDeleteParts},
};
use elasticsearch_dsl::{FieldSort, Query, Search, SearchResponse, Sort};
use linkify::{LinkFinder, LinkKind};
use revolt_database::{Database, Message, MessageWithUser, User};
use serde_json::{Map, Value, json, to_value};
pub use elasticsearch;
use crate::{AuthorType, MessageComponent, SearchTerms};
/// Elasticsearch errors
#[derive(Debug)]
pub enum Error {
Http(elasticsearch::Error),
Exception(Exception),
}
impl From<elasticsearch::Error> for Error {
fn from(value: elasticsearch::Error) -> Self {
Self::Http(value)
}
}
impl From<Exception> for Error {
fn from(value: Exception) -> Self {
Self::Exception(value)
}
}
impl std::error::Error for Error {}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Http(error) => write!(f, "Http error: {error}"),
Error::Exception(exception) => write!(f, "Elasticsearch error: {exception:?}"),
}
}
}
/// Higher level elasticsearch API more fit for our specific usecase
#[derive(Debug, Clone)]
pub struct ElasticsearchClient {
pub inner: Elasticsearch,
}
impl ElasticsearchClient {
pub fn new(host: &str, port: u16, key: String) -> Self {
let pool =
SingleNodeConnectionPool::new(format!("{host}:{port}").as_str().try_into().unwrap());
let transport = TransportBuilder::new(pool)
.auth(Credentials::EncodedApiKey(key))
.build()
.unwrap();
let inner = Elasticsearch::new(transport);
Self { inner }
}
/// Delete messages index along with all documents
pub async fn delete_indexes(&self) -> Result<(), Error> {
let exception = self
.inner
.indices()
.delete(IndicesDeleteParts::Index(&["messages"]))
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Create the messages index
pub async fn setup_indexes(&self) -> Result<(), Error> {
let exception = self
.inner
.indices()
.create(IndicesCreateParts::Index("messages"))
.body(json!({
"mappings": {
"properties": {
"content": {"type": "text"},
"author": {"type": "keyword"},
"author_type": {"type": "keyword"},
"channel": {"type": "keyword"},
"mentions": {"type": "keyword"},
"role_mentions": {"type": "keyword"},
"pinned": {"type": "boolean"},
"embeds": {
"properties": {}
},
"attachments": {
"type": "nested",
"dynamic": false,
"properties": {
"metadata.type": {
"type": "keyword"
}
}
},
"has_link": { "type": "boolean" },
}
}
}))
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Performs a search for messages, returns a vec of message ids
pub async fn search(&self, terms: SearchTerms) -> Result<Vec<String>, Error> {
let mut query = Query::bool().filter(Query::terms("channel", terms.channels));
if let Some(content) = terms.filters.content {
query = query.filter(Query::r#match("content", content))
}
if let Some(author) = terms.filters.author {
query = query.filter(Query::terms("author", author))
}
if let Some(mentions) = terms.filters.mentions {
query = query.filter(Query::terms("mentions", mentions))
}
if let Some(author_type) = terms.filters.author_type {
query = query.filter(Query::terms("author_type", author_type))
}
if let Some(pinned) = terms.filters.pinned {
if pinned {
query = query.filter(Query::exists("pinned"))
} else {
query = query.filter(Query::bool().must_not(Query::exists("pinned")))
}
}
if let Some(components) = terms.filters.components {
let mut components_query = Query::bool();
let mut attachments_query = Query::bool();
for component in components {
match component {
MessageComponent::Image => {
attachments_query = attachments_query
.should(Query::term("attachments.metadata.type", "Image"))
}
MessageComponent::Video => {
attachments_query = attachments_query
.should(Query::term("attachments.metadata.type", "Video"))
}
MessageComponent::Link => {
components_query = components_query.should(Query::exists("has_link"))
}
MessageComponent::File => {
attachments_query = attachments_query.should(Query::exists("attachments"))
}
MessageComponent::Embed => {
components_query = components_query.should(Query::exists("embeds"))
}
};
}
query = query
.filter(components_query.should(Query::nested("attachments", attachments_query)));
}
let search = Search::new()
.query(query)
.stats(false)
.sort(Sort::FieldSort(
FieldSort::new("_id".to_string()).order(terms.sort.unwrap_or_default().into()),
));
let response = self
.inner
.search(SearchParts::Index(&["messages"]))
.stored_fields(&[])
.body(search)
.size(terms.limit.unwrap_or(100) as i64)
.from(terms.offset.unwrap_or(0) as i64)
.send()
.await?;
if response.status_code().is_success() {
let messages = response.json::<SearchResponse>().await?;
Ok(messages.hits.hits.into_iter().map(|hit| hit.id).collect())
} else {
Err(response
.exception()
.await?
.expect("No exception with error response.")
.into())
}
}
/// Creates a source for a message which can be stored and indexed into elasticsearch
fn create_message_source(
&self,
_db: &Database,
message: Message,
author: Option<User>,
) -> Value {
let mut map = Map::new();
map.insert("channel".to_string(), Value::String(message.channel));
map.insert("author".to_string(), Value::String(message.author));
if let Some(content) = message.content {
// Is there a better way to handle this? can elasticsearch index links itself?
// Maybe in the future store the domains and be able to filter by that as well
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Url]);
if finder.links(&content).next().is_some() {
map.insert("has_link".to_string(), Value::Bool(true));
}
map.insert("content".to_string(), Value::String(content));
}
if let Some(attachments) = message.attachments {
// TODO: fetch the file metadata from FileHash because of File.metadata deprecation
// let metadata = attachment.as_hash(db).await.expect("Failed to fetch FileHash").metadata;
map.insert(
"attachments".to_string(),
serde_json::to_value(attachments).unwrap(),
);
}
if let Some(embeds) = message.embeds {
map.insert("embeds".to_string(), serde_json::to_value(embeds).unwrap());
}
if let Some(mentions) = message.mentions {
map.insert(
"mentions".to_string(),
serde_json::to_value(mentions).unwrap(),
);
}
if let Some(role_mentions) = message.role_mentions {
map.insert(
"role_mentions".to_string(),
serde_json::to_value(role_mentions).unwrap(),
);
}
if let Some(pinned) = message.pinned {
map.insert("pinned".to_string(), Value::Bool(pinned));
}
// This will turn bot author type to user author type if this is ran on a deleted message,
// due to the author not existing anymore so fetching will fail, this is probably niche enough
// to not really matter, might try fix in the future.
map.insert(
"author_type".to_string(),
to_value(if message.webhook.is_some() {
AuthorType::Webhook
} else if author.is_some_and(|user| user.bot.is_some()) {
AuthorType::Bot
} else {
AuthorType::User
})
.unwrap(),
);
Value::Object(map)
}
/// Bulk uploads and indexes messages to elasticsearch
pub async fn bulk_index_messages(
&self,
db: &Database,
messages: Vec<MessageWithUser>,
) -> Result<(), Error> {
let mut ops = Vec::<BulkOperation<Value>>::new();
for message in messages {
let id = message.message.id.clone();
let source = self.create_message_source(db, message.message, message.user);
ops.push(BulkOperation::create(source).id(id).into());
}
let exception = self
.inner
.bulk(BulkParts::Index("messages"))
.body(ops)
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Uploads and indexes a single message to elasticsearch
pub async fn index_message(
&self,
db: &Database,
message: Message,
author: Option<User>,
) -> Result<(), Error> {
let id = message.id.clone();
let source = self.create_message_source(db, message, author);
let exception = self
.inner
.create(CreateParts::IndexId("messages", &id))
.body(source)
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Updates or upserts an existing message to elasticsearch
pub async fn edit_message(
&self,
db: &Database,
message: Message,
author: Option<User>,
) -> Result<(), Error> {
let id = message.id.clone();
let source = self.create_message_source(db, message, author);
let exception = self
.inner
.index(IndexParts::IndexId("messages", &id))
.body(source)
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Deletes a message from elasticsearch
pub async fn delete_message(&self, message_id: &str) -> Result<(), Error> {
let exception = self
.inner
.delete(DeleteParts::IndexId("messages", message_id))
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
/// Deletes all messages in a channel from elasticsearch
pub async fn delete_channel(&self, channel_id: &str) -> Result<(), Error> {
let exception = self
.inner
.delete_by_query(DeleteByQueryParts::Index(&["messages"]))
.body(Search::new().query(Query::term("channel", channel_id)))
.send()
.await?
.exception()
.await?;
if let Some(exception) = exception {
Err(exception.into())
} else {
Ok(())
}
}
}

View File

@@ -0,0 +1,5 @@
mod client;
mod types;
pub use client::*;
pub use types::*;

View File

@@ -0,0 +1,134 @@
use std::collections::HashSet;
use iso8601_timestamp::Timestamp;
use revolt_models::v0;
use serde::Serialize;
/// Message author type
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Hash)]
pub enum AuthorType {
User,
Bot,
Webhook,
}
/// Message component
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MessageComponent {
Image,
Video,
Link,
File,
Embed,
}
/// Message search filters
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SearchFilters {
/// Message content
pub content: Option<String>,
/// Specific user
pub author: Option<HashSet<String>>,
/// Mentions a user
pub mentions: Option<HashSet<String>>,
/// Mentions a role
pub role_mentions: Option<HashSet<String>>,
/// Send before a specific date
pub before_date: Option<Timestamp>,
/// Sent after a specific date
pub after_date: Option<Timestamp>,
/// What type of user sent the message
pub author_type: Option<HashSet<AuthorType>>,
/// Whether the message is pinned or not
pub pinned: Option<bool>,
/// Require message to have a specific component type
pub components: Option<HashSet<MessageComponent>>,
}
/// Message sort order
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SortOrder {
Asc,
#[default]
Desc,
}
/// Options for searching messages in a server or channel
#[derive(Debug, Clone, PartialEq)]
pub struct SearchTerms {
/// Channels to search in
pub channels: Vec<String>,
/// Filter options
pub filters: SearchFilters,
/// What index to start the search at
pub offset: Option<u64>,
/// Max amount of messages to return
pub limit: Option<u64>,
/// Sort order
pub sort: Option<SortOrder>,
}
impl From<v0::AuthorType> for AuthorType {
fn from(value: v0::AuthorType) -> Self {
match value {
v0::AuthorType::User => AuthorType::User,
v0::AuthorType::Bot => AuthorType::Bot,
v0::AuthorType::Webhook => AuthorType::Webhook,
}
}
}
impl From<v0::MessageComponent> for MessageComponent {
fn from(value: v0::MessageComponent) -> Self {
match value {
v0::MessageComponent::Image => MessageComponent::Image,
v0::MessageComponent::Video => MessageComponent::Video,
v0::MessageComponent::Link => MessageComponent::Link,
v0::MessageComponent::File => MessageComponent::File,
v0::MessageComponent::Embed => MessageComponent::Embed,
}
}
}
impl From<v0::SortOrder> for SortOrder {
fn from(value: v0::SortOrder) -> Self {
match value {
v0::SortOrder::Asc => SortOrder::Asc,
v0::SortOrder::Desc => SortOrder::Desc,
}
}
}
impl From<v0::DataChannelMessagesSearchFilters> for SearchFilters {
fn from(value: v0::DataChannelMessagesSearchFilters) -> Self {
Self {
content: value.content,
author: value.author,
mentions: value.mentions,
role_mentions: value.role_mentions,
before_date: value.before_date,
after_date: value.after_date,
author_type: value
.author_type
.map(|types| types.into_iter().map(Into::into).collect()),
pinned: value.pinned,
components: value
.components
.map(|types| types.into_iter().map(Into::into).collect()),
}
}
}
impl From<SortOrder> for elasticsearch_dsl::SortOrder {
fn from(value: SortOrder) -> Self {
match value {
SortOrder::Asc => elasticsearch_dsl::SortOrder::Asc,
SortOrder::Desc => elasticsearch_dsl::SortOrder::Desc,
}
}
}

View File

@@ -1,10 +1,10 @@
use std::time::Duration;
use revolt_database::Database;
use revolt_database::{Database, AMQP};
use revolt_result::Result;
use tokio::time::sleep;
pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
pub async fn task(db: Database, amqp: AMQP) -> Result<()> {
loop {
let accounts = db.fetch_accounts_due_for_deletion().await?;
let count = accounts.len();
@@ -12,7 +12,7 @@ pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
for mut account in accounts {
let mut user = db.fetch_user(&account.id).await?;
user.delete(&db).await?;
user.delete(&db, Some(&amqp)).await?;
account.mark_deleted(&db).await?;
}

View File

@@ -1,10 +1,9 @@
use std::sync::Arc;
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -16,11 +15,7 @@ pub struct AckConsumer {
#[async_trait]
impl Consumer for AckConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,11 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use log::debug;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -17,11 +16,7 @@ pub struct DmCallConsumer {
#[async_trait]
impl Consumer for DmCallConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,11 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use log::debug;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -17,11 +16,7 @@ pub struct FRAcceptedConsumer {
#[async_trait]
impl Consumer for FRAcceptedConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,11 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use log::debug;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -17,11 +16,7 @@ pub struct FRReceivedConsumer {
#[async_trait]
impl Consumer for FRReceivedConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,11 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use log::debug;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -17,11 +16,7 @@ pub struct GenericConsumer {
#[async_trait]
impl Consumer for GenericConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -4,13 +4,13 @@ use std::{
sync::Arc,
};
use crate::utils::{render_notification_content, Consumer};
use crate::utils::render_notification_content;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use revolt_database::{
events::rabbit::*, util::bulk_permissions::BulkDatabasePermissionQuery, Database, Member,
MessageFlagsValue,
amqp::consumer::Consumer, events::rabbit::*,
util::bulk_permissions::BulkDatabasePermissionQuery, Database, Member, MessageFlagsValue,
};
use revolt_models::v0::{MessageFlags, PushNotification};
use revolt_result::ToRevoltError;
@@ -29,11 +29,7 @@ impl MassMessageConsumer {
push: &PushNotification,
users: &[String],
) -> Result<()> {
if let Ok(sessions) = self
.db
.fetch_sessions_with_subscription(users)
.await
{
if let Ok(sessions) = self.db.fetch_sessions_with_subscription(users).await {
let config = revolt_config::config().await;
for session in sessions {
if let Some(sub) = session.subscription {
@@ -72,11 +68,7 @@ impl MassMessageConsumer {
#[async_trait]
impl Consumer for MassMessageConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,11 +1,11 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::{render_notification_content, Consumer};
use crate::utils::render_notification_content;
use anyhow::Result;
use async_trait::async_trait;
use lapin::{message::Delivery, Channel, Connection};
use log::debug;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
#[derive(Clone)]
#[allow(unused)]
@@ -17,11 +17,7 @@ pub struct MessageConsumer {
#[async_trait]
impl Consumer for MessageConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
Self {
db,
connection,

View File

@@ -1,6 +1,5 @@
use std::{borrow::Cow, collections::BTreeMap, io::Cursor, sync::Arc};
use crate::utils::Consumer;
use anyhow::Result;
use async_trait::async_trait;
use base64::{
@@ -15,7 +14,7 @@ use revolt_a2::{
},
Client, ClientConfig, Endpoint, Error, ErrorBody, ErrorReason, Priority, PushType, Response,
};
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
use revolt_models::v0::{Channel, Message, PushNotification};
use serde::Serialize;
@@ -123,11 +122,7 @@ impl ApnsOutboundConsumer {
#[async_trait]
impl Consumer for ApnsOutboundConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<AMQPChannel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
let config = revolt_config::config().await;
if config.pushd.apn.pkcs8.is_empty()

View File

@@ -1,6 +1,5 @@
use std::{collections::HashMap, sync::Arc, time::Duration};
use crate::utils::Consumer;
use anyhow::{bail, Result};
use async_trait::async_trait;
use fcm_v1::{
@@ -10,7 +9,7 @@ use fcm_v1::{
};
use lapin::{message::Delivery, Channel as AMQPChannel, Connection};
use revolt_config::config;
use revolt_database::{events::rabbit::*, Database};
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
use serde_json::Value;
/// Custom notification data
@@ -126,11 +125,7 @@ pub struct FcmOutboundConsumer {
#[async_trait]
impl Consumer for FcmOutboundConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<AMQPChannel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
let config = revolt_config::config().await;
Self {

View File

@@ -1,7 +1,5 @@
use std::{collections::HashMap, sync::Arc};
use crate::utils::Consumer;
use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
use base64::{
@@ -9,7 +7,9 @@ use base64::{
Engine as _,
};
use lapin::{message::Delivery, Channel as AMQPChannel, Connection};
use revolt_database::{events::rabbit::*, util::format_display_name, Database};
use revolt_database::{
amqp::consumer::Consumer, events::rabbit::*, util::format_display_name, Database,
};
use web_push::{
ContentEncoding, IsahcWebPushClient, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder,
WebPushClient, WebPushError, WebPushMessageBuilder,
@@ -27,11 +27,7 @@ pub struct VapidOutboundConsumer {
#[async_trait]
impl Consumer for VapidOutboundConsumer {
async fn create(
db: Database,
connection: Arc<Connection>,
channel: Arc<AMQPChannel>,
) -> Self {
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
let config = revolt_config::config().await;
if config.pushd.vapid.private_key.is_empty() || config.pushd.vapid.public_key.is_empty() {

View File

@@ -9,7 +9,7 @@ use lapin::{
Channel, Connection, ConnectionProperties,
};
use revolt_config::{config, Settings};
use revolt_database::Database;
use revolt_database::{Database, amqp::consumer::{Consumer, Delegate}};
use tokio::signal::ctrl_c;
mod consumers;
@@ -23,8 +23,6 @@ use consumers::{
outbound::{apn::ApnsOutboundConsumer, fcm::FcmOutboundConsumer, vapid::VapidOutboundConsumer},
};
use crate::utils::{Consumer, Delegate};
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() {
// Configure logging and environment
@@ -275,11 +273,12 @@ where
consumer.tag()
);
let delegate = Delegate(
let delegate = Delegate::new(
F::create(
db.clone(),
connection.clone(),
channel.clone(),
()
)
.await,
);

View File

@@ -1,5 +1,3 @@
mod renderer;
mod consumer;
pub use renderer::render_notification_content;
pub use consumer::{Consumer, Delegate};
pub use renderer::render_notification_content;

View File

@@ -0,0 +1,17 @@
[package]
name = "revolt-searchd"
version = "0.13.7"
edition = "2024"
license = "AGPL-3.0-or-later"
publish = false
[dependencies]
tokio = { workspace = true, features = ["macros"] }
revolt-database = { workspace = true }
revolt-config = { workspace = true }
revolt-search = { workspace = true }
lapin = { workspace = true }
async-trait = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
anyhow = { workspace = true }

View File

@@ -0,0 +1,59 @@
use std::sync::Arc;
use lapin::{Channel, Connection, message::Delivery, options::{BasicAckOptions, BasicRejectOptions}};
use async_trait::async_trait;
use revolt_database::{Database, events::rabbit::ChannelDeletePayload, amqp::consumer::Consumer};
use revolt_search::ElasticsearchClient;
use anyhow::Result;
#[allow(unused)]
#[derive(Clone)]
pub struct ChannelDeleteConsumer {
client: ElasticsearchClient,
database: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
}
#[async_trait]
impl Consumer<ElasticsearchClient> for ChannelDeleteConsumer {
async fn create(database: Database, connection: Arc<Connection>, channel: Arc<Channel>, client: ElasticsearchClient) -> Self {
Self {
client,
database,
connection,
channel,
}
}
fn channel(&self) -> &Arc<Channel> {
&self.channel
}
async fn consume(&self, delivery: Delivery) -> Result<()> {
let payload = serde_json::from_slice::<ChannelDeletePayload>(&delivery.data)
.expect("Failed to decode message");
log::debug!("Received channel delete {payload:?}");
if self
.client
.delete_channel(&payload.channel_id)
.await
.is_ok()
{
self.channel
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
.await
.expect("Failed to ack");
} else {
self.channel
.basic_reject(delivery.delivery_tag, BasicRejectOptions {
requeue: true,
})
.await
.expect("Failed to reject");
};
Ok(())
}
}

View File

@@ -0,0 +1,60 @@
use std::sync::Arc;
use lapin::{Channel, Connection, message::Delivery, options::{BasicAckOptions, BasicRejectOptions}};
use async_trait::async_trait;
use revolt_database::{Database, events::rabbit::MessageCreatePayload, amqp::consumer::Consumer};
use revolt_search::ElasticsearchClient;
use anyhow::Result;
#[allow(unused)]
#[derive(Clone)]
pub struct MessageConsumer {
client: ElasticsearchClient,
database: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
}
#[async_trait]
impl Consumer<ElasticsearchClient> for MessageConsumer {
async fn create(database: Database, connection: Arc<Connection>, channel: Arc<Channel>, client: ElasticsearchClient) -> Self {
Self {
client,
database,
connection,
channel,
}
}
fn channel(&self) -> &Arc<Channel> {
&self.channel
}
async fn consume(&self, delivery: Delivery) -> Result<()> {
let payload = serde_json::from_slice::<MessageCreatePayload>(&delivery.data)
.expect("Failed to decode message");
log::debug!("Received message {payload:?}");
if self
.client
.index_message(&self.database, payload.message, payload.user)
.await
.is_ok()
{
self.channel
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
.await
.expect("Failed to ack");
} else {
self.channel
.basic_reject(delivery.delivery_tag, BasicRejectOptions {
requeue: true,
})
.await
.expect("Failed to reject");
};
Ok(())
}
}

View File

@@ -0,0 +1,61 @@
use std::sync::Arc;
use lapin::{Channel, Connection, message::Delivery, options::{BasicAckOptions, BasicRejectOptions}};
use async_trait::async_trait;
use revolt_database::{Database, events::rabbit::MessageDeletePayload, amqp::consumer::Consumer};
use revolt_search::ElasticsearchClient;
use anyhow::Result;
#[allow(unused)]
#[derive(Clone)]
pub struct MessageDeleteConsumer {
client: ElasticsearchClient,
database: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
}
#[async_trait]
impl Consumer<ElasticsearchClient> for MessageDeleteConsumer {
async fn create(database: Database, connection: Arc<Connection>, channel: Arc<Channel>, client: ElasticsearchClient) -> Self {
Self {
client,
database,
connection,
channel,
}
}
fn channel(&self) -> &Arc<Channel> {
&self.channel
}
async fn consume(&self, delivery: Delivery) -> Result<()> {
let payload = serde_json::from_slice::<MessageDeletePayload>(&delivery.data)
.expect("Failed to decode message");
log::debug!("Received message delete {payload:?}");
if self
.client
.delete_message(&payload.message_id)
.await
.is_ok()
{
self.channel
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
.await
.expect("Failed to ack");
} else {
self.channel
.basic_reject(delivery.delivery_tag, BasicRejectOptions {
requeue: true,
})
.await
.expect("Failed to reject");
};
Ok(())
}
}

View File

@@ -0,0 +1,60 @@
use std::sync::Arc;
use lapin::{Channel, Connection, message::Delivery, options::{BasicAckOptions, BasicRejectOptions}};
use async_trait::async_trait;
use revolt_database::{Database, events::rabbit::MessageEditPayload, amqp::consumer::Consumer};
use revolt_search::ElasticsearchClient;
use anyhow::Result;
#[derive(Clone)]
pub struct MessageEditConsumer {
client: ElasticsearchClient,
database: Database,
connection: Arc<Connection>,
channel: Arc<Channel>,
}
#[async_trait]
impl Consumer<ElasticsearchClient> for MessageEditConsumer {
async fn create(database: Database, connection: Arc<Connection>, channel: Arc<Channel>, client: ElasticsearchClient) -> Self {
Self {
client,
database,
connection,
channel,
}
}
fn channel(&self) -> &Arc<Channel> {
&self.channel
}
async fn consume(&self, delivery: Delivery) -> Result<()> {
let payload = serde_json::from_slice::<MessageEditPayload>(&delivery.data)
.expect("Failed to decode message");
log::debug!("Received edit message {payload:?}");
if self
.client
.edit_message(&self.database, payload.message, payload.user)
.await
.is_ok()
{
self.channel
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
.await
.expect("Failed to ack");
} else {
self.channel
.basic_reject(delivery.delivery_tag, BasicRejectOptions {
requeue: true,
})
.await
.expect("Failed to reject");
};
Ok(())
}
}

View File

@@ -0,0 +1,9 @@
mod channel_delete;
mod message;
mod message_delete;
mod message_edit;
pub use channel_delete::*;
pub use message::*;
pub use message_delete::*;
pub use message_edit::*;

View File

@@ -0,0 +1,36 @@
use revolt_config::capture_error;
use revolt_database::Database;
use revolt_search::ElasticsearchClient;
pub async fn index_existing_messages(db: Database, client: ElasticsearchClient) {
log::info!("Starting bulk indexing.");
let mut generator = db
.fetch_all_messages()
.await
.expect("Database query failed");
let mut chunk = Vec::new();
while let Some(message) = generator.next().await.expect("Failed to fetch batch") {
chunk.push(message);
if chunk.len() >= 1000
&& let Err(e) = client
.bulk_index_messages(&db, std::mem::take(&mut chunk))
.await
{
log::error!("Error bulk indexing messages: {e}");
capture_error(&e);
}
}
if !chunk.is_empty()
&& let Err(e) = client.bulk_index_messages(&db, chunk).await
{
log::error!("Error bulk indexing messages: {e}");
capture_error(&e);
}
log::info!("Finished bulk indexing.")
}

View File

@@ -0,0 +1,206 @@
use lapin::{
options::{BasicConsumeOptions, ExchangeDeclareOptions, QueueBindOptions, QueueDeclareOptions},
types::{AMQPValue, FieldTable},
Channel, Connection, ConnectionProperties,
};
use log::info;
use revolt_config::{Settings, config, configure};
use revolt_database::{Database, DatabaseInfo, amqp::consumer::{Consumer, Delegate}};
use revolt_search::ElasticsearchClient;
use tokio::{signal::ctrl_c, spawn};
use std::{marker::PhantomData, sync::Arc};
mod consumers;
mod index;
async fn _main() {
configure!(api);
let config = config().await;
let db = DatabaseInfo::Auto.connect().await.unwrap();
let client = ElasticsearchClient::new(
&config.elasticsearch.host,
config.elasticsearch.port,
config.elasticsearch.api_key.clone(),
);
if std::env::var("REMAKE_MESSAGES_INDEX")
.as_deref()
.is_ok_and(|v| v == "1")
{
if client.delete_indexes().await.is_err() {
log::info!("Index does not existing, skipping.")
};
client.setup_indexes().await.unwrap();
};
let connection = Arc::new(
Connection::connect(
&format!(
"amqp://{}:{}@{}:{}",
&config.rabbit.username,
&config.rabbit.password,
&config.rabbit.host,
&config.rabbit.port,
),
ConnectionProperties::default(),
)
.await
.expect("Failed to connect to RabbitMQ"),
);
let mut channels = Vec::new();
channels.push(
make_queue_and_consume::<consumers::MessageConsumer>(
&client,
&db,
&connection,
&config,
&config.elasticsearch.message_queue,
)
.await,
);
channels.push(
make_queue_and_consume::<consumers::MessageEditConsumer>(
&client,
&db,
&connection,
&config,
&config.elasticsearch.message_edit_queue,
)
.await,
);
channels.push(
make_queue_and_consume::<consumers::MessageDeleteConsumer>(
&client,
&db,
&connection,
&config,
&config.elasticsearch.message_delete_queue,
)
.await,
);
channels.push(
make_queue_and_consume::<consumers::ChannelDeleteConsumer>(
&client,
&db,
&connection,
&config,
&config.elasticsearch.channel_delete_queue,
)
.await,
);
let mut task = None;
if std::env::var("INDEX_ALL_MESSAGES")
.as_deref()
.is_ok_and(|v| v == "1")
{
task = Some(spawn(index::index_existing_messages(db, client.clone())));
}
ctrl_c().await.unwrap();
for channel in channels {
let _ = channel.close(0, "close".into()).await;
}
if let Some(task) = task {
task.abort();
}
}
async fn make_queue_and_consume<F: Consumer<ElasticsearchClient>>(
client: &ElasticsearchClient,
db: &Database,
connection: &Arc<Connection>,
config: &Settings,
queue_name: &str,
) -> Arc<Channel> {
let channel = Arc::new(connection.create_channel().await.unwrap());
channel
.exchange_declare(
config.elasticsearch.exchange.clone().into(),
lapin::ExchangeKind::Direct,
ExchangeDeclareOptions {
durable: true,
..Default::default()
},
FieldTable::default(),
)
.await
.expect("Failed to declare exchange");
let mut table = FieldTable::default();
table.insert("x-queue-type".try_into().unwrap(), AMQPValue::LongString("quorum".into()));
let args = QueueDeclareOptions {
durable: true,
..Default::default()
};
channel
.queue_declare(queue_name.into(), args, table)
.await
.unwrap();
channel
.queue_bind(
queue_name.into(),
config.elasticsearch.exchange.clone().into(),
queue_name.into(),
QueueBindOptions::default(),
FieldTable::default(),
)
.await
.expect(
"This probably means the revolt.messages exchange does not exist in rabbitmq!",
);
let consumer = channel
.basic_consume(
queue_name.into(),
"".into(),
BasicConsumeOptions {
no_ack: true,
..Default::default()
},
FieldTable::default(),
)
.await
.unwrap();
info!(
"Consuming routing key {} as queue {}, tag {}",
queue_name,
queue_name,
consumer.tag()
);
let delegate = Delegate::new(
F::create(
db.clone(),
connection.clone(),
channel.clone(),
client.clone(),
)
.await,
);
consumer.set_delegate(delegate);
channel
}
#[tokio::main]
async fn main() {
_main().await
}

View File

@@ -1,15 +1,19 @@
use livekit_api::{access_token::TokenVerifier, webhooks::WebhookReceiver};
use livekit_protocol::TrackType;
use revolt_database::{
AMQP, Database, PartialMessage, SystemMessage, events::client::EventV1, iso8601_timestamp::{Duration, Timestamp}, util::reference::Reference, voice::{
RoomMetadata, UserVoiceChannel, VoiceClient, create_voice_state, delete_channel_voice_state, delete_voice_state, get_call_notification_recipients, get_user_moved_from_voice, get_user_moved_to_voice, get_voice_channel_members, set_channel_call_started_system_message, take_channel_call_started_system_message, update_voice_state_tracks
}
events::client::EventV1,
iso8601_timestamp::{Duration, Timestamp},
util::reference::Reference,
voice::{
create_voice_state, delete_channel_voice_state, delete_voice_state,
get_user_moved_from_voice, get_user_moved_to_voice, update_voice_state_tracks,
RoomMetadata, UserVoiceChannel, VoiceClient,
},
Database, AMQP,
};
use revolt_models::v0;
use revolt_result::{Result, ToRevoltError};
use rocket::{post, State};
use rocket_empty::EmptyResponse;
use ulid::Ulid;
use crate::guard::AuthHeader;
@@ -17,12 +21,12 @@ use crate::guard::AuthHeader;
pub async fn ingress(
db: &State<Database>,
voice_client: &State<VoiceClient>,
amqp: &State<AMQP>,
_amqp: &State<AMQP>,
node: &str,
auth_header: AuthHeader<'_>,
body: &str,
) -> Result<EmptyResponse> {
log::debug!("received event: {body}");
log::debug!("received event: {body:?}");
let config = revolt_config::config().await;
@@ -59,18 +63,16 @@ pub async fn ingress(
let channel_id = channel_id.to_internal_error()?;
let user_id = user_id.to_internal_error()?;
let server_id = room_metadata.to_internal_error()?.server;
let voice_channel = UserVoiceChannel {
let channel = UserVoiceChannel {
id: channel_id.clone(),
server_id: server_id.clone(),
};
let channel = Reference::from_unchecked(channel_id).as_channel(db).await?;
let joined_at = Timestamp::UNIX_EPOCH
.checked_add(Duration::seconds(event.created_at))
.unwrap();
let voice_state = create_voice_state(&voice_channel, user_id, joined_at).await?;
let voice_state = create_voice_state(&channel, user_id, joined_at).await?;
// Only publish one event when a user is moved from one channel to another.
if let Some(moved_from) = get_user_moved_to_voice(channel_id, user_id).await? {
@@ -91,66 +93,63 @@ pub async fn ingress(
.await;
};
let participants = voice_client.get_room_participants(node, channel_id).await?;
// TODO: fix `num_participants` being incorrect sometimes see (#457)
// First user who joined - send call started system message.
// if event.room.as_ref().unwrap().num_participants == 1 {
// let user = Reference::from_unchecked(user_id).as_user(db).await?;
if participants.len() == 1 {
let user = Reference::from_unchecked(user_id).as_user(db).await?;
let message_id = Ulid::from_datetime(
Timestamp::UNIX_EPOCH
.checked_add(Duration::seconds(event.created_at))
.unwrap()
.into(),
)
.to_string();
// let message_id =
// Ulid::from_datetime(DateTime::from_timestamp_secs(event.created_at).unwrap())
// .to_string();
let mut call_started_message = SystemMessage::CallStarted {
by: user_id.to_string(),
finished_at: None,
}
.into_message(channel_id.clone());
// let mut call_started_message = SystemMessage::CallStarted {
// by: user_id.to_string(),
// finished_at: None,
// }
// .into_message(channel.id().to_string());
call_started_message.id = message_id;
// call_started_message.id = message_id;
set_channel_call_started_system_message(channel_id, &call_started_message.id)
.await?;
// set_channel_call_started_system_message(channel.id(), &call_started_message.id)
// .await?;
call_started_message
.send(
db,
Some(amqp),
v0::MessageAuthor::System {
username: &user.username,
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
},
None,
None,
&channel,
false,
)
.await?;
// call_started_message
// .send(
// db,
// Some(amqp),
// v0::MessageAuthor::System {
// username: &user.username,
// avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
// },
// None,
// None,
// &channel,
// false,
// )
// .await?;
let recipients = get_call_notification_recipients(channel_id, user_id).await?;
let now = joined_at.format_short().to_string();
// let recipients = get_call_notification_recipients(&channel_id, &user_id).await?;
// let now = joined_at.format_short().to_string();
if let Err(e) = amqp
.dm_call_updated(&user.id, channel_id, Some(&now), false, recipients)
.await
{
revolt_config::capture_error(&e);
}
}
// if let Err(e) = amqp
// .dm_call_updated(&user.id, channel.id(), Some(&now), false, recipients)
// .await
// {
// revolt_config::capture_error(&e);
// }
// }
}
// User left a channel
"participant_left" => {
let channel_id = channel_id.to_internal_error()?;
let user_id = user_id.to_internal_error()?;
let server_id = room_metadata.to_internal_error()?.server;
let voice_channel = UserVoiceChannel {
let channel = UserVoiceChannel {
id: channel_id.clone(),
server_id: server_id.clone(),
};
delete_voice_state(&voice_channel, user_id).await?;
delete_voice_state(&channel, user_id).await?;
// Dont send leave event when a user is moved
if get_user_moved_from_voice(channel_id, user_id)
@@ -165,47 +164,49 @@ pub async fn ingress(
.await;
};
// See above for why this is commented out
// // Update CallStarted system message if everyone has left with the end time
let members = get_voice_channel_members(&voice_channel).await?;
// let members = get_voice_channel_members(channel_id).await?;
if members.is_none_or(|m| m.is_empty()) {
// The channel is empty so send out an "end" message for ringing
if let Err(e) = amqp
.dm_call_updated(user_id, channel_id, None, true, None)
.await
{
revolt_config::capture_internal_error!(&e);
}
// if members.is_none_or(|m| m.is_empty()) {
// // The channel is empty so send out an "end" message for ringing
// if let Err(e) = amqp
// .dm_call_updated(user_id, channel_id, None, true, None)
// .await
// {
// revolt_config::capture_internal_error!(&e);
// }
if let Some(system_message_id) =
take_channel_call_started_system_message(channel_id).await?
{
// Could have been deleted
if let Ok(mut message) = Reference::from_unchecked(&system_message_id)
.as_message(db)
.await
{
if let Some(SystemMessage::CallStarted { finished_at, .. }) =
&mut message.system
{
*finished_at = Some(Timestamp::now_utc());
// if let Some(system_message_id) =
// take_channel_call_started_system_message(channel_id).await?
// {
// // Could have been deleted
// if let Ok(mut message) = Reference::from_unchecked(&system_message_id)
// .as_message(db)
// .await
// {
// if let Some(SystemMessage::CallStarted { finished_at, .. }) =
// &mut message.system
// {
// *finished_at = Some(Timestamp::now_utc());
message
.update(
db,
PartialMessage {
system: message.system.clone(),
..Default::default()
},
Vec::new(),
)
.await?;
} else {
log::error!("Broken State: Call started message ID ({}) does not contain a CallStarted system message.", &message.id)
}
};
};
}
// message
// .update(
// db,
// PartialMessage {
// system: message.system.clone(),
// ..Default::default()
// },
// Vec::new(),
// )
// .await?;
// } else {
// log::error!("Broken State: Call started message ID ({}) does not contain a CallStarted system message.", &message.id)
// }
// };
// };
// }
}
// Audio/video track was started/stopped/unmuted/muted
"track_published" | "track_unpublished" | "track_unmuted" | "track_muted" => {

View File

@@ -77,6 +77,7 @@ revolt-presence = { workspace = true }
revolt-result = { workspace = true, features = ["rocket", "okapi"] }
revolt-permissions = { workspace = true, features = ["schemas"] }
revolt-ratelimits = { workspace = true, features = ["rocket"] }
revolt-search = { workspace = true }
# voice
livekit-api = { workspace = true }

View File

@@ -11,6 +11,7 @@ pub mod util;
use revolt_config::config;
use revolt_database::AMQP;
use revolt_ratelimits::rocket as ratelimiter;
use revolt_search::ElasticsearchClient;
use rocket::{Build, Rocket};
use rocket_cors::{AllowedOrigins, CorsOptions};
use rocket_prometheus::PrometheusMetrics;
@@ -79,6 +80,13 @@ pub async fn web() -> Rocket<Build> {
// Ratelimits
let ratelimits = ratelimiter::RatelimitStorage::new(util::ratelimits::DeltaRatelimits);
// Search
let elasticsearch = ElasticsearchClient::new(
&config.elasticsearch.host,
config.elasticsearch.port,
config.elasticsearch.api_key.clone(),
);
routes::mount(config, rocket)
.attach(prometheus.clone())
.mount("/metrics", prometheus)
@@ -90,6 +98,7 @@ pub async fn web() -> Rocket<Build> {
.manage(cors.clone())
.manage(voice_client)
.manage(ratelimits)
.manage(elasticsearch)
.attach(ratelimiter::RatelimitFairing)
.attach(cors)
.configure(rocket::Config {

View File

@@ -56,7 +56,7 @@ pub async fn invite_bot(
.throw_if_lacking_channel_permission(ChannelPermission::InviteOthers)?;
channel
.add_user_to_group(db, amqp, &bot_user, &user.id)
.add_user_to_group(db, Some(amqp), &bot_user, &user.id)
.await
.map(|_| EmptyResponse)
}

View File

@@ -50,7 +50,7 @@ pub async fn delete(
channel
.remove_user_from_group(
db,
amqp,
Some(amqp),
&user,
None,
options.leave_silently.unwrap_or_default(),
@@ -65,7 +65,7 @@ pub async fn delete(
}
Channel::TextChannel { .. } => {
permissions.throw_if_lacking_channel_permission(ChannelPermission::ManageChannel)?;
channel.delete(db).await?;
channel.delete(db, Some(amqp)).await?;
delete_voice_channel(voice_client, &UserVoiceChannel::from_channel(&channel)).await?;
}

View File

@@ -221,9 +221,6 @@ pub async fn edit(
v0::FieldsChannel::Voice => {
voice.take();
}
v0::FieldsChannel::Slowmode => {
slowmode.take();
}
_ => {}
}
}

View File

@@ -39,7 +39,7 @@ pub async fn add_member(
}
channel
.add_user_to_group(db, amqp, &member, &user.id)
.add_user_to_group(db, Some(amqp), &member, &user.id)
.await
.map(|_| EmptyResponse)
}

View File

@@ -46,7 +46,7 @@ pub async fn remove_member(
}
channel
.remove_user_from_group(db, amqp, &member, Some(&user.id), false)
.remove_user_from_group(db, Some(amqp), &member, Some(&user.id), false)
.await?;
} else {
return Err(create_error!(InvalidOperation));

View File

@@ -1,6 +1,6 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, PartialMessage, User,
Database, PartialMessage, User, AMQP,
};
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::Result;
@@ -16,6 +16,7 @@ use rocket_empty::EmptyResponse;
#[delete("/<target>/messages/<msg>/reactions")]
pub async fn clear_reactions(
db: &State<Database>,
amqp: &State<AMQP>,
user: User,
target: Reference<'_>,
msg: Reference<'_>,
@@ -33,11 +34,12 @@ pub async fn clear_reactions(
message
.update(
db,
Some(amqp),
PartialMessage {
reactions: Some(Default::default()),
..Default::default()
},
vec![]
vec![],
)
.await
.map(|_| EmptyResponse)

View File

@@ -1,6 +1,5 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, User,
AMQP, Database, User, util::{permissions::DatabasePermissionQuery, reference::Reference}
};
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::Result;
@@ -14,6 +13,7 @@ use rocket_empty::EmptyResponse;
#[delete("/<target>/messages/<msg>", rank = 2)]
pub async fn delete(
db: &State<Database>,
amqp: &State<AMQP>,
user: User,
target: Reference<'_>,
msg: Reference<'_>,
@@ -28,5 +28,5 @@ pub async fn delete(
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;
}
message.delete(db).await.map(|_| EmptyResponse)
message.delete(db, Some(amqp)).await.map(|_| EmptyResponse)
}

View File

@@ -1,8 +1,9 @@
use iso8601_timestamp::Timestamp;
use revolt_config::capture_error;
use revolt_database::{
tasks,
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, Message, PartialMessage, User,
Database, Message, PartialMessage, User, AMQP,
};
use revolt_models::v0::{self, Embed};
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
@@ -17,6 +18,7 @@ use validator::Validate;
#[patch("/<target>/messages/<msg>", data = "<edit>")]
pub async fn edit(
db: &State<Database>,
amqp: &State<AMQP>,
user: User,
target: Reference<'_>,
msg: Reference<'_>,
@@ -82,7 +84,7 @@ pub async fn edit(
partial.embeds = Some(new_embeds);
message.update(db, partial, vec![]).await?;
message.update(db, Some(amqp), partial, vec![]).await?;
// Queue up a task for processing embeds if the we have sufficient permissions
if permissions.has_channel_permission(ChannelPermission::SendEmbeds) {

View File

@@ -1,3 +1,4 @@
use revolt_config::capture_error;
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Channel, Database, PartialMessage, SystemMessage, User, AMQP,
@@ -38,6 +39,7 @@ pub async fn message_pin(
message
.update(
db,
Some(amqp),
PartialMessage {
pinned: Some(true),
..Default::default()
@@ -46,6 +48,14 @@ pub async fn message_pin(
)
.await?;
if let Err(e) = amqp
.edit_message_search(message.clone(), message.fetch_author(db).await)
.await
{
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
SystemMessage::MessagePinned {
id: message.id.clone(),
by: user.id.clone(),
@@ -116,8 +126,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.into()),
Some(user.clone()),
Some(member),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,

View File

@@ -10,6 +10,7 @@ use validator::Validate;
/// # Search for Messages
///
/// DEPRECATED: use /search instead.
/// This route searches for messages within the given parameters.
#[openapi(tag = "Messaging")]
#[post("/<target>/search", data = "<options>")]

View File

@@ -162,16 +162,10 @@ pub async fn message_send(
// Make sure we have server member (edge case if server owner)
query.are_we_a_member().await;
// Create model user / members
let model_user = user
.clone()
.into_known_static(revolt_presence::is_online(&user.id).await)
.await;
let model_member: Option<v0::Member> = query
let member = query
.member_ref()
.as_ref()
.map(|member| member.clone().into_owned().into());
.map(|member| member.clone().into_owned());
Ok(Json(
Message::create_from_api(
@@ -180,15 +174,22 @@ pub async fn message_send(
channel,
data,
v0::MessageAuthor::User(&author),
Some(model_user.clone()),
model_member.clone(),
Some(user.clone()),
member.clone(),
user.limits().await,
idempotency,
permissions.has_channel_permission(ChannelPermission::SendEmbeds),
allow_mentions,
)
.await?
.into_model(Some(model_user), model_member),
.into_model(
Some(
user.clone()
.into_known_static(revolt_presence::is_online(&user.id).await)
.await,
),
member.map(Into::into),
),
))
}
@@ -298,8 +299,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,
@@ -338,8 +339,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("1".to_string()),
false,
@@ -387,8 +388,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("2".to_string()),
false,
@@ -433,8 +434,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("1".to_string()),
false,
@@ -457,7 +458,7 @@ mod test {
// Delete the message
message
.clone()
.delete(&harness.db)
.delete(&harness.db, None)
.await
.expect("Failed to delete message");
@@ -484,8 +485,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("3".to_string()),
false,
@@ -521,7 +522,7 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone()),
Some(member.clone().into()),
user.limits().await,
IdempotencyKey::unchecked_from_string("4".to_string()),
@@ -552,7 +553,7 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone()),
Some(member.clone().into()),
user.limits().await,
IdempotencyKey::unchecked_from_string("4".to_string()),
@@ -607,13 +608,8 @@ mod test {
.into(&harness.db, Some(&other_user))
.await,
),
Some(
other_user
.clone()
.into(&harness.db, Some(&other_user))
.await,
),
Some(other_member.clone().into()),
Some(other_user.clone()),
Some(other_member.clone()),
other_user.limits().await,
IdempotencyKey::unchecked_from_string("1".to_string()),
false,
@@ -652,13 +648,8 @@ mod test {
.into(&harness.db, Some(&other_user))
.await,
),
Some(
other_user
.clone()
.into(&harness.db, Some(&other_user))
.await,
),
Some(other_member.clone().into()),
Some(other_user.clone()),
Some(other_member.clone()),
other_user.limits().await,
IdempotencyKey::unchecked_from_string("1".to_string()),
false,
@@ -722,13 +713,8 @@ mod test {
.into(&harness.db, Some(&other_user))
.await,
),
Some(
other_user
.clone()
.into(&harness.db, Some(&other_user))
.await,
),
Some(other_member.clone().into()),
Some(other_user.clone()),
Some(other_member.clone()),
other_user.limits().await,
IdempotencyKey::unchecked_from_string("1".to_string()),
false,

View File

@@ -1,3 +1,4 @@
use revolt_config::capture_error;
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Channel, Database, FieldsMessage, PartialMessage, SystemMessage, User, AMQP,
@@ -36,9 +37,22 @@ pub async fn message_unpin(
}
message
.update(db, PartialMessage::default(), vec![FieldsMessage::Pinned])
.update(
db,
Some(amqp),
PartialMessage::default(),
vec![FieldsMessage::Pinned],
)
.await?;
if let Err(e) = amqp
.edit_message_search(message.clone(), message.fetch_author(db).await)
.await
{
log::error!("Error pushing message to RabbitMQ: {e}");
capture_error(&e);
}
SystemMessage::MessageUnpinned {
id: message.id.clone(),
by: user.id.clone(),
@@ -114,8 +128,8 @@ mod test {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.into()),
Some(user.clone()),
Some(member),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,

View File

@@ -35,7 +35,7 @@ pub async fn join(
channel, creator, ..
} => {
let mut channel = db.fetch_channel(channel).await?;
channel.add_user_to_group(db, amqp, &user, creator).await?;
channel.add_user_to_group(db, Some(amqp), &user, creator).await?;
if let Channel::Group { recipients, .. } = &channel {
Ok(Json(InviteJoinResponse::Group {
users: User::fetch_many_ids_as_mutuals(db, &user, recipients).await?,

View File

@@ -17,6 +17,7 @@ mod servers;
mod sync;
mod users;
mod webhooks;
mod search;
mod account;
mod session;
mod mfa;
@@ -43,7 +44,8 @@ pub fn mount(config: Settings, mut rocket: Rocket<Build>) -> Rocket<Build> {
"/policy" => policy::routes(),
"/push" => push::routes(),
"/sync" => sync::routes(),
"/webhooks" => webhooks::routes()
"/webhooks" => webhooks::routes(),
"/search" => search::routes(),
};
} else {
mount_endpoints_and_merged_docs! {
@@ -63,7 +65,8 @@ pub fn mount(config: Settings, mut rocket: Rocket<Build>) -> Rocket<Build> {
"/onboard" => onboard::routes(),
"/policy" => policy::routes(),
"/push" => push::routes(),
"/sync" => sync::routes()
"/sync" => sync::routes(),
"/search" => search::routes(),
};
}

View File

@@ -0,0 +1,10 @@
use revolt_rocket_okapi::revolt_okapi::openapi3::OpenApi;
use rocket::Route;
mod search_messages;
pub fn routes() -> (Vec<Route>, OpenApi) {
openapi_get_routes_spec![
search_messages::search_messages
]
}

View File

@@ -0,0 +1,88 @@
use revolt_database::{util::permissions::DatabasePermissionQuery, Database, Message, User};
use revolt_models::v0;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::{create_error, Result, ToRevoltError};
use revolt_search::{ElasticsearchClient, SearchTerms};
use rocket::{serde::json::Json, State};
#[openapi(tag = "Search")]
#[post("/", data = "<data>")]
pub async fn search_messages(
db: &State<Database>,
es: &State<ElasticsearchClient>,
user: User,
data: Json<v0::DataChannelMessagesSearch>,
) -> Result<Json<v0::BulkMessageResponse>> {
if user.bot.is_some() {
return Err(create_error!(IsBot));
}
let data = data.into_inner();
if (data.server.is_some() && data.channel.is_some())
|| (data.server.is_none() && data.channel.is_none())
{
return Err(create_error!(InvalidOperation));
}
let (server_id, channels) = if let Some(server_id) = data.server {
let server = db.fetch_server(&server_id).await?;
let server_channels = db.fetch_channels(&server.channels).await?;
let mut channels = Vec::new();
let query = DatabasePermissionQuery::new(db, &user).server(&server);
for channel in server_channels {
let perms = calculate_channel_permissions(&mut query.clone().channel(&channel)).await;
if perms.has(ChannelPermission::ReadMessageHistory as u64) {
channels.push(channel.id().to_string())
}
}
(Some(server_id.clone()), channels)
} else {
let channel_id = data.channel.unwrap();
let channel = db.fetch_channel(&channel_id).await?;
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
let perms = calculate_channel_permissions(&mut query).await;
if !perms.has(ChannelPermission::ReadMessageHistory as u64) {
return Err(create_error!(InvalidOperation));
}
(channel.server().map(ToString::to_string), vec![channel_id])
};
let terms = SearchTerms {
channels,
filters: data.filters.map(Into::into).unwrap_or_default(),
offset: data.offset,
limit: data.limit.map(|limit| limit.min(100)),
sort: data.sort.map(Into::into),
};
let message_ids = es.search(terms).await.to_internal_error()?;
let mut messages = db.fetch_messages_by_id(&message_ids).await?;
// Fetching the messages looses the order so resort
let sort_order = data.sort.unwrap_or_default();
messages.sort_by(|a, b| match sort_order {
v0::SortOrder::Asc => a.id.cmp(&b.id),
v0::SortOrder::Desc => b.id.cmp(&a.id),
});
let (users, members) = Message::fetch_users(db, &messages, server_id.as_deref()).await?;
let response = v0::BulkMessageResponse::MessagesAndUsers {
messages: messages
.into_iter()
.map(|msg| msg.into_model(None, None))
.collect(),
users: User::into_mutuals(&user, users).await,
members: Some(members.into_iter().map(Into::into).collect()),
};
Ok(Json(response))
}

View File

@@ -4,7 +4,7 @@ use revolt_database::{
delete_voice_channel, get_user_voice_channel_in_server, remove_user_from_voice_channel,
UserVoiceChannel, VoiceClient,
},
Database, RemovalIntention, User,
AMQP, Database, RemovalIntention, User,
};
use revolt_models::v0;
use revolt_result::Result;
@@ -19,6 +19,7 @@ use rocket_empty::EmptyResponse;
#[delete("/<target>?<options..>")]
pub async fn delete(
db: &State<Database>,
amqp: &State<AMQP>,
voice_client: &State<VoiceClient>,
user: User,
target: Reference<'_>,
@@ -39,7 +40,7 @@ pub async fn delete(
.await?;
}
server.delete(db).await
server.delete(db, Some(amqp)).await
} else {
if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? {
remove_user_from_voice_channel(

View File

@@ -22,6 +22,6 @@ pub async fn add(
return Err(create_error!(IsBot));
}
user.add_friend(db, amqp, &mut target).await?;
user.add_friend(db, Some(amqp), &mut target).await?;
Ok(Json(target.into(db, &user).await))
}

View File

@@ -23,7 +23,7 @@ pub async fn send_friend_request(
return Err(create_error!(IsBot));
}
user.add_friend(db, amqp, &mut target).await?;
user.add_friend(db, Some(amqp), &mut target).await?;
Ok(Json(target.into(db, &user).await))
} else {
Err(create_error!(InvalidProperty))

View File

@@ -1,4 +1,4 @@
use revolt_database::{util::reference::Reference, Database};
use revolt_database::{util::reference::Reference, Database, AMQP};
use revolt_result::{create_error, Result};
use rocket::State;
use rocket_empty::EmptyResponse;
@@ -10,6 +10,7 @@ use rocket_empty::EmptyResponse;
#[delete("/<webhook_id>/<token>/<message_id>")]
pub async fn webhook_delete_message(
db: &State<Database>,
amqp: &State<AMQP>,
webhook_id: Reference<'_>,
token: String,
message_id: Reference<'_>,
@@ -23,5 +24,5 @@ pub async fn webhook_delete_message(
return Err(create_error!(CannotDeleteMessage));
}
message.delete(db).await.map(|_| EmptyResponse)
message.delete(db, Some(amqp)).await.map(|_| EmptyResponse)
}

View File

@@ -2,6 +2,7 @@ use iso8601_timestamp::Timestamp;
use revolt_config::config;
use revolt_database::{
tasks::process_embeds::queue, util::reference::Reference, Database, Message, PartialMessage,
AMQP,
};
use revolt_models::v0::{self, DataEditMessage, Embed};
use revolt_models::validator::Validate;
@@ -15,6 +16,7 @@ use rocket::{serde::json::Json, State};
#[patch("/<webhook_id>/<token>/<message_id>", data = "<data>")]
pub async fn webhook_edit_message(
db: &State<Database>,
amqp: &State<AMQP>,
webhook_id: Reference<'_>,
token: String,
message_id: Reference<'_>,
@@ -73,7 +75,7 @@ pub async fn webhook_edit_message(
partial.embeds = Some(new_embeds);
message.update(db, partial, vec![]).await?;
message.update(db, Some(amqp), partial, vec![]).await?;
// Queue up a task for processing embeds
if let Some(content) = edit.content {

View File

@@ -181,8 +181,8 @@ impl TestHarness {
flags: None,
},
v0::MessageAuthor::User(&user.clone().into(&self.db, Some(user)).await),
Some(user.clone().into(&self.db, Some(user)).await),
Some(member.clone().into()),
Some(user.clone()),
Some(member.clone()),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,

View File

@@ -10,7 +10,7 @@ use tokio::sync::RwLock;
pub mod types;
const TENOR_API_BASE_URL: &str = "https://compat.gifbox.me/v2";
const TENOR_API_BASE_URL: &str = "https://tenor.googleapis.com/v2";
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TenorError {
@@ -59,11 +59,7 @@ impl Tenor {
}
}
pub async fn request<T: DeserializeOwned>(
&self,
path: &str,
query: &[Option<(&str, &str)>],
) -> Result<Arc<T>, TenorError> {
pub async fn request<T: DeserializeOwned>(&self, path: &str, query: &[Option<(&str, &str)>]) -> Result<Arc<T>, TenorError> {
let response = self
.client
.get(format!("{TENOR_API_BASE_URL}{path}"))
@@ -99,27 +95,24 @@ impl Tenor {
}
}
let res = self
.coalescion
.execute(unique_key.clone(), || async move {
self.request::<types::PaginatedMediaResponse>(
"/search",
&[
Some(("key", &self.key)),
Some(("q", query)),
Some(("client_key", "Gifbox")),
Some(("media_filter", "webm,tinywebm")),
Some(("locale", locale)),
Some(("contentfilter", "high")),
Some(("limit", &limit.to_string())),
position.is_empty().then_some(("pos", position)),
is_category.then_some(("component", "categories")),
],
)
.await
})
.await
.unwrap();
let res = self.coalescion.execute(unique_key.clone(), || async move {
self.request::<types::PaginatedMediaResponse>(
"/search",
&[
Some(("key", &self.key)),
Some(("q", query)),
Some(("client_key", "Gifbox")),
Some(("media_filter", "webm,tinywebm")),
Some(("locale", locale)),
Some(("contentfilter", "high")),
Some(("limit", &limit.to_string())),
position.is_empty().then_some(("pos", position)),
is_category.then_some(("component", "categories"))
]
).await
})
.await
.unwrap();
if let Ok(resp) = &*res {
self.cache.write().await.insert(unique_key, resp.clone());
@@ -150,9 +143,8 @@ impl Tenor {
Some(("client_key", "Gifbox")),
Some(("locale", locale)),
Some(("contentfilter", "high")),
],
)
.await
]
).await
})
.await
.unwrap();
@@ -181,25 +173,22 @@ impl Tenor {
}
}
let res = self
.coalescion
.execute(unique_key.clone(), || async move {
self.request::<types::PaginatedMediaResponse>(
"/featured",
&[
Some(("key", &self.key)),
Some(("client_key", "Gifbox")),
Some(("media_filter", "webm,tinywebm")),
Some(("locale", locale)),
Some(("contentfilter", "high")),
Some(("limit", &limit.to_string())),
position.is_empty().then_some(("pos", position)),
],
)
.await
})
.await
.unwrap();
let res = self.coalescion.execute(unique_key.clone(), || async move {
self.request::<types::PaginatedMediaResponse>(
"/featured",
&[
Some(("key", &self.key)),
Some(("client_key", "Gifbox")),
Some(("media_filter", "webm,tinywebm")),
Some(("locale", locale)),
Some(("contentfilter", "high")),
Some(("limit", &limit.to_string())),
position.is_empty().then_some(("pos", position)),
]
).await
})
.await
.unwrap();
if let Ok(resp) = &*res {
self.featured.write().await.insert(unique_key, resp.clone());

View File

@@ -22,8 +22,8 @@
"@scalar/docusaurus": "^0.7.21",
"clsx": "^2.0.0",
"prism-react-renderer": "^2.3.0",
"react": "^19.2.7",
"react-dom": "^19.2.7"
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.9.2",

View File

@@ -70,6 +70,11 @@
"path": "crates/core/result/Cargo.toml",
"jsonpath": "$.package.version"
},
{
"type": "toml",
"path": "crates/core/search/Cargo.toml",
"jsonpath": "$.package.version"
},
{
"type": "toml",
"path": "crates/daemons/crond/Cargo.toml",
@@ -85,6 +90,11 @@
"path": "crates/daemons/voice-ingress/Cargo.toml",
"jsonpath": "$.package.version"
},
{
"type": "toml",
"path": "crates/daemons/searchd/Cargo.toml",
"jsonpath": "$.package.version"
},
{
"type": "toml",
"path": "crates/services/autumn/Cargo.toml",
@@ -149,6 +159,11 @@
"type": "toml",
"path": "Cargo.toml",
"jsonpath": "$.workspace.dependencies['revolt-result'].version"
},
{
"type": "toml",
"path": "Cargo.toml",
"jsonpath": "$.workspace.dependencies['revolt-search'].version"
}
]
}

5
scripts/mongo-init.js Normal file
View File

@@ -0,0 +1,5 @@
try {
rs.status();
} catch (e) {
rs.initiate({ _id: "rs0", members: [{ _id: 0, host: "localhost:27017" }] });
}