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

if logged in, update user activity #403

Merged
merged 3 commits into from
Feb 19, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/cyd-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean | APIErrorResponse> {
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?")
}
}
}
35 changes: 35 additions & 0 deletions src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: ReturnType<typeof setTimeout> | 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);
Expand All @@ -98,6 +127,12 @@ onMounted(async () => {
isSignedIn.value = true;
}

// If logged in, update the server with user activity (gets truncated to day)
if (isSignedIn.value) {
await updateUserActivity();
startUserActivityInterval();
}

isReady.value = true;

// Change the app title
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/modals/SignInModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down