Wrap list chips, add list sharing and public/unlisted/private visibility
- Home page list chips wrap to new rows instead of scrolling off the edge - Lists are public, unlisted or private (migrated from the visible flag) - Unlisted lists are reachable only at /s/<token>; slug URLs are public-only - Share button on list pages and in the editor/dashboard; owners can reset an unlisted link Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
268af7b9ed
commit
87e3d1fb30
16 files changed
+292
-28
No files matched your search
+62
-8
@@ -15,6 +15,26 @@ const (
|
||||
SoftListSizeWarning = 1000
|
||||
)
|
||||
|
||||
// List visibility. Public lists show on the home page; unlisted ones are
|
||||
// only reachable through their share link; private ones only by the owner.
|
||||
const (
|
||||
VisibilityPublic = "public"
|
||||
VisibilityUnlisted = "unlisted"
|
||||
VisibilityPrivate = "private"
|
||||
)
|
||||
|
||||
// SharePath is the address to send people: the slug for public lists, the
|
||||
// share token for unlisted ones. Private lists have none.
|
||||
func (l List) SharePath() string {
|
||||
switch l.Visibility {
|
||||
case VisibilityPublic:
|
||||
return "/l/" + l.Slug
|
||||
case VisibilityUnlisted:
|
||||
return "/s/" + l.ShareToken
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type List struct {
|
||||
ID int64 `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
@@ -23,19 +43,21 @@ type List struct {
|
||||
Description string `json:"description"`
|
||||
MaxItems int `json:"maxItems"`
|
||||
Position int `json:"position"`
|
||||
Visible bool `json:"visible"`
|
||||
Visible bool `json:"visible"` // Visibility == "public"; kept for older clients
|
||||
Visibility string `json:"visibility"` // "public", "unlisted" or "private"
|
||||
ShareToken string `json:"shareToken"`
|
||||
Layout string `json:"layout"` // a ListStyles key, e.g. "podium"
|
||||
Art string `json:"art"` // "", "poster" or "square"; "" = category default
|
||||
ItemCount int `json:"itemCount"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
|
||||
const listColumns = `l.id, l.slug, l.title, l.category, l.description, l.max_items, l.position, l.visible, l.layout, l.art, l.updated_at,
|
||||
const listColumns = `l.id, l.slug, l.title, l.category, l.description, l.max_items, l.position, l.visible, l.visibility, l.share_token, l.layout, l.art, l.updated_at,
|
||||
(SELECT COUNT(*) FROM items i WHERE i.list_id = l.id)`
|
||||
|
||||
func scanList(row interface{ Scan(...any) error }) (List, error) {
|
||||
var l List
|
||||
err := row.Scan(&l.ID, &l.Slug, &l.Title, &l.Category, &l.Description, &l.MaxItems, &l.Position, &l.Visible, &l.Layout, &l.Art, &l.UpdatedAt, &l.ItemCount)
|
||||
err := row.Scan(&l.ID, &l.Slug, &l.Title, &l.Category, &l.Description, &l.MaxItems, &l.Position, &l.Visible, &l.Visibility, &l.ShareToken, &l.Layout, &l.Art, &l.UpdatedAt, &l.ItemCount)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return l, ErrNotFound
|
||||
}
|
||||
@@ -68,6 +90,13 @@ func (s *Store) ListByID(ctx context.Context, id int64) (List, error) {
|
||||
return scanList(s.db.QueryRowContext(ctx, `SELECT `+listColumns+` FROM lists l WHERE l.id = ?`, id))
|
||||
}
|
||||
|
||||
func (s *Store) ListByShareToken(ctx context.Context, token string) (List, error) {
|
||||
if token == "" {
|
||||
return List{}, ErrNotFound
|
||||
}
|
||||
return scanList(s.db.QueryRowContext(ctx, `SELECT `+listColumns+` FROM lists l WHERE l.share_token = ?`, token))
|
||||
}
|
||||
|
||||
func (s *Store) ListBySlug(ctx context.Context, slug string) (List, error) {
|
||||
return scanList(s.db.QueryRowContext(ctx, `SELECT `+listColumns+` FROM lists l WHERE l.slug = ?`, slug))
|
||||
}
|
||||
@@ -103,6 +132,18 @@ func (l *List) normalize() error {
|
||||
if l.Art != "poster" && l.Art != "square" {
|
||||
l.Art = ""
|
||||
}
|
||||
switch l.Visibility {
|
||||
case VisibilityPublic, VisibilityUnlisted, VisibilityPrivate:
|
||||
case "":
|
||||
// Older clients only send the visible flag.
|
||||
l.Visibility = VisibilityPrivate
|
||||
if l.Visible {
|
||||
l.Visibility = VisibilityPublic
|
||||
}
|
||||
default:
|
||||
return invalid("Unknown visibility %q", l.Visibility)
|
||||
}
|
||||
l.Visible = l.Visibility == VisibilityPublic
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -152,9 +193,9 @@ func (s *Store) CreateList(ctx context.Context, l List) (List, error) {
|
||||
if err != nil {
|
||||
return l, err
|
||||
}
|
||||
res, err := s.db.ExecContext(ctx, `INSERT INTO lists (slug, title, category, description, max_items, position, visible, layout, art)
|
||||
VALUES (?, ?, ?, ?, ?, (SELECT COALESCE(MAX(position), 0) + 1 FROM lists), ?, ?, ?)`,
|
||||
slug, l.Title, l.Category, l.Description, l.MaxItems, l.Visible, l.Layout, l.Art)
|
||||
res, err := s.db.ExecContext(ctx, `INSERT INTO lists (slug, title, category, description, max_items, position, visible, visibility, share_token, layout, art)
|
||||
VALUES (?, ?, ?, ?, ?, (SELECT COALESCE(MAX(position), 0) + 1 FROM lists), ?, ?, lower(hex(randomblob(9))), ?, ?)`,
|
||||
slug, l.Title, l.Category, l.Description, l.MaxItems, l.Visible, l.Visibility, l.Layout, l.Art)
|
||||
if err != nil {
|
||||
return l, err
|
||||
}
|
||||
@@ -187,14 +228,27 @@ func (s *Store) UpdateList(ctx context.Context, l List) (List, error) {
|
||||
return l, err
|
||||
}
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE lists SET slug=?, title=?, category=?, description=?, max_items=?, visible=?, layout=?, art=?, updated_at=unixepoch() WHERE id=?`,
|
||||
slug, l.Title, l.Category, l.Description, l.MaxItems, l.Visible, l.Layout, l.Art, l.ID)
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE lists SET slug=?, title=?, category=?, description=?, max_items=?, visible=?, visibility=?, layout=?, art=?, updated_at=unixepoch() WHERE id=?`,
|
||||
slug, l.Title, l.Category, l.Description, l.MaxItems, l.Visible, l.Visibility, l.Layout, l.Art, l.ID)
|
||||
if err != nil {
|
||||
return l, err
|
||||
}
|
||||
return s.ListByID(ctx, l.ID)
|
||||
}
|
||||
|
||||
// ResetShareToken gives the list a new share link, so the old one stops
|
||||
// working.
|
||||
func (s *Store) ResetShareToken(ctx context.Context, id int64) (List, error) {
|
||||
res, err := s.db.ExecContext(ctx, `UPDATE lists SET share_token = lower(hex(randomblob(9))) WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return List{}, err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return List{}, ErrNotFound
|
||||
}
|
||||
return s.ListByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteList(ctx context.Context, id int64) error {
|
||||
res, err := s.db.ExecContext(ctx, `DELETE FROM lists WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
|
||||
@@ -117,6 +117,13 @@ CREATE INDEX items_list_rank ON items(list_id, rank);
|
||||
// rather than taking the source's; refresh leaves it alone unless asked.
|
||||
`ALTER TABLE items ADD COLUMN image_custom INTEGER NOT NULL DEFAULT 0;
|
||||
UPDATE items SET image_custom = 1 WHERE image_source = 'upload';`,
|
||||
// Public / unlisted / private, replacing the visible flag (kept in sync
|
||||
// for older code paths), plus a random token for unlisted share links.
|
||||
`ALTER TABLE lists ADD COLUMN visibility TEXT NOT NULL DEFAULT 'public';
|
||||
UPDATE lists SET visibility = 'private' WHERE visible = 0;
|
||||
ALTER TABLE lists ADD COLUMN share_token TEXT NOT NULL DEFAULT '';
|
||||
UPDATE lists SET share_token = lower(hex(randomblob(9)));
|
||||
CREATE UNIQUE INDEX lists_share_token ON lists(share_token);`,
|
||||
}
|
||||
|
||||
func (s *Store) migrate(ctx context.Context) error {
|
||||
|
||||
@@ -334,3 +334,46 @@ func TestPlaces(t *testing.T) {
|
||||
}
|
||||
check("close gaps", "D,H,A,F,B,G")
|
||||
}
|
||||
|
||||
func TestVisibility(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestStore(t)
|
||||
// Old clients send only the visible flag.
|
||||
pub, err := s.CreateList(ctx, List{Title: "Pub", Category: "movie", MaxItems: 5, Visible: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pub.Visibility != VisibilityPublic || !pub.Visible || pub.ShareToken == "" {
|
||||
t.Fatalf("public: %+v", pub)
|
||||
}
|
||||
un, err := s.CreateList(ctx, List{Title: "Un", Category: "movie", MaxItems: 5, Visibility: VisibilityUnlisted})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if un.Visible || un.SharePath() != "/s/"+un.ShareToken || un.ShareToken == pub.ShareToken {
|
||||
t.Fatalf("unlisted: %+v", un)
|
||||
}
|
||||
if _, err := s.CreateList(ctx, List{Title: "X", Category: "movie", MaxItems: 5, Visibility: "secret"}); err == nil {
|
||||
t.Fatal("bad visibility accepted")
|
||||
}
|
||||
got, err := s.ListByShareToken(ctx, un.ShareToken)
|
||||
if err != nil || got.ID != un.ID {
|
||||
t.Fatalf("by token: %v %+v", err, got)
|
||||
}
|
||||
lists, _ := s.Lists(ctx, false)
|
||||
if len(lists) != 1 || lists[0].ID != pub.ID {
|
||||
t.Fatalf("home lists: %+v", lists)
|
||||
}
|
||||
old := un.ShareToken
|
||||
un, err = s.ResetShareToken(ctx, un.ID)
|
||||
if err != nil || un.ShareToken == old {
|
||||
t.Fatalf("reset: %v %q", err, un.ShareToken)
|
||||
}
|
||||
if _, err := s.ListByShareToken(ctx, old); err != ErrNotFound {
|
||||
t.Fatalf("old token still works: %v", err)
|
||||
}
|
||||
un.Visibility = VisibilityPrivate
|
||||
if un, err = s.UpdateList(ctx, un); err != nil || un.SharePath() != "" {
|
||||
t.Fatalf("private: %v %+v", err, un)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user