Remove duplicate entries before sending back to the user to save bandwidth for the client

This commit is contained in:
David Dworken 2024-08-25 12:00:52 -07:00
parent 3875bddddc
commit 05c71b2f21
No known key found for this signature in database
2 changed files with 13 additions and 2 deletions

View File

@ -27,7 +27,19 @@ func (db *DB) AllHistoryEntriesForUser(ctx context.Context, userID string) ([]*s
return nil, fmt.Errorf("tx.Error: %w", tx.Error) return nil, fmt.Errorf("tx.Error: %w", tx.Error)
} }
return historyEntries, nil // Remove duplicate entries using EncryptedId as the key to save bandwidth when sending data to the client
uniqueEntries := make(map[string]*shared.EncHistoryEntry)
for _, entry := range historyEntries {
uniqueEntries[entry.EncryptedId] = entry
}
// Convert the map back to a slice
dedupedEntries := make([]*shared.EncHistoryEntry, 0, len(uniqueEntries))
for _, entry := range uniqueEntries {
dedupedEntries = append(dedupedEntries, entry)
}
return dedupedEntries, nil
} }
func (db *DB) HistoryEntriesForDevice(ctx context.Context, deviceID string, limit int) ([]*shared.EncHistoryEntry, error) { func (db *DB) HistoryEntriesForDevice(ctx context.Context, deviceID string, limit int) ([]*shared.EncHistoryEntry, error) {

View File

@ -74,7 +74,6 @@ func (s *Server) apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) apiBootstrapHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) apiBootstrapHandler(w http.ResponseWriter, r *http.Request) {
// TODO: Update this to filter out duplicate entries
userId := getRequiredQueryParam(r, "user_id") userId := getRequiredQueryParam(r, "user_id")
deviceId := getRequiredQueryParam(r, "device_id") deviceId := getRequiredQueryParam(r, "device_id")
version := getHishtoryVersion(r) version := getHishtoryVersion(r)