Compare commits

...

6 Commits

Author SHA1 Message Date
Paul Makles
95db641f8f feat: initialise Discord bridge crate 2023-04-21 22:29:17 +01:00
Paul Makles
43e45aef3f fix: patch redis-rs for SMISMEMBER support 2023-04-21 19:37:33 +01:00
Paul Makles
c5880596a4 refactor: use a separate online user ID set 2023-04-21 19:36:25 +01:00
Paul Makles
59a644891d fix: collect IDs as set first to deduplicate entries 2023-04-21 18:52:41 +01:00
Paul Makles
a326cdc736 refactor: remove unused import 2023-04-21 18:40:00 +01:00
Paul Makles
03a28dbb3e fix: use debian:bullseye image for glibc 2.29 2023-04-21 18:11:06 +01:00
15 changed files with 1427 additions and 64 deletions

89
Cargo.lock generated
View File

@@ -871,12 +871,6 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "dtoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]]
name = "dyn-clone"
version = "1.0.5"
@@ -1831,6 +1825,27 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "metrics"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e52eb6380b6d2a10eb3434aec0885374490f5b82c8aaf5cd487a183c98be834"
dependencies = [
"ahash",
"metrics-macros",
]
[[package]]
name = "metrics-macros"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49e30813093f757be5cf21e50389a24dc7dbb22c49f23b7e8f51d69b508a5ffa"
dependencies = [
"proc-macro2",
"quote 1.0.18",
"syn 1.0.107",
]
[[package]]
name = "mime"
version = "0.3.16"
@@ -1897,9 +1912,9 @@ dependencies = [
[[package]]
name = "mobc"
version = "0.7.3"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f76d2f2e2dcbb00a8d3b2b09f026a74a82693ea52cd071647aa6cfa7f1ff37e"
checksum = "fc79c4a77e312fee9c7bd4b957c12ad1196db73c4a81e5c0b13f02083c4f7f2f"
dependencies = [
"async-std",
"async-trait",
@@ -1908,17 +1923,21 @@ dependencies = [
"futures-timer",
"futures-util",
"log",
"metrics",
"thiserror",
"tokio 1.18.2",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "mobc-redis"
version = "0.7.0"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7b5db77b37c9224d5b9949b214041ea3e1c15b6b1e5dd24a5acb8e73975d6d6"
checksum = "7bd8e2fd6bf7e35263b86662e663a9496a0352ceddd413b6c33313c36d5068fd"
dependencies = [
"mobc",
"redis 0.19.0",
"redis 0.22.3",
]
[[package]]
@@ -2624,57 +2643,55 @@ dependencies = [
[[package]]
name = "redis"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a6ddfecac9391fed21cce10e83c65fa4abafd77df05c98b1c647c65374ce9b3"
version = "0.22.3"
source = "git+https://github.com/insertish/redis-rs?rev=6575a6b1c09eb8c9cc7f0082d95fe6b8f903c4d7#6575a6b1c09eb8c9cc7f0082d95fe6b8f903c4d7"
dependencies = [
"async-std",
"async-trait",
"bytes 1.1.0",
"combine",
"dtoa",
"futures-util",
"itoa 0.4.8",
"itoa 1.0.2",
"percent-encoding",
"pin-project-lite 0.2.9",
"sha1",
"ryu",
"sha1_smol",
"tokio 1.18.2",
"tokio-util 0.6.10",
"tokio-util 0.7.2",
"url",
]
[[package]]
name = "redis"
version = "0.21.5"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a80b5f38d7f5a020856a0e16e40a9cfabf88ae8f0e4c2dcd8a3114c1e470852"
checksum = "3ea8c51b5dc1d8e5fd3350ec8167f464ec0995e79f2e90a075b63371500d557f"
dependencies = [
"async-std",
"async-trait",
"bytes 1.1.0",
"combine",
"dtoa",
"futures-util",
"itoa 0.4.8",
"itoa 1.0.2",
"percent-encoding",
"pin-project-lite 0.2.9",
"sha1",
"ryu",
"tokio 1.18.2",
"tokio-util 0.6.10",
"tokio-util 0.7.2",
"url",
]
[[package]]
name = "redis-kiss"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4f2f48fb776a308331c4a1ecb3a7b7b99f4d46c6beb2fc03db2da0f63fcad6"
checksum = "58605cfd83a2146161de13bb253f24db25eef6919b35403fc90f508ef89bec92"
dependencies = [
"bincode",
"lazy_static",
"mobc",
"mobc-redis",
"redis 0.21.5",
"redis 0.23.0",
"rmp-serde",
"serde",
"serde_json",
@@ -2792,7 +2809,7 @@ dependencies = [
[[package]]
name = "revolt-bonfire"
version = "0.5.18"
version = "0.5.19"
dependencies = [
"async-std",
"async-tungstenite",
@@ -2808,7 +2825,7 @@ dependencies = [
[[package]]
name = "revolt-delta"
version = "0.5.18"
version = "0.5.19"
dependencies = [
"async-channel",
"async-std",
@@ -2823,12 +2840,9 @@ dependencies = [
"linkify 0.6.0",
"log",
"lru",
"mobc",
"mobc-redis",
"nanoid",
"num_enum",
"once_cell",
"redis 0.21.5",
"regex",
"reqwest",
"revolt-quark",
@@ -2847,7 +2861,7 @@ dependencies = [
[[package]]
name = "revolt-quark"
version = "0.5.18"
version = "0.5.19"
dependencies = [
"async-lock",
"async-recursion",
@@ -3471,15 +3485,6 @@ dependencies = [
"digest 0.10.3",
]
[[package]]
name = "sha1"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1da05c97445caa12d05e848c4a4fcbbea29e748ac28f7e80e9b010392063770"
dependencies = [
"sha1_smol",
]
[[package]]
name = "sha1_smol"
version = "1.0.0"

View File

@@ -1,2 +1,7 @@
[workspace]
members = ["crates/*"]
members = ["crates/*", "crates/bridges/*"]
exclude = ["crates/bridges"]
[patch.crates-io]
# mobc-redis = { git = "https://github.com/insertish/mobc", rev = "8b880bb59f2ba80b4c7bc40c649c113d8857a186" }
redis = { git = "https://github.com/insertish/redis-rs", rev = "6575a6b1c09eb8c9cc7f0082d95fe6b8f903c4d7" }

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt-bonfire"
version = "0.5.18"
version = "0.5.19"
license = "AGPL-3.0-or-later"
edition = "2021"

View File

@@ -2,7 +2,7 @@
FROM ghcr.io/revoltchat/base:latest AS builder
# Bundle Stage
FROM debian:buster-slim
FROM debian:bullseye-slim
RUN apt-get update && \
apt-get install -y ca-certificates && \
apt-get clean

1294
crates/bridges/discord/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
[package]
name = "revolt-discord-bridge"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serenity = { version = "0.11.5", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

View File

@@ -0,0 +1,45 @@
use std::env;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::model::channel::Channel;
use serenity::model::gateway::{GatewayIntents, Ready};
use serenity::prelude::*;
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
println!("Received message: {}", msg.content);
if msg.content == "deez" {
if let Ok(Channel::Guild(channel)) = msg.channel(&ctx).await {
channel.send_message(&ctx, |m| {
m.content("this is where a message would usually go")
}).await.unwrap();
}
}
}
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
#[tokio::main]
async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents =
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt-delta"
version = "0.5.18"
version = "0.5.19"
license = "AGPL-3.0-or-later"
authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018"
@@ -43,11 +43,6 @@ async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"]
# internal util
lettre = "0.10.0-alpha.4"
# redis
redis = { version = "0.21.2", features = ["async-std-comp"] }
mobc = { version = "0.7.3" }
mobc-redis = { version = "0.7.0", default-features = false, features = ["async-std-comp"] }
# web
rocket = { version = "0.5.0-rc.2", default-features = false, features = ["json"] }
rocket_empty = { version = "0.1.1", features = ["schema"] }

View File

@@ -2,7 +2,7 @@
FROM ghcr.io/revoltchat/base:latest AS builder
# Bundle Stage
FROM debian:buster-slim
FROM debian:bullseye-slim
RUN apt-get update && \
apt-get install -y ca-certificates && \
apt-get clean

View File

@@ -1,6 +1,6 @@
use revolt_quark::{
models::{Invite, User},
perms, Db, Error, Permission, Ref, Result,
perms, Db, Permission, Ref, Result,
};
use rocket::serde::json::Json;

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt-quark"
version = "0.5.18"
version = "0.5.19"
license = "AGPL-3.0-or-later"
edition = "2021"
@@ -42,7 +42,7 @@ revolt_okapi = "0.9.1"
revolt_rocket_okapi = { version = "0.9.1", features = [ "swagger" ] }
# Databases
redis-kiss = { version = "0.1.3" }
redis-kiss = { version = "0.1.4" }
mongodb = { optional = true, version = "2.1.0", default-features = false }
# Async

View File

@@ -99,7 +99,7 @@ impl State {
let mut user = self.clone_user();
// Find all relationships to the user.
let mut user_ids: Vec<String> = user
let mut user_ids: HashSet<String> = user
.relations
.as_ref()
.map(|arr| arr.iter().map(|x| x.id.to_string()).collect())
@@ -128,14 +128,15 @@ impl State {
for channel in &channels {
match channel {
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
user_ids.append(&mut recipients.clone());
user_ids.extend(&mut recipients.clone().into_iter());
}
_ => {}
}
}
// Fetch presence data for known users.
let online_ids = presence_filter_online(&user_ids).await;
let online_ids =
presence_filter_online(&user_ids.iter().cloned().collect::<Vec<String>>()).await;
user.online = Some(true);
// Fetch user data.

View File

@@ -305,12 +305,12 @@ impl IntoUsers for Message {
impl IntoUsers for Vec<Message> {
fn get_user_ids(&self) -> Vec<String> {
let mut ids = vec![];
let mut ids = HashSet::new();
for message in self {
ids.append(&mut message.get_user_ids());
ids.extend(&mut message.get_user_ids().into_iter());
}
ids
ids.into_iter().collect()
}
}

View File

@@ -10,3 +10,4 @@ pub static REGION_ID: Lazy<u16> = Lazy::new(|| {
});
pub static REGION_KEY: Lazy<String> = Lazy::new(|| format!("region{}", &*REGION_ID));
pub static ONLINE_SET: &str = "online";

View File

@@ -11,7 +11,7 @@ use operations::{
__get_set_size, __remove_from_set_string, __remove_from_set_u32,
};
use self::entry::REGION_KEY;
use self::entry::{ONLINE_SET, REGION_KEY};
/// Create a new presence session, returns the ID of this session
pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u32) {
@@ -29,6 +29,7 @@ pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u32) {
// Add session to user's sessions and to the region
__add_to_set_u32(&mut conn, user_id, session_id).await;
__add_to_set_string(&mut conn, ONLINE_SET, user_id).await;
__add_to_set_string(&mut conn, &REGION_KEY, &format!("{user_id}:{session_id}")).await;
info!("Created session for {user_id}, assigned them a session ID of {session_id}.");
@@ -65,6 +66,7 @@ async fn presence_delete_session_internal(
// Return whether this was the last session
let is_empty = __get_set_size(&mut conn, user_id).await == 0;
if is_empty {
__remove_from_set_string(&mut conn, ONLINE_SET, user_id).await;
info!("User ID {} just went offline.", &user_id);
}
@@ -109,14 +111,19 @@ pub async fn presence_filter_online(user_ids: &'_ [String]) -> HashSet<String> {
// Otherwise, go ahead as normal.
if let Ok(mut conn) = get_connection().await {
let data: Vec<Option<Vec<u8>>> = conn.get(user_ids).await.unwrap_or_default();
// Ok so, if this breaks, that means we've lost the Redis patch which adds SMISMEMBER
// Currently it's patched in through a forked repository, investigate what happen to it
let data: Vec<bool> = conn
.sismember("online", user_ids)
.await
.expect("this shouldn't happen, please read this code! presence/mod.rs");
if data.is_empty() {
return set;
}
// We filter known values to figure out who is online.
for i in 0..user_ids.len() {
if data[i].is_some() {
if data[i] {
set.insert(user_ids[i].to_string());
}
}