Sha256: 3af31870adf47e4d989c2a5aac5c0e67ca9419b70a4cd2e9f68c90815cd9ad49

Contents?: true

Size: 1.97 KB

Versions: 12

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    # A command with all the business logic to create a new comment
    class CreateComment < Decidim::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form, author)
        @form = form
        @author = author
      end

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

        create_comment

        broadcast(:ok, comment)
      end

      private

      attr_reader :form, :comment

      def create_comment
        parsed = Decidim::ContentProcessor.parse(form.body, current_organization: form.current_organization)

        params = {
          author: @author,
          commentable: form.commentable,
          root_commentable: root_commentable(form.commentable),
          body: { I18n.locale => parsed.rewrite },
          alignment: form.alignment,
          decidim_user_group_id: form.user_group_id,
          participatory_space: form.current_component.try(:participatory_space)
        }

        @comment = Decidim.traceability.create!(
          Comment,
          @author,
          params,
          visibility: "public-only"
        )

        mentioned_users = parsed.metadata[:user].users
        mentioned_groups = parsed.metadata[:user_group].groups
        CommentCreation.publish(@comment, parsed.metadata)
        send_notifications(mentioned_users, mentioned_groups)
      end

      def send_notifications(mentioned_users, mentioned_groups)
        NewCommentNotificationCreator.new(comment, mentioned_users, mentioned_groups).create
      end

      def root_commentable(commentable)
        return commentable.root_commentable if commentable.is_a? Decidim::Comments::Comment

        commentable
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-comments-0.27.9 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.8 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.7 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.6 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.5 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.4 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.3 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.2 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.0 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.0.rc2 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.27.0.rc1 app/commands/decidim/comments/create_comment.rb