Skip to content

Commit

Permalink
Add settings for toggling signups and guest comments
Browse files Browse the repository at this point in the history
  • Loading branch information
agraven committed Mar 11, 2021
1 parent 8b83886 commit 21b8f8e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Guest comment stuff based on permissions rather than config
* Implement MaybeSession?
* Use ArticleIdPath where appropriate
* Site administration
Expand Down
6 changes: 6 additions & 0 deletions mogger.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ host_address = "localhost:6096"
# Postgres url to connect to the database with
database_url = "postgres://mogger@localhost/mogger"

[features]
# Allow registering an account
signups = false
# Allow unregistered users to make comments
guest_comments = false

[cookie]
# Require HTTPS for cookies
secure = false
Expand Down
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub struct Settings {
pub database_url: String,
/// IP address to bind to
pub host_address: String,
/// Toggles for enabling and disabling features
pub features: Features,
/// Cookie settings
pub cookie: Cookie,
}
Expand All @@ -18,6 +20,15 @@ impl Settings {
}
}

/// Feature toggles
#[derive(Deserialize, Clone)]
pub struct Features {
/// Allow registering an account
pub signups: bool,
/// Allow unregistered users to make comments
pub guest_comments: bool,
}

/// Cookie related settings
#[derive(Deserialize, Clone)]
pub struct Cookie {
Expand Down
14 changes: 12 additions & 2 deletions src/document/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use gotham::{
use crate::{
article::{self, Article, ArticleChanges, NewArticle},
comment::{self, Comment},
config::Settings,
db::{Connection, DbConnection},
document::{DocumentResult, TemplateExt},
handler::articles::{ArticleIdPath, ArticlePath},
Expand All @@ -26,6 +27,7 @@ pub struct ArticleTemplate<'a> {
comments: Vec<CommentTemplate<'a>>,
session: Option<&'a Session>,
connection: &'a Connection,
can_comment: bool,
}

#[derive(Template)]
Expand All @@ -35,37 +37,42 @@ pub struct CommentTemplate<'a> {
pub children: Vec<CommentTemplate<'a>>,
pub connection: &'a Connection,
pub session: Option<&'a Session>,
pub can_comment: bool,
}

impl<'a> CommentTemplate<'a> {
pub fn from_node(
tree: &'a comment::Node,
connection: &'a Connection,
session: Option<&'a Session>,
can_comment: bool,
) -> Self {
CommentTemplate {
comment: &tree.comment,
children: tree
.children
.iter()
.map(|child| CommentTemplate::from_node(child, connection, session))
.map(|child| CommentTemplate::from_node(child, connection, session, can_comment))
.collect(),
connection,
session,
can_comment,
}
}

pub fn from_list(
list: &'a [Comment],
connection: &'a Connection,
session: Option<&'a Session>,
can_comment: bool,
) -> Vec<Self> {
list.iter()
.map(|comment| CommentTemplate {
comment,
children: Vec::new(),
connection,
session,
can_comment,
})
.collect()
}
Expand All @@ -84,6 +91,7 @@ pub fn view(state: &State) -> DocumentResult {
let connection = &DbConnection::from_state(state)?;
let id = &ArticlePath::borrow_from(state).id;
let session = Session::try_borrow_from(state);
let can_comment = Settings::borrow_from(state).features.guest_comments || session.is_some();

let article = article::view(connection, &id)?;
// Return a 404 if the user isn't allowed to view the article
Expand All @@ -94,15 +102,17 @@ pub fn view(state: &State) -> DocumentResult {
let comments = comment::list(connection, article.id)?;
let comments_template = comments
.iter()
.map(|child| CommentTemplate::from_node(child, connection, session))
.map(|child| CommentTemplate::from_node(child, connection, session, can_comment))
.collect();
let author = article.user(connection)?;
// true if logged in or guest comments permitted
let template = ArticleTemplate {
article,
author_name: author.name,
comments: comments_template,
session,
connection,
can_comment,
};
let response = template.to_response(state);
Ok(response)
Expand Down
5 changes: 4 additions & 1 deletion src/document/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ pub fn login_post(state: &State, post: Vec<u8>) -> DocumentResult {
struct SignupTemplate<'a> {
session: Option<&'a Session>,
connection: &'a Connection,
signup_enabled: bool,
}

pub fn signup(state: &State) -> DocumentResult {
let connection = &DbConnection::from_state(state)?;
let signup_enabled = Settings::borrow_from(state).features.signups;
Ok(SignupTemplate {
session: Session::try_borrow_from(state),
connection,
signup_enabled,
}
.to_response(state))
}
Expand Down Expand Up @@ -190,7 +193,7 @@ pub fn view(state: &State) -> DocumentResult {
let user_id = &UserPath::borrow_from(state).user;
let user = user::get(connection, user_id)?;
let comments = comment::by_user(connection, user_id)?;
let comment_templates = CommentTemplate::from_list(&comments, connection, session);
let comment_templates = CommentTemplate::from_list(&comments, connection, session, false);

let template = UserTemplate {
user: &user,
Expand Down
11 changes: 10 additions & 1 deletion src/handler/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use mime::APPLICATION_JSON as JSON;
use crate::{
comment,
comment::{CommentChanges, NewComment},
config::Settings,
document::TemplateExt,
handler::articles::ArticlePath,
user::{
Expand Down Expand Up @@ -78,11 +79,14 @@ pub fn render(state: &State) -> Result<Response<Body>, failure::Error> {
let id = CommentPath::borrow_from(&state).id;

if let Some(comment) = comment::view_single(connection, id)? {
let session = Session::try_borrow_from(state);
let can_comment = session.is_some() || Settings::borrow_from(state).features.guest_comments;
let template = crate::document::article::CommentTemplate {
comment: &comment,
children: Vec::new(),
connection,
session: Session::try_borrow_from(state),
session,
can_comment,
};
Ok(template.to_response(state))
} else {
Expand All @@ -91,6 +95,11 @@ pub fn render(state: &State) -> Result<Response<Body>, failure::Error> {
}

pub fn submit(state: &State, post: Vec<u8>) -> Result<Response<Body>, failure::Error> {
let session = Session::try_borrow_from(state);
let settings = Settings::borrow_from(state);
if session.is_none() && !settings.features.guest_comments {
return Err(failure::err_msg("Permission denied"));
}
let connection = &DbConnection::borrow_from(state).lock()?;

let new: NewComment = serde_json::from_slice(&post)?;
Expand Down
7 changes: 7 additions & 0 deletions templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ <h1>{{ article.title }}</h1>
{{ article.formatted() }}
</article>

{% if can_comment %}
<form class="comment" id="new-comment">
<h2>Write a comment</h2>
<input type="hidden" name="article" data-type="int" value="{{ article.id }}">
Expand All @@ -49,6 +50,12 @@ <h2>Write a comment</h2>
<textarea name="content" required></textarea>
<button type="button" data-action="submit">Submit</button>
</form>
{% else %}
<form class="comment" id="new-comment">
<h2>Write a comment</h2>
<textarea disabled>Comments are disabled for guest users</textarea>
</form>
{% endif %}

{% for tree in comments %}
{{ tree.render().unwrap()|safe }}
Expand Down
2 changes: 2 additions & 0 deletions templates/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<button type="button" data-action="cancel">Cancel</button>
</form>
<ul class="links">
{% if can_comment %}
<li><button class="link-like" data-action="reply">reply</button></li>
{% endif %}
{% if comment.editable(session, connection).unwrap_or(false) %}
<li><button class="link-like" data-action="edit">edit</button></li>
<li><button class="link-like" data-action="remove">remove</button></li>
Expand Down
4 changes: 4 additions & 0 deletions templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<header>
<h1>Sign up</h1>
</header>
{% if signup_enabled %}
<p>Create an account.
<form method="post" class="validate">
<div>
Expand Down Expand Up @@ -36,5 +37,8 @@ <h1>Sign up</h1>
<input type="submit">
</div>
</form>
{% else %}
<p>Account registration is currently disabled.
{% endif %}
</article>
{% endblock %}

0 comments on commit 21b8f8e

Please sign in to comment.