Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Consider tokenless users in inactivity activity calculation

Neeraj Guptaf1c4f6ad5654
diff --git a/server/pkg/controller/user/inactive_user_orchestrator.go b/server/pkg/controller/user/inactive_user_orchestrator.go
index 8a9cd9b9d8..7e0d773f76 100644
--- a/server/pkg/controller/user/inactive_user_orchestrator.go
+++ b/server/pkg/controller/user/inactive_user_orchestrator.go
@@ -180,12 +180,12 @@ func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivit
return false, nil
}
- lastActivity, found, err := c.UserRepo.GetLatestTokenActivity(user.ID)
+ lastActivity, found, err := c.UserRepo.GetLatestActivity(user.ID)
if err != nil {
return false, err
}
if !found {
- // Tokens are the source of truth for this workflow; skip if activity is no longer available.
+ // User is no longer active.
return false, nil
}
@@ -217,7 +217,7 @@ func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivit
// Re-check right before deletion to avoid deleting users who became active
// after earlier reads in long processing runs.
- latestActivity, latestFound, err := c.UserRepo.GetLatestTokenActivity(user.ID)
+ latestActivity, latestFound, err := c.UserRepo.GetLatestActivity(user.ID)
if err != nil {
return false, err
}
diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go
index a5d107470e..8b43311953 100644
--- a/server/pkg/repo/user.go
+++ b/server/pkg/repo/user.go
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
+ "errors"
"fmt"
"strings"
@@ -135,27 +136,54 @@ func (repo *UserRepository) GetAll(sinceTime int64, tillTime int64) ([]ente.User
return users, nil
}
-// GetActiveUsersByLastActivityBefore returns active users whose latest token
-// activity is older than or equal to beforeTime. Only users having at least one
-// token row are considered. Paging is done by user_id.
-//
-// TODO(inactive-user-deletion): Tokens are not a durable activity source.
-// `RemoveDeletedTokens` prunes logged-out sessions after 30 days, which can make
-// last_activity stale or missing for users who logged out from all devices.
-// Move inactivity selection to a durable, app-wide last-activity signal.
+// GetActiveUsersByLastActivityBefore returns active users whose effective last
+// activity is older than or equal to beforeTime. Effective activity uses latest
+// token activity when present, otherwise falls back to max(users.creation_time,
+// authenticator_entity.updated_at, collections.updation_time). Paging is done
+// by user_id.
func (repo *UserRepository) GetActiveUsersByLastActivityBefore(beforeTime int64, afterUserID int64, limit int) ([]UserInactivityCandidate, error) {
rows, err := repo.DB.Query(`
SELECT
- u.user_id,
- MAX(t.last_used_at) AS last_activity
- FROM users u
- INNER JOIN tokens t
- ON t.user_id = u.user_id
- WHERE u.encrypted_email IS NOT NULL
- AND u.user_id > $2
- GROUP BY u.user_id
- HAVING MAX(t.last_used_at) <= $1
- ORDER BY u.user_id
+ candidate.user_id,
+ candidate.last_activity
+ FROM (
+ SELECT
+ u.user_id,
+ COALESCE(
+ t.last_token_activity,
+ GREATEST(
+ u.creation_time,
+ COALESCE(a.last_auth_activity, 0),
+ COALESCE(c.last_collection_activity, 0)
+ )
+ ) AS last_activity
+ FROM users u
+ LEFT JOIN LATERAL (
+ SELECT t.last_used_at AS last_token_activity
+ FROM tokens t
+ WHERE t.user_id = u.user_id
+ ORDER BY t.last_used_at DESC
+ LIMIT 1
+ ) t ON TRUE
+ LEFT JOIN LATERAL (
+ SELECT ae.updated_at AS last_auth_activity
+ FROM authenticator_entity ae
+ WHERE ae.user_id = u.user_id
+ ORDER BY ae.updated_at DESC
+ LIMIT 1
+ ) a ON TRUE
+ LEFT JOIN LATERAL (
+ SELECT c.updation_time AS last_collection_activity
+ FROM collections c
+ WHERE c.owner_id = u.user_id
+ ORDER BY c.updation_time DESC
+ LIMIT 1
+ ) c ON TRUE
+ WHERE u.encrypted_email IS NOT NULL
+ AND u.user_id > $2
+ ) candidate
+ WHERE candidate.last_activity <= $1
+ ORDER BY candidate.user_id
LIMIT $3`, beforeTime, afterUserID, limit)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to fetch inactive users")
@@ -176,18 +204,52 @@ func (repo *UserRepository) GetActiveUsersByLastActivityBefore(beforeTime int64,
return result, nil
}
-// GetLatestTokenActivity returns the latest known token activity for a user.
-// The second return value is false when no token row exists for the user.
-func (repo *UserRepository) GetLatestTokenActivity(userID int64) (int64, bool, error) {
- var lastActivity sql.NullInt64
- err := repo.DB.QueryRow(`SELECT MAX(last_used_at) FROM tokens WHERE user_id = $1`, userID).Scan(&lastActivity)
+// GetLatestActivity returns the latest effective activity for a user.
+// The second return value is false when the user is no longer active.
+func (repo *UserRepository) GetLatestActivity(userID int64) (int64, bool, error) {
+ var lastActivity int64
+ err := repo.DB.QueryRow(`
+ SELECT
+ COALESCE(
+ t.last_token_activity,
+ GREATEST(
+ u.creation_time,
+ COALESCE(a.last_auth_activity, 0),
+ COALESCE(c.last_collection_activity, 0)
+ )
+ ) AS last_activity
+ FROM users u
+ LEFT JOIN LATERAL (
+ SELECT t.last_used_at AS last_token_activity
+ FROM tokens t
+ WHERE t.user_id = u.user_id
+ ORDER BY t.last_used_at DESC
+ LIMIT 1
+ ) t ON TRUE
+ LEFT JOIN LATERAL (
+ SELECT ae.updated_at AS last_auth_activity
+ FROM authenticator_entity ae
+ WHERE ae.user_id = u.user_id
+ ORDER BY ae.updated_at DESC
+ LIMIT 1
+ ) a ON TRUE
+ LEFT JOIN LATERAL (
+ SELECT c.updation_time AS last_collection_activity
+ FROM collections c
+ WHERE c.owner_id = u.user_id
+ ORDER BY c.updation_time DESC
+ LIMIT 1
+ ) c ON TRUE
+ WHERE u.user_id = $1
+ AND u.encrypted_email IS NOT NULL
+ `, userID).Scan(&lastActivity)
if err != nil {
- return 0, false, stacktrace.Propagate(err, "failed to fetch latest token activity")
- }
- if !lastActivity.Valid {
- return 0, false, nil
+ if errors.Is(err, sql.ErrNoRows) {
+ return 0, false, nil
+ }
+ return 0, false, stacktrace.Propagate(err, "failed to fetch latest user activity")
}
- return lastActivity.Int64, true, nil
+ return lastActivity, true, nil
}
// GetUserUsageWithSubData will return current storage usage & basic information about subscription for given list