From ef7861691c35d97fa7e5b7017e20fca7c11bed43 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Tue, 1 Jun 2021 13:47:29 +0930 Subject: [PATCH] fix: return local server if no my_server key exists --- app/cache/user.ts | 4 +- app/graphql/schema/utils.ts | 97 +++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/app/cache/user.ts b/app/cache/user.ts index 7f18ad3d2..049a64a05 100644 --- a/app/cache/user.ts +++ b/app/cache/user.ts @@ -18,10 +18,10 @@ export interface CachedServer { apikey: string; name: string; status: Status; - wanip: IpAddress; + wanip: IpAddress | null; lanip: IpAddress; localurl: URL; - remoteurl: string; + remoteurl: string | null; } export interface CachedServers { diff --git a/app/graphql/schema/utils.ts b/app/graphql/schema/utils.ts index 49b0aa4d0..7de4d07dc 100644 --- a/app/graphql/schema/utils.ts +++ b/app/graphql/schema/utils.ts @@ -48,53 +48,7 @@ type makeNullUndefinedAndOptional = { type Server = makeNullUndefinedAndOptional; -export const getServers = async (): Promise => { - // Check if we have the servers already cached, if so return them - const cachedServers = userCache.get('mine')?.servers; - if (cachedServers) { - return cachedServers; - } - - // For now use the my_servers key - // Later we should return the correct one for the current user with the correct scope, etc. - const apiKey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString()!; - - // No cached servers found - if (!cachedServers) { - // Fetch servers from mothership - const servers = await getUserServers(apiKey); - - graphqlLogger.debug('Using upstream for /servers endpoint'); - - // No servers found - if (!servers || servers.length === 0) { - return []; - } - - // Cache servers - userCache.set('mine', { - servers - }); - - // Get first server's owner object - const owner = servers[0].owner; - - try { - throw new Error('THIS_HERE'); - } catch (error: unknown) { - console.log(error); - } - - // Publish owner event - await pubsub.publish('owner', { - owner - }); - - // Return servers from mothership - return servers; - } - - graphqlLogger.debug('Falling back to local state for /servers endpoint'); +const getLocalServer = (apiKey: string): [CachedServer] => { const guid = varState?.data?.regGuid; const name = varState?.data?.name; const wanip = null; @@ -119,3 +73,52 @@ export const getServers = async (): Promise => { remoteurl }]; }; + +export const getServers = async (): Promise => { + // Check if we have the servers already cached, if so return them + const cachedServers = userCache.get('mine')?.servers; + if (cachedServers) { + return cachedServers; + } + + // For now use the my_servers key + // Later we should return the correct one for the current user with the correct scope, etc. + const apiKey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString()!; + + // Return only current server if we have no key + if (!apiKey) { + return getLocalServer(apiKey); + } + + // No cached servers found + if (!cachedServers) { + // Fetch servers from mothership + const servers = await getUserServers(apiKey); + + graphqlLogger.debug('Using upstream for /servers endpoint'); + + // No servers found + if (!servers || servers.length === 0) { + return []; + } + + // Cache servers + userCache.set('mine', { + servers + }); + + // Get first server's owner object + const owner = servers[0].owner; + + // Publish owner event + await pubsub.publish('owner', { + owner + }); + + // Return servers from mothership + return servers; + } + + graphqlLogger.debug('Falling back to local state for /servers endpoint'); + return getLocalServer(apiKey); +};