Clean up legacy comment logic [SCI-9348]

This commit is contained in:
Martin Artnik 2023-09-20 13:07:30 +02:00
parent 825f0e839f
commit 44394fa1b6
16 changed files with 30 additions and 455 deletions

View file

@ -1,71 +0,0 @@
# frozen_string_literal: true
class MyModuleCommentsController < ApplicationController
include ActionView::Helpers::TextHelper
include InputSanitizeHelper
include ApplicationHelper
include CommentHelper
before_action :load_vars
before_action :load_comment, only: %i(update destroy)
before_action :check_view_permissions, only: :index
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def index
comments = @my_module.last_comments(@last_comment_id, @per_page)
unless comments.blank?
more_url = url_for(my_module_my_module_comments_url(@my_module, format: :json, from: comments.first.id))
end
comment_index_helper(comments, more_url, @last_comment_id.positive? ? nil : '/my_module_comments/index')
end
def create
@comment = TaskComment.new(
message: comment_params[:message],
user: current_user,
my_module: @my_module
)
comment_create_helper(@comment)
end
def update
old_text = @comment.message
@comment.message = comment_params[:message]
comment_update_helper(@comment, old_text)
end
def destroy
comment_destroy_helper(@comment)
end
private
def load_vars
@last_comment_id = params[:from].to_i
@per_page = Constants::COMMENTS_SEARCH_LIMIT
@my_module = MyModule.find_by(id: params[:my_module_id])
render_404 unless @my_module
end
def load_comment
@comment = @my_module.task_comments.find(params[:id])
end
def check_view_permissions
render_403 unless can_read_my_module?(@my_module)
end
def check_create_permissions
render_403 unless can_create_my_module_comments?(@my_module)
end
def check_manage_permissions
render_403 unless can_manage_my_module_comment?(@comment)
end
def comment_params
params.require(:comment).permit(:message)
end
end

View file

@ -1,66 +0,0 @@
# frozen_string_literal: true
class ProjectCommentsController < ApplicationController
include ActionView::Helpers::TextHelper
include InputSanitizeHelper
include ApplicationHelper
include CommentHelper
before_action :load_vars
before_action :check_view_permissions, only: :index
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def index
comments = @project.last_comments(@last_comment_id, @per_page)
more_url = project_project_comments_url(@project, format: :json, from: comments.first.id) unless comments.blank?
comment_index_helper(comments, more_url, @last_comment_id.positive? ? nil : '/project_comments/index')
end
def create
@comment = ProjectComment.new(
message: comment_params[:message],
user: current_user,
project: @project
)
comment_create_helper(@comment)
end
def update
old_text = @comment.message
@comment.message = comment_params[:message]
comment_update_helper(@comment, old_text)
end
def destroy
comment_destroy_helper(@comment)
end
private
def load_vars
@last_comment_id = params[:from].to_i
@per_page = Constants::COMMENTS_SEARCH_LIMIT
@project = current_team.projects.find_by(id: params[:project_id])
render_404 unless @project
end
def check_view_permissions
render_403 unless can_read_project?(@project)
end
def check_create_permissions
render_403 unless can_create_project_comments?(@project)
end
def check_manage_permissions
@comment = @project.project_comments.find_by(id: params[:id])
render_403 unless @comment.present? &&
can_manage_project_comment?(@comment)
end
def comment_params
params.require(:comment).permit(:message)
end
end

View file

