diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 3e9fd8e62..5fca85ba8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -46,7 +46,6 @@ class PostsController < ApplicationController @post = Post.find(params[:id]) @post.update(post_params) if @post.visible? - save_recent_tags respond_with_post_after_update(@post) end @@ -96,15 +95,6 @@ private params[:tags] || (params[:post] && params[:post][:tags]) end - def save_recent_tags - if @post - tags = Tag.scan_tags(@post.tag_string) - tags = (TagAlias.to_aliased(tags) + Tag.scan_tags(cookies[:recent_tags])).uniq.slice(0, 30) - cookies[:recent_tags] = tags.join(" ") - cookies[:recent_tags_with_categories] = Tag.categories_for(tags).to_a.flatten.join(" ") - end - end - def respond_with_post_after_update(post) respond_with(post) do |format| format.html do diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 5af7fa47e..e70bb60e8 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -58,21 +58,11 @@ class UploadsController < ApplicationController flash[:notice] = @service.warnings.join(".\n \n") end - save_recent_tags respond_with(@upload) end private - def save_recent_tags - if @upload - tags = Tag.scan_tags(@upload.tag_string) - tags = (TagAlias.to_aliased(tags) + Tag.scan_tags(cookies[:recent_tags])).compact.uniq.slice(0, 30) - cookies[:recent_tags] = tags.join(" ") - cookies[:recent_tags_with_categories] = Tag.categories_for(tags).to_a.flatten.join(" ") - end - end - def upload_params permitted_params = %i[ file source tag_string rating status parent_id artist_commentary_title diff --git a/app/logical/related_tag_query.rb b/app/logical/related_tag_query.rb index 1464c23bf..57f3f0717 100644 --- a/app/logical/related_tag_query.rb +++ b/app/logical/related_tag_query.rb @@ -26,6 +26,17 @@ class RelatedTagQuery end end + # Returns the top 20 most frequently added tags within the last 20 edits made by the user in the last hour. + def recent_tags(since: 1.hour.ago, max_edits: 20, max_tags: 20) + return [] unless user.present? && PostArchive.enabled? + + versions = PostArchive.where(updater_id: user.id).where("updated_at > ?", since).order(id: :desc).limit(max_edits) + tags = versions.flat_map { |v| v.diff[:added_tags] } + tags = tags.reject { |tag| Tag.is_metatag?(tag) } + tags = tags.group_by(&:itself).transform_values(&:size).sort_by { |tag, count| [-count, tag] }.map(&:first) + tags.take(max_tags) + end + def favorite_tags user&.favorite_tags.to_s.split end diff --git a/app/views/related_tags/_related_tags.html.erb b/app/views/related_tags/_related_tags.html.erb index 7ebb38e94..d04fca311 100644 --- a/app/views/related_tags/_related_tags.html.erb +++ b/app/views/related_tags/_related_tags.html.erb @@ -2,7 +2,7 @@