Skip to content

Commit

Permalink
Improve Code Quality (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 authored Nov 19, 2023
1 parent e590e44 commit d22d12f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 87 deletions.
8 changes: 4 additions & 4 deletions server/src/routes/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub async fn get(
.await
.map_err(profile_http_error_mapper)?;

if let Some(header) = profile.header {
return Ok(Redirect::to(header.as_str()));
}
let Some(header) = profile.header else {
return Err(http_simple_status_error(StatusCode::NOT_FOUND));
};

Err(http_simple_status_error(StatusCode::NOT_FOUND))
Ok(Redirect::to(header.as_str()))
}
8 changes: 4 additions & 4 deletions server/src/routes/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub async fn get(
.await
.map_err(profile_http_error_mapper)?;

if let Some(avatar) = profile.avatar {
return Ok(Redirect::to(avatar.as_str()));
}
let Some(avatar) = profile.avatar else {
return Err(http_simple_status_error(StatusCode::NOT_FOUND));
};

Err(http_simple_status_error(StatusCode::NOT_FOUND))
Ok(Redirect::to(avatar.as_str()))
}
90 changes: 45 additions & 45 deletions shared/src/models/lookup/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,52 +70,52 @@ impl ENSLookup for Image {
return Ok(format!("{}{hash}", self.ipfs_gateway));
}

if let Some(captures) = EIP155_REGEX.captures(&value) {
let chain_id = captures.get(1).unwrap().as_str();
let contract_type = captures.get(2).unwrap().as_str();
let contract_address = captures.get(3).unwrap().as_str();
let token_id = captures.get(4).unwrap().as_str();

let chain_id = chain_id
.parse::<u64>()
.map_err(|err| ImageLookupError::FormatError(err.to_string()))?;

let token_id = U256::from_dec_str(token_id)
.map_err(|err| ImageLookupError::FormatError(err.to_string()))?;

let contract_type = match contract_type {
"erc721" => EIP155ContractType::ERC721,
"erc1155" => EIP155ContractType::ERC1155,
_ => {
return Err(
ImageLookupError::FormatError("invalid contract type".to_string()).into(),
)
}
};

info!(
"Encountered Avatar: {chain_id} {contract_type} {contract_address} {token_id}",
chain_id = chain_id,
contract_type = contract_type.as_str(),
contract_address = contract_address,
token_id = token_id
);

let resolved_uri = resolve_eip155(
ChainId::from(chain_id),
contract_type,
contract_address,
token_id,
&state.rpc,
&opensea_api_key,
)
.await?;

// TODO: Remove naive approach
return Ok(resolved_uri);
}
let Some(captures) = EIP155_REGEX.captures(&value) else {
return Ok(value);
};

let chain_id = captures.get(1).unwrap().as_str();
let contract_type = captures.get(2).unwrap().as_str();
let contract_address = captures.get(3).unwrap().as_str();
let token_id = captures.get(4).unwrap().as_str();

let chain_id = chain_id
.parse::<u64>()
.map_err(|err| ImageLookupError::FormatError(err.to_string()))?;

let token_id = U256::from_dec_str(token_id)
.map_err(|err| ImageLookupError::FormatError(err.to_string()))?;

let contract_type = match contract_type {
"erc721" => EIP155ContractType::ERC721,
"erc1155" => EIP155ContractType::ERC1155,
_ => {
return Err(
ImageLookupError::FormatError("invalid contract type".to_string()).into(),
)
}
};

info!(
"Encountered Avatar: {chain_id} {contract_type} {contract_address} {token_id}",
chain_id = chain_id,
contract_type = contract_type.as_str(),
contract_address = contract_address,
token_id = token_id
);

Ok(value)
let resolved_uri = resolve_eip155(
ChainId::from(chain_id),
contract_type,
contract_address,
token_id,
&state.rpc,
&opensea_api_key,
)
.await?;

// TODO: Remove naive approach
return Ok(resolved_uri);
}

fn name(&self) -> String {
Expand Down
25 changes: 9 additions & 16 deletions shared/src/models/profile/from_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@ impl Profile {
let name = if let Ok(name) = cache.get(&cache_key).await {
name
} else {
let result = match rpc.lookup_address(address).await {
Ok(result) => result,
Err(error) => {
println!("Error resolving address: {error:?}");

if let ProviderError::EnsError(_) = error {
// Cache the value, and expire it after 10 minutes
cache
.set(&cache_key, "", 600)
.await
.map_err(|_| ProfileError::Other("cache set failed".to_string()))?;
};

return Err(ProfileError::NotFound);
}
};
let result = rpc
.lookup_address(address)
.await
.or_else(|error| match error {
// address doesn't resolve, cache ""
ProviderError::EnsError(_) => Ok("".to_string()),
// yield error up, don't cache
_ => Err(error),
})?;

// Cache the value, and expire it after 10 minutes
cache
Expand Down
16 changes: 10 additions & 6 deletions shared/src/models/universal_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ pub async fn resolve_universal(
.call(&typed_transaction, None)
.await
.map_err(|err| {
if let JsonRpcClientError(rpc_err) = &err {
if let Some(rpc_err_raw) = rpc_err.as_error_response() {
if rpc_err_raw.message == MAGIC_UNIVERSAL_RESOLVER_ERROR_MESSAGE {
return ProfileError::NotFound;
}
}
let JsonRpcClientError(rpc_err) = &err else {
return ProfileError::RPCError(err);
};

let Some(rpc_err_raw) = rpc_err.as_error_response() else {
return ProfileError::RPCError(err);
};

if rpc_err_raw.message == MAGIC_UNIVERSAL_RESOLVER_ERROR_MESSAGE {
return ProfileError::NotFound;
}

ProfileError::RPCError(err)
Expand Down
23 changes: 11 additions & 12 deletions worker/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,18 @@ impl LookupType {
_ => unreachable!(),
};

if let Some(img) = field {
let url = Url::parse(img.as_str()).map_err(|_| {
Response::error("Invalid avatar URL", StatusCode::NOT_ACCEPTABLE.as_u16())
.expect("status should be in correct range")
})?;

return Ok(Response::redirect(url).map_err(|_| {
Response::error("Worker error", 500)
.expect("status should be in correct range")
})?);
}
let Some(img) = field else {
return Err(http_simple_status_error(StatusCode::NOT_FOUND));
};

let url = Url::parse(img.as_str()).map_err(|_| {
Response::error("Invalid avatar URL", StatusCode::NOT_ACCEPTABLE.as_u16())
.expect("status should be in correct range")
})?;

Err(http_simple_status_error(StatusCode::NOT_FOUND))
Ok(Response::redirect(url).map_err(|_| {
Response::error("Worker error", 500).expect("status should be in correct range")
})?)
}
_ => {
let profile = match self {
Expand Down

0 comments on commit d22d12f

Please sign in to comment.