Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Revert attempts at deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaJak committed Oct 30, 2023
1 parent 48037b1 commit d6d2b0d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 166 deletions.
107 changes: 0 additions & 107 deletions src/routes/v3/follow.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/routes/v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{auth::oauth, util::cors::default_cors};
use actix_web::{web, HttpResponse};
use serde_json::json;

pub mod follow;
pub mod oauth_clients;
pub mod organizations;
pub mod users;
Expand Down
79 changes: 50 additions & 29 deletions src/routes/v3/organizations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
database::{self, redis::RedisPool},
auth::get_user_from_headers,
database::{self, models::DatabaseError, redis::RedisPool},
models::pats::Scopes,
queue::session::AuthQueue,
routes::ApiError,
};
Expand Down Expand Up @@ -29,24 +31,38 @@ pub async fn organization_follow(
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
crate::routes::v3::follow::follow(
req,
target_id,
pool,
redis,
session_queue,
|id, pool, redis| async move { DBOrganization::get(&id, &**pool, &redis).await },
|follower_id, target_id, pool| async move {
DBOrganizationFollow {
follower_id,
target_id,
}
.insert(&**pool)
.await
},
"organization",
let (_, current_user) = get_user_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::USER_WRITE]),
)
.await?;

let target = DBOrganization::get(&target_id, &**pool, &redis)
.await?
.ok_or_else(|| {
ApiError::InvalidInput("The specified organization does not exist!".to_string())
})?;

DBOrganizationFollow {
follower_id: current_user.id.into(),
target_id: target.id,
}
.insert(&**pool)
.await
.map_err(|e| match e {
DatabaseError::Database(e)
if e.as_database_error()
.is_some_and(|e| e.is_unique_violation()) =>
{
ApiError::InvalidInput("You are already following this organization!".to_string())
}
e => e.into(),
})?;

Ok(HttpResponse::NoContent().body(""))
}

#[delete("{id}/follow")]
Expand All @@ -57,17 +73,22 @@ pub async fn organization_unfollow(
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
crate::routes::v3::follow::unfollow(
req,
target_id,
pool,
redis,
session_queue,
|id, pool, redis| async move { DBOrganization::get(&id, &**pool, &redis).await },
|follower_id, target_id, pool| async move {
DBOrganizationFollow::unfollow(follower_id, target_id, &**pool).await
},
"organization",
let (_, current_user) = get_user_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::USER_WRITE]),
)
.await
.await?;

let target = DBOrganization::get(&target_id, &**pool, &redis)
.await?
.ok_or_else(|| {
ApiError::InvalidInput("The specified organization does not exist!".to_string())
})?;

DBOrganizationFollow::unfollow(current_user.id.into(), target.id.into(), &**pool).await?;

Check warning on line 91 in src/routes/v3/organizations.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `database::models::ids::OrganizationId`

warning: useless conversion to the same type: `database::models::ids::OrganizationId` --> src/routes/v3/organizations.rs:91:60 | 91 | DBOrganizationFollow::unfollow(current_user.id.into(), target.id.into(), &**pool).await?; | ^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `target.id` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default

Ok(HttpResponse::NoContent().body(""))
}
76 changes: 47 additions & 29 deletions src/routes/v3/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
auth::{filter_authorized_projects, get_user_from_headers},
database::{
self,
models::event_item::{EventData, EventSelector, EventType},
models::{
event_item::{EventData, EventSelector, EventType},
DatabaseError,
},
redis::RedisPool,
},
models::{
Expand Down Expand Up @@ -49,24 +52,36 @@ pub async fn user_follow(
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
crate::routes::v3::follow::follow(
req,
target_id,
pool,
redis,
session_queue,
|id, pool, redis| async move { DBUser::get(&id, &**pool, &redis).await },
|follower_id, target_id, pool| async move {
DBUserFollow {
follower_id,
target_id,
}
.insert(&**pool)
.await
},
"user",
let (_, current_user) = get_user_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::USER_WRITE]),
)
.await?;

let target = DBUser::get(&target_id, &**pool, &redis)
.await?
.ok_or_else(|| ApiError::InvalidInput("The specified user does not exist!".to_string()))?;

DBUserFollow {
follower_id: current_user.id.into(),
target_id: target.id,
}
.insert(&**pool)
.await
.map_err(|e| match e {
DatabaseError::Database(e)
if e.as_database_error()
.is_some_and(|e| e.is_unique_violation()) =>
{
ApiError::InvalidInput("You are already following this user!".to_string())
}
e => e.into(),
})?;

Ok(HttpResponse::NoContent().body(""))
}

#[delete("{id}/follow")]
Expand All @@ -77,19 +92,22 @@ pub async fn user_unfollow(
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
crate::routes::v3::follow::unfollow(
req,
target_id,
pool,
redis,
session_queue,
|id, pool, redis| async move { DBUser::get(&id, &**pool, &redis).await },
|follower_id, target_id, pool| async move {
DBUserFollow::unfollow(follower_id, target_id, &**pool).await
},
"organization",
let (_, current_user) = get_user_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::USER_WRITE]),
)
.await
.await?;

let target = DBUser::get(&target_id, &**pool, &redis)
.await?
.ok_or_else(|| ApiError::InvalidInput("The specified user does not exist!".to_string()))?;

DBUserFollow::unfollow(current_user.id.into(), target.id.into(), &**pool).await?;

Check warning on line 108 in src/routes/v3/users.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `database::models::ids::UserId`

warning: useless conversion to the same type: `database::models::ids::UserId` --> src/routes/v3/users.rs:108:52 | 108 | DBUserFollow::unfollow(current_user.id.into(), target.id.into(), &**pool).await?; | ^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `target.id` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Ok(HttpResponse::NoContent().body(""))
}

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit d6d2b0d

Please sign in to comment.