From 962c7d62c7eddbb4250671cbaea3bb973ea743d3 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Tue, 11 Jun 2024 18:46:07 +0100 Subject: [PATCH] fix: allow fetching discoverable servers as invites --- crates/core/database/src/util/reference.rs | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/core/database/src/util/reference.rs b/crates/core/database/src/util/reference.rs index 4c2206545..9282d39d7 100644 --- a/crates/core/database/src/util/reference.rs +++ b/crates/core/database/src/util/reference.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use revolt_result::Result; #[cfg(feature = "rocket-impl")] use rocket::request::FromParam; @@ -44,9 +46,27 @@ impl Reference { db.fetch_channel(&self.id).await } - /// Fetch invite from Ref + /// Fetch invite from Ref or create invite to server if discoverable pub async fn as_invite(&self, db: &Database) -> Result { - db.fetch_invite(&self.id).await + if ulid::Ulid::from_str(&self.id).is_ok() { + let server = self.as_server(db).await?; + if !server.discoverable { + return Err(create_error!(NotFound)); + } + + Ok(Invite::Server { + code: self.id.to_string(), + server: server.id, + creator: server.owner, + channel: server + .channels + .into_iter() + .next() + .ok_or(create_error!(NotFound))?, + }) + } else { + db.fetch_invite(&self.id).await + } } /// Fetch message from Ref