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.
This commit is contained in:
evazion
2022-01-13 12:45:01 -06:00
parent 4cb01d5813
commit 5adeedcf01
6 changed files with 100 additions and 54 deletions

View File

@@ -16,34 +16,62 @@
<%= render "application/update_notice", record: forum_post %>
<menu>
<% if policy(forum_post).create? %>
<li><%= link_to "Reply", new_forum_post_path(post_id: forum_post.id), method: :get, remote: true %></li>
<% end %>
<% if policy(forum_post).destroy? && !forum_post.is_original_post?(original_forum_post_id) %>
<% if forum_post.is_deleted %>
<li><%= link_to "Undelete", undelete_forum_post_path(forum_post.id), method: :post, remote: true %></li>
<% if policy(forum_post).reply? %>
<% if current_user.is_anonymous? %>
<li><%= link_to "Reply", login_path(url: request.fullpath) %></li>
<% else %>
<li><%= 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 %></li>
<li><%= link_to "Reply", new_forum_post_path(post_id: forum_post.id), method: :get, remote: true %></li>
<% end %>
<% end %>
<% if policy(forum_post).update? %>
<% if forum_post.is_original_post?(original_forum_post_id) %>
<li><%= link_to "Edit", edit_forum_topic_path(forum_post.topic), id: "edit_forum_topic_link_#{forum_post.topic.id}", class: "edit_forum_topic_link" %></li>
<% else %>
<li><%= link_to "Edit", edit_forum_post_path(forum_post.id), id: "edit_forum_post_link_#{forum_post.id}", class: "edit_forum_post_link" %></li>
<% end %>
<% end %>
<% if policy(forum_post).reportable? %>
<li><%= 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" %></li>
<% end %>
<% if has_moderation_reports? %>
<li class="moderation-report-notice">Reported (<%= link_to pluralize(forum_post.moderation_reports.length, "report"), moderation_reports_path(search: { model_type: "ForumPost", model_id: forum_post.id }) %>)</li>
<% 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? %>
<menu class="votes" id="forum-post-votes-for-<%= forum_post.id %>">
<%= render "forum_post_votes/list", votes: forum_post.votes, forum_post: forum_post %>

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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?