diff --git a/app/controllers/artist_versions_controller.rb b/app/controllers/artist_versions_controller.rb index ce29e6e2a..53e4312af 100644 --- a/app/controllers/artist_versions_controller.rb +++ b/app/controllers/artist_versions_controller.rb @@ -2,7 +2,7 @@ class ArtistVersionsController < ApplicationController respond_to :html, :xml, :json def index - @artist_versions = ArtistVersion.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) + @artist_versions = ArtistVersion.includes(:updater).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) respond_with(@artist_versions) do |format| format.xml do render :xml => @artist_versions.to_xml(:root => "artist-versions") diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index ad8767907..b1f623d34 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -83,7 +83,7 @@ private end def index_by_comment - @comments = Comment.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) + @comments = Comment.includes(:creator, :updater).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) respond_with(@comments) do |format| format.atom do @comments = @comments.includes(:post, :creator).load diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 3db3e6a99..270edb3ba 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -6,7 +6,7 @@ class NotesController < ApplicationController end def index - @notes = Note.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) + @notes = Note.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) respond_with(@notes) do |format| format.html { @notes = @notes.includes(:creator) } format.xml do diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index e569d768a..0b78f81b3 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -17,7 +17,7 @@ class PoolsController < ApplicationController end def index - @pools = Pool.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) + @pools = Pool.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) respond_with(@pools) do |format| format.xml do render :xml => @pools.to_xml(:root => "pools") diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 62589e044..a06091ff7 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -15,7 +15,7 @@ class WikiPagesController < ApplicationController end def index - @wiki_pages = WikiPage.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) + @wiki_pages = WikiPage.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search]) respond_with(@wiki_pages) do |format| format.html do if params[:page].nil? || params[:page].to_i == 1 diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index abae53942..6b44f8cf0 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -135,6 +135,10 @@ module PostSets else temp = ::Post.tag_match(tag_string).where("true /* PostSets::Post#posts:2 */").paginate(page, :count => post_count, :limit => per_page) end + + # XXX HACK: uploader_name is needed in api responses and in data-uploader attribs (visible to mods only). + temp = temp.includes(:uploader) if format.to_sym != :html || CurrentUser.is_moderator? + temp.each # hack to force rails to eager load temp end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index fc2eb27dd..d73c4dd7a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -274,10 +274,6 @@ class ApplicationRecord < ActiveRecord::Base rec.creator_ip_addr = CurrentUser.ip_addr if rec.respond_to?(:creator_ip_addr=) end end - - define_method :creator_name do - User.id_to_name(creator_id) - end end end @@ -288,10 +284,6 @@ class ApplicationRecord < ActiveRecord::Base rec.updater_id = CurrentUser.id rec.updater_ip_addr = CurrentUser.ip_addr if rec.respond_to?(:updater_ip_addr=) end - - define_method :updater_name do - User.id_to_name(updater_id) - end end end end diff --git a/app/models/ban.rb b/app/models/ban.rb index 4d54228ee..ad19e6bf2 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -101,7 +101,7 @@ class Ban < ApplicationRecord end def user_name=(username) - self.user_id = User.name_to_id(username) + self.user = User.find_by_name(username) end def duration=(dur) diff --git a/app/models/comment.rb b/app/models/comment.rb index 4b604ef14..75eac8b4f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,8 +17,8 @@ class Comment < ApplicationRecord end mentionable( :message_field => :body, - :title => ->(user_name) {"#{creator_name} mentioned you in a comment on post ##{post_id}"}, - :body => ->(user_name) {"@#{creator_name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n"}, + :title => ->(user_name) {"#{creator.name} mentioned you in a comment on post ##{post_id}"}, + :body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n"}, ) module SearchMethods @@ -183,6 +183,14 @@ class Comment < ApplicationRecord super + [:creator_name, :updater_name] end + def creator_name + creator.name + end + + def updater_name + updater.name + end + def delete! update(is_deleted: true) end @@ -192,6 +200,6 @@ class Comment < ApplicationRecord end def quoted_response - DText.quote(body, creator_name) + DText.quote(body, creator.name) end end diff --git a/app/models/dmail.rb b/app/models/dmail.rb index f55613a82..922109dd5 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -49,7 +49,7 @@ class Dmail < ApplicationRecord module AddressMethods def to_name=(name) - self.to_id = User.name_to_id(name) + self.to = User.find_by_name(name) end def initialize_attributes diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index c157c0285..2993e7611 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -26,8 +26,8 @@ class ForumPost < ApplicationRecord end mentionable( :message_field => :body, - :title => ->(user_name) {%{#{creator_name} mentioned you in topic ##{topic_id} (#{topic.title})}}, - :body => ->(user_name) {%{@#{creator_name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n}}, + :title => ->(user_name) {%{#{creator.name} mentioned you in topic ##{topic_id} (#{topic.title})}}, + :body => ->(user_name) {%{@#{creator.name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n}}, ) module SearchMethods @@ -209,16 +209,8 @@ class ForumPost < ApplicationRecord self.is_deleted = false if is_deleted.nil? end - def creator_name - User.id_to_name(creator_id) - end - - def updater_name - User.id_to_name(updater_id) - end - def quoted_response - DText.quote(body, creator_name) + DText.quote(body, creator.name) end def forum_topic_page diff --git a/app/models/janitor_trial.rb b/app/models/janitor_trial.rb index b8996bd5a..37b5a0d24 100644 --- a/app/models/janitor_trial.rb +++ b/app/models/janitor_trial.rb @@ -30,7 +30,7 @@ class JanitorTrial < ApplicationRecord end def user_name=(name) - self.user_id = User.name_to_id(name) + self.user = User.find_by_name(name) end def send_dmail diff --git a/app/models/note.rb b/app/models/note.rb index d54dedb01..bcc6481b2 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -64,6 +64,10 @@ class Note < ApplicationRecord end end + def creator_name + creator.name + end + extend SearchMethods include ApiMethods diff --git a/app/models/pool.rb b/app/models/pool.rb index 19595c691..b55c074a9 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -289,6 +289,10 @@ class Pool < ApplicationRecord super + [:creator_name, :post_count] end + def creator_name + creator.name + end + def update_category_pseudo_tags_for_posts_async if saved_change_to_category? UpdatePoolPseudoTagsJob.perform_later(self) diff --git a/app/models/pool_archive.rb b/app/models/pool_archive.rb index f91ef9be4..ef30ca77d 100644 --- a/app/models/pool_archive.rb +++ b/app/models/pool_archive.rb @@ -95,10 +95,6 @@ class PoolArchive < ApplicationRecord User.find(updater_id) end - def updater_name - User.id_to_name(updater_id) - end - def pretty_name name.tr("_", " ") end diff --git a/app/models/post.rb b/app/models/post.rb index a80beb856..38264e126 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1015,7 +1015,7 @@ class Post < ApplicationRecord end def uploader_name - User.id_to_name(uploader_id) + uploader.name end end @@ -1507,7 +1507,7 @@ class Post < ApplicationRecord "created_at" => created_at.to_formatted_s(:db), "has_notes" => has_notes?, "rating" => rating, - "author" => uploader_name, + "author" => uploader.name, "creator_id" => uploader_id, "width" => image_width, "source" => source, diff --git a/app/models/post_archive.rb b/app/models/post_archive.rb index af35e3efb..991187b9c 100644 --- a/app/models/post_archive.rb +++ b/app/models/post_archive.rb @@ -256,6 +256,10 @@ class PostArchive < ApplicationRecord post.save! end + def updater_name + updater.name + end + def method_attributes super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name] end diff --git a/app/models/post_version.rb b/app/models/post_version.rb index 7607889fb..b64af481c 100644 --- a/app/models/post_version.rb +++ b/app/models/post_version.rb @@ -156,7 +156,7 @@ class PostVersion < ApplicationRecord end def updater_name - User.id_to_name(updater_id) + updater.name end def method_attributes diff --git a/app/models/upload.rb b/app/models/upload.rb index 49e99578c..f535baa61 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -155,7 +155,7 @@ class Upload < ApplicationRecord module UploaderMethods def uploader_name - User.id_to_name(uploader_id) + uploader.name end end diff --git a/app/models/user.rb b/app/models/user.rb index 10bb2b92f..2cf4f5f9b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,7 +21,6 @@ class User < ApplicationRecord :approver, :voter, :super_voter, - :verified, ] # candidates for removal: @@ -84,7 +83,6 @@ class User < ApplicationRecord before_validation :normalize_email before_create :encrypt_password_on_create before_update :encrypt_password_on_update - after_save :update_cache before_create :promote_to_admin_if_first_user before_create :customize_new_user has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy @@ -131,15 +129,7 @@ class User < ApplicationRecord concerning :NameMethods do class_methods do def name_to_id(name) - Cache.get("uni:#{Cache.hash(name)}", 4.hours) do - find_by_name(name).try(:id) - end - end - - def id_to_name(user_id) - Cache.get("uin:#{user_id}", 4.hours) do - find_by_id(user_id).try(:name) || Danbooru.config.default_guest_name - end + find_by_name(name).try(:id) end # XXX downcasing is the wrong way to do case-insensitive comparison for unicode (should use casefolding). @@ -155,11 +145,6 @@ class User < ApplicationRecord def pretty_name name.gsub(/([^_])_+(?=[^_])/, "\\1 \\2") end - - def update_cache - Cache.put("uin:#{id}", name, 4.hours) - Cache.put("uni:#{Cache.hash(name)}", id, 4.hours) - end end module PasswordMethods diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index 1f8009783..9bcb90138 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -9,10 +9,10 @@ class UserFeedback < ApplicationRecord validate :user_is_not_creator after_create :create_dmail, unless: :disable_dmail_notification after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_update) + ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user.name}":/users/#{rec.user_id}},:user_feedback_update) end after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_delete) + ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user.name}":/users/#{rec.user_id}},:user_feedback_delete) end module SearchMethods @@ -76,12 +76,8 @@ class UserFeedback < ApplicationRecord extend SearchMethods - def user_name - User.id_to_name(user_id) - end - def user_name=(name) - self.user_id = User.name_to_id(name) + self.user = User.find_by_name(name) end def disclaimer @@ -89,11 +85,11 @@ class UserFeedback < ApplicationRecord return nil end - "The purpose of feedback is to help you become a valuable member of the site by highlighting adverse behaviors. The author, #{creator_name}, should have sent you a message in the recent past as a warning. The fact that you're receiving this feedback now implies you've ignored their advice.\n\nYou can protest this feedback by petitioning the mods and admins in the forum. If #{creator_name} fails to provide sufficient evidence, you can have the feedback removed. However, if you fail to defend yourself against the accusations, you will likely earn yourself another negative feedback.\n\nNegative feedback generally doesn't affect your usability of the site. But it does mean other users may trust you less and give you less benefit of the doubt.\n\n" + "The purpose of feedback is to help you become a valuable member of the site by highlighting adverse behaviors. The author, #{creator.name}, should have sent you a message in the recent past as a warning. The fact that you're receiving this feedback now implies you've ignored their advice.\n\nYou can protest this feedback by petitioning the mods and admins in the forum. If #{creator.name} fails to provide sufficient evidence, you can have the feedback removed. However, if you fail to defend yourself against the accusations, you will likely earn yourself another negative feedback.\n\nNegative feedback generally doesn't affect your usability of the site. But it does mean other users may trust you less and give you less benefit of the doubt.\n\n" end def create_dmail - body = %{#{disclaimer}@#{creator_name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body}} + body = %{#{disclaimer}@#{creator.name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body}} Dmail.create_automated(:to_id => user_id, :title => "Your user record has been updated", :body => body) end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 8ab02896e..ccd39ebab 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -108,6 +108,10 @@ class WikiPage < ApplicationRecord end end + def creator_name + creator.name + end + extend SearchMethods include ApiMethods diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 6fb07d92a..cdac88196 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -117,7 +117,7 @@ class PostPresenter < Presenter } if CurrentUser.is_moderator? - attributes["data-uploader"] = post.uploader_name + attributes["data-uploader"] = post.uploader.name end if post.visible? diff --git a/app/views/artist_versions/_standard_listing.html.erb b/app/views/artist_versions/_standard_listing.html.erb index 3773d0715..8c12d3cef 100644 --- a/app/views/artist_versions/_standard_listing.html.erb +++ b/app/views/artist_versions/_standard_listing.html.erb @@ -37,7 +37,7 @@ <%= link_to_user artist_version.updater %> - <%= link_to "»", artist_versions_path(search: { updater_name: artist_version.updater_name }) %> + <%= link_to "»", artist_versions_path(search: { updater_name: artist_version.updater.name }) %>

