DEV: Update rubocop-discourse to latest version

The lastest version of rubocop-discourse enables rules regarding
plugins.
This commit is contained in:
Loïc Guitaut 2024-02-01 17:28:10 +01:00 committed by Loïc Guitaut
parent 7a6ba47e7b
commit f7d7092a7a
27 changed files with 169 additions and 118 deletions

View File

@ -8,6 +8,30 @@ Discourse/NoAddReferenceOrAliasesActiveRecordMigration:
Discourse/NoResetColumnInformationInMigrations: Discourse/NoResetColumnInformationInMigrations:
Enabled: true Enabled: true
Discourse/Plugins/CallRequiresPlugin:
Include:
- "plugins/*/app/controllers/**/*"
Discourse/Plugins/UsePluginInstanceOn:
Include:
- "plugins/**/*"
Discourse/Plugins/NamespaceMethods:
Include:
- "plugins/**/*"
Discourse/Plugins/NamespaceConstants:
Include:
- "plugins/**/*"
Discourse/Plugins/UseRequireRelative:
Include:
- "plugins/**/*"
Discourse/Plugins/NoMonkeyPatching:
Include:
- "plugins/**/*"
Lint/Debugger: Lint/Debugger:
Exclude: Exclude:
- script/**/* - script/**/*

View File

@ -305,6 +305,7 @@ GEM
racc racc
pg (1.5.4) pg (1.5.4)
prettier_print (1.2.1) prettier_print (1.2.1)
prism (0.24.0)
progress (3.6.0) progress (3.6.0)
pry (0.14.2) pry (0.14.2)
coderay (~> 1.1) coderay (~> 1.1)
@ -417,12 +418,16 @@ GEM
rubocop-ast (>= 1.30.0, < 2.0) rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0) unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.1) rubocop-ast (1.31.0)
parser (>= 3.3.0.4) parser (>= 3.3.0.4)
prism (>= 0.24.0)
rubocop-capybara (2.20.0) rubocop-capybara (2.20.0)
rubocop (~> 1.41) rubocop (~> 1.41)
rubocop-discourse (3.6.1) rubocop-discourse (3.7.1)
activesupport (>= 6.1)
rubocop (>= 1.59.0) rubocop (>= 1.59.0)
rubocop-capybara (>= 2.0.0)
rubocop-factory_bot (>= 2.0.0)
rubocop-rspec (>= 2.25.0) rubocop-rspec (>= 2.25.0)
rubocop-factory_bot (2.25.1) rubocop-factory_bot (2.25.1)
rubocop (~> 1.41) rubocop (~> 1.41)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Chat::Api::CategoryChatablesController < ApplicationController class Chat::Api::CategoryChatablesController < ApplicationController
requires_plugin Chat::PLUGIN_NAME
def permissions def permissions
category = Category.find(params[:id]) category = Category.find(params[:id])

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
CHANNEL_EDITABLE_PARAMS ||= %i[name description slug] class Chat::Api::ChannelsController < Chat::ApiController
CATEGORY_CHANNEL_EDITABLE_PARAMS ||= %i[ CHANNEL_EDITABLE_PARAMS ||= %i[name description slug]
CATEGORY_CHANNEL_EDITABLE_PARAMS ||= %i[
auto_join_users auto_join_users
allow_channel_wide_mentions allow_channel_wide_mentions
threading_enabled threading_enabled
] ]
class Chat::Api::ChannelsController < Chat::ApiController
def index def index
permitted = params.permit(:filter, :limit, :offset, :status) permitted = params.permit(:filter, :limit, :offset, :status)

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
MEMBERSHIP_EDITABLE_PARAMS = %i[muted desktop_notification_level mobile_notification_level]
class Chat::Api::ChannelsCurrentUserNotificationsSettingsController < Chat::Api::ChannelsController class Chat::Api::ChannelsCurrentUserNotificationsSettingsController < Chat::Api::ChannelsController
MEMBERSHIP_EDITABLE_PARAMS = %i[muted desktop_notification_level mobile_notification_level]
def update def update
settings_params = params.require(:notifications_settings).permit(MEMBERSHIP_EDITABLE_PARAMS) settings_params = params.require(:notifications_settings).permit(MEMBERSHIP_EDITABLE_PARAMS)
membership_from_params.update!(settings_params.to_h) membership_from_params.update!(settings_params.to_h)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Chat::Api::HintsController < ApplicationController class Chat::Api::HintsController < ApplicationController
requires_plugin Chat::PLUGIN_NAME
before_action :ensure_logged_in before_action :ensure_logged_in
def check_group_mentions def check_group_mentions

