2019-06-04 20:40:21 +08:00
|
|
|
# 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
|
|
|
|
|
2020-01-08 19:32:59 +08:00
|
|
|
def comment_index_helper(comments, more_url, partial = nil)
|
2023-06-21 20:13:20 +08:00
|
|
|
partial ||= 'shared/comments/list'
|
2020-01-08 19:32:59 +08:00
|
|
|
render json: {
|
2019-06-04 20:40:21 +08:00
|
|
|
perPage: @per_page,
|
|
|
|
resultsNumber: comments.size,
|
|
|
|
moreUrl: more_url,
|
|
|
|
html: render_to_string(
|
2023-07-05 18:43:23 +08:00
|
|
|
partial: partial, locals: { comments: comments }, formats: :html
|
2019-06-04 20:40:21 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-11 17:03:13 +08:00
|
|
|
def comment_create_helper(comment, partial = 'item')
|
2019-06-04 20:40:21 +08:00
|
|
|
if comment.save
|
|
|
|
case comment.type
|
|
|
|
when 'StepComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
step_comment_annotation_notification(comment)
|
|
|
|
log_step_activity(:add_comment_to_step, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ResultComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
result_comment_annotation_notification(comment)
|
|
|
|
log_result_activity(:add_comment_to_result, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ProjectComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
project_comment_annotation_notification(comment)
|
|
|
|
log_project_activity(:add_comment_to_project, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'TaskComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
my_module_comment_annotation_notification(comment)
|
|
|
|
log_my_module_activity(:add_comment_to_module, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2020-11-21 03:25:28 +08:00
|
|
|
partial: "/shared/comments/#{partial}.html.erb",
|
2019-06-04 20:40:21 +08:00
|
|
|
locals: {
|
2022-05-25 21:04:14 +08:00
|
|
|
comment: comment,
|
|
|
|
skip_header: false
|
2023-07-05 18:43:23 +08:00
|
|
|
},
|
|
|
|
formats: :html
|
2019-06-04 20:40:21 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else
|
2020-11-21 03:25:28 +08:00
|
|
|
render json: { errors: comment.errors.to_hash(true) }, status: :unprocessable_entity
|
2019-06-04 20:40:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-30 18:42:30 +08:00
|
|
|
def comment_addable?(object)
|
|
|
|
case object.class.name
|
|
|
|
when 'MyModule'
|
2021-09-14 17:08:35 +08:00
|
|
|
can_create_my_module_comments?(object)
|
2021-09-24 19:11:41 +08:00
|
|
|
when 'Step'
|
2023-03-22 19:52:08 +08:00
|
|
|
can_create_comments_in_my_module_steps?(object.my_module)
|
2021-09-24 19:11:41 +08:00
|
|
|
when 'Result'
|
2023-03-22 19:52:08 +08:00
|
|
|
can_create_my_module_result_comments?(object.my_module)
|
2020-12-30 18:42:30 +08:00
|
|
|
when 'Project'
|
2021-09-07 17:27:52 +08:00
|
|
|
can_create_project_comments?(object)
|
2020-12-30 18:42:30 +08:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-08 19:32:59 +08:00
|
|
|
def comment_editable?(comment)
|
|
|
|
case comment.type
|
2021-09-22 17:28:32 +08:00
|
|
|
when 'TaskComment'
|
|
|
|
can_manage_my_module_comment?(comment)
|
2021-09-24 19:11:41 +08:00
|
|
|
when 'StepComment'
|
2021-10-11 16:19:16 +08:00
|
|
|
can_update_comment_in_my_module_steps?(comment)
|
2021-09-24 19:11:41 +08:00
|
|
|
when 'ResultComment'
|
2021-10-14 17:48:09 +08:00
|
|
|
can_manage_result_comment?(comment)
|
2020-01-08 19:32:59 +08:00
|
|
|
when 'ProjectComment'
|
2021-09-30 17:32:11 +08:00
|
|
|
can_manage_project_comment?(comment)
|
2020-01-08 19:32:59 +08:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-21 03:25:28 +08:00
|
|
|
def comment_update_helper(comment, old_text, partial = nil)
|
2019-06-04 20:40:21 +08:00
|
|
|
if comment.save
|
|
|
|
case comment.type
|
|
|
|
when 'StepComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
step_comment_annotation_notification(comment, old_text)
|
|
|
|
log_step_activity(:edit_step_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ResultComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
result_comment_annotation_notification(comment, old_text)
|
|
|
|
log_result_activity(:edit_result_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ProjectComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
project_comment_annotation_notification(comment, old_text)
|
|
|
|
log_project_activity(:edit_project_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'TaskComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
my_module_comment_annotation_notification(comment, old_text)
|
|
|
|
log_my_module_activity(:edit_module_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
end
|
|
|
|
|
2020-11-21 03:25:28 +08:00
|
|
|
if partial
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: "/shared/comments/#{partial}.html.erb",
|
|
|
|
locals: {
|
2022-05-25 21:04:14 +08:00
|
|
|
comment: comment,
|
|
|
|
skip_header: false
|
2023-07-05 18:43:23 +08:00
|
|
|
},
|
|
|
|
formats: :html
|
2020-11-21 03:25:28 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
message = custom_auto_link(comment.message, team: current_team, simple_format: true)
|
|
|
|
render json: { comment: message }, status: :ok
|
|
|
|
end
|
2019-06-04 20:40:21 +08:00
|
|
|
else
|
|
|
|
render json: { errors: comment.errors.to_hash(true) },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-11 17:03:13 +08:00
|
|
|
def comment_destroy_helper(comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
if comment.destroy
|
|
|
|
case comment.type
|
|
|
|
when 'StepComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
log_step_activity(:delete_step_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ResultComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
log_result_activity(:delete_result_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'ProjectComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
log_project_activity(:delete_project_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
when 'TaskComment'
|
2020-11-21 03:25:28 +08:00
|
|
|
log_my_module_activity(:delete_module_comment, comment)
|
2019-06-04 20:40:21 +08:00
|
|
|
end
|
2021-01-11 17:03:13 +08:00
|
|
|
render json: {}, status: :ok
|
2019-06-04 20:40:21 +08:00
|
|
|
else
|
|
|
|
render json: { message: I18n.t('comments.delete_error') },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2020-11-21 03:25:28 +08:00
|
|
|
|
|
|
|
def result_comment_annotation_notification(comment, old_text = nil)
|
|
|
|
result = comment.result
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: old_text,
|
|
|
|
new_text: comment.message,
|
|
|
|
title: t('notifications.result_comment_annotation_title',
|
|
|
|
result: result.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.result_annotation_message_html',
|
|
|
|
project: link_to(result.my_module.experiment.project.name,
|
|
|
|
project_url(result.my_module
|
|
|
|
.experiment
|
|
|
|
.project)),
|
|
|
|
experiment: link_to(result.my_module.experiment.name,
|
2023-01-12 18:31:04 +08:00
|
|
|
my_modules_experiment_url(result.my_module.experiment)),
|
2020-11-21 03:25:28 +08:00
|
|
|
my_module: link_to(result.my_module.name,
|
|
|
|
protocols_my_module_url(
|
|
|
|
result.my_module
|
|
|
|
)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_comment_annotation_notification(comment, old_text = nil)
|
|
|
|
project = comment.project
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: old_text,
|
|
|
|
new_text: comment.message,
|
|
|
|
title: t('notifications.project_comment_annotation_title',
|
|
|
|
project: project.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.project_annotation_message_html',
|
|
|
|
project: link_to(project.name, project_url(project)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def step_comment_annotation_notification(comment, old_text = nil)
|
|
|
|
step = comment.step
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: old_text,
|
|
|
|
new_text: comment.message,
|
|
|
|
title: t('notifications.step_comment_annotation_title',
|
|
|
|
step: step.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.step_annotation_message_html',
|
|
|
|
project: link_to(step.my_module.experiment.project.name,
|
|
|
|
project_url(step.my_module
|
|
|
|
.experiment
|
|
|
|
.project)),
|
|
|
|
experiment: link_to(step.my_module.experiment.name,
|
2023-01-12 18:31:04 +08:00
|
|
|
my_modules_experiment_url(step.my_module.experiment)),
|
2020-11-21 03:25:28 +08:00
|
|
|
my_module: link_to(step.my_module.name,
|
|
|
|
protocols_my_module_url(
|
|
|
|
step.my_module
|
|
|
|
)),
|
|
|
|
step: link_to(step.name,
|
|
|
|
protocols_my_module_url(step.my_module)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def my_module_comment_annotation_notification(comment, old_text = nil)
|
|
|
|
my_module = comment.my_module
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: old_text,
|
|
|
|
new_text: comment.message,
|
|
|
|
title: t('notifications.my_module_comment_annotation_title',
|
|
|
|
my_module: my_module.name,
|
|
|
|
user: current_user.full_name),
|
2021-03-20 00:11:19 +08:00
|
|
|
message: t('notifications.my_module_comment_annotation_message_html',
|
2020-11-21 03:25:28 +08:00
|
|
|
project: link_to(my_module.experiment.project.name,
|
|
|
|
project_url(my_module
|
|
|
|
.experiment
|
|
|
|
.project)),
|
|
|
|
experiment: link_to(my_module.experiment.name,
|
2023-01-12 18:31:04 +08:00
|
|
|
my_modules_experiment_url(my_module.experiment)),
|
2020-11-21 03:25:28 +08:00
|
|
|
my_module: link_to(my_module.name,
|
|
|
|
protocols_my_module_url(
|
|
|
|
my_module
|
|
|
|
)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_my_module_activity(type_of, comment)
|
|
|
|
my_module = comment.my_module
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
team: my_module.experiment.project.team,
|
|
|
|
project: my_module.experiment.project,
|
|
|
|
subject: my_module,
|
|
|
|
message_items: { my_module: my_module.id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_project_activity(type_of, comment)
|
|
|
|
project = comment.project
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: project,
|
|
|
|
team: project.team,
|
|
|
|
project: project,
|
|
|
|
message_items: { project: project.id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_step_activity(type_of, comment)
|
|
|
|
step = comment.step
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: step.protocol,
|
|
|
|
team: step.my_module.experiment.project.team,
|
|
|
|
project: step.my_module.experiment.project,
|
|
|
|
message_items: {
|
|
|
|
my_module: step.my_module.id,
|
|
|
|
step: step.id,
|
|
|
|
step_position: { id: step.id, value_for: 'position_plus_one' }
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_result_activity(type_of, comment)
|
|
|
|
result = comment.result
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: result,
|
|
|
|
team: result.my_module.experiment.project.team,
|
|
|
|
project: result.my_module.experiment.project,
|
|
|
|
message_items: { result: result.id })
|
|
|
|
end
|
2020-11-30 22:55:20 +08:00
|
|
|
|
|
|
|
def has_unseen_comments?(commentable)
|
2021-12-13 23:43:16 +08:00
|
|
|
commentable.comments.any? { |comment| comment.unseen_by.include?(current_user.id) }
|
2020-11-30 22:55:20 +08:00
|
|
|
end
|
2022-10-27 20:41:35 +08:00
|
|
|
|
|
|
|
def count_unseen_comments(commentable, current_user)
|
|
|
|
commentable.comments.count { |comment| comment.unseen_by.include?(current_user.id) }
|
|
|
|
end
|
2019-06-04 20:40:21 +08:00
|
|
|
end
|