| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use std::time::Duration; |
| 28 |
|
| 29 |
use async_trait::async_trait; |
| 30 |
use livechat::{ChatError, ChatModeration, MessageId, Room, RoomId, UserId}; |
| 31 |
use mt_core::types::{BanType, ModAction, ModActor}; |
| 32 |
use sqlx::PgPool; |
| 33 |
|
| 34 |
use super::host_error; |
| 35 |
|
| 36 |
pub struct MtChatModeration { |
| 37 |
db: PgPool, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
actor_is_moderator: bool, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
actor_is_owner: bool, |
| 53 |
} |
| 54 |
|
| 55 |
impl MtChatModeration { |
| 56 |
pub fn new(db: PgPool, actor_is_moderator: bool) -> Self { |
| 57 |
Self { |
| 58 |
db, |
| 59 |
actor_is_moderator, |
| 60 |
actor_is_owner: false, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub fn for_owner(db: PgPool) -> Self { |
| 70 |
Self { |
| 71 |
db, |
| 72 |
actor_is_moderator: true, |
| 73 |
actor_is_owner: true, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
fn require_moderator(&self) -> Result<(), ChatError> { |
| 79 |
if self.actor_is_moderator { |
| 80 |
return Ok(()); |
| 81 |
} |
| 82 |
Err(ChatError::host(std::io::Error::other( |
| 83 |
"chat moderation attempted without moderator powers", |
| 84 |
))) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[async_trait] |
| 89 |
impl ChatModeration for MtChatModeration { |
| 90 |
async fn delete_message( |
| 91 |
&self, |
| 92 |
actor: UserId, |
| 93 |
room: &Room, |
| 94 |
message: MessageId, |
| 95 |
) -> Result<(), ChatError> { |
| 96 |
|
| 97 |
|
| 98 |
let author = mt_db::queries::chat_message_author(&self.db, room.id.0, message.0) |
| 99 |
.await |
| 100 |
.map_err(host_error)?; |
| 101 |
|
| 102 |
let Some(author) = author else { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
return Ok(()); |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
if author != actor.0 && !self.actor_is_moderator { |
| 111 |
return Err(ChatError::host(std::io::Error::other( |
| 112 |
"cannot delete another user's chat message", |
| 113 |
))); |
| 114 |
} |
| 115 |
|
| 116 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 117 |
|
| 118 |
mt_db::mutations::delete_chat_message(&mut *tx, room.id.0, message.0) |
| 119 |
.await |
| 120 |
.map_err(host_error)?; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if self.actor_is_moderator && author != actor.0 { |
| 126 |
mt_db::mutations::insert_mod_log( |
| 127 |
&mut *tx, |
| 128 |
Some(room.id.0), |
| 129 |
ModActor::User(actor.0), |
| 130 |
ModAction::ChatDeleteMessage, |
| 131 |
Some(author), |
| 132 |
|
| 133 |
|
| 134 |
None, |
| 135 |
Some(&format!("chat message {}", message.0)), |
| 136 |
) |
| 137 |
.await |
| 138 |
.map_err(host_error)?; |
| 139 |
} |
| 140 |
|
| 141 |
tx.commit().await.map_err(host_error)?; |
| 142 |
Ok(()) |
| 143 |
} |
| 144 |
|
| 145 |
async fn purge_user( |
| 146 |
&self, |
| 147 |
actor: UserId, |
| 148 |
room: &Room, |
| 149 |
target: UserId, |
| 150 |
) -> Result<u64, ChatError> { |
| 151 |
self.require_moderator()?; |
| 152 |
|
| 153 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 154 |
|
| 155 |
let removed = mt_db::mutations::purge_author_messages(&mut *tx, room.id.0, target.0) |
| 156 |
.await |
| 157 |
.map_err(host_error)?; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
mt_db::mutations::insert_mod_log( |
| 164 |
&mut *tx, |
| 165 |
Some(room.id.0), |
| 166 |
ModActor::User(actor.0), |
| 167 |
ModAction::ChatPurge, |
| 168 |
Some(target.0), |
| 169 |
None, |
| 170 |
Some(&format!("{removed} chat messages")), |
| 171 |
) |
| 172 |
.await |
| 173 |
.map_err(host_error)?; |
| 174 |
|
| 175 |
tx.commit().await.map_err(host_error)?; |
| 176 |
Ok(removed) |
| 177 |
} |
| 178 |
|
| 179 |
async fn wipe_room(&self, actor: UserId, room: &Room) -> Result<u64, ChatError> { |
| 180 |
if !self.actor_is_owner { |
| 181 |
return Err(ChatError::host(std::io::Error::other( |
| 182 |
"chat wipe attempted without ownership", |
| 183 |
))); |
| 184 |
} |
| 185 |
|
| 186 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 187 |
|
| 188 |
let removed = mt_db::mutations::wipe_chat_room(&mut *tx, room.id.0) |
| 189 |
.await |
| 190 |
.map_err(host_error)?; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
mt_db::mutations::insert_mod_log( |
| 197 |
&mut *tx, |
| 198 |
Some(room.id.0), |
| 199 |
ModActor::User(actor.0), |
| 200 |
ModAction::ChatWipe, |
| 201 |
None, |
| 202 |
None, |
| 203 |
Some(&format!("{removed} chat messages")), |
| 204 |
) |
| 205 |
.await |
| 206 |
.map_err(host_error)?; |
| 207 |
|
| 208 |
tx.commit().await.map_err(host_error)?; |
| 209 |
Ok(removed) |
| 210 |
} |
| 211 |
|
| 212 |
async fn timeout_user( |
| 213 |
&self, |
| 214 |
actor: UserId, |
| 215 |
room: &Room, |
| 216 |
target: UserId, |
| 217 |
duration: Duration, |
| 218 |
) -> Result<(), ChatError> { |
| 219 |
self.require_moderator()?; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
let expires_at = |
| 227 |
chrono::Utc::now() + chrono::Duration::from_std(duration).map_err(host_error)?; |
| 228 |
|
| 229 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 230 |
|
| 231 |
mt_db::mutations::create_community_ban( |
| 232 |
&mut *tx, |
| 233 |
room.id.0, |
| 234 |
target.0, |
| 235 |
actor.0, |
| 236 |
BanType::Mute, |
| 237 |
Some("chat timeout"), |
| 238 |
Some(expires_at), |
| 239 |
) |
| 240 |
.await |
| 241 |
.map_err(host_error)?; |
| 242 |
|
| 243 |
mt_db::mutations::insert_mod_log( |
| 244 |
&mut *tx, |
| 245 |
Some(room.id.0), |
| 246 |
ModActor::User(actor.0), |
| 247 |
ModAction::ChatTimeout, |
| 248 |
Some(target.0), |
| 249 |
None, |
| 250 |
Some(&format!("{} seconds", duration.as_secs())), |
| 251 |
) |
| 252 |
.await |
| 253 |
.map_err(host_error)?; |
| 254 |
|
| 255 |
tx.commit().await.map_err(host_error)?; |
| 256 |
Ok(()) |
| 257 |
} |
| 258 |
|
| 259 |
async fn ban_user( |
| 260 |
&self, |
| 261 |
actor: UserId, |
| 262 |
room: &Room, |
| 263 |
target: UserId, |
| 264 |
reason: Option<&str>, |
| 265 |
) -> Result<(), ChatError> { |
| 266 |
self.require_moderator()?; |
| 267 |
|
| 268 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
mt_db::mutations::create_community_ban( |
| 273 |
&mut *tx, |
| 274 |
room.id.0, |
| 275 |
target.0, |
| 276 |
actor.0, |
| 277 |
BanType::Ban, |
| 278 |
reason, |
| 279 |
None, |
| 280 |
) |
| 281 |
.await |
| 282 |
.map_err(host_error)?; |
| 283 |
|
| 284 |
mt_db::mutations::insert_mod_log( |
| 285 |
&mut *tx, |
| 286 |
Some(room.id.0), |
| 287 |
ModActor::User(actor.0), |
| 288 |
ModAction::ChatBan, |
| 289 |
Some(target.0), |
| 290 |
None, |
| 291 |
reason, |
| 292 |
) |
| 293 |
.await |
| 294 |
.map_err(host_error)?; |
| 295 |
|
| 296 |
tx.commit().await.map_err(host_error)?; |
| 297 |
Ok(()) |
| 298 |
} |
| 299 |
|
| 300 |
async fn log_action( |
| 301 |
&self, |
| 302 |
actor: UserId, |
| 303 |
room_id: RoomId, |
| 304 |
action: &str, |
| 305 |
detail: Option<&str>, |
| 306 |
) -> Result<(), ChatError> { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
let mapped = match action { |
| 313 |
"delete" | "chat_delete_message" => ModAction::ChatDeleteMessage, |
| 314 |
"timeout" | "chat_timeout" => ModAction::ChatTimeout, |
| 315 |
"ban" | "chat_ban" => ModAction::ChatBan, |
| 316 |
"purge" | "chat_purge" => ModAction::ChatPurge, |
| 317 |
"wipe" | "chat_wipe" => ModAction::ChatWipe, |
| 318 |
other => { |
| 319 |
tracing::warn!( |
| 320 |
action = other, |
| 321 |
"unmapped chat mod action; logging as delete" |
| 322 |
); |
| 323 |
ModAction::ChatDeleteMessage |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
mt_db::mutations::insert_mod_log( |
| 328 |
&self.db, |
| 329 |
Some(room_id.0), |
| 330 |
ModActor::User(actor.0), |
| 331 |
mapped, |
| 332 |
None, |
| 333 |
None, |
| 334 |
detail, |
| 335 |
) |
| 336 |
.await |
| 337 |
.map_err(host_error) |
| 338 |
} |
| 339 |
} |
| 340 |
|