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

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Oct 3, 2023
1 parent 5d47008 commit e9e7fc1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 76 deletions.
110 changes: 45 additions & 65 deletions src/database/models/organization_item.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::models::ids::base62_impl::{parse_base62, to_base62};
use crate::{models::ids::base62_impl::{parse_base62, to_base62}, database::redis::RedisPool};

use super::{ids::*, TeamMember};
use redis::cmd;
use serde::{Deserialize, Serialize};

const ORGANIZATIONS_NAMESPACE: &str = "organizations";
const ORGANIZATIONS_TITLES_NAMESPACE: &str = "organizations_titles";

const DEFAULT_EXPIRY: i64 = 1800;

#[derive(Deserialize, Serialize, Clone, Debug)]
/// An organization of users who together control one or more projects and organizations.
pub struct Organization {
Expand Down Expand Up @@ -55,7 +52,7 @@ impl Organization {
pub async fn get<'a, E>(
string: &str,
exec: E,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<Option<Self>, super::DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
Expand All @@ -68,7 +65,7 @@ impl Organization {
pub async fn get_id<'a, 'b, E>(
id: OrganizationId,
exec: E,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<Option<Self>, super::DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
Expand All @@ -81,7 +78,7 @@ impl Organization {
pub async fn get_many_ids<'a, 'b, E>(
organization_ids: &[OrganizationId],
exec: E,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<Vec<Self>, super::DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
Expand All @@ -96,7 +93,7 @@ impl Organization {
pub async fn get_many<'a, E, T: ToString>(
organization_strings: &[T],
exec: E,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<Vec<Self>, super::DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
Expand All @@ -107,8 +104,6 @@ impl Organization {
return Ok(Vec::new());
}

let mut redis = redis.get().await?;

let mut found_organizations = Vec::new();
let mut remaining_strings = organization_strings
.iter()
Expand All @@ -121,36 +116,24 @@ impl Organization {
.collect::<Vec<_>>();

organization_ids.append(
&mut cmd("MGET")
.arg(
organization_strings
.iter()
.map(|x| {
format!(
"{}:{}",
ORGANIZATIONS_TITLES_NAMESPACE,
x.to_string().to_lowercase()
)
})
.collect::<Vec<_>>(),
)
.query_async::<_, Vec<Option<i64>>>(&mut redis)
.await?
.into_iter()
.flatten()
.collect(),
&mut redis
.multi_get::<i64, _>(
ORGANIZATIONS_TITLES_NAMESPACE,
organization_strings
.iter()
.map(|x| x.to_string().to_lowercase())
.collect(),
)
.await?
.into_iter()
.flatten()
.collect()
);

if !organization_ids.is_empty() {
let organizations = cmd("MGET")
.arg(
organization_ids
.iter()
.map(|x| format!("{}:{}", ORGANIZATIONS_NAMESPACE, x))
.collect::<Vec<_>>(),
)
.query_async::<_, Vec<Option<String>>>(&mut redis)
.await?;
let organizations = redis
.multi_get::<String, _>(ORGANIZATIONS_NAMESPACE, organization_ids)
.await?;

for organization in organizations {
if let Some(organization) =
Expand Down Expand Up @@ -201,25 +184,23 @@ impl Organization {
.await?;

for organization in organizations {
cmd("SET")
.arg(format!("{}:{}", ORGANIZATIONS_NAMESPACE, organization.id.0))
.arg(serde_json::to_string(&organization)?)
.arg("EX")
.arg(DEFAULT_EXPIRY)
.query_async::<_, ()>(&mut redis)
.await?;

cmd("SET")
.arg(format!(
"{}:{}",
redis
.set(
ORGANIZATIONS_NAMESPACE,
organization.id.0,
serde_json::to_string(&organization)?,
None,
)
.await?;
redis
.set(
ORGANIZATIONS_TITLES_NAMESPACE,
organization.title.to_lowercase()
))
.arg(organization.id.0)
.arg("EX")
.arg(DEFAULT_EXPIRY)
.query_async::<_, ()>(&mut redis)
organization.title.to_lowercase(),
organization.id.0,
None,
)
.await?;

found_organizations.push(organization);
}
}
Expand Down Expand Up @@ -265,7 +246,7 @@ impl Organization {
pub async fn remove(
id: OrganizationId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<Option<()>, super::DatabaseError> {
use futures::TryStreamExt;

Expand Down Expand Up @@ -333,19 +314,18 @@ impl Organization {
pub async fn clear_cache(
id: OrganizationId,
title: Option<String>,
redis: &deadpool_redis::Pool,
redis: &RedisPool,
) -> Result<(), super::DatabaseError> {
let mut redis = redis.get().await?;
let mut cmd = cmd("DEL");
cmd.arg(format!("{}:{}", ORGANIZATIONS_NAMESPACE, id.0));

redis.delete(ORGANIZATIONS_NAMESPACE, id.0).await?;
if let Some(title) = title {
cmd.arg(format!(
"{}:{}",
ORGANIZATIONS_TITLES_NAMESPACE,
title.to_lowercase()
));
redis
.delete(
ORGANIZATIONS_TITLES_NAMESPACE,
title.to_lowercase(),
)
.await?;
}
cmd.query_async::<_, ()>(&mut redis).await?;

Ok(())
}
Expand Down
21 changes: 11 additions & 10 deletions src/routes/v2/organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;
use crate::auth::{filter_authorized_projects, get_user_from_headers};
use crate::database::models::team_item::TeamMember;
use crate::database::models::{generate_organization_id, team_item, Organization};
use crate::database::redis::RedisPool;
use crate::file_hosting::FileHost;
use crate::models::ids::base62_impl::parse_base62;
use crate::models::organizations::OrganizationId;
Expand Down Expand Up @@ -56,7 +57,7 @@ pub async fn organization_create(
req: HttpRequest,
new_organization: web::Json<NewOrganization>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, CreateError> {
let current_user = get_user_from_headers(
Expand Down Expand Up @@ -143,7 +144,7 @@ pub async fn organization_get(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let id = info.into_inner().0;
Expand Down Expand Up @@ -208,7 +209,7 @@ pub async fn organizations_get(
req: HttpRequest,
web::Query(ids): web::Query<OrganizationIds>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let ids = serde_json::from_str::<Vec<&str>>(&ids.ids)?;
Expand Down Expand Up @@ -298,7 +299,7 @@ pub async fn organizations_edit(
info: web::Path<(String,)>,
new_organization: web::Json<OrganizationEdit>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(
Expand Down Expand Up @@ -434,7 +435,7 @@ pub async fn organization_delete(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(
Expand Down Expand Up @@ -498,7 +499,7 @@ pub async fn organization_projects_get(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let info = info.into_inner().0;
Expand Down Expand Up @@ -547,7 +548,7 @@ pub async fn organization_projects_add(
info: web::Path<(String,)>,
project_info: web::Json<OrganizationProjectAdd>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let info = info.into_inner().0;
Expand Down Expand Up @@ -649,7 +650,7 @@ pub async fn organization_projects_remove(
req: HttpRequest,
info: web::Path<(String, String)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let (organization_id, project_id) = info.into_inner();
Expand Down Expand Up @@ -743,7 +744,7 @@ pub async fn organization_icon_edit(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
file_host: web::Data<Arc<dyn FileHost + Send + Sync>>,
mut payload: web::Payload,
session_queue: web::Data<AuthQueue>,
Expand Down Expand Up @@ -848,7 +849,7 @@ pub async fn delete_organization_icon(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
file_host: web::Data<Arc<dyn FileHost + Send + Sync>>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v2/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub async fn team_members_get_organization(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
redis: web::Data<deadpool_redis::Pool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let string = info.into_inner().0;
Expand Down

0 comments on commit e9e7fc1

Please sign in to comment.