Skip to content

Commit

Permalink
Merge pull request #16340 from saraycp/drop_description_notification_…
Browse files Browse the repository at this point in the history
…component

Drop NotificationActionDescriptionComponent
  • Loading branch information
saraycp committed Jun 17, 2024
2 parents 740c994 + a3aed0e commit 934fa0c
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 287 deletions.

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/app/components/notification_component.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
.col-auto.pe-0
= render NotificationAvatarsComponent.new(@notification)
.col-auto.ps-xs-2
= render NotificationActionDescriptionComponent.new(@notification)
.smart-overflow= helpers.description(@notification)
.row.d-none.d-md-block.ps-4
.col
%p.mt-3.mb-0= helpers.render_without_markdown(helpers.excerpt(@notification))
82 changes: 82 additions & 0 deletions src/api/app/helpers/webui/notification_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics/ModuleLength
module Webui::NotificationHelper
TRUNCATION_LENGTH = 100
TRUNCATION_ELLIPSIS_LENGTH = 3 # `...` is the default ellipsis for String#truncate
Expand Down Expand Up @@ -27,6 +28,41 @@ def excerpt(notification)
truncate_to_first_new_line(text.to_s) # sometimes text can be nil
end

# rubocop:disable Metrics/CyclomaticComplexity
# TODO: Content of ViewComponent. Move to sub-classes once STI is set.
def description(notification)
case notification.event_type
when 'Event::RequestStatechange', 'Event::RequestCreate', 'Event::ReviewWanted', 'Event::CommentForRequest'
# TODO: find an alternative when this is moved to the STI model
source_and_target(notification)
when 'Event::CommentForProject'
"#{notification.notifiable.commentable.name}"
when 'Event::CommentForPackage'
commentable = notification.notifiable.commentable
"#{commentable.project.name} / #{commentable.name}"
when 'Event::RelationshipCreate'
"#{notification.event_payload['who']} made #{recipient(notification)} #{notification.event_payload['role']} of #{target_object(notification)}"
when 'Event::RelationshipDelete'
"#{notification.event_payload['who']} removed #{recipient(notification)} as #{notification.event_payload['role']} of #{target_object(notification)}"
when 'Event::BuildFail'
"Build was triggered because of #{notification.event_payload['reason']}"
# TODO: Remove `Event::CreateReport` after all existing records are migrated to the new STI classes
when 'Event::CreateReport', 'Event::ReportForProject', 'Event::ReportForPackage', 'Event::ReportForUser'
"'#{notification.notifiable.user.login}' created a report for a #{notification.event_payload['reportable_type'].downcase}. This is the reason:"
when 'Event::ReportForRequest'
"'#{notification.notifiable.user.login}' created a report for a request. This is the reason:"
when 'Event::ReportForComment'
"'#{notification.notifiable.user.login}' created a report for a comment from #{notification.event_payload['commenter']}. This is the reason:"
when 'Event::ClearedDecision'
"'#{notification.notifiable.moderator.login}' decided to clear the report. This is the reason:"
when 'Event::FavoredDecision'
"'#{notification.notifiable.moderator.login}' decided to favor the report. This is the reason:"
when 'Event::AppealCreated'
"'#{notification.notifiable.appellant.login}' appealed the decision for the following reason:"
end
end
# rubocop:enable Metrics/CyclomaticComplexity

private

def mark_as_read_or_unread_button(notification)
Expand All @@ -45,4 +81,50 @@ def truncate_to_first_new_line(text)
truncation_index = !first_new_line_index.nil? && first_new_line_index < TRUNCATION_LENGTH ? first_new_line_index + TRUNCATION_ELLIPSIS_LENGTH : TRUNCATION_LENGTH
text.truncate(truncation_index)
end

def bs_request(notification)
if notification.notifiable_type == 'BsRequest'
notification.notifiable
elsif notification.notifiable.commentable.is_a?(BsRequestAction)
notification.notifiable.commentable.bs_request
else
notification.notifiable.commentable
end
end

def recipient(notification)
# If a notification is for a group, the notified user needs to know for which group. Otherwise, the user is simply referred to as 'you'.
notification.event_payload.fetch('group', 'you')
end

def target_object(notification)
[notification.event_payload['project'], notification.event_payload['package']].compact.join(' / ')
end

def source(notification)
first_bs_request_action = bs_request(notification).bs_request_actions.first

return '' if bs_request(notification).bs_request_actions.size > 1

[first_bs_request_action.source_project, first_bs_request_action.source_package].compact.join(' / ')
end

def target(notification)
first_bs_request_action = bs_request(notification).bs_request_actions.first

return first_bs_request_action.target_project if bs_request(notification).bs_request_actions.size > 1

[first_bs_request_action.target_project, first_bs_request_action.target_package].compact.join(' / ')
end

def source_and_target(notification)
capture do
if source(notification).present?
concat(tag.span(source(notification)))
concat(tag.i(nil, class: 'fas fa-long-arrow-alt-right text-info mx-2'))
end
concat(tag.span(target(notification)))
end
end
end
# rubocop:enable Metrics/ModuleLength

This file was deleted.

This file was deleted.

Loading

0 comments on commit 934fa0c

Please sign in to comment.