Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes issue where some accounts were not correctly loading subscription info #2361

Merged
merged 2 commits into from Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 28 additions & 13 deletions packages/api/src/Domain/Http/HttpService.ts
@@ -1,6 +1,6 @@
import { joinPaths, sleep } from '@standardnotes/utils'
import { Environment } from '@standardnotes/models'
import { Session, SessionToken } from '@standardnotes/domain-core'
import { LegacySession, Session, SessionToken } from '@standardnotes/domain-core'
import {
HttpStatusCode,
HttpRequestParams,
Expand All @@ -18,7 +18,7 @@ import { FetchRequestHandler } from './FetchRequestHandler'
import { RequestHandlerInterface } from './RequestHandlerInterface'

export class HttpService implements HttpServiceInterface {
private session: Session | null
private session?: Session | LegacySession
private __latencySimulatorMs?: number
private declare host: string

Expand All @@ -29,7 +29,6 @@ export class HttpService implements HttpServiceInterface {
private requestHandler: RequestHandlerInterface

constructor(private environment: Environment, private appVersion: string, private snjsVersion: string) {
this.session = null
this.requestHandler = new FetchRequestHandler(this.snjsVersion, this.appVersion, this.environment)
}

Expand All @@ -42,12 +41,12 @@ export class HttpService implements HttpServiceInterface {
}

public deinit(): void {
this.session = null
;(this.session as unknown) = undefined
;(this.updateMetaCallback as unknown) = undefined
;(this.refreshSessionCallback as unknown) = undefined
}

setSession(session: Session): void {
setSession(session: Session | LegacySession): void {
this.session = session
}

Expand All @@ -59,6 +58,18 @@ export class HttpService implements HttpServiceInterface {
return this.host
}

private getSessionAccessToken(): string | undefined {
if (!this.session) {
return undefined
}

if (this.session instanceof Session) {
return this.session.accessToken.value
} else {
return this.session.accessToken
}
}

async get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>> {
if (!this.host) {
throw new Error('Attempting to make network request before host is set')
Expand All @@ -68,7 +79,7 @@ export class HttpService implements HttpServiceInterface {
url: joinPaths(this.host, path),
params,
verb: HttpVerb.Get,
authentication: authentication ?? this.session?.accessToken.value,
authentication: authentication ?? this.getSessionAccessToken(),
})
}

Expand All @@ -90,7 +101,7 @@ export class HttpService implements HttpServiceInterface {
url: joinPaths(this.host, path),
params,
verb: HttpVerb.Post,
authentication: authentication ?? this.session?.accessToken.value,
authentication: authentication ?? this.getSessionAccessToken(),
})
}

Expand All @@ -99,7 +110,7 @@ export class HttpService implements HttpServiceInterface {
url: joinPaths(this.host, path),
params,
verb: HttpVerb.Put,
authentication: authentication ?? this.session?.accessToken.value,
authentication: authentication ?? this.getSessionAccessToken(),
})
}

Expand All @@ -108,7 +119,7 @@ export class HttpService implements HttpServiceInterface {
url: joinPaths(this.host, path),
params,
verb: HttpVerb.Patch,
authentication: authentication ?? this.session?.accessToken.value,
authentication: authentication ?? this.getSessionAccessToken(),
})
}

Expand All @@ -117,7 +128,7 @@ export class HttpService implements HttpServiceInterface {
url: joinPaths(this.host, path),
params,
verb: HttpVerb.Delete,
authentication: authentication ?? this.session?.accessToken.value,
authentication: authentication ?? this.getSessionAccessToken(),
})
}

Expand All @@ -130,7 +141,7 @@ export class HttpService implements HttpServiceInterface {
if (this.inProgressRefreshSessionPromise && !isRefreshRequest) {
await this.inProgressRefreshSessionPromise

httpRequest.authentication = this.session?.accessToken.value
httpRequest.authentication = this.getSessionAccessToken()
}

const response = await this.requestHandler.handleRequest<T>(httpRequest)
Expand All @@ -152,7 +163,7 @@ export class HttpService implements HttpServiceInterface {
}
}

httpRequest.authentication = this.session?.accessToken.value
httpRequest.authentication = this.getSessionAccessToken()

return this.runHttp(httpRequest)
}
Expand All @@ -161,7 +172,11 @@ export class HttpService implements HttpServiceInterface {
}

private async refreshSession(): Promise<boolean> {
if (this.session === null) {
if (!this.session) {
return false
}

if (this.session instanceof LegacySession) {
return false
}

Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/Domain/Http/HttpServiceInterface.ts
@@ -1,20 +1,23 @@
import { Session } from '@standardnotes/domain-core'
import { LegacySession, Session } from '@standardnotes/domain-core'
import { HttpRequest, HttpRequestParams, HttpResponse, HttpResponseMeta } from '@standardnotes/responses'

export interface HttpServiceInterface {
setHost(host: string): void
getHost(): string
setSession(session: Session): void

get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
getExternal<T>(url: string, params?: HttpRequestParams): Promise<HttpResponse<T>>
post<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
put<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
patch<T>(path: string, params: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
delete<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
runHttp<T>(httpRequest: HttpRequest): Promise<HttpResponse<T>>

setSession(session: Session | LegacySession): void
setCallbacks(
updateMetaCallback: (meta: HttpResponseMeta) => void,
refreshSessionCallback: (session: Session) => void,
): void

deinit(): void
}
4 changes: 1 addition & 3 deletions packages/snjs/lib/Services/Session/SessionManager.ts
Expand Up @@ -169,9 +169,7 @@ export class SNSessionManager
private setSession(session: Session | LegacySession, persist = true): void {
this.session = session

if (session instanceof Session) {
this.httpService.setSession(session)
}
this.httpService.setSession(session)

this.apiService.setSession(session, persist)

Expand Down