Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

退会時に分報チャンネルを自動で削除する #6626

Merged
merged 13 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def edit; end
def update
if @user.update(user_params)
destroy_subscription(@user)
Newspaper.publish(:retirement_create, @user)
Newspaper.publish(:retirement_create, @user) if @user.saved_change_to_retired_on?
redirect_to admin_users_url, notice: 'ユーザー情報を更新しました。'
else
render :edit
Expand Down Expand Up @@ -61,7 +61,7 @@ def user_params
:auto_retire,
:profile_image, :profile_name, :profile_job, :mentor,
:profile_text, { authored_books_attributes: %i[id title url cover _destroy] },
:country_code, :subdivision_code, discord_profile_attributes: %i[account_name times_url]
:country_code, :subdivision_code, discord_profile_attributes: %i[account_name times_url times_id]
)
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/discord/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def create_text_channel(name:, parent: nil)
nil
end

def delete_text_channel(channel_id)
response = Discordrb::API::Channel.delete(authorize_token, channel_id)
response.code == 200
rescue Discordrb::Errors::CodeError => e
log_error(e)
nil
end

def find_by(id:, token:)
return nil unless enabled?

Expand Down
13 changes: 13 additions & 0 deletions app/models/times_channel_destroyer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class TimesChannelDestroyer
def call(user)
return unless user.discord_profile.times_id

if Discord::Server.delete_text_channel(user.discord_profile.times_id)
user.discord_profile.update!(times_id: nil)
else
Rails.logger.warn "[Discord API] #{user.login_name}の分報チャンネルが削除できませんでした。"
end
end
end
2 changes: 0 additions & 2 deletions app/models/unfinished_data_destroyer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class UnfinishedDataDestroyer
def call(user)
return unless user.saved_change_to_retired_on?
Copy link
Contributor Author

@ogawa-tomo ogawa-tomo Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今回、チャンネル削除処理をNewspaper経由で呼ぶことにしましたが、その際に一緒に呼ばれるのがこのクラスです。ユーザーの未確認提出物の削除・WIP日報の削除・就職活動フラグOFFの操作をしています。
この箇所では「ユーザーの退会フラグに変更があったか」を判断しそのときのみ処理を実行するようになっていますが、その判断は呼び出し側で行うべき(このクラスの責務ではない)と考え、処理を移しています。

この処理を呼んでいるのは以下の3箇所であり、いずれも退会処理を行うときに呼んでいます。(ユーザーが自ら退会するとき、管理画面から退会させるとき、休会六ヶ月で自動退会するとき)

Newspaper.publish(:retirement_create, user)

Newspaper.publish(:retirement_create, @user) if @user.saved_change_to_retired_on?

Newspaper.publish(:retirement_create, user)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご説明ありがとうございます〜
そうですね、確かにUnfinishedDataDestroyerではなく呼び出し側でフラグOFFの操作を行った方が良いと思います。


Product.where(user: user).unchecked.destroy_all
Report.where(user: user).wip.destroy_all
user.update(job_seeking: false)
Expand Down
1 change: 1 addition & 0 deletions app/views/users/form/_sns.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
| こちら
span.a-help
i.fa-solid.fa-question
= discord_profile_fields.hidden_field :times_id

.form-item#form-github-account
= f.label :github_account, class: 'a-form-label'
Expand Down
1 change: 1 addition & 0 deletions config/initializers/newspaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
Newspaper.subscribe(:question_update, ai_answer_creator)

Newspaper.subscribe(:retirement_create, UnfinishedDataDestroyer.new)
Newspaper.subscribe(:retirement_create, TimesChannelDestroyer.new)

question_notifier = QuestionNotifier.new
Newspaper.subscribe(:question_create, question_notifier)
Expand Down
85 changes: 85 additions & 0 deletions test/cassettes/discord/server/delete_text_channel.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/models/discord/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ class ServerTest < ActiveSupport::TestCase
end
end

test '.delete_text_channel' do
VCR.use_cassette 'discord/server/delete_text_channel' do
assert Discord::Server.delete_text_channel('987654321987654321')
end
end

test '.delete_text_channel with error' do
logs = []
Rails.logger.stub(:error, ->(message) { logs << message }) do
VCR.use_cassette 'discord/server/delete_text_channel_with_unknown_channel_id' do
assert_nil Discord::Server.delete_text_channel('12345')
assert_equal '[Discord API] Unknown Channel', logs.pop
end

VCR.use_cassette 'discord/server/delete_text_channel_with_unauthorized' do
Discord::Server.authorize_token = 'Bot invalid token'
assert_nil Discord::Server.delete_text_channel('987654321987654321')
assert_equal '[Discord API] 401: Unauthorized', logs.pop
end
end
end

test '.enabled?' do
Discord::Server.guild_id = '1234567890123456789'
Discord::Server.authorize_token = 'Bot valid token'
Expand Down
31 changes: 31 additions & 0 deletions test/models/times_channel_destroyer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'test_helper'

class TimesChannelCreatorTest < ActiveSupport::TestCase
test '#call' do
logs = []
user = users(:hajime)
user.discord_profile.update!(times_id: '987654321987654321')
Rails.logger.stub(:warn, ->(message) { logs << message }) do
Discord::Server.stub(:delete_text_channel, true) do
TimesChannelDestroyer.new.call(user)
end
assert_nil user.discord_profile.times_id
assert_nil logs.last
end
end

test '#call with failure' do
logs = []
user = users(:hajime)
user.discord_profile.update!(times_id: '987654321987654321')
Rails.logger.stub(:warn, ->(message) { logs << message }) do
Discord::Server.stub(:delete_text_channel, nil) do
TimesChannelDestroyer.new.call(user)
end
assert_equal '987654321987654321', user.discord_profile.times_id
assert_equal "[Discord API] #{user.login_name}の分報チャンネルが削除できませんでした。", logs.last
end
end
end
14 changes: 9 additions & 5 deletions test/system/admin/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,19 @@ class Admin::UsersTest < ApplicationSystemTestCase

test 'make user retired' do
user = users(:hatsuno)
user.discord_profile.update!(times_id: '987654321987654321')
date = Date.current
VCR.use_cassette 'subscription/update' do
visit_with_auth edit_admin_user_path(user.id), 'komagata'
check '退会済', allow_label_click: true
fill_in 'user_retired_on', with: date
click_on '更新する'
Discord::Server.stub(:delete_text_channel, true) do
VCR.use_cassette 'subscription/update' do
visit_with_auth edit_admin_user_path(user.id), 'komagata'
check '退会済', allow_label_click: true
fill_in 'user_retired_on', with: date
click_on '更新する'
end
end
assert_text 'ユーザー情報を更新しました。'
assert_equal date, user.reload.retired_on
assert_nil user.discord_profile.times_id

assert_requested(:post, "https://api.stripe.com/v1/subscriptions/#{user.subscription_id}") do |req|
req.body.include?('cancel_at_period_end=true')
Expand Down
Loading