View File

@ -2,18 +2,10 @@
module Chat module Chat
class ApiController < ::Chat::BaseController class ApiController < ::Chat::BaseController
before_action :ensure_logged_in
before_action :ensure_can_chat
include Chat::WithServiceHelper include Chat::WithServiceHelper
private private
def ensure_can_chat
raise Discourse::NotFound unless SiteSetting.chat_enabled
guardian.ensure_can_chat!
end
def default_actions_for_service def default_actions_for_service
proc do proc do
on_success { render(json: success_json) } on_success { render(json: success_json) }

View File

@ -2,13 +2,14 @@
module Chat module Chat
class BaseController < ::ApplicationController class BaseController < ::ApplicationController
requires_plugin Chat::PLUGIN_NAME
before_action :ensure_logged_in before_action :ensure_logged_in
before_action :ensure_can_chat before_action :ensure_can_chat
private private
def ensure_can_chat def ensure_can_chat
raise Discourse::NotFound unless SiteSetting.chat_enabled
guardian.ensure_can_chat! guardian.ensure_can_chat!
end end

View File

@ -33,6 +33,10 @@ GlobalSetting.add_default(:allow_unsecure_chat_uploads, false)
module ::Chat module ::Chat
PLUGIN_NAME = "chat" PLUGIN_NAME = "chat"
RETENTION_SETTINGS_TO_USER_OPTION_FIELDS = {
chat_channel_retention_days: :dismissed_channel_retention_reminder,
chat_dm_retention_days: :dismissed_dm_retention_reminder,
}
end end
require_relative "lib/chat/engine" require_relative "lib/chat/engine"
@ -70,7 +74,7 @@ after_initialize do
Group.prepend Chat::GroupExtension Group.prepend Chat::GroupExtension
Jobs::UserEmail.prepend Chat::UserEmailExtension Jobs::UserEmail.prepend Chat::UserEmailExtension
Plugin::Instance.prepend Chat::PluginInstanceExtension Plugin::Instance.prepend Chat::PluginInstanceExtension
Jobs::ExportCsvFile.class_eval { prepend Chat::MessagesExporter } Jobs::ExportCsvFile.prepend Chat::MessagesExporter
WebHook.prepend Chat::OutgoingWebHookExtension WebHook.prepend Chat::OutgoingWebHookExtension
end end
@ -270,12 +274,8 @@ after_initialize do
include_condition: -> { SiteSetting.chat_enabled && SiteSetting.create_thumbnails }, include_condition: -> { SiteSetting.chat_enabled && SiteSetting.create_thumbnails },
) { object.thumbnail } ) { object.thumbnail }
RETENTION_SETTINGS_TO_USER_OPTION_FIELDS = {
chat_channel_retention_days: :dismissed_channel_retention_reminder,
chat_dm_retention_days: :dismissed_dm_retention_reminder,
}
on(:site_setting_changed) do |name, old_value, new_value| on(:site_setting_changed) do |name, old_value, new_value|
user_option_field = RETENTION_SETTINGS_TO_USER_OPTION_FIELDS[name.to_sym] user_option_field = Chat::RETENTION_SETTINGS_TO_USER_OPTION_FIELDS[name.to_sym]
begin begin
if user_option_field && old_value != new_value && !new_value.zero? if user_option_field && old_value != new_value && !new_value.zero?
UserOption.where(user_option_field => true).update_all(user_option_field => false) UserOption.where(user_option_field => true).update_all(user_option_field => false)
@ -342,10 +342,9 @@ after_initialize do
nil nil
end end
CHAT_NOTIFICATION_TYPES = [Notification.types[:chat_mention], Notification.types[:chat_message]]
register_push_notification_filter do |user, payload| register_push_notification_filter do |user, payload|
if user.user_option.only_chat_push_notifications && user.user_option.chat_enabled if user.user_option.only_chat_push_notifications && user.user_option.chat_enabled
CHAT_NOTIFICATION_TYPES.include?(payload[:notification_type]) payload[:notification_type].in?(::Notification.types.values_at(:chat_mention, :chat_message))
else else
true true
end end

