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

Email verification: Require confirmation to invoke message & conversation workers #313

Merged
merged 3 commits into from
Dec 11, 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
1 change: 1 addition & 0 deletions app/controllers/concerns/action_rescuing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/services/conversation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/services/message_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/services/conversation_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions spec/services/message_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading