From 7da3cd69a62d06d7c64f26a0f0025695df9a3b35 Mon Sep 17 00:00:00 2001 From: Martin Ortbauer Date: Wed, 5 Jun 2024 11:35:55 +0200 Subject: [PATCH] another shitty attempt to fix the message plugin --- .../app/controllers/messages_controller.rb | 17 ++++--- plugins/messages/app/models/message.rb | 46 ++++++++----------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/plugins/messages/app/controllers/messages_controller.rb b/plugins/messages/app/controllers/messages_controller.rb index d18477a26..15a88cce6 100644 --- a/plugins/messages/app/controllers/messages_controller.rb +++ b/plugins/messages/app/controllers/messages_controller.rb @@ -34,15 +34,18 @@ def create ActiveRecord::Base.transaction do @current_user.with_lock do @message = @current_user.send_messages.new(params[:message]) - raise ActiveRecord::Rollback unless @message.save - - DeliverMessageJob.perform_later(@message) - redirect_to messages_url, notice: I18n.t('messages.create.notice') + if @message.save + DeliverMessageJob.perform_later(@message) + redirect_to messages_url, notice: I18n.t('messages.create.notice') + else + Rails.logger.info "Message validation failed with: #{@message.errors.inspect}" + render :new, status: :unprocessable_entity + raise ActiveRecord::Rollback + end end end - rescue ActiveRecord::RecordInvalid - # Handle validation errors - render action: 'new' + rescue ActiveRecord::RecordInvalid, ActiveRecord::Rollback => e + render :new, status: :unprocessable_entity end # Shows a single message. diff --git a/plugins/messages/app/models/message.rb b/plugins/messages/app/models/message.rb index 3da0e9e58..0c10af90b 100644 --- a/plugins/messages/app/models/message.rb +++ b/plugins/messages/app/models/message.rb @@ -7,8 +7,6 @@ class Message < ApplicationRecord has_many :message_recipients, dependent: :destroy has_many :recipients, through: :message_recipients, source: :user - validate :recipients_ids_must_be_valid - attr_accessor :send_method, :recipient_tokens, :order_id, :recipients_ids scope :threads, -> { where(reply_to: nil) } @@ -31,6 +29,14 @@ class Message < ApplicationRecord validates_presence_of :message_recipients, :subject, :body validates_length_of :subject, in: 1..255 + after_initialize do + @recipients_ids ||= [] + @send_method ||= 'recipients' + end + + before_create :create_salt + before_validation :create_message_recipients, on: :create + has_rich_text :body # Override the `attributes=` method to exclude `recipients_ids` @@ -53,27 +59,6 @@ def attributes=(new_attributes) def recipients_ids=(value) @recipients_ids = value - Rails.logger.debug "Setting recipients_ids to: #{value.inspect}" - end - - after_initialize do - @recipients_ids ||= [] - @send_method ||= 'recipients' - end - - before_create :create_salt - before_validation :create_message_recipients, on: :create - - def create_message_recipients - user_ids = @recipients_ids - user_ids += User.undeleted.pluck(:id) if send_method == 'all' - user_ids += Group.find(group_id).users.pluck(:id) if group_id.present? - user_ids += Order.find(order_id).users_ordered.pluck(:id) if send_method == 'order' - - user_ids.uniq.each do |user_id| - recipient = MessageRecipient.new message: self, user_id: user_id - message_recipients << recipient - end end def add_recipients(users) @@ -166,11 +151,20 @@ def can_toggle_private?(user) private + def create_message_recipients + user_ids = @recipients_ids + user_ids += User.undeleted.pluck(:id) if send_method == 'all' + user_ids += Group.find(group_id).users.pluck(:id) if group_id.present? + user_ids += Order.find(order_id).users_ordered.pluck(:id) if send_method == 'order' + + user_ids.uniq.each do |user_id| + recipient = MessageRecipient.new message: self, user_id: user_id + message_recipients << recipient + end + end + def create_salt self.salt = [Array.new(6) { rand(256).chr }.join].pack('m').chomp end - def recipients_ids_must_be_valid - errors.add(:recipients_ids, "must be valid") if recipients_ids.blank? - end end