@@ -27,15 +27,13 @@ import { Paginated } from '@feathersjs/feathers'
2727
2828import { AdminBot , CreateBotAsAdmin } from '@etherealengine/common/src/interfaces/AdminBot'
2929import multiLogger from '@etherealengine/common/src/logger'
30- import { matches , Validator } from '@etherealengine/engine/src/common/functions/MatchesUtils '
31- import { defineAction , defineState , dispatchAction , getMutableState } from '@etherealengine/hyperflux'
30+ import { Engine } from '@etherealengine/engine/src/ecs/classes/Engine '
31+ import { defineState , getMutableState } from '@etherealengine/hyperflux'
3232
33- import { API } from '../../API'
34- import { AuthState } from '../../user/services/AuthService'
33+ import { userIsAdmin } from '../../user/userHasAccess'
3534
3635const logger = multiLogger . child ( { component : 'client-core:BotsService' } )
3736
38- //State
3937export const BOTS_PAGE_LIMIT = 100
4038
4139export const AdminBotState = defineState ( {
@@ -52,56 +50,21 @@ export const AdminBotState = defineState({
5250 } )
5351} )
5452
55- const fetchedBotReceptor = ( action : typeof AdminBotsActions . fetchedBot . matches . _TYPE ) => {
56- const state = getMutableState ( AdminBotState )
57- return state . merge ( {
58- bots : action . bots . data ,
59- retrieving : false ,
60- fetched : true ,
61- updateNeeded : false ,
62- lastFetched : Date . now ( )
63- } )
64- }
65-
66- const botCreatedReceptor = ( action : typeof AdminBotsActions . botCreated . matches . _TYPE ) => {
67- const state = getMutableState ( AdminBotState )
68- return state . merge ( { updateNeeded : true } )
69- }
70-
71- const botPatchedReceptor = ( action : typeof AdminBotsActions . botPatched . matches . _TYPE ) => {
72- const state = getMutableState ( AdminBotState )
73- return state . merge ( { updateNeeded : true } )
74- }
75-
76- const botRemovedReceptor = ( action : typeof AdminBotsActions . botRemoved . matches . _TYPE ) => {
77- const state = getMutableState ( AdminBotState )
78- return state . merge ( { updateNeeded : true } )
79- }
80-
81- export const AdminBotServiceReceptors = {
82- fetchedBotReceptor,
83- botCreatedReceptor,
84- botPatchedReceptor,
85- botRemovedReceptor
86- }
87-
88- //Service
8953export const AdminBotService = {
9054 createBotAsAdmin : async ( data : CreateBotAsAdmin ) => {
9155 try {
92- const bot = await API . instance . client . service ( 'bot' ) . create ( data )
93- dispatchAction ( AdminBotsActions . botCreated ( { bot } ) )
56+ await Engine . instance . api . service ( 'bot' ) . create ( data )
57+ getMutableState ( AdminBotState ) . merge ( { updateNeeded : true } )
9458 } catch ( error ) {
9559 logger . error ( error )
9660 }
9761 } ,
9862 fetchBotAsAdmin : async ( incDec ?: 'increment' | 'decrement' ) => {
9963 try {
100- const user = getMutableState ( AuthState ) . user
10164 const skip = getMutableState ( AdminBotState ) . skip . value
10265 const limit = getMutableState ( AdminBotState ) . limit . value
103- if ( user . scopes ?. value ?. find ( ( scope ) => scope . type === 'admin:admin' ) ) {
104- const bots = ( await API . instance . client . service ( 'bot' ) . find ( {
66+ if ( userIsAdmin ( ) ) {
67+ const bots = ( await Engine . instance . api . service ( 'bot' ) . find ( {
10568 query : {
10669 $sort : {
10770 name : 1
@@ -111,45 +74,32 @@ export const AdminBotService = {
11174 action : 'admin'
11275 }
11376 } ) ) as Paginated < AdminBot >
114- dispatchAction ( AdminBotsActions . fetchedBot ( { bots } ) )
77+ getMutableState ( AdminBotState ) . merge ( {
78+ bots : bots . data ,
79+ retrieving : false ,
80+ fetched : true ,
81+ updateNeeded : false ,
82+ lastFetched : Date . now ( )
83+ } )
11584 }
11685 } catch ( error ) {
11786 logger . error ( error )
11887 }
11988 } ,
12089 removeBots : async ( id : string ) => {
12190 try {
122- const bot = ( await API . instance . client . service ( 'bot' ) . remove ( id ) ) as AdminBot
123- dispatchAction ( AdminBotsActions . botRemoved ( { bot } ) )
91+ await Engine . instance . api . service ( 'bot' ) . remove ( id )
92+ getMutableState ( AdminBotState ) . merge ( { updateNeeded : true } )
12493 } catch ( error ) {
12594 logger . error ( error )
12695 }
12796 } ,
12897 updateBotAsAdmin : async ( id : string , bot : CreateBotAsAdmin ) => {
12998 try {
130- const result = ( await API . instance . client . service ( 'bot' ) . patch ( id , bot ) ) as AdminBot
131- dispatchAction ( AdminBotsActions . botPatched ( { bot : result } ) )
99+ await Engine . instance . api . service ( 'bot' ) . patch ( id , bot )
100+ getMutableState ( AdminBotState ) . merge ( { updateNeeded : true } )
132101 } catch ( error ) {
133102 logger . error ( error )
134103 }
135104 }
136105}
137- //Action
138- export class AdminBotsActions {
139- static fetchedBot = defineAction ( {
140- type : 'ee.client.AdminBots.BOT_ADMIN_DISPLAY' as const ,
141- bots : matches . object as Validator < unknown , Paginated < AdminBot > >
142- } )
143- static botCreated = defineAction ( {
144- type : 'ee.client.AdminBots.BOT_ADMIN_CREATE' as const ,
145- bot : matches . object as Validator < unknown , AdminBot >
146- } )
147- static botRemoved = defineAction ( {
148- type : 'ee.client.AdminBots.BOT_ADMIN_REMOVE' as const ,
149- bot : matches . object as Validator < unknown , AdminBot >
150- } )
151- static botPatched = defineAction ( {
152- type : 'ee.client.AdminBots.BOT_ADMIN_UPDATE' as const ,
153- bot : matches . object as Validator < unknown , AdminBot >
154- } )
155- }
0 commit comments