Sha256: 9c1c792cc5df5f8843c52b4fae2ae4ca899ca7a6d45120ae53bee0d195ad8f13

Contents?: true

Size: 1.36 KB

Versions: 23

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Decidim
  module Messaging
    # A command with all the business logic for replying to a conversation
    class ReplyToConversation < Rectify::Command
      # Public: Initializes the command.
      #
      # conversation - The conversation to be updated.
      # form - A form object with the params.
      def initialize(conversation, form)
        @conversation = conversation
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the message.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        if message.save
          notify_interlocutors

          broadcast(:ok, message)
        else
          broadcast(:invalid)
        end
      end

      private

      def sender
        form.current_user
      end

      def message
        @message ||= conversation.add_message(sender: sender, body: form.body)
      end

      def notify_interlocutors
        conversation.interlocutors(sender).each do |recipient|
          ConversationMailer.new_message(sender, recipient, conversation).deliver_later if conversation.unread_count(recipient) == 1
        end
      end

      attr_reader :conversation, :form
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
decidim-core-0.15.2 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.15.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.15.0 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.14.4 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.14.3 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.14.2 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.14.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.13.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.12.2 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.13.0 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.12.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.13.0.pre1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.12.0 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.11.2 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.12.0.pre app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.11.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.11.0.pre1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.10.1 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.10.0 app/commands/decidim/messaging/reply_to_conversation.rb
decidim-core-0.9.3 app/commands/decidim/messaging/reply_to_conversation.rb