Module: Decidim::Comments

Defined in:
lib/decidim/comments.rb,
lib/decidim/comments/admin.rb,
lib/decidim/comments/engine.rb,
lib/decidim/comments/export.rb,
app/models/decidim/comments/seed.rb,
lib/decidim/comments/commentable.rb,
lib/decidim/comments/admin_engine.rb,
app/models/decidim/comments/comment.rb,
lib/decidim/comments/comments_helper.rb,
lib/decidim/comments/api/comment_type.rb,
lib/decidim/comments/query_extensions.rb,
app/forms/decidim/comments/comment_form.rb,
lib/decidim/comments/comment_serializer.rb,
app/models/decidim/comments/comment_vote.rb,
lib/decidim/comments/mutation_extensions.rb,
lib/decidim/comments/api/add_comment_type.rb,
app/commands/decidim/comments/vote_comment.rb,
app/types/decidim/comments/commentable_type.rb,
app/commands/decidim/comments/create_comment.rb,
app/queries/decidim/comments/sorted_comments.rb,
app/models/decidim/comments/application_record.rb,
lib/decidim/comments/api/comment_mutation_type.rb,
app/models/decidim/comments/abilities/admin_user.rb,
app/types/decidim/comments/commentable_interface.rb,
app/resolvers/decidim/comments/vote_comment_resolver.rb,
app/types/decidim/comments/commentable_mutation_type.rb,
app/mailers/decidim/comments/comment_notification_mailer.rb,
app/models/decidim/comments/abilities/process_admin_user.rb

Overview

This module contains all the logic related to the comments feature. It exposes a single entry point as a rails helper method to render a React component which handle all the comments render and logic.

Defined Under Namespace

Modules: Abilities, Admin, Commentable, CommentsHelper, Export, MutationExtensions, QueryExtensions Classes: AdminEngine, ApplicationRecord, Comment, CommentForm, CommentNotificationMailer, CommentSerializer, CommentVote, CreateComment, Engine, Seed, SortedComments, VoteComment, VoteCommentResolver

Constant Summary collapse

CommentType =

This type represents a comment on a commentable object.

GraphQL::ObjectType.define do
  name "Comment"
  description "A comment"

  interfaces [
    Decidim::Comments::CommentableInterface
  ]

  field :id, !types.ID, "The Comment's unique ID"

  field :sgid, !types.String, "The Comment's signed global id" do
    resolve lambda { |obj, _args, _ctx|
      obj.to_sgid.to_s
    }
  end

  field :body, !types.String, "The comment message"

  field :createdAt, !types.String, "The creation date of the comment" do
    resolve lambda { |obj, _args, _ctx|
      obj.created_at.iso8601
    }
  end

  field :author, !Decidim::AuthorInterface, "The comment's author" do
    resolve lambda { |obj, _args, _ctx|
      obj.user_group || obj.author
    }
  end

  field :alignment, types.Int, "The comment's alignment. Can be 0 (neutral), 1 (in favor) or -1 (against)'"

  field :upVotes, !types.Int, "The number of comment's upVotes" do
    resolve lambda { |obj, _args, _ctx|
      obj.up_votes.size
    }
  end

  field :upVoted, !types.Boolean, "Check if the current user has upvoted the comment" do
    resolve lambda { |obj, _args, ctx|
      obj.up_voted_by?(ctx[:current_user])
    }
  end

  field :downVotes, !types.Int, "The number of comment's downVotes" do
    resolve lambda { |obj, _args, _ctx|
      obj.down_votes.size
    }
  end

  field :downVoted, !types.Boolean, "Check if the current user has downvoted the comment" do
    resolve lambda { |obj, _args, ctx|
      obj.down_voted_by?(ctx[:current_user])
    }
  end

  field :hasComments, !types.Boolean, "Check if the commentable has comments" do
    resolve lambda { |obj, _args, _ctx|
      obj.accepts_new_comments? && obj.comments.size.positive?
    }
  end

  field :alreadyReported, !types.Boolean, "Check if the current user has reported the comment" do
    resolve lambda { |obj, _args, ctx|
      obj.reported_by?(ctx[:current_user])
    }
  end
end
AddCommentType =

This type represents a mutation to create new comments.

GraphQL::ObjectType.define do
  name "Add comment"
  description "Add a new comment"

  field :comment, CommentType, "The new created comment"
end
CommentableType =

This type represents a commentable object.

GraphQL::ObjectType.define do
  name "Commentable"
  description "A commentable object"

  interfaces [
    Decidim::Comments::CommentableInterface
  ]
end
CommentMutationType =
GraphQL::ObjectType.define do
  name "CommentMutation"
  description "A comment which includes its available mutations"

  field :id, !types.ID, "The Comment's unique ID"

  field :upVote, Decidim::Comments::CommentType do
    resolve VoteCommentResolver.new(weight: 1)
  end

  field :downVote, Decidim::Comments::CommentType do
    resolve VoteCommentResolver.new(weight: -1)
  end
end
CommentableInterface =

This interface represents a commentable object.

GraphQL::InterfaceType.define do
  name "CommentableInterface"
  description "A commentable interface"

  field :id, !types.ID, "The commentable's ID"

  field :type, !types.String, "The commentable's class name. i.e. `Decidim::ParticipatoryProcess`" do
    property :commentable_type
  end

  field :acceptsNewComments, !types.Boolean, "Whether the object can have new comments or not" do
    property :accepts_new_comments?
  end

  field :commentsHaveAlignment, !types.Boolean, "Whether the object comments have alignment or not" do
    property :comments_have_alignment?
  end

  field :commentsHaveVotes, !types.Boolean, "Whether the object comments have votes or not" do
    property :comments_have_votes?
  end

  field :comments do
    type !types[CommentType]

    argument :orderBy, types.String, "Order the comments"

    resolve lambda { |obj, args, _ctx|
      SortedComments.for(obj, order_by: args[:orderBy])
    }
  end

  field :hasComments, !types.Boolean, "Check if the commentable has comments" do
    resolve lambda { |obj, _args, _ctx|
      obj.comments.size.positive?
    }
  end
end
CommentableMutationType =
GraphQL::ObjectType.define do
  name "CommentableMutation"
  description "A commentable which includes its available mutations"

  field :id, !types.ID, "The Commentable's unique ID"

  field :addComment, Decidim::Comments::CommentType do
    description "Add a new comment to a commentable"

    argument :body, !types.String, "The comments's body"
    argument :alignment, types.Int, "The comment's alignment. Can be 0 (neutral), 1 (in favor) or -1 (against)'", default_value: 0
    argument :userGroupId, types.ID, "The comment's user group id. Replaces the author."

    resolve lambda { |obj, args, ctx|
      params = { "comment" => { "body" => args[:body], "alignment" => args[:alignment], "user_group_id" => args[:userGroupId] } }
      form = Decidim::Comments::CommentForm.from_params(params)
      Decidim::Comments::CreateComment.call(form, ctx[:current_user], obj) do
        on(:ok) do |comment|
          return comment
        end
      end
    }
  end
end