| 1 |
use super::{PgPool, Uuid}; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#[tracing::instrument(skip_all)] |
| 10 |
pub async fn create_post( |
| 11 |
pool: &PgPool, |
| 12 |
thread_id: Uuid, |
| 13 |
author_id: Uuid, |
| 14 |
body_markdown: &str, |
| 15 |
body_html: &str, |
| 16 |
) -> Result<Uuid, sqlx::Error> { |
| 17 |
let mut tx = pool.begin().await?; |
| 18 |
let post_id = create_post_tx(&mut tx, thread_id, author_id, body_markdown, body_html) |
| 19 |
.await? |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
.ok_or(sqlx::Error::RowNotFound)?; |
| 24 |
tx.commit().await?; |
| 25 |
Ok(post_id) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#[tracing::instrument(skip_all)] |
| 35 |
pub async fn create_post_tx( |
| 36 |
conn: &mut sqlx::PgConnection, |
| 37 |
thread_id: Uuid, |
| 38 |
author_id: Uuid, |
| 39 |
body_markdown: &str, |
| 40 |
body_html: &str, |
| 41 |
) -> Result<Option<Uuid>, sqlx::Error> { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
let post_id = sqlx::query_scalar!( |
| 50 |
"INSERT INTO posts (thread_id, author_id, body_markdown, body_html) |
| 51 |
SELECT $1, $2, $3, $4 |
| 52 |
WHERE EXISTS ( |
| 53 |
SELECT 1 FROM threads |
| 54 |
WHERE id = $1 AND deleted_at IS NULL AND locked = false |
| 55 |
) |
| 56 |
RETURNING id", |
| 57 |
thread_id, |
| 58 |
author_id, |
| 59 |
body_markdown, |
| 60 |
body_html, |
| 61 |
) |
| 62 |
.fetch_optional(&mut *conn) |
| 63 |
.await?; |
| 64 |
|
| 65 |
|
| 66 |
if post_id.is_some() { |
| 67 |
sqlx::query!( |
| 68 |
"UPDATE threads SET last_activity_at = now() WHERE id = $1", |
| 69 |
thread_id |
| 70 |
) |
| 71 |
.execute(&mut *conn) |
| 72 |
.await?; |
| 73 |
} |
| 74 |
|
| 75 |
Ok(post_id) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[tracing::instrument(skip_all)] |
| 88 |
pub async fn create_post_external_ref( |
| 89 |
pool: &PgPool, |
| 90 |
thread_id: Uuid, |
| 91 |
author_id: Uuid, |
| 92 |
external_ref: &str, |
| 93 |
body_markdown: &str, |
| 94 |
body_html: &str, |
| 95 |
) -> Result<(Uuid, bool), sqlx::Error> { |
| 96 |
let mut tx = pool.begin().await?; |
| 97 |
|
| 98 |
let inserted = sqlx::query_scalar!( |
| 99 |
"INSERT INTO posts (thread_id, author_id, body_markdown, body_html, external_ref) |
| 100 |
VALUES ($1, $2, $3, $4, $5) |
| 101 |
ON CONFLICT (external_ref) WHERE external_ref IS NOT NULL DO NOTHING |
| 102 |
RETURNING id", |
| 103 |
thread_id, |
| 104 |
author_id, |
| 105 |
body_markdown, |
| 106 |
body_html, |
| 107 |
external_ref, |
| 108 |
) |
| 109 |
.fetch_optional(&mut *tx) |
| 110 |
.await?; |
| 111 |
|
| 112 |
let result = match inserted { |
| 113 |
Some(id) => { |
| 114 |
|
| 115 |
sqlx::query!( |
| 116 |
"UPDATE threads SET last_activity_at = now() WHERE id = $1", |
| 117 |
thread_id |
| 118 |
) |
| 119 |
.execute(&mut *tx) |
| 120 |
.await?; |
| 121 |
(id, true) |
| 122 |
} |
| 123 |
None => { |
| 124 |
|
| 125 |
let existing = |
| 126 |
sqlx::query_scalar!("SELECT id FROM posts WHERE external_ref = $1", external_ref,) |
| 127 |
.fetch_one(&mut *tx) |
| 128 |
.await?; |
| 129 |
(existing, false) |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
tx.commit().await?; |
| 134 |
Ok(result) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#[cfg(feature = "test-support")] |
| 145 |
#[tracing::instrument(skip_all)] |
| 146 |
pub async fn mod_remove_post( |
| 147 |
pool: &PgPool, |
| 148 |
post_id: Uuid, |
| 149 |
removed_by_id: Uuid, |
| 150 |
) -> Result<bool, sqlx::Error> { |
| 151 |
let result = sqlx::query!( |
| 152 |
"UPDATE posts SET removed_by = $2, removed_at = now() |
| 153 |
WHERE id = $1 AND removed_at IS NULL", |
| 154 |
post_id, |
| 155 |
removed_by_id, |
| 156 |
) |
| 157 |
.execute(pool) |
| 158 |
.await?; |
| 159 |
Ok(result.rows_affected() > 0) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 164 |
pub struct PostRemoval { |
| 165 |
|
| 166 |
pub post_removed: bool, |
| 167 |
|
| 168 |
|
| 169 |
pub thread_removed: bool, |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#[tracing::instrument(skip_all)] |
| 182 |
pub async fn mod_remove_post_cascade( |
| 183 |
conn: &mut sqlx::PgConnection, |
| 184 |
post_id: Uuid, |
| 185 |
removed_by_id: Uuid, |
| 186 |
) -> Result<PostRemoval, sqlx::Error> { |
| 187 |
let post_removed = sqlx::query!( |
| 188 |
"UPDATE posts SET removed_by = $2, removed_at = now() |
| 189 |
WHERE id = $1 AND removed_at IS NULL", |
| 190 |
post_id, |
| 191 |
removed_by_id, |
| 192 |
) |
| 193 |
.execute(&mut *conn) |
| 194 |
.await? |
| 195 |
.rows_affected() |
| 196 |
> 0; |
| 197 |
|
| 198 |
let mut thread_removed = false; |
| 199 |
if post_removed { |
| 200 |
|
| 201 |
thread_removed = sqlx::query!( |
| 202 |
"UPDATE threads SET deleted_at = now() |
| 203 |
WHERE deleted_at IS NULL |
| 204 |
AND id = (SELECT thread_id FROM posts WHERE id = $1) |
| 205 |
AND $1 = ( |
| 206 |
SELECT id FROM posts |
| 207 |
WHERE thread_id = (SELECT thread_id FROM posts WHERE id = $1) |
| 208 |
ORDER BY created_at ASC, id ASC |
| 209 |
LIMIT 1 |
| 210 |
)", |
| 211 |
post_id, |
| 212 |
) |
| 213 |
.execute(&mut *conn) |
| 214 |
.await? |
| 215 |
.rows_affected() |
| 216 |
> 0; |
| 217 |
} |
| 218 |
|
| 219 |
Ok(PostRemoval { |
| 220 |
post_removed, |
| 221 |
thread_removed, |
| 222 |
}) |
| 223 |
} |
| 224 |
|