Skip to main content

weirdnet_collab/
decision.rs

1//! A decision and the reason chain behind it.
2use crate::ids::{Permission, RecordId, RoleId};
3use crate::records::{Duration, Effect};
4use serde::{Deserialize, Serialize};
5use weirdnet_contacts::UserId;
6
7/// The answer.
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
9pub enum Decision {
10    /// Yes.
11    Allowed,
12    /// No.
13    Denied,
14    /// Not yet: the audio's owner has to consent first.
15    NeedsConsent {
16        /// Whose consent.
17        owner: UserId,
18    },
19}
20
21/// One step of the reasoning, in the order the rules ran.
22#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23pub enum Step {
24    /// The caller is banned from the project.
25    Banned,
26    /// The caller is not a member.
27    NotMember,
28    /// The session is closed.
29    SessionClosed,
30    /// A rule for this person decided.
31    MemberRule {
32        /// The rule.
33        rule: RecordId,
34        /// What it says.
35        effect: Effect,
36        /// Who set it.
37        set_by: UserId,
38        /// When.
39        set_ms: i64,
40    },
41    /// A rule for the person's role decided.
42    RoleRule {
43        /// The role.
44        role: RoleId,
45        /// The rule.
46        rule: RecordId,
47        /// What it says.
48        effect: Effect,
49        /// Who set it.
50        set_by: UserId,
51        /// When.
52        set_ms: i64,
53    },
54    /// A matching rule had expired and was ignored.
55    ExpiredRule {
56        /// The rule.
57        rule: RecordId,
58    },
59    /// No rule decided; the role's default did.
60    RoleDefault {
61        /// The role.
62        role: RoleId,
63        /// Whether it has the permission.
64        granted: bool,
65    },
66    /// The audio is the caller's own: no consent needed.
67    OwnAudio,
68    /// A consent grant from the audio's owner covers it.
69    Consent {
70        /// The grant.
71        grant: RecordId,
72        /// The owner.
73        owner: UserId,
74        /// Once or always.
75        duration: Duration,
76    },
77    /// The owner has not consented.
78    ConsentMissing {
79        /// The owner.
80        owner: UserId,
81    },
82    /// A gated permission was asked for without saying whose audio it is.
83    NoOwnerNamed,
84    /// A switch only the caller's device can check.
85    LocalGate {
86        /// The switch.
87        name: String,
88    },
89    /// Nothing allowed it.
90    DefaultDeny,
91}
92
93/// A decision with its chain.
94#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
95pub struct Explained {
96    /// The asker.
97    pub who: UserId,
98    /// The permission.
99    pub permission: Permission,
100    /// The answer.
101    pub decision: Decision,
102    /// The steps that led to it.
103    pub chain: Vec<Step>,
104}
105
106impl Explained {
107    /// Whether it is a plain yes.
108    pub fn allowed(&self) -> bool {
109        self.decision == Decision::Allowed
110    }
111}