feat(sync): paginate Android sync with change tokens
This commit is contained in:
1 parent
0ff94f9b10
commit
97c237be80
76 files changed
+3401
-851
No files matched your search
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
errListAccessDenied = errors.New("list access denied")
|
||||
errCellIdentityMismatch = errors.New("item and field belong to different lists")
|
||||
)
|
||||
|
||||
type rowQueryer interface {
|
||||
QueryRow(context.Context, string, ...any) pgx.Row
|
||||
}
|
||||
|
||||
func hasListMembership(ctx context.Context, queryer rowQueryer, userID int64, listID string) (bool, error) {
|
||||
var member bool
|
||||
err := queryer.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM list_memberships WHERE listid = $1 AND user_id = $2)`, listID, userID).Scan(&member)
|
||||
return member, err
|
||||
}
|
||||
|
||||
func authorizeList(ctx context.Context, queryer rowQueryer, userID int64, listID string, allowCreate bool) (bool, error) {
|
||||
if userID <= 0 || listID == "" {
|
||||
return false, errListAccessDenied
|
||||
}
|
||||
member, err := hasListMembership(ctx, queryer, userID, listID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if member {
|
||||
return true, nil
|
||||
}
|
||||
var exists bool
|
||||
if err := queryer.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM lists WHERE id = $1)`, listID).Scan(&exists); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if exists || !allowCreate {
|
||||
return false, errListAccessDenied
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func addListOwner(ctx context.Context, tx pgx.Tx, userID int64, listID string) error {
|
||||
_, err := tx.Exec(ctx, `INSERT INTO list_memberships (listid, user_id, is_owner) VALUES ($1, $2, TRUE) ON CONFLICT (listid, user_id) DO NOTHING`, listID, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) requireListMembership(w http.ResponseWriter, r *http.Request, listID string) bool {
|
||||
member, err := hasListMembership(r.Context(), s.db.pool, requestUserID(r), listID)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to authorize list")
|
||||
return false
|
||||
}
|
||||
if !member {
|
||||
writeJSONError(w, http.StatusNotFound, "List not found")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user