Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

refactor(client-core)(bot service): remove action receptor usage #8321

Merged
merged 8 commits into from
Jul 20, 2023
86 changes: 18 additions & 68 deletions packages/client-core/src/admin/services/BotsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ import { Paginated } from '@feathersjs/feathers'

import { AdminBot, CreateBotAsAdmin } from '@etherealengine/common/src/interfaces/AdminBot'
import multiLogger from '@etherealengine/common/src/logger'
import { matches, Validator } from '@etherealengine/engine/src/common/functions/MatchesUtils'
import { defineAction, defineState, dispatchAction, getMutableState } from '@etherealengine/hyperflux'
import { Engine } from '@etherealengine/engine/src/ecs/classes/Engine'
import { defineState, getMutableState } from '@etherealengine/hyperflux'

import { API } from '../../API'
import { AuthState } from '../../user/services/AuthService'
import { userIsAdmin } from '../../user/userHasAccess'

const logger = multiLogger.child({ component: 'client-core:BotsService' })

//State
export const BOTS_PAGE_LIMIT = 100

export const AdminBotState = defineState({
Expand All @@ -52,56 +50,21 @@ export const AdminBotState = defineState({
})
})

const fetchedBotReceptor = (action: typeof AdminBotsActions.fetchedBot.matches._TYPE) => {
const state = getMutableState(AdminBotState)
return state.merge({
bots: action.bots.data,
retrieving: false,
fetched: true,
updateNeeded: false,
lastFetched: Date.now()
})
}

const botCreatedReceptor = (action: typeof AdminBotsActions.botCreated.matches._TYPE) => {
const state = getMutableState(AdminBotState)
return state.merge({ updateNeeded: true })
}

const botPatchedReceptor = (action: typeof AdminBotsActions.botPatched.matches._TYPE) => {
const state = getMutableState(AdminBotState)
return state.merge({ updateNeeded: true })
}

const botRemovedReceptor = (action: typeof AdminBotsActions.botRemoved.matches._TYPE) => {
const state = getMutableState(AdminBotState)
return state.merge({ updateNeeded: true })
}

export const AdminBotServiceReceptors = {
fetchedBotReceptor,
botCreatedReceptor,
botPatchedReceptor,
botRemovedReceptor
}

