diff --git a/app/controllers/concerns/action_rescuing.rb b/app/controllers/concerns/action_rescuing.rb index 69f83211..a329a2dc 100644 --- a/app/controllers/concerns/action_rescuing.rb +++ b/app/controllers/concerns/action_rescuing.rb @@ -29,6 +29,7 @@ module ActionRescuing rescue_from Talk::BannedUserError, with: :forbidden rescue_from Talk::BlockedUserError, with: :forbidden rescue_from Talk::UserBlockedError, with: :forbidden + rescue_from Talk::UserUnconfirmedError, with: :forbidden rescue_from OauthAccessToken::ExpiredError, with: :unauthorized rescue_from OauthAccessToken::RevokedError, with: :unauthorized end diff --git a/app/services/conversation_service.rb b/app/services/conversation_service.rb index 3bb7854e..0f2f240c 100644 --- a/app/services/conversation_service.rb +++ b/app/services/conversation_service.rb @@ -13,6 +13,7 @@ def build_user_conversations_for(conversation) raise Talk::BlockedUserError.new if blocked? raise Talk::UserBlockedError.new if blocking? + raise Talk::UserUnconfirmedError.new if unconfirmed? recipient_ids.each do |recipient_id| conversation.user_conversations.build user_id: recipient_id, is_unread: true end @@ -48,4 +49,8 @@ def blocked? def blocking? BlockedUser.blocked_by(current_user.id).blocking(recipient_ids).exists? end + + def unconfirmed? + current_user.confirmed_at.nil? + end end diff --git a/app/services/message_service.rb b/app/services/message_service.rb index bee69353..de8e4077 100644 --- a/app/services/message_service.rb +++ b/app/services/message_service.rb @@ -6,6 +6,7 @@ def build built.user_ip = user_ip raise Talk::BlockedUserError.new if blocked? raise Talk::UserBlockedError.new if blocking? + raise Talk::UserUnconfirmedError.new if unconfirmed? end end @@ -19,6 +20,11 @@ def blocking? BlockedUser.blocked_by(current_user.id).blocking(recipient_ids).exists? end + def unconfirmed? + return false unless resource.conversation + current_user.confirmed_at.nil? + end + def recipient_ids resource.conversation.participant_ids - [current_user.id] end diff --git a/config/application.rb b/config/application.rb index 85a24d24..27c53b48 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,6 +31,13 @@ def message alias_method :to_s, :message end + class UserUnconfirmedError < StandardError + def message + 'You must confirm your account' + end + alias_method :to_s, :message + end + class InvalidParameterError < StandardError def initialize(param, expected, actual) @param = param diff --git a/spec/services/conversation_service_spec.rb b/spec/services/conversation_service_spec.rb index 6b8d95d8..3f99b573 100644 --- a/spec/services/conversation_service_spec.rb +++ b/spec/services/conversation_service_spec.rb @@ -69,6 +69,13 @@ expect{ service.build }.to raise_error Talk::UserBlockedError end end + + context 'when the sender is unconfirmed' do + it 'should fail' do + current_user.confirmed_at = nil + expect{ service.build }.to raise_error Talk::UserUnconfirmedError + end + end end end end diff --git a/spec/services/message_service_spec.rb b/spec/services/message_service_spec.rb index a62f1a5e..305fb9cf 100644 --- a/spec/services/message_service_spec.rb +++ b/spec/services/message_service_spec.rb @@ -33,6 +33,13 @@ expect{ service.build }.to raise_error Talk::UserBlockedError end end + + context 'when the user is unconfirmed' do + it 'should fail' do + current_user.confirmed_at = nil + expect{ service.build }.to raise_error Talk::UserUnconfirmedError + end + end end context 'creating the message' do