@ -6,7 +6,6 @@ class ProtocolsController < ApplicationController
include InputSanitizeHelper
include ProtocolsIoHelper
include TeamsHelper
include CommentHelper
include ProtocolsExporterV2
before_action :check_create_permissions, only: %i(

View file

@ -1,68 +0,0 @@
# frozen_string_literal: true
class ResultCommentsController < ApplicationController
include ActionView::Helpers::TextHelper
include InputSanitizeHelper
include ApplicationHelper
include CommentHelper
before_action :load_vars
before_action :check_view_permissions, only: [:index]
before_action :check_add_permissions, only: [:create]
before_action :check_manage_permissions, only: %i(update destroy)
def index
comments = @result.last_comments(@last_comment_id, @per_page)
more_url = result_result_comments_path(@result, format: :json, from: comments.first.id) unless comments.blank?
comment_index_helper(comments, more_url)
end
def create
@comment = ResultComment.new(
message: comment_params[:message],
user: current_user,
result: @result
)
comment_create_helper(@comment)
end
def update
old_text = @comment.message
@comment.message = comment_params[:message]
comment_update_helper(@comment, old_text)
end
def destroy
comment_destroy_helper(@comment)
end
private
def load_vars
@last_comment_id = params[:from].to_i
@per_page = Constants::COMMENTS_SEARCH_LIMIT
@result = Result.find_by_id(params[:result_id])
@my_module = @result.my_module
render_404 unless @result
end
def check_view_permissions
render_403 unless can_read_my_module?(@my_module)
end
def check_add_permissions
render_403 unless can_create_my_module_result_comments?(@my_module)
end
def check_manage_permissions
@comment = @result.result_comments.find_by(id: params[:id])
render_403 unless @comment.present? &&
can_manage_result_comment?(@comment)
end
def comment_params
params.require(:comment).permit(:message)
end
end

View file

@ -1,89 +0,0 @@
# frozen_string_literal: true
class StepCommentsController < ApplicationController
include ActionView::Helpers::TextHelper
include InputSanitizeHelper
include ApplicationHelper
include CommentHelper
before_action :load_vars
before_action :check_view_permissions, only: [:index]
before_action :check_add_permissions, only: [:create]
before_action :check_update_permissions, only: %i(update)
before_action :check_destroy_permissions, only: %i(destroy)
def index
comments = @step.last_comments(@last_comment_id, @per_page)
more_url = step_step_comments_path(@step, format: :json, from: comments.first.id) unless comments.blank?
comment_index_helper(comments, more_url)
end
def create
@comment = StepComment.new(
message: comment_params[:message],
user: current_user,
step: @step
)
comment_create_helper(@comment)
end
def update
old_text = @comment.message
@comment.message = comment_params[:message]
comment_update_helper(@comment, old_text)
end
def destroy
comment_destroy_helper(@comment)
end
private
def load_vars
@last_comment_id = params[:from].to_i
@per_page = Constants::COMMENTS_SEARCH_LIMIT
@step = Step.find_by_id(params[:step_id])
@protocol = @step&.protocol
render_404 unless @step && @protocol
end
def check_view_permissions
render_403 unless can_read_protocol_in_module?(@protocol)
end
def check_add_permissions
render_403 unless can_create_my_module_comments?(@protocol.my_module)
end
def check_destroy_permissions
@comment = @step.step_comments.find_by(id: params[:id])
render_403 unless @comment.present? &&
can_delete_comment_in_my_module_steps?(@comment)
end
def check_update_permissions
@comment = @step.step_comments.find_by(id: params[:id])
render_403 unless @comment.present? &&
can_update_comment_in_my_module_steps?(@comment)
end
def comment_params
params.require(:comment).permit(:message)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: @step.my_module.team,
project: @step.my_module.project,
message_items: {
my_module: @step.my_module.id,
step: @step.id,
step_position: { id: @step.id, value_for: 'position_plus_one' }
})
end
end

View file

@ -1,31 +1,6 @@
# frozen_string_literal: true
module CommentHelper
def comment_action_url(comment)
case comment.type
when 'StepComment'
step_step_comment_path(comment.step, comment, format: :json)
when 'ResultComment'
result_result_comment_path(comment.result, comment, format: :json)
when 'ProjectComment'
project_project_comment_path(comment.project, comment, format: :json)
when 'TaskComment'
my_module_my_module_comment_path(comment.my_module, comment, format: :json)
end
end
def comment_index_helper(comments, more_url, partial = nil)
partial ||= 'shared/comments/list'
render json: {
perPage: @per_page,
resultsNumber: comments.size,
moreUrl: more_url,
html: render_to_string(
partial: partial, locals: { comments: comments }, formats: :html
)
}
end
def comment_create_helper(comment, partial = 'item')
if comment.save
case comment.type

