Skip to content

Commit

Permalink
replace avatar url with cid
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill-K-1 committed Mar 5, 2024
1 parent e31981d commit 6ea7e1f
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 50 deletions.
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tower-http = { version = "0", features = ["cors"] }
reqwest = { version = "0", features = ["rustls-tls", "json", "deflate"] }
jsonwebtoken = "9"
url = { version = "2", features = ["serde"] }
cid = "0"

# SerDe
serde = { version = "1", features = ["derive"] }
Expand Down
4 changes: 1 addition & 3 deletions gov-portal-db/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
"telegramMaxLength": 32,
"twitterMaxLength": 32,
"bioMaxLength": 250,
"bioMinLength": 2,
"avatarUrlMaxLength": 250,
"avatarUrlMinLength": 7
"bioMinLength": 2
},
"emailVerification": {
"mailerBaseUrl": null,
Expand Down
4 changes: 2 additions & 2 deletions gov-portal-db/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tower_http::cors::CorsLayer;

use shared::common::{
SendEmailRequest, SendEmailRequestKind, SessionToken, UserEmailConfirmationToken, UserInfo,
UserProfile, UserProfileStatus,
UserProfile, UserProfileStatus, WrappedCid,
};

use crate::{
Expand Down Expand Up @@ -86,7 +86,7 @@ pub struct UpdateUserRequest {
#[serde(default)]
pub bio: Option<String>,
#[serde(default)]
pub avatar: Option<url::Url>,
pub avatar: Option<WrappedCid>,
#[serde(flatten)]
pub session: SessionToken,
}
Expand Down
19 changes: 1 addition & 18 deletions gov-portal-db/src/users_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ pub struct UserProfileAttributes {
pub twitter_max_length: usize,
pub bio_max_length: usize,
pub bio_min_length: usize,
pub avatar_url_max_length: usize,
pub avatar_url_min_length: usize,
}

impl Default for UserProfileAttributes {
Expand All @@ -81,8 +79,6 @@ impl Default for UserProfileAttributes {
twitter_max_length: 32,
bio_max_length: 250,
bio_min_length: 2,
avatar_url_max_length: 250,
avatar_url_min_length: 7, // length("a://a.a")
}
}
}
Expand Down Expand Up @@ -393,17 +389,6 @@ impl UsersManager {
)));
}

if user.avatar.as_ref().is_some_and(|value| {
value.as_str().len() < self.config.user_profile_attributes.avatar_url_min_length
|| value.as_str().len() > self.config.user_profile_attributes.avatar_url_max_length
}) {
return Err(error::Error::InvalidInput(format!(
"Avatar URL doesn't met requirements (min: {}, max: {})",
self.config.user_profile_attributes.avatar_url_min_length,
self.config.user_profile_attributes.avatar_url_max_length
)));
}

Ok(())
}