<%= compact_time(artist_version.updated_at) %> <% if CurrentUser.is_moderator? %> diff --git a/app/views/comments/index.atom.builder b/app/views/comments/index.atom.builder index 14508448b..d89a15700 100644 --- a/app/views/comments/index.atom.builder +++ b/app/views/comments/index.atom.builder @@ -9,7 +9,7 @@ atom_feed(root_url: comments_url(host: Danbooru.config.hostname)) do |feed| @comments.each do |comment| feed.entry(comment, published: comment.created_at, updated: comment.updated_at) do |entry| - entry.title("@#{comment.creator_name} on post ##{comment.post_id} (#{comment.post.presenter.humanized_essential_tag_string})") + entry.title("@#{comment.creator.name} on post ##{comment.post_id} (#{comment.post.presenter.humanized_essential_tag_string})") entry.content(<<-EOS.strip_heredoc, type: "html") @@ -17,7 +17,7 @@ atom_feed(root_url: comments_url(host: Danbooru.config.hostname)) do |feed| EOS entry.author do |author| - author.name(comment.creator_name) + author.name(comment.creator.name) author.uri(user_url(comment.creator)) end end diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index d73289c51..6eb97c8a8 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -1,6 +1,6 @@ <% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %> -

" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator_name %>" data-is-sticky="<%= comment.is_sticky %>"> +
" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator.name %>" data-is-sticky="<%= comment.is_sticky %>">

