Move followings to internal API (#6494)
This commit is contained in:
parent
e2f190512b
commit
0fd6c948d1
9 changed files with 72 additions and 76 deletions
|
|
@ -94,13 +94,13 @@ function fetchNextFollowingPage(el) {
|
|||
var indexParams = JSON.parse(el.dataset.params);
|
||||
var action = indexParams.action;
|
||||
if (action.includes("users")) {
|
||||
fetchNext(el, "/api/followings/users", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
fetchNext(el, "/followings/users", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
} else if (action.includes("podcasts")) {
|
||||
fetchNext(el, "/api/followings/podcasts", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
fetchNext(el, "/followings/podcasts", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
} else if (action.includes("organizations")) {
|
||||
fetchNext(el, "/api/followings/organizations", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
fetchNext(el, "/followings/organizations", insertNext({ elId: "follows" }, buildFollowsHTML));
|
||||
} else {
|
||||
fetchNext(el, "/api/followings/tags", insertNext({ elId: "follows" }, buildTagsHTML));
|
||||
fetchNext(el, "/followings/tags", insertNext({ elId: "follows" }, buildTagsHTML));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
module Api
|
||||
module V0
|
||||
class FollowingsController < ApiController
|
||||
before_action :authenticate_user!
|
||||
before_action -> { limit_per_page(default: 80, max: 1000) }
|
||||
|
||||
def users
|
||||
relation = current_user.follows_by_type("User").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@follows = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def tags
|
||||
relation = current_user.follows_by_type("ActsAsTaggableOn::Tag").
|
||||
select(TAGS_ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(points: :desc)
|
||||
@followed_tags = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def organizations
|
||||
relation = current_user.follows_by_type("Organization").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@followed_organizations = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def podcasts
|
||||
relation = current_user.follows_by_type("Podcast").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@followed_podcasts = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
ATTRIBUTES_FOR_SERIALIZATION = %i[id followable_id followable_type].freeze
|
||||
private_constant :ATTRIBUTES_FOR_SERIALIZATION
|
||||
|
||||
TAGS_ATTRIBUTES_FOR_SERIALIZATION = [*ATTRIBUTES_FOR_SERIALIZATION, :points].freeze
|
||||
private_constant :TAGS_ATTRIBUTES_FOR_SERIALIZATION
|
||||
|
||||
private
|
||||
|
||||
def limit_per_page(default:, max:)
|
||||
per_page = (params[:per_page] || default).to_i
|
||||
@follows_limit = [per_page, max].min
|
||||
end
|
||||
|
||||
def load_follows_and_paginate(relation)
|
||||
relation.includes(:followable).page(params[:page]).per(@follows_limit)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
49
app/controllers/followings_controller.rb
Normal file
49
app/controllers/followings_controller.rb
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
class FollowingsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
before_action -> { limit_per_page(default: 80, max: 1000) }
|
||||
|
||||
def users
|
||||
relation = current_user.follows_by_type("User").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@follows = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def tags
|
||||
relation = current_user.follows_by_type("ActsAsTaggableOn::Tag").
|
||||
select(TAGS_ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(points: :desc)
|
||||
@followed_tags = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def organizations
|
||||
relation = current_user.follows_by_type("Organization").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@followed_organizations = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
def podcasts
|
||||
relation = current_user.follows_by_type("Podcast").
|
||||
select(ATTRIBUTES_FOR_SERIALIZATION).
|
||||
order(created_at: :desc)
|
||||
@followed_podcasts = load_follows_and_paginate(relation)
|
||||
end
|
||||
|
||||
ATTRIBUTES_FOR_SERIALIZATION = %i[id followable_id followable_type].freeze
|
||||
private_constant :ATTRIBUTES_FOR_SERIALIZATION
|
||||
|
||||
TAGS_ATTRIBUTES_FOR_SERIALIZATION = [*ATTRIBUTES_FOR_SERIALIZATION, :points].freeze
|
||||
private_constant :TAGS_ATTRIBUTES_FOR_SERIALIZATION
|
||||
|
||||
private
|
||||
|
||||
def limit_per_page(default:, max:)
|
||||
per_page = (params[:per_page] || default).to_i
|
||||
@follows_limit = [per_page, max].min
|
||||
end
|
||||
|
||||
def load_follows_and_paginate(relation)
|
||||
relation.includes(:followable).page(params[:page]).per(@follows_limit)
|
||||
end
|
||||
end
|
||||
|
|
@ -133,12 +133,6 @@ Rails.application.routes.draw do
|
|||
get :users
|
||||
get :organizations
|
||||
end
|
||||
namespace :followings do
|
||||
get :users
|
||||
get :tags
|
||||
get :organizations
|
||||
get :podcasts
|
||||
end
|
||||
resources :webhooks, only: %i[index create show destroy]
|
||||
|
||||
resources :classified_listings, path: :listings, only: %i[index show create update]
|
||||
|
|
@ -219,6 +213,12 @@ Rails.application.routes.draw do
|
|||
resources :user_blocks, param: :blocked_id, only: %i[show create destroy]
|
||||
resources :podcasts, only: %i[new create]
|
||||
resolve("ProMembership") { [:pro_membership] } # see https://guides.rubyonrails.org/routing.html#using-resolve
|
||||
namespace :followings, defaults: { format: :json } do
|
||||
get :users
|
||||
get :tags
|
||||
get :organizations
|
||||
get :podcasts
|
||||
end
|
||||
|
||||
get "/search/tags" => "search#tags"
|
||||
get "/search/chat_channels" => "search#chat_channels"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "Api::V0::FollowingsController", type: :request do
|
||||
RSpec.describe "FollowingsController", type: :request do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
describe "GET /api/followings/users" do
|
||||
describe "GET /followings/users" do
|
||||
let(:followed) { create(:user) }
|
||||
|
||||
before do
|
||||
|
|
@ -14,7 +14,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
|
||||
context "when user is unauthorized" do
|
||||
it "returns unauthorized" do
|
||||
get api_followings_users_path
|
||||
get followings_users_path
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
|
@ -26,7 +26,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
|
||||
it "returns user's followings list with the correct format" do
|
||||
get api_followings_users_path
|
||||
get followings_users_path
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
response_following = response.parsed_body.first
|
||||
|
|
@ -40,7 +40,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
describe "GET /api/followings/tags" do
|
||||
describe "GET /followings/tags" do
|
||||
let(:followed) { create(:tag) }
|
||||
|
||||
before do
|
||||
|
|
@ -51,7 +51,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
|
||||
context "when user is unauthorized" do
|
||||
it "returns unauthorized" do
|
||||
get api_followings_tags_path
|
||||
get followings_tags_path
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
|
@ -63,7 +63,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
|
||||
it "returns user's followings list with the correct format" do
|
||||
get api_followings_tags_path
|
||||
get followings_tags_path
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
follow = user.follows.last
|
||||
|
|
@ -79,7 +79,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
describe "GET /api/followings/organizations" do
|
||||
describe "GET /followings/organizations" do
|
||||
let(:followed) { create(:organization) }
|
||||
|
||||
before do
|
||||
|
|
@ -90,7 +90,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
|
||||
context "when user is unauthorized" do
|
||||
it "returns unauthorized" do
|
||||
get api_followings_organizations_path
|
||||
get followings_organizations_path
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
|
@ -102,7 +102,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
|
||||
it "returns user's followings list with the correct format" do
|
||||
get api_followings_organizations_path
|
||||
get followings_organizations_path
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
response_following = response.parsed_body.first
|
||||
|
|
@ -116,7 +116,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
describe "GET /api/followings/podcasts" do
|
||||
describe "GET /followings/podcasts" do
|
||||
let(:followed) { create(:podcast) }
|
||||
|
||||
before do
|
||||
|
|
@ -127,7 +127,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
|
||||
context "when user is unauthorized" do
|
||||
it "returns unauthorized" do
|
||||
get api_followings_podcasts_path
|
||||
get followings_podcasts_path
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
|
@ -139,7 +139,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do
|
|||
end
|
||||
|
||||
it "returns user's followings list with the correct format" do
|
||||
get api_followings_podcasts_path
|
||||
get followings_podcasts_path
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
response_following = response.parsed_body.first
|
||||
Loading…
Add table
Reference in a new issue