| 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 |
|
| 16 |
use crate::error::ChatError; |
| 17 |
use crate::ids::{MessageId, RoomId, UserId}; |
| 18 |
use crate::room::Room; |
| 19 |
|
| 20 |
|
| 21 |
#[async_trait] |
| 22 |
pub trait ChatRooms: Send + Sync { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
async fn resolve(&self, key: &str) -> Result<Option<Room>, ChatError>; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 35 |
pub enum DenyReason { |
| 36 |
|
| 37 |
Anonymous, |
| 38 |
|
| 39 |
NotAMember, |
| 40 |
|
| 41 |
Banned, |
| 42 |
|
| 43 |
Muted, |
| 44 |
|
| 45 |
Suspended, |
| 46 |
|
| 47 |
TierRequired, |
| 48 |
|
| 49 |
RateLimited { retry_after: Duration }, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 54 |
pub enum WriteAccess { |
| 55 |
Allow, |
| 56 |
Deny(DenyReason), |
| 57 |
} |
| 58 |
|
| 59 |
impl WriteAccess { |
| 60 |
pub fn is_allowed(&self) -> bool { |
| 61 |
matches!(self, Self::Allow) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
#[async_trait] |
| 67 |
pub trait ChatAuthz: Send + Sync { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
async fn can_read(&self, viewer: Option<UserId>, room: &Room) -> Result<bool, ChatError>; |
| 73 |
|
| 74 |
|
| 75 |
async fn can_write(&self, user: UserId, room: &Room) -> Result<WriteAccess, ChatError>; |
| 76 |
|
| 77 |
|
| 78 |
async fn is_moderator(&self, user: UserId, room: &Room) -> Result<bool, ChatError>; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 83 |
pub struct Identity { |
| 84 |
pub display_name: String, |
| 85 |
pub avatar_url: Option<String>, |
| 86 |
|
| 87 |
pub flair: Option<String>, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#[async_trait] |
| 92 |
pub trait ChatIdentity: Send + Sync { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
async fn identify(&self, users: &[UserId]) -> Result<HashMap<UserId, Identity>, ChatError>; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#[async_trait] |
| 115 |
pub trait ChatModeration: Send + Sync { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
async fn delete_message( |
| 120 |
&self, |
| 121 |
actor: UserId, |
| 122 |
room: &Room, |
| 123 |
message: MessageId, |
| 124 |
) -> Result<(), ChatError>; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
async fn purge_user( |
| 133 |
&self, |
| 134 |
actor: UserId, |
| 135 |
room: &Room, |
| 136 |
target: UserId, |
| 137 |
) -> Result<u64, ChatError>; |
| 138 |
|
| 139 |
|
| 140 |
async fn timeout_user( |
| 141 |
&self, |
| 142 |
actor: UserId, |
| 143 |
room: &Room, |
| 144 |
target: UserId, |
| 145 |
duration: Duration, |
| 146 |
) -> Result<(), ChatError>; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
async fn ban_user( |
| 153 |
&self, |
| 154 |
actor: UserId, |
| 155 |
room: &Room, |
| 156 |
target: UserId, |
| 157 |
reason: Option<&str>, |
| 158 |
) -> Result<(), ChatError>; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
async fn log_action( |
| 165 |
&self, |
| 166 |
actor: UserId, |
| 167 |
room_id: RoomId, |
| 168 |
action: &str, |
| 169 |
detail: Option<&str>, |
| 170 |
) -> Result<(), ChatError>; |
| 171 |
} |
| 172 |
|
| 173 |
#[cfg(test)] |
| 174 |
mod tests { |
| 175 |
use super::*; |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn only_allow_is_allowed() { |
| 179 |
assert!(WriteAccess::Allow.is_allowed()); |
| 180 |
assert!(!WriteAccess::Deny(DenyReason::Muted).is_allowed()); |
| 181 |
assert!( |
| 182 |
!WriteAccess::Deny(DenyReason::RateLimited { |
| 183 |
retry_after: Duration::from_secs(3) |
| 184 |
}) |
| 185 |
.is_allowed() |
| 186 |
); |
| 187 |
} |
| 188 |
} |
| 189 |
|