* Create comment in transaction. Fixes #3741 * Removing if let on comment create.
This commit is contained in:
parent
246e38a45b
commit
719b76a6e7
1 changed files with 51 additions and 48 deletions
|
@ -59,52 +59,55 @@ impl Comment {
|
||||||
) -> Result<Comment, Error> {
|
) -> Result<Comment, Error> {
|
||||||
let conn = &mut get_conn(pool).await?;
|
let conn = &mut get_conn(pool).await?;
|
||||||
|
|
||||||
// Insert, to get the id
|
conn
|
||||||
let inserted_comment = insert_into(comment)
|
.build_transaction()
|
||||||
.values(comment_form)
|
.run(|conn| {
|
||||||
.on_conflict(ap_id)
|
Box::pin(async move {
|
||||||
.do_update()
|
// Insert, to get the id
|
||||||
.set(comment_form)
|
let inserted_comment = insert_into(comment)
|
||||||
.get_result::<Self>(conn)
|
.values(comment_form)
|
||||||
.await;
|
.on_conflict(ap_id)
|
||||||
|
.do_update()
|
||||||
|
.set(comment_form)
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
if let Ok(comment_insert) = inserted_comment {
|
let comment_id = inserted_comment.id;
|
||||||
let comment_id = comment_insert.id;
|
|
||||||
|
|
||||||
// You need to update the ltree column
|
// You need to update the ltree column
|
||||||
let ltree = Ltree(if let Some(parent_path) = parent_path {
|
let ltree = Ltree(if let Some(parent_path) = parent_path {
|
||||||
// The previous parent will already have 0 in it
|
// The previous parent will already have 0 in it
|
||||||
// Append this comment id
|
// Append this comment id
|
||||||
format!("{}.{}", parent_path.0, comment_id)
|
format!("{}.{}", parent_path.0, comment_id)
|
||||||
} else {
|
} else {
|
||||||
// '0' is always the first path, append to that
|
// '0' is always the first path, append to that
|
||||||
format!("{}.{}", 0, comment_id)
|
format!("{}.{}", 0, comment_id)
|
||||||
});
|
});
|
||||||
|
|
||||||
let updated_comment = diesel::update(comment.find(comment_id))
|
let updated_comment = diesel::update(comment.find(comment_id))
|
||||||
.set(path.eq(ltree))
|
.set(path.eq(ltree))
|
||||||
.get_result::<Self>(conn)
|
.get_result::<Self>(conn)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Update the child count for the parent comment_aggregates
|
// Update the child count for the parent comment_aggregates
|
||||||
// You could do this with a trigger, but since you have to do this manually anyway,
|
// You could do this with a trigger, but since you have to do this manually anyway,
|
||||||
// you can just have it here
|
// you can just have it here
|
||||||
if let Some(parent_path) = parent_path {
|
if let Some(parent_path) = parent_path {
|
||||||
// You have to update counts for all parents, not just the immediate one
|
// You have to update counts for all parents, not just the immediate one
|
||||||
// TODO if the performance of this is terrible, it might be better to do this as part of a
|
// TODO if the performance of this is terrible, it might be better to do this as part of a
|
||||||
// scheduled query... although the counts would often be wrong.
|
// scheduled query... although the counts would often be wrong.
|
||||||
//
|
//
|
||||||
// The child_count query for reference:
|
// The child_count query for reference:
|
||||||
// select c.id, c.path, count(c2.id) as child_count from comment c
|
// select c.id, c.path, count(c2.id) as child_count from comment c
|
||||||
// left join comment c2 on c2.path <@ c.path and c2.path != c.path
|
// left join comment c2 on c2.path <@ c.path and c2.path != c.path
|
||||||
// group by c.id
|
// group by c.id
|
||||||
|
|
||||||
let parent_id = parent_path.0.split('.').nth(1);
|
let parent_id = parent_path.0.split('.').nth(1);
|
||||||
|
|
||||||
if let Some(parent_id) = parent_id {
|
if let Some(parent_id) = parent_id {
|
||||||
let top_parent = format!("0.{}", parent_id);
|
let top_parent = format!("0.{}", parent_id);
|
||||||
let update_child_count_stmt = format!(
|
let update_child_count_stmt = format!(
|
||||||
"
|
"
|
||||||
update comment_aggregates ca set child_count = c.child_count
|
update comment_aggregates ca set child_count = c.child_count
|
||||||
from (
|
from (
|
||||||
select c.id, c.path, count(c2.id) as child_count from comment c
|
select c.id, c.path, count(c2.id) as child_count from comment c
|
||||||
|
@ -113,15 +116,15 @@ from (
|
||||||
group by c.id
|
group by c.id
|
||||||
) as c
|
) as c
|
||||||
where ca.comment_id = c.id"
|
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
|
updated_comment
|
||||||
} else {
|
}) as _
|
||||||
inserted_comment
|
})
|
||||||
}
|
.await
|
||||||
}
|
}
|
||||||
pub async fn read_from_apub_id(
|
pub async fn read_from_apub_id(
|
||||||
pool: &mut DbPool<'_>,
|
pool: &mut DbPool<'_>,
|
||||||
|
|
Loading…
Reference in a new issue