View File

@ -19,7 +19,7 @@ after_initialize do
POST_CUSTOM_FIELD ||= "local_dates".freeze POST_CUSTOM_FIELD ||= "local_dates".freeze
end end
%w[../lib/discourse_local_dates/engine.rb].each { |path| load File.expand_path(path, __FILE__) } require_relative "lib/discourse_local_dates/engine"
register_post_custom_field_type(DiscourseLocalDates::POST_CUSTOM_FIELD, :json) register_post_custom_field_type(DiscourseLocalDates::POST_CUSTOM_FIELD, :json)

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module DiscourseNarrativeBot
module PostGuardianExtension
extend ActiveSupport::Concern
prepended do
alias_method :existing_can_create_post?, :can_create_post?
def can_create_post?(parent)
if SiteSetting.discourse_narrative_bot_enabled &&
parent.try(:subtype) == "system_message" &&
parent.try(:user) == ::DiscourseNarrativeBot::Base.new.discobot_user
return true
end
existing_can_create_post?(parent)
end
end
end
end

View File

@ -21,7 +21,7 @@ if Rails.env == "development"
Rails.configuration.autoload_paths << File.expand_path("../autoload/jobs", __FILE__) Rails.configuration.autoload_paths << File.expand_path("../autoload/jobs", __FILE__)
end end
require_relative "lib/discourse_narrative_bot/welcome_post_type_site_setting.rb" require_relative "lib/discourse_narrative_bot/welcome_post_type_site_setting"
register_asset "stylesheets/discourse-narrative-bot.scss" register_asset "stylesheets/discourse-narrative-bot.scss"
after_initialize do after_initialize do
@ -32,24 +32,23 @@ after_initialize do
Mime::Type.register "image/svg+xml", :svg Mime::Type.register "image/svg+xml", :svg
%w[ require_relative "autoload/jobs/regular/bot_input"
../autoload/jobs/regular/bot_input.rb require_relative "autoload/jobs/regular/narrative_timeout"
../autoload/jobs/regular/narrative_timeout.rb require_relative "autoload/jobs/regular/narrative_init"
../autoload/jobs/regular/narrative_init.rb require_relative "autoload/jobs/regular/send_default_welcome_message"
../autoload/jobs/regular/send_default_welcome_message.rb require_relative "autoload/jobs/onceoff/discourse_narrative_bot/grant_badges"
../autoload/jobs/onceoff/discourse_narrative_bot/grant_badges.rb require_relative "autoload/jobs/onceoff/discourse_narrative_bot/remap_old_bot_images"
../autoload/jobs/onceoff/discourse_narrative_bot/remap_old_bot_images.rb require_relative "lib/discourse_narrative_bot/actions"
../lib/discourse_narrative_bot/actions.rb require_relative "lib/discourse_narrative_bot/base"
../lib/discourse_narrative_bot/base.rb require_relative "lib/discourse_narrative_bot/new_user_narrative"
../lib/discourse_narrative_bot/new_user_narrative.rb require_relative "lib/discourse_narrative_bot/advanced_user_narrative"
../lib/discourse_narrative_bot/advanced_user_narrative.rb require_relative "lib/discourse_narrative_bot/track_selector"
../lib/discourse_narrative_bot/track_selector.rb require_relative "lib/discourse_narrative_bot/certificate_generator"
../lib/discourse_narrative_bot/certificate_generator.rb require_relative "lib/discourse_narrative_bot/dice"
../lib/discourse_narrative_bot/dice.rb require_relative "lib/discourse_narrative_bot/quote_generator"
../lib/discourse_narrative_bot/quote_generator.rb require_relative "lib/discourse_narrative_bot/magic_8_ball"
../lib/discourse_narrative_bot/magic_8_ball.rb require_relative "lib/discourse_narrative_bot/welcome_post_type_site_setting"
../lib/discourse_narrative_bot/welcome_post_type_site_setting.rb require_relative "lib/discourse_narrative_bot/post_guardian_extension"
].each { |path| load File.expand_path(path, __FILE__) }
RailsMultisite::ConnectionManagement.safe_each_connection do RailsMultisite::ConnectionManagement.safe_each_connection do
if SiteSetting.discourse_narrative_bot_enabled if SiteSetting.discourse_narrative_bot_enabled
@ -315,16 +314,5 @@ after_initialize do
end end
end end
PostGuardian.class_eval do PostGuardian.prepend(DiscourseNarrativeBot::PostGuardianExtension)
alias_method :existing_can_create_post?, :can_create_post?
def can_create_post?(parent)
if SiteSetting.discourse_narrative_bot_enabled && parent.try(:subtype) == "system_message" &&
parent.try(:user) == ::DiscourseNarrativeBot::Base.new.discobot_user
return true
end
existing_can_create_post?(parent)
end
end
end end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
module DiscoursePoll
module PostExtension
extend ActiveSupport::Concern
prepended do
attr_accessor :extracted_polls
has_many :polls, dependent: :destroy
after_save do
polls = self.extracted_polls
self.extracted_polls = nil
next if polls.blank? || !polls.is_a?(Hash)
post = self
::Poll.transaction do
polls.values.each { |poll| DiscoursePoll::Poll.create!(post.id, poll) }
post.custom_fields[DiscoursePoll::HAS_POLLS] = true
post.save_custom_fields(true)
end
end
end
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
module DiscoursePoll
module UserExtension
extend ActiveSupport::Concern
prepended { has_many :poll_votes, dependent: :delete_all }
end
end

