feat: separate friend request route

This commit is contained in:
Paul Makles
2022-06-09 17:02:01 +01:00
parent d069acd5a5
commit cb2e6fb2f7
7 changed files with 56 additions and 12 deletions

4
Cargo.lock generated
View File

@@ -2505,7 +2505,7 @@ dependencies = [
[[package]] [[package]]
name = "rauth" name = "rauth"
version = "1.0.0" version = "1.0.0"
source = "git+https://github.com/insertish/rauth?rev=e71d54eba4712b6c6a8a4354ad9fa39677714e1f#e71d54eba4712b6c6a8a4354ad9fa39677714e1f" source = "git+https://github.com/insertish/rauth?rev=7a579daa6a90845fe86139edcadbd01639b330b6#7a579daa6a90845fe86139edcadbd01639b330b6"
dependencies = [ dependencies = [
"async-std", "async-std",
"async-trait", "async-trait",
@@ -2986,7 +2986,7 @@ dependencies = [
[[package]] [[package]]
name = "rocket_rauth" name = "rocket_rauth"
version = "1.0.0" version = "1.0.0"
source = "git+https://github.com/insertish/rauth?rev=e71d54eba4712b6c6a8a4354ad9fa39677714e1f#e71d54eba4712b6c6a8a4354ad9fa39677714e1f" source = "git+https://github.com/insertish/rauth?rev=7a579daa6a90845fe86139edcadbd01639b330b6#7a579daa6a90845fe86139edcadbd01639b330b6"
dependencies = [ dependencies = [
"async-std", "async-std",
"base32", "base32",

View File

@@ -52,7 +52,7 @@ mobc-redis = { version = "0.7.0", default-features = false, features = ["async-s
# web # web
rocket = { version = "0.5.0-rc.2", default-features = false, features = ["json"] } rocket = { version = "0.5.0-rc.2", default-features = false, features = ["json"] }
rocket_empty = { git = "https://github.com/insertish/rocket_empty", branch = "master" } rocket_empty = { git = "https://github.com/insertish/rocket_empty", branch = "master" }
rocket_rauth = { git = "https://github.com/insertish/rauth", rev = "e71d54eba4712b6c6a8a4354ad9fa39677714e1f" } rocket_rauth = { git = "https://github.com/insertish/rauth", rev = "7a579daa6a90845fe86139edcadbd01639b330b6" }
# spec generation # spec generation
schemars = "0.8.8" schemars = "0.8.8"

View File

@@ -101,7 +101,8 @@ fn custom_openapi_spec() -> OpenApi {
"tags": [ "tags": [
"Account", "Account",
"Session", "Session",
"Onboarding" "Onboarding",
"MFA"
] ]
}, },
{ {
@@ -118,7 +119,7 @@ fn custom_openapi_spec() -> OpenApi {
openapi: OpenApi::default_version(), openapi: OpenApi::default_version(),
info: Info { info: Info {
title: "Revolt API".to_owned(), title: "Revolt API".to_owned(),
description: Some("User-first privacy focused chat platform.".to_owned()), description: Some("Open source user-first chat platform.".to_owned()),
terms_of_service: Some("https://revolt.chat/terms".to_owned()), terms_of_service: Some("https://revolt.chat/terms".to_owned()),
contact: Some(Contact { contact: Some(Contact {
name: Some("Revolt Support".to_owned()), name: Some("Revolt Support".to_owned()),
@@ -243,6 +244,11 @@ fn custom_openapi_spec() -> OpenApi {
description: Some("Create and manage sessions".to_owned()), description: Some("Create and manage sessions".to_owned()),
..Default::default() ..Default::default()
}, },
Tag {
name: "MFA".to_owned(),
description: Some("Multi-factor Authentication".to_owned()),
..Default::default()
},
Tag { Tag {
name: "Onboarding".to_owned(), name: "Onboarding".to_owned(),
description: Some( description: Some(

View File

@@ -1,16 +1,20 @@
use revolt_quark::models::User; use revolt_quark::models::User;
use revolt_quark::{Database, Error, Result}; use revolt_quark::{Database, Error, Ref, Result};
use rocket::serde::json::Json; use rocket::serde::json::Json;
use rocket::State; use rocket::State;
/// # Send Friend Request / Accept Request /// # Accept Friend Request
/// ///
/// Send a friend request to another user or accept another user's friend request. /// Accept another user's friend request.
#[openapi(tag = "Relationships")] #[openapi(tag = "Relationships")]
#[put("/<username>/friend")] #[put("/<target>/friend")]
pub async fn req(db: &State<Database>, user: User, username: String) -> Result<Json<User>> { pub async fn req(db: &State<Database>, user: User, target: Ref) -> Result<Json<User>> {
let mut target = db.fetch_user_by_username(&username).await?; let mut target = if let Ok(user) = db.fetch_user_by_username(&target.id).await {
user
} else {
target.as_user(db).await?
};
if user.bot.is_some() || target.bot.is_some() { if user.bot.is_some() || target.bot.is_some() {
return Err(Error::IsBot); return Err(Error::IsBot);

View File

@@ -13,6 +13,7 @@ mod find_mutual;
mod get_default_avatar; mod get_default_avatar;
mod open_dm; mod open_dm;
mod remove_friend; mod remove_friend;
mod send_friend_request;
mod unblock_user; mod unblock_user;
pub fn routes() -> (Vec<Route>, OpenApi) { pub fn routes() -> (Vec<Route>, OpenApi) {
@@ -33,5 +34,6 @@ pub fn routes() -> (Vec<Route>, OpenApi) {
remove_friend::req, remove_friend::req,
block_user::req, block_user::req,
unblock_user::req, unblock_user::req,
send_friend_request::req,
] ]
} }

View File

@@ -0,0 +1,32 @@
use revolt_quark::models::User;
use revolt_quark::{Database, Error, Result};
use rocket::serde::json::Json;
use rocket::State;
use serde::{Deserialize, Serialize};
/// # User Lookup Information
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct DataSendFriendRequest {
username: String,
}
/// # Send Friend Request
///
/// Send a friend request to another user.
#[openapi(tag = "Relationships")]
#[post("/friend", data = "<data>")]
pub async fn req(
db: &State<Database>,
user: User,
data: Json<DataSendFriendRequest>,
) -> Result<Json<User>> {
let mut target = db.fetch_user_by_username(&data.username).await?;
if user.bot.is_some() || target.bot.is_some() {
return Err(Error::IsBot);
}
user.add_friend(db, &mut target).await?;
Ok(Json(target.with_auto_perspective(db, &user).await))
}

View File

@@ -84,7 +84,7 @@ rocket_empty = { optional = true, git = "https://github.com/insertish/rocket_emp
rocket_cors = { optional = true, git = "https://github.com/lawliet89/rocket_cors", rev = "5843861a88958c16bfaa0b40f0d8910772bcd2f6" } rocket_cors = { optional = true, git = "https://github.com/lawliet89/rocket_cors", rev = "5843861a88958c16bfaa0b40f0d8910772bcd2f6" }
# rAuth # rAuth
rauth = { git = "https://github.com/insertish/rauth", rev = "e71d54eba4712b6c6a8a4354ad9fa39677714e1f", features = [ "async-std-runtime" ] } rauth = { git = "https://github.com/insertish/rauth", rev = "7a579daa6a90845fe86139edcadbd01639b330b6", features = [ "async-std-runtime" ] }
# Sentry # Sentry
sentry = "0.25.0" sentry = "0.25.0"