| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use super::{PgPool, Uuid}; |
| 14 |
|
| 15 |
|
| 16 |
pub struct InsertedChatMessage { |
| 17 |
pub id: i64, |
| 18 |
|
| 19 |
pub created_at: i64, |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[tracing::instrument(skip_all)] |
| 27 |
pub async fn insert_chat_message( |
| 28 |
pool: &PgPool, |
| 29 |
community_id: Uuid, |
| 30 |
author_id: Uuid, |
| 31 |
body_html: &str, |
| 32 |
retention_hours: i32, |
| 33 |
) -> Result<InsertedChatMessage, sqlx::Error> { |
| 34 |
sqlx::query_as!( |
| 35 |
InsertedChatMessage, |
| 36 |
r#"INSERT INTO chat_messages (community_id, author_id, body_html, expires_at) |
| 37 |
VALUES ($1, $2, $3, now() + make_interval(hours => $4)) |
| 38 |
RETURNING id, EXTRACT(EPOCH FROM created_at)::BIGINT AS "created_at!""#, |
| 39 |
community_id, |
| 40 |
author_id, |
| 41 |
body_html, |
| 42 |
retention_hours, |
| 43 |
) |
| 44 |
.fetch_one(pool) |
| 45 |
.await |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#[tracing::instrument(skip_all)] |
| 51 |
pub async fn delete_chat_message<'e, E: sqlx::PgExecutor<'e>>( |
| 52 |
executor: E, |
| 53 |
community_id: Uuid, |
| 54 |
message_id: i64, |
| 55 |
) -> Result<u64, sqlx::Error> { |
| 56 |
let result = sqlx::query!( |
| 57 |
"DELETE FROM chat_messages WHERE community_id = $1 AND id = $2", |
| 58 |
community_id, |
| 59 |
message_id, |
| 60 |
) |
| 61 |
.execute(executor) |
| 62 |
.await?; |
| 63 |
Ok(result.rows_affected()) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[tracing::instrument(skip_all)] |
| 73 |
pub async fn purge_author_messages<'e, E: sqlx::PgExecutor<'e>>( |
| 74 |
executor: E, |
| 75 |
community_id: Uuid, |
| 76 |
author_id: Uuid, |
| 77 |
) -> Result<u64, sqlx::Error> { |
| 78 |
let result = sqlx::query!( |
| 79 |
"DELETE FROM chat_messages WHERE community_id = $1 AND author_id = $2", |
| 80 |
community_id, |
| 81 |
author_id, |
| 82 |
) |
| 83 |
.execute(executor) |
| 84 |
.await?; |
| 85 |
Ok(result.rows_affected()) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#[tracing::instrument(skip_all)] |
| 94 |
pub async fn sweep_expired_chat_messages(pool: &PgPool) -> Result<u64, sqlx::Error> { |
| 95 |
let result = sqlx::query!("DELETE FROM chat_messages WHERE expires_at < now()") |
| 96 |
.execute(pool) |
| 97 |
.await?; |
| 98 |
Ok(result.rows_affected()) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#[tracing::instrument(skip_all)] |
| 112 |
pub async fn trim_chat_rooms_to_cap(pool: &PgPool) -> Result<u64, sqlx::Error> { |
| 113 |
let result = sqlx::query!( |
| 114 |
"DELETE FROM chat_messages c |
| 115 |
USING ( |
| 116 |
SELECT ranked.id |
| 117 |
FROM ( |
| 118 |
SELECT id, |
| 119 |
community_id, |
| 120 |
row_number() OVER ( |
| 121 |
PARTITION BY community_id ORDER BY id DESC |
| 122 |
) AS rn |
| 123 |
FROM chat_messages |
| 124 |
) ranked |
| 125 |
JOIN communities co ON co.id = ranked.community_id |
| 126 |
WHERE ranked.rn > co.chat_max_messages |
| 127 |
) over_cap |
| 128 |
WHERE c.id = over_cap.id" |
| 129 |
) |
| 130 |
.execute(pool) |
| 131 |
.await?; |
| 132 |
Ok(result.rows_affected()) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[tracing::instrument(skip_all)] |
| 146 |
pub async fn recompute_chat_expiry( |
| 147 |
pool: &PgPool, |
| 148 |
community_id: Uuid, |
| 149 |
retention_hours: i32, |
| 150 |
) -> Result<u64, sqlx::Error> { |
| 151 |
let result = sqlx::query!( |
| 152 |
"UPDATE chat_messages |
| 153 |
SET expires_at = created_at + make_interval(hours => $2) |
| 154 |
WHERE community_id = $1", |
| 155 |
community_id, |
| 156 |
retention_hours, |
| 157 |
) |
| 158 |
.execute(pool) |
| 159 |
.await?; |
| 160 |
Ok(result.rows_affected()) |
| 161 |
} |
| 162 |
|