From 4b56f7cd4b013a876d49c2b26ca113dd16285c3b Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 1 Nov 2026 08:59:18 -0700 Subject: [PATCH] fix: Tags keep the order they were entered in (v2.6.4) add_tags_to_note() rewrites note_tags in entry order on every save, so note_tags.id already records it; get_note_tags(entered_order=True) reads it back. Crossposts (Threads topic, which hashtags fit on Bluesky), the editor and Micropub q=source/update use it, so saving no longer reorders tags alphabetically. Public pages stay alphabetical. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +++ starpunk/__init__.py | 4 +- starpunk/micropub.py | 11 +++-- starpunk/routes/admin.py | 4 +- starpunk/syndication.py | 2 +- starpunk/tags.py | 17 +++++--- tests/test_micropub_v240.py | 2 +- tests/test_tag_order.py | 86 +++++++++++++++++++++++++++++++++++++ 8 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 tests/test_tag_order.py diff --git a/CHANGELOG.md b/CHANGELOG.md index d992f3d..02f4af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.6.4] - 2026-11-01 + +### Fixed + +- Tags keep the order you entered them in. The first tag is the note's main tag: it's the Threads topic, and Bluesky keeps the first tags when space runs out. Before, both used the alphabetically first tag. Public pages still list tags alphabetically +- Opening a note in the editor or updating it via Micropub reordered its tags alphabetically on save; the editor and Micropub `q=source` now show, and keep, the entered order + ## [2.6.3] - 2026-11-01 ### Changed diff --git a/starpunk/__init__.py b/starpunk/__init__.py index 361c3cc..3e3339f 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -464,5 +464,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.6.3" -__version_info__ = (2, 6, 3) +__version__ = "2.6.4" +__version_info__ = (2, 6, 4) diff --git a/starpunk/micropub.py b/starpunk/micropub.py index 5f3339e..f8f57d7 100644 --- a/starpunk/micropub.py +++ b/starpunk/micropub.py @@ -603,8 +603,10 @@ def handle_update(data: dict, token_info: dict): if set(add) - {"category", "photo"}: raise MicropubValidationError("Only category and photo can be added to") + from starpunk.tags import get_note_tags + title, body = split_title(note.content) - tags = [t["display_name"] for t in note.tags] + tags = [t["display_name"] for t in get_note_tags(note.id, entered_order=True)] photos = _note_photos(note) changes = {} @@ -717,8 +719,11 @@ def note_to_mf2(note, wanted: Optional[list] = None) -> dict: } if title: props["name"] = [title] - if note.tags: - props["category"] = [tag["display_name"] for tag in note.tags] + from starpunk.tags import get_note_tags + + tags = get_note_tags(note.id, entered_order=True) + if tags: + props["category"] = [tag["display_name"] for tag in tags] if getattr(note, "in_reply_to", None): props["in-reply-to"] = [note.in_reply_to] if getattr(note, "like_of", None): diff --git a/starpunk/routes/admin.py b/starpunk/routes/admin.py index 6d3e59f..575e51d 100644 --- a/starpunk/routes/admin.py +++ b/starpunk/routes/admin.py @@ -226,8 +226,8 @@ def edit_note_form(note_id: int): flash("Note not found", "error") return redirect(url_for("admin.dashboard")), 404 - # Pre-load tags for the edit form (v1.3.0 Phase 3) - tags = get_note_tags(note.id) + # Pre-load tags for the edit form (v1.3.0 Phase 3), as entered so saving keeps the order + tags = get_note_tags(note.id, entered_order=True) object.__setattr__(note, '_cached_tags', tags) from starpunk.syndication import get_note_syndications, get_targets diff --git a/starpunk/syndication.py b/starpunk/syndication.py index 6498208..5f71f7a 100644 --- a/starpunk/syndication.py +++ b/starpunk/syndication.py @@ -1239,7 +1239,7 @@ def _reconcile(row, target: Target) -> tuple: post = Post( note=note, permalink=f"{site}/note/{note.slug}", - tags=[t["display_name"] for t in get_note_tags(note.id)], + tags=[t["display_name"] for t in get_note_tags(note.id, entered_order=True)], max_chars=target.max_chars, media=_media_files(note.id, target.max_media) if note.kind == "note" else [], kind=note.kind, diff --git a/starpunk/tags.py b/starpunk/tags.py index bbd1e23..ed3b64b 100644 --- a/starpunk/tags.py +++ b/starpunk/tags.py @@ -115,27 +115,32 @@ def add_tags_to_note(note_id: int, tags: list[str]) -> None: db.commit() -def get_note_tags(note_id: int) -> list[dict]: +def get_note_tags(note_id: int, entered_order: bool = False) -> list[dict]: """ Get all tags for a note Args: note_id: Note database ID + entered_order: In the order they were entered (the first is the + note's main tag, e.g. its Threads topic) instead of alphabetical. + add_tags_to_note() rewrites the links in entry order on every + save, so note_tags.id records it. Use this wherever the tags are + saved back (editor, Micropub update) so the order survives. Returns: List of tag dicts with 'name' and 'display_name' - Ordered alphabetically by display_name (case-insensitive) - - Query should use: ORDER BY LOWER(tags.display_name) ASC + Ordered alphabetically by display_name (case-insensitive), unless + entered_order """ db = get_db(current_app) + order = "note_tags.id" if entered_order else "LOWER(tags.display_name)" tags = db.execute( - """ + f""" SELECT tags.name, tags.display_name FROM tags JOIN note_tags ON tags.id = note_tags.tag_id WHERE note_tags.note_id = ? - ORDER BY LOWER(tags.display_name) ASC + ORDER BY {order} ASC """, (note_id,) ).fetchall() diff --git a/tests/test_micropub_v240.py b/tests/test_micropub_v240.py index 6f06f6f..f370b17 100644 --- a/tests/test_micropub_v240.py +++ b/tests/test_micropub_v240.py @@ -118,7 +118,7 @@ class TestUpdate: def test_categories(self, app, client, h): url = create(client, h, category=["one", "two"]) self.update(client, h, url, add={"category": ["three", "One"]}) - assert source(client, h, url)["properties"]["category"] == ["one", "three", "two"] + assert source(client, h, url)["properties"]["category"] == ["one", "two", "three"] # entered order (v2.6.4) self.update(client, h, url, delete={"category": ["two"]}) assert source(client, h, url)["properties"]["category"] == ["one", "three"] self.update(client, h, url, delete=["category"]) diff --git a/tests/test_tag_order.py b/tests/test_tag_order.py new file mode 100644 index 0000000..005264b --- /dev/null +++ b/tests/test_tag_order.py @@ -0,0 +1,86 @@ +""" +Tags keep the order they were entered in (v2.6.4) + +Public pages list tags alphabetically; crossposts (the Threads topic, which +tags fit on Bluesky), the editor and Micropub use the entered order, and +saving from the editor or Micropub keeps it. +""" + +import pytest + +from starpunk import create_app +from starpunk.auth import create_session +from starpunk.indieauth import create_access_token +from starpunk.notes import create_note, get_note +from starpunk.tags import get_note_tags + +ENTERED = ["Zebra", "apple", "Mango"] + + +@pytest.fixture +def app(tmp_path): + data = tmp_path / "data" + data.mkdir() + yield create_app(config={ + "TESTING": True, "DATABASE_PATH": data / "starpunk.db", "DATA_PATH": data, + "NOTES_PATH": data / "notes", "SESSION_SECRET": "s", "ADMIN_ME": "https://test.example.com", + "SITE_URL": "http://localhost:5000", "DEV_MODE": False, + }) + + +@pytest.fixture +def note(app): + with app.test_request_context(): + return create_note(content="Tagged", published=True, tags=ENTERED) + + +def names(app, note_id, **kw): + with app.app_context(): + return [t["display_name"] for t in get_note_tags(note_id, **kw)] + + +def test_alphabetical_by_default_entered_on_request(app, note): + assert names(app, note.id) == ["apple", "Mango", "Zebra"] + assert names(app, note.id, entered_order=True) == ENTERED + + +def test_public_page_stays_alphabetical(app, note): + html = app.test_client().get(f"/note/{note.slug}").data.decode() + assert html.index(">apple<") < html.index(">Mango<") < html.index(">Zebra<") + + +def test_editor_shows_and_keeps_entered_order(app, note): + client = app.test_client() + with app.test_request_context(): + client.set_cookie("starpunk_session", create_session("https://test.example.com")) + html = client.get(f"/admin/edit/{note.id}").data.decode() + assert 'value="Zebra, apple, Mango"' in html + # Saving the form unchanged keeps the order + response = client.post(f"/admin/edit/{note.id}", data={ + "content": "Tagged, edited", "published": "on", "tags": "Zebra, apple, Mango"}) + assert response.status_code in (200, 302) + assert names(app, note.id, entered_order=True) == ENTERED + + +def test_micropub_source_and_update_keep_entered_order(app, note): + with app.test_request_context(): + token = create_access_token("https://quill.example/", "create update")["access_token"] + client = app.test_client() + auth = {"Authorization": f"Bearer {token}"} + url = f"http://localhost:5000/note/{note.slug}" + source = client.get(f"/micropub?q=source&url={url}", headers=auth).get_json() + assert source["properties"]["category"] == ENTERED + response = client.post("/micropub", headers=auth, json={ + "action": "update", "url": url, "add": {"category": ["Kiwi"]}}) + assert response.status_code in (200, 204), response.get_data(as_text=True) + assert names(app, note.id, entered_order=True) == ENTERED + ["Kiwi"] + + +def test_crossposts_use_entered_order(app, note): + from starpunk.syndication import compose_text, threads_topic + tags = names(app, note.id, entered_order=True) + assert threads_topic(tags) == "Zebra" + with app.app_context(): + text = compose_text(get_note(id=note.id, load_content=True), + "https://x.example/note/1", tags, 25) + assert "#Zebra" in text and "#Mango" not in text # room for the first tags only