From 5adeedcf01f4f9770d7e1cebde54db73c3f7225c Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 13 Jan 2022 12:45:01 -0600 Subject: [PATCH] forum: add "..." menu to forum posts, like comments have. Adds the ability to copy forum post IDs, like with comments. Fixes #4768: Display DText shortlink for forum posts/topics. --- .../forum_post_component.html.erb | 68 +++++++++++++------ .../forum_post_component.js | 45 ++++++++++++ app/javascript/packs/application.js | 2 + app/javascript/src/javascripts/forum_posts.js | 33 --------- app/policies/forum_post_policy.rb | 4 ++ app/policies/forum_topic_policy.rb | 2 +- 6 files changed, 100 insertions(+), 54 deletions(-) create mode 100644 app/components/forum_post_component/forum_post_component.js delete mode 100644 app/javascript/src/javascripts/forum_posts.js diff --git a/app/components/forum_post_component/forum_post_component.html.erb b/app/components/forum_post_component/forum_post_component.html.erb index fe9ed547a..18d7ec8f9 100644 --- a/app/components/forum_post_component/forum_post_component.html.erb +++ b/app/components/forum_post_component/forum_post_component.html.erb @@ -16,34 +16,62 @@ <%= render "application/update_notice", record: forum_post %> - <% if policy(forum_post).create? %> -
  • <%= link_to "Reply", new_forum_post_path(post_id: forum_post.id), method: :get, remote: true %>
  • - <% end %> - - <% if policy(forum_post).destroy? && !forum_post.is_original_post?(original_forum_post_id) %> - <% if forum_post.is_deleted %> -
  • <%= link_to "Undelete", undelete_forum_post_path(forum_post.id), method: :post, remote: true %>
  • + <% if policy(forum_post).reply? %> + <% if current_user.is_anonymous? %> +
  • <%= link_to "Reply", login_path(url: request.fullpath) %>
  • <% else %> -
  • <%= link_to "Delete", forum_post_path(forum_post.id), "data-confirm": "Are you sure you want to delete this forum post?", method: :delete, remote: true %>
  • +
  • <%= link_to "Reply", new_forum_post_path(post_id: forum_post.id), method: :get, remote: true %>
  • <% end %> <% end %> - <% if policy(forum_post).update? %> - <% if forum_post.is_original_post?(original_forum_post_id) %> -
  • <%= link_to "Edit", edit_forum_topic_path(forum_post.topic), id: "edit_forum_topic_link_#{forum_post.topic.id}", class: "edit_forum_topic_link" %>
  • - <% else %> -
  • <%= link_to "Edit", edit_forum_post_path(forum_post.id), id: "edit_forum_post_link_#{forum_post.id}", class: "edit_forum_post_link" %>
  • - <% end %> - <% end %> - - <% if policy(forum_post).reportable? %> -
  • <%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "ForumPost", model_id: forum_post.id }), remote: true, title: "Report this forum post to the moderators" %>
  • - <% end %> - <% if has_moderation_reports? %>
  • Reported (<%= link_to pluralize(forum_post.moderation_reports.length, "report"), moderation_reports_path(search: { model_type: "ForumPost", model_id: forum_post.id }) %>)
  • <% end %> + <%= render PopupMenuComponent.new do |menu| %> + <% if policy(forum_post).update? %> + <% menu.item do %> + <% if forum_post.is_original_post?(original_forum_post_id) %> + <%= link_to edit_forum_topic_path(forum_post.topic), id: "edit_forum_topic_link_#{forum_post.topic_id}", class: "edit_forum_topic_link" do %> + <%= edit_icon %> Edit + <% end %> + <% else %> + <%= link_to edit_forum_post_path(forum_post.id), id: "edit_forum_post_link_#{forum_post.id}", class: "edit_forum_post_link" do %> + <%= edit_icon %> Edit + <% end %> + <% end %> + <% end %> + + <% if policy(forum_post).destroy? && !forum_post.is_original_post?(original_forum_post_id) %> + <% menu.item do %> + <% if forum_post.is_deleted? %> + <%= link_to undelete_forum_post_path(forum_post.id), method: :post, remote: true do %> + <%= undelete_icon %> Undelete + <% end %> + <% else %> + <%= link_to forum_post_path(forum_post.id), "data-confirm": "Are you sure you want to delete this forum post?", method: :delete, remote: true do %> + <%= delete_icon %> Delete + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + + <% if policy(forum_post).reportable? %> + <% menu.item do %> + <%= link_to new_moderation_report_path(moderation_report: { model_type: "ForumPost", model_id: forum_post.id }), remote: true do %> + <%= flag_icon %> Report + <% end %> + <% end %> + <% end %> + + <% menu.item do %> + <%= link_to forum_post_path(forum_post.id), class: "forum-post-copy-link" do %> + <%= link_icon %> Copy ID + <% end %> + <% end %> + <% end %> + <% if forum_post.bulk_update_request.present? %> <%= render "forum_post_votes/list", votes: forum_post.votes, forum_post: forum_post %> diff --git a/app/components/forum_post_component/forum_post_component.js b/app/components/forum_post_component/forum_post_component.js new file mode 100644 index 000000000..72bac0bb7 --- /dev/null +++ b/app/components/forum_post_component/forum_post_component.js @@ -0,0 +1,45 @@ +import Utility from "../../javascript/src/javascripts/utility.js"; + +class ForumPostComponent { + static initialize() { + if ($("#c-forum-topics #a-show, #c-forum-posts #a-show").length) { + $(document).on("click.danbooru.forum_post", ".edit_forum_post_link", ForumPostComponent.showEditPostForm); + $(document).on("click.danbooru.forum_post", ".edit_forum_topic_link", ForumPostComponent.showEditTopicForm); + $(document).on("click.danbooru.forum_post", "#new-response-link", ForumPostComponent.showNewForumPostForm); + $(document).on("click.danbooru.forum_post", ".forum-post-copy-link", ForumPostComponent.copyLink); + } + } + + static showNewForumPostForm(e) { + $("#topic-response").show(); + document.body.scrollIntoView(false); + e.preventDefault(); + } + + static showEditPostForm(e) { + $(this).closest(".forum-post").find(".edit_forum_post").show(); + e.preventDefault(); + } + + static showEditTopicForm(e) { + $(this).closest(".forum-post").find(".edit_forum_topic").show(); + e.preventDefault(); + } + + static async copyLink(e) { + let $forumPost = $(this).closest(".forum-post"); + let link = `forum #${$forumPost.data("id")}`; + e.preventDefault(); + + try { + await navigator.clipboard.writeText(link); + Utility.notice(`Copied ${link} to clipboard.`); + } catch (error) { + Utility.error("Couldn't copy link to clipboard"); + } + } +} + +$(document).ready(ForumPostComponent.initialize); + +export default ForumPostComponent; diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 0b17829c3..a0caf04f5 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -39,6 +39,7 @@ import CommentVotesTooltipComponent from "../../components/comment_votes_tooltip import CurrentUser from "../src/javascripts/current_user.js"; import Dtext from "../src/javascripts/dtext.js"; import FavoritesTooltipComponent from "../../components/favorites_tooltip_component/favorites_tooltip_component.js"; +import ForumPostComponent from "../../components/forum_post_component/forum_post_component.js"; import IqdbQuery from "../src/javascripts/iqdb_queries.js"; import Note from "../src/javascripts/notes.js"; import PopupMenuComponent from "../../components/popup_menu_component/popup_menu_component.js"; @@ -63,6 +64,7 @@ Danbooru.CommentVotesTooltipComponent = CommentVotesTooltipComponent; Danbooru.CurrentUser = CurrentUser; Danbooru.Dtext = Dtext; Danbooru.FavoritesTooltipComponent = FavoritesTooltipComponent; +Danbooru.ForumPostComponent = ForumPostComponent; Danbooru.IqdbQuery = IqdbQuery; Danbooru.Note = Note; Danbooru.PopupMenuComponent = PopupMenuComponent; diff --git a/app/javascript/src/javascripts/forum_posts.js b/app/javascript/src/javascripts/forum_posts.js deleted file mode 100644 index 28437c5df..000000000 --- a/app/javascript/src/javascripts/forum_posts.js +++ /dev/null @@ -1,33 +0,0 @@ -let ForumPost = {}; - -ForumPost.initialize_all = function() { - if ($("#c-forum-topics #a-show,#c-forum-posts #a-show").length) { - this.initialize_edit_links(); - } -} - -ForumPost.initialize_edit_links = function() { - $(document).on("click.danbooru", ".edit_forum_post_link", function(e) { - let $form = $(this).parents("article.forum-post").find("form.edit_forum_post"); - $form.fadeToggle("fast"); - e.preventDefault(); - }); - - $(document).on("click.danbooru", ".edit_forum_topic_link", function(e) { - let $form = $(this).parents("article.forum-post").find("form.edit_forum_topic"); - $form.fadeToggle("fast"); - e.preventDefault(); - }); - - $(document).on("click.danbooru", "#c-forum-topics #a-show #new-response-link", function (e) { - $("#topic-response").show(); - document.body.scrollIntoView(false); - e.preventDefault(); - }); -} - -$(document).ready(function() { - ForumPost.initialize_all(); -}); - -export default ForumPost diff --git a/app/policies/forum_post_policy.rb b/app/policies/forum_post_policy.rb index 355877613..bf8dddbf2 100644 --- a/app/policies/forum_post_policy.rb +++ b/app/policies/forum_post_policy.rb @@ -25,6 +25,10 @@ class ForumPostPolicy < ApplicationPolicy unbanned? && show? && user.is_moderator? end + def reply? + policy(record.topic).reply? + end + def votable? unbanned? && show? && record.bulk_update_request.present? && record.bulk_update_request.is_pending? && record.bulk_update_request.user_id != user.id end diff --git a/app/policies/forum_topic_policy.rb b/app/policies/forum_topic_policy.rb index 82539b7c0..73bd92645 100644 --- a/app/policies/forum_topic_policy.rb +++ b/app/policies/forum_topic_policy.rb @@ -26,7 +26,7 @@ class ForumTopicPolicy < ApplicationPolicy end def reply? - unbanned? && show? && (user.is_moderator? || !record.is_locked?) + show? && (user.is_moderator? || !record.is_locked?) end def moderate?