2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::PersonId,
|
2022-11-09 10:05:00 +00:00
|
|
|
schema::person_block::dsl::*,
|
2021-08-19 20:54:15 +00:00
|
|
|
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},
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-08-19 20:54:15 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl PersonBlock {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read(
|
|
|
|
pool: &DbPool,
|
2021-08-19 20:54:15 +00:00
|
|
|
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?;
|
2021-08-19 20:54:15 +00:00
|
|
|
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
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-19 20:54:15 +00:00
|
|
|
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?;
|
2021-08-19 20:54:15 +00:00
|
|
|
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
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
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?;
|
2021-08-19 20:54:15 +00:00
|
|
|
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
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|