docbrown/app/controllers/dashboards_controller.rb
John Curcio 157a6f1ef1 Add pagination to followers and following page in dashboard (#258) (#4375) [deploy]
* Add infinite scroll to followers list (#258)

* Refactor fetchNext function for clarity (#258)

* Sepparate following tab into multiple tabs to support infinite scroll (#258)

* Add infinite scroll to following pages (#258)

* Add tests to infinite scroll api

* Refactor dashboard loading text

* Refactor infine scroll function

* Fix duplicated entries problem in infinite scrolling

* Switch randomized attributes to sequential to avoid InvalidRecord error

* Add acceptance tests for infinite scroll

* Remove unused following method

* parameterize limit per page for followers and followings

* Split follows endpoint into followers and followings

* Authenticate user with api key in followers and followings

* Split followers endpoint into users and organizations

* Add redundant html to sublist partial

* Speed up infinite scroll tests

* Refactor api json responses to use partials

* Authenticate api user before follows create

* Resolve conflicts on scrolling js

* Improve partials organization and fix organization username bug

* Improve readability of unauthorized test

* Use let! to create scrolling test data

* Fix not working podcasts link

* Refactor initScrolling to remove linting errors

* Fix codeclimate coding style issue

* Test tags forms and podcasts hyperlinks

* Fix eslint issue with double equals

* Improve before_action usage and readability
2019-11-20 18:21:18 -05:00

84 lines
2.9 KiB
Ruby

class DashboardsController < ApplicationController
before_action :set_no_cache_header
before_action :authenticate_user!
before_action :fetch_and_authorize_user, except: :pro
before_action -> { limit_per_page(default: 80, max: 1000) }, except: %i[show pro]
after_action :verify_authorized
def show
@current_user_pro = current_user.pro?
target = @user
not_authorized if params[:org_id] && !@user.org_admin?(params[:org_id] || @user.any_admin?)
@organizations = @user.admin_organizations
if params[:which] == "organization" && params[:org_id] && (@user.org_admin?(params[:org_id]) || @user.any_admin?)
target = @organizations.find_by(id: params[:org_id])
@organization = target
end
@articles = target.articles.includes(:organization).sorting(params[:sort]).decorate
# Updates analytics in background if appropriate
Articles::UpdateAnalyticsJob.perform_later(current_user.id) if @articles && ApplicationConfig["GA_FETCH_RATE"] < 50 # Rate limit concerned, sometimes we throttle down.
end
def following_tags
@followed_tags = @user.follows_by_type("ActsAsTaggableOn::Tag").
order("points DESC").includes(:followable).limit(@follows_limit)
end
def following_users
@follows = @user.follows_by_type("User").
order("created_at DESC").includes(:followable).limit(@follows_limit)
end
def following_organizations
@followed_organizations = @user.follows_by_type("Organization").
order("created_at DESC").includes(:followable).limit(@follows_limit)
end
def following_podcasts
@followed_podcasts = @user.follows_by_type("Podcast").
order("created_at DESC").includes(:followable).limit(@follows_limit)
end
def followers
if params[:which] == "user_followers"
@follows = Follow.where(followable_id: @user.id, followable_type: "User").
includes(:follower).order("created_at DESC").limit(@follows_limit)
elsif params[:which] == "organization_user_followers"
@follows = Follow.where(followable_id: @user.organization_id, followable_type: "Organization").
includes(:follower).order("created_at DESC").limit(@follows_limit)
end
end
def pro
@user_or_org = if params[:org_id]
org = Organization.find_by(id: params[:org_id])
authorize org, :pro_org_user?
org
else
authorize current_user, :pro_user?
current_user
end
@organizations = current_user.member_organizations
end
private
def fetch_and_authorize_user
@user = if params[:username] && current_user.any_admin?
User.find_by(username: params[:username])
else
current_user
end
authorize (@user || User), :dashboard_show?
end
def limit_per_page(default:, max:)
per_page = (params[:per_page] || default).to_i
@follows_limit = [per_page, max].min
end
end