Skip to main content

laravel_comments/
config.rs

1//! The package's settings.
2
3use std::fmt;
4use std::str::FromStr;
5
6use laravel::config::Section;
7
8/// The package's settings, read from the environment. An application changes them in
9/// `with_config`:
10///
11/// ```no_run
12/// # use laravel::Application;
13/// use laravel_comments::CommentsConfig;
14///
15/// # let _ =
16/// Application::configure().with_config(|config, _| {
17///     config.section_mut::<CommentsConfig>().reactions = vec!["like".into(), "heart".into()];
18/// })
19/// # ;
20/// ```
21#[derive(Debug, Clone, Section)]
22pub struct CommentsConfig {
23    /// The deepest level a reply may sit at: `0` for top-level comments, `1` for their replies,
24    /// and so on. A reply to a comment at this level joins its parent's replies instead, as on
25    /// Reddit. `0` turns replies off.
26    #[env("COMMENTS_MAX_DEPTH", default = 8)]
27    pub max_depth: i64,
28    /// The most characters a comment's text may have.
29    #[env("COMMENTS_MAX_LENGTH", default = 10_000)]
30    pub max_length: usize,
31    /// The reactions people may leave on a comment, such as `like` or `heart`.
32    #[env("COMMENTS_REACTIONS", separator = ",", default = vec!["like".to_owned()])]
33    pub reactions: Vec<String>,
34    /// Which new comments are approved at once, unless the application sets a rule of its own
35    /// with [`CommentsServiceProvider::moderate_with`](crate::CommentsServiceProvider::moderate_with).
36    #[env("COMMENTS_AUTO_APPROVE", default = AutoApprove::Users)]
37    pub auto_approve: AutoApprove,
38    /// For how many minutes after posting an author may edit a comment, by the default policy.
39    /// `0` for no limit.
40    #[env("COMMENTS_EDIT_WINDOW", default = 0)]
41    pub edit_window: i64,
42    /// How many comments an author may post a minute, by [`throttle`](crate::throttle): users
43    /// by their key, guests by their IP address. `0` for no limit.
44    #[env("COMMENTS_RATE_LIMIT", default = 10)]
45    pub rate_limit: u32,
46    /// Whether to keep the text before each edit, for [`CommentModel::revisions`](crate::CommentModel::revisions).
47    #[env("COMMENTS_REVISIONS", default = true)]
48    pub revisions: bool,
49    /// Whether and how the events broadcast, on the channel `comments.{type}.{id}` of the model
50    /// the comment belongs to.
51    #[env("COMMENTS_BROADCAST", default = BroadcastChannel::Off)]
52    pub broadcast: BroadcastChannel,
53}
54
55/// How the events broadcast.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum BroadcastChannel {
58    /// They don't.
59    Off,
60    /// On a public channel, which anyone may subscribe to.
61    Public,
62    /// On a private channel, whose subscriptions the application authorizes.
63    Private,
64}
65
66impl FromStr for BroadcastChannel {
67    type Err = UnknownBroadcastChannel;
68
69    fn from_str(value: &str) -> Result<Self, Self::Err> {
70        match value.to_ascii_lowercase().as_str() {
71            "off" | "" => Ok(Self::Off),
72            "public" => Ok(Self::Public),
73            "private" => Ok(Self::Private),
74            _ => Err(UnknownBroadcastChannel(value.to_owned())),
75        }
76    }
77}
78
79/// A `COMMENTS_BROADCAST` value other than `off`, `public`, or `private`.
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct UnknownBroadcastChannel(String);
82
83impl fmt::Display for UnknownBroadcastChannel {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(f, "`{}` isn't one of `off`, `public`, `private`", self.0)
86    }
87}
88
89impl std::error::Error for UnknownBroadcastChannel {}
90
91/// Which new comments are approved at once. The rest wait for a moderator.
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum AutoApprove {
94    /// Every comment.
95    Everyone,
96    /// Comments by users; guests' comments wait.
97    Users,
98    /// No comment.
99    Nobody,
100}
101
102impl FromStr for AutoApprove {
103    type Err = UnknownAutoApprove;
104
105    fn from_str(value: &str) -> Result<Self, Self::Err> {
106        match value.to_ascii_lowercase().as_str() {
107            "everyone" => Ok(Self::Everyone),
108            "users" => Ok(Self::Users),
109            "nobody" => Ok(Self::Nobody),
110            _ => Err(UnknownAutoApprove(value.to_owned())),
111        }
112    }
113}
114
115/// A `COMMENTS_AUTO_APPROVE` value other than `everyone`, `users`, or `nobody`.
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct UnknownAutoApprove(String);
118
119impl fmt::Display for UnknownAutoApprove {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        write!(f, "`{}` isn't one of `everyone`, `users`, `nobody`", self.0)
122    }
123}
124
125impl std::error::Error for UnknownAutoApprove {}