Expand Down Expand Up @@ -496,9 +481,7 @@ mod tests {
"telegramMaxLength": 32,
"twitterMaxLength": 32,
"bioMaxLength": 250,
"bioMinLength": 2,
"avatarUrlMaxLength": 250,
"avatarUrlMinLength": 7
"bioMinLength": 2
},
"emailVerification": {
"mailerBaseUrl": "http://localhost:10002",
Expand Down
7 changes: 4 additions & 3 deletions gov-portal-db/tests/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#![cfg(feature = "enable-integration-tests")]
use std::str::FromStr;

use airdao_gov_portal_db::{
quiz::{Quiz, QuizAnswer, QuizConfig},
users_manager::{EmailVerificationConfig, *},
};
use assert_matches::assert_matches;
use shared::common::{EmailFrom, UserInfo, UserProfile, UserProfileStatus};
use shared::common::{EmailFrom, UserInfo, UserProfile, UserProfileStatus, WrappedCid};
use web3::types::Address;

#[tokio::test]
Expand Down Expand Up @@ -210,7 +209,9 @@ async fn test_complete_profile() -> Result<(), anyhow::Error> {
name: Some("some name".to_owned()),
role: Some("some role".to_owned()),
bio: Some("some bio".to_owned()),
avatar: Some(url::Url::from_str("http://avatar.link")?),
avatar: Some(WrappedCid::new(
"bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
)?),
..default_user_info()
})
.await?;
Expand Down
26 changes: 12 additions & 14 deletions gov-portal-mocker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tower_http::cors::CorsLayer;
use shared::{
common::{
ApprovedResponse, PendingResponse, SBTRequest, SessionToken, SignedSBTRequest,
UserDbConfig, UserInfo, UserProfile, VerifyAccountResponse,
UserDbConfig, UserInfo, UserProfile, VerifyAccountResponse, WrappedCid,
},
logger, utils,
};
Expand Down Expand Up @@ -96,7 +96,7 @@ pub enum UpdateUserQuery {
telegram: Option<String>,
twitter: Option<String>,
bio: Option<String>,
avatar: Option<String>,
avatar: Box<Option<WrappedCid>>,
},
NoJwtToken {},
}
Expand Down Expand Up @@ -219,10 +219,11 @@ async fn index_route(
&utils::get_checksum_address(&user.wallet),
)
.replace(
"{{USER_AVATAR_URL}}",
user.avatar
"{{USER_AVATAR_CID}}",
&user
.avatar
.as_ref()
.map(|url| url.as_str())
.map(|cid| cid.to_string())
.unwrap_or_default(),
)
.replace("{{USER_NAME}}", user.name.as_deref().unwrap_or_default())
Expand Down Expand Up @@ -264,7 +265,7 @@ async fn update_user_route(
bio,
avatar,
} => match state
.update_user(&token, name, role, telegram, twitter, bio, avatar)
.update_user(&token, name, role, telegram, twitter, bio, *avatar)
.and_then(|_| state.get_user(&token))
.await
{
Expand Down Expand Up @@ -299,10 +300,11 @@ async fn update_user_route(
&utils::get_checksum_address(&user.wallet),
)
.replace(
"{{USER_AVATAR_URL}}",
user.avatar
"{{USER_AVATAR_CID}}",
&user
.avatar
.as_ref()
.map(|url| url.as_str())
.map(|cid| cid.to_string())
.unwrap_or_default(),
)
.replace("{{USER_NAME}}", user.name.as_deref().unwrap_or_default())
Expand Down Expand Up @@ -596,12 +598,8 @@ impl AppState {
telegram: Option<String>,
twitter: Option<String>,
bio: Option<String>,
avatar: Option<String>,
avatar: Option<WrappedCid>,
) -> Result<(), anyhow::Error> {
let avatar = match avatar {
Some(avatar_url) => Some(url::Url::parse(&avatar_url)?),
None => None,
};
let raw_data = self
.client
.post(&[&self.config.user_db.base_url, "/update-user"].concat())
Expand Down
4 changes: 2 additions & 2 deletions gov-portal-mocker/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ <h1>User Profile</h1>
<input type="text" id="wallet" name="wallet" value="{{USER_WALLET}}" maxlength="64" size="64" disabled="true" />
</p>
<p>
<label for="avatar">Avatar URL:</label>
<input type="text" id="avatar" name="avatar" value="{{USER_AVATAR_URL}}" maxlength="64" size="64" />
<label for="avatar">Avatar ipfs url (or CID):</label>
<input type="text" id="avatar" name="avatar" value="{{USER_AVATAR_CID}}" maxlength="256" size="64" />
</p>
<p>
<label for="name">Name:</label>
Expand Down
1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ k256 = { workspace = true }
# Web
jsonwebtoken = { workspace = true }
url = { workspace = true }
cid = { workspace = true }

# SerDe
serde = { workspace = true }
Expand Down
Loading

0 comments on commit 6ea7e1f

Please sign in to comment.