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

ChannelLastSpeech: 発言がない場合に refresh! が失敗しないようにする #291

Merged
merged 8 commits into from
Aug 26, 2021
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'

gem 'minitest-reporters'

gem 'guard'
gem 'guard-minitest'

Expand All @@ -111,6 +109,8 @@ end
group :test do
gem 'test-unit-rails'

gem 'database_cleaner-active_record'

gem 'rake'
gem 'rubocop'
gem 'rubocop-rails'
Expand Down
15 changes: 8 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ GEM
zeitwerk (~> 2.3)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
ast (2.4.2)
autoprefixer-rails (10.2.5.1)
execjs (> 0)
Expand All @@ -98,6 +97,10 @@ GEM
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
crass (1.0.6)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
docile (1.4.0)
erubi (1.10.0)
execjs (2.8.1)
Expand Down Expand Up @@ -184,18 +187,15 @@ GEM
method_source (1.0.0)
mini_mime (1.1.0)
minitest (5.14.4)
minitest-reporters (1.4.3)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
msgpack (1.4.2)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
mysql2 (0.5.3)
nenv (0.3.0)
nio4r (2.5.7)
nokogiri (1.11.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.11.7-x86_64-linux)
racc (~> 1.4)
notiffany (0.1.3)
Expand Down Expand Up @@ -383,6 +383,7 @@ GEM
zeitwerk (2.4.2)

PLATFORMS
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
Expand All @@ -392,6 +393,7 @@ DEPENDENCIES
byebug
capybara (>= 3.26)
cfnv
database_cleaner-active_record
factory_bot_rails
friendly_id
guard
Expand All @@ -403,7 +405,6 @@ DEPENDENCIES
lumberjack
mcinch
meta-tags
minitest-reporters
mysql2 (~> 0.5)
pry-rails
puma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def show
def create
archiver = ConversationMessageArchiver.new

if am = archiver.archive!(archived_conversation_message_params_for_create)
am = archiver.archive!(archived_conversation_message_params_for_create)
if am
flash[:success] = t('views.flash.added_archived_conversation_message')
redirect_to(admin_archived_conversation_message_path(am.id))
else
Expand Down Expand Up @@ -49,7 +50,8 @@ def update
def destroy
archiver = ConversationMessageArchiver.new

if cm = archiver.reconstitute!(params[:id])
cm = archiver.reconstitute!(params[:id].to_i)
if cm
flash[:success] = t('views.flash.deleted_archived_conversation_message')
redirect_to(admin_conversation_message_path(cm))
else
Expand Down
7 changes: 3 additions & 4 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ def lowercase_name_with_prefix
# 現在の(キャッシュされていない)最終発言を求める
# @return [ConversationMessage]
def current_last_speech
conversation_messages.
order(timestamp: :desc).
limit(1).
first
conversation_messages
.order(timestamp: :desc)
.first
end

# canonical 属性の URL テンプレートを、日付を入れた URL にして返す
Expand Down
18 changes: 10 additions & 8 deletions app/models/channel_last_speech.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ class ChannelLastSpeech < ApplicationRecord

# チャンネルの最終発言を更新する
# @param [Channel] channel 更新対象のチャンネル
# @return [self]
# @return [ChannelLastSpeech, nil] 更新後の最終発言データ
def self.refresh!(channel)
last_speech = channel.channel_last_speech || new(channel: channel)
current_last_speech = channel.current_last_speech
unless current_last_speech
where(channel: channel)
.delete_all

self.transaction do
last_speech.conversation_message = nil

yield if block_given?
return nil
end

last_speech.conversation_message = channel.current_last_speech
last_speech.save!
find_or_initialize_by(channel: channel).tap do |s|
s.conversation_message = current_last_speech
s.save!
end
end
end
31 changes: 16 additions & 15 deletions app/models/conversation_message_archiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,37 @@ class ConversationMessageArchiver
# @option [String] :archive_reason 非表示理由
# @return [ArchivedConversationMessage]
def archive!(params)
old_id = params[:old_id].to_i
unless cm = ConversationMessage.find(old_id)
cm = ConversationMessage.find(params[:old_id].to_i)
unless cm
raise(ArgumentError, 'old_id is required.')
end
archive_reason = params[:archive_reason_id].to_i
unless reason = ArchiveReason.find(archive_reason)
raise(ArgumentError, 'archive_reason is required.')

archive_reason = ArchiveReason.find(params[:archive_reason_id].to_i)
unless archive_reason
raise(ArgumentError, 'archive_reason_id is required.')
end

am = ArchivedConversationMessage.from_conversation_message(cm)
am.archive_reason = reason
am.archive_reason = archive_reason

# Mroonga ストレージモードはトランザクション非対応
ApplicationRecord.transaction do
ChannelLastSpeech.refresh!(cm.channel) do
am.save!
cm.destroy!
end
am.save!
cm.destroy!

ChannelLastSpeech.refresh!(cm.channel)
end

am
end

# Archived -> ConversationMessage
# @param [Integer/String] old_id ArchivedConversationMessage.id
# @option [String] :id 元の ArchivedConversationMessage.old_id
# @param [Integer] archived_conversation_message_id
# @return [ConversationMessage]
def reconstitute!(old_id)
unless am = ArchivedConversationMessage.find(old_id.to_i)
raise(ArgumentError, 'old_id is required.')
def reconstitute!(archived_conversation_message_id)
am = ArchivedConversationMessage.find(archived_conversation_message_id)
unless am
raise(ArgumentError, 'archived_conversation_message_id is required.')
end

