rubocop: fix various Rubocop warnings.

This commit is contained in:
evazion
2021-06-16 18:24:42 -05:00
parent cfe471e0b5
commit 07e23204b6
99 changed files with 412 additions and 374 deletions

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class Artist < ::Struct.new(:user, :count)
def self.all(min_date, max_level)
::ArtistVersion.joins(:updater)
::ArtistVersion
.joins(:updater)
.where("artist_versions.created_at > ?", min_date)
.where("users.level <= ?", max_level)
.group(:updater)

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class Comment < ::Struct.new(:comment, :count)
def self.all(min_date, max_level)
::CommentVote.joins(comment: [:creator])
::CommentVote
.joins(comment: [:creator])
.where("comments.score < 0")
.where("comment_votes.created_at > ?", min_date)
.where("users.level <= ?", max_level)

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class Note < ::Struct.new(:user, :count)
def self.all(min_date, max_level)
::NoteVersion.joins(:updater)
::NoteVersion
.joins(:updater)
.where("note_versions.created_at > ?", min_date)
.where("users.level <= ?", max_level)
.group(:updater)

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class PostAppeal
def self.all(min_date)
::Post.joins(:appeals).includes(:uploader, :flags, appeals: [:creator])
::Post
.joins(:appeals).includes(:uploader, :flags, appeals: [:creator])
.deleted
.where("post_appeals.created_at > ?", min_date)
.group(:id)

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class Upload < ::Struct.new(:user, :count)
def self.all(min_date, max_level)
::Post.joins(:uploader)
::Post
.joins(:uploader)
.where("posts.created_at > ?", min_date)
.where("users.level <= ?", max_level)
.group(:uploader)

View File

@@ -3,7 +3,8 @@ module Moderator
module Queries
class WikiPage < ::Struct.new(:user, :count)
def self.all(min_date, max_level)
::WikiPageVersion.joins(:updater)
::WikiPageVersion
.joins(:updater)
.where("wiki_page_versions.created_at > ?", min_date)
.where("users.level <= ?", max_level)
.group(:updater)

View File

@@ -62,7 +62,7 @@ module Moderator
end
def add_row(sums, counts)
sums.merge!(counts) { |key, oldcount, newcount| oldcount + newcount }
sums.merge!(counts) { |_key, oldcount, newcount| oldcount + newcount }
end
def add_row_id(sums, counts)

View File

@@ -11,9 +11,10 @@ class NicoSeigaApiClient
end
def image_ids
if @work_type == "illust"
case @work_type
when "illust"
[api_response["id"]]
elsif @work_type == "manga"
when "manga"
manga_api_response.map do |x|
case x["meta"]["source_url"]
when %r{/thumb/(\d+)\w}i then Regexp.last_match(1)
@@ -40,20 +41,22 @@ class NicoSeigaApiClient
end
def user_name
if @work_type == "illust"
case @work_type
when "illust"
api_response["nickname"]
elsif @work_type == "manga"
when "manga"
user_api_response(user_id)["nickname"]
end
end
def api_response
if @work_type == "illust"
case @work_type
when "illust"
resp = get("https://sp.seiga.nicovideo.jp/ajax/seiga/im#{@work_id}")
return {} if resp.blank? || resp.code.to_i == 404
api_response = JSON.parse(resp)["target_image"]
elsif @work_type == "manga"
when "manga"
resp = http.cache(1.minute).get("#{XML_API}/theme/info?id=#{@work_id}")
return {} if resp.blank? || resp.code.to_i == 404
api_response = Hash.from_xml(resp.to_s)["response"]["theme"]
@@ -86,16 +89,13 @@ class NicoSeigaApiClient
# XXX should fail gracefully instead of raising exception
resp = http.cache(1.hour).post("https://account.nicovideo.jp/login/redirector?site=seiga", form: form)
raise RuntimeError, "NicoSeiga login failed (status=#{resp.status} url=#{url})" if resp.status != 200
raise "NicoSeiga login failed (status=#{resp.status} url=#{url})" if resp.status != 200
http
end
def get(url)
resp = login.cache(1.minute).get(url)
#raise RuntimeError, "NicoSeiga get failed (status=#{resp.status} url=#{url})" if resp.status != 200
resp
login.cache(1.minute).get(url)
end
memoize :api_response, :manga_api_response, :user_api_response

View File

