Add config options to disable comments and the forum.

Add options to disable comments, the forum, and autocomplete. This is
for personal boorus and potentially for safe mode. Note that disabling
the forum may cause difficulties with creating and approving BURs.

Disabling comments and the forum merely hides them from most areas,
rather than completely removing them.
This commit is contained in:
evazion
2022-05-18 14:40:53 -05:00
parent 2fe38c1c07
commit 1e78b97eb8
14 changed files with 102 additions and 51 deletions

View File

@@ -4,6 +4,10 @@ class CommentsController < ApplicationController
respond_to :html, :xml, :json, :atom
respond_to :js, only: [:new, :update, :destroy, :undelete]
before_action if: -> { request.format.html? && !Danbooru.config.comments_enabled?.to_s.truthy? } do
redirect_to root_path
end
rate_limit :create, rate: 1.0/1.minute, burst: 50
def index

View File

@@ -3,7 +3,11 @@
class ForumTopicsController < ApplicationController
respond_to :html, :xml, :json
respond_to :atom, only: [:index, :show]
before_action :normalize_search, :only => :index
before_action if: -> { request.format.html? && !Danbooru.config.forum_enabled?.to_s.truthy? } do
redirect_to root_path
end
rate_limit :create, rate: 1.0/1.minute, burst: 50

View File

@@ -28,23 +28,27 @@ class AutocompleteService
TAG_PREFIXES = TagCategory.mapping.keys.map { |prefix| prefix + ":" }
attr_reader :query, :type, :limit, :current_user
attr_reader :query, :type, :limit, :current_user, :enabled
alias_method :enabled?, :enabled
# Perform completion for the given search type and query.
# @param query [String] the string being completed
# @param type [String] the type of completion being performed
# @param current_user [User] the user we're performing completion for
# @param limit [Integer] the max number of results to return
def initialize(query, type, current_user: User.anonymous, limit: 10)
def initialize(query, type, current_user: User.anonymous, limit: 10, enabled: Danbooru.config.autocomplete_enabled?.to_s.truthy?)
@query = query.to_s
@type = type.to_s.to_sym
@current_user = current_user
@limit = limit
@enabled = enabled
end
# Return the results of the completion.
# @return [Array<Hash>] the autocomplete results
def autocomplete_results
return [] if !enabled?
case type
when :tag_query
autocomplete_tag_query

View File

@@ -16,7 +16,7 @@ class RateLimiter
attr_reader :action, :keys, :cost, :rate, :burst, :enabled
alias_method :enabled?, :enabled
def initialize(action, keys = ["*"], cost: 1, rate: 1, burst: 1, enabled: Danbooru.config.rate_limits_enabled?)
def initialize(action, keys = ["*"], cost: 1, rate: 1, burst: 1, enabled: Danbooru.config.rate_limits_enabled?.to_s.truthy?)
@action = action
@keys = keys
@cost = cost

View File

@@ -87,13 +87,13 @@
<% end %>
<%= nav_link_to("Posts", posts_path) %>
<%= nav_link_to("Comments", comments_path) %>
<%= nav_link_to("Comments", comments_path) if Danbooru.config.comments_enabled?.to_s.truthy? %>
<%= nav_link_to("Notes", notes_path) %>
<%= nav_link_to("Artists", artists_path) %>
<%= nav_link_to("Tags", tags_path) %>
<%= nav_link_to("Pools", gallery_pools_path) %>
<%= nav_link_to("Wiki", wiki_page_path("help:home")) %>
<%= nav_link_to("Forum", forum_topics_path, :class => (CurrentUser.has_forum_been_updated? ? "forum-updated" : nil)) %>
<%= nav_link_to("Forum", forum_topics_path, class: ("forum-updated" if CurrentUser.has_forum_been_updated?)) if Danbooru.config.forum_enabled?.to_s.truthy? %>
<% if CurrentUser.is_moderator? %>
<%= nav_link_to("Reports", moderation_reports_path, :class => (ModerationReport.where(status: "pending").present? ? "reports-pending" : nil)) %>

View File

