From 7dda625e46050d2a7be3991ca1c7186f8b096950 Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Sun, 16 Feb 2025 14:27:33 -0500 Subject: [PATCH 1/3] if logged in, update user activity --- src/cyd-api-client.ts | 18 ++++++++++++++++++ src/renderer/src/App.vue | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/src/cyd-api-client.ts b/src/cyd-api-client.ts index 0bcc947c..c00f6531 100644 --- a/src/cyd-api-client.ts +++ b/src/cyd-api-client.ts @@ -567,4 +567,22 @@ export default class CydAPIClient { return this.returnError("Failed to subscribe to newsletter. Maybe the server is down?") } } + + // User activity + + async postUserActivity(): Promise { + if (!await this.validateAPIToken()) { + return this.returnError("Failed to get a new API token.") + } + + try { + const response = await this.fetchAuthenticated("POST", `${this.apiURL}/user/activity`, null); + if (response.status != 200) { + return this.returnError("Failed to update user activity.", response.status) + } + return true; + } catch { + return this.returnError("Failed to update user activity. Maybe the server is down?") + } + } } diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index 095a6327..e9d74649 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -98,6 +98,15 @@ onMounted(async () => { isSignedIn.value = true; } + // If logged in, update the server with user activity (gets truncated to day) + if (isSignedIn.value) { + try { + await apiClient.value.postUserActivity(); + } catch (error) { + console.error("Failed to update user activity:", error); + } + } + isReady.value = true; // Change the app title From c577b07298f363fe0c03f0ae5720945b95c96581 Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Tue, 18 Feb 2025 23:06:46 -0500 Subject: [PATCH 2/3] fix: update user activity on sign in, and every 6 hours --- src/renderer/src/App.vue | 36 +++++++++++++++++++++---- src/renderer/src/modals/SignInModal.vue | 3 +++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index e9d74649..12b53369 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -84,6 +84,35 @@ emitter?.on('show-advanced-settings', () => { showAdvancedSettingsModal.value = true; }); +// Track whether a user is actively using Cyd +const updateUserActivity = async () => { + if (isSignedIn.value) { + try { + await apiClient.value.postUserActivity(); + } catch (error) { + console.error("Failed to update user activity:", error); + } + } +}; + +// Update user activity periodically (every 6 hours) +let userActivityInterval: NodeJS.Timeout | null = null; + +const startUserActivityInterval = () => { + if (userActivityInterval) { + clearInterval(userActivityInterval); + } + // Update every 6 hours (6 * 60 * 60 * 1000 = 21600000 ms) + userActivityInterval = setInterval(updateUserActivity, 21600000); +}; + +// Stop the interval when signing out +emitter?.on('signed-out', () => { + if (userActivityInterval) { + clearInterval(userActivityInterval); + userActivityInterval = null; + } +}); onMounted(async () => { await window.electron.trackEvent(PlausibleEvents.APP_OPENED, navigator.userAgent); @@ -100,11 +129,8 @@ onMounted(async () => { // If logged in, update the server with user activity (gets truncated to day) if (isSignedIn.value) { - try { - await apiClient.value.postUserActivity(); - } catch (error) { - console.error("Failed to update user activity:", error); - } + await updateUserActivity(); + startUserActivityInterval(); } isReady.value = true; diff --git a/src/renderer/src/modals/SignInModal.vue b/src/renderer/src/modals/SignInModal.vue index ff570d18..2c94db7a 100644 --- a/src/renderer/src/modals/SignInModal.vue +++ b/src/renderer/src/modals/SignInModal.vue @@ -157,6 +157,9 @@ async function registerDevice() { signInState.value = 'token'; hide(); + // Update user activity immediately after successful sign in + await apiClient.value.postUserActivity(); + // Emit the signed-in event emitter?.emit('signed-in'); } From a3a7f7ad789322773a738f8dec09d8c54604a4b0 Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Tue, 18 Feb 2025 23:17:19 -0500 Subject: [PATCH 3/3] fix: type annotation for `userActivityInterval` --- src/renderer/src/App.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index 12b53369..98066978 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -96,7 +96,7 @@ const updateUserActivity = async () => { }; // Update user activity periodically (every 6 hours) -let userActivityInterval: NodeJS.Timeout | null = null; +let userActivityInterval: ReturnType | null = null; const startUserActivityInterval = () => { if (userActivityInterval) {