View File

@ -32,17 +32,19 @@ after_initialize do
end end
end end
require_relative "app/controllers/polls_controller.rb" require_relative "app/controllers/polls_controller"
require_relative "app/models/poll_option.rb" require_relative "app/models/poll_option"
require_relative "app/models/poll_vote.rb" require_relative "app/models/poll_vote"
require_relative "app/models/poll.rb" require_relative "app/models/poll"
require_relative "app/serializers/poll_option_serializer.rb" require_relative "app/serializers/poll_option_serializer"
require_relative "app/serializers/poll_serializer.rb" require_relative "app/serializers/poll_serializer"
require_relative "jobs/regular/close_poll.rb" require_relative "jobs/regular/close_poll"
require_relative "lib/poll.rb" require_relative "lib/poll"
require_relative "lib/polls_updater.rb" require_relative "lib/polls_updater"
require_relative "lib/polls_validator.rb" require_relative "lib/polls_validator"
require_relative "lib/post_validator.rb" require_relative "lib/post_validator"
require_relative "lib/post_extension"
require_relative "lib/user_extension"
DiscoursePoll::Engine.routes.draw do DiscoursePoll::Engine.routes.draw do
put "/vote" => "polls#vote" put "/vote" => "polls#vote"
@ -59,26 +61,8 @@ after_initialize do
topic_view_post_custom_fields_allowlister { [DiscoursePoll::HAS_POLLS] } topic_view_post_custom_fields_allowlister { [DiscoursePoll::HAS_POLLS] }
reloadable_patch do reloadable_patch do
Post.class_eval do Post.prepend(DiscoursePoll::PostExtension)
attr_accessor :extracted_polls User.prepend(DiscoursePoll::UserExtension)
has_many :polls, dependent: :destroy
after_save do
polls = self.extracted_polls
self.extracted_polls = nil
next if polls.blank? || !polls.is_a?(Hash)
post = self
Poll.transaction do
polls.values.each { |poll| DiscoursePoll::Poll.create!(post.id, poll) }
post.custom_fields[DiscoursePoll::HAS_POLLS] = true
post.save_custom_fields(true)
end
end
end
User.class_eval { has_many :poll_votes, dependent: :delete_all }
end end
validate(:post, :validate_polls) do |force = nil| validate(:post, :validate_polls) do |force = nil|

