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