Backend : token_version (000022, claim tv, 401 si révoqué, bump au password — fin limite V1), quota (000023 idempotente, colonne héritée de 000001, 413 QUOTA_EXCEEDED, GET /users/me/usage), devices (GET + DELETE révocation, EnforceDeviceExists en prod), renames concurrents transparents (baseUpdatedAt + conflicts[] non-fataux, tolérance 2 s). Tests service + handlers + migrations + auth. Mobile : pipeline conflits (DTO + SyncConflictStore + worker + dialogue), baseUpdatedAt prêt, liens 7j par défaut, écran Appareils (+ quota), import classeur unifié (scan/note/dossier), onboarding cercle Famille. Contrat docs/api-v1.md, V3.md (S6 livré).
113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
)
|
|
|
|
// Devices persists registered devices (idempotence outbox par device, dernier
|
|
// user_id informatif).
|
|
type Devices struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
// Upsert registers the device if absent and refreshes last_seen_at.
|
|
func (d *Devices) Upsert(deviceID string) error {
|
|
_, err := d.DB.Exec(
|
|
`INSERT INTO devices (device_id, registered_at, last_seen_at)
|
|
VALUES ($1, NOW(), NOW())
|
|
ON CONFLICT (device_id) DO UPDATE SET last_seen_at = NOW()`,
|
|
deviceID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
// Exists reports whether a device has been registered.
|
|
func (d *Devices) Exists(deviceID string) (bool, error) {
|
|
var exists int
|
|
err := d.DB.QueryRow(`SELECT 1 FROM devices WHERE device_id = $1`, deviceID).Scan(&exists)
|
|
if err == sql.ErrNoRows {
|
|
return false, nil
|
|
}
|
|
return err == nil, err
|
|
}
|
|
|
|
// MarkUser mémorise le dernier utilisateur connecté sur ce device (INFORMATIF,
|
|
// jamais autorisant) et rafraîchit last_seen_at. Appelé au login.
|
|
// Device absent → ErrNotFound (le handler distingue la course
|
|
// Exists→MarkUser au lieu d'émettre un token pour un device fantôme).
|
|
func (d *Devices) MarkUser(deviceID, userID string) error {
|
|
result, err := d.DB.Exec(
|
|
`UPDATE devices SET user_id = $2, last_seen_at = NOW() WHERE device_id = $1`,
|
|
deviceID, userID,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
affected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affected == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeviceRow is one registered device of an account (A1 — voir / révoquer).
|
|
type DeviceRow struct {
|
|
DeviceID string
|
|
LastSeenAt *int64 // ms epoch, NULL si jamais vu depuis l'enregistrement
|
|
}
|
|
|
|
// ListByUser returns the devices whose last login was this account
|
|
// (devices.user_id informatif, jamais autorisant).
|
|
func (d *Devices) ListByUser(userID string) ([]DeviceRow, error) {
|
|
rows, err := d.DB.Query(
|
|
`SELECT device_id, (EXTRACT(EPOCH FROM last_seen_at) * 1000)::bigint
|
|
FROM devices WHERE user_id = $1 ORDER BY last_seen_at DESC NULLS LAST`,
|
|
userID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []DeviceRow{}
|
|
for rows.Next() {
|
|
var row DeviceRow
|
|
if err := rows.Scan(&row.DeviceID, &row.LastSeenAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, row)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// Revoke deletes a device registration of this account (A1 — téléphone
|
|
// perdu). Avec EnforceDeviceExists, les tokens du device deviennent 401 et
|
|
// le login refuse le device (ré-enregistrement explicite requis).
|
|
// Ligne absente ou d'un autre compte → ErrNotFound (pas d'énumération).
|
|
// Les hints de présence (`storage_locations`) suivent.
|
|
func (d *Devices) Revoke(userID, deviceID string) error {
|
|
result, err := d.DB.Exec(
|
|
`DELETE FROM devices WHERE device_id = $1 AND user_id = $2`,
|
|
deviceID, userID,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
affected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affected == 0 {
|
|
return ErrNotFound
|
|
}
|
|
if _, err := d.DB.Exec(
|
|
`DELETE FROM storage_locations WHERE device_id = $1 AND user_id = $2`,
|
|
deviceID, userID,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|