View file

@ -1,9 +0,0 @@
<h5 class="text-center"><%= t('experiments.canvas.popups.comments_tab') %></h5>
<hr>
<%= render partial: 'shared/comments/comments', locals: {
object: @my_module,
comments: comments,
can_create_comments: can_create_my_module_comments?(@my_module),
create_url: my_module_my_module_comments_path(@my_module, format: :json),
more_url: my_module_my_module_comments_path(@my_module, format: :json, from: comments.first&.id)
} %>

View file

@ -1,10 +0,0 @@
<h5 class="text-center"><%= t('projects.index.comment_tab') %></h5>
<hr>
<%= render partial: 'shared/comments/comments', locals: {
object: @project,
comments: comments,
can_create_comments: can_create_project_comments?(@project),
create_url: project_project_comments_path(@project, format: :json),
more_url: project_project_comments_path(@project, format: :json, from: comments.first&.id)
} %>

View file

@ -0,0 +1,26 @@
<div
class="comment-container report"
data-field-to-update="message"
data-params-group="comment"
data-original-name="<%= comment.message %>"
data-response-field="comment"
data-smart-annotation="true"
data-edit-mode="0"
>
<div class="avatar-placehodler">
<span class='global-avatar-container'>
<% base64_encoded = defined?(export_all) && export_all %>
<%= image_tag user_avatar_absolute_url(comment.user, :icon_small, base64_encoded), class: 'avatar' %>
</span>
</div>
<div class="content-placeholder">
<div class="comment-name"><%= comment.user.full_name %></div>
<div class="comment-right">
<div class="comment-datetime"><%= l(comment.created_at, format: :full) %></div>
</div>
<div class="comment-message">
<div class="view-mode"><%= custom_auto_link(comment.message, team: current_team, simple_format: true) %></div>
</div>
<div class="error-block"></div>
</div>
</div>

View file

@ -15,8 +15,8 @@
<div class="col-xs-12 comments-container simple">
<ul class="no-style content-comments">
<% comments.each do |comment| %>
<%= render partial: 'shared/comments/item',
locals: { comment: comment, readonly: true, report: true, export_all: export_all } %>
<%= render partial: 'reports/elements/comment_element',
locals: { comment: comment, export_all: export_all } %>
<% end %>
</ul>
</div>

View file

@ -12,8 +12,8 @@
<div class="comments-container simple">
<ul class="no-style content-comments">
<% comments.each do |comment| %>
<%= render partial: 'shared/comments/item',
locals: { comment: comment, readonly: true, report: true, export_all: export_all } %>
<%= render partial: 'reports/elements/comment_element',
locals: { comment: comment, export_all: export_all } %>
<% end %>
</ul>
</div>

View file

@ -1,16 +0,0 @@
<hr>
<div class="col-xs-12 comments-title">
<h4>
<%=t('my_modules.results.comments_tab') %>
(<span id="comment-counter-<%= result.id %>"><%= comments.count %></span>)
</h4>
</div>
<%= render partial: 'shared/comments/comments', locals: {
object: result,
comments: comments,
can_create_comments: can_create_my_module_result_comments?(@my_module),
create_url: result_result_comments_path(result, format: :json),
more_url: result_result_comments_path(result, format: :json, from: comments.first&.id)
} %>

View file