cm = ConversationMessage.new
Expand Down
9 changes: 0 additions & 9 deletions lib/ircs/plugins/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ def record_message_to_channels(message, cinch_channels)
end
end
end

# [DEPRECATED] チャンネルごとの最終発言を更新する
# @param [::Channel] チャンネル
# @param [::ConversationMessage] 発言のメッセージ
# @return [::ChannelLastSpeech]
def update_last_speech!(channel, message)
ChannelLastSpeech.refresh!(channel)
@logger.warn('このメソッドは廃止予定です。ChannelLastSpeech.refresh! を使ってください')
end
end
end
end
5 changes: 0 additions & 5 deletions test/controllers/channels/today_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

class Channels::TodayControllerTest < ActionController::TestCase
setup do
Channel.delete_all
@channel = create(:channel)
end

teardown do
Channel.delete_all
end

test '今日のページにリダイレクトされる' do
today = Time.zone.local(2017, 5, 1)

Expand Down
5 changes: 0 additions & 5 deletions test/controllers/channels/yesterday_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

class Channels::YesterdayControllerTest < ActionController::TestCase
setup do
Channel.delete_all
@channel = create(:channel)
end

teardown do
Channel.delete_all
end

test '昨日のページにリダイレクトされる' do
today = Time.zone.local(2017, 5, 1)

Expand Down
11 changes: 11 additions & 0 deletions test/factories/archive_reasons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FactoryBot.define do
factory :archive_reason do
reason { '利用規約に違反しているため' }

initialize_with {
ArchiveReason.find_or_initialize_by(reason: reason) do |new_reason|
new_reason.attributes = attributes
end
}
end
end
6 changes: 0 additions & 6 deletions test/integration/channels_create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ class ChannelsCreateTest < ActionDispatch::IntegrationTest
@setting = create(:setting)
@user = create(:user)

Channel.delete_all

@login_helper = UserLoginTestHelper.new(self, @user, channels_path)
end

teardown do
Channel.delete_all
end

test 'ログインしていない場合、ログインページにリダイレクトされる' do
@login_helper.assert_redirected_to_login_on_logged_out do
post(
Expand Down
13 changes: 0 additions & 13 deletions test/integration/channels_days_show_linking_to_keyword_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ class ChannelsDaysShowLinkingToKeywordTest < ActionDispatch::IntegrationTest
create(:setting)
create(:user)
@channel = create(:channel)

PrivmsgKeywordRelationship.delete_all
Message.delete_all
ConversationMessage.delete_all
Keyword.delete_all

@keyword = create(:keyword)

# メッセージの準備
Expand All @@ -26,13 +20,6 @@ class ChannelsDaysShowLinkingToKeywordTest < ActionDispatch::IntegrationTest
@privmsgs = privmsg_factory_ids.map { |id| create(id) }
end

teardown do
PrivmsgKeywordRelationship.delete_all
Message.delete_all
ConversationMessage.delete_all
Keyword.delete_all
end

test 'キーワードのリンク先および表記が正しい' do
# メッセージとキーワードを関連付ける
extract_keyword = LogArchiver::ExtractKeyword.new
Expand Down
11 changes: 0 additions & 11 deletions test/integration/channels_days_show_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ class ChannelsDaysShowTest < ActionDispatch::IntegrationTest
setup do
create(:setting)
create(:user)

MessageDate.delete_all
Message.delete_all
ConversationMessage.delete_all

@channel = create(:channel)

# NICK 1件
Expand Down Expand Up @@ -45,12 +40,6 @@ class ChannelsDaysShowTest < ActionDispatch::IntegrationTest
style: :normal)
end

teardown do
MessageDate.delete_all
Message.delete_all
ConversationMessage.delete_all
end

test 'メッセージの一覧が正しく表示される' do
get(@browse.path)
assert_response(:success)
Expand Down
5 changes: 0 additions & 5 deletions test/integration/channels_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ class ChannelsEditTest < ActionDispatch::IntegrationTest
@setting = create(:setting)
@user = create(:user)

Channel.delete_all
@channel = create(:channel)
@path = edit_channel_path(@channel)

@login_helper = UserLoginTestHelper.new(self, @user, @path)
end

teardown do
Channel.delete_all
end

test 'ログインしている場合、表示される' do
@login_helper.assert_successful_login_and_get
end
Expand Down
6 changes: 0 additions & 6 deletions test/integration/channels_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ class ChannelsIndexTest < ActionDispatch::IntegrationTest
setup do
@setting = create(:setting)
@user = create(:user)

Channel.delete_all
@channel = create(:channel)
end

teardown do
Channel.delete_all
end

test '表示される' do
get(channels_path)
assert_response(:success)
Expand Down
6 changes: 0 additions & 6 deletions test/integration/channels_show_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ class ChannelsShowTest < ActionDispatch::IntegrationTest
setup do
@setting = create(:setting)
@user = create(:user)

Channel.delete_all
@channel = create(:channel)
end

teardown do
Channel.delete_all
end

test '表示される' do
get(channel_path(@channel))
assert_response(:success)
Expand Down
6 changes: 0 additions & 6 deletions test/integration/channels_update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ class ChannelsUpdateTest < ActionDispatch::IntegrationTest
setup do
@setting = create(:setting)
@user = create(:user)

Channel.delete_all
@channel = create(:channel)
@path = channel_path(@channel)

@login_helper = UserLoginTestHelper.new(self, @user, @path)
end

teardown do
Channel.delete_all
end

test 'ログインしていない場合、ログインページにリダイレクトされる' do
@login_helper.assert_redirected_to_login_on_logged_out do
patch(
Expand Down
Loading