Merge pull request #3957 from evazion/fix-related-tags

Related tags: build html server-side, eliminate cookies
This commit is contained in:
Albert Yi
2018-10-24 17:34:14 -07:00
committed by GitHub
24 changed files with 282 additions and 426 deletions

View File

@@ -1,11 +1,16 @@
class RelatedTagQuery
attr_reader :query, :category
attr_reader :query, :category, :user
def initialize(query, category = nil)
@query = TagAlias.to_aliased(query.strip).join(" ")
def initialize(query: nil, category: nil, user: nil)
@user = user
@query = TagAlias.to_aliased(query.to_s.downcase.strip).join(" ")
@category = category
end
def pretty_name
query.tr("_", " ")
end
def tags
if query =~ /\*/
pattern_matching_tags
@@ -18,6 +23,21 @@ 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(&: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
def wiki_page_tags
results = wiki_page.try(:tags) || []
results.reject! do |name|
@@ -27,17 +47,12 @@ class RelatedTagQuery
end
def other_wiki_category_tags
if Tag.category_for(query) != Tag.categories.copyright
return []
end
listtags = (wiki_page.try(:tags) || []).select {|name| name =~ /^list_of_/i }
results = listtags.map do |name|
listlinks = WikiPage.titled(name).first.try(:tags) || []
if listlinks.length > 0
{"title" => name, "wiki_page_tags" => map_with_category_data(listlinks)}
end
end
results.reject {|list| list.nil?}
return [] unless Tag.category_for(query) == Tag.categories.copyright
other_wikis = wiki_page&.tags.to_a.grep(/^list_of_/i)
other_wikis = other_wikis.map { |name| WikiPage.titled(name).first }
other_wikis = other_wikis.select { |wiki| wiki.tags.present? }
other_wikis
end
def tags_for_html

View File

@@ -28,7 +28,6 @@ class SessionLoader
update_last_logged_in_at
update_last_ip_addr
set_time_zone
store_favorite_tags_in_cookies
CurrentUser.user.unban! if CurrentUser.user.ban_expired?
end
@@ -92,14 +91,6 @@ private
cookies[:password_hash] && cookies.signed[:user_name] && User.authenticate_cookie_hash(cookies.signed[:user_name], cookies[:password_hash])
end
def store_favorite_tags_in_cookies
if (cookies[:favorite_tags].blank? || cookies[:favorite_tags_with_categories].blank?) && CurrentUser.user.favorite_tags.present?
favorite_tags = CurrentUser.user.favorite_tags.slice(0, 1024)
cookies[:favorite_tags] = favorite_tags
cookies[:favorite_tags_with_categories] = Tag.categories_for(favorite_tags.split(/[[:space:]]+/)).to_a.flatten.join(" ")
end
end
def update_last_logged_in_at
return if CurrentUser.is_anonymous?
return if CurrentUser.last_logged_in_at && CurrentUser.last_logged_in_at > 1.week.ago