@ -1,27 +0,0 @@
<div class="comments-container" data-object-id = <%= object.id %>>
<% per_page = Constants::COMMENTS_SEARCH_LIMIT %>
<div class="content-comments inline_scroll_block">
<% if comments.size == per_page %>
<div class="comment-more text-center">
<a class="btn btn-secondary btn-more-comments-new"
href="<%= more_url %>"
data-remote="true">
<%=t "general.more_comments" %>
</a>
</div>
<% end %>
<div class="comments-list">
<%= render partial: 'shared/comments/list', locals: { comments: comments} %>
<% comments.mark_as_seen_by(current_user, object) %>
</div>
</div>
<% if can_create_comments %>
<div class="new-message-container" data-create-url="<%= create_url %>">
<%= text_area_tag 'message', '', placeholder: t('general.comment_placeholder_new'),
class: 'form-control smart-text-area textarea-sm',
data: { 'atwho-edit' => '' } %>
<i class="sn-icon sn-icon-send new-comment-button"></i>
<span class="new-message-error"></span>
</div>
<% end %>
</div>

View file

@ -1,55 +0,0 @@
<% report = false unless defined?(report) %>
<% readonly = false unless defined?(readonly) %>
<% edit_mode = (!readonly && comment_editable?(comment)) %>
<div
class="comment-container <%= edit_mode ? 'inline-init-handler' : '' %> <%= report ? 'report' : '' %>"
data-field-to-update="message"
data-params-group="comment"
data-path-to-update="<%= comment_action_url(comment) %>"
data-original-name="<%= comment.message %>"
data-response-field="comment"
data-smart-annotation="true"
data-edit-mode="0"
>
<div class="avatar-placehodler">
<span class='global-avatar-container'>
<% if report %>
<% base64_encoded = defined?(export_all) && export_all %>
<%= image_tag user_avatar_absolute_url(comment.user, :icon_small, base64_encoded), class: 'avatar' %>
<% else %>
<%= image_tag avatar_path(comment.user, :icon_small), class: 'avatar' %>
<% end %>
</span>
</div>
<div class="content-placeholder">
<div class="comment-name"><%= comment.user.full_name %></div>
<div class="comment-right">
<div class="comment-datetime"><%= l(comment.created_at, format: :full) %></div>
<% if edit_mode %>
<div class="comment-actions">
<div class="edit-buttons">
<span class="save-button"><i class="fas fa-save"></i><%= t('general.save') %></span>
<span class="cancel-button"><i class="sn-icon sn-icon-close"></i></span>
</div>
<div class="view-buttons">
<span class="edit-button"><i class="sn-icon sn-icon-edit"></i><%= t('general.edit') %></span>
<a href="#"
class="delete-button"
data-url="<%= comment_action_url(comment) %>"
data-confirm-message="<%= t('comments.delete_confirm') %>"
data-turbolinks="false">
<span class="sn-icon sn-icon-delete action-icon-delete"></span>
</a>
</div>
</div>
<% end %>
</div>
<div class="comment-message">
<div class="view-mode"><%= custom_auto_link(comment.message, team: current_team, simple_format: true) %></div>
<% if edit_mode %>
<%= text_area_tag 'message', comment.message, disabled: true, class: 'smart-text-area hidden' %>
<% end %>
</div>
<div class="error-block"></div>
</div>
</div>

View file

@ -1,4 +0,0 @@
<% comments.each do |comment| %>
<%= render partial: 'shared/comments/item',
locals: { comment: comment } %>
<% end %>

View file

@ -326,9 +326,6 @@ Rails.application.routes.draw do
end
resources :projects, except: [:destroy] do
resources :project_comments,
path: '/comments',
only: %i(create index update destroy)
# Activities popup (JSON) for individual project in projects index,
# as well as all activities page for single project (HTML)
resources :project_activities, path: '/activities', only: [:index]
@ -467,10 +464,6 @@ Rails.application.routes.draw do
resource :shareable_link, controller: :my_module_shareable_links, only: %i(show create update destroy)
resources :my_module_comments,
path: '/comments',
only: %i(create index update destroy)
get :repositories_dropdown_list, controller: :my_module_repositories
get :repositories_list_html, controller: :my_module_repositories
@ -569,9 +562,6 @@ Rails.application.routes.draw do
resources :step_orderable_elements do
post :reorder, on: :collection
end
resources :step_comments,
path: '/comments',
only: %i(create index update destroy)
resources :tables, controller: 'step_elements/tables', only: %i(create destroy update) do
member do
get :move_targets