2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2024-04-16 12:48:15 +00:00
|
|
|
diesel::OptionalExtension,
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::{CommentId, PersonId, PersonMentionId},
|
2024-03-26 14:46:37 +00:00
|
|
|
schema::person_mention,
|
2022-11-19 04:33:54 +00:00
|
|
|
source::person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for PersonMention {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = PersonMentionInsertForm;
|
|
|
|
type UpdateForm = PersonMentionUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = PersonMentionId;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn create(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
person_mention_form: &Self::InsertForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-12-16 14:42:57 +00:00
|
|
|
// since the return here isnt utilized, we dont need to do an update
|
2024-04-17 12:35:54 +00:00
|
|
|
// but get_result doesn't return the existing row here
|
2024-03-26 14:46:37 +00:00
|
|
|
insert_into(person_mention::table)
|
2021-02-26 13:49:58 +00:00
|
|
|
.values(person_mention_form)
|
2024-03-26 14:46:37 +00:00
|
|
|
.on_conflict((person_mention::recipient_id, person_mention::comment_id))
|
2020-12-16 14:42:57 +00:00
|
|
|
.do_update()
|
2021-02-26 13:49:58 +00:00
|
|
|
.set(person_mention_form)
|
2019-10-20 00:46:29 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
person_mention_id: PersonMentionId,
|
2022-10-27 09:24:07 +00:00
|
|
|
person_mention_form: &Self::UpdateForm,
|
2019-10-20 00:46:29 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-03-26 14:46:37 +00:00
|
|
|
diesel::update(person_mention::table.find(person_mention_id))
|
2021-02-26 13:49:58 +00:00
|
|
|
.set(person_mention_form)
|
2019-10-20 00:46:29 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl PersonMention {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn mark_all_as_read(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_recipient_id: PersonId,
|
2021-02-26 13:49:58 +00:00
|
|
|
) -> Result<Vec<PersonMention>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-07-20 14:56:40 +00:00
|
|
|
diesel::update(
|
2024-03-26 14:46:37 +00:00
|
|
|
person_mention::table
|
|
|
|
.filter(person_mention::recipient_id.eq(for_recipient_id))
|
|
|
|
.filter(person_mention::read.eq(false)),
|
2020-07-20 14:56:40 +00:00
|
|
|
)
|
2024-03-26 14:46:37 +00:00
|
|
|
.set(person_mention::read.eq(true))
|
2020-07-20 14:56:40 +00:00
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-07-20 14:56:40 +00:00
|
|
|
}
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read_by_comment_and_person(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-08 14:28:32 +00:00
|
|
|
for_comment_id: CommentId,
|
|
|
|
for_recipient_id: PersonId,
|
2024-04-16 12:48:15 +00:00
|
|
|
) -> Result<Option<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-03-26 14:46:37 +00:00
|
|
|
person_mention::table
|
|
|
|
.filter(person_mention::comment_id.eq(for_comment_id))
|
|
|
|
.filter(person_mention::recipient_id.eq(for_recipient_id))
|
2024-04-16 12:48:15 +00:00
|
|
|
.first(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2024-04-16 12:48:15 +00:00
|
|
|
.optional()
|
2021-10-08 14:28:32 +00:00
|
|
|
}
|
2020-07-20 14:56:40 +00:00
|
|
|
}
|