docbrown/app/controllers/notifications_controller.rb
Keshav Biswa 8d57d45ecc Rubocop Style cops enabled (#2056)
* Rubocop enabled style/alias

* Enabled Style/ArrayJoin Cop

* Enabled Style/Attr

* Enabled Case Equality

* Enabled CharacterLiteral

*  Enabled ColonMethodCall Cop

* Enabled CommentAnnotation cop

* Enabled PreferredHashMethods Cop

*  Enabled DoubleNegation Cop

* Enabled EachWithObject Cop

* Enabled EmptyLiteral Cop

* Enabled EvenOdd Cop

* Enabled IfWithSemicolon Cop

* Enabled Lambda and LambdaCall Cop

* Enabled LineEndConcatenation Cop

* Enabled ModuleFunction Cop;

* Enable NegatedIf and NegatedWhile Cop

* Enabled NilComparison Cop

* Enabled Not Cop

* Enabled NumericLiterals Cop

* Enabled OneLineConditional Cop

* Enabled PercentLiteralDelimiters Cop

* Excluded internal/users_controller from negated_if cop

* Reverted the double negation change from github_issue_tag and github_issue.rb"

* Enabled PerlBackrefs Cop

* Changed Regexp.last_match(1) to Regexp.last_match(0)

* Enabled proc cop

* Enabled RaiseArgs Cop

* Reverted Regexp.last_match(0) to Regexp.last_match(1)

* Enabled SelfAssignment Cop

* Enabled SingleLineMethods Cop

* Enabled SpecialGlobalVars Cop

* Enabled VariableInterpolation Cop

* Enabled WhenThen Cop

* Enabled WhileUntilModifier Cop

* Enabled WordArray Cop

* Enabled IfUnlessModifier Cop

* Enabled GuardClause Cop
2019-03-15 18:33:54 -04:00

66 lines
2.7 KiB
Ruby

class NotificationsController < ApplicationController
# No authorization required because we provide authentication on notifications page
def index
return unless user_signed_in?
@notifications_index = true
@user = user_to_view
if params[:page]
num = 45
notified_at_offset = Notification.find(params[:page])&.notified_at
else
num = 10
end
@notifications = if (params[:org_id].present? || params[:filter] == "org") && allowed_user?
organization_notifications
elsif params[:org_id].blank? && params[:filter].present?
filtered_notifications
else
Notification.where(user_id: @user.id).order("notified_at DESC")
end
@last_user_reaction = @user.reactions.last&.id
@last_user_comment = @user.comments.last&.id
@notifications = @notifications.where("notified_at < ?", notified_at_offset) if notified_at_offset
@notifications = NotificationDecorator.decorate_collection(@notifications.limit(num))
org_id = params[:org_id] || current_user.organization_id
# in the future this can be an array of numbers, when people can belong to multiple orgs.
@total_org_unread = Notification.where(organization_id: org_id, user_id: nil, read: false).count
render partial: "notifications_list" if notified_at_offset
end
private
def user_to_view
if params[:username] && current_user.admin?
User.find_by_username(params[:username])
else
current_user
end
end
def filtered_notifications
if params[:filter].to_s.casecmp("posts").zero?
Notification.where(user_id: @user.id, notifiable_type: "Article", action: "Published").
order("notified_at DESC")
elsif params[:filter].to_s.casecmp("comments").zero?
Notification.where(user_id: @user.id, notifiable_type: "Comment", action: nil). # Nil action means not reaction in this context
or(Notification.where(user_id: @user.id, notifiable_type: "Mention")).
order("notified_at DESC")
end
end
def organization_notifications
if params[:filter].to_s.casecmp("comments").zero?
Notification.where(organization_id: params[:org_id], notifiable_type: "Comment", action: nil, user_id: nil). # Nil action means not reaction in this context
or(Notification.where(organization_id: params[:org_id], notifiable_type: "Mention", user_id: nil)).
order("notified_at DESC")
else
Notification.where(organization_id: params[:org_id], user_id: nil).
order("notified_at DESC")
end
end
def allowed_user?
@user.organization_id == params[:org_id] || @user.admin?
end
end