decidim-comments 0.23.3 → 0.24.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/decidim/comments/bundle.js.map +1 -1
- data/app/assets/javascripts/decidim/comments/comments.component.js.es6 +292 -0
- data/app/assets/javascripts/decidim/comments/comments.component.test.js +581 -0
- data/app/assets/javascripts/decidim/comments/comments.js.erb +1 -1
- data/app/cells/decidim/comments/comment/actions.erb +7 -0
- data/app/cells/decidim/comments/comment/alignment_badge.erb +4 -0
- data/app/cells/decidim/comments/comment/author.erb +1 -0
- data/app/cells/decidim/comments/comment/show.erb +40 -0
- data/app/cells/decidim/comments/comment/utilities.erb +13 -0
- data/app/cells/decidim/comments/comment/votes.erb +25 -0
- data/app/cells/decidim/comments/comment_card_cell.rb +22 -0
- data/app/cells/decidim/comments/comment_cell.rb +142 -8
- data/app/cells/decidim/comments/comment_form/comment_as.erb +10 -0
- data/app/cells/decidim/comments/comment_form/show.erb +24 -0
- data/app/cells/decidim/comments/comment_form_cell.rb +94 -0
- data/app/cells/decidim/comments/comment_m/top.erb +1 -1
- data/app/cells/decidim/comments/comment_thread/show.erb +6 -0
- data/app/cells/decidim/comments/comment_thread/title.erb +3 -0
- data/app/cells/decidim/comments/comment_thread_cell.rb +30 -0
- data/app/cells/decidim/comments/comments/add_comment.erb +30 -0
- data/app/cells/decidim/comments/comments/blocked_comments_warning.erb +3 -0
- data/app/cells/decidim/comments/comments/order_control.erb +37 -0
- data/app/cells/decidim/comments/comments/show.erb +32 -0
- data/app/cells/decidim/comments/comments/single_comment_warning.erb +9 -0
- data/app/cells/decidim/comments/comments/user_comments_blocked_warning.erb +3 -0
- data/app/cells/decidim/comments/comments_cell.rb +134 -0
- data/app/commands/decidim/comments/vote_comment.rb +34 -10
- data/app/controllers/decidim/comments/application_controller.rb +16 -0
- data/app/controllers/decidim/comments/comments_controller.rb +122 -0
- data/app/controllers/decidim/comments/votes_controller.rb +41 -0
- data/app/events/decidim/comments/comment_created_event.rb +1 -1
- data/app/events/decidim/comments/comment_downvoted_event.rb +8 -0
- data/app/events/decidim/comments/comment_upvoted_event.rb +8 -0
- data/app/events/decidim/comments/comment_voted_event.rb +26 -0
- data/app/forms/decidim/comments/comment_form.rb +6 -2
- data/app/models/decidim/comments/comment.rb +22 -6
- data/app/models/decidim/comments/seed.rb +1 -1
- data/app/permissions/decidim/comments/permissions.rb +59 -0
- data/app/queries/decidim/comments/metrics/comment_participants_metric_measure.rb +2 -2
- data/app/queries/decidim/comments/metrics/comments_metric_manage.rb +5 -6
- data/app/queries/decidim/comments/sorted_comments.rb +18 -14
- data/app/views/decidim/comments/comments/_comment.html.erb +5 -0
- data/app/views/decidim/comments/comments/_comments.html.erb +1 -0
- data/app/views/decidim/comments/comments/create.js.erb +16 -0
- data/app/views/decidim/comments/comments/error.js.erb +1 -0
- data/app/views/decidim/comments/comments/index.js.erb +24 -0
- data/app/views/decidim/comments/comments/reload.js.erb +21 -0
- data/app/views/decidim/comments/votes/create.js.erb +23 -0
- data/app/views/decidim/comments/votes/error.js.erb +1 -0
- data/config/locales/ar.yml +0 -2
- data/config/locales/ca.yml +17 -2
- data/config/locales/cs.yml +24 -7
- data/config/locales/de.yml +17 -2
- data/config/locales/el.yml +0 -2
- data/config/locales/en.yml +17 -2
- data/config/locales/es-MX.yml +17 -2
- data/config/locales/es-PY.yml +17 -2
- data/config/locales/es.yml +17 -2
- data/config/locales/eu.yml +0 -2
- data/config/locales/fi-plain.yml +17 -2
- data/config/locales/fi.yml +17 -2
- data/config/locales/fr-CA.yml +17 -2
- data/config/locales/fr.yml +17 -2
- data/config/locales/gl.yml +7 -2
- data/config/locales/hu.yml +0 -2
- data/config/locales/id-ID.yml +0 -2
- data/config/locales/is-IS.yml +0 -1
- data/config/locales/it.yml +4 -2
- data/config/locales/ja.yml +8 -4
- data/config/locales/lv.yml +0 -2
- data/config/locales/nl.yml +0 -2
- data/config/locales/no.yml +0 -2
- data/config/locales/pl.yml +21 -4
- data/config/locales/pt-BR.yml +0 -2
- data/config/locales/pt.yml +0 -2
- data/config/locales/ro-RO.yml +19 -2
- data/config/locales/ru.yml +0 -2
- data/config/locales/sk.yml +0 -2
- data/config/locales/sv.yml +17 -2
- data/config/locales/tr-TR.yml +7 -2
- data/config/locales/uk.yml +0 -1
- data/config/locales/zh-CN.yml +0 -2
- data/lib/decidim/api/add_comment_type.rb +13 -0
- data/lib/decidim/api/comment_mutation_type.rb +22 -0
- data/lib/decidim/api/comment_type.rb +89 -0
- data/lib/decidim/api/commentable_interface.rb +50 -0
- data/lib/decidim/api/commentable_mutation_type.rb +30 -0
- data/{app/types/decidim/comments → lib/decidim/api}/commentable_type.rb +2 -5
- data/lib/decidim/comments.rb +1 -3
- data/lib/decidim/comments/api.rb +12 -0
- data/lib/decidim/comments/comments_helper.rb +10 -52
- data/lib/decidim/comments/engine.rb +9 -7
- data/lib/decidim/comments/mutation_extensions.rb +22 -22
- data/lib/decidim/comments/query_extensions.rb +12 -14
- data/lib/decidim/comments/test.rb +1 -0
- data/lib/decidim/comments/test/shared_examples/comment_voted_event.rb +65 -0
- data/lib/decidim/comments/version.rb +1 -1
- metadata +65 -64
- data/app/assets/javascripts/decidim/comments/bundle.js +0 -268
- data/app/frontend/application/apollo_client.ts +0 -12
- data/app/frontend/application/application.component.test.tsx +0 -23
- data/app/frontend/application/application.component.tsx +0 -35
- data/app/frontend/application/icon.component.test.tsx +0 -38
- data/app/frontend/application/icon.component.tsx +0 -46
- data/app/frontend/comments/add_comment_form.component.test.tsx +0 -265
- data/app/frontend/comments/add_comment_form.component.tsx +0 -482
- data/app/frontend/comments/comment.component.test.tsx +0 -490
- data/app/frontend/comments/comment.component.tsx +0 -677
- data/app/frontend/comments/comment_order_selector.component.test.tsx +0 -29
- data/app/frontend/comments/comment_order_selector.component.tsx +0 -106
- data/app/frontend/comments/comment_thread.component.test.tsx +0 -82
- data/app/frontend/comments/comment_thread.component.tsx +0 -81
- data/app/frontend/comments/comments.component.test.tsx +0 -150
- data/app/frontend/comments/comments.component.tsx +0 -289
- data/app/frontend/comments/down_vote_button.component.test.tsx +0 -59
- data/app/frontend/comments/down_vote_button.component.tsx +0 -133
- data/app/frontend/comments/up_vote_button.component.test.tsx +0 -59
- data/app/frontend/comments/up_vote_button.component.tsx +0 -133
- data/app/frontend/comments/vote_button.component.tsx +0 -50
- data/app/frontend/comments/vote_button_component.test.tsx +0 -64
- data/app/frontend/entry.ts +0 -38
- data/app/frontend/entry_test.ts +0 -6
- data/app/frontend/fragments/add_comment_form_commentable.fragment.graphql +0 -4
- data/app/frontend/fragments/add_comment_form_session.fragment.graphql +0 -6
- data/app/frontend/fragments/comment.fragment.graphql +0 -14
- data/app/frontend/fragments/comment_data.fragment.graphql +0 -27
- data/app/frontend/fragments/comment_thread.fragment.graphql +0 -6
- data/app/frontend/fragments/down_vote_button.fragment.graphql +0 -6
- data/app/frontend/fragments/up_vote_button.fragment.graphql +0 -6
- data/app/frontend/mutations/add_comment.mutation.graphql +0 -9
- data/app/frontend/mutations/down_vote.mutation.graphql +0 -9
- data/app/frontend/mutations/up_vote.mutation.graphql +0 -9
- data/app/frontend/queries/comments.query.graphql +0 -26
- data/app/frontend/support/asset_url.ts +0 -11
- data/app/frontend/support/generate_comments_data.ts +0 -49
- data/app/frontend/support/generate_user_data.ts +0 -14
- data/app/frontend/support/generate_user_group_data.ts +0 -14
- data/app/frontend/support/graphql_transformer.js +0 -32
- data/app/frontend/support/load_translations.ts +0 -48
- data/app/frontend/support/require_all.ts +0 -10
- data/app/frontend/support/resolve_graphql_query.ts +0 -37
- data/app/frontend/support/schema.ts +0 -2026
- data/app/types/decidim/comments/commentable_interface.rb +0 -61
- data/app/types/decidim/comments/commentable_mutation_type.rb +0 -33
- data/lib/decidim/comments/api/add_comment_type.rb +0 -13
- data/lib/decidim/comments/api/comment_mutation_type.rb +0 -20
- data/lib/decidim/comments/api/comment_type.rb +0 -89
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module Comments
|
5
|
+
# A cell to display a comment thread.
|
6
|
+
class CommentThreadCell < Decidim::ViewModel
|
7
|
+
def title
|
8
|
+
return unless has_threads?
|
9
|
+
|
10
|
+
render :title
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def has_threads?
|
16
|
+
model.comment_threads.any?
|
17
|
+
end
|
18
|
+
|
19
|
+
def author_name
|
20
|
+
return t("decidim.components.comment.deleted_user") if model.author.deleted?
|
21
|
+
|
22
|
+
model.author.name
|
23
|
+
end
|
24
|
+
|
25
|
+
def order
|
26
|
+
options[:order] || "older"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
<div class="add-comment">
|
2
|
+
<h4 class="section-heading"><%= t("decidim.components.add_comment_form.title") %></h4>
|
3
|
+
|
4
|
+
<% if user_signed_in? %>
|
5
|
+
<% if alignment_enabled? %>
|
6
|
+
<div class="opinion-toggle button-group">
|
7
|
+
<button class="button tiny button--muted opinion-toggle--ok">
|
8
|
+
<%= icon "thumb-up", role: "none presentation" %>
|
9
|
+
</button>
|
10
|
+
<button class="button tiny button--muted opinion-toggle--meh is-active">
|
11
|
+
<%= t("decidim.components.add_comment_form.opinion.neutral") %>
|
12
|
+
</button>
|
13
|
+
<button class="button tiny button--muted opinion-toggle--ko">
|
14
|
+
<%= icon "thumb-down", role: "none presentation" %>
|
15
|
+
</button>
|
16
|
+
</div>
|
17
|
+
<% end %>
|
18
|
+
<%== cell("decidim/comments/comment_form", model, root_depth: root_depth) %>
|
19
|
+
<% else %>
|
20
|
+
<p>
|
21
|
+
<span>
|
22
|
+
<%== t(
|
23
|
+
"decidim.components.add_comment_form.account_message",
|
24
|
+
sign_in_url: decidim.new_user_session_path,
|
25
|
+
sign_up_url: decidim.new_user_registration_path
|
26
|
+
) %>
|
27
|
+
</span>
|
28
|
+
</p>
|
29
|
+
<% end %>
|
30
|
+
</div>
|
@@ -0,0 +1,37 @@
|
|
1
|
+
<div class="order-by__dropdown order-by__dropdown--right">
|
2
|
+
<span class="order-by__text"><%= t("decidim.components.comment_order_selector.title") %></span>
|
3
|
+
<ul class="dropdown menu"
|
4
|
+
data-dropdown-menu="data-dropdown-menu"
|
5
|
+
data-autoclose="false"
|
6
|
+
data-disable-hover="true"
|
7
|
+
data-click-open="true"
|
8
|
+
data-close-on-click="true"
|
9
|
+
tabindex="-1"
|
10
|
+
role="menubar">
|
11
|
+
<li class="is-dropdown-submenu-parent opens-right" tabindex="-1" role="none">
|
12
|
+
<a href="#" id="comments-order-menu-control"
|
13
|
+
aria-label="<%= t("decidim.components.comment_order_selector.title") %>"
|
14
|
+
aria-controls="comments-order-menu"
|
15
|
+
aria-haspopup="true"
|
16
|
+
role="menuitem"><%= t("decidim.components.comment_order_selector.order.#{order}") %></a>
|
17
|
+
<ul class="menu is-dropdown-submenu submenu first-sub vertical"
|
18
|
+
id="comments-order-chooser-menu"
|
19
|
+
role="menu"
|
20
|
+
aria-labelledby="comments-order-menu-control"
|
21
|
+
tabindex="-1"
|
22
|
+
data-submenu="">
|
23
|
+
<% available_orders.each do |order_value| %>
|
24
|
+
<li role="none" class="is-submenu-item is-dropdown-submenu-item">
|
25
|
+
<%= link_to(
|
26
|
+
t("decidim.components.comment_order_selector.order.#{order_value}"),
|
27
|
+
decidim_comments.comments_path(commentable_gid: model.to_signed_global_id.to_s, order: order_value, reload: 1),
|
28
|
+
tabindex: -1,
|
29
|
+
role: "menuitem",
|
30
|
+
remote: true
|
31
|
+
) %>
|
32
|
+
</li>
|
33
|
+
<% end %>
|
34
|
+
</ul>
|
35
|
+
</li>
|
36
|
+
</ul>
|
37
|
+
</div>
|
@@ -0,0 +1,32 @@
|
|
1
|
+
<%= content_tag :div, id: node_id, data: { decidim_comments: comments_data } do %>
|
2
|
+
<div class="columns large-9 comments-container" id="comments">
|
3
|
+
<div class="comments">
|
4
|
+
<div class="row collapse order-by">
|
5
|
+
<h2 class="order-by__text section-heading">
|
6
|
+
<% if single_comment? %>
|
7
|
+
<%= t("decidim.components.comments.comment_details_title") %>
|
8
|
+
<% else %>
|
9
|
+
<span class="comments-count">
|
10
|
+
<%= t("decidim.components.comments.title", count: comments_count) %>
|
11
|
+
</span>
|
12
|
+
<% end %>
|
13
|
+
</h2>
|
14
|
+
<%= render :order_control %>
|
15
|
+
</div>
|
16
|
+
<%= single_comment_warning %>
|
17
|
+
<%= blocked_comments_warning %>
|
18
|
+
<div class="comment-threads">
|
19
|
+
<% comments.each do |comment| %>
|
20
|
+
<%= cell("decidim/comments/comment_thread", comment, order: order) %>
|
21
|
+
<% end %>
|
22
|
+
</div>
|
23
|
+
<%= add_comment %>
|
24
|
+
<%= user_comments_blocked_warning %>
|
25
|
+
</div>
|
26
|
+
<div class="callout primary loading-comments hide">
|
27
|
+
<p><%= t("decidim.components.comments.loading") %></p>
|
28
|
+
</div>
|
29
|
+
</div>
|
30
|
+
<% end %>
|
31
|
+
|
32
|
+
<%= javascript_include_tag("decidim/comments/comments") %>
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module Comments
|
5
|
+
# A cell to display a comments section for a commentable object.
|
6
|
+
class CommentsCell < Decidim::ViewModel
|
7
|
+
include Decidim::IconHelper
|
8
|
+
|
9
|
+
delegate :user_signed_in?, to: :controller
|
10
|
+
|
11
|
+
def add_comment
|
12
|
+
return if single_comment?
|
13
|
+
return if comments_blocked?
|
14
|
+
return if user_comments_blocked?
|
15
|
+
|
16
|
+
render :add_comment
|
17
|
+
end
|
18
|
+
|
19
|
+
def single_comment_warning
|
20
|
+
return unless single_comment?
|
21
|
+
|
22
|
+
render :single_comment_warning
|
23
|
+
end
|
24
|
+
|
25
|
+
def blocked_comments_warning
|
26
|
+
return unless comments_blocked?
|
27
|
+
return unless user_comments_blocked?
|
28
|
+
|
29
|
+
render :blocked_comments_warning
|
30
|
+
end
|
31
|
+
|
32
|
+
def user_comments_blocked_warning
|
33
|
+
return if comments_blocked? # Shows already the general warning
|
34
|
+
return unless user_comments_blocked?
|
35
|
+
|
36
|
+
render :user_comments_blocked_warning
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def decidim_comments
|
42
|
+
Decidim::Comments::Engine.routes.url_helpers
|
43
|
+
end
|
44
|
+
|
45
|
+
def comments
|
46
|
+
if single_comment?
|
47
|
+
[single_comment]
|
48
|
+
else
|
49
|
+
SortedComments.for(model, order_by: order)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def comments_count
|
54
|
+
model.comments.count
|
55
|
+
end
|
56
|
+
|
57
|
+
def root_depth
|
58
|
+
return 0 unless single_comment?
|
59
|
+
|
60
|
+
single_comment.depth
|
61
|
+
end
|
62
|
+
|
63
|
+
def commentable_path(params = {})
|
64
|
+
resource_locator(model).path(params)
|
65
|
+
end
|
66
|
+
|
67
|
+
def alignment_enabled?
|
68
|
+
model.comments_have_alignment?
|
69
|
+
end
|
70
|
+
|
71
|
+
def available_orders
|
72
|
+
%w(best_rated recent older most_discussed)
|
73
|
+
end
|
74
|
+
|
75
|
+
def order
|
76
|
+
options[:order] || "older"
|
77
|
+
end
|
78
|
+
|
79
|
+
def decidim
|
80
|
+
Decidim::Core::Engine.routes.url_helpers
|
81
|
+
end
|
82
|
+
|
83
|
+
def node_id
|
84
|
+
"comments-for-#{commentable_type.demodulize}-#{model.id}"
|
85
|
+
end
|
86
|
+
|
87
|
+
def commentable_type
|
88
|
+
model.commentable_type
|
89
|
+
end
|
90
|
+
|
91
|
+
def comments_data
|
92
|
+
{
|
93
|
+
singleComment: single_comment?,
|
94
|
+
toggleTranslations: machine_translations_toggled?,
|
95
|
+
commentableGid: model.to_signed_global_id.to_s,
|
96
|
+
commentsUrl: decidim_comments.comments_path,
|
97
|
+
rootDepth: root_depth,
|
98
|
+
lastCommentId: last_comment_id,
|
99
|
+
order: order
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
def last_comment_id
|
104
|
+
Decidim::Comments::Comment.where(
|
105
|
+
root_commentable: model
|
106
|
+
).order(:id).pluck(:id).last
|
107
|
+
end
|
108
|
+
|
109
|
+
def single_comment?
|
110
|
+
single_comment.present?
|
111
|
+
end
|
112
|
+
|
113
|
+
def single_comment
|
114
|
+
return if options[:single_comment].blank?
|
115
|
+
|
116
|
+
@single_comment ||= model.comments.find_by(id: options[:single_comment])
|
117
|
+
end
|
118
|
+
|
119
|
+
def machine_translations_toggled?
|
120
|
+
options[:machine_translations] == true
|
121
|
+
end
|
122
|
+
|
123
|
+
def comments_blocked?
|
124
|
+
!model.accepts_new_comments?
|
125
|
+
end
|
126
|
+
|
127
|
+
def user_comments_blocked?
|
128
|
+
return false unless user_signed_in?
|
129
|
+
|
130
|
+
!model.user_allowed_to_comment?(current_user)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -23,27 +23,51 @@ module Decidim
|
|
23
23
|
#
|
24
24
|
# Returns nothing.
|
25
25
|
def call
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
case @weight
|
27
|
+
when 1
|
28
|
+
previous_vote = @comment.up_votes.find_by(author: @author)
|
29
|
+
if previous_vote
|
30
|
+
previous_vote.destroy!
|
30
31
|
else
|
31
|
-
@comment.up_votes.create!(author: @author)
|
32
|
+
@vote = @comment.up_votes.create!(author: @author)
|
32
33
|
end
|
33
|
-
|
34
|
-
|
35
|
-
if
|
36
|
-
|
34
|
+
when -1
|
35
|
+
previous_vote = @comment.down_votes.find_by(author: @author)
|
36
|
+
if previous_vote
|
37
|
+
previous_vote.destroy!
|
37
38
|
else
|
38
|
-
@comment.down_votes.create!(author: @author)
|
39
|
+
@vote = @comment.down_votes.create!(author: @author)
|
39
40
|
end
|
40
41
|
else
|
41
42
|
return broadcast(:invalid)
|
42
43
|
end
|
44
|
+
|
45
|
+
notify_comment_author if @vote
|
43
46
|
broadcast(:ok, @comment)
|
44
47
|
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
45
48
|
broadcast(:invalid)
|
46
49
|
end
|
50
|
+
|
51
|
+
def notify_comment_author
|
52
|
+
Decidim::EventsManager.publish(
|
53
|
+
event: "decidim.events.comments.comment_#{upvote? ? "upvoted" : "downvoted"}",
|
54
|
+
event_class: upvote? ? Decidim::Comments::CommentUpvotedEvent : Decidim::Comments::CommentDownvotedEvent,
|
55
|
+
resource: @comment.commentable,
|
56
|
+
affected_users: [@comment.author],
|
57
|
+
extra: {
|
58
|
+
comment_id: @comment.id,
|
59
|
+
weight: @weight,
|
60
|
+
downvotes: @comment.down_votes.count,
|
61
|
+
upvotes: @comment.up_votes.count
|
62
|
+
}
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def upvote?
|
69
|
+
@weight.positive?
|
70
|
+
end
|
47
71
|
end
|
48
72
|
end
|
49
73
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module Comments
|
5
|
+
# This controller is the abstract class from which all other controllers of
|
6
|
+
# this engine inherit.
|
7
|
+
class ApplicationController < Decidim::ApplicationController
|
8
|
+
def permission_class_chain
|
9
|
+
[
|
10
|
+
::Decidim::Comments::Permissions,
|
11
|
+
::Decidim::Permissions
|
12
|
+
]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module Comments
|
5
|
+
# Controller that manages the comments for a commentable object.
|
6
|
+
#
|
7
|
+
class CommentsController < Decidim::Comments::ApplicationController
|
8
|
+
include Decidim::ResourceHelper
|
9
|
+
|
10
|
+
before_action :authenticate_user!, only: [:create]
|
11
|
+
before_action :set_commentable
|
12
|
+
before_action :ensure_commentable!
|
13
|
+
|
14
|
+
helper_method :root_depth, :commentable, :order, :reply?, :reload?
|
15
|
+
|
16
|
+
def index
|
17
|
+
enforce_permission_to :read, :comment, commentable: commentable
|
18
|
+
|
19
|
+
@comments = SortedComments.for(
|
20
|
+
commentable,
|
21
|
+
order_by: order,
|
22
|
+
after: params.fetch(:after, 0).to_i
|
23
|
+
)
|
24
|
+
@comments_count = commentable.comments.count
|
25
|
+
|
26
|
+
respond_to do |format|
|
27
|
+
format.js do
|
28
|
+
if reload?
|
29
|
+
render :reload
|
30
|
+
else
|
31
|
+
render :index
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# This makes sure bots are not causing unnecessary log entries.
|
36
|
+
format.html { redirect_to commentable_path }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def create
|
41
|
+
enforce_permission_to :create, :comment, commentable: commentable
|
42
|
+
|
43
|
+
form = Decidim::Comments::CommentForm.from_params(
|
44
|
+
params.merge(commentable: commentable)
|
45
|
+
).with_context(
|
46
|
+
current_organization: current_organization,
|
47
|
+
current_component: commentable.component
|
48
|
+
)
|
49
|
+
Decidim::Comments::CreateComment.call(form, current_user) do
|
50
|
+
on(:ok) do |comment|
|
51
|
+
handle_success(comment)
|
52
|
+
respond_to do |format|
|
53
|
+
format.js { render :create }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
on(:invalid) do
|
58
|
+
@error = t("create.error", scope: "decidim.comments.comments")
|
59
|
+
respond_to do |format|
|
60
|
+
format.js { render :error }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_reader :commentable, :comment
|
69
|
+
|
70
|
+
def set_commentable
|
71
|
+
@commentable = GlobalID::Locator.locate_signed(commentable_gid)
|
72
|
+
end
|
73
|
+
|
74
|
+
def ensure_commentable!
|
75
|
+
raise ActionController::RoutingError, "Not Found" unless commentable
|
76
|
+
end
|
77
|
+
|
78
|
+
def handle_success(comment)
|
79
|
+
@comment = comment
|
80
|
+
@comments_count = begin
|
81
|
+
case commentable
|
82
|
+
when Decidim::Comments::Comment
|
83
|
+
commentable.root_commentable.comments.count
|
84
|
+
else
|
85
|
+
commentable.comments.count
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def commentable_gid
|
91
|
+
case action_name
|
92
|
+
when "create"
|
93
|
+
params.require(:comment).fetch(:commentable_gid)
|
94
|
+
else
|
95
|
+
params.fetch(:commentable_gid, nil)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def reply?(comment)
|
100
|
+
comment.root_commentable != comment.commentable
|
101
|
+
end
|
102
|
+
|
103
|
+
def order
|
104
|
+
params.fetch(:order, "older")
|
105
|
+
end
|
106
|
+
|
107
|
+
def reload?
|
108
|
+
params.fetch(:reload, 0).to_i == 1
|
109
|
+
end
|
110
|
+
|
111
|
+
def root_depth
|
112
|
+
params.fetch(:root_depth, 0).to_i
|
113
|
+
end
|
114
|
+
|
115
|
+
def commentable_path
|
116
|
+
return commentable.polymorphic_resource_path({}) if commentable&.respond_to?(:polymorphic_resource_path)
|
117
|
+
|
118
|
+
resource_locator(commentable).path
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|