-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16147 from hennevogel/refactoring/notification-sc…
…opes Refactor Notification scopes
- Loading branch information
Showing
21 changed files
with
267 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 12 additions & 12 deletions
24
src/api/app/components/notification_filter_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/api/app/controllers/concerns/webui/notifications_filter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Webui::NotificationsFilter | ||
extend ActiveSupport::Concern | ||
|
||
# It's just a case... | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
def filter_notifications_by_type(notifications, filter_type) | ||
case filter_type | ||
when 'comments' | ||
notifications.for_comments | ||
when 'requests' | ||
notifications.for_requests | ||
when 'incoming_requests' | ||
notifications.for_incoming_requests(User.session!) | ||
when 'outgoing_requests' | ||
notifications.for_outgoing_requests(User.session!) | ||
when 'relationships_created' | ||
notifications.for_relationships_created | ||
when 'relationships_deleted' | ||
notifications.for_relationships_deleted | ||
when 'build_failures' | ||
notifications.for_failed_builds | ||
when 'reports' | ||
notifications.for_reports | ||
when 'workflow_runs' | ||
notifications.for_workflow_runs | ||
when 'appealed_decisions' | ||
notifications.for_appealed_decisions | ||
else # all | ||
notifications | ||
end | ||
end | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
end |
71 changes: 45 additions & 26 deletions
71
src/api/app/controllers/person/notifications_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,72 @@ | ||
module Person | ||
class NotificationsController < ApplicationController | ||
include Person::Errors | ||
include Webui::NotificationsFilter | ||
|
||
MAX_PER_PAGE = 300 | ||
ALLOWED_FILTERS = %w[read comments requests unread incoming_requests outgoing_requests relationships_created relationships_deleted | ||
build_failures reports workflow_runs appealed_decisions].freeze | ||
ALLOWED_FILTERS = %w[all comments requests incoming_requests outgoing_requests relationships_created relationships_deleted build_failures | ||
reports reviews workflow_runs appealed_decisions].freeze | ||
ALLOWED_STATES = %w[unread read].freeze | ||
|
||
before_action :check_filter_type, except: [:update] | ||
before_action :set_filter_type, only: :index | ||
before_action :set_filter_state, only: :index | ||
before_action :set_notifications, only: :index | ||
before_action :set_notification, only: :update | ||
|
||
# GET /my/notifications | ||
def index | ||
@notifications = paginated_notifications | ||
@notifications_count = @notifications.count | ||
@paged_notifications = @notifications.page(params[:page]) | ||
|
||
params[:page] = @paged_notifications.total_pages if @paged_notifications.out_of_range? | ||
params[:show_maximum] ? show_maximum(@notifications) : @paged_notifications | ||
end | ||
|
||
def update | ||
notification = authorize Notification.find(params[:id]) | ||
|
||
notification.toggle(:delivered).save! | ||
authorize @notification | ||
|
||
render_ok | ||
if @notification.toggle(:delivered).save | ||
render_ok | ||
else | ||
render_error(message: @notification.errors.full_messages.to_sentence, status: 400) | ||
end | ||
end | ||
|
||
private | ||
|
||
def show_maximum(notifications) | ||
total = notifications.size | ||
notifications.page(params[:page]).per([total, MAX_PER_PAGE].min) | ||
def set_notifications | ||
@notifications = User.session!.notifications | ||
@notifications = @notifications.for_project_name(params[:project]) if params[:project] | ||
@notifications = @notifications.for_group_title(params[:group]) if params[:group] | ||
@notifications = filter_notifications_by_type(@notifications, @filter_type) | ||
@notifications = filter_notifications_by_state(@notifications, @filter_state) | ||
end | ||
|
||
def set_notification | ||
@notification = User.session!.notifications.find(params[:id]) | ||
end | ||
|
||
def fetch_notifications | ||
notifications = policy_scope(Notification) | ||
filtered_notifications = if params[:project] | ||
notifications.unread.for_project_name(params[:project]) | ||
else | ||
notifications | ||
end | ||
filtered_notifications.for_notifiable_type(@filter_type) | ||
def filter_notifications_by_state(notifications, filter_state) | ||
case filter_state | ||
when 'read' | ||
notifications.read | ||
else | ||
notifications.unread | ||
end | ||
end | ||
|
||
def paginated_notifications | ||
notifications = fetch_notifications | ||
params[:page] = notifications.page(params[:page]).total_pages if notifications.page(params[:page]).out_of_range? | ||
params[:show_maximum] ? show_maximum(notifications) : notifications.page(params[:page]) | ||
def show_maximum(notifications) | ||
total = notifications.size | ||
notifications.page(params[:page]).per([total, Notification::MAX_PER_PAGE].min) | ||
end | ||
|
||
def check_filter_type | ||
@filter_type = params[:notifications_type] | ||
def set_filter_type | ||
@filter_type = params[:kind] || 'all' | ||
raise FilterNotSupportedError if @filter_type.present? && ALLOWED_FILTERS.exclude?(@filter_type) | ||
end | ||
|
||
def set_filter_state | ||
@filter_state = params[:state] || 'unread' | ||
raise FilterNotSupportedError if @filter_state.present? && ALLOWED_STATES.exclude?(@filter_state) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.