//Service
export const AdminBotService = {
createBotAsAdmin: async (data: CreateBotAsAdmin) => {
try {
const bot = await API.instance.client.service('bot').create(data)
dispatchAction(AdminBotsActions.botCreated({ bot }))
await Engine.instance.api.service('bot').create(data)
getMutableState(AdminBotState).merge({ updateNeeded: true })
} catch (error) {
logger.error(error)
}
},
fetchBotAsAdmin: async (incDec?: 'increment' | 'decrement') => {
try {
const user = getMutableState(AuthState).user
const skip = getMutableState(AdminBotState).skip.value
const limit = getMutableState(AdminBotState).limit.value
if (user.scopes?.value?.find((scope) => scope.type === 'admin:admin')) {
const bots = (await API.instance.client.service('bot').find({
if (userIsAdmin()) {
const bots = (await Engine.instance.api.service('bot').find({
query: {
$sort: {
name: 1
Expand All @@ -111,45 +74,32 @@ export const AdminBotService = {
action: 'admin'
}
})) as Paginated<AdminBot>
dispatchAction(AdminBotsActions.fetchedBot({ bots }))
getMutableState(AdminBotState).merge({
bots: bots.data,
retrieving: false,
fetched: true,
updateNeeded: false,
lastFetched: Date.now()
})
}
} catch (error) {
logger.error(error)
}
},
removeBots: async (id: string) => {
try {
const bot = (await API.instance.client.service('bot').remove(id)) as AdminBot
dispatchAction(AdminBotsActions.botRemoved({ bot }))
await Engine.instance.api.service('bot').remove(id)
getMutableState(AdminBotState).merge({ updateNeeded: true })
} catch (error) {
logger.error(error)
}
},
updateBotAsAdmin: async (id: string, bot: CreateBotAsAdmin) => {
try {
const result = (await API.instance.client.service('bot').patch(id, bot)) as AdminBot
dispatchAction(AdminBotsActions.botPatched({ bot: result }))
await Engine.instance.api.service('bot').patch(id, bot)
getMutableState(AdminBotState).merge({ updateNeeded: true })
} catch (error) {
logger.error(error)
}
}
}
//Action
export class AdminBotsActions {
static fetchedBot = defineAction({
type: 'ee.client.AdminBots.BOT_ADMIN_DISPLAY' as const,
bots: matches.object as Validator<unknown, Paginated<AdminBot>>
})
static botCreated = defineAction({
type: 'ee.client.AdminBots.BOT_ADMIN_CREATE' as const,
bot: matches.object as Validator<unknown, AdminBot>
})
static botRemoved = defineAction({
type: 'ee.client.AdminBots.BOT_ADMIN_REMOVE' as const,
bot: matches.object as Validator<unknown, AdminBot>
})
static botPatched = defineAction({
type: 'ee.client.AdminBots.BOT_ADMIN_UPDATE' as const,
bot: matches.object as Validator<unknown, AdminBot>
})
}
23 changes: 1 addition & 22 deletions packages/client-core/src/systems/AdminSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ Ethereal Engine. All Rights Reserved.
*/

import { defineSystem } from '@etherealengine/engine/src/ecs/functions/SystemFunctions'
import { defineAction, defineActionQueue } from '@etherealengine/hyperflux'
import { defineActionQueue } from '@etherealengine/hyperflux'

import { AdminActiveRouteActions, AdminActiveRouteReceptors } from '../admin/services/ActiveRouteService'
import { AdminBotsActions, AdminBotServiceReceptors } from '../admin/services/BotsService'
import { AdminBuildStatusActions, AdminBuildStatusReceptors } from '../admin/services/BuildStatusService'
import { AdminGroupActions, AdminGroupServiceReceptors } from '../admin/services/GroupService'
import { AdminLocationActions, AdminLocationReceptors } from '../admin/services/LocationService'
import { AdminPartyActions, AdminPartyReceptors } from '../admin/services/PartyService'
import { AdminResourceActions, AdminResourceReceptors } from '../admin/services/ResourceService'
Expand Down Expand Up @@ -89,15 +86,6 @@ const partyRetrievedQueue = defineActionQueue(AdminPartyActions.partyRetrieved.m
const partyAdminCreatedQueue = defineActionQueue(AdminPartyActions.partyAdminCreated.matches)
const partyRemovedQueue = defineActionQueue(AdminPartyActions.partyRemoved.matches)
const partyPatchedQueue = defineActionQueue(AdminPartyActions.partyPatched.matches)
const fetchedBotQueue = defineActionQueue(AdminBotsActions.fetchedBot.matches)
const botCreatedQueue = defineActionQueue(AdminBotsActions.botCreated.matches)
const botPatchedQueue = defineActionQueue(AdminBotsActions.botPatched.matches)
const botRemovedQueue = defineActionQueue(AdminBotsActions.botRemoved.matches)
const fetchingGroupQueue = defineActionQueue(AdminGroupActions.fetchingGroup.matches)
const setAdminGroupQueue = defineActionQueue(AdminGroupActions.setAdminGroup.matches)
const updateGroupQueue = defineActionQueue(AdminGroupActions.updateGroup.matches)
const removeGroupActionQueue = defineActionQueue(AdminGroupActions.removeGroupAction.matches)
const addAdminGroupQueue = defineActionQueue(AdminGroupActions.addAdminGroup.matches)
const activeRoutesRetrievedQueue = defineActionQueue(AdminActiveRouteActions.activeRoutesRetrieved.matches)
const fetchedInstanceServerQueue = defineActionQueue(InstanceServerSettingActions.fetchedInstanceServer.matches)
const chargebeeSettingRetrievedQueue = defineActionQueue(AdminChargebeeSettingActions.chargebeeSettingRetrieved.matches)
Expand Down Expand Up @@ -142,15 +130,6 @@ const execute = () => {
for (const action of partyAdminCreatedQueue()) AdminPartyReceptors.partyAdminCreatedReceptor(action)
for (const action of partyRemovedQueue()) AdminPartyReceptors.partyRemovedReceptor(action)
for (const action of partyPatchedQueue()) AdminPartyReceptors.partyPatchedReceptor(action)
for (const action of fetchedBotQueue()) AdminBotServiceReceptors.fetchedBotReceptor(action)
for (const action of botCreatedQueue()) AdminBotServiceReceptors.botCreatedReceptor(action)
for (const action of botPatchedQueue()) AdminBotServiceReceptors.botPatchedReceptor(action)
for (const action of botRemovedQueue()) AdminBotServiceReceptors.botRemovedReceptor(action)
for (const action of fetchingGroupQueue()) AdminGroupServiceReceptors.fetchingGroupReceptor(action)
for (const action of setAdminGroupQueue()) AdminGroupServiceReceptors.setAdminGroupReceptor(action)
for (const action of updateGroupQueue()) AdminGroupServiceReceptors.updateGroupReceptor(action)
for (const action of removeGroupActionQueue()) AdminGroupServiceReceptors.removeGroupActionReceptor(action)
for (const action of addAdminGroupQueue()) AdminGroupServiceReceptors.addAdminGroupReceptor(action)
for (const action of activeRoutesRetrievedQueue()) AdminActiveRouteReceptors.activeRoutesRetrievedReceptor(action)
for (const action of fetchedInstanceServerQueue()) AdminInstanceServerReceptors.fetchedInstanceServerReceptor(action)
for (const action of chargebeeSettingRetrievedQueue())
Expand Down