View File

@ -10,7 +10,7 @@ register_asset "stylesheets/styleguide.scss"
enabled_site_setting :styleguide_enabled enabled_site_setting :styleguide_enabled
hide_plugin hide_plugin
load File.expand_path("../lib/styleguide/engine.rb", __FILE__) require_relative "lib/styleguide/engine"
Discourse::Application.routes.append { mount ::Styleguide::Engine, at: "/styleguide" } Discourse::Application.routes.append { mount ::Styleguide::Engine, at: "/styleguide" }

View File

@ -601,11 +601,9 @@ RSpec.describe Auth::DefaultCurrentUserProvider do
end end
describe "user api" do describe "user api" do
fab! :user do fab!(:user)
Fabricate(:user)
end
let :api_key do let(:api_key) do
UserApiKey.create!( UserApiKey.create!(
application_name: "my app", application_name: "my app",
client_id: "1234", client_id: "1234",

View File

@ -445,7 +445,8 @@ RSpec.describe ComposerMessagesFinder do
end end
context "when user can't send private messages" do context "when user can't send private messages" do
fab!(:group) { Fabricate(:group) } fab!(:group)
before { SiteSetting.personal_message_enabled_groups = group.id } before { SiteSetting.personal_message_enabled_groups = group.id }
it "does not show the message" do it "does not show the message" do

View File

@ -3,7 +3,7 @@
RSpec.describe RejectionMailer do RSpec.describe RejectionMailer do
describe "send_rejection" do describe "send_rejection" do
context "when sending rejection email" do context "when sending rejection email" do
fab!(:user) { Fabricate(:user) } fab!(:user)
let(:template_args) do let(:template_args) do
{ former_title: "Mail Subject", destination: user.email, site_name: SiteSetting.title } { former_title: "Mail Subject", destination: user.email, site_name: SiteSetting.title }
end end

View File

@ -1,20 +1,19 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Theme do RSpec.describe Theme do
after { Theme.clear_cache! } fab!(:user)
fab!(:theme) { Fabricate(:theme, user: user) }
before { ThemeJavascriptCompiler.disable_terser! }
after { ThemeJavascriptCompiler.enable_terser! }
fab! :user do
Fabricate(:user)
end
let(:guardian) { Guardian.new(user) } let(:guardian) { Guardian.new(user) }
fab!(:theme) { Fabricate(:theme, user: user) }
let(:child) { Fabricate(:theme, user: user, component: true) } let(:child) { Fabricate(:theme, user: user, component: true) }
before { ThemeJavascriptCompiler.disable_terser! }
after do
Theme.clear_cache!
ThemeJavascriptCompiler.enable_terser!
end
it "can properly clean up color schemes" do it "can properly clean up color schemes" do
scheme = ColorScheme.create!(theme_id: theme.id, name: "test") scheme = ColorScheme.create!(theme_id: theme.id, name: "test")
scheme2 = ColorScheme.create!(theme_id: theme.id, name: "test2") scheme2 = ColorScheme.create!(theme_id: theme.id, name: "test2")

View File

@ -4,7 +4,7 @@ RSpec.describe UserSearch do
before_all { SearchIndexer.enable } # Enable for prefabrication before_all { SearchIndexer.enable } # Enable for prefabrication
before { SearchIndexer.enable } # Enable for each test before { SearchIndexer.enable } # Enable for each test
fab!(:topic) { Fabricate :topic } fab!(:topic)
fab!(:topic2) { Fabricate :topic } fab!(:topic2) { Fabricate :topic }
fab!(:topic3) { Fabricate :topic } fab!(:topic3) { Fabricate :topic }
fab!(:topic4) { Fabricate :topic } fab!(:topic4) { Fabricate :topic }
@ -29,7 +29,7 @@ RSpec.describe UserSearch do
fab!(:inactive) { Fabricate :user, username: "Ghost", active: false } fab!(:inactive) { Fabricate :user, username: "Ghost", active: false }
fab!(:admin) { Fabricate :admin, username: "theadmin" } fab!(:admin) { Fabricate :admin, username: "theadmin" }
fab!(:moderator) { Fabricate :moderator, username: "themod" } fab!(:moderator) { Fabricate :moderator, username: "themod" }
fab!(:staged) { Fabricate :staged } fab!(:staged)
def search_for(*args) def search_for(*args)
# mapping "username" so it's easier to debug # mapping "username" so it's easier to debug

View File

@ -4903,7 +4903,7 @@ RSpec.describe UsersController do
end end
describe "#search_users" do describe "#search_users" do
fab!(:topic) { Fabricate :topic } fab!(:topic)
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" } let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
let(:post1) { Fabricate(:post, user: user, topic: topic) } let(:post1) { Fabricate(:post, user: user, topic: topic) }
let(:staged_user) { Fabricate(:user, staged: true) } let(:staged_user) { Fabricate(:user, staged: true) }

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe BasicUserSerializer do RSpec.describe BasicUserSerializer do
fab!(:user) { Fabricate(:user) } fab!(:user)
let(:serializer) { BasicUserSerializer.new(user, scope: Guardian.new(user), root: false) } let(:serializer) { BasicUserSerializer.new(user, scope: Guardian.new(user), root: false) }
describe "#as_json" do describe "#as_json" do
@ -30,7 +30,7 @@ RSpec.describe BasicUserSerializer do
end end
describe "#status" do describe "#status" do
fab!(:user_status) { Fabricate(:user_status) } fab!(:user_status)
before { user.user_status = user_status } before { user.user_status = user_status }

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe UserStatusMixin do RSpec.describe UserStatusMixin do
fab!(:user_status) { Fabricate(:user_status) } fab!(:user_status)
fab!(:user) { Fabricate(:user, user_status: user_status) } fab!(:user) { Fabricate(:user, user_status: user_status) }
class DummySerializer < ApplicationSerializer class DummySerializer < ApplicationSerializer

View File

@ -28,7 +28,7 @@ RSpec.describe FoundUserSerializer do
end end
describe "#status" do describe "#status" do
fab!(:user_status) { Fabricate(:user_status) } fab!(:user_status)
before { user.user_status = user_status } before { user.user_status = user_status }

View File

@ -364,7 +364,7 @@ RSpec.describe PostSerializer do
context "with mentions" do context "with mentions" do
fab!(:user_status) fab!(:user_status)
fab!(:user) { Fabricate(:user) } fab!(:user)
fab!(:user1) { Fabricate(:user, user_status: user_status) } fab!(:user1) { Fabricate(:user, user_status: user_status) }
fab!(:post) { Fabricate(:post, user: user, raw: "Hey @#{user1.username}") } fab!(:post) { Fabricate(:post, user: user, raw: "Hey @#{user1.username}") }
let(:serializer) { described_class.new(post, scope: Guardian.new(user), root: false) } let(:serializer) { described_class.new(post, scope: Guardian.new(user), root: false) }

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
describe "Group activity", type: :system do describe "Group activity", type: :system do
fab!(:user) { Fabricate(:user) } fab!(:user)
fab!(:group) { Fabricate(:group) } fab!(:group)
context "when on the posts activity page" do context "when on the posts activity page" do
let(:posts_page) { PageObjects::Pages::GroupActivityPosts.new } let(:posts_page) { PageObjects::Pages::GroupActivityPosts.new }