-
Notifications
You must be signed in to change notification settings - Fork 71
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
分報チャンネルがアカウントIDの頭文字のグループ内で自動作成されるようにする #6522
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Discord | ||
class TimesCategory | ||
include ActiveSupport::Configurable | ||
config_accessor :default_times_category_id, instance_accessor: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [自分用補足]
両方から属性に対して
の両方の操作が出来る |
||
CATEGORY_NAME_KEYWORD = 'ひとりごと・分報' | ||
|
||
class << self | ||
def categorize_by_initials(name) | ||
return Discord::TimesCategory.default_times_category_id if name.blank? | ||
|
||
times_categories = Discord::Server.categories(keyword: CATEGORY_NAME_KEYWORD) | ||
|
||
category_type = /\A[0-9]/.match?(name) ? '数字' : name.upcase[0] | ||
found_times_category = times_categories&.find { |times_category| /#{category_type}/.match? times_category.name } | ||
found_times_category&.id || Discord::TimesCategory.default_times_category_id | ||
end | ||
end | ||
end | ||
end |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,5 +109,56 @@ class ServerTest < ActiveSupport::TestCase | |
Discord::Server.authorize_token = 'skip' | ||
assert_not Discord::Server.enabled? | ||
end | ||
|
||
test '.channels' do | ||
logs = [] | ||
Rails.logger.stub(:error, ->(message) { logs << message }) do | ||
VCR.use_cassette 'discord/server/channels' do | ||
actual = Discord::Server.channels(id: '1234567890123456789', token: 'Bot valid token') | ||
assert_kind_of Array, actual | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [自分用補足] |
||
assert actual.all?(Discordrb::Channel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [自分用補足] module Discordrb
class Channel |
||
assert_nil logs.last | ||
end | ||
|
||
VCR.use_cassette 'discord/server/channels_with_unauthorized' do | ||
actual = Discord::Server.channels(id: '1234567890123456789', token: 'Bot invalid token') | ||
assert_nil actual | ||
assert_equal '[Discord API] 401: Unauthorized', logs.pop | ||
end | ||
|
||
Discord::Server.stub(:enabled?, -> { false }) do | ||
actual = Discord::Server.channels(id: '1234567890123456789', token: 'Bot valid token') | ||
assert_nil actual | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [質問]
なので、
という理解でよろしいでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @syo-tokeshi 少し補足いたしますと
ので
になります! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ご質問に親切に答えてくださってありがとうございます🙇♂️ 明日の夜までに返信出来る事を目安に頑張ります😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @syo-tokeshi |
||
assert_nil logs.last | ||
end | ||
end | ||
end | ||
|
||
test '.categories' do | ||
logs = [] | ||
Rails.logger.stub(:error, ->(message) { logs << message }) do | ||
VCR.use_cassette 'discord/server/categories' do | ||
actual = Discord::Server.categories | ||
|
||
assert_kind_of Array, actual | ||
assert(actual.all? { |channel| channel.type == Discordrb::Channel::TYPES[:category] }) | ||
end | ||
|
||
VCR.use_cassette 'discord/server/categories_with_keyword' do | ||
actual = Discord::Server.categories(keyword: 'ひとりごと・分報') | ||
|
||
assert_kind_of Array, actual | ||
assert(actual.all? { |channel| channel.type == Discordrb::Channel::TYPES[:category] }) | ||
assert(actual.all? { |channel| /ひとりごと・分報/.match? channel.name }) | ||
end | ||
|
||
Discord::Server.stub(:categories, -> { nil }) do | ||
actual = Discord::Server.categories | ||
|
||
assert_nil actual | ||
assert_nil logs.last | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||||||||||||||||||||||||||||
# frozen_string_literal: true | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
require 'test_helper' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
module Discord | ||||||||||||||||||||||||||||||
class TimesCategoryTest < ActiveSupport::TestCase | ||||||||||||||||||||||||||||||
setup do | ||||||||||||||||||||||||||||||
@default_times_category_id = Discord::TimesCategory.default_times_category_id | ||||||||||||||||||||||||||||||
Discord::TimesCategory.default_times_category_id = '1111111111111111111' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@stub_times_categories = lambda { |_| | ||||||||||||||||||||||||||||||
channel_data = { 'id' => '123', 'type' => Discordrb::Channel::TYPES[:category], 'name' => 'A・B・C (ひとりごと・分報)' } | ||||||||||||||||||||||||||||||
[Discordrb::Channel.new(channel_data, nil)] | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
teardown do | ||||||||||||||||||||||||||||||
Discord::TimesCategory.default_times_category_id = @default_times_category_id | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
test '.categorize_by_initials' do | ||||||||||||||||||||||||||||||
Discord::Server.stub(:categories, @stub_times_categories) do | ||||||||||||||||||||||||||||||
actual = Discord::TimesCategory.categorize_by_initials('alice🔰') | ||||||||||||||||||||||||||||||
assert_equal 123, actual | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
test '.categorize_by_initials_with_not_matched_category' do | ||||||||||||||||||||||||||||||
Discord::Server.stub(:categories, @stub_times_categories) do | ||||||||||||||||||||||||||||||
actual = Discord::TimesCategory.categorize_by_initials('ありす🔰') | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [質問] なぜそのような事を言うのかと言いますと、 サインアップ(新規会員登録)の処理フロー前提
処理の確認
念のため、 module Discord
class TimesChannel
class << self
def to_channel_name(username)
username.downcase
end
end
def initialize(username)
@name = Discord::TimesChannel.to_channel_name(username)
end
def save
@channel = Discord::Server.create_text_channel(
name: @name,
parent: Discord::TimesCategory.categorize_by_initials(@name)
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 長くなりましたが、ようは
という理解でよろしいでしょうか? [私の考え]
等です🙇♂️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ymmtd0x0b
とは、他のクラスの事は考慮せず、それ単体(今回で言うと つまり、私が最初に言ってた「usersコントローラーから呼ばれてますよね」みたいな理屈は関係なく、 参考文献https://and-engineer.com/articles/X_-EBRAAACcAJnau
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. はい!このコードではDiscrodカテゴリーの分類パターンをテストしたいだけなので、ユーザー名に特に意味はないと捉えています! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解です!🙇♂️ |
||||||||||||||||||||||||||||||
assert_equal Discord::TimesCategory.default_times_category_id, actual | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
test '.categorize_by_initials_with_invalid_name' do | ||||||||||||||||||||||||||||||
Discord::Server.stub(:categories, @stub_times_categories) do | ||||||||||||||||||||||||||||||
actual = Discord::TimesCategory.categorize_by_initials(nil) | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらはそのままで良いと思います🙇♂️ |
||||||||||||||||||||||||||||||
assert_equal Discord::TimesCategory.default_times_category_id, actual | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[自分用補足]
こちらの
rescue
の使い方は「rescue修飾子」と言い、https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html
このようなコードがあった場合
式1 rescue 式2
式1で例外が発生したら、式2を評価する