Skip to content

Commit 3f63607

Browse files
authored
Stopped logging "not logged in" repeatedly (#25)
Stopped logging not logged in repeatedly
1 parent f929c06 commit 3f63607

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/scripture-forge/src/auth/scripture-forge-authentication-provider.model.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ type RevokeRefreshTokenRequestBody = {
7171
/** Path on extension redirect URL for picking up auth response */
7272
export const AUTH_PATH = '/callback/auth0';
7373

74+
/** Error message thrown when not logged in when trying to get access token */
75+
export const NOT_LOGGED_IN_ERROR_MESSAGE = 'Not logged in';
76+
7477
/** Necessary auth scopes for accessing Slingshot drafts */
7578
const SCOPES = ['openid', 'profile', 'email', 'sf_data', 'offline_access'].join(' ');
7679
/** Auth audience for Scripture Forge...? */
@@ -492,7 +495,7 @@ export default class ScriptureForgeAuthenticationProvider implements Dispose {
492495
}
493496
this.#hasRetrievedAuthTokensFromStorage = true;
494497
}
495-
if (!this.#authTokens) throw new Error('Not logged in');
498+
if (!this.#authTokens) throw new Error(NOT_LOGGED_IN_ERROR_MESSAGE);
496499

497500
if (!this.#authTokens.didExpire && this.#authTokens.accessTokenExpireTime > Date.now())
498501
return this.#authTokens.accessToken;

src/scripture-forge/src/main.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ export async function activate(context: ExecutionActivationContext) {
193193
// const scriptureForgeApi = new ScriptureForgeSampleApi(authenticationProvider);
194194
const scriptureForgeApi = new ScriptureForgeApi(authenticationProvider);
195195

196-
const slingshotPdpef = new SlingshotProjectDataProviderEngineFactory(scriptureForgeApi);
196+
const slingshotPdpef = new SlingshotProjectDataProviderEngineFactory(
197+
scriptureForgeApi,
198+
sessionChangeEmitter.event,
199+
);
200+
201+
context.registrations.add(slingshotPdpef);
202+
197203
const slingshotPdpefPromise = papi.projectDataProviders.registerProjectDataProviderEngineFactory(
198204
SCRIPTURE_FORGE_SLINGSHOT_PDPF_ID,
199205
SLINGSHOT_PROJECT_INTERFACES,

src/scripture-forge/src/projects/slingshot-project-data-provider-engine-factory.model.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {
44
ProjectMetadataWithoutFactoryInfo,
55
} from '@papi/core';
66
import { logger } from '@papi/backend';
7-
import { getErrorMessage, Mutex } from 'platform-bible-utils';
7+
import { Dispose, getErrorMessage, Mutex, PlatformEvent, Unsubscriber } from 'platform-bible-utils';
88
import SlingshotProjectDataProviderEngine, {
99
SLINGSHOT_PROJECT_INTERFACES,
1010
} from './slingshot-project-data-provider-engine.model';
1111
import ScriptureForgeApi, { ScriptureForgeProjectInfo } from './scripture-forge-api.model';
12+
import { NOT_LOGGED_IN_ERROR_MESSAGE } from '../auth/scripture-forge-authentication-provider.model';
1213

1314
/**
1415
* Duration in milliseconds to throttle the `getProjects` API call. We will return the previous
@@ -30,16 +31,29 @@ function getSlingshotAppProjectId(projectId: string): string {
3031
}
3132

3233
export default class SlingshotProjectDataProviderEngineFactory
33-
implements IProjectDataProviderEngineFactory<typeof SLINGSHOT_PROJECT_INTERFACES>
34+
implements IProjectDataProviderEngineFactory<typeof SLINGSHOT_PROJECT_INTERFACES>, Dispose
3435
{
3536
private projectInfoByAppProjectId = new Map<string, ScriptureForgeProjectInfo>();
3637
private pdpeByAppProjectId = new Map<string, SlingshotProjectDataProviderEngine>();
3738
/** Last time we ran `getProjects` on the API so we can throttle it */
3839
private lastGetProjectsTime: number = 0;
3940
private lastAvailableProjects: ProjectMetadataWithoutFactoryInfo[] = [];
4041
private getProjectsMutex = new Mutex();
41-
42-
constructor(private scriptureForgeApi: ScriptureForgeApi) {}
42+
/**
43+
* If we know we are logged out and therefore shouldn't try to check for projects. If this is
44+
* `false`, it doesn't mean we are logged in but that we need to try getting projects again
45+
*/
46+
private isLoggedOut = false;
47+
private onDidSessionChangeUnsubscriber: Unsubscriber;
48+
49+
constructor(
50+
private scriptureForgeApi: ScriptureForgeApi,
51+
onDidSessionChange: PlatformEvent<undefined>,
52+
) {
53+
this.onDidSessionChangeUnsubscriber = onDidSessionChange(() => {
54+
this.isLoggedOut = false;
55+
});
56+
}
4357

4458
async getAvailableProjects(): Promise<ProjectMetadataWithoutFactoryInfo[]> {
4559
return this.#getAvailableProjects();
@@ -64,13 +78,20 @@ export default class SlingshotProjectDataProviderEngineFactory
6478
return pdpe;
6579
}
6680

81+
async dispose(): Promise<boolean> {
82+
return this.onDidSessionChangeUnsubscriber();
83+
}
84+
6785
/**
6886
* Gets available projects from cache or reaches out to get new project info
6987
*
7088
* @param force If `true`, will get new project info
7189
*/
7290
async #getAvailableProjects(force = false) {
7391
if (force) this.lastGetProjectsTime = 0;
92+
// If we're not forcing and we know we're logged out, return empty array instead of failing
93+
// again because we know we're already logged out
94+
else if (this.isLoggedOut) return [];
7495

7596
if (Date.now() - this.lastGetProjectsTime < GET_PROJECTS_THROTTLE_MS)
7697
return this.lastAvailableProjects;
@@ -86,9 +107,14 @@ export default class SlingshotProjectDataProviderEngineFactory
86107
try {
87108
projectsInfo = await this.scriptureForgeApi.getProjects();
88109
} catch (e) {
110+
const errorMessage = getErrorMessage(e);
89111
logger.warn(
90-
`Slingshot PDPEF caught Scripture Forge API error while getting available projects. ${getErrorMessage(e)}`,
112+
`Slingshot PDPEF caught Scripture Forge API error while getting available projects. ${errorMessage}`,
91113
);
114+
115+
// Keep track if we're logged out so we don't keep spamming the logs
116+
if (errorMessage === NOT_LOGGED_IN_ERROR_MESSAGE) this.isLoggedOut = true;
117+
92118
return [];
93119
}
94120

0 commit comments

Comments
 (0)