@@ -48,7 +48,9 @@
<li><%= link_to "Random", random_posts_path(tags: params[:tags]), id: "random-post", "data-shortcut": "r", rel: "nofollow" %></li>
<% if @post_set.post_query.has_single_tag? %>
<li><%= link_to "History", post_versions_path(search: { changed_tags: @post_set.post_query.tag_name }), rel: "nofollow" %></li>
<li><%= link_to "Discussions", forum_posts_path(search: { linked_to: @post_set.post_query.tag_name }), rel: "nofollow" %></li>
<% if Danbooru.config.forum_enabled?.to_s.truthy? %>
<li><%= link_to "Discussions", forum_posts_path(search: { linked_to: @post_set.post_query.tag_name }), rel: "nofollow" %></li>
<% end %>
<% end %>
<li><%= link_to "Count", posts_counts_path(tags: params[:tags]), rel: "nofollow" %></li>
</ul>

View File

@@ -11,7 +11,7 @@
<%= upvote_icon %>
</span>
<% if @post.last_commented_at.present? %>
<% if Danbooru.config.comments_enabled?.to_s.truthy? && @post.last_commented_at.present? %>
<span class="post-tooltip-comments post-tooltip-info">
<span><%= @post.comments.count %></span>
<%= comments_icon(class: "fa-xs") %>

View File

@@ -1,7 +1,9 @@
<% page_title @post.presenter.humanized_essential_tag_string %>
<% meta_description "View this #{@post.image_width}x#{@post.image_height} #{number_to_human_size(@post.file_size)} image" %>
<% canonical_url post_url(@post, host: Danbooru.config.hostname) %>
<% atom_feed_tag "Comments for post ##{@post.id}", comments_url(:atom, search: { post_id: @post.id }) %>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<% atom_feed_tag "Comments for post ##{@post.id}", comments_url(:atom, search: { post_id: @post.id }) %>
<% end %>
<%= render "posts/partials/common/secondary_links" %>
@@ -322,7 +324,9 @@
<% end %>
<menu id="post-sections" class="mb-4">
<li class="active"><a href="#comments">Comments</a></li>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<li class="active"><a href="#comments">Comments</a></li>
<% end %>
<% if RecommenderService.available_for_post?(@post) %>
<li><a href="#recommended">Recommended</a></li>
@@ -339,9 +343,11 @@
</section>
<% end %>
<section id="comments">
<%= render_comment_section(@post, current_user: CurrentUser.user) %>
</section>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<section id="comments">
<%= render_comment_section(@post, current_user: CurrentUser.user) %>
</section>
<% end %>
<section id="notes" style="display: none;">
<% if @post.has_notes? %>

View File

@@ -77,22 +77,26 @@
</ul>
</section>
<section class="flex-auto space-y-4">
<ul>
<li><h2>Comments</h2></li>
<li><%= link_to_wiki "Help", "help:comments" %></li>
<li><%= link_to("Listing", comments_path) %></li>
<li><%= link_to("Search", search_comments_path) %></li>
<li><%= link_to("Votes", comment_votes_path) %></li>
<li><%= link_to("RSS", comments_path(:atom)) %></li>
</ul>
<ul>
<li><h2>Forum</h2></li>
<li><%= link_to_wiki "Help", "help:forum" %></li>
<li><%= link_to("Listing", forum_topics_path) %></li>
<li><%= link_to("Search", search_forum_posts_path) %></li>
<li><%= link_to("Votes", forum_post_votes_path) %></li>
<li><%= link_to("RSS", forum_topics_path(:atom)) %></li>
</ul>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<ul>
<li><h2>Comments</h2></li>
<li><%= link_to_wiki "Help", "help:comments" %></li>
<li><%= link_to("Listing", comments_path) %></li>
<li><%= link_to("Search", search_comments_path) %></li>
<li><%= link_to("Votes", comment_votes_path) %></li>
<li><%= link_to("RSS", comments_path(:atom)) %></li>
</ul>
<% end %>
<% if Danbooru.config.forum_enabled?.to_s.truthy? %>
<ul>
<li><h2>Forum</h2></li>
<li><%= link_to_wiki "Help", "help:forum" %></li>
<li><%= link_to("Listing", forum_topics_path) %></li>
<li><%= link_to("Search", search_forum_posts_path) %></li>
<li><%= link_to("Votes", forum_post_votes_path) %></li>
<li><%= link_to("RSS", forum_topics_path(:atom)) %></li>
</ul>
<% end %>
<ul>
<li><h2>Wiki</h2></li>
<li><%= link_to_wiki "Help", "help:wiki" %></li>

View File

