Move AssignTagModerator from labor to services (#11826)

* Move AssignTagModerator from labor to services

* Remove unnecessary spec file

* Remove extra newline

* Update Admin::Tags::ModeratorsController
This commit is contained in:
Michael Kohl 2020-12-17 09:51:26 +07:00 committed by GitHub
parent b6301bb4f2
commit d21707a959
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 77 deletions

View file

@ -20,7 +20,7 @@ module Admin
def update
@user = User.find(params[:id])
AssignTagModerator.add_trusted_role(@user)
TagModerators::AddTrustedRole.call(@user)
redirect_to admin_mods_path(state: :potential),
flash: { success: "#{@user.username} now has Trusted role!" }

View file

@ -12,7 +12,7 @@ module Admin
def create
user = User.find_by(id: tag_params[:user_id])
if user&.update(email_tag_mod_newsletter: true)
AssignTagModerator.add_tag_moderators([user.id], [params[:tag_id]])
TagModerators::Add.call([user.id], [params[:tag_id]])
flash[:success] = "#{user.username} was added as a tag moderator!"
else
flash[:error] = "Error: User ID ##{tag_params[:user_id]} was not found,
@ -25,7 +25,7 @@ module Admin
user = User.find_by(id: tag_params[:user_id])
tag = Tag.find_by(id: params[:tag_id])
if user&.update(email_tag_mod_newsletter: false)
AssignTagModerator.remove_tag_moderator(user, tag)
TagModerators::Remove.call(user, tag)
flash[:success] = "@#{user.username} - ID ##{user.id} was removed as a tag moderator."
else
flash[:error] = "Error: User ID ##{tag_params[:user_id]} was not found,

View file

@ -1,71 +0,0 @@
module AssignTagModerator
def self.add_trusted_role(user)
return if user.has_role?(:trusted)
return if user.has_role?(:banned)
user.add_role(:trusted)
user.update(email_community_mod_newsletter: true)
MailchimpBot.new(user).manage_community_moderator_list if community_mod_newsletter_enabled?
Rails.cache.delete("user-#{user.id}/has_trusted_role")
NotifyMailer.with(user: user).trusted_role_email.deliver_now
end
def self.add_tag_moderators(user_ids, tag_ids)
user_ids.each_with_index do |user_id, index|
user = User.find(user_id)
tag = Tag.find(tag_ids[index])
add_tag_mod_role(user, tag)
add_trusted_role(user)
add_to_chat_channels(user, tag)
tag.update(supported: true) unless tag.supported?
NotifyMailer.with(user: user, tag: tag, channel_slug: chat_channel_slug(tag))
.tag_moderator_confirmation_email
.deliver_now
end
end
def self.add_to_chat_channels(user, tag)
user_channels = user.chat_channels
ChatChannel.find_by(slug: "tag-moderators")&.add_users(user) unless
user_channels.exists?(slug: "tag-moderators")
if tag.mod_chat_channel_id && !user_channels.exists?(id: tag.mod_chat_channel_id)
ChatChannel.find(tag.mod_chat_channel_id).add_users(user)
elsif tag.mod_chat_channel_id.blank?
channel = ChatChannels::CreateWithUsers.call(
users: ([user] + User.with_role(:mod_relations_admin)).flatten.uniq,
channel_type: "invite_only",
contrived_name: "##{tag.name} mods",
)
tag.update_column(:mod_chat_channel_id, channel.id)
end
end
def self.add_tag_mod_role(user, tag)
user.update(email_tag_mod_newsletter: true) if user.email_tag_mod_newsletter == false
user.add_role(:tag_moderator, tag)
Rails.cache.delete("user-#{user.id}/tag_moderators_list")
MailchimpBot.new(user).manage_tag_moderator_list if tag_mod_newsletter_enabled?
end
def self.remove_tag_moderator(user, tag)
user.remove_role(:tag_moderator, tag)
user.update(email_tag_mod_newsletter: false) if user.email_tag_mod_newsletter == true
Rails.cache.delete("user-#{user.id}/tag_moderators_list")
MailchimpBot.new(user).manage_tag_moderator_list if tag_mod_newsletter_enabled?
end
def self.chat_channel_slug(tag)
tag.mod_chat_channel&.slug
end
def self.community_mod_newsletter_enabled?
SiteConfig.mailchimp_api_key.present? && SiteConfig.mailchimp_community_moderators_id.present?
end
def self.tag_mod_newsletter_enabled?
SiteConfig.mailchimp_api_key.present? && SiteConfig.mailchimp_tag_moderators_id.present?
end
end

View file

@ -0,0 +1,69 @@
module TagModerators
class Add
def self.call(user_ids, tag_ids)
new(user_ids, tag_ids).call
end
def initialize(user_ids, tag_ids)
@user_ids = user_ids
@tag_ids = tag_ids
end
def call
user_ids.each_with_index do |user_id, index|
user = User.find(user_id)
tag = Tag.find(tag_ids[index])
add_tag_mod_role(user, tag)
::TagModerators::AddTrustedRole.call(user)
add_to_chat_channels(user, tag)
tag.update(supported: true) unless tag.supported?
NotifyMailer
.with(user: user, tag: tag, channel_slug: chat_channel_slug(tag))
.tag_moderator_confirmation_email
.deliver_now
end
end
private
attr_accessor :user_ids, :tag_ids
def add_to_chat_channels(user, tag)
user_channels = user.chat_channels
unless user_channels.exists?(slug: "tag-moderators")
ChatChannel.find_by(slug: "tag-moderators")&.add_users(user)
end
if tag.mod_chat_channel_id && !user_channels.exists?(id: tag.mod_chat_channel_id)
ChatChannel.find(tag.mod_chat_channel_id).add_users(user)
elsif tag.mod_chat_channel_id.blank?
channel = ChatChannels::CreateWithUsers.call(
users: ([user] + User.with_role(:mod_relations_admin)).flatten.uniq,
channel_type: "invite_only",
contrived_name: "##{tag.name} mods",
)
tag.update_column(:mod_chat_channel_id, channel.id)
end
end
def add_tag_mod_role(user, tag)
user.update(email_tag_mod_newsletter: true) unless user.email_tag_mod_newsletter?
user.add_role(:tag_moderator, tag)
Rails.cache.delete("user-#{user.id}/tag_moderators_list")
return unless tag_mod_newsletter_enabled?
MailchimpBot.new(user).manage_tag_moderator_list
end
def tag_mod_newsletter_enabled?
SiteConfig.mailchimp_api_key.present? &&
SiteConfig.mailchimp_tag_moderators_id.present?
end
def chat_channel_slug(tag)
tag.mod_chat_channel&.slug
end
end
end

View file

@ -0,0 +1,21 @@
module TagModerators
class AddTrustedRole
def self.call(user)
return if user.has_role?(:trusted) || user.has_role?(:banned)
user.add_role(:trusted)
user.update(email_community_mod_newsletter: true)
Rails.cache.delete("user-#{user.id}/has_trusted_role")
NotifyMailer.with(user: user).trusted_role_email.deliver_now
return unless community_mod_newsletter_enabled?
MailchimpBot.new(user).manage_community_moderator_list
end
def self.community_mod_newsletter_enabled?
SiteConfig.mailchimp_api_key.present? &&
SiteConfig.mailchimp_community_moderators_id.present?
end
private_class_method :community_mod_newsletter_enabled?
end
end

View file

@ -0,0 +1,18 @@
module TagModerators
class Remove
def self.call(user, tag)
user.remove_role(:tag_moderator, tag)
user.update(email_tag_mod_newsletter: false) if user.email_tag_mod_newsletter?
Rails.cache.delete("user-#{user.id}/tag_moderators_list")
return unless tag_mod_newsletter_enabled?
MailchimpBot.new(user).manage_tag_moderator_list
end
def self.tag_mod_newsletter_enabled?
SiteConfig.mailchimp_api_key.present? &&
SiteConfig.mailchimp_tag_moderators_id.present?
end
private_class_method :tag_mod_newsletter_enabled?
end
end

View file

@ -1,6 +1,6 @@
require "rails_helper"
RSpec.describe AssignTagModerator, type: :labor do
RSpec.describe TagModerators::Add, type: :service do
let(:user_one) { create(:user) }
let(:user_two) { create(:user) }
let(:mod_relator) { create(:user) }
@ -17,7 +17,7 @@ RSpec.describe AssignTagModerator, type: :labor do
mod_relator.add_role(:mod_relations_admin)
user_ids = [user_one.id, user_two.id]
tag_ids = [tag_one.id, tag_two.id]
described_class.add_tag_moderators(user_ids, tag_ids)
described_class.call(user_ids, tag_ids)
end
def destroy_tag_moderator_channel
@ -51,7 +51,7 @@ RSpec.describe AssignTagModerator, type: :labor do
it "adds user to channel when tag already has channel" do
user_three = create(:user)
described_class.add_tag_moderators([user_three.id], [tag_one.id])
described_class.call([user_three.id], [tag_one.id])
channel = ChatChannel.find_by(channel_name: "##{tag_one.name} mods")
expect(channel.active_users).to include(user_three)
end

View file

@ -0,0 +1,20 @@
require "rails_helper"
RSpec.describe TagModerators::AddTrustedRole, type: :service do
let(:user) { create(:user) }
it "adds the trusted role to a user" do
expect { described_class.call(user) }.to change { user.reload.roles.size }.by(1)
end
it "does not add the fole for banned users" do
user = create(:user, :banned)
expect { described_class.call(user) }.not_to change { user.reload.roles.size }
end
it "signs the user up for the community mods newsletter" do
expect do
described_class.call(user)
end.to change { user.reload.email_community_mod_newsletter }.from(false).to(true)
end
end

View file

@ -0,0 +1,13 @@
require "rails_helper"
RSpec.describe TagModerators::Remove, type: :service do
let(:user) { create(:user) }
let(:tag) { create(:tag) }
it "removes the tag_moderator role from the user" do
user.add_role(:tag_moderator, tag)
expect do
described_class.call(user, tag)
end.to change { user.reload.roles.count }.by(-1)
end
end