Skip to content

Commit

Permalink
Add extra logging for deep link debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphiiko committed Oct 30, 2024
1 parent d2bd22a commit 37365fa
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions src-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fn configure_tauri_plugin_aptabase() -> TauriPlugin<Wry> {
fn configure_tauri_plugin_deep_link(app_handle: AppHandle) {
if let Err(e) = tauri_plugin_deep_link::register("oyasumivr", move |request| {
dbg!(&request);
info!("[Core] Received deep link request: {:#?}", request);
app_handle.emit_all("onDeepLinkCall", request).unwrap();
}) {
warn!("[Core] Could not register schema for handling deep links. Functionality requiring deep links will not work properly. Error: {:#?}", e);
Expand Down
38 changes: 36 additions & 2 deletions src-ui/app/services/deep-link.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { listen } from '@tauri-apps/api/event';
import { warn } from 'tauri-plugin-log-api';
import { info, warn } from 'tauri-plugin-log-api';
import { PulsoidService } from './integrations/pulsoid.service';

@Injectable({
Expand All @@ -12,24 +12,38 @@ export class DeepLinkService {
async init() {
await listen<string>('onDeepLinkCall', async (event) => {
let url: URL | null = null;
info('[DEEPLINK_DEBUG] RECEIVED DEEP LINK CALL: ' + event.payload);
try {
url = new URL(event.payload);
info('[DEEPLINK_DEBUG] PARSED URL: ' + url.toString());
} catch (e) {
info('[DEEPLINK_DEBUG] FAILED TO PARSE URL: ' + e);
await warn(`[DeepLinkService] Failed to parse deep link URL: ${event.payload}`);
return;
}
await this.handleDeepLinkCall(url);
try {
await this.handleDeepLinkCall(url);
} catch (e) {
info('[DEEPLINK_DEBUG] FAILED TO HANDLE DEEP LINK CALL: ' + e);
await warn(`[DeepLinkService] Failed to handle deep link call: ${e}`);
}
});
}

private async handleDeepLinkCall(url: URL) {
info('[DEEPLINK_DEBUG] handleDeepLinkCall 1');
let pathname = url.pathname;
// Remove any leading slashes
while (pathname.startsWith('/')) pathname = pathname.substring(1);
info(`[DEEPLINK_DEBUG] handleDeepLinkCall 2 ${pathname}`);
const route = pathname.split('/');
info(`[DEEPLINK_DEBUG] handleDeepLinkCall 3 ${route}`);
info(`[DEEPLINK_DEBUG] handleDeepLinkCall 4 ${route[0]}`);
switch (route[0]) {
case 'integration':
info(`[DEEPLINK_DEBUG] handleDeepLinkCall 5`);
if (route.length < 2) break;
info(`[DEEPLINK_DEBUG] handleDeepLinkCall 6`);
await this.handleDeepLinkByIntegration(
route[1],
'/' + route.slice(2).join('/'),
Expand All @@ -50,18 +64,38 @@ export class DeepLinkService {
params: Record<string, string[]>,
fragment: string
) {
info(
`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 1 ${JSON.stringify({
integration,
path,
params,
fragment,
})}`
);
const fragmentParams: Record<string, string[]> = {};
try {
info(`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 2`);
for (const [key, value] of new URLSearchParams(fragment)) {
if (fragmentParams.hasOwnProperty(key)) fragmentParams[key].push(value);
else fragmentParams[key] = [value];
}
info(`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 3`);
} catch (e) {
// It's ok if we can't parse the fragment
info(`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 4`);
}
info(`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 5 ${integration}`);
switch (integration) {
case 'pulsoid':
info(
`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 6 ${JSON.stringify({
path,
params,
fragmentParams,
})}`
);
await this.pulsoid.handleDeepLink(path, params, fragmentParams);
info(`[DEEPLINK_DEBUG] handleDeepLinkByIntegration 7`);
break;
}
}
Expand Down
60 changes: 60 additions & 0 deletions src-ui/app/services/integrations/pulsoid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Observable,
of,
switchMap,
tap,
timeout,
} from 'rxjs';
import { Client, getClient } from '@tauri-apps/api/http';
Expand Down Expand Up @@ -138,53 +139,93 @@ export class PulsoidService {
params: Record<string, string[]>,
fragmentParams: Record<string, string[]>
) {
info(
`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 1 ${JSON.stringify({
path,
params,
fragmentParams,
})}`
);
// Validate response
let errorOccurred = false;
let errorCode: string | undefined;
let errorDescription: string | undefined;
if (params['error']) {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.1.1`);
errorOccurred = true;
errorCode = params['error'][0];
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.1.2`);
errorDescription = params['error_description']
? params['error_description'][0].replace(/\+/g, ' ')
: undefined;
info(
`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.1.3 ${JSON.stringify({
errorOccurred,
errorCode,
errorDescription,
})}`
);
error(
`[PulsoidService] Pulsoid login failed, received error response: ${errorDescription} (${errorCode})`
);
} else if (!fragmentParams['state'] || !this.csrfCache.includes(fragmentParams['state'][0])) {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.2.1 ${fragmentParams['state'][0]}`);
errorOccurred = true;
error(`[PulsoidService] Pulsoid login failed, csrf check failed: ${fragmentParams['state']}`);
} else if (!fragmentParams['access_token']) {
info(
`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.3.1 ${fragmentParams['access_token']}`
);
errorOccurred = true;
error(`[PulsoidService] Pulsoid login failed, no access token received`);
} else if (!fragmentParams['expires_in']) {
info(
`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 2.4.1 ${fragmentParams['expires_in']}`
);
errorOccurred = true;
error(`[PulsoidService] Pulsoid login failed, no token expiry received`);
}
info(
`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 3 ${JSON.stringify({
errorOccurred,
errorCode,
errorDescription,
})}`
);
if (!errorOccurred) {
// Save token set
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 4`);
const tokenSet: PulsoidTokenSet = {
access_token: fragmentParams['access_token'][0],
expires_in: parseInt(fragmentParams['expires_in'][0]),
};
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 5 ${JSON.stringify(tokenSet)}`);
await this.setActiveTokenSet(tokenSet);
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 6`);
// Fetch the profile
let profile: PulsoidProfile | undefined;
try {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 7`);
profile = await firstValueFrom(this.getProfile());
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 8`);
} catch (e) {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 9 ${e}`);
errorOccurred = true;
error(`[PulsoidService] Pulsoid login failed, could not fetch profile: ${e}`);
await this.setActiveTokenSet(null);
}
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 10`);
// Save the active profile
if (profile) {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 11`);
await this.setActiveProfile(profile);
return;
}
}
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 12`);
// If a validation error occurred, stop here and inform the user.
if (errorOccurred) {
info(`[DEEPLINK_DEBUG] [PulsoidService] handleDeepLink 13 ${errorCode} ${errorDescription}`);
this.modalService
.addModal<ConfirmModalInputModel, ConfirmModalOutputModel>(ConfirmModalComponent, {
title: 'pulsoid.login.error.title',
Expand All @@ -206,25 +247,39 @@ export class PulsoidService {
}

private async setActiveTokenSet(tokenSet: PulsoidTokenSet | null) {
info(`[DEEPLINK_DEBUG] [PulsoidService] setActiveTokenSet 1 ${JSON.stringify(tokenSet)}`);
const newSettings = structuredClone(this.settings.value);
newSettings.accessToken = tokenSet?.access_token ?? undefined;
newSettings.expiresAt = tokenSet
? Math.floor(Date.now() / 1000) + tokenSet!.expires_in
: undefined;
info(`[DEEPLINK_DEBUG] [PulsoidService] setActiveTokenSet 2`);
this.settings.next(newSettings);
await this.setActiveProfile(null);
info(`[DEEPLINK_DEBUG] [PulsoidService] setActiveTokenSet 3`);
}

private async setActiveProfile(profile: PulsoidProfile | null) {
info(`[DEEPLINK_DEBUG] [PulsoidService] setActiveProfile 1`);
const newSettings = structuredClone(this.settings.value);
newSettings.username = profile?.username ?? undefined;
this.settings.next(newSettings);
await this.saveSettings();
info(`[DEEPLINK_DEBUG] [PulsoidService] setActiveProfile 2`);
}

private getProfile(): Observable<PulsoidProfile> {
info(`[DEEPLINK_DEBUG] [PulsoidService] getProfile 1 ${this.settings.value.accessToken}`);
if (!this.settings.value.accessToken) throw new Error('No token set available');
info(`[DEEPLINK_DEBUG] [PulsoidService] getProfile 2`);
return this.getApiUrl('profile').pipe(
tap((url) => {
info(
`[DEEPLINK_DEBUG] [PulsoidService] getProfile 3 ${url} HEADERS=${JSON.stringify({
Authorization: `Bearer ${this.settings.value?.accessToken}`,
})}`
);
}),
switchMap((url) =>
this.client!.get<PulsoidProfile>(url, {
headers: {
Expand All @@ -234,6 +289,11 @@ export class PulsoidService {
),
// If response was not successful, throw an error
map((response) => {
info(
`[DEEPLINK_DEBUG] [PulsoidService] getProfile 4 ${response.ok} ${JSON.stringify(
response.data
)}`
);
if (!response.ok) throw response;
return response;
}),
Expand Down

0 comments on commit 37365fa

Please sign in to comment.