Sha256: 4b25fe2c042735de3be860c6c07980e1f04f8c60bef3f0e618a6842647290eac

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Decidim
  module Messaging
    module ConversationHelper
      #
      # Links to the conversation between the current user and another user
      #
      def link_to_current_or_new_conversation_with(user, title = t("decidim.contact"))
        link_to current_or_new_conversation_path_with(user), title: title do
          icon "envelope-closed", aria_label: title, class: "icon--small"
        end
      end

      #
      # Finds the right path to the conversation the current user and another
      # user.
      #
      # * If there's no current user, it returns to the login form path.
      #
      # * If there's no prior existing conversation between the users, it
      #   returns the new conversation form path.
      #
      # * Otherwise, it returns the path to the existing conversation.
      #
      # @param user [Decidim::User] The user to link to a conversation with
      #
      # @return [String] The resulting route
      #
      def current_or_new_conversation_path_with(user)
        return decidim.new_user_session_path unless user_signed_in?

        conversation = conversation_between(current_user, user)

        if conversation
          decidim.conversation_path(conversation)
        else
          decidim.new_conversation_path(recipient_id: user.id)
        end
      end

      #
      # Finds the conversation between the given participants
      #
      # @param participants [Array<Decidim::User>] The participants to find a
      #   conversation between.
      #
      # @return [Decidim::Messaging::Conversation]
      def conversation_between(*participants)
        return if participants.to_set.length <= 1

        UserConversations.for(participants.first).find do |conversation|
          conversation.participants.to_set == participants.to_set
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.10.1 app/helpers/decidim/messaging/conversation_helper.rb
decidim-core-0.10.0 app/helpers/decidim/messaging/conversation_helper.rb
decidim-core-0.9.3 app/helpers/decidim/messaging/conversation_helper.rb
decidim-core-0.9.2 app/helpers/decidim/messaging/conversation_helper.rb
decidim-core-0.9.1 app/helpers/decidim/messaging/conversation_helper.rb
decidim-core-0.9.0 app/helpers/decidim/messaging/conversation_helper.rb