diff --git a/app/controllers/api/v1/groups/accounts_controller.rb b/app/controllers/api/v1/groups/accounts_controller.rb index d0a89f5e..9836c200 100644 --- a/app/controllers/api/v1/groups/accounts_controller.rb +++ b/app/controllers/api/v1/groups/accounts_controller.rb @@ -37,12 +37,15 @@ class Api::V1::Groups::AccountsController < Api::BaseController end def destroy - authorize @group, :leave? - - GroupAccount.where(group: @group, account_id: current_account.id).destroy_all - - if current_user.allows_group_in_home_feed? - current_user.force_regeneration! + @join_request = GroupJoinRequest.where(group: @group, account_id: current_account.id) + if @join_request.count > 0 + @join_request.destroy_all + else + authorize @group, :leave? + GroupAccount.where(group: @group, account_id: current_account.id).destroy_all + if current_user.allows_group_in_home_feed? + current_user.force_regeneration! + end end render json: @group, serializer: REST::GroupRelationshipSerializer, relationships: relationships diff --git a/app/controllers/api/v1/groups/requests_controller.rb b/app/controllers/api/v1/groups/requests_controller.rb index ab01e85d..39518444 100644 --- a/app/controllers/api/v1/groups/requests_controller.rb +++ b/app/controllers/api/v1/groups/requests_controller.rb @@ -16,29 +16,21 @@ class Api::V1::Groups::RequestsController < Api::BaseController render json: @accounts, each_serializer: REST::AccountSerializer end - def create - authorize @group, :leave? - GroupJoinRequest.where(group: @group, account_id: current_account.id).destroy_all - render json: @group, serializer: REST::GroupRelationshipSerializer, relationships: relationships - end - - def approve_request - GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all - GroupAccount.create(group: @group, account_id: params[:accountId]) - render json: {"message": "ok"} - end - - def reject_request - GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all - render json: {"message": "ok"} + def respond_to_request + if params[:type] === 'reject' + GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all + render json: { message: "ok", type: 'reject', accountId: params[:accountId] } + elsif params[:type] === 'approve' + GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all + GroupAccount.create(group: @group, account_id: params[:accountId]) + render json: { message: "ok", type: 'approve', accountId: params[:accountId] } + else + render json: { message: "error", error: true }, status: 422 + end end private - def relationships - GroupRelationshipsPresenter.new([@group.id], current_user.account_id) - end - def set_group @group = Group.find(params[:group_id]) end diff --git a/app/javascript/gabsocial/actions/groups.js b/app/javascript/gabsocial/actions/groups.js index cb9c7508..a99111a7 100644 --- a/app/javascript/gabsocial/actions/groups.js +++ b/app/javascript/gabsocial/actions/groups.js @@ -702,8 +702,8 @@ export function expandJoinRequestsFail(id, error) { export const approveJoinRequest = (accountId, groupId) => (dispatch, getState) => { if (!me) return - api(getState).post(`/api/v1/groups/${groupId}/join_requests/approve`, { accountId }).then((response) => { - dispatch(approveJoinRequestSuccess(accountId, groupId)) + api(getState).post(`/api/v1/groups/${groupId}/join_requests/respond`, { accountId, type: 'approve' }).then((response) => { + dispatch(approveJoinRequestSuccess(response.data.accountId, groupId)) }).catch((error) => { dispatch(approveJoinRequestFail(accountId, groupId, error)) }) @@ -729,8 +729,9 @@ export function approveJoinRequestFail(accountId, groupId, error) { export const rejectJoinRequest = (accountId, groupId) => (dispatch, getState) => { if (!me) return - api(getState).delete(`/api/v1/groups/${groupId}/join_requests/reject`, { accountId }).then((response) => { - dispatch(rejectJoinRequestSuccess(accountId, groupId)) + api(getState).post(`/api/v1/groups/${groupId}/join_requests/respond`, { accountId, type: 'reject' }).then((response) => { + console.log("response:", response) + dispatch(rejectJoinRequestSuccess(response.data.accountId, groupId)) }).catch((error) => { dispatch(rejectJoinRequestFail(accountId, groupId, error)) }) diff --git a/app/javascript/gabsocial/reducers/user_lists.js b/app/javascript/gabsocial/reducers/user_lists.js index 76a718d3..7ffb596e 100644 --- a/app/javascript/gabsocial/reducers/user_lists.js +++ b/app/javascript/gabsocial/reducers/user_lists.js @@ -179,8 +179,7 @@ export default function userLists(state = initialState, action) { return appendToList(state, 'group_join_requests', action.id, action.accounts, action.next); case GROUP_JOIN_REQUESTS_APPROVE_SUCCESS: case GROUP_JOIN_REQUESTS_REJECT_SUCCESS: - return state.updateIn(['group_join_requests', action.groupId, 'items'], list => list.filterNot(item => item === action.id)); - + return state.updateIn(['group_join_requests', action.groupId, 'items'], list => list.filterNot(item => item === action.accountId)); default: return state; } diff --git a/config/routes.rb b/config/routes.rb index b3ad7628..438e0cb6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -440,10 +440,9 @@ Rails.application.routes.draw do resources :relationships, only: :index, controller: 'groups/relationships' resource :accounts, only: [:show, :create, :update, :destroy], controller: 'groups/accounts' resource :removed_accounts, only: [:show, :create, :destroy], controller: 'groups/removed_accounts' - resource :join_requests, only: [:show, :create], controller: 'groups/requests' + resource :join_requests, only: [:show], controller: 'groups/requests' - post '/join_requests/approve', to: 'groups/requests#approve_request' - delete '/join_requests/reject', to: 'groups/requests#reject_request' + post '/join_requests/respond', to: 'groups/requests#respond_to_request' resource :pin, only: :create, controller: 'groups/pins' post :unpin, to: 'groups/pins#destroy'