Sha256: ca0e8e100d2a2fbc71d818f00fb87da8fda39058a3940b79f1175b40bd8fa727

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    # A command with all the business logic to create a new comment
    class CreateComment < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form, author, commentable)
        @form = form
        @author = author
        @commentable = commentable
      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?

        transaction do
          create_comment
        end

        broadcast(:ok, comment)
      end

      private

      attr_reader :form, :comment

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

        @comment = Comment.create!(author: @author,
                                   commentable: @commentable,
                                   root_commentable: root_commentable(@commentable),
                                   body: parsed.rewrite,
                                   alignment: form.alignment,
                                   decidim_user_group_id: form.user_group_id)

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

      def send_notifications(mentioned_users)
        NewCommentNotificationCreator.new(comment, mentioned_users).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

3 entries across 3 versions & 1 rubygems

Version Path
decidim-comments-0.11.2 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.11.1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.11.0.pre1 app/commands/decidim/comments/create_comment.rb