lemmy/crates/db_schema/src/impls/person_block.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2021-10-16 13:33:38 +00:00
use crate::{
newtypes::PersonId,
schema::person_block::dsl::{person_block, person_id, target_id},
source::person_block::{PersonBlock, PersonBlockForm},
2021-10-16 13:33:38 +00:00
traits::Blockable,
2022-11-09 10:05:00 +00:00
utils::{get_conn, DbPool},
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
2021-10-16 13:33:38 +00:00
impl PersonBlock {
2022-11-09 10:05:00 +00:00
pub async fn read(
pool: &DbPool,
for_person_id: PersonId,
for_recipient_id: PersonId,
) -> Result<Self, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
person_block
.filter(person_id.eq(for_person_id))
.filter(target_id.eq(for_recipient_id))
.first::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
}
}
2022-11-09 10:05:00 +00:00
#[async_trait]
impl Blockable for PersonBlock {
type Form = PersonBlockForm;
2022-11-09 10:05:00 +00:00
async fn block(pool: &DbPool, person_block_form: &PersonBlockForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(person_block)
.values(person_block_form)
.on_conflict((person_id, target_id))
.do_update()
.set(person_block_form)
.get_result::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
async fn unblock(pool: &DbPool, person_block_form: &Self::Form) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(
person_block
.filter(person_id.eq(person_block_form.person_id))
.filter(target_id.eq(person_block_form.target_id)),
)
.execute(conn)
2022-11-09 10:05:00 +00:00
.await
}
}