From 7db4f2e55948b7850b672a2811aff813e30e7ea4 Mon Sep 17 00:00:00 2001 From: Martin Ortbauer Date: Fri, 15 Mar 2024 08:05:00 -0500 Subject: [PATCH] explicitely use transactions for messages seems to fix #1055 --- .../app/controllers/messages_controller.rb | 19 ++++++---- .../mail_receivers/messages_mail_receiver.rb | 36 ++++++++++--------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/plugins/messages/app/controllers/messages_controller.rb b/plugins/messages/app/controllers/messages_controller.rb index 8206ec1d6..05815b735 100644 --- a/plugins/messages/app/controllers/messages_controller.rb +++ b/plugins/messages/app/controllers/messages_controller.rb @@ -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. diff --git a/plugins/messages/app/mail_receivers/messages_mail_receiver.rb b/plugins/messages/app/mail_receivers/messages_mail_receiver.rb index 7b8a03569..cfce5a1e9 100644 --- a/plugins/messages/app/mail_receivers/messages_mail_receiver.rb +++ b/plugins/messages/app/mail_receivers/messages_mail_receiver.rb @@ -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