Updated account, groups relationships routes to post data instead of get

• Updated:
- account, groups relationships routes to post data instead of get
This commit is contained in:
mgabdev 2021-01-13 23:51:44 -05:00
parent 1ed29ddd53
commit fb75f33b12
5 changed files with 24 additions and 14 deletions

View File

@ -4,21 +4,22 @@ class Api::V1::Accounts::RelationshipsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:follows' } before_action -> { doorkeeper_authorize! :read, :'read:follows' }
before_action :require_user! before_action :require_user!
def index def relationships
accounts = Account.where(id: account_ids).select('id') accounts = Account.where(id: account_ids).select('id')
# .where doesn't guarantee that our results are in the same order # .where doesn't guarantee that our results are in the same order
# we requested them, so return the "right" order to the requestor. # we requested them, so return the "right" order to the requestor.
@accounts = accounts.index_by(&:id).values_at(*account_ids).compact @accounts = accounts.index_by(&:id).values_at(*account_ids).compact
render json: @accounts, each_serializer: REST::RelationshipSerializer, relationships: relationships render json: @accounts, each_serializer: REST::RelationshipSerializer, relationships: get_relationships
end end
private private
def relationships def get_relationships
AccountRelationshipsPresenter.new(@accounts, current_user.account_id) AccountRelationshipsPresenter.new(@accounts, current_user.account_id)
end end
def account_ids def account_ids
Array(params[:id]).map(&:to_i) the_take = current_user.staff? ? 100 : 25
params[:accountIds].map(&:to_i).take(the_take)
end end
end end

View File

@ -1,24 +1,25 @@
# frozen_string_literal: true # frozen_string_literal: true
class Api::V1::Groups::RelationshipsController < Api::BaseController class Api::V1::GroupRelationshipsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:groups' } before_action -> { doorkeeper_authorize! :read, :'read:groups' }
before_action :require_user! before_action :require_user!
def index def relationships
groups = Group.where(id: group_ids, is_archived: false).select('id') groups = Group.where(id: group_ids, is_archived: false).select('id')
# .where doesn't guarantee that our results are in the same order # .where doesn't guarantee that our results are in the same order
# we requested them, so return the "right" order to the requestor. # we requested them, so return the "right" order to the requestor.
@groups = groups.index_by(&:id).values_at(*group_ids).compact @groups = groups.index_by(&:id).values_at(*group_ids).compact
render json: @groups, each_serializer: REST::GroupRelationshipSerializer, relationships: relationships render json: @groups, each_serializer: REST::GroupRelationshipSerializer, relationships: get_relationships
end end
private private
def relationships def get_relationships
GroupRelationshipsPresenter.new(@groups, current_user.account_id) GroupRelationshipsPresenter.new(@groups, current_user.account_id)
end end
def group_ids def group_ids
Array(params[:id]).map(&:to_i) the_take = current_user.staff? ? 100 : 25
params[:groupIds].map(&:to_i).take(the_take)
end end
end end

View File

@ -542,7 +542,9 @@ export const fetchRelationships = (accountIds) => (dispatch, getState) => {
dispatch(fetchRelationshipsRequest(newAccountIds)) dispatch(fetchRelationshipsRequest(newAccountIds))
api(getState).get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`).then((response) => { api(getState).post('/api/v1/accounts/relationships', {
accountIds: newAccountIds,
}).then((response) => {
dispatch(fetchRelationshipsSuccess(response.data)) dispatch(fetchRelationshipsSuccess(response.data))
}).catch((error) => { }).catch((error) => {
dispatch(fetchRelationshipsFail(error)) dispatch(fetchRelationshipsFail(error))

View File

@ -182,13 +182,18 @@ export const fetchGroupRelationships = (groupIds) => (dispatch, getState) => {
if (!me || !Array.isArray(groupIds)) return if (!me || !Array.isArray(groupIds)) return
const loadedRelationships = getState().get('group_relationships') const loadedRelationships = getState().get('group_relationships')
const newGroupIds = groupIds.filter((id) => loadedRelationships.get(id, null) === null) let newGroupIds = groupIds.filter((id) => loadedRelationships.get(id, null) === null)
if (newGroupIds.length === 0) return if (newGroupIds.length === 0) return
// Unique
newGroupIds = Array.from(new Set(newGroupIds))
dispatch(fetchGroupRelationshipsRequest(newGroupIds)) dispatch(fetchGroupRelationshipsRequest(newGroupIds))
api(getState).get(`/api/v1/groups/${newGroupIds[0]}/relationships?${newGroupIds.map(id => `id[]=${id}`).join('&')}`).then((response) => { api(getState).post('/api/v1/group_relationships', {
groupIds: newGroupIds,
}).then((response) => {
dispatch(fetchGroupRelationshipsSuccess(response.data)) dispatch(fetchGroupRelationshipsSuccess(response.data))
}).catch((error) => { }).catch((error) => {
dispatch(fetchGroupRelationshipsFail(error)) dispatch(fetchGroupRelationshipsFail(error))

View File

@ -320,7 +320,7 @@ Rails.application.routes.draw do
patch :update_credentials, to: 'credentials#update' patch :update_credentials, to: 'credentials#update'
post :resend_email_confirmation, to: 'credentials#resend_email_confirmation' post :resend_email_confirmation, to: 'credentials#resend_email_confirmation'
resource :search, only: :show, controller: :search resource :search, only: :show, controller: :search
resources :relationships, only: :index post :relationships, to: 'relationships#relationships'
end end
resources :accounts, only: [:create, :show] do resources :accounts, only: [:create, :show] do
@ -355,7 +355,6 @@ Rails.application.routes.draw do
get '/category/:category', to: 'groups#by_category' get '/category/:category', to: 'groups#by_category'
get '/tag/:tag', to: 'groups#by_tag' get '/tag/:tag', to: 'groups#by_tag'
resources :relationships, only: :index, controller: 'groups/relationships'
resource :accounts, only: [:show, :create, :update, :destroy], controller: 'groups/accounts' resource :accounts, only: [:show, :create, :update, :destroy], controller: 'groups/accounts'
resource :removed_accounts, only: [:show, :create, :destroy], controller: 'groups/removed_accounts' resource :removed_accounts, only: [:show, :create, :destroy], controller: 'groups/removed_accounts'
resource :password, only: [:create], controller: 'groups/password' resource :password, only: [:create], controller: 'groups/password'
@ -367,6 +366,8 @@ Rails.application.routes.draw do
post :unpin, to: 'groups/pins#destroy' post :unpin, to: 'groups/pins#destroy'
end end
post :group_relationships, to: 'group_relationships#relationships'
resources :polls, only: [:create, :show] do resources :polls, only: [:create, :show] do
resources :votes, only: :create, controller: 'polls/votes' resources :votes, only: :create, controller: 'polls/votes'
end end