@@ -1,19 +1,19 @@
module NoteSanitizer
ALLOWED_ELEMENTS = %w(
ALLOWED_ELEMENTS = %w[
code center tn h1 h2 h3 h4 h5 h6 a span div blockquote br p ul li ol em
strong small big b i font u s pre ruby rb rt rp rtc sub sup hr wbr
)
]
ALLOWED_ATTRIBUTES = {
:all => %w(style title),
"a" => %w(href),
"span" => %w(class),
"div" => %w(class align),
"p" => %w(class align),
"font" => %w(color size)
:all => %w[style title],
"a" => %w[href],
"span" => %w[class],
"div" => %w[class align],
"p" => %w[class align],
"font" => %w[color size]
}
ALLOWED_PROPERTIES = %w(
ALLOWED_PROPERTIES = %w[
align-items
background background-color
border border-color border-image border-radius border-style border-width
@@ -48,7 +48,7 @@ module NoteSanitizer
word-wrap overflow-wrap
writing-mode
vertical-align
)
]
def self.sanitize(text)
text.gsub!(/<( |-|3|:|>|\Z)/, "&lt;\\1")

View File

@@ -46,21 +46,23 @@ module PaginationExtension
end
def is_first_page?
if paginator_mode == :numbered
case paginator_mode
when :numbered
current_page == 1
elsif paginator_mode == :sequential_before
when :sequential_before
false
elsif paginator_mode == :sequential_after
when :sequential_after
size <= records_per_page
end
end
def is_last_page?
if paginator_mode == :numbered
case paginator_mode
when :numbered
current_page >= total_pages
elsif paginator_mode == :sequential_before
when :sequential_before
size <= records_per_page
elsif paginator_mode == :sequential_after
when :sequential_after
false
end
end
@@ -98,11 +100,12 @@ module PaginationExtension
# we override a rails internal method to discard that extra record. See
# #2044, #3642.
def records
if paginator_mode == :sequential_before
case paginator_mode
when :sequential_before
super.first(records_per_page)
elsif paginator_mode == :sequential_after
when :sequential_after
super.first(records_per_page).reverse
elsif paginator_mode == :numbered
when :numbered
super
end
end

View File

@@ -20,7 +20,7 @@ module PostSets
end
def has_blank_wiki?
tag.present? && !wiki_page.present?
tag.present? && wiki_page.nil?
end
def wiki_page
@@ -94,7 +94,7 @@ module PostSets
end
def get_post_count
if %w(json atom xml).include?(format.downcase)
if %w[json atom xml].include?(format.downcase)
# no need to get counts for formats that don't use a paginator
nil
else

View File

@@ -17,14 +17,14 @@ class RateLimiter
case action
when "users:create"
rate, burst = 1.0/5.minutes, 10
rate, burst = 1.0 / 5.minutes, 10
when "emails:update", "sessions:create", "moderation_reports:create"
rate, burst = 1.0/1.minute, 10
rate, burst = 1.0 / 1.minute, 10
when "dmails:create", "comments:create", "forum_posts:create", "forum_topics:create"
rate, burst = 1.0/1.minute, 50
rate, burst = 1.0 / 1.minute, 50
when "comment_votes:create", "comment_votes:destroy", "post_votes:create",
"post_votes:destroy", "favorites:create", "favorites:destroy", "post_disapprovals:create"
rate, burst = 1.0/1.second, 200
rate, burst = 1.0 / 1.second, 200
else
rate = user.api_regen_multiplier
burst = 200

View File

@@ -55,7 +55,7 @@ module RecommenderService
if user.present?
raise User::PrivilegeError unless Pundit.policy!(CurrentUser.user, user).can_see_favorites?
max_recommendations = params.fetch(:max_recommendations, user.favorite_count + 500).to_i.clamp(0, 50000)
max_recommendations = params.fetch(:max_recommendations, user.favorite_count + 500).to_i.clamp(0, 50_000)
recs = RecommenderService.recommend_for_user(user, tags: params[:post_tags_match], limit: max_recommendations)
elsif post.present?
max_recommendations = params.fetch(:max_recommendations, 100).to_i.clamp(0, 1000)

View File

@@ -10,7 +10,7 @@ class ReportbooruService
reportbooru_server.present?
end
def missed_search_rankings(expires_in: 1.minutes)
def missed_search_rankings(expires_in: 1.minute)
return [] unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches")
@@ -20,11 +20,11 @@ class ReportbooruService
body.lines.map(&:split).map { [_1, _2.to_i] }
end
def post_search_rankings(date, expires_in: 1.minutes)
def post_search_rankings(date, expires_in: 1.minute)
request("#{reportbooru_server}/post_searches/rank?date=#{date}", expires_in)
end
def post_view_rankings(date, expires_in: 1.minutes)
def post_view_rankings(date, expires_in: 1.minute)
request("#{reportbooru_server}/post_views/rank?date=#{date}", expires_in)
end

