Skip to content

Commit

Permalink
explicitely use transactions for messages seems to fix foodcoops#1055
Browse files Browse the repository at this point in the history
  • Loading branch information
mortbauer committed Mar 15, 2024
1 parent 4b0082c commit 7db4f2e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
19 changes: 13 additions & 6 deletions plugins/messages/app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ def new

# Creates a new message.
def create
@message = @current_user.send_messages.new(params[:message])
if @message.save
DeliverMessageJob.perform_later(@message)
redirect_to messages_url, notice: I18n.t('messages.create.notice')
else
render action: 'new'
ActiveRecord::Base.transaction do
@current_user.with_lock do
@message = @current_user.send_messages.new(params[:message])
if @message.save
DeliverMessageJob.perform_later(@message)
redirect_to messages_url, notice: I18n.t('messages.create.notice')
else
raise ActiveRecord::Rollback # Rollback the transaction if the save fails
end
end
end
rescue ActiveRecord::RecordInvalid => e
# Handle validation errors
render action: 'new'
end

# Shows a single message.
Expand Down
36 changes: 19 additions & 17 deletions plugins/messages/app/mail_receivers/messages_mail_receiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ def received(data)
body = EmailReplyTrimmer.trim(body)
raise BlankBodyException if body.empty?

message = @user.send_messages.new body: body,
group: @message.group,
private: @message.private,
received_email: data
message.reply_to_message = if @message.reply_to
@message.reply_to_message
else
@message
end
message.subject = if mail.subject
mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", '')
else
I18n.t('messages.model.reply_subject', subject: message.reply_to_message.subject)
end
message.add_recipients [@message.sender_id]
ActiveRecord::Base.transaction do
message = @user.send_messages.new body: body,
group: @message.group,
private: @message.private,
received_email: data
message.reply_to_message = if @message.reply_to
@message.reply_to_message
else
@message
end
message.subject = if mail.subject
mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", '')
else
I18n.t('messages.model.reply_subject', subject: message.reply_to_message.subject)
end
message.add_recipients [@message.sender_id]

message.save!
DeliverMessageJob.perform_later(message)
message.save!
DeliverMessageJob.perform_later(message)
end
end

private
Expand Down

0 comments on commit 7db4f2e

Please sign in to comment.