Skip to content

Commit

Permalink
Merge pull request #403 from lockdown-systems/user-activity-endpoint
Browse files Browse the repository at this point in the history
if logged in, update user activity
  • Loading branch information
micahflee authored Feb 19, 2025
2 parents bab4c53 + a3a7f7a commit eefb6d5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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

0 comments on commit eefb6d5

Please sign in to comment.