Sha256: aea31b0250d450e5212c5321d7bc8cd2fdc682c14988b17f6053e855a03a48b4

Contents?: true

Size: 1.29 KB

Versions: 40

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module Messaging
    # A command with all the business logic for replying to a conversation
    class StartConversation < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A conversation form
      def initialize(form)
        @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 conversation.save
          notify_interlocutors

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

      private

      def conversation
        @conversation ||= Conversation.start(
          originator: originator,
          interlocutors: [form.recipient],
          body: form.body
        )
      end

      def notify_interlocutors
        conversation.interlocutors(originator).each do |recipient|
          ConversationMailer.new_conversation(originator, recipient, conversation).deliver_later
        end
      end

      def originator
        form.current_user
      end

      attr_reader :form
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.20.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.20.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.19.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.18.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.19.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.17.2 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.18.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.17.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.16.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.17.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.16.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.15.2 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.15.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.15.0 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.14.4 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.14.3 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.14.2 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.14.1 app/commands/decidim/messaging/start_conversation.rb
decidim-core-0.13.1 app/commands/decidim/messaging/start_conversation.rb