docbrown/app/controllers/notifications_controller.rb
Andy Zhao 5ace9a5fc9 Organization Notifications (#1762)
* Add spec for org reaction notification

* Add organization_id to notifications

* Add validations for user and org IDs

* Move comment json_data out of loop

* Use actual column names instead

* Add organization filters

* Remove extra text-align

* Add organization filtering

* Refactor to reduce complexity

* Add receiver argument to handle orgs

* Add has_many notifications for orgs

* Add follow notifications for orgs

* Add comment notifications for orgs

* Rename some tests for specificity

* Add mark org notifications as read functionality

* Add radix to parseInt

* Define json_data outside of loop

* Notify org followers of a new post as well

* Move test to proper location

* Add test for aggregated method

* Make send org reaction notification more readable

* Test the rest of the notification model

* Refactor tag adjustment spec

* Fix extra html
2019-02-25 14:25:37 -08:00

66 lines
2.7 KiB
Ruby

class NotificationsController < ApplicationController
# No authorization required because we provide authentication on notifications page
def index
if 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
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