Skip to main content

max / makenotwork

1.7 KB · 64 lines History Blame Raw
1 //! Room identity and lifecycle.
2
3 use crate::ids::RoomId;
4 use crate::retention::Retention;
5
6 /// Whether a room accepts reads and writes right now.
7 ///
8 /// The host maps its own state onto this. Multithreaded: an active community
9 /// with chat enabled is [`RoomState::Open`]; `Frozen` and `Archived` communities
10 /// are [`RoomState::ReadOnly`], matching how threads already behave; a community
11 /// with `chat_policy = off` is [`RoomState::Closed`].
12 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
13 pub enum RoomState {
14 /// Reads and writes, subject to per-user authz.
15 Open,
16 /// Reads only. The room and its backlog stay visible; nobody can send.
17 ReadOnly,
18 /// The room does not exist as far as the outside is concerned. Callers should
19 /// render a 404, not a disabled input: a closed room has no route, no hub
20 /// entry, and no UI affordance.
21 Closed,
22 }
23
24 impl RoomState {
25 pub fn allows_reads(&self) -> bool {
26 matches!(self, Self::Open | Self::ReadOnly)
27 }
28
29 pub fn allows_writes(&self) -> bool {
30 matches!(self, Self::Open)
31 }
32 }
33
34 /// A resolved room.
35 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
36 pub struct Room {
37 pub id: RoomId,
38 pub state: RoomState,
39 pub retention: Retention,
40 }
41
42 #[cfg(test)]
43 mod tests {
44 use super::*;
45
46 #[test]
47 fn closed_rooms_allow_nothing() {
48 assert!(!RoomState::Closed.allows_reads());
49 assert!(!RoomState::Closed.allows_writes());
50 }
51
52 #[test]
53 fn read_only_rooms_read_but_do_not_write() {
54 assert!(RoomState::ReadOnly.allows_reads());
55 assert!(!RoomState::ReadOnly.allows_writes());
56 }
57
58 #[test]
59 fn open_rooms_allow_both() {
60 assert!(RoomState::Open.allows_reads());
61 assert!(RoomState::Open.allows_writes());
62 }
63 }
64