<%= link_to_user comment.creator %> diff --git a/app/views/posts/index.atom.erb b/app/views/posts/index.atom.erb index a03daa71d..3fcfe4d7e 100644 --- a/app/views/posts/index.atom.erb +++ b/app/views/posts/index.atom.erb @@ -29,7 +29,7 @@

- <%= post.uploader_name %> + <%= post.uploader.name %> <% end %> diff --git a/app/views/posts/partials/show/_image.html.erb b/app/views/posts/partials/show/_image.html.erb index 0d7629b7a..6255a146a 100644 --- a/app/views/posts/partials/show/_image.html.erb +++ b/app/views/posts/partials/show/_image.html.erb @@ -1,3 +1,3 @@ <% if post.visible? %> - <%= image_tag(post.file_url_for(CurrentUser.user), :width => post.image_width_for(CurrentUser.user), :height => post.image_height_for(CurrentUser.user), :id => "image", "data-original-width" => post.image_width, "data-original-height" => post.image_height, "data-large-width" => post.large_image_width, "data-large-height" => post.large_image_height, "data-tags" => post.tag_string, :alt => post.presenter.humanized_essential_tag_string, "data-uploader" => post.uploader_name, "data-rating" => post.rating, "data-flags" => post.status_flags, "data-parent-id" => post.parent_id, "data-has-children" => post.has_children?, "data-has-active-children" => post.has_active_children?, "data-score" => post.score, "data-fav-count" => post.fav_count, "itemprop" => "contentUrl") %> + <%= image_tag(post.file_url_for(CurrentUser.user), :width => post.image_width_for(CurrentUser.user), :height => post.image_height_for(CurrentUser.user), :id => "image", "data-original-width" => post.image_width, "data-original-height" => post.image_height, "data-large-width" => post.large_image_width, "data-large-height" => post.large_image_height, "data-tags" => post.tag_string, :alt => post.presenter.humanized_essential_tag_string, "data-uploader" => post.uploader.name, "data-rating" => post.rating, "data-flags" => post.status_flags, "data-parent-id" => post.parent_id, "data-has-children" => post.has_children?, "data-has-active-children" => post.has_active_children?, "data-score" => post.score, "data-fav-count" => post.fav_count, "itemprop" => "contentUrl") %> <% end %> diff --git a/app/views/posts/partials/show/_ugoira_original.html.erb b/app/views/posts/partials/show/_ugoira_original.html.erb index 6781742e6..24ab12600 100644 --- a/app/views/posts/partials/show/_ugoira_original.html.erb +++ b/app/views/posts/partials/show/_ugoira_original.html.erb @@ -9,7 +9,7 @@ "data-large-width" => post.image_width, "data-large-height" => post.image_height, "data-tags" => post.tag_string, - "data-uploader" => post.uploader_name, + "data-uploader" => post.uploader.name, "data-rating" => post.rating, "data-flags" => post.status_flags, "data-parent-id" => post.parent_id, diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index d23e03749..cfc6350c9 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -120,7 +120,7 @@ <% if !CurrentUser.user.is_builder? %>

Before commenting, read the <%= link_to "how to comment guide", wiki_pages_path(:search => {:title => "howto:comment"}) %>.

<% end %> - <%= render "comments/partials/index/list", :comments => @post.comments.visible(CurrentUser.user), :post => @post, :show_header => false %> + <%= render "comments/partials/index/list", :comments => @post.comments.visible(CurrentUser.user).includes(:creator), :post => @post, :show_header => false %>