| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use std::collections::HashMap; |
| 12 |
use std::time::Duration; |
| 13 |
|
| 14 |
use async_trait::async_trait; |
| 15 |
use serde::{Deserialize, Serialize}; |
| 16 |
|
| 17 |
use crate::error::ChatError; |
| 18 |
use crate::ids::{MessageId, RoomId, UserId}; |
| 19 |
use crate::message::Message; |
| 20 |
use crate::room::Room; |
| 21 |
|
| 22 |
|
| 23 |
#[async_trait] |
| 24 |
pub trait ChatRooms: Send + Sync { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
async fn resolve(&self, key: &str) -> Result<Option<Room>, ChatError>; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 37 |
pub enum DenyReason { |
| 38 |
|
| 39 |
Anonymous, |
| 40 |
|
| 41 |
NotAMember, |
| 42 |
|
| 43 |
Banned, |
| 44 |
|
| 45 |
Muted, |
| 46 |
|
| 47 |
Suspended, |
| 48 |
|
| 49 |
TierRequired, |
| 50 |
|
| 51 |
RateLimited { retry_after: Duration }, |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 56 |
pub enum WriteAccess { |
| 57 |
Allow, |
| 58 |
Deny(DenyReason), |
| 59 |
} |
| 60 |
|
| 61 |
impl WriteAccess { |
| 62 |
pub fn is_allowed(&self) -> bool { |
| 63 |
matches!(self, Self::Allow) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
#[async_trait] |
| 69 |
pub trait ChatAuthz: Send + Sync { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
async fn can_read(&self, viewer: Option<UserId>, room: &Room) -> Result<bool, ChatError>; |
| 75 |
|
| 76 |
|
| 77 |
async fn can_write(&self, user: UserId, room: &Room) -> Result<WriteAccess, ChatError>; |
| 78 |
|
| 79 |
|
| 80 |
async fn is_moderator(&self, user: UserId, room: &Room) -> Result<bool, ChatError>; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 88 |
pub struct Identity { |
| 89 |
pub display_name: String, |
| 90 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 91 |
pub avatar_url: Option<String>, |
| 92 |
|
| 93 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 94 |
pub flair: Option<String>, |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
#[async_trait] |
| 99 |
pub trait ChatIdentity: Send + Sync { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
async fn identify(&self, users: &[UserId]) -> Result<HashMap<UserId, Identity>, ChatError>; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
async fn attach(&self, messages: &mut [Message]) -> Result<(), ChatError> { |
| 123 |
if messages.is_empty() { |
| 124 |
return Ok(()); |
| 125 |
} |
| 126 |
|
| 127 |
let mut distinct: Vec<UserId> = messages.iter().map(|m| m.author_id).collect(); |
| 128 |
distinct.sort_unstable_by_key(|u| u.0); |
| 129 |
distinct.dedup(); |
| 130 |
|
| 131 |
let identities = self.identify(&distinct).await?; |
| 132 |
for message in messages { |
| 133 |
message.author = identities.get(&message.author_id).cloned(); |
| 134 |
} |
| 135 |
Ok(()) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[async_trait] |
| 149 |
pub trait ChatModeration: Send + Sync { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
async fn delete_message( |
| 154 |
&self, |
| 155 |
actor: UserId, |
| 156 |
room: &Room, |
| 157 |
message: MessageId, |
| 158 |
) -> Result<(), ChatError>; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
async fn purge_user( |
| 167 |
&self, |
| 168 |
actor: UserId, |
| 169 |
room: &Room, |
| 170 |
target: UserId, |
| 171 |
) -> Result<u64, ChatError>; |
| 172 |
|
| 173 |
|
| 174 |
async fn timeout_user( |
| 175 |
&self, |
| 176 |
actor: UserId, |
| 177 |
room: &Room, |
| 178 |
target: UserId, |
| 179 |
duration: Duration, |
| 180 |
) -> Result<(), ChatError>; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
async fn ban_user( |
| 187 |
&self, |
| 188 |
actor: UserId, |
| 189 |
room: &Room, |
| 190 |
target: UserId, |
| 191 |
reason: Option<&str>, |
| 192 |
) -> Result<(), ChatError>; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
async fn log_action( |
| 199 |
&self, |
| 200 |
actor: UserId, |
| 201 |
room_id: RoomId, |
| 202 |
action: &str, |
| 203 |
detail: Option<&str>, |
| 204 |
) -> Result<(), ChatError>; |
| 205 |
} |
| 206 |
|
| 207 |
#[cfg(test)] |
| 208 |
mod tests { |
| 209 |
use super::*; |
| 210 |
use crate::ids::MessageId; |
| 211 |
use std::sync::Mutex; |
| 212 |
use uuid::Uuid; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#[derive(Default)] |
| 217 |
struct Directory { |
| 218 |
batches: Mutex<Vec<Vec<UserId>>>, |
| 219 |
unknown: Option<UserId>, |
| 220 |
} |
| 221 |
|
| 222 |
#[async_trait] |
| 223 |
impl ChatIdentity for Directory { |
| 224 |
async fn identify(&self, users: &[UserId]) -> Result<HashMap<UserId, Identity>, ChatError> { |
| 225 |
self.batches.lock().unwrap().push(users.to_vec()); |
| 226 |
Ok(users |
| 227 |
.iter() |
| 228 |
.filter(|u| Some(**u) != self.unknown) |
| 229 |
.map(|u| { |
| 230 |
( |
| 231 |
*u, |
| 232 |
Identity { |
| 233 |
display_name: u.to_string(), |
| 234 |
avatar_url: None, |
| 235 |
flair: None, |
| 236 |
}, |
| 237 |
) |
| 238 |
}) |
| 239 |
.collect()) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
fn message(id: i64, author: UserId) -> Message { |
| 244 |
Message { |
| 245 |
id: MessageId(id), |
| 246 |
room_id: crate::ids::RoomId(Uuid::nil()), |
| 247 |
author_id: author, |
| 248 |
body_html: "hi".into(), |
| 249 |
created_at: 0, |
| 250 |
nonce: None, |
| 251 |
author: None, |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
#[tokio::test] |
| 256 |
async fn attach_resolves_every_author() { |
| 257 |
let (a, b) = (UserId(Uuid::new_v4()), UserId(Uuid::new_v4())); |
| 258 |
let mut messages = vec![message(1, a), message(2, b)]; |
| 259 |
|
| 260 |
Directory::default().attach(&mut messages).await.unwrap(); |
| 261 |
|
| 262 |
assert_eq!( |
| 263 |
messages[0].author.as_ref().unwrap().display_name, |
| 264 |
a.to_string() |
| 265 |
); |
| 266 |
assert_eq!( |
| 267 |
messages[1].author.as_ref().unwrap().display_name, |
| 268 |
b.to_string() |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
#[tokio::test] |
| 273 |
async fn attach_asks_once_per_distinct_author() { |
| 274 |
|
| 275 |
|
| 276 |
let loud = UserId(Uuid::new_v4()); |
| 277 |
let mut messages: Vec<_> = (1..=50).map(|i| message(i, loud)).collect(); |
| 278 |
|
| 279 |
let directory = Directory::default(); |
| 280 |
directory.attach(&mut messages).await.unwrap(); |
| 281 |
|
| 282 |
let batches = directory.batches.lock().unwrap(); |
| 283 |
assert_eq!(batches.len(), 1, "one call for the whole backlog"); |
| 284 |
assert_eq!(batches[0], vec![loud], "deduplicated to one id"); |
| 285 |
assert!(messages.iter().all(|m| m.author.is_some())); |
| 286 |
} |
| 287 |
|
| 288 |
#[tokio::test] |
| 289 |
async fn an_unresolvable_author_leaves_the_message_readable() { |
| 290 |
|
| 291 |
let (gone, present) = (UserId(Uuid::new_v4()), UserId(Uuid::new_v4())); |
| 292 |
let mut messages = vec![message(1, gone), message(2, present)]; |
| 293 |
|
| 294 |
Directory { |
| 295 |
unknown: Some(gone), |
| 296 |
..Default::default() |
| 297 |
} |
| 298 |
.attach(&mut messages) |
| 299 |
.await |
| 300 |
.unwrap(); |
| 301 |
|
| 302 |
assert!(messages[0].author.is_none()); |
| 303 |
assert_eq!(messages[0].body_html, "hi", "the message itself survives"); |
| 304 |
assert!(messages[1].author.is_some()); |
| 305 |
} |
| 306 |
|
| 307 |
#[tokio::test] |
| 308 |
async fn attach_on_an_empty_batch_queries_nothing() { |
| 309 |
let directory = Directory::default(); |
| 310 |
directory.attach(&mut []).await.unwrap(); |
| 311 |
assert!(directory.batches.lock().unwrap().is_empty()); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn only_allow_is_allowed() { |
| 316 |
assert!(WriteAccess::Allow.is_allowed()); |
| 317 |
assert!(!WriteAccess::Deny(DenyReason::Muted).is_allowed()); |
| 318 |
assert!( |
| 319 |
!WriteAccess::Deny(DenyReason::RateLimited { |
| 320 |
retry_after: Duration::from_secs(3) |
| 321 |
}) |
| 322 |
.is_allowed() |
| 323 |
); |
| 324 |
} |
| 325 |
} |
| 326 |
|