@@ -208,20 +208,24 @@
</tr>
<% end %>
<tr>
<th>Forum Posts</th>
<td><%= presenter.forum_post_count(self) %></td>
</tr>
<% if Danbooru.config.forum_enabled?.to_s.truthy? %>
<tr>
<th>Forum Posts</th>
<td><%= presenter.forum_post_count(self) %></td>
</tr>
<% end %>
<tr>
<th>Approvals</th>
<td><%= presenter.approval_count(self) %></td>
</tr>
<tr>
<th>Comments</th>
<td><%= presenter.comment_count(self) %> in <%= presenter.commented_posts_count(self) %> posts</td>
</tr>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<tr>
<th>Comments</th>
<td><%= presenter.comment_count(self) %> in <%= presenter.commented_posts_count(self) %> posts</td>
</tr>
<% end %>
<tr>
<th>Appeals</th>

View File

@@ -111,17 +111,21 @@
<div class="user-tooltip-stat-name">Favorites</div>
<% end %>
</li>
<li class="user-tooltip-stat-item">
<%= link_to comments_path(search: { creator_id: @user.id }), class: "link-plain" do %>
<div class="user-tooltip-stat-value"><%= humanized_number(@user.comment_count) %></div>
<div class="user-tooltip-stat-name">Comments</div>
<% end %>
</li>
<li class="user-tooltip-stat-item">
<%= link_to forum_posts_path(search: { creator_id: @user.id }), class: "link-plain" do %>
<div class="user-tooltip-stat-value"><%= humanized_number(@user.forum_post_count) %></div>
<div class="user-tooltip-stat-name">Forum Posts</div>
<% end %>
</li>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<li class="user-tooltip-stat-item">
<%= link_to comments_path(search: { creator_id: @user.id }), class: "link-plain" do %>
<div class="user-tooltip-stat-value"><%= humanized_number(@user.comment_count) %></div>
<div class="user-tooltip-stat-name">Comments</div>
<% end %>
</li>
<% end %>
<% if Danbooru.config.forum_enabled?.to_s.truthy? %>
<li class="user-tooltip-stat-item">
<%= link_to forum_posts_path(search: { creator_id: @user.id }), class: "link-plain" do %>
<div class="user-tooltip-stat-value"><%= humanized_number(@user.forum_post_count) %></div>
<div class="user-tooltip-stat-name">Forum Posts</div>
<% end %>
</li>
<% end %>
</ul>
</div>

View File

@@ -12,5 +12,7 @@
</div>
</div>
<% atom_feed_tag "Comments on #{@user.pretty_name}'s uploads", comments_url(:atom, search: { post_tags_match: "user:#{@user.name}" }) %>
<% atom_feed_tag "Comments on posts commented on by #{@user.pretty_name}", comments_url(:atom, search: { post_tags_match: "commenter:#{@user.name}" }) %>
<% if Danbooru.config.comments_enabled?.to_s.truthy? %>
<% atom_feed_tag "Comments on #{@user.pretty_name}'s uploads", comments_url(:atom, search: { post_tags_match: "user:#{@user.name}" }) %>
<% atom_feed_tag "Comments on posts commented on by #{@user.pretty_name}", comments_url(:atom, search: { post_tags_match: "commenter:#{@user.name}" }) %>
<% end %>

View File

@@ -10,7 +10,9 @@
<li><%= link_to "Tag History", post_versions_path(search: { changed_tags: @wiki_page.title }) %></li>
<% end %>
<li><%= link_to "Wiki History", wiki_page_versions_path(search: { wiki_page_id: @wiki_page.id }) %></li>
<li><%= link_to "Discussions", forum_posts_path(search: { linked_to: @wiki_page.title }) %></li>
<% if Danbooru.config.forum_enabled?.to_s.truthy? %>
<li><%= link_to "Discussions", forum_posts_path(search: { linked_to: @wiki_page.title }) %></li>
<% end %>
<li><%= link_to "What Links Here", wiki_pages_path(search: { linked_to: @wiki_page.title }) %></li>
</ul>
<% end %>

View File

@@ -377,6 +377,21 @@ module Danbooru
true
end
# Whether to enable comments.
def comments_enabled?
true
end
# Whether to enable the forum.
def forum_enabled?
true
end
# Whether to enable autocomplete.
def autocomplete_enabled?
true
end
def stripe_secret_key
end