Skip to content

Commit

Permalink
Merge pull request #252 from pubky/fix/remove-loose-unwraps
Browse files Browse the repository at this point in the history
Remove unsafe unwraps
  • Loading branch information
amirRamirfatahi authored Dec 16, 2024
2 parents ac21ac5 + 8d36c60 commit 4997cd6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/models/tag/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ impl TagGlobal {
) -> Result<Option<Vec<String>>, DynError> {
match user_id {
None => read_from_set(&label, Some(skip), Some(limit)).await,
Some(id) => get_tag_taggers_by_reach(&label, &id, reach.unwrap(), skip, limit).await,
Some(id) => {
get_tag_taggers_by_reach(
&label,
&id,
reach.unwrap_or(TagStreamReach::Friends),
skip,
limit,
)
.await
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/models/tag/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ impl TagSearch {
) -> Result<(), DynError> {
let post_key_slice: &[&str] = &[author_id, post_id];
let key_parts = [&TAG_GLOBAL_POST_TIMELINE[..], &[tag_label]].concat();
let tag_search = Self::check_sorted_set_member(&key_parts, post_key_slice)
.await
.unwrap();
let tag_search = Self::check_sorted_set_member(&key_parts, post_key_slice).await?;
if tag_search.is_none() {
let option = PostDetails::try_from_index_json(post_key_slice).await?;
if let Some(post_details) = option {
Expand Down
28 changes: 20 additions & 8 deletions src/models/tag/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl HotTags {
let key_parts_vector: Vec<&str> =
key_parts.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
let data = HotTagsData::try_from_index_json(key_parts_vector.clone().as_slice()).await?;
let ranking = HotTags::try_from_index_sorted_set(
let cached_ranking = HotTags::try_from_index_sorted_set(
key_parts_vector.as_slice(),
None,
None,
Expand All @@ -159,14 +159,18 @@ impl HotTags {
)
.await?;

if data.is_none() || ranking.is_none() {
return Ok(None);
}
let ranking = match cached_ranking {
Some(ranking) => ranking,
None => return Ok(None),
};

let mapping = data.unwrap();
let mapping = match data {
Some(data) => data,
None => return Ok(None),
};
// for each value in ranking, look up the value in data
let mut hot_tags = Vec::new();
for (label, _) in ranking.unwrap() {
for (label, _) in ranking {
if let Some(tag) = mapping.0.get(&label) {
hot_tags.push(HotTag {
label,
Expand Down Expand Up @@ -232,7 +236,12 @@ impl HotTags {
) -> Result<Option<HotTags>, DynError> {
match user_id {
Some(user_id) => {
HotTags::get_hot_tags_by_reach(user_id, reach.unwrap(), tags_query).await
HotTags::get_hot_tags_by_reach(
user_id,
reach.unwrap_or(TagStreamReach::Friends),
tags_query,
)
.await
}
None => HotTags::get_global_hot_tags(tags_query).await,
}
Expand Down Expand Up @@ -262,7 +271,10 @@ impl HotTags {
});
let result = retrieve_from_graph::<HotTags>(query, "hot_tags").await?;

let hot_tags = result.unwrap();
let hot_tags = match result {
Some(hot_tags) => hot_tags,
None => return Ok(None),
};
if hot_tags.len() > 0 {
HotTags::set_to_global_cache(hot_tags.clone(), tags_query).await?;
}
Expand Down
12 changes: 7 additions & 5 deletions src/routes/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use axum::{
extract::Request,
http::HeaderValue,
middleware::{self, Next},
response::Response,
routing::get_service,
Expand Down Expand Up @@ -34,13 +35,14 @@ async fn static_files_middleware(request: Request, next: Next) -> Result<Respons
let file = &value[0];
match file {
Some(value) => {
response.headers_mut().insert(
"content-length",
value.size.to_string().as_str().parse().unwrap(),
);
let content_type = match HeaderValue::try_from(value.content_type.clone()) {
Ok(value) => value,
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
};
response
.headers_mut()
.insert("content-type", value.content_type.parse().unwrap());
.insert("content-length", HeaderValue::from(value.size));
response.headers_mut().insert("content-type", content_type);
Ok(response)
}
None => Err(StatusCode::NOT_FOUND),
Expand Down
6 changes: 3 additions & 3 deletions src/types/timeframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ impl Timeframe {
Timeframe::Today => now
.date_naive()
.and_hms_opt(0, 0, 0)
.unwrap()
.unwrap_or_default()
.and_utc()
.timestamp_millis(),
Timeframe::ThisMonth => now
.date_naive()
.with_day(1)
.unwrap()
.unwrap_or_default()
.and_hms_opt(0, 0, 0)
.unwrap()
.unwrap_or_default()
.and_utc()
.timestamp_millis(),
Timeframe::AllTime => 0,
Expand Down

0 comments on commit 4997cd6

Please sign in to comment.