diff --git a/app/controllers/thredded/posts_controller.rb b/app/controllers/thredded/posts_controller.rb index c2f5bd49e..226e235fc 100644 --- a/app/controllers/thredded/posts_controller.rb +++ b/app/controllers/thredded/posts_controller.rb @@ -7,7 +7,7 @@ class PostsController < Thredded::ApplicationController include Thredded::NewPostParams helper_method :topic - before_action :assign_messageboard_for_actions, only: %i[mark_as_read mark_as_unread] + before_action :assign_messageboard_for_actions, only: %i[mark_as_read mark_as_unread report] after_action :update_user_activity after_action :verify_authorized @@ -77,6 +77,18 @@ def quote render plain: Thredded::ContentFormatter.quote_content(post.content) end + def report + authorize_reading post + post.update(moderation_state: 'pending_moderation') + respond_to do |format| + format.html do + redirect_back fallback_location: post_path(post, user: thredded_current_user), + notice: I18n.t('thredded.posts.reported_post') + end + format.json { render(json: { reported: true }) } + end + end + private def canonical_topic_params diff --git a/app/models/concerns/thredded/post_common.rb b/app/models/concerns/thredded/post_common.rb index 0bfa45856..7ce62a9ea 100644 --- a/app/models/concerns/thredded/post_common.rb +++ b/app/models/concerns/thredded/post_common.rb @@ -30,6 +30,10 @@ module PostCommon SELECT MAX(p2.created_at) from #{posts_table_name} p2 WHERE p2.postable_id = #{posts_table_name}.postable_id) SQL ) + + # When we can't load a preloader, return the results as-is + return result if preloader.empty? + preloader[0].preloaded_records.each do |post| topic = owners_by_id.delete(post.postable_id) next unless topic diff --git a/app/policies/thredded/post_policy.rb b/app/policies/thredded/post_policy.rb index 2619f5015..d0b298fde 100644 --- a/app/policies/thredded/post_policy.rb +++ b/app/policies/thredded/post_policy.rb @@ -45,6 +45,11 @@ def destroy? !@post.first_post_in_topic? && update? end + def report? + # Only allow logged-in users to report posts + !(@user.is_a? Thredded::NullUser) + end + def anonymous? @user.thredded_anonymous? end diff --git a/app/view_models/thredded/post_view.rb b/app/view_models/thredded/post_view.rb index 9e7e0b5e1..f7adde089 100644 --- a/app/view_models/thredded/post_view.rb +++ b/app/view_models/thredded/post_view.rb @@ -46,6 +46,10 @@ def can_moderate? @can_moderate ||= @policy.moderate? end + def can_report? + @can_report ||= !!@policy.try(:report?) + end + def quote_url_params if @post.private_topic_post? { post: { quote_private_post_id: @post.id } } @@ -66,6 +70,10 @@ def mark_unread_path Thredded::UrlsHelper.mark_unread_path(@post) end + def report_path + Thredded::UrlsHelper.report_post_path(@post) + end + def destroy_path Thredded::UrlsHelper.delete_post_path(@post) end diff --git a/app/views/thredded/moderation/_post_moderation_record.html.erb b/app/views/thredded/moderation/_post_moderation_record.html.erb index 4a5426d23..644f74de2 100644 --- a/app/views/thredded/moderation/_post_moderation_record.html.erb +++ b/app/views/thredded/moderation/_post_moderation_record.html.erb @@ -13,7 +13,7 @@ time_ago: time_ago(record.created_at) } %> -
+

<% if record.approved? %> diff --git a/app/views/thredded/posts_common/_actions.html.erb b/app/views/thredded/posts_common/_actions.html.erb index 8a6fdf518..8104562f9 100644 --- a/app/views/thredded/posts_common/_actions.html.erb +++ b/app/views/thredded/posts_common/_actions.html.erb @@ -10,11 +10,17 @@ <% if post.can_destroy? %> <%= render 'thredded/posts_common/actions/delete', post: post %> <% end %> + <% if post.can_report? %> + <%= render 'thredded/posts_common/actions/report', post: post %> + <% end %> <% if post.read_state %> <%= view_hooks.post_common.mark_as_unread.render self, post: post do %> <%= render 'thredded/posts_common/actions/mark_as_unread', post: post %> <% end %> <% end %> + <% if user_signed_in? && current_user.forum_moderator? && post.instance_variable_get(:@post).is_a?(Thredded::Post) %> + <%= render 'thredded/moderation/post_moderation_actions', post: post %> + <% end %> <% end %> <% end %> diff --git a/app/views/thredded/posts_common/actions/_flag.html.erb b/app/views/thredded/posts_common/actions/_flag.html.erb new file mode 100644 index 000000000..406efe936 --- /dev/null +++ b/app/views/thredded/posts_common/actions/_flag.html.erb @@ -0,0 +1,5 @@ +<%= button_to t('thredded.posts.flag'), post.flag_path, + method: :post, + class: 'thredded--post--report thredded--post--dropdown--actions--item', + data: { confirm: I18n.t('thredded.posts.flag_confirm') } +%> diff --git a/app/views/thredded/posts_common/actions/_report.html.erb b/app/views/thredded/posts_common/actions/_report.html.erb new file mode 100644 index 000000000..d8a9157f0 --- /dev/null +++ b/app/views/thredded/posts_common/actions/_report.html.erb @@ -0,0 +1,5 @@ +<%= button_to t('thredded.posts.report'), post.report_path, + method: :post, + class: 'thredded--post--report thredded--post--dropdown--actions--item', + data: { confirm: I18n.t('thredded.posts.report_confirm') } +%> diff --git a/config/locales/en.yml b/config/locales/en.yml index 6a7ae1052..5f70b79d4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -136,6 +136,8 @@ en: delete_confirm: Are you sure you want to delete this post? deleted_notice: Your post has been deleted. edit: :thredded.nav.edit_post + flag: Flag Post as Spam + flag_confirm: Are you sure you want to flag this post as spam? form: content_label: Content create_btn: Submit Reply @@ -145,6 +147,9 @@ en: update_btn_submitting: :thredded.form.update_btn_submitting pending_moderation_notice: Your post will be published when it has been reviewed by a moderator. quote_btn: Quote + report: Report Post + report_confirm: Are you sure you want to report this post? + reported_post: This post has been reported to moderation for review. spoiler_summary: Spoiler - click to show. spoiler_summary_for_email: 'Spoiler - select the contents below to see:' preferences: diff --git a/config/routes.rb b/config/routes.rb index 87e1d2985..23d20c353 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -95,6 +95,7 @@ member do post 'mark_as_read' post 'mark_as_unread' + post 'report' end end @@ -102,6 +103,7 @@ member do post 'mark_as_read' post 'mark_as_unread' + post 'report' end end end