Sha256: 788c7521e350608e2768eb8db48f9cb84b1c85d3491887f45fa98ab748e2a832

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 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?

        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)

        @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

11 entries across 11 versions & 1 rubygems

Version Path
decidim-comments-0.14.4 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.14.3 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.14.2 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.14.1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.13.1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.12.2 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.13.0 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.12.1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.13.0.pre1 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.12.0 app/commands/decidim/comments/create_comment.rb
decidim-comments-0.12.0.pre app/commands/decidim/comments/create_comment.rb