From 650ada3f8ed80f7f976c9d5a2e2063bf3969fbce Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 26 Dec 2022 17:56:20 +0100 Subject: [PATCH 1/9] feat(Settings): implement trakt auth and install addon --- src/routes/Settings/Settings.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/routes/Settings/Settings.js b/src/routes/Settings/Settings.js index c90b7150e..fe11e82bd 100644 --- a/src/routes/Settings/Settings.js +++ b/src/routes/Settings/Settings.js @@ -75,8 +75,11 @@ const Settings = () => { }); }, []); const authenticateTraktOnClick = React.useCallback(() => { - // TODO - }, []); + if (profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt === null && typeof profile.auth.user._id === 'string') { + const auth_url = `https://www.strem.io/trakt/auth/${profile.auth.user._id}`; + window.open(auth_url); + } + }, [profile.auth]); const importFacebookOnClick = React.useCallback(() => { // TODO }, []); @@ -130,6 +133,16 @@ const Settings = () => { const sectionsContainerOnScorll = React.useCallback(throttle(() => { updateSelectedSectionId(); }, 50), []); + React.useEffect(() => { + if (profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt !== null) { + core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'InstallTraktAddon' + } + }); + } + }, [profile.auth]); React.useEffect(() => { if (dataExport.exportUrl !== null && typeof dataExport.exportUrl === 'string') { window.open(dataExport.exportUrl); @@ -223,9 +236,10 @@ const Settings = () => {
Trakt Scrobbling
-
From b9e826553ccdd22fee2f4aa7405a627cbd1fa4b2 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 26 Dec 2022 17:57:03 +0100 Subject: [PATCH 2/9] refactor(ServicesToaster): prevent installed trakt addon toast error --- src/App/ServicesToaster.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/App/ServicesToaster.js b/src/App/ServicesToaster.js index 43b076c9a..6ae2c7107 100644 --- a/src/App/ServicesToaster.js +++ b/src/App/ServicesToaster.js @@ -19,6 +19,10 @@ const ServicesToaster = () => { break; } + if (args.source.event === 'AddonInstalled' && args.source.args.transport_url.startsWith('https://www.strem.io/trakt/addon')) { + break; + } + toast.show({ type: 'error', title: args.source.event, From 998cd7d9e032697b2940ccf076a96d04854b735d Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 08:48:33 +0100 Subject: [PATCH 3/9] refactor(Settings): improve trakt auth logic --- src/routes/Settings/Settings.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/routes/Settings/Settings.js b/src/routes/Settings/Settings.js index fe11e82bd..6ad3ea941 100644 --- a/src/routes/Settings/Settings.js +++ b/src/routes/Settings/Settings.js @@ -49,6 +49,10 @@ const Settings = () => { streamingServerUrlInput.onChange(configureServerUrlInputRef.current.value); closeConfigureServerUrlModal(); }, [streamingServerUrlInput]); + const [traktAuthStarted, setTraktAuthStarted] = React.useState(false); + const isTraktAuthenticated = React.useMemo(() => { + return profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt !== null; + }, [profile.auth]); const configureServerUrlModalButtons = React.useMemo(() => { return [ { @@ -75,11 +79,11 @@ const Settings = () => { }); }, []); const authenticateTraktOnClick = React.useCallback(() => { - if (profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt === null && typeof profile.auth.user._id === 'string') { - const auth_url = `https://www.strem.io/trakt/auth/${profile.auth.user._id}`; - window.open(auth_url); + if (!isTraktAuthenticated && profile.auth !== null && profile.auth.user !== null && typeof profile.auth.user._id === 'string') { + window.open(`https://www.strem.io/trakt/auth/${profile.auth.user._id}`); + setTraktAuthStarted(true); } - }, [profile.auth]); + }, [isTraktAuthenticated, profile.auth]); const importFacebookOnClick = React.useCallback(() => { // TODO }, []); @@ -134,15 +138,16 @@ const Settings = () => { updateSelectedSectionId(); }, 50), []); React.useEffect(() => { - if (profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt !== null) { + if (isTraktAuthenticated && traktAuthStarted) { core.transport.dispatch({ action: 'Ctx', args: { action: 'InstallTraktAddon' } }); + setTraktAuthStarted(false); } - }, [profile.auth]); + }, [isTraktAuthenticated, traktAuthStarted]); React.useEffect(() => { if (dataExport.exportUrl !== null && typeof dataExport.exportUrl === 'string') { window.open(dataExport.exportUrl); @@ -153,7 +158,19 @@ const Settings = () => { updateSelectedSectionId(); } closeConfigureServerUrlModal(); - }, [routeFocused]); + const onWindowFocus = () => { + if (!isTraktAuthenticated && traktAuthStarted) { + core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'PullUserFromAPI' + } + }); + } + }; + window.addEventListener('focus', onWindowFocus); + return () => window.removeEventListener('focus', onWindowFocus); + }, [routeFocused, isTraktAuthenticated, traktAuthStarted]); return (
From f57a053b1ef009b41355ecd0b7c1b957c57b1381 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 09:06:36 +0100 Subject: [PATCH 4/9] chore: update stremio-core-web --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 275f9aa31..8959f8609 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@babel/runtime": "7.16.0", "@sentry/browser": "6.13.3", "@stremio/stremio-colors": "5.0.1", - "@stremio/stremio-core-web": "0.44.10", + "@stremio/stremio-core-web": "0.44.11", "@stremio/stremio-icons": "4.0.0", "@stremio/stremio-video": "0.0.24", "a-color-picker": "1.2.1", @@ -2699,9 +2699,9 @@ "integrity": "sha512-Dt3PYmy1DZ473QNs99KYXVWQPHtpIl37VUY0+gCEvvuCqE1fRrZIJtZ9KbysUKonvO7WwdQDztgcW0iGoc1dEA==" }, "node_modules/@stremio/stremio-core-web": { - "version": "0.44.10", - "resolved": "https://registry.npmjs.org/@stremio/stremio-core-web/-/stremio-core-web-0.44.10.tgz", - "integrity": "sha512-+AUJ3d5u5SH64lcuQIAtXPfeIaWzSNfdDL92HECXP29qat2TtCyM+ENLhTuMZzrbInD22SQrRhfIS3T8nFVQYg==", + "version": "0.44.11", + "resolved": "https://registry.npmjs.org/@stremio/stremio-core-web/-/stremio-core-web-0.44.11.tgz", + "integrity": "sha512-KkNEZenQ+94ylYEMl/AqvL1Md4p+xwqg/02FKuVxjFBEoKyXEDXCgOKOI7wbbXdZJe8tnvk+XFq39jzAiTinSw==", "dependencies": { "@babel/runtime": "7.16.0" } @@ -16716,9 +16716,9 @@ "integrity": "sha512-Dt3PYmy1DZ473QNs99KYXVWQPHtpIl37VUY0+gCEvvuCqE1fRrZIJtZ9KbysUKonvO7WwdQDztgcW0iGoc1dEA==" }, "@stremio/stremio-core-web": { - "version": "0.44.10", - "resolved": "https://registry.npmjs.org/@stremio/stremio-core-web/-/stremio-core-web-0.44.10.tgz", - "integrity": "sha512-+AUJ3d5u5SH64lcuQIAtXPfeIaWzSNfdDL92HECXP29qat2TtCyM+ENLhTuMZzrbInD22SQrRhfIS3T8nFVQYg==", + "version": "0.44.11", + "resolved": "https://registry.npmjs.org/@stremio/stremio-core-web/-/stremio-core-web-0.44.11.tgz", + "integrity": "sha512-KkNEZenQ+94ylYEMl/AqvL1Md4p+xwqg/02FKuVxjFBEoKyXEDXCgOKOI7wbbXdZJe8tnvk+XFq39jzAiTinSw==", "requires": { "@babel/runtime": "7.16.0" } diff --git a/package.json b/package.json index 6aff4873d..f570411c1 100755 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@babel/runtime": "7.16.0", "@sentry/browser": "6.13.3", "@stremio/stremio-colors": "5.0.1", - "@stremio/stremio-core-web": "0.44.10", + "@stremio/stremio-core-web": "0.44.11", "@stremio/stremio-icons": "4.0.0", "@stremio/stremio-video": "0.0.24", "a-color-picker": "1.2.1", From 0a55c3bdf815b86d984db28b9896b5bcc28ca976 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 09:07:12 +0100 Subject: [PATCH 5/9] feat(Settings): implement trakt logout --- src/routes/Settings/Settings.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/routes/Settings/Settings.js b/src/routes/Settings/Settings.js index 6ad3ea941..807658fd2 100644 --- a/src/routes/Settings/Settings.js +++ b/src/routes/Settings/Settings.js @@ -78,10 +78,17 @@ const Settings = () => { } }); }, []); - const authenticateTraktOnClick = React.useCallback(() => { + const toggleTraktOnClick = React.useCallback(() => { if (!isTraktAuthenticated && profile.auth !== null && profile.auth.user !== null && typeof profile.auth.user._id === 'string') { window.open(`https://www.strem.io/trakt/auth/${profile.auth.user._id}`); setTraktAuthStarted(true); + } else { + core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'LogoutTrakt' + } + }); } }, [isTraktAuthenticated, profile.auth]); const importFacebookOnClick = React.useCallback(() => { @@ -253,10 +260,11 @@ const Settings = () => {
Trakt Scrobbling
-
From 43f3481632e42fe7c5fe2408ea33702279668556 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 09:31:01 +0100 Subject: [PATCH 6/9] refactor: dispatch ctx api actions on window focus instead --- src/App/App.js | 44 ++++++++++++++++++--------------- src/routes/Settings/Settings.js | 14 +---------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/App/App.js b/src/App/App.js index c0a79b727..124e4f53d 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -90,26 +90,30 @@ const App = () => { }; }, []); React.useEffect(() => { - if (services.core.active) { - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'PullAddonsFromAPI' - } - }); - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'PullUserFromAPI' - } - }); - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'SyncLibraryWithAPI' - } - }); - } + const onWindowFocus = () => { + if (services.core.active) { + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'PullAddonsFromAPI' + } + }); + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'PullUserFromAPI' + } + }); + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'SyncLibraryWithAPI' + } + }); + } + }; + window.addEventListener('focus', onWindowFocus); + return () => window.removeEventListener('focus', onWindowFocus); }, [initialized]); return ( diff --git a/src/routes/Settings/Settings.js b/src/routes/Settings/Settings.js index 3ee6a2ed2..a4820c132 100644 --- a/src/routes/Settings/Settings.js +++ b/src/routes/Settings/Settings.js @@ -162,19 +162,7 @@ const Settings = () => { updateSelectedSectionId(); } closeConfigureServerUrlModal(); - const onWindowFocus = () => { - if (!isTraktAuthenticated && traktAuthStarted) { - core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'PullUserFromAPI' - } - }); - } - }; - window.addEventListener('focus', onWindowFocus); - return () => window.removeEventListener('focus', onWindowFocus); - }, [routeFocused, isTraktAuthenticated, traktAuthStarted]); + }, [routeFocused]); return (
From 2beaffc9ab6cf7213b97765cb7485ffdffc9dbf7 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 09:38:11 +0100 Subject: [PATCH 7/9] fix(App): update ctx api actions hook logic --- src/App/App.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/App/App.js b/src/App/App.js index 124e4f53d..1735cee0d 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -91,28 +91,29 @@ const App = () => { }, []); React.useEffect(() => { const onWindowFocus = () => { - if (services.core.active) { - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'PullAddonsFromAPI' - } - }); - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'PullUserFromAPI' - } - }); - services.core.transport.dispatch({ - action: 'Ctx', - args: { - action: 'SyncLibraryWithAPI' - } - }); - } + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'PullAddonsFromAPI' + } + }); + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'PullUserFromAPI' + } + }); + services.core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'SyncLibraryWithAPI' + } + }); }; - window.addEventListener('focus', onWindowFocus); + if (services.core.active) { + onWindowFocus(); + window.addEventListener('focus', onWindowFocus); + } return () => window.removeEventListener('focus', onWindowFocus); }, [initialized]); return ( From 758347076a5f8cb983e5330e9996c3fb161a2271 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 10:11:29 +0100 Subject: [PATCH 8/9] fix(Settings): check for trakt token expires --- src/routes/Settings/Settings.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routes/Settings/Settings.js b/src/routes/Settings/Settings.js index a4820c132..fb891a404 100644 --- a/src/routes/Settings/Settings.js +++ b/src/routes/Settings/Settings.js @@ -51,7 +51,8 @@ const Settings = () => { }, [streamingServerUrlInput]); const [traktAuthStarted, setTraktAuthStarted] = React.useState(false); const isTraktAuthenticated = React.useMemo(() => { - return profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt !== null; + return profile.auth !== null && profile.auth.user !== null && profile.auth.user.trakt !== null && + (Date.now() / 1000) < (profile.auth.user.trakt.created_at + profile.auth.user.trakt.expires_in); }, [profile.auth]); const configureServerUrlModalButtons = React.useMemo(() => { return [ From d1819f4bd68bebf286d2538bf3d06ba2736c1353 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 29 Dec 2022 10:11:35 +0100 Subject: [PATCH 9/9] refactor(ServicesToaster): add more checks when suppressing trakt addon installed error --- src/App/ServicesToaster.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/ServicesToaster.js b/src/App/ServicesToaster.js index 6ae2c7107..d3fdd3461 100644 --- a/src/App/ServicesToaster.js +++ b/src/App/ServicesToaster.js @@ -19,7 +19,7 @@ const ServicesToaster = () => { break; } - if (args.source.event === 'AddonInstalled' && args.source.args.transport_url.startsWith('https://www.strem.io/trakt/addon')) { + if (args.error.type === 'Other' && args.error.code === 3 && args.source.event === 'AddonInstalled' && args.source.args.transport_url.startsWith('https://www.strem.io/trakt/addon')) { break; }