View File

@@ -156,12 +156,12 @@ module Sources::Strategies
"https://twitter.com/i/web/status/#{status_id}"
elsif url =~ %r{\Ahttps?://(?:o|image-proxy-origin)\.twimg\.com/\d/proxy\.jpg\?t=(\w+)&}i
str = Base64.decode64($1)
source = URI.extract(str, ['http', 'https'])
source = URI.extract(str, %w[http https])
if source.any?
source = source[0]
if source =~ %r{^https?://twitpic.com/show/large/[a-z0-9]+}i
source.gsub!(%r{show/large/}, "")
index = source.rindex('.')
index = source.rindex(".")
source = source[0..index - 1]
end
source

View File

@@ -68,7 +68,7 @@ class TagCategory
end
def category_ids_regex
@@category_ids_regex ||= "[#{category_ids.join("")}]"
@@category_ids_regex ||= "[#{category_ids.join}]"
end
end

View File

@@ -82,7 +82,7 @@ class TagMover
old_cosplay_tag = "#{old_tag.name}_(cosplay)"
new_cosplay_tag = "#{new_tag.name}_(cosplay)"
if Tag.nonempty.where(name: old_cosplay_tag).exists?
if Tag.nonempty.exists?(name: old_cosplay_tag)
TagMover.new(old_cosplay_tag, new_cosplay_tag).move!
end
end

View File

@@ -16,7 +16,7 @@ module TagRelationshipRetirementService
if topic.nil?
CurrentUser.as(User.system) do
topic = ForumTopic.create!(creator: User.system, title: forum_topic_title, category_id: 1)
forum_post = ForumPost.create!(creator: User.system, body: forum_topic_body, topic: topic)
ForumPost.create!(creator: User.system, body: forum_topic_body, topic: topic)
end
end
topic
@@ -45,6 +45,6 @@ module TagRelationshipRetirementService
end
def is_unused?(name)
!Post.raw_tag_match(name).where("created_at > ?", THRESHOLD.ago).exists?
!Post.raw_tag_match(name).exists?(["created_at > ?", THRESHOLD.ago])
end
end

View File

@@ -1,8 +1,3 @@
require 'upload_service/controller_helper'
require 'upload_service/preprocessor'
require 'upload_service/replacer'
require 'upload_service/utils'
class UploadService
attr_reader :params, :post, :upload

View File

@@ -34,14 +34,12 @@ class UploadService
def in_progress?
if md5.present?
return Upload.where(status: "preprocessing", md5: md5).exists?
Upload.exists?(status: "preprocessing", md5: md5)
elsif Utils.is_downloadable?(source)
Upload.exists?(status: "preprocessing", source: source)
else
false
end
if Utils.is_downloadable?(source)
return Upload.where(status: "preprocessing", source: source).exists?
end
false
end
def predecessor

View File

@@ -11,7 +11,7 @@ class UploadService
end
def comment_replacement_message(post, replacement)
%("#{replacement.creator.name}":[#{Routes.user_path(replacement.creator)}] replaced this post with a new file:\n\n#{replacement_message(post, replacement)})
%{"#{replacement.creator.name}":[#{Routes.user_path(replacement.creator)}] replaced this post with a new file:\n\n#{replacement_message(post, replacement)}}
end
def replacement_message(post, replacement)
@@ -49,7 +49,7 @@ class UploadService
truncated_source = source.gsub(%r{\Ahttps?://}, "").truncate(64, omission: "...#{source.last(32)}")
if source =~ %r{\Ahttps?://}i
%("#{truncated_source}":[#{source}])
%{"#{truncated_source}":[#{source}]}
else
truncated_source
end
@@ -70,7 +70,7 @@ class UploadService
return "file://#{repl.replacement_file.original_filename}"
end
if !upload.source.present?
if upload.source.blank?
raise "No source found in upload for replacement"
end

View File

@@ -14,7 +14,7 @@ class UploadService
end
def is_downloadable?(source)
source =~ /^https?:\/\//
source =~ %r{\Ahttps?://}
end
def generate_resizes(media_file)

View File

@@ -42,8 +42,8 @@ class UserDeletion
user.email_address = nil
user.last_logged_in_at = nil
user.last_forum_read_at = nil
user.favorite_tags = ''
user.blacklisted_tags = ''
user.favorite_tags = ""
user.blacklisted_tags = ""
user.hide_deleted_posts = false
user.show_deleted_children = false
user.time_zone = "Eastern Time (US & Canada)"