| 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 |
impl MtChatModeration { |
| 48 |
pub fn new(db: PgPool, actor_is_moderator: bool) -> Self { |
| 49 |
Self { |
| 50 |
db, |
| 51 |
actor_is_moderator, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
fn require_moderator(&self) -> Result<(), ChatError> { |
| 57 |
if self.actor_is_moderator { |
| 58 |
return Ok(()); |
| 59 |
} |
| 60 |
Err(ChatError::host(std::io::Error::other( |
| 61 |
"chat moderation attempted without moderator powers", |
| 62 |
))) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[async_trait] |
| 67 |
impl ChatModeration for MtChatModeration { |
| 68 |
async fn delete_message( |
| 69 |
&self, |
| 70 |
actor: UserId, |
| 71 |
room: &Room, |
| 72 |
message: MessageId, |
| 73 |
) -> Result<(), ChatError> { |
| 74 |
|
| 75 |
|
| 76 |
let author = mt_db::queries::chat_message_author(&self.db, room.id.0, message.0) |
| 77 |
.await |
| 78 |
.map_err(host_error)?; |
| 79 |
|
| 80 |
let Some(author) = author else { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
return Ok(()); |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
if author != actor.0 && !self.actor_is_moderator { |
| 89 |
return Err(ChatError::host(std::io::Error::other( |
| 90 |
"cannot delete another user's chat message", |
| 91 |
))); |
| 92 |
} |
| 93 |
|
| 94 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 95 |
|
| 96 |
mt_db::mutations::delete_chat_message(&mut *tx, room.id.0, message.0) |
| 97 |
.await |
| 98 |
.map_err(host_error)?; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if self.actor_is_moderator && author != actor.0 { |
| 104 |
mt_db::mutations::insert_mod_log( |
| 105 |
&mut *tx, |
| 106 |
Some(room.id.0), |
| 107 |
ModActor::User(actor.0), |
| 108 |
ModAction::ChatDeleteMessage, |
| 109 |
Some(author), |
| 110 |
|
| 111 |
|
| 112 |
None, |
| 113 |
Some(&format!("chat message {}", message.0)), |
| 114 |
) |
| 115 |
.await |
| 116 |
.map_err(host_error)?; |
| 117 |
} |
| 118 |
|
| 119 |
tx.commit().await.map_err(host_error)?; |
| 120 |
Ok(()) |
| 121 |
} |
| 122 |
|
| 123 |
async fn purge_user( |
| 124 |
&self, |
| 125 |
actor: UserId, |
| 126 |
room: &Room, |
| 127 |
target: UserId, |
| 128 |
) -> Result<u64, ChatError> { |
| 129 |
self.require_moderator()?; |
| 130 |
|
| 131 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 132 |
|
| 133 |
let removed = mt_db::mutations::purge_author_messages(&mut *tx, room.id.0, target.0) |
| 134 |
.await |
| 135 |
.map_err(host_error)?; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
mt_db::mutations::insert_mod_log( |
| 142 |
&mut *tx, |
| 143 |
Some(room.id.0), |
| 144 |
ModActor::User(actor.0), |
| 145 |
ModAction::ChatPurge, |
| 146 |
Some(target.0), |
| 147 |
None, |
| 148 |
Some(&format!("{removed} chat messages")), |
| 149 |
) |
| 150 |
.await |
| 151 |
.map_err(host_error)?; |
| 152 |
|
| 153 |
tx.commit().await.map_err(host_error)?; |
| 154 |
Ok(removed) |
| 155 |
} |
| 156 |
|
| 157 |
async fn timeout_user( |
| 158 |
&self, |
| 159 |
actor: UserId, |
| 160 |
room: &Room, |
| 161 |
target: UserId, |
| 162 |
duration: Duration, |
| 163 |
) -> Result<(), ChatError> { |
| 164 |
self.require_moderator()?; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
let expires_at = |
| 172 |
chrono::Utc::now() + chrono::Duration::from_std(duration).map_err(host_error)?; |
| 173 |
|
| 174 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 175 |
|
| 176 |
mt_db::mutations::create_community_ban( |
| 177 |
&mut *tx, |
| 178 |
room.id.0, |
| 179 |
target.0, |
| 180 |
actor.0, |
| 181 |
BanType::Mute, |
| 182 |
Some("chat timeout"), |
| 183 |
Some(expires_at), |
| 184 |
) |
| 185 |
.await |
| 186 |
.map_err(host_error)?; |
| 187 |
|
| 188 |
mt_db::mutations::insert_mod_log( |
| 189 |
&mut *tx, |
| 190 |
Some(room.id.0), |
| 191 |
ModActor::User(actor.0), |
| 192 |
ModAction::ChatTimeout, |
| 193 |
Some(target.0), |
| 194 |
None, |
| 195 |
Some(&format!("{} seconds", duration.as_secs())), |
| 196 |
) |
| 197 |
.await |
| 198 |
.map_err(host_error)?; |
| 199 |
|
| 200 |
tx.commit().await.map_err(host_error)?; |
| 201 |
Ok(()) |
| 202 |
} |
| 203 |
|
| 204 |
async fn ban_user( |
| 205 |
&self, |
| 206 |
actor: UserId, |
| 207 |
room: &Room, |
| 208 |
target: UserId, |
| 209 |
reason: Option<&str>, |
| 210 |
) -> Result<(), ChatError> { |
| 211 |
self.require_moderator()?; |
| 212 |
|
| 213 |
let mut tx = self.db.begin().await.map_err(host_error)?; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
mt_db::mutations::create_community_ban( |
| 218 |
&mut *tx, |
| 219 |
room.id.0, |
| 220 |
target.0, |
| 221 |
actor.0, |
| 222 |
BanType::Ban, |
| 223 |
reason, |
| 224 |
None, |
| 225 |
) |
| 226 |
.await |
| 227 |
.map_err(host_error)?; |
| 228 |
|
| 229 |
mt_db::mutations::insert_mod_log( |
| 230 |
&mut *tx, |
| 231 |
Some(room.id.0), |
| 232 |
ModActor::User(actor.0), |
| 233 |
ModAction::ChatBan, |
| 234 |
Some(target.0), |
| 235 |
None, |
| 236 |
reason, |
| 237 |
) |
| 238 |
.await |
| 239 |
.map_err(host_error)?; |
| 240 |
|
| 241 |
tx.commit().await.map_err(host_error)?; |
| 242 |
Ok(()) |
| 243 |
} |
| 244 |
|
| 245 |
async fn log_action( |
| 246 |
&self, |
| 247 |
actor: UserId, |
| 248 |
room_id: RoomId, |
| 249 |
action: &str, |
| 250 |
detail: Option<&str>, |
| 251 |
) -> Result<(), ChatError> { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
let mapped = match action { |
| 258 |
"delete" | "chat_delete_message" => ModAction::ChatDeleteMessage, |
| 259 |
"timeout" | "chat_timeout" => ModAction::ChatTimeout, |
| 260 |
"ban" | "chat_ban" => ModAction::ChatBan, |
| 261 |
"purge" | "chat_purge" => ModAction::ChatPurge, |
| 262 |
other => { |
| 263 |
tracing::warn!( |
| 264 |
action = other, |
| 265 |
"unmapped chat mod action; logging as delete" |
| 266 |
); |
| 267 |
ModAction::ChatDeleteMessage |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
mt_db::mutations::insert_mod_log( |
| 272 |
&self.db, |
| 273 |
Some(room_id.0), |
| 274 |
ModActor::User(actor.0), |
| 275 |
mapped, |
| 276 |
None, |
| 277 |
None, |
| 278 |
detail, |
| 279 |
) |
| 280 |
.await |
| 281 |
.map_err(host_error) |
| 282 |
} |
| 283 |
} |
| 284 |
|