Update community.rs

This commit is contained in:
dullbananas 2024-03-29 20:55:01 -07:00 committed by GitHub
parent d34f8aefc8
commit 93fdeda16a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -150,31 +150,14 @@ impl Community {
for p in &posts {
debug_assert!(p.community_id == community_id);
}
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
update(
// first remove all existing featured posts
post::table,
)
.filter(post::dsl::community_id.eq(community_id))
.filter(post::dsl::featured_community)
.set(post::dsl::featured_community.eq(false))
.execute(conn)
.await?;
// then mark the given posts as featured
let post_ids: Vec<_> = posts.iter().map(|p| p.id).collect();
update(post::table)
.filter(post::dsl::id.eq_any(post_ids))
.set(post::dsl::featured_community.eq(true))
.execute(conn)
.await?;
Ok(())
}) as _
})
.await
// Mark the given posts as featured and all other posts as not featured
update(post::table)
.filter(post::dsl::community_id.eq(community_id))
// This filter just prevents locking rows that need no update
.filter(post::dsl::featured_community.ne(post::dsl::id.eq_any(post_ids)))
.set(post::dsl::featured_community.eq(post::dsl::id.eq_any(post_ids)))
.execute(conn)
.await?;
}
}