<commit_before>import { takeEvery, takeLatest, take, put, select, call, fork} from 'redux-saga/effects'
import { delay } from 'redux-saga'
import {push} from 'react-router-redux'
import R from 'ramda'
import merge from 'lodash/merge'
import {apiSaga, redirectFromOrgIndexIfNeeded} from './helpers'
import pluralize from 'pluralize'
import {decamelize} from 'xcase'
import {
  SELECTED_OBJECT,

  FETCH_OBJECT_DETAILS_REQUEST,
  FETCH_OBJECT_DETAILS_API_SUCCESS,
  FETCH_OBJECT_DETAILS_SUCCESS,
  FETCH_OBJECT_DETAILS_FAILED,

  CREATE_OBJECT_REQUEST,
  CREATE_OBJECT_SUCCESS,
  CREATE_OBJECT_FAILED,

  UPDATE_OBJECT_SETTINGS_REQUEST,
  UPDATE_OBJECT_SETTINGS_SUCCESS,
  UPDATE_OBJECT_SETTINGS_FAILED,

  RENAME_OBJECT_REQUEST,
  RENAME_OBJECT_SUCCESS,
  RENAME_OBJECT_FAILED,

  REMOVE_OBJECT_REQUEST,
  REMOVE_OBJECT_SUCCESS,
  REMOVE_OBJECT_FAILED,

  API_SUCCESS,
  API_FAILED,

  CHECK_INVITES_ACCEPTED_REQUEST,

  SOCKET_UNSUBSCRIBE_OBJECT_CHANNEL,

  VERIFY_ORG_PUBKEYS_SUCCESS,

  socketSubscribeObjectChannel,
  generateEnvUpdateId,
  importAllEnvironments,
  decryptEnvs,
  verifyOrgPubkeys,
  resetSession
} from "actions"
import {
  getCurrentOrg,
  getCurrentUser,
  getIsPollingInviteesPendingAcceptance,
  getEnvUpdateId
} from "selectors"
import {
  decryptEnvParent
} from './helpers'

