| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use async_trait::async_trait; |
| 8 |
use livechat::{ChatError, ChatRooms, Retention, Room, RoomId, RoomState}; |
| 9 |
use mt_core::types::{ChatPolicy, CommunityState}; |
| 10 |
use sqlx::PgPool; |
| 11 |
|
| 12 |
use super::host_error; |
| 13 |
|
| 14 |
pub struct MtChatRooms { |
| 15 |
db: PgPool, |
| 16 |
} |
| 17 |
|
| 18 |
impl MtChatRooms { |
| 19 |
pub fn new(db: PgPool) -> Self { |
| 20 |
Self { db } |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
pub(crate) fn room_state(policy: ChatPolicy, state: CommunityState, suspended: bool) -> RoomState { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
if !policy.is_enabled() { |
| 34 |
return RoomState::Closed; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
if suspended { |
| 41 |
return RoomState::Closed; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if state.allows_writes_for_members() { |
| 49 |
RoomState::Open |
| 50 |
} else { |
| 51 |
RoomState::ReadOnly |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
fn retention(hours: i32, max_messages: i32) -> Result<Retention, ChatError> { |
| 62 |
Retention::new( |
| 63 |
std::time::Duration::from_hours(u64::try_from(hours).unwrap_or(0)), |
| 64 |
usize::try_from(max_messages).unwrap_or(0), |
| 65 |
) |
| 66 |
} |
| 67 |
|
| 68 |
#[async_trait] |
| 69 |
impl ChatRooms for MtChatRooms { |
| 70 |
async fn resolve(&self, key: &str) -> Result<Option<Room>, ChatError> { |
| 71 |
let Some(row) = mt_db::queries::get_chat_room_by_slug(&self.db, key) |
| 72 |
.await |
| 73 |
.map_err(host_error)? |
| 74 |
else { |
| 75 |
return Ok(None); |
| 76 |
}; |
| 77 |
|
| 78 |
Ok(Some(Room { |
| 79 |
id: RoomId(row.id), |
| 80 |
state: room_state(row.policy, row.state, row.suspended), |
| 81 |
retention: retention(row.retention_hours, row.max_messages)?, |
| 82 |
})) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[cfg(test)] |
| 87 |
mod tests { |
| 88 |
use super::*; |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn off_closes_the_room_whatever_else_is_true() { |
| 92 |
for state in [ |
| 93 |
CommunityState::Active, |
| 94 |
CommunityState::Restricted, |
| 95 |
CommunityState::Frozen, |
| 96 |
CommunityState::Archived, |
| 97 |
] { |
| 98 |
for suspended in [false, true] { |
| 99 |
assert_eq!( |
| 100 |
room_state(ChatPolicy::Off, state, suspended), |
| 101 |
RoomState::Closed, |
| 102 |
"off must be total: state={state:?} suspended={suspended}" |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn suspension_closes_rather_than_quietens() { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
assert_eq!( |
| 114 |
room_state(ChatPolicy::Members, CommunityState::Frozen, true), |
| 115 |
RoomState::Closed |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn frozen_and_archived_are_read_only() { |
| 121 |
for state in [CommunityState::Frozen, CommunityState::Archived] { |
| 122 |
assert_eq!( |
| 123 |
room_state(ChatPolicy::Members, state, false), |
| 124 |
RoomState::ReadOnly, |
| 125 |
"{state:?} must match how threads behave" |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn active_and_restricted_are_open() { |
| 132 |
|
| 133 |
|
| 134 |
for state in [CommunityState::Active, CommunityState::Restricted] { |
| 135 |
assert_eq!( |
| 136 |
room_state(ChatPolicy::Members, state, false), |
| 137 |
RoomState::Open |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn every_enabled_policy_resolves_the_same_state() { |
| 144 |
|
| 145 |
|
| 146 |
for policy in [ |
| 147 |
ChatPolicy::Members, |
| 148 |
ChatPolicy::PublicRead, |
| 149 |
ChatPolicy::FanPlus, |
| 150 |
] { |
| 151 |
assert_eq!( |
| 152 |
room_state(policy, CommunityState::Active, false), |
| 153 |
RoomState::Open |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn the_column_defaults_build_a_valid_retention() { |
| 160 |
|
| 161 |
|
| 162 |
let r = retention(168, 5_000).expect("forum defaults"); |
| 163 |
assert_eq!(r, Retention::forum_default()); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn retention_past_the_ceiling_is_refused_not_clamped() { |
| 168 |
assert!(retention(721, 5_000).is_err()); |
| 169 |
assert!(retention(168, 20_001).is_err()); |
| 170 |
assert!(retention(0, 5_000).is_err()); |
| 171 |
} |
| 172 |
} |
| 173 |
|