CommentSavedForm,
CommentUpdateForm,
},
- traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
- utils::{get_conn, naive_now, DbPool},
+ traits::{Crud, Likeable, Saveable},
+ utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT},
};
use diesel::{
dsl::{insert_into, sql_query},
impl Comment {
pub async fn permadelete_for_creator(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
+
diesel::update(comment.filter(creator_id.eq(for_creator_id)))
.set((
- content.eq("*Permananently Deleted*"),
+ content.eq(DELETED_REPLACEMENT_TEXT),
deleted.eq(true),
updated.eq(naive_now()),
))
}
pub async fn update_removed_for_creator(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
for_creator_id: PersonId,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
}
pub async fn create(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
comment_form: &CommentInsertForm,
parent_path: Option<&Ltree>,
) -> Result<Comment, Error> {
// left join comment c2 on c2.path <@ c.path and c2.path != c.path
// group by c.id
- let top_parent = format!("0.{}", parent_path.0.split('.').collect::<Vec<&str>>()[1]);
- let update_child_count_stmt = format!(
- "
+ let parent_id = parent_path.0.split('.').nth(1);
+
+ if let Some(parent_id) = parent_id {
+ let top_parent = format!("0.{}", parent_id);
+ let update_child_count_stmt = format!(
+ "
update comment_aggregates ca set child_count = c.child_count
from (
select c.id, c.path, count(c2.id) as child_count from comment c
group by c.id
) as c
where ca.comment_id = c.id"
- );
+ );
- sql_query(update_child_count_stmt).execute(conn).await?;
+ sql_query(update_child_count_stmt).execute(conn).await?;
+ }
}
updated_comment
} else {
inserted_comment
}
}
- pub async fn read_from_apub_id(pool: &DbPool, object_id: Url) -> Result<Option<Self>, Error> {
+ pub async fn read_from_apub_id(
+ pool: &mut DbPool<'_>,
+ object_id: Url,
+ ) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let object_id: DbUrl = object_id.into();
Ok(
let mut ltree_split: Vec<&str> = self.path.0.split('.').collect();
ltree_split.remove(0); // The first is always 0
if ltree_split.len() > 1 {
- ltree_split[ltree_split.len() - 2]
- .parse::<i32>()
- .map(CommentId)
- .ok()
+ let parent_comment_id = ltree_split.get(ltree_split.len() - 2);
+ parent_comment_id.and_then(|p| p.parse::<i32>().map(CommentId).ok())
} else {
None
}
type InsertForm = CommentInsertForm;
type UpdateForm = CommentUpdateForm;
type IdType = CommentId;
- async fn read(pool: &DbPool, comment_id: CommentId) -> Result<Self, Error> {
+ async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
comment.find(comment_id).first::<Self>(conn).await
}
- async fn delete(pool: &DbPool, comment_id: CommentId) -> Result<usize, Error> {
+ async fn delete(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(comment.find(comment_id)).execute(conn).await
}
/// This is unimplemented, use [[Comment::create]]
- async fn create(_pool: &DbPool, _comment_form: &Self::InsertForm) -> Result<Self, Error> {
+ async fn create(_pool: &mut DbPool<'_>, _comment_form: &Self::InsertForm) -> Result<Self, Error> {
unimplemented!();
}
async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
comment_id: CommentId,
comment_form: &Self::UpdateForm,
) -> Result<Self, Error> {
impl Likeable for CommentLike {
type Form = CommentLikeForm;
type IdType = CommentId;
- async fn like(pool: &DbPool, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
+ async fn like(pool: &mut DbPool<'_>, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id};
let conn = &mut get_conn(pool).await?;
insert_into(comment_like)
.await
}
async fn remove(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
person_id_: PersonId,
comment_id_: CommentId,
) -> Result<usize, Error> {
#[async_trait]
impl Saveable for CommentSaved {
type Form = CommentSavedForm;
- async fn save(pool: &DbPool, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
+ async fn save(
+ pool: &mut DbPool<'_>,
+ comment_saved_form: &CommentSavedForm,
+ ) -> Result<Self, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
let conn = &mut get_conn(pool).await?;
insert_into(comment_saved)
.get_result::<Self>(conn)
.await
}
- async fn unsave(pool: &DbPool, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
+ async fn unsave(
+ pool: &mut DbPool<'_>,
+ comment_saved_form: &CommentSavedForm,
+ ) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
let conn = &mut get_conn(pool).await?;
diesel::delete(
}
}
-impl DeleteableOrRemoveable for Comment {
- fn blank_out_deleted_or_removed_info(mut self) -> Self {
- self.content = String::new();
- self
- }
-}
-
#[cfg(test)]
mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
use crate::{
newtypes::LanguageId,
source::{
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
- let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+ let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+ .await
+ .unwrap();
let new_person = PersonInsertForm::builder()
.name("terry".into())