const
  getUpdateUrlFn = (path)=> ({meta: {objectType, targetId}}) => {
    return `/${pluralize(decamelize(objectType))}/${targetId}${path ? ('/' + path): ''}.json`
  },

  onCreateObject = apiSaga({
    authenticated: true,
    method: "post",
    actionTypes: [CREATE_OBJECT_SUCCESS, CREATE_OBJECT_FAILED],
    urlFn: ({meta: {objectType}}) => `/${pluralize(objectType)}.json`
  }),

  onUpdateObjectSettings = apiSaga({
    authenticated: true,
    method: "patch",
    actionTypes: [UPDATE_OBJECT_SETTINGS_SUCCESS, UPDATE_OBJECT_SETTINGS_FAILED],
    urlFn: getUpdateUrlFn("update_settings")
  }),

  onRenameObject = apiSaga({
    authenticated: true,
    method: "patch",
    actionTypes: [RENAME_OBJECT_SUCCESS, RENAME_OBJECT_FAILED],
    urlFn: getUpdateUrlFn("rename")
  }),

  onFetchObjectDetails = action => apiSaga({
    authenticated: true,
    method: "get",
    minDelay: (action.meta && action.meta.socketUpdate ? 2000 : 0),
    actionTypes: [FETCH_OBJECT_DETAILS_API_SUCCESS, FETCH_OBJECT_DETAILS_FAILED],
    urlFn: ({meta: {objectType, targetId}})=> `/${pluralize(objectType)}/${targetId}.json`
  })(action),

  onSelectedObject = function*({payload: object}){
    yield put({type: SOCKET_UNSUBSCRIBE_OBJECT_CHANNEL})
    const currentOrg = yield select(getCurrentOrg)

    if (object && object.objectType == "app" && object.broadcastChannel && object.id != currentOrg.id){
      yield put(socketSubscribeObjectChannel(object))
    }

    if (object.objectType == "app"){
      const envUpdateId = yield select(getEnvUpdateId(object.id))
      if (!envUpdateId){
        yield put(generateEnvUpdateId({parentId: object.id, parentType: object.objectType}))
      }
    }
  },

  onRemoveObject = function*(action){
    const currentOrg = yield select(getCurrentOrg),
          currentUser = yield select(getCurrentUser),
          shouldClearSession = ((action.meta.objectType == "user" && action.meta.targetId == currentUser.id) ||
                          (action.meta.objectType == "org" && action.meta.targetId == currentOrg.id))

    yield fork(apiSaga({
      authenticated: true,
      method: "delete",
      actionTypes: [REMOVE_OBJECT_SUCCESS, REMOVE_OBJECT_FAILED],
      urlFn:  getUpdateUrlFn()
    }), action)

    if(!action.meta.isOnboardAction){
      const {type: apiResultType} = yield take([API_SUCCESS, API_FAILED])
      if (apiResultType == API_SUCCESS) {
        // If user just deleted their account or organization, log them out and return
        if (shouldClearSession){
          yield put(push("/home"))
          yield put(resetSession())
        } else {
          yield put(push(`/${currentOrg.slug}`))
          yield call(redirectFromOrgIndexIfNeeded)
        }
      }
    }
  },

  onFetchObjectDetailsApiSuccess = function*(action){
    if (action.meta.decryptEnvs){
      yield put(verifyOrgPubkeys())
      yield take(VERIFY_ORG_PUBKEYS_SUCCESS)
      const decrypted = yield call(decryptEnvParent, action.payload)
      yield put({...action, type: FETCH_OBJECT_DETAILS_SUCCESS, payload: decrypted})
    } else {
      yield put({...action, type: FETCH_OBJECT_DETAILS_SUCCESS})
    }
  },

  onCreateObjectSuccess = function*({
    meta: {status, createAssoc, objectType, isOnboardAction, willImport, toImport},
    payload
  }){
    const {id, slug} = payload

    if (toImport){
      yield put(importAllEnvironments({<commit_msg>Don't redirect on invite revocation<commit_after>import { takeEvery, takeLatest, take, put, select, call, fork} from 'redux-saga/effects'
import { delay } from 'redux-saga'
import {push} from 'react-router-redux'
import R from 'ramda'
import merge from 'lodash/merge'
import {apiSaga, redirectFromOrgIndexIfNeeded} from './helpers'
import pluralize from 'pluralize'
import {decamelize} from 'xcase'
import {
  SELECTED_OBJECT,

  FETCH_OBJECT_DETAILS_REQUEST,
  FETCH_OBJECT_DETAILS_API_SUCCESS,
  FETCH_OBJECT_DETAILS_SUCCESS,
  FETCH_OBJECT_DETAILS_FAILED,

  CREATE_OBJECT_REQUEST,
  CREATE_OBJECT_SUCCESS,
  CREATE_OBJECT_FAILED,

  UPDATE_OBJECT_SETTINGS_REQUEST,
  UPDATE_OBJECT_SETTINGS_SUCCESS,
  UPDATE_OBJECT_SETTINGS_FAILED,

  RENAME_OBJECT_REQUEST,
  RENAME_OBJECT_SUCCESS,
  RENAME_OBJECT_FAILED,

  REMOVE_OBJECT_REQUEST,
  REMOVE_OBJECT_SUCCESS,
  REMOVE_OBJECT_FAILED,

  API_SUCCESS,
  API_FAILED,

  CHECK_INVITES_ACCEPTED_REQUEST,

  SOCKET_UNSUBSCRIBE_OBJECT_CHANNEL,

  VERIFY_ORG_PUBKEYS_SUCCESS,

  socketSubscribeObjectChannel,
  generateEnvUpdateId,
  importAllEnvironments,
  decryptEnvs,
  verifyOrgPubkeys,
  resetSession
} from "actions"
import {
  getCurrentOrg,
  getCurrentUser,
  getIsPollingInviteesPendingAcceptance,
  getEnvUpdateId
} from "selectors"
import {
  decryptEnvParent
} from './helpers'

const
  getUpdateUrlFn = (path)=> ({meta: {objectType, targetId}}) => {
    return `/${pluralize(decamelize(objectType))}/${targetId}${path ? ('/' + path): ''}.json`
  },

  onCreateObject = apiSaga({
    authenticated: true,
    method: "post",
    actionTypes: [CREATE_OBJECT_SUCCESS, CREATE_OBJECT_FAILED],
    urlFn: ({meta: {objectType}}) => `/${pluralize(objectType)}.json`
  }),

  onUpdateObjectSettings = apiSaga({
    authenticated: true,
    method: "patch",
    actionTypes: [UPDATE_OBJECT_SETTINGS_SUCCESS, UPDATE_OBJECT_SETTINGS_FAILED],
    urlFn: getUpdateUrlFn("update_settings")
  }),

  onRenameObject = apiSaga({
    authenticated: true,
    method: "patch",
    actionTypes: [RENAME_OBJECT_SUCCESS, RENAME_OBJECT_FAILED],
    urlFn: getUpdateUrlFn("rename")
  }),

  onFetchObjectDetails = action => apiSaga({
    authenticated: true,
    method: "get",
    minDelay: (action.meta && action.meta.socketUpdate ? 2000 : 0),
    actionTypes: [FETCH_OBJECT_DETAILS_API_SUCCESS, FETCH_OBJECT_DETAILS_FAILED],
    urlFn: ({meta: {objectType, targetId}})=> `/${pluralize(objectType)}/${targetId}.json`
  })(action),

  onSelectedObject = function*({payload: object}){
    yield put({type: SOCKET_UNSUBSCRIBE_OBJECT_CHANNEL})
    const currentOrg = yield select(getCurrentOrg)

    if (object && object.objectType == "app" && object.broadcastChannel && object.id != currentOrg.id){
      yield put(socketSubscribeObjectChannel(object))
    }

    if (object.objectType == "app"){
      const envUpdateId = yield select(getEnvUpdateId(object.id))
      if (!envUpdateId){
        yield put(generateEnvUpdateId({parentId: object.id, parentType: object.objectType}))
      }
    }
  },

  onRemoveObject = function*(action){
    const currentOrg = yield select(getCurrentOrg),
          currentUser = yield select(getCurrentUser),
          shouldClearSession = ((action.meta.objectType == "user" && action.meta.targetId == currentUser.id) ||
                          (action.meta.objectType == "org" && action.meta.targetId == currentOrg.id))

    yield fork(apiSaga({
      authenticated: true,
      method: "delete",
      actionTypes: [REMOVE_OBJECT_SUCCESS, REMOVE_OBJECT_FAILED],
      urlFn:  getUpdateUrlFn()
    }), action)

    if(!action.meta.isOnboardAction){
      const {type: apiResultType} = yield take([API_SUCCESS, API_FAILED])
      if (apiResultType == API_SUCCESS) {
        // If user just deleted their account or organization, log them out and return
        if (shouldClearSession){
          yield put(push("/home"))
          yield put(resetSession())
        } else if (!action.meta.noRedirect){
          yield put(push(`/${currentOrg.slug}`))
          yield call(redirectFromOrgIndexIfNeeded)
        }
      }
    }
  },

  onFetchObjectDetailsApiSuccess = function*(action){
    if (action.meta.decryptEnvs){
      yield put(verifyOrgPubkeys())
      yield take(VERIFY_ORG_PUBKEYS_SUCCESS)
      const decrypted = yield call(decryptEnvParent, action.payload)
      yield put({...action, type: FETCH_OBJECT_DETAILS_SUCCESS, payload: decrypted})
    } else {
      yield put({...action, type: FETCH_OBJECT_DETAILS_SUCCESS})
    }
  },

  onCreateObjectSuccess = function*({
    meta: {status, createAssoc, objectType, isOnboardAction, willImport, toImport},
    payload
  }){
    const {id, slug} = payload

    if (toImport){
      yield put(importAllEnvironments({
{'fraction_non_alphanumeric': 0.1152298241897903, 'fraction_numerical': 0.0010590976488032196, 'mean_word_length': 4.154475982532751, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4026556', 'n_tokens_mistral': 3160, 'n_tokens_neox': 3043, 'n_words': 577}