fix (backend-rs): wrong unwrap_or usage

This commit is contained in:
naskya 2024-05-21 22:57:33 +09:00
parent 97d0a21312
commit b24530065e
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
2 changed files with 29 additions and 26 deletions

View file

@ -36,11 +36,13 @@ type Note = note::Model;
async fn antennas() -> Result<Vec<Antenna>, Error> {
const CACHE_KEY: &str = "antennas";
Ok(cache::get::<Vec<Antenna>>(CACHE_KEY).await?.unwrap_or({
if let Some(antennas) = cache::get::<Vec<Antenna>>(CACHE_KEY).await? {
Ok(antennas)
} else {
let antennas = antenna::Entity::find().all(db_conn().await?).await?;
cache::set(CACHE_KEY, &antennas, 5 * 60).await?;
antennas
}))
Ok(antennas)
}
}
#[crate::export]

View file

@ -94,9 +94,10 @@ pub async fn check_hit_antenna(
let db = db_conn().await?;
let blocked_user_ids: Vec<String> = cache::get_one(cache::Category::Block, &note.user_id)
.await?
.unwrap_or({
let blocked_user_ids: Vec<String> =
if let Some(ids) = cache::get_one(cache::Category::Block, &note.user_id).await? {
ids
} else {
// cache miss
let blocks = blocking::Entity::find()
.select_only()
@ -107,7 +108,7 @@ pub async fn check_hit_antenna(
.await?;
cache::set_one(cache::Category::Block, &note.user_id, &blocks, 10 * 60).await?;
blocks
});
};
// if the antenna owner is blocked by the note author, return false
if blocked_user_ids.contains(&antenna.user_id) {
@ -116,26 +117,26 @@ pub async fn check_hit_antenna(
if [NoteVisibilityEnum::Home, NoteVisibilityEnum::Followers].contains(&note.visibility) {
let following_user_ids: Vec<String> =
cache::get_one(cache::Category::Follow, &antenna.user_id)
.await?
.unwrap_or({
// cache miss
let following = following::Entity::find()
.select_only()
.column(following::Column::FolloweeId)
.filter(following::Column::FollowerId.eq(&antenna.user_id))
.into_tuple::<String>()
.all(db)
.await?;
cache::set_one(
cache::Category::Follow,
&antenna.user_id,
&following,
10 * 60,
)
if let Some(ids) = cache::get_one(cache::Category::Follow, &antenna.user_id).await? {
ids
} else {
// cache miss
let following = following::Entity::find()
.select_only()
.column(following::Column::FolloweeId)
.filter(following::Column::FollowerId.eq(&antenna.user_id))
.into_tuple::<String>()
.all(db)
.await?;
following
});
cache::set_one(
cache::Category::Follow,
&antenna.user_id,
&following,
10 * 60,
)
.await?;
following
};
// if the antenna owner is not following the note author, return false
if !following_user_ids.contains(&note.user_id) {