Compare commits
42
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43ec7cb8be | ||
|
|
4aaa7a8122 | ||
|
|
0cfc9965df | ||
|
|
106495d114 | ||
|
|
3c1e109358 | ||
|
|
d43361d505 | ||
|
|
f924413934 | ||
|
|
4b2ae4c7fc | ||
|
|
d3148063e3 | ||
|
|
39c8259f3f | ||
|
|
1ce88b38a5 | ||
|
|
4ffe36f9d3 | ||
|
|
a551770c76 | ||
|
|
d0bfc80c2e | ||
|
|
d16463ef8a | ||
|
|
4b56f7cd4b | ||
|
|
2474628646 | ||
|
|
0fe341161e | ||
|
|
a62077ab30 | ||
|
|
fade75fd2d | ||
|
|
0dd59fbc18 | ||
|
|
11e09a0d02 | ||
|
|
565adaeecf | ||
|
|
53d10ed271 | ||
|
|
e8c1b03980 | ||
|
|
0d3a5cd519 | ||
|
|
88088e10e1 | ||
|
|
391bdb9db3 | ||
|
|
c51cf726b0 | ||
|
|
2616ef5060 | ||
|
|
0ae16bd1fc | ||
|
|
cc09098741 | ||
|
|
16925663a3 | ||
|
|
e92c27a84c | ||
|
|
ed63e70c46 | ||
|
|
57b02ad6c7 | ||
|
|
94e8c672df | ||
|
|
011f7cc87d | ||
|
|
f46130569f | ||
|
|
b4e6cf3cc3 | ||
|
|
2d0568d9c2 | ||
|
|
f117913797 |
No files matched your search
@@ -50,6 +50,27 @@ SESSION_LIFETIME=30
|
||||
# Lifetime of Micropub access tokens StarPunk issues, in days (default: 90)
|
||||
INDIEAUTH_TOKEN_LIFETIME_DAYS=90
|
||||
|
||||
# =============================================================================
|
||||
# CROSSPOSTING (optional, v2.1.0)
|
||||
# =============================================================================
|
||||
|
||||
# GoToSocial or Mastodon account to copy new notes to. Create an app token
|
||||
# with scopes: write:statuses write:media
|
||||
GOTOSOCIAL_URL=
|
||||
GOTOSOCIAL_ACCESS_TOKEN=
|
||||
# Name shown in the editor and Micropub apps (default: GoToSocial)
|
||||
GOTOSOCIAL_NAME=GoToSocial
|
||||
# public, unlisted, private (default: public)
|
||||
GOTOSOCIAL_VISIBILITY=public
|
||||
|
||||
# Threads account (v2.6.0). A long-lived user token with threads_basic,
|
||||
# threads_content_publish and threads_delete; StarPunk refreshes it daily and
|
||||
# keeps the current one in DATA_PATH/threads_token.json.
|
||||
# How to get one: docs/design/v2.6.0/2026-10-31-threads-crossposting.md
|
||||
THREADS_ACCESS_TOKEN=
|
||||
# Your Threads username (for the Micropub syndicate-to uid)
|
||||
THREADS_USERNAME=
|
||||
|
||||
# =============================================================================
|
||||
# DATA STORAGE
|
||||
# =============================================================================
|
||||
|
||||
@@ -1,58 +1,42 @@
|
||||
# Gitea Actions workflow for StarPunk
|
||||
# Builds and pushes container images on version tags
|
||||
|
||||
# Builds and pushes container images on version tags.
|
||||
#
|
||||
# The runner has no Docker daemon (that would give workflows root on the
|
||||
# server). Images are built by the rootless BuildKit container next to it,
|
||||
# reached through its socket.
|
||||
name: Build Container
|
||||
|
||||
on:
|
||||
# Trigger on version tags
|
||||
push:
|
||||
tags:
|
||||
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||
# Allow manual trigger from Gitea UI
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
REGISTRY: git.thesatelliteoflove.com
|
||||
BUILDKIT: unix:///run/buildkit/buildkit/buildkitd.sock
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
if command -v apk > /dev/null; then
|
||||
apk add --no-cache nodejs npm docker git
|
||||
elif command -v apt-get > /dev/null; then
|
||||
apt-get update && apt-get install -y nodejs npm docker.io git
|
||||
elif command -v yum > /dev/null; then
|
||||
yum install -y nodejs npm docker git
|
||||
fi
|
||||
|
||||
run: apk add --no-cache nodejs git docker-cli docker-cli-buildx
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Extract registry URL
|
||||
id: registry
|
||||
run: |
|
||||
# Extract hostname from server URL (remove protocol)
|
||||
REGISTRY_URL=$(echo "${{ github.server_url }}" | sed 's|https://||' | sed 's|http://||')
|
||||
echo "url=${REGISTRY_URL}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Login to Gitea Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ steps.registry.outputs.url }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
# The automatic Actions token can't log in to the container registry, so
|
||||
# this uses a personal access token with only the write:package scope,
|
||||
# stored as the repository secret REGISTRY_TOKEN.
|
||||
- name: Log in to the Gitea registry
|
||||
run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "$REGISTRY" -u "${{ github.repository_owner }}" --password-stdin
|
||||
- name: Use the BuildKit socket
|
||||
run: docker buildx create --name ci --driver remote "$BUILDKIT" --use
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Containerfile
|
||||
push: true
|
||||
tags: |
|
||||
${{ steps.registry.outputs.url }}/${{ github.repository }}:${{ github.ref_name }}
|
||||
${{ steps.registry.outputs.url }}/${{ github.repository }}:latest
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
run: |
|
||||
image="$REGISTRY/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')"
|
||||
docker buildx build --push -f Containerfile \
|
||||
-t "$image:${{ github.ref_name }}" -t "$image:latest" .
|
||||
- name: Clean up
|
||||
if: always()
|
||||
run: |
|
||||
docker buildx rm ci || true
|
||||
docker logout "$REGISTRY" || true
|
||||
+240
@@ -7,6 +7,246 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [2.7.0] - 2026-11-03
|
||||
|
||||
Webmention now follows the [W3C Recommendation](https://www.w3.org/TR/webmention/) throughout.
|
||||
|
||||
### Changed
|
||||
|
||||
- Webmentions are only accepted for published notes. Mentions of the home page or other pages were stored but never shown; they're now refused with 400
|
||||
- Deleted notes answer 410 Gone with a "This post has been deleted" page instead of 404
|
||||
- A received mention that is sent again keeps its moderation: one you hid stays hidden, one you approved stays approved
|
||||
|
||||
### Fixed
|
||||
|
||||
- Sending: endpoint discovery now handles `rel` values with several words (`rel="webmention other"`), any capitalisation, `rel` after other Link parameters, and several Link headers, and takes the first `<link>` or `<a>` in page order
|
||||
- Sending: the endpoint is looked up again every time (the 1–24 hour cache broke the spec's rule for updated posts)
|
||||
- Sending: editing a note notifies every page it mentioned before, including an old reply target and links the edit removed. Deleting or restoring a note notifies them too, so they can remove or restore the mention
|
||||
- Sending: a redirect from the endpoint is no longer treated as success (the POST was turned into a GET); "429 Too Many Requests" is retried
|
||||
- Receiving: the source must link to the exact target URL (in a link, image, or other HTML attribute, any JSON value, or plain text). A longer URL that starts with the target, or the target mentioned in plain text or an HTML comment, no longer counts
|
||||
- Receiving: relative links in the source are resolved, using the URL after redirects
|
||||
- Receiving: the source is fetched with an `Accept` header and decoded in its declared character set
|
||||
- Receiving: if the source can't be reached when a mention is sent again, the existing mention stays up instead of disappearing
|
||||
- Receiving: the target's host is compared without regard to case or a default port, and `#fragment`s are dropped, so one mention isn't stored twice
|
||||
- Mention snippets cut at 500 characters no longer leave HTML tags open, and the "Mentions" list no longer shows raw HTML tags
|
||||
- The `Link: rel="webmention"` header is added alongside other Link headers instead of replacing them
|
||||
- Links at the top level of a code block were still sent webmentions
|
||||
|
||||
### Security
|
||||
|
||||
- Fetches for webmentions check every redirect, not just the first URL, so a public page can't redirect StarPunk to a private address. Every address a hostname resolves to (IPv4 and IPv6) must be public; carrier-grade NAT, `0.0.0.0/8`, multicast, reserved and IPv4-mapped addresses are blocked too. Each fetch has a total time limit and a 1 MB size limit, including endpoint discovery
|
||||
|
||||
## [2.6.9] - 2026-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Received webmentions failed when the sender's profile photo had alt text (Bridgy, Mastodon and most IndieWeb sites do this). The photo is now stored as its URL
|
||||
- Outgoing webmentions were never sent: endpoint discovery crashed on every target that had an endpoint, and the target was then marked as having none. Discovery now works, and network errors during discovery are retried instead of giving up
|
||||
- Each server process ran its own webmention worker, so a webmention could be processed or sent more than once. Only one process handles the queues at a time now; if it stops, another takes over
|
||||
|
||||
### Security
|
||||
|
||||
- A webmention sender could set its author link to a `javascript:` URL, which was shown as a clickable link on the note page. Author links and photos are now only kept if they are http or https URLs
|
||||
|
||||
## [2.6.8] - 2026-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Webmentions were never processed. The background worker checked URLs against this site's settings without the app loaded, so every incoming webmention was marked "error" ("Working outside of application context") and every outgoing one kept failing and retrying. The worker now runs with the app loaded. A received webmention showed up in the admin list but never on the note
|
||||
- The webmention worker also stopped as soon as any request finished, so after startup it often wasn't running at all. It now runs for the life of the server process
|
||||
|
||||
## [2.6.7] - 2026-11-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- Admin pages were misaligned: the notes table ran past the right edge of the header and admin menu, because every page sat in the 40rem reading column and the table couldn't shrink to fit it. Admin pages now use a 60rem frame for the header, menu, content and footer alike, long post previews wrap, and on narrow screens tables scroll sideways instead of spilling out
|
||||
|
||||
## [2.6.6] - 2026-11-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- Crossposts left out photos that were embedded in the post text instead of attached. Some Micropub clients (OutPost, for one) upload a photo to the media endpoint and then put it in `content` as an image rather than sending it as `photo`; both are valid Micropub. Images in the text that are in this site's media library are now sent to GoToSocial, Bluesky and Threads like attached photos, with their alt text, after any attached ones and within each target's photo limit. GoToSocial's text no longer includes the image Markdown. Images hosted on other sites are still left out
|
||||
|
||||
## [2.6.5] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- A Micropub post that failed partway (for example, the same photo listed twice) answered "Failed to create post" but stayed published without its photos, so a client retrying would post it twice. Photos are now looked up before the post is made, and a post that fails after that is removed again
|
||||
- After such a failure, every request failed with "database is locked" for about 10 seconds: database connections now roll back unfinished writes when they're returned to the pool
|
||||
- The same photo listed twice in one post is shown once instead of failing the post
|
||||
- Replies didn't read as replies to IndieWeb readers and webmention receivers: on note pages `u-in-reply-to` sat inside an unrelated `h-cite`, and the home page didn't mark replies at all, so [Post Type Discovery](https://www.w3.org/TR/post-type-discovery/) saw notes (or photo posts). The reply context is now the entry's `u-in-reply-to h-cite`, and home page entries show "↩ In reply to"
|
||||
- Photos without alt text used "Image" as their description, which IndieWeb readers passed on as if you'd written it. They now use the post's text (up to 150 characters), and fall back to "Image" only when the post has no text
|
||||
- Home page entries always have an `e-content`, so a photo post without text never gets an implied name (which readers would show as its caption)
|
||||
|
||||
### Changed
|
||||
|
||||
- Photo posts no longer need text. Micropub doesn't require `content`, and `q=config` already offered a Photo post type with only `photo`. A Micropub update may also clear the text of a post that keeps its photos. Crossposts of a photo post without text start with its link, and its page is titled "Photo"
|
||||
|
||||
## [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
|
||||
|
||||
- **A note's own link gets the preview card** (ADR-070). When a note links somewhere (say, an article), the crosspost's card shows that page instead of the note. Bluesky cards are built from the page's title, description and image; Threads previews it from the text. Notes without links keep their own card
|
||||
|
||||
### Fixed
|
||||
|
||||
- Markdown links (`[text](url)`) lost their URL on Bluesky and Threads. On Bluesky the link text is now clickable; on Threads it's written "text (url)"
|
||||
- Notes with many links failed on Threads (Meta allows 5 per post); extra links now keep just their text
|
||||
|
||||
## [2.6.2] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- New notes failed their first Threads crosspost with "Invalid Link Attachment" (Meta rejects an explicit link preview for a page it hasn't fetched yet) and only went out on the retry a minute later. StarPunk no longer sends the explicit link preview; Threads still shows one, built from the note's link in the text
|
||||
|
||||
## [2.6.1] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- Threads crossposts gave up after Meta's publish step returned a one-off `HTTP 400 Fatal`; publish errors are now retried (a retry checks for an existing post first, so nothing is posted twice). Only a bad token or missing permission stops retries
|
||||
- Threads errors now include Meta's error code, subcode, user message and trace id, so the editor shows why a post failed
|
||||
- Wait a moment before the first readiness check on a Threads post, since Meta can report it ready too early
|
||||
|
||||
## [2.6.0] - 2026-11-01
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to Threads** (ADR-069): set `THREADS_ACCESS_TOKEN` (and optionally `THREADS_USERNAME`) to add a Threads target. Text posts get a link preview; 1 image posts as an image, 2–4 as a carousel, with alt text; the note's first tag becomes the post's topic. Shortened to 500 characters. Edits don't follow (the API can't edit); unpublish and delete remove the post. Likes aren't crossposted to Threads; reposts post "🔁 title url" (with the comment, if any)
|
||||
- The Threads token (60-day lifetime) is refreshed automatically once a day and kept in `data/threads_token.json`, so it survives restarts; setting a new token in the config replaces it. Setup steps: `docs/design/v2.6.0/`
|
||||
|
||||
## [2.5.0] - 2026-11-01
|
||||
|
||||
### Added
|
||||
|
||||
- **Likes and reposts** via Micropub `like-of` / `repost-of` (ADR-068); a comment is optional
|
||||
- Likes are kept off the home page and feeds and listed on a new `/likes` page (linked in the header)
|
||||
- Reposts appear in the stream and feeds with a "🔁 Reposted" line
|
||||
- The liked/reposted post's title and author are fetched and shown; it is sent a webmention
|
||||
- GoToSocial: likes/reposts of fediverse posts become native favourites/boosts; reposts of other pages (or with a comment) post "🔁 title url"
|
||||
- Bluesky: likes/reposts of Bluesky posts become native likes/reposts; a repost with a comment is a quote post; reposts of other pages post with a link card
|
||||
- Likes of ordinary web pages are not crossposted; deleting a like/repost undoes it everywhere
|
||||
- Migration 013: `notes.like_of`, `notes.repost_of`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Outgoing webmentions named posts `https://site//note/<slug>` (double slash)
|
||||
|
||||
### Notes
|
||||
|
||||
- The GoToSocial token needs the extra scopes `read:search` and `write:favourites`
|
||||
|
||||
## [2.4.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Micropub update** (`action=update`, JSON): replace/add/delete for `content`, `name`, `category`, `photo`, `in-reply-to`, `post-status`; edits follow to crossposts (ADR-067)
|
||||
- **Micropub delete and undelete**: soft delete, restorable; crossposts are removed and re-posted
|
||||
- **Drafts** via `post-status: draft`; **replies** via `in-reply-to`
|
||||
- Photos uploaded in a multipart create request; photos hosted elsewhere are downloaded (public addresses only, 20 MB cap)
|
||||
- `q=source` without `url` lists recent posts (drafts included, `limit`/`offset`); `properties[]` filter; returns photos, syndication links, reply and post status
|
||||
- `q=category` (tag suggestions, optional `filter`); `q=config` lists supported queries and post types
|
||||
- Media endpoint accepts the `media` scope
|
||||
|
||||
### Fixed
|
||||
|
||||
- Media endpoint was advertised at `/micropub/media` but served at `/media` (404 for clients); now both work
|
||||
- Unsupported Micropub types (e.g. `h=event`) were silently stored as notes; now rejected
|
||||
|
||||
## [2.3.3] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Micropub `name` was ignored, so titles sent by Micropub clients never appeared. It is now stored as the note's `# Heading` first line, which is how StarPunk titles work
|
||||
- Micropub `q=source` returned `/notes/<slug>` links (a 404); it now returns the real permalink `/note/<slug>`
|
||||
- Micropub `q=source` reported every note's first line as its `name`; it now returns `name` only for titled notes, with the heading left out of `content`, matching what was posted
|
||||
- Titled notes showed their title twice: as the page heading and again at the top of the body (and of the home-page summary)
|
||||
|
||||
## [2.3.2] - 2026-10-31
|
||||
|
||||
### Changed
|
||||
|
||||
- Header link reads "Admin" (was "Dashboard") when logged in
|
||||
|
||||
### Fixed
|
||||
|
||||
- Public pages never knew you were logged in (only `/admin` routes checked the session), so the header always said "Log in"
|
||||
|
||||
## [2.3.1] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- "Log in" is back in the header nav (2.3.0 had moved it to the footer, where it was easy to miss)
|
||||
|
||||
## [2.3.0] - 2026-10-31
|
||||
|
||||
### Changed
|
||||
|
||||
- **Public pages restyled**: compact header with the site name from `SITE_NAME`, serif reading font for notes, one-line metadata (date · #tags · Also on …), dark mode via `prefers-color-scheme`, footer pinned to the bottom, "Log in" moved to the footer
|
||||
- Home and tag pages show short notes in full; titled notes show a summary and "Read more"
|
||||
- Author h-card kept for parsers but visually hidden on this single-author site
|
||||
- Page titles use the note text and `SITE_NAME` instead of the slug and "StarPunk"
|
||||
|
||||
### Fixed
|
||||
|
||||
- Every note's metadata was styled as the page footer (centered, ruled, padded)
|
||||
- Previews cut the note's HTML at 300 characters, which could break markup
|
||||
- Bare URLs in notes are now links
|
||||
|
||||
## [2.2.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to Bluesky**, ADR-066
|
||||
- Plain-text note (Markdown stripped) with clickable link and hashtag facets, shortened to 300 characters with the permalink kept
|
||||
- Images (up to 4, shrunk under Bluesky's 1 MB limit) with alt text; notes without images get a link card
|
||||
- Unpublishing, unticking and deleting remove the Bluesky post; edits don't follow (Bluesky has no edits)
|
||||
- Logs in with an app password: `BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD` (optional `BLUESKY_SERVICE`, default `https://bsky.social`)
|
||||
- Shown in the editor and Micropub `syndicate-to` next to GoToSocial
|
||||
|
||||
## [2.1.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to GoToSocial** (or any Mastodon-API server), ADR-065
|
||||
- Full note as Markdown with images (alt text from captions), permalink and hashtags
|
||||
- Edits, unpublishing and deletes follow the note
|
||||
- On by default for new notes; untick in the editor, or send `mp-syndicate-to` from Micropub apps
|
||||
- Micropub `q=syndicate-to` and `q=config` list the target
|
||||
- Editor shows crosspost status, errors and a retry button; note pages link the copy with `u-syndication`
|
||||
- Configure with `GOTOSOCIAL_URL` and `GOTOSOCIAL_ACCESS_TOKEN` (scopes `write:statuses write:media`)
|
||||
- Migration 012: `syndications`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Micropub `Location` header pointed at `/notes/<slug>` (404); now the real permalink
|
||||
- "Delete Note" on the edit page submitted the edit form (nested forms)
|
||||
- Search results linked to `/notes/<slug>` (404); now `/note/<slug>`
|
||||
|
||||
## [2.0.2] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Container builds in Gitea Actions log in to the registry with the
|
||||
`REGISTRY_TOKEN` secret (a `write:package` token); the automatic Actions
|
||||
token is refused by the registry. No application changes.
|
||||
|
||||
## [2.0.1] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Page links built from the request said `http://` behind an HTTPS reverse proxy
|
||||
(RSS/OPML `<link>`s and microformats `u-url`/`u-uid` permalinks). All absolute
|
||||
URLs in templates now come from `SITE_URL` via `absolute_url_for()`.
|
||||
- Container builds in Gitea Actions: build through the server's rootless BuildKit
|
||||
and log in to the public registry name (the old workflow never succeeded).
|
||||
|
||||
## [2.0.0] - 2026-10-31
|
||||
|
||||
### Changed (breaking)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# ADR-065: Direct Syndication (POSSE) to GoToSocial
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.1.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
StarPunk posts were copied to other networks by Echofeed reading the feed.
|
||||
That gives no control per post, no record of where a post went, and edits
|
||||
and deletes never follow. The operator runs GoToSocial locally and wants
|
||||
StarPunk to post there directly, with Bluesky likely next.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Publish here, copy out (POSSE).** Each (note, target) has a row in
|
||||
`syndications` (migration 012). Creating, editing, unpublishing or
|
||||
deleting a note marks its rows pending; a background worker reconciles
|
||||
the remote post: create, update (`PUT /api/v1/statuses/:id`) or delete.
|
||||
2. **Defaults.** New notes go to every configured target unless unticked in
|
||||
the editor or a Micropub client sends `mp-syndicate-to` (an explicit
|
||||
list, possibly empty). Existing notes are never backfilled.
|
||||
3. **Content.** The full note as Markdown (`content_type: text/markdown`),
|
||||
then the permalink and the note's tags as hashtags. Longer than the
|
||||
target's limit: shortened at a word boundary with the link kept.
|
||||
Images are uploaded first (the `large` variant) with their captions as
|
||||
alt text.
|
||||
4. **Delivery.** Rows are claimed atomically so the four gunicorn workers'
|
||||
threads never send twice; an `Idempotency-Key` guards retries; a row
|
||||
`version` keeps edits made during a send. Network errors and 5xx/429
|
||||
retry with exponential backoff (8 attempts); other 4xx fail at once.
|
||||
A remote post deleted by hand is posted again on the next edit.
|
||||
5. **Backlinks.** Synced URLs are shown on the note page as `u-syndication`
|
||||
links, and in the editor with status, last error and a retry button.
|
||||
6. **Targets** are config-driven. One kind exists, a Mastodon-API client
|
||||
(`GOTOSOCIAL_URL`, `GOTOSOCIAL_ACCESS_TOKEN`); it also works for
|
||||
Mastodon. A Bluesky target would add a second client behind the same
|
||||
reconcile loop.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Echofeed must stop posting to GoToSocial or posts appear twice.
|
||||
- The token needs only `write:statuses write:media`.
|
||||
- `syndications.note_id` has no foreign key so a hard-deleted note's
|
||||
remote post can still be deleted.
|
||||
@@ -0,0 +1,37 @@
|
||||
# ADR-066: Crossposting to Bluesky
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.2.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
ADR-065 added direct crossposting to GoToSocial. Bluesky was still fed by
|
||||
Echofeed, with the same problems: no per-post control, no record of the
|
||||
copy, deletes never follow.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Second client, same loop.** `BlueskyClient` sits behind the reconcile
|
||||
loop and `syndications` table from ADR-065, as target id `bluesky`.
|
||||
Clients share a small interface: `create`, `update`, `delete`, `can_edit`.
|
||||
2. **Auth.** An app password (`BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD`).
|
||||
`createSession` against `BLUESKY_SERVICE` (default `https://bsky.social`);
|
||||
writes go to the PDS named in the returned DID document. Expired access
|
||||
tokens are refreshed, falling back to a new session.
|
||||
3. **Content.** Plain text (Markdown rendered and stripped), then the
|
||||
permalink and hashtags (at most a third of the space), shortened to 300
|
||||
characters at a word boundary. Link and hashtag facets use UTF-8 byte
|
||||
offsets. Images (up to 4) are re-encoded as JPEG if over 1 MB, with alt
|
||||
text and aspect ratio; a note without images gets a link card.
|
||||
4. **Edits don't follow.** Bluesky has no edit; rewriting the record would
|
||||
break likes and replies, and delete-and-repost would lose them. The post
|
||||
stays as first sent. Unpublish, untick and delete remove it.
|
||||
5. **No double posts.** When a create is retried, recent posts are checked
|
||||
for one already linking to the note (a send whose reply was lost).
|
||||
|
||||
## Consequences
|
||||
|
||||
- Echofeed is no longer needed; it must stop posting to Bluesky or posts
|
||||
appear twice.
|
||||
- A post deleted by hand on Bluesky is not re-created.
|
||||
@@ -0,0 +1,34 @@
|
||||
# ADR-067: Micropub update, delete, drafts and replies
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.4.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
The Micropub endpoint could only create published notes. Apps could not edit
|
||||
or delete posts, drafts and replies were dropped, and photos sent with the
|
||||
post or hosted elsewhere were lost.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Update** (JSON only, `update` scope). Supported properties are the ones a
|
||||
note can store: `content`, `name` (the `# Heading` first line),
|
||||
`category`, `photo`, `in-reply-to`, `post-status`. Anything else is a 400,
|
||||
not silently ignored. Order: delete, then replace, then add (`add` only for
|
||||
`category` and `photo`). Success is 204.
|
||||
2. **Delete is soft** (`delete` scope) so `undelete` can restore the note and
|
||||
its file from `.trash/`. Crossposts are removed on delete and posted again
|
||||
on undelete.
|
||||
3. **Drafts**: `post-status: draft` creates an unpublished note; crossposts
|
||||
wait until it is published.
|
||||
4. **Photos**: multipart uploads are saved like media-endpoint uploads; remote
|
||||
URLs are downloaded only from public IP addresses, up to 20 MB, max 4.
|
||||
5. **Queries**: `q=source` returns everything create accepts (round-trip);
|
||||
without `url` it lists recent posts, drafts included, for editing apps.
|
||||
`q=category` lists tags by use.
|
||||
|
||||
## Consequences
|
||||
|
||||
- `published` (backdating) can be set on create but not changed by update.
|
||||
- Likes, reposts and bookmarks remain unsupported (round 2).
|
||||
@@ -0,0 +1,32 @@
|
||||
# ADR-068: Likes and reposts
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.5.0, 2026-11-01). Bookmarks were considered and declined.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Storage.** `notes.like_of` / `notes.repost_of` (migration 013). The note's
|
||||
content is an optional comment and may be empty.
|
||||
2. **Display.** Likes stay off the home page and feeds and are listed on
|
||||
`/likes`. Reposts appear in the stream and feeds. Both show the target's
|
||||
fetched title and author (the reply-context cache) and carry
|
||||
`u-like-of` / `u-repost-of`. The target is sent a webmention.
|
||||
3. **Crossposting, per target.**
|
||||
|
||||
| | Target is a post on that network | Anything else |
|
||||
|---|---|---|
|
||||
| Like | native favourite / like | nothing |
|
||||
| Repost, no comment | native boost / repost | "🔁 title url" post |
|
||||
| Repost, with comment | Bluesky quote post; GoToSocial status | comment + "🔁 title url" post |
|
||||
|
||||
GoToSocial finds fediverse posts with `/api/v2/search?resolve=true`
|
||||
(needs `read:search`; favourites need `write:favourites`). Bluesky
|
||||
recognises `bsky.app/profile/<actor>/post/<rkey>` URLs. Native actions
|
||||
are stored as `favourite:<id>` / `reblog:<id>` or the like/repost record
|
||||
URI, and deleting the note undoes them.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Editing a like/repost doesn't change a native favourite/boost.
|
||||
- A repost with a comment can't be a GoToSocial boost (no quote posts there).
|
||||
@@ -0,0 +1,57 @@
|
||||
# ADR-069: Crossposting to Threads
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.6.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
ADR-065 and ADR-066 crosspost notes to GoToSocial and Bluesky. Threads
|
||||
(Meta) has a publishing API, so it can be a third target on the same loop.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Third client, same loop.** `ThreadsClient` is target id `threads`
|
||||
behind the `syndications` table and reconcile loop from ADR-065.
|
||||
2. **Auth.** A long-lived Threads user token (`THREADS_ACCESS_TOKEN`) with
|
||||
`threads_basic`, `threads_content_publish` and `threads_delete`. These
|
||||
tokens expire after 60 days, so StarPunk refreshes the token about once a
|
||||
day (`th_refresh_token`) from the worker loop, even when nothing is being
|
||||
posted. The current token is kept in `DATA_PATH/threads_token.json`
|
||||
(mode 0600) with a hash of the configured token. A different configured
|
||||
token replaces the saved one. A file lock stops gunicorn workers from
|
||||
refreshing at the same time. The file avoids a migration for one value.
|
||||
3. **Posting.** Create a container, poll its status until `FINISHED`, then
|
||||
`threads_publish`. Text-only notes are `TEXT` posts. Threads previews
|
||||
the first link in the text (the permalink) by itself. An explicit
|
||||
`link_attachment` is not sent, because Meta rejects one for a page it
|
||||
hasn't fetched yet ("Invalid Link Attachment", subcode 4279047), which
|
||||
is always the case for a new note (v2.6.2). One image is an `IMAGE` post and 2–4 images are a
|
||||
`CAROUSEL`. Meta fetches images from their public `/media/` URL. Only
|
||||
JPEG and PNG up to 8 MB are allowed, so GIF and WebP images are left off.
|
||||
Alt text goes in `alt_text`.
|
||||
4. **Content.** Plain text (as for Bluesky), then the permalink, shortened
|
||||
to 500 characters. Non-ASCII characters are counted as UTF-8 bytes,
|
||||
because Threads counts emoji that way. Threads allows one tag per post,
|
||||
so the note's first tag is sent as `topic_tag` and no hashtags are added
|
||||
to the text.
|
||||
5. **Edits don't follow.** The API has no way to edit a post. As with
|
||||
Bluesky, the post stays as first sent, and unpublishing, unticking the
|
||||
target or deleting the note removes it.
|
||||
6. **Likes and reposts (ADR-068).** The API can't like or repost outside
|
||||
content, so likes are not crossposted. Reposts are text posts with
|
||||
"🔁 title" and the URL, plus the comment if there is one, and the
|
||||
reposted page as the link preview.
|
||||
7. **No double posts.** A retried create first looks for a recent post that
|
||||
already contains the permalink.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Meta limits accounts to 250 posts and 100 deletes per day. Rate-limit
|
||||
errors (Graph codes 4, 17, 32, 613) are retried.
|
||||
- If StarPunk is down for more than 60 days, the token expires and a new
|
||||
one is needed.
|
||||
- Getting the first token requires a Meta developer app with the Threads
|
||||
use case. See the setup notes in `docs/design/v2.6.0/`.
|
||||
- Notes with more than five links fail on Threads (a Meta limit). The
|
||||
error is shown in the editor.
|
||||
@@ -0,0 +1,44 @@
|
||||
# ADR-070: Links and link cards in crossposts
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.6.3, 2026-11-01)
|
||||
|
||||
## Context
|
||||
|
||||
Every crosspost carries the note's permalink. Notes often link elsewhere
|
||||
too, usually to the article being discussed. Before this change:
|
||||
|
||||
- Bluesky and Threads get plain text, and a Markdown link `[text](url)`
|
||||
lost its URL, so readers there couldn't follow it.
|
||||
- The Bluesky card always showed the note, and the Threads preview showed
|
||||
whichever link came first.
|
||||
- Threads rejects posts with more than 5 different links.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **The card shows what the note links to.** If the note has its own link
|
||||
(a Markdown link, `<url>` or a bare URL), the first one gets the
|
||||
preview card. A note without links gets a card for itself, as before.
|
||||
Images still take the place of a card on Bluesky.
|
||||
2. **Bluesky.** The card is built by StarPunk: it fetches the page's Open
|
||||
Graph, Twitter or plain HTML title, description and image, and uploads
|
||||
the image (shrunk to under 1 MB) as the thumbnail. If the page can't
|
||||
be fetched, the card has just the URL. Markdown link text becomes a
|
||||
clickable link facet, so no characters are spent on the URL.
|
||||
3. **Threads.** It has no clickable link text, so Markdown links are
|
||||
written "text (url)". Threads previews the first link in the text, which
|
||||
is now the linked page. To stay within Meta's 5-link limit, after 4
|
||||
different links in the body a Markdown link keeps only its text.
|
||||
4. **GoToSocial** already gets Markdown, so its links are unchanged.
|
||||
Receiving servers build their own cards.
|
||||
5. **Fetching is careful.** Only http(s) URLs on public addresses are
|
||||
fetched, and this is checked again at every redirect. At most 512 KB of
|
||||
a page and 5 MB of an image are read. A failed fetch never fails the
|
||||
post.
|
||||
|
||||
## Consequences
|
||||
|
||||
- One or two extra requests to the linked site when a note is crossposted
|
||||
to Bluesky.
|
||||
- Reposts (ADR-068) are unchanged: their card is the reposted page.
|
||||
@@ -0,0 +1,23 @@
|
||||
# v2.3.3: Micropub titles, q=source URL, single title display
|
||||
|
||||
Found while building tuipub, a terminal Micropub client, against StarPunk.
|
||||
|
||||
## Problems
|
||||
|
||||
1. **Micropub `name` dropped.** `handle_create` called `extract_title()` and threw the result away. StarPunk has no title column: `Note.title` is derived from the content's first line, and a `# Heading` first line marks a titled note (Q22). So a `name` sent by a client never showed up.
|
||||
2. **`q=source` URL wrong.** It built `{SITE_URL}notes/{slug}`, but the route is `/note/<slug>`, so the URL was a 404.
|
||||
3. **`q=source` name wrong.** It returned `note.title` for every note. For untitled notes that is just the first line of the content, so clients saw a `name` they never sent.
|
||||
4. **Title shown twice.** `note.html` renders the title as `<h1 class="p-name">` and then the whole content, which includes the `# Heading` again. Home-page previews did the same with the excerpt.
|
||||
|
||||
## Decisions
|
||||
|
||||
- Store `name` as a `# {name}` first line (`micropub.add_title_heading`) rather than adding a title column. This matches how titled notes already work everywhere (pages, feeds, search, admin editor), and needs no migration. If the content already starts with that exact heading, it isn't added twice.
|
||||
- `models.split_title()` is the single definition of "title heading": the first non-blank line starting with `#` (the same rule as the templates' `has_explicit_title`). `q=source` uses it to return `name` + `content` the way they were posted.
|
||||
- `Note.body_html` renders the content without the title heading; `note.html` uses it. `Note.excerpt` also leaves the title out. `Note.html` is unchanged, so feeds and syndication behave as before (feed items carry a separate title anyway).
|
||||
- `extract_title()` no longer derives a fallback title from content: that value was never used.
|
||||
|
||||
## Tests
|
||||
|
||||
- `tests/test_micropub.py`: name stored as heading, no duplicate heading, `q=source` url equals the Location permalink, `name` only for titled notes, round trip.
|
||||
- `tests/test_models.py`: `split_title`, `body_html`, excerpt without title.
|
||||
- Checked end to end with tuipub against a local dev server: titled post → page shows the title once, `q=source` returns `name`, `content` and a working url.
|
||||
@@ -0,0 +1,62 @@
|
||||
# v2.6.0: Crossposting to Threads
|
||||
|
||||
Implements ADR-069.
|
||||
|
||||
## What changed
|
||||
|
||||
- `starpunk/syndication.py`: `ThreadsClient` (target `threads`); media
|
||||
entries now carry their public `url`; `process_due()` calls each client's
|
||||
optional `maintain()` hook, which Threads uses to refresh its token.
|
||||
- `starpunk/config.py`: `THREADS_ACCESS_TOKEN`, `THREADS_USERNAME`,
|
||||
`THREADS_NAME`, `THREADS_MAX_CHARS` (500), `THREADS_MAX_MEDIA` (4).
|
||||
- `tests/test_syndication_threads.py`: a fake Graph API covering text,
|
||||
image and carousel posts; container polling; edits not following; deletes
|
||||
(including posts already deleted by hand); a lost publish reply;
|
||||
permanent and rate-limit errors; and token refresh, persistence and
|
||||
replacement.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Meaning |
|
||||
|---|---|
|
||||
| `THREADS_ACCESS_TOKEN` | Long-lived Threads user token. Enables the target. |
|
||||
| `THREADS_USERNAME` | Optional. Used for the Micropub `syndicate-to` uid (`https://www.threads.com/@name`) and for fallback links. |
|
||||
| `THREADS_NAME` | Label in the editor (default `Threads`) |
|
||||
|
||||
After the first daily refresh, the working token lives in
|
||||
`DATA_PATH/threads_token.json`. The configured value only seeds it.
|
||||
Changing `THREADS_ACCESS_TOKEN` switches to the new token.
|
||||
|
||||
## Getting a token (one time)
|
||||
|
||||
1. At <https://developers.facebook.com/apps>, create an app with the
|
||||
**Access the Threads API** use case.
|
||||
2. Under the use case's permissions, add `threads_basic`,
|
||||
`threads_content_publish` and `threads_delete`.
|
||||
3. Under **App roles → Roles**, add your Threads account as a Threads
|
||||
tester. Accept the invite in Threads (Settings → Account → Website
|
||||
permissions → Invites). The app can stay in development mode.
|
||||
4. In the use case settings, add `https://<site>/` as a redirect URI. Then
|
||||
open:
|
||||
|
||||
https://threads.net/oauth/authorize?client_id=APP_ID&redirect_uri=https://<site>/&scope=threads_basic,threads_content_publish,threads_delete&response_type=code
|
||||
|
||||
After you approve, the browser lands on `https://<site>/?code=...`. Copy
|
||||
the code (drop a trailing `#_`).
|
||||
5. Exchange the code for a short-lived token, then exchange that for a
|
||||
long-lived one:
|
||||
|
||||
curl -s -X POST https://graph.threads.net/oauth/access_token \
|
||||
-F client_id=APP_ID -F client_secret=APP_SECRET \
|
||||
-F grant_type=authorization_code -F redirect_uri=https://<site>/ -F code=CODE
|
||||
curl -s "https://graph.threads.net/access_token?grant_type=th_exchange_token&client_secret=APP_SECRET&access_token=SHORT_TOKEN"
|
||||
|
||||
6. Set `THREADS_ACCESS_TOKEN` to the long-lived `access_token`.
|
||||
|
||||
The app secret is only needed for these steps. StarPunk doesn't use it.
|
||||
|
||||
## Not done
|
||||
|
||||
- Video, GIF and WebP images (Threads takes video by URL. StarPunk only
|
||||
stores still images, and Threads rejects GIF and WebP).
|
||||
- Edits (no API for them).
|
||||
@@ -0,0 +1,89 @@
|
||||
# v2.6.5: Micropub media fixes
|
||||
|
||||
Found while adding media uploads to tuipub, a Micropub client written to the
|
||||
spec rather than to StarPunk.
|
||||
|
||||
## Problems
|
||||
|
||||
1. **Failed creates left a post behind.** `handle_create` made the note, then
|
||||
attached photos. Listing one photo twice broke the `note_media` unique key,
|
||||
so the client got a 500 while the note stayed published without photos.
|
||||
A retry would post it twice.
|
||||
2. **Then the database stayed locked.** `attach_media_to_note` didn't roll
|
||||
back, and `ConnectionPool.return_connection` pooled the connection with
|
||||
its write transaction still open. Other requests got "database is locked"
|
||||
until the 10 s timeout.
|
||||
3. **Photo posts needed text.** The Micropub spec doesn't require `content`,
|
||||
and `q=config` advertised a `photo` post type with only `photo`, yet
|
||||
creates without content got 400 "Content is required".
|
||||
|
||||
## Changes
|
||||
|
||||
- `database/pool.py`: `return_connection` rolls back an open transaction
|
||||
(or closes the connection if that fails).
|
||||
- `media.py`: `attach_media_to_note` rolls back on error.
|
||||
- `micropub.py`:
|
||||
- `handle_create` resolves photos (by media URL, downloading remote ones)
|
||||
before `create_note`. If attaching or crosspost selection fails, it
|
||||
hard-deletes the note before answering 500.
|
||||
- `_resolve_photos` skips repeated media.
|
||||
- Content is optional when photos resolve. Content-less posts whose photos
|
||||
all fail still get a 400 from `create_note`.
|
||||
- `handle_update` allows `replace: {content: [""]}` and `delete: ["content"]`
|
||||
while the note keeps photos (or is a like or repost). Otherwise it answers
|
||||
400 "A post needs text or a photo".
|
||||
- `notes.py`: `create_note` and `update_note` take `has_media`, so
|
||||
empty content is allowed for photo posts.
|
||||
- `syndication.py`: `compose_text` drops the leading blank line when there's
|
||||
no body.
|
||||
|
||||
## Post types (IndieWeb standards check)
|
||||
|
||||
Post Type Discovery (W3C Note) has no separate "photo" type to store. A post
|
||||
with a valid `photo` is a photo post, unless it's first a repost, like or
|
||||
reply (step order: repost, like, reply, video, photo). Otherwise it's a note,
|
||||
or an article when its `name` isn't a prefix of its content. h-entry treats
|
||||
`u-photo` as the primary content, with `name` as a caption. mf2 implies a
|
||||
`name` only when an entry has no other `p-*`/`e-*` properties and no nested
|
||||
microformats.
|
||||
|
||||
StarPunk already stores photo posts as notes with media and renders
|
||||
`u-photo` outside `e-content`, which is what the standards ask for. Checking
|
||||
the rendered pages with mf2py found:
|
||||
|
||||
- **Replies weren't replies.** In `note.html`, `u-in-reply-to` was inside a
|
||||
bare `h-cite`, so it was the h-cite's property, not the entry's.
|
||||
`note_preview.html` had no reply markup at all. Fixed: the blockquote is
|
||||
`u-in-reply-to h-cite` (with `u-url` inside), and previews show
|
||||
"↩ In reply to" with `u-in-reply-to`.
|
||||
- **Implied names were possible.** Previews left out `e-content` for posts
|
||||
without text. They worked only because the author h-card blocked an
|
||||
implied name. Previews now always emit `e-content`.
|
||||
- Page title for text-free posts follows PTD order: Repost, Like, Reply,
|
||||
Photo.
|
||||
|
||||
Photos without a caption take the post's text as alt (`partials/media.html`
|
||||
`fallback_alt`) instead of the made-up "Image". The Micropub `q=source`
|
||||
still reports the stored caption (`""`).
|
||||
|
||||
`tests/test_post_types.py` parses note pages and the home page with mf2py and
|
||||
runs PTD for notes, articles, photo posts (no text, caption, title), replies
|
||||
(with and without a photo) and reposts.
|
||||
|
||||
## Not changed (noted for later)
|
||||
|
||||
- Text-free photos without alt text still render `alt="Image"`. Captioned
|
||||
photos use their caption, and others now use the post's text (`display_media`
|
||||
`fallback_alt`, the excerpt cut to 150 characters).
|
||||
|
||||
- More than 4 photos are still cut to 4 without an error (ADR-057).
|
||||
- `video` and `audio` are ignored. This is allowed by the spec, and they
|
||||
aren't in `q=config` post-types.
|
||||
- Media endpoint errors for non-images expose Python text
|
||||
("cannot identify image file <_io.BytesIO …>").
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_micropub_v265.py`. The full suite runs with
|
||||
`SESSION_SECRET=test`. 8 tests in `test_media_upload.py` and
|
||||
`test_migration_race_condition.py` already fail on main, and still fail here.
|
||||
@@ -0,0 +1,37 @@
|
||||
# v2.6.8: Webmention worker app context
|
||||
|
||||
## Problem
|
||||
|
||||
Production webmention #1 (Bridgy, reply from Mastodon to note 20261101162512)
|
||||
was stored with `validation_status = 'error'`:
|
||||
"Working outside of application context". Both outgoing queue items had the
|
||||
same error and were on their last retry.
|
||||
|
||||
## Causes
|
||||
|
||||
1. `webmention_ssrf.safe_resolve()` reads `current_app.config['DEV_MODE']`.
|
||||
`WebmentionWorker` is a plain thread with no Flask app context, so every
|
||||
`validate_url_for_fetch()` call raised `RuntimeError`, for both
|
||||
validation (incoming) and endpoint discovery and sending (outgoing).
|
||||
2. `create_app()` registered a `teardown_appcontext` handler that called
|
||||
`webmention_worker.stop()`. Teardown runs whenever any app context ends
|
||||
(every request), so the worker stopped soon after startup.
|
||||
|
||||
## Fix
|
||||
|
||||
- `WebmentionWorker` takes the Flask app and runs each poll inside
|
||||
`app.app_context()`, as `SyndicationWorker` already does.
|
||||
- The teardown handler is removed. The worker is a daemon thread and ends with
|
||||
the process.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_worker.py`: an outgoing send completes through the
|
||||
worker thread, and the worker is still alive after an app context and a
|
||||
request end. Both fail without the fix.
|
||||
|
||||
## Production data
|
||||
|
||||
After deploying, reset webmention 1 to `validation_status = 'pending'` and the
|
||||
two queue items to `retry_count = 0, next_retry_at = NULL` so the worker
|
||||
processes them again.
|
||||
@@ -0,0 +1,36 @@
|
||||
# v2.6.9: Webmention processing fixes
|
||||
|
||||
Found after v2.6.8 let the worker run in production, and by a review of the
|
||||
implementation against the W3C Webmention Recommendation.
|
||||
|
||||
## Fixes
|
||||
|
||||
1. **Author photo with alt text** (`webmention_processor.extract_author`).
|
||||
mf2py returns `u-photo` with an `alt` attribute as
|
||||
`{'value': url, 'alt': ...}`; the dict was bound to SQLite and the update
|
||||
failed. `_first_str()` reduces mf2 values to their `value`.
|
||||
2. **Endpoint discovery** (`WebmentionWorker._discover_endpoint`).
|
||||
`response.url` is an `httpx.URL`; `urljoin(str, URL)` raised `TypeError`,
|
||||
which was caught and returned as "no endpoint", so every target was marked
|
||||
`no_endpoint`. Pass `str(response.url)`. Fetch errors now propagate so the
|
||||
caller schedules a retry, instead of being treated as "no endpoint".
|
||||
3. **Duplicate processing.** gunicorn runs 4 processes, each with a worker,
|
||||
and queue items were claimed by SELECT then UPDATE. The worker now takes a
|
||||
non-blocking `flock` on `<data>/webmention-worker.lock` before processing;
|
||||
others wait and retry each poll, so one takes over if the holder dies.
|
||||
Orphan cleanup runs when the lock is acquired.
|
||||
4. **XSS (spec 4.1).** `author_url` and `author_photo` are kept only if they
|
||||
are absolute http(s) URLs; otherwise the source URL / no photo is used.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_worker.py`: the send test uses a real `httpx.Response`
|
||||
(the old `MagicMock` with a string URL hid fix 2), plus tests for the lock,
|
||||
an alt-text photo and a `javascript:` author URL. All fail on v2.6.8.
|
||||
|
||||
## Remaining spec gaps
|
||||
|
||||
Tracked separately: rel-token and document-order endpoint discovery, exact
|
||||
target match during verification, Accept header, re-sending on delete, 410
|
||||
for deleted notes, SSRF re-validation on redirects, and the others listed in
|
||||
the compliance review.
|
||||
@@ -0,0 +1,50 @@
|
||||
# v2.7.0: Webmention W3C compliance
|
||||
|
||||
Review of StarPunk's Webmention support against the W3C Recommendation
|
||||
(https://www.w3.org/TR/webmention/, 12 January 2017), and the changes made.
|
||||
v2.6.8 and v2.6.9 fixed the bugs that stopped webmentions working at all.
|
||||
|
||||
## Sending
|
||||
|
||||
| Section | Requirement | Change |
|
||||
|---|---|---|
|
||||
| 3.1.2 | Link header with rel=webmention first (MUST) | RFC 8288 parsing: several links per header, several headers, `rel` in any position, token list, case-insensitive |
|
||||
| 3.1.2 | Then first `<link>`/`<a>` rel=webmention in document order (MUST) | Single pass over `link` and `a` in document order; rel token match; elements without `href` skipped |
|
||||
| 3.1.2 | Relative URL resolved against target; empty href is the target; query kept (MUST) | `urljoin` against the final URL after redirects |
|
||||
| 3.1.2 | Follow redirects | `safe_get` follows up to 10, checking each hop |
|
||||
| 3.1.2 | User-Agent with "Webmention" (MAY) | `StarPunk/<version> Webmention (+site)` |
|
||||
| 3.1.3 | Form-encoded POST; any 2xx success (MUST) | Unchanged. Redirects not followed (httpx turned 301/302 POST into GET); 429 retried |
|
||||
| 3.1.4 | Re-discover endpoint on update (MUST) | Discovery cache removed |
|
||||
| 3.1.4 | Re-send to previously sent targets, including removed links (SHOULD) | `previous_targets()` from the queue, plus removed links; not capped |
|
||||
| 3.1.5 | Deleted post returns 410 (SHOULD), tombstone (SHOULD) | `public.note` aborts 410 for soft-deleted notes and hard-deleted notes that sent webmentions; `410.html` |
|
||||
| 3.1.5 | Re-send on delete (SHOULD) | `requeue_previous_webmentions()`; for a hard delete the rows are detached (`source_note_id = NULL`) first, since the foreign key cascades. Also on undelete |
|
||||
|
||||
## Receiving
|
||||
|
||||
| Section | Requirement | Change |
|
||||
|---|---|---|
|
||||
| 3.2 | 202 for async processing (MUST) | Unchanged |
|
||||
| 3.2.1 | Valid http(s) URLs; source ≠ target; 400 on error (MUST) | Unchanged |
|
||||
| 3.2.1 | Target is a valid resource (SHOULD) | Only published notes; others 400 |
|
||||
| 3.2.1 | Fragment ignored | Stripped before storing (no duplicate rows) |
|
||||
| 3.2.2 | GET source, follow redirects, limit redirects | `safe_get` |
|
||||
| 3.2.2 | Accept header (SHOULD) | Added |
|
||||
| 3.2.2 | Exact match of target, per media type (MUST) | HTML: every link attribute (`lxml iterlinks`) resolved against the final URL; JSON: any string value; `text/*`: substring; other types: no match. Fragments ignored |
|
||||
| 3.2.4 | Update existing data; 410 / no link removes it (SHOULD) | Unchanged in effect. Moderation decisions kept on update; transient errors keep a valid mention |
|
||||
|
||||
## Security (section 4)
|
||||
|
||||
- 4.1 XSS: author URL/photo must be http(s) (v2.6.9); snippets re-sanitized after truncation; Mentions list uses `striptags`.
|
||||
- 4.2 / 4.5: `safe_get` checks every redirect hop, checks all `getaddrinfo` addresses, blocks private, loopback, link-local, multicast, reserved, unspecified, CGNAT, `0.0.0.0/8` and IPv4-mapped forms; 1 MB and 10 s total limits.
|
||||
- 4.3: endpoint URL checked before POST, no redirects followed.
|
||||
|
||||
## Known limits
|
||||
|
||||
- DNS is checked before connecting, not pinned for the connection, so a
|
||||
rebinding DNS server could still answer differently on connect.
|
||||
- Homepage mentions are refused rather than stored.
|
||||
- `error` rows (unexpected failures) are not retried automatically.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_spec.py` (section-numbered), `tests/test_webmention_worker.py`.
|
||||
@@ -0,0 +1,29 @@
|
||||
-- Migration 012: Crossposting (POSSE) to syndication targets
|
||||
-- Version: 2.1.0
|
||||
-- Per ADR-065: Direct syndication to GoToSocial / Mastodon-API servers
|
||||
--
|
||||
-- One row per (note, target). The worker reconciles the remote post with the
|
||||
-- note: create, update, or delete as needed. note_id has no foreign key on
|
||||
-- purpose: a row must outlive a hard-deleted note so the remote post can be
|
||||
-- deleted too.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS syndications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
note_id INTEGER NOT NULL,
|
||||
target TEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
remote_id TEXT,
|
||||
remote_url TEXT,
|
||||
state TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (state IN ('pending', 'synced', 'failed')),
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
last_error TEXT,
|
||||
next_attempt_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
claimed_until TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (note_id, target)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_syndications_due ON syndications(state, next_attempt_at);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Migration 013: Likes and reposts (v2.5.0, ADR-068)
|
||||
-- A note with like_of is a like; with repost_of, a repost. Content is the
|
||||
-- optional comment and may be empty.
|
||||
|
||||
ALTER TABLE notes ADD COLUMN like_of TEXT;
|
||||
ALTER TABLE notes ADD COLUMN repost_of TEXT;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_like_of ON notes(like_of);
|
||||
+31
-8
@@ -191,6 +191,10 @@ def create_app(config=None):
|
||||
register_routes(app)
|
||||
|
||||
# Template context processor - Inject author profile (v1.2.0 Phase 2)
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
app.jinja_env.globals["absolute_url_for"] = absolute_url_for
|
||||
|
||||
@app.context_processor
|
||||
def inject_author():
|
||||
"""
|
||||
@@ -276,6 +280,21 @@ def create_app(config=None):
|
||||
"""Add correlation ID to request context for tracing"""
|
||||
add_correlation_id()
|
||||
|
||||
# Know who is logged in on every page, not just /admin (v2.3.2), so
|
||||
# the header can say "Admin" instead of "Log in". Only requests that
|
||||
# carry a session cookie touch the database.
|
||||
from flask import g, request
|
||||
token = request.cookies.get("starpunk_session")
|
||||
if token:
|
||||
from starpunk.auth import verify_session
|
||||
try:
|
||||
info = verify_session(token)
|
||||
except Exception:
|
||||
info = None
|
||||
if info:
|
||||
g.user = info
|
||||
g.me = info["me"]
|
||||
|
||||
# Response middleware - Add webmention Link header (v1.6.0)
|
||||
@app.after_request
|
||||
def add_webmention_headers(response):
|
||||
@@ -283,7 +302,7 @@ def create_app(config=None):
|
||||
if (app.config.get('WEBMENTION_ENABLED', True) and
|
||||
response.content_type and
|
||||
'text/html' in response.content_type):
|
||||
response.headers['Link'] = '</webmention>; rel="webmention"'
|
||||
response.headers.add('Link', '</webmention>; rel="webmention"')
|
||||
return response
|
||||
|
||||
# Register centralized error handlers
|
||||
@@ -311,6 +330,7 @@ def create_app(config=None):
|
||||
if app.config.get('WEBMENTION_WORKER_ENABLED', True) and not app.config.get('TESTING', False):
|
||||
from starpunk.webmention_worker import WebmentionWorker
|
||||
webmention_worker = WebmentionWorker(
|
||||
app,
|
||||
db_path=app.config['DATABASE_PATH'],
|
||||
site_url=app.config['SITE_URL'],
|
||||
poll_interval=app.config.get('WEBMENTION_POLL_INTERVAL', 10),
|
||||
@@ -326,11 +346,14 @@ def create_app(config=None):
|
||||
f"batch_size={webmention_worker.batch_size})"
|
||||
)
|
||||
|
||||
# Register cleanup handler
|
||||
@app.teardown_appcontext
|
||||
def cleanup_webmention_worker(error=None):
|
||||
if hasattr(app, 'webmention_worker') and app.webmention_worker.is_alive():
|
||||
app.webmention_worker.stop()
|
||||
# Start syndication worker when a target is configured (v2.1.0)
|
||||
from starpunk.syndication import SyndicationWorker, targets_configured
|
||||
if targets_configured(app.config) and not app.config.get("TESTING", False):
|
||||
app.syndication_worker = SyndicationWorker(
|
||||
app, poll_interval=app.config["SYNDICATION_POLL_INTERVAL"]
|
||||
)
|
||||
app.syndication_worker.start()
|
||||
app.logger.info("Syndication worker started")
|
||||
|
||||
# Health check endpoint for containers and monitoring
|
||||
@app.route("/health")
|
||||
@@ -436,5 +459,5 @@ def create_app(config=None):
|
||||
|
||||
# Package version (Semantic Versioning 2.0.0)
|
||||
# See docs/standards/versioning-strategy.md for details
|
||||
__version__ = "2.0.0"
|
||||
__version_info__ = (2, 0, 0)
|
||||
__version__ = "2.7.0"
|
||||
__version_info__ = (2, 7, 0)
|
||||
@@ -124,6 +124,27 @@ def load_config(app, config_override=None):
|
||||
app.config["WEBMENTION_RATE_LIMIT_WINDOW"] = int(os.getenv("WEBMENTION_RATE_LIMIT_WINDOW", "60"))
|
||||
app.config["TRUSTED_PROXY"] = os.getenv("TRUSTED_PROXY", "false").lower() == "true"
|
||||
|
||||
# Crossposting to GoToSocial / Mastodon-API (v2.1.0, ADR-065)
|
||||
# Enabled when both URL and token are set
|
||||
app.config["GOTOSOCIAL_URL"] = os.getenv("GOTOSOCIAL_URL", "")
|
||||
app.config["GOTOSOCIAL_ACCESS_TOKEN"] = os.getenv("GOTOSOCIAL_ACCESS_TOKEN", "")
|
||||
app.config["GOTOSOCIAL_NAME"] = os.getenv("GOTOSOCIAL_NAME", "GoToSocial")
|
||||
app.config["GOTOSOCIAL_VISIBILITY"] = os.getenv("GOTOSOCIAL_VISIBILITY", "public")
|
||||
app.config["GOTOSOCIAL_MAX_CHARS"] = int(os.getenv("GOTOSOCIAL_MAX_CHARS", "5000"))
|
||||
app.config["GOTOSOCIAL_MAX_MEDIA"] = int(os.getenv("GOTOSOCIAL_MAX_MEDIA", "4"))
|
||||
app.config["BLUESKY_HANDLE"] = os.getenv("BLUESKY_HANDLE", "")
|
||||
app.config["BLUESKY_APP_PASSWORD"] = os.getenv("BLUESKY_APP_PASSWORD", "")
|
||||
app.config["BLUESKY_SERVICE"] = os.getenv("BLUESKY_SERVICE", "https://bsky.social")
|
||||
app.config["BLUESKY_NAME"] = os.getenv("BLUESKY_NAME", "Bluesky")
|
||||
app.config["BLUESKY_MAX_CHARS"] = int(os.getenv("BLUESKY_MAX_CHARS", "300"))
|
||||
# Crossposting to Threads (v2.6.0, ADR-069): long-lived token, refreshed automatically
|
||||
app.config["THREADS_ACCESS_TOKEN"] = os.getenv("THREADS_ACCESS_TOKEN", "")
|
||||
app.config["THREADS_USERNAME"] = os.getenv("THREADS_USERNAME", "")
|
||||
app.config["THREADS_NAME"] = os.getenv("THREADS_NAME", "Threads")
|
||||
app.config["THREADS_MAX_CHARS"] = int(os.getenv("THREADS_MAX_CHARS", "500"))
|
||||
app.config["THREADS_MAX_MEDIA"] = int(os.getenv("THREADS_MAX_MEDIA", "4"))
|
||||
app.config["SYNDICATION_POLL_INTERVAL"] = int(os.getenv("SYNDICATION_POLL_INTERVAL", "10"))
|
||||
|
||||
# Apply overrides if provided
|
||||
if config_override:
|
||||
app.config.update(config_override)
|
||||
|
||||
@@ -107,6 +107,16 @@ class ConnectionPool:
|
||||
if not conn:
|
||||
return
|
||||
|
||||
# A request that failed mid-write must not hand an open transaction
|
||||
# (and its write lock) to the next request (v2.6.5)
|
||||
if conn.in_transaction:
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
conn.close()
|
||||
self._stats['connections_closed'] += 1
|
||||
return
|
||||
|
||||
with self._lock:
|
||||
if len(self._pool) < self.pool_size:
|
||||
# Return to pool
|
||||
|
||||
@@ -82,6 +82,11 @@ def register_error_handlers(app):
|
||||
|
||||
return render_template('404.html'), 404
|
||||
|
||||
@app.errorhandler(410)
|
||||
def gone(error):
|
||||
"""Handle 410 Gone (deleted notes, Webmention 3.1.5)"""
|
||||
return render_template('410.html'), 410
|
||||
|
||||
@app.errorhandler(405)
|
||||
def method_not_allowed(error):
|
||||
"""Handle 405 Method Not Allowed errors"""
|
||||
|
||||
@@ -198,7 +198,7 @@ def generate_atom_streaming(
|
||||
yield f' <link rel="enclosure" type="{_escape_xml(mime_type)}" href="{_escape_xml(media_url)}" length="{size}"{title_attr}/>\n'
|
||||
|
||||
# Content - include media as HTML (per Q24)
|
||||
if note.html:
|
||||
if note.feed_html:
|
||||
# Build HTML content with media at top
|
||||
html_content = ""
|
||||
|
||||
@@ -212,7 +212,7 @@ def generate_atom_streaming(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += note.html
|
||||
html_content += note.feed_html
|
||||
|
||||
# HTML content - escaped
|
||||
yield ' <content type="html">'
|
||||
|
||||
@@ -267,7 +267,7 @@ def _build_item_object(site_url: str, note: Note) -> Dict[str, Any]:
|
||||
|
||||
# Add content (HTML or text)
|
||||
# Per Q24: Include media as HTML in content_html
|
||||
if note.html:
|
||||
if note.feed_html:
|
||||
content_html = ""
|
||||
|
||||
# Add media at top if present (v1.2.0 Phase 3)
|
||||
@@ -280,7 +280,7 @@ def _build_item_object(site_url: str, note: Note) -> Dict[str, Any]:
|
||||
content_html += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
content_html += note.html
|
||||
content_html += note.feed_html
|
||||
item["content_html"] = content_html
|
||||
else:
|
||||
item["content_text"] = note.content
|
||||
|
||||
@@ -137,7 +137,7 @@ def generate_rss(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += clean_html_for_rss(note.html)
|
||||
html_content += clean_html_for_rss(note.feed_html)
|
||||
|
||||
# feedgen automatically wraps content in CDATA for RSS
|
||||
fe.description(html_content)
|
||||
@@ -278,7 +278,7 @@ def generate_rss_streaming(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += clean_html_for_rss(note.html)
|
||||
html_content += clean_html_for_rss(note.feed_html)
|
||||
|
||||
# Build item XML
|
||||
item_xml = f""" <item>
|
||||
|
||||
@@ -22,6 +22,7 @@ from bs4 import BeautifulSoup
|
||||
from flask import current_app
|
||||
|
||||
from starpunk.database import get_db
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
CODE_LIFETIME_MINUTES = 10
|
||||
SUPPORTED_SCOPES = ["create", "update", "delete", "media", "profile"]
|
||||
@@ -55,13 +56,6 @@ def issuer() -> str:
|
||||
return current_app.config["SITE_URL"].rstrip("/") + "/"
|
||||
|
||||
|
||||
def site_url_for(endpoint: str) -> str:
|
||||
"""Absolute URL built from SITE_URL (request host/scheme is unreliable behind a proxy)"""
|
||||
from flask import url_for
|
||||
|
||||
return current_app.config["SITE_URL"].rstrip("/") + url_for(endpoint)
|
||||
|
||||
|
||||
def admin_me() -> str:
|
||||
"""The single identity StarPunk issues tokens for"""
|
||||
return current_app.config["ADMIN_ME"]
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Markdown extension: turn bare URLs into links (v2.3.0)
|
||||
|
||||
Python-Markdown only links <https://...>; notes often contain pasted URLs.
|
||||
"""
|
||||
|
||||
import re
|
||||
import xml.etree.ElementTree as etree
|
||||
|
||||
from markdown.extensions import Extension
|
||||
from markdown.inlinepatterns import InlineProcessor
|
||||
from markdown.treeprocessors import Treeprocessor
|
||||
from markdown.util import AtomicString
|
||||
|
||||
# Stop at whitespace and brackets; trailing punctuation is not part of the URL
|
||||
BARE_URL_RE = r"(?<![\w/\"'=])https?://[^\s<>()\[\]\"']*[^\s<>()\[\]\"'.,;:!?…]"
|
||||
|
||||
|
||||
class _BareUrl(InlineProcessor):
|
||||
def handleMatch(self, m, data):
|
||||
url = m.group(0)
|
||||
el = etree.Element("a")
|
||||
el.set("href", url)
|
||||
el.text = AtomicString(url)
|
||||
return el, m.start(0), m.end(0)
|
||||
|
||||
|
||||
class _UnnestLinks(Treeprocessor):
|
||||
"""[https://x](https://x) would put a link inside a link; keep the outer one"""
|
||||
|
||||
def run(self, root):
|
||||
for outer in root.iter("a"):
|
||||
for inner in list(outer.iter("a"))[1:]:
|
||||
inner.tag = "span"
|
||||
inner.attrib.clear()
|
||||
|
||||
|
||||
class BareUrlExtension(Extension):
|
||||
def extendMarkdown(self, md):
|
||||
# Below links (160) and <autolinks> (120), so those win
|
||||
md.inlinePatterns.register(_BareUrl(BARE_URL_RE, md), "bare_url", 105)
|
||||
md.treeprocessors.register(_UnnestLinks(md), "unnest_links", 0)
|
||||
+16
-12
@@ -868,20 +868,24 @@ def attach_media_to_note(note_id: int, media_ids: List[int], captions: List[str]
|
||||
|
||||
db = get_db(current_app)
|
||||
|
||||
# Delete existing associations (for edit case)
|
||||
db.execute("DELETE FROM note_media WHERE note_id = ?", (note_id,))
|
||||
try:
|
||||
# Delete existing associations (for edit case)
|
||||
db.execute("DELETE FROM note_media WHERE note_id = ?", (note_id,))
|
||||
|
||||
# Insert new associations
|
||||
for i, (media_id, caption) in enumerate(zip(media_ids, captions)):
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO note_media (note_id, media_id, display_order, caption)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(note_id, media_id, i, caption or None)
|
||||
)
|
||||
# Insert new associations
|
||||
for i, (media_id, caption) in enumerate(zip(media_ids, captions)):
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO note_media (note_id, media_id, display_order, caption)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(note_id, media_id, i, caption or None)
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
|
||||
|
||||
def get_note_media(note_id: int) -> List[Dict]:
|
||||
|
||||
+519
-190
@@ -7,9 +7,11 @@ interface for creating posts via external clients.
|
||||
Functions:
|
||||
normalize_properties: Convert form/JSON data to Micropub properties format
|
||||
extract_content: Get content from Micropub properties
|
||||
extract_title: Get or generate title from Micropub properties
|
||||
extract_title: Get the explicit title (name) from Micropub properties
|
||||
extract_tags: Get category tags from Micropub properties
|
||||
handle_create: Process Micropub create action
|
||||
handle_update: Process Micropub update action (v2.4.0)
|
||||
handle_delete: Process Micropub delete / undelete actions (v2.4.0)
|
||||
handle_query: Process Micropub query endpoints
|
||||
extract_bearer_token: Get token from Authorization header or form
|
||||
|
||||
@@ -28,8 +30,13 @@ from typing import Optional
|
||||
|
||||
from flask import Request, current_app, jsonify
|
||||
|
||||
from starpunk.notes import create_note, get_note, InvalidNoteDataError, NoteNotFoundError
|
||||
from starpunk.models import split_title
|
||||
from starpunk.notes import (
|
||||
create_note, delete_note, get_note, list_notes, undelete_note, update_note,
|
||||
InvalidNoteDataError, NoteNotFoundError,
|
||||
)
|
||||
from starpunk.indieauth import check_scope
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
|
||||
# Custom Exceptions
|
||||
@@ -135,6 +142,8 @@ def normalize_properties(data: dict) -> dict:
|
||||
# Form format - convert to properties dict
|
||||
properties = {}
|
||||
for key, value in data.items():
|
||||
if hasattr(value, "filename") or (isinstance(value, list) and value and hasattr(value[0], "filename")):
|
||||
continue # uploaded files are handled separately
|
||||
# Skip reserved Micropub parameters
|
||||
if key.startswith("mp-") or key in ["action", "url", "access_token", "h"]:
|
||||
continue
|
||||
@@ -186,40 +195,43 @@ def extract_content(properties: dict) -> str:
|
||||
return content.strip()
|
||||
|
||||
|
||||
def _has_text(content) -> bool:
|
||||
"""Whether a content property has any text. Likes, reposts (v2.5.0) and
|
||||
photo posts (v2.6.5) may come without."""
|
||||
values = content if isinstance(content, list) else [content]
|
||||
return any(
|
||||
(c.strip() if isinstance(c, str) else (c.get("text") or c.get("html") or "").strip())
|
||||
for c in values if c
|
||||
)
|
||||
|
||||
|
||||
def extract_title(properties: dict) -> Optional[str]:
|
||||
"""
|
||||
Extract or generate title from Micropub properties
|
||||
Extract the explicit title (Micropub 'name' property)
|
||||
|
||||
Per ADR-029 mapping rules:
|
||||
1. Use 'name' property if provided
|
||||
2. If no name, extract from content (first line, max 50 chars)
|
||||
StarPunk has no title field: a note's title is a '# Heading' first line
|
||||
(see Note.title), so the caller stores the name as that heading.
|
||||
|
||||
Args:
|
||||
properties: Normalized Micropub properties dict
|
||||
|
||||
Returns:
|
||||
Title string or None
|
||||
Title on a single line, or None if no name was sent
|
||||
"""
|
||||
# Try explicit name property first
|
||||
name = properties.get("name", [""])[0]
|
||||
if name:
|
||||
return name.strip()
|
||||
name = (properties.get("name") or [""])[0]
|
||||
if not isinstance(name, str):
|
||||
return None
|
||||
return " ".join(name.split()) or None
|
||||
|
||||
# Generate from content (first line, max 50 chars)
|
||||
content_list = properties.get("content", [])
|
||||
if content_list:
|
||||
content = content_list[0]
|
||||
# Handle structured content
|
||||
if isinstance(content, dict):
|
||||
content = content.get("text") or content.get("html", "")
|
||||
|
||||
if content:
|
||||
first_line = content.split("\n")[0].strip()
|
||||
if len(first_line) > 50:
|
||||
return first_line[:50] + "..."
|
||||
return first_line
|
||||
|
||||
return None
|
||||
def add_title_heading(content: str, title: Optional[str]) -> str:
|
||||
"""Prefix content with '# title' unless it already starts with that heading"""
|
||||
if not title:
|
||||
return content
|
||||
first_line = content.lstrip().split("\n", 1)[0]
|
||||
if first_line.startswith("#") and first_line.lstrip("#").strip() == title:
|
||||
return content
|
||||
return f"# {title}\n\n{content}"
|
||||
|
||||
|
||||
def extract_tags(properties: dict) -> list[str]:
|
||||
@@ -263,6 +275,51 @@ def extract_published_date(properties: dict) -> Optional[datetime]:
|
||||
|
||||
# Action Handlers
|
||||
|
||||
MAX_PHOTOS = 4 # ADR-057
|
||||
MAX_REMOTE_PHOTO_BYTES = 20 * 1024 * 1024
|
||||
|
||||
# Properties we can store, and so can be updated (v2.4.0)
|
||||
UPDATABLE = {"content", "name", "category", "in-reply-to", "post-status", "photo"}
|
||||
|
||||
|
||||
def _first(value):
|
||||
if isinstance(value, list):
|
||||
return value[0] if value else None
|
||||
return value
|
||||
|
||||
|
||||
def _require_scope(token_info: dict, *scopes: str) -> None:
|
||||
granted = token_info.get("scope", "")
|
||||
if not any(check_scope(s, granted) for s in scopes):
|
||||
raise MicropubError(
|
||||
"insufficient_scope", f"Token lacks {' or '.join(scopes)} scope", status_code=403
|
||||
)
|
||||
|
||||
|
||||
def extract_post_status(properties: dict) -> bool:
|
||||
"""post-status: 'draft' -> unpublished; 'published' or absent -> published"""
|
||||
status = _first(properties.get("post-status"))
|
||||
if status in (None, "", "published"):
|
||||
return True
|
||||
if status == "draft":
|
||||
return False
|
||||
raise MicropubValidationError(f"Unsupported post-status: {status}")
|
||||
|
||||
|
||||
def extract_url_property(properties: dict, name: str) -> Optional[str]:
|
||||
"""A URL property (in-reply-to, like-of, repost-of): string or h-cite"""
|
||||
value = _first(properties.get(name))
|
||||
if isinstance(value, dict): # h-cite
|
||||
value = _first((value.get("properties") or {}).get("url")) or value.get("value")
|
||||
value = (value or "").strip() if isinstance(value, str) else ""
|
||||
if value and not value.startswith(("http://", "https://")):
|
||||
raise MicropubValidationError(f"{name} must be an http(s) URL")
|
||||
return value or None
|
||||
|
||||
|
||||
def extract_in_reply_to(properties: dict) -> Optional[str]:
|
||||
return extract_url_property(properties, "in-reply-to")
|
||||
|
||||
|
||||
def extract_photos(properties: dict) -> list[dict[str, str]]:
|
||||
"""
|
||||
@@ -270,101 +327,173 @@ def extract_photos(properties: dict) -> list[dict[str, str]]:
|
||||
|
||||
Handles both simple URL strings and structured photo objects with alt text.
|
||||
|
||||
Args:
|
||||
properties: Normalized Micropub properties dict
|
||||
|
||||
Returns:
|
||||
List of dicts with 'url' and optional 'alt' keys
|
||||
|
||||
Examples:
|
||||
>>> # Simple URL
|
||||
>>> extract_photos({'photo': ['https://example.com/photo.jpg']})
|
||||
[{'url': 'https://example.com/photo.jpg', 'alt': ''}]
|
||||
|
||||
>>> # With alt text
|
||||
>>> extract_photos({'photo': [{'value': 'https://example.com/photo.jpg', 'alt': 'Sunset'}]})
|
||||
[{'url': 'https://example.com/photo.jpg', 'alt': 'Sunset'}]
|
||||
"""
|
||||
photos = properties.get("photo", [])
|
||||
result = []
|
||||
|
||||
for photo in photos:
|
||||
for photo in properties.get("photo", []):
|
||||
if isinstance(photo, str):
|
||||
# Simple URL string
|
||||
result.append({'url': photo, 'alt': ''})
|
||||
elif isinstance(photo, dict):
|
||||
# Structured object with value and alt
|
||||
url = photo.get('value') or photo.get('url', '')
|
||||
alt = photo.get('alt', '')
|
||||
if url:
|
||||
result.append({'url': url, 'alt': alt})
|
||||
|
||||
result.append({'url': url, 'alt': photo.get('alt', '')})
|
||||
return result
|
||||
|
||||
|
||||
def _attach_photos_to_note(note_id: int, photos: list[dict[str, str]]) -> None:
|
||||
"""
|
||||
Attach photos to a note by URL
|
||||
def _public_address(host: str) -> bool:
|
||||
"""True if every address the host resolves to is public (no SSRF into the LAN)"""
|
||||
import ipaddress
|
||||
import socket
|
||||
|
||||
Photos must already exist on this server (uploaded via media endpoint).
|
||||
External URLs are accepted but stored as-is (no download).
|
||||
try:
|
||||
infos = socket.getaddrinfo(host, None)
|
||||
except socket.gaierror:
|
||||
return False
|
||||
for info in infos:
|
||||
ip = ipaddress.ip_address(info[4][0])
|
||||
if not ip.is_global:
|
||||
return False
|
||||
return bool(infos)
|
||||
|
||||
Args:
|
||||
note_id: ID of the note to attach to
|
||||
photos: List of dicts with 'url' and 'alt' keys
|
||||
"""
|
||||
|
||||
def _fetch_remote_photo(url: str) -> Optional[int]:
|
||||
"""Download a photo hosted elsewhere into the media library. Returns media id."""
|
||||
from urllib.parse import urlparse
|
||||
import httpx
|
||||
from starpunk.media import save_media
|
||||
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https") or not parsed.hostname:
|
||||
return None
|
||||
if not current_app.config.get("TESTING") and not _public_address(parsed.hostname):
|
||||
current_app.logger.warning(f"Refusing to fetch photo from non-public address: {url}")
|
||||
return None
|
||||
try:
|
||||
with httpx.stream("GET", url, timeout=20.0, follow_redirects=True) as response:
|
||||
response.raise_for_status()
|
||||
data = bytearray()
|
||||
for chunk in response.iter_bytes():
|
||||
data += chunk
|
||||
if len(data) > MAX_REMOTE_PHOTO_BYTES:
|
||||
raise ValueError("photo too large")
|
||||
name = parsed.path.rsplit("/", 1)[-1] or "photo.jpg"
|
||||
return save_media(bytes(data), name)["id"]
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Could not fetch photo {url}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def _media_id_for_url(url: str) -> Optional[int]:
|
||||
"""Media id for a photo URL: ours by path, or downloaded if hosted elsewhere"""
|
||||
from starpunk.database import get_db
|
||||
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000").rstrip('/')
|
||||
if url.startswith(site_url) or url.startswith('/media/'):
|
||||
path = url[len(site_url):] if url.startswith(site_url) else url
|
||||
if path.startswith('/media/'):
|
||||
path = path[len('/media/'):]
|
||||
row = get_db(current_app).execute("SELECT id FROM media WHERE path = ?", (path,)).fetchone()
|
||||
if row:
|
||||
return row[0]
|
||||
current_app.logger.warning(f"Photo URL not found in media: {url}")
|
||||
return None
|
||||
return _fetch_remote_photo(url)
|
||||
|
||||
|
||||
def _save_uploads(files) -> list[dict]:
|
||||
"""Photos uploaded in a multipart create request -> [{'media_id', 'alt'}]"""
|
||||
from starpunk.media import save_media
|
||||
|
||||
result = []
|
||||
for key in ("photo", "photo[]"):
|
||||
for upload in (files.getlist(key) if files else []):
|
||||
if upload and upload.filename:
|
||||
try:
|
||||
result.append({"media_id": save_media(upload.read(), upload.filename)["id"], "alt": ""})
|
||||
except ValueError as e:
|
||||
raise MicropubValidationError(f"Invalid photo {upload.filename}: {e}")
|
||||
return result
|
||||
|
||||
|
||||
def _resolve_photos(photos: list[dict]) -> tuple[list[int], list[str]]:
|
||||
"""Photo dicts ({'url'|'media_id', 'alt'}) -> (media ids, captions), max 4"""
|
||||
if len(photos) > MAX_PHOTOS:
|
||||
current_app.logger.warning(
|
||||
f"Micropub received {len(photos)} photos, truncating to {MAX_PHOTOS} per ADR-057"
|
||||
)
|
||||
media_ids, captions = [], []
|
||||
for photo in photos:
|
||||
if len(media_ids) == MAX_PHOTOS:
|
||||
break
|
||||
media_id = photo.get("media_id") or _media_id_for_url(photo["url"])
|
||||
# The same photo twice is shown once (v2.6.5)
|
||||
if media_id and media_id not in media_ids:
|
||||
media_ids.append(media_id)
|
||||
captions.append(photo.get("alt", ""))
|
||||
return media_ids, captions
|
||||
|
||||
|
||||
def _attach_photos_to_note(note_id: int, photos: list[dict]) -> None:
|
||||
"""Attach photos (by URL or uploaded media id) to a note, replacing any others"""
|
||||
from starpunk.media import attach_media_to_note
|
||||
|
||||
# Normalize SITE_URL by stripping trailing slash for consistent comparison
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000").rstrip('/')
|
||||
db = get_db(current_app)
|
||||
|
||||
media_ids = []
|
||||
captions = []
|
||||
|
||||
# Log warning if photos are being truncated
|
||||
if len(photos) > 4:
|
||||
current_app.logger.warning(
|
||||
f"Micropub create received {len(photos)} photos, truncating to 4 per ADR-057"
|
||||
)
|
||||
|
||||
for photo in photos[:4]: # Max 4 photos per ADR-057
|
||||
url = photo['url']
|
||||
alt = photo.get('alt', '')
|
||||
|
||||
# Check if URL is on our server
|
||||
if url.startswith(site_url) or url.startswith('/media/'):
|
||||
# Extract path from URL
|
||||
if url.startswith(site_url):
|
||||
path = url[len(site_url):]
|
||||
else:
|
||||
path = url
|
||||
|
||||
# Remove leading /media/ if present
|
||||
if path.startswith('/media/'):
|
||||
path = path[7:]
|
||||
|
||||
# Look up media by path
|
||||
row = db.execute(
|
||||
"SELECT id FROM media WHERE path = ?",
|
||||
(path,)
|
||||
).fetchone()
|
||||
|
||||
if row:
|
||||
media_ids.append(row[0])
|
||||
captions.append(alt)
|
||||
else:
|
||||
current_app.logger.warning(f"Photo URL not found in media: {url}")
|
||||
else:
|
||||
# External URL - log but don't fail
|
||||
current_app.logger.info(f"External photo URL ignored: {url}")
|
||||
|
||||
if media_ids:
|
||||
media_ids, captions = _resolve_photos(photos)
|
||||
if media_ids or not photos:
|
||||
attach_media_to_note(note_id, media_ids, captions)
|
||||
|
||||
|
||||
def handle_create(data: dict, token_info: dict):
|
||||
def _note_photos(note) -> list[dict]:
|
||||
"""A note's current photos as Micropub photo dicts"""
|
||||
from starpunk.media import get_note_media
|
||||
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000").rstrip('/')
|
||||
return [
|
||||
{"url": f"{site_url}/media/{m['path']}", "alt": m.get("caption") or "", "media_id": m["id"]}
|
||||
for m in get_note_media(note.id)
|
||||
]
|
||||
|
||||
|
||||
def _check_type(data: dict) -> None:
|
||||
"""Only h-entry is supported"""
|
||||
h = _first(data.get("h"))
|
||||
kind = _first(data.get("type"))
|
||||
if (h and h != "entry") or (kind and kind != "h-entry"):
|
||||
raise MicropubValidationError(f"Only h-entry posts are supported (got {h or kind})")
|
||||
|
||||
|
||||
def _note_from_url(url: Optional[str], deleted: bool = False):
|
||||
"""Find the note a URL points at (/note/<slug> on this site)"""
|
||||
from urllib.parse import urlparse
|
||||
from starpunk.database import get_db
|
||||
|
||||
if not url:
|
||||
raise MicropubValidationError("No url provided")
|
||||
site = urlparse(current_app.config.get("SITE_URL", ""))
|
||||
parsed = urlparse(url)
|
||||
if parsed.netloc and site.netloc and parsed.netloc != site.netloc:
|
||||
raise MicropubValidationError("URL is not on this site")
|
||||
parts = [p for p in parsed.path.split("/") if p]
|
||||
if len(parts) != 2 or parts[0] not in ("note", "notes"):
|
||||
raise MicropubValidationError("URL is not a post on this site")
|
||||
slug = parts[1]
|
||||
if deleted:
|
||||
row = get_db(current_app).execute(
|
||||
"SELECT id FROM notes WHERE slug = ? AND deleted_at IS NOT NULL", (slug,)
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise MicropubError("not_found", "No deleted post at that URL", 404)
|
||||
return slug
|
||||
note = get_note(slug=slug)
|
||||
if note is None:
|
||||
raise MicropubError("not_found", "Post not found", 404)
|
||||
return note
|
||||
|
||||
|
||||
def handle_create(data: dict, token_info: dict, files=None):
|
||||
"""
|
||||
Handle Micropub create action
|
||||
|
||||
@@ -374,154 +503,354 @@ def handle_create(data: dict, token_info: dict):
|
||||
Args:
|
||||
data: Raw request data (form or JSON)
|
||||
token_info: Authenticated token information (me, client_id, scope)
|
||||
files: Uploaded files for multipart requests (v2.4.0)
|
||||
|
||||
Returns:
|
||||
Tuple of (response_body, status_code, headers)
|
||||
|
||||
Raises:
|
||||
MicropubError: If scope insufficient or creation fails
|
||||
"""
|
||||
# Check scope
|
||||
if not check_scope("create", token_info.get("scope", "")):
|
||||
raise MicropubError(
|
||||
"insufficient_scope", "Token lacks create scope", status_code=403
|
||||
)
|
||||
_require_scope(token_info, "create")
|
||||
_check_type(data)
|
||||
|
||||
# Extract mp-slug BEFORE normalizing properties (it's not a property!)
|
||||
# mp-slug is a Micropub server extension parameter that gets filtered during normalization
|
||||
# mp-slug and mp-syndicate-to are server commands, not properties
|
||||
custom_slug = None
|
||||
if isinstance(data, dict) and 'mp-slug' in data:
|
||||
# Handle both form-encoded (list) and JSON (could be string or list)
|
||||
slug_value = data.get('mp-slug')
|
||||
if isinstance(slug_value, list) and slug_value:
|
||||
custom_slug = slug_value[0]
|
||||
elif isinstance(slug_value, str):
|
||||
custom_slug = slug_value
|
||||
|
||||
# Normalize and extract properties
|
||||
# Absent means "all targets"; present means exactly those (v2.1.0)
|
||||
requested_targets = None
|
||||
if isinstance(data, dict):
|
||||
for key in ("mp-syndicate-to", "mp-syndicate-to[]"):
|
||||
if key in data:
|
||||
value = data[key]
|
||||
requested_targets = (requested_targets or []) + (
|
||||
value if isinstance(value, list) else [value]
|
||||
)
|
||||
|
||||
try:
|
||||
properties = normalize_properties(data)
|
||||
content = extract_content(properties)
|
||||
like_of = extract_url_property(properties, "like-of")
|
||||
repost_of = extract_url_property(properties, "repost-of")
|
||||
if like_of and repost_of:
|
||||
raise MicropubValidationError("A post can't be both a like and a repost")
|
||||
has_content = _has_text(properties.get("content"))
|
||||
photos = extract_photos(properties) + _save_uploads(files)
|
||||
# Photo posts may have no text (v2.6.5): Micropub doesn't require content
|
||||
content = (
|
||||
extract_content(properties)
|
||||
if has_content or not (like_of or repost_of or photos)
|
||||
else ""
|
||||
)
|
||||
title = extract_title(properties)
|
||||
tags = extract_tags(properties)
|
||||
published_date = extract_published_date(properties)
|
||||
photos = extract_photos(properties) # v1.4.0
|
||||
|
||||
except MicropubValidationError as e:
|
||||
raise e
|
||||
published = extract_post_status(properties)
|
||||
in_reply_to = extract_in_reply_to(properties)
|
||||
# Look up and download photos before the note exists, so a bad photo
|
||||
# can't leave a half-made post behind (v2.6.5)
|
||||
media_ids, captions = _resolve_photos(photos)
|
||||
except MicropubError:
|
||||
raise
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Property extraction failed: {e}")
|
||||
raise MicropubValidationError(f"Failed to parse request: {str(e)}")
|
||||
|
||||
# Create note using existing CRUD
|
||||
note = None
|
||||
try:
|
||||
note = create_note(
|
||||
content=content,
|
||||
published=True, # Micropub posts are published by default
|
||||
content=add_title_heading(content, title),
|
||||
published=published,
|
||||
created_at=published_date,
|
||||
custom_slug=custom_slug,
|
||||
tags=tags if tags else None # Pass tags to create_note (v1.3.0)
|
||||
tags=tags if tags else None,
|
||||
in_reply_to=in_reply_to,
|
||||
like_of=like_of,
|
||||
repost_of=repost_of,
|
||||
has_media=bool(media_ids),
|
||||
)
|
||||
|
||||
# Attach photos if present (v1.4.0)
|
||||
if photos:
|
||||
_attach_photos_to_note(note.id, photos)
|
||||
if media_ids:
|
||||
from starpunk.media import attach_media_to_note
|
||||
attach_media_to_note(note.id, media_ids, captions)
|
||||
|
||||
# Build permalink URL
|
||||
# Note: SITE_URL is normalized to include trailing slash (for IndieAuth spec compliance)
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000")
|
||||
permalink = f"{site_url}notes/{note.slug}"
|
||||
# Crosspost after photos are attached (v2.1.0); drafts wait until published
|
||||
from starpunk.syndication import get_targets, resolve_targets, select_targets
|
||||
if requested_targets is None:
|
||||
select_targets(note.id, list(get_targets()))
|
||||
else:
|
||||
select_targets(note.id, resolve_targets(requested_targets))
|
||||
|
||||
# Return 201 Created with Location header
|
||||
return "", 201, {"Location": permalink}
|
||||
return "", 201, {"Location": absolute_url_for("public.note", slug=note.slug)}
|
||||
|
||||
except InvalidNoteDataError as e:
|
||||
raise MicropubValidationError(str(e))
|
||||
except MicropubError:
|
||||
raise
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Failed to create note via Micropub: {e}")
|
||||
raise MicropubError(
|
||||
"server_error", "Failed to create post", status_code=500
|
||||
)
|
||||
# The client is told it failed, so don't leave the post up (v2.6.5)
|
||||
if note is not None:
|
||||
try:
|
||||
from starpunk.notes import delete_note
|
||||
delete_note(id=note.id, soft=False)
|
||||
except Exception as cleanup:
|
||||
current_app.logger.error(f"Could not remove failed note {note.slug}: {cleanup}")
|
||||
raise MicropubError("server_error", "Failed to create post", status_code=500)
|
||||
|
||||
|
||||
def handle_query(args: dict, token_info: dict):
|
||||
def handle_update(data: dict, token_info: dict):
|
||||
"""
|
||||
Handle Micropub update action (v2.4.0)
|
||||
|
||||
JSON body: {"action": "update", "url": ..., "replace": {...},
|
||||
"add": {...}, "delete": [...] or {...}}. Edits follow to crossposts.
|
||||
"""
|
||||
from starpunk.syndication import note_changed
|
||||
|
||||
_require_scope(token_info, "update")
|
||||
note = _note_from_url(_first(data.get("url")))
|
||||
|
||||
replace = data.get("replace") or {}
|
||||
add = data.get("add") or {}
|
||||
delete = data.get("delete") or {}
|
||||
if not isinstance(replace, dict) or not isinstance(add, dict) or not isinstance(delete, (dict, list)):
|
||||
raise MicropubValidationError("replace and add must be objects; delete an object or list")
|
||||
for prop, values in list(replace.items()) + list(add.items()):
|
||||
if not isinstance(values, list):
|
||||
raise MicropubValidationError(f"Values for {prop} must be an array")
|
||||
touched = set(replace) | set(add) | set(delete)
|
||||
unsupported = sorted(touched - UPDATABLE)
|
||||
if unsupported:
|
||||
raise MicropubValidationError(f"Updating {', '.join(unsupported)} is not supported")
|
||||
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 get_note_tags(note.id, entered_order=True)]
|
||||
photos = _note_photos(note)
|
||||
changes = {}
|
||||
|
||||
# Removals first, then replacements, then additions
|
||||
if isinstance(delete, list):
|
||||
for prop in delete:
|
||||
if prop == "name":
|
||||
title = None
|
||||
elif prop == "content":
|
||||
body = ""
|
||||
elif prop == "category":
|
||||
tags = []
|
||||
elif prop == "photo":
|
||||
photos = []
|
||||
elif prop == "in-reply-to":
|
||||
changes["in_reply_to"] = ""
|
||||
elif prop == "post-status":
|
||||
changes["published"] = True
|
||||
else:
|
||||
for prop, values in delete.items():
|
||||
if prop == "category":
|
||||
drop = {v.lower() for v in values}
|
||||
tags = [t for t in tags if t.lower() not in drop]
|
||||
elif prop == "photo":
|
||||
drop = {p["url"] for p in extract_photos({"photo": values})}
|
||||
photos = [p for p in photos if p["url"] not in drop]
|
||||
else:
|
||||
raise MicropubValidationError(f"Can't delete individual values of {prop}")
|
||||
|
||||
if "content" in replace:
|
||||
# Empty text is fine if the note keeps its photos; update_note checks (v2.6.5)
|
||||
body = extract_content(replace) if _has_text(replace["content"]) else ""
|
||||
if "name" in replace:
|
||||
title = extract_title(replace)
|
||||
if "category" in replace:
|
||||
tags = extract_tags(replace)
|
||||
if "photo" in replace:
|
||||
photos = extract_photos(replace)
|
||||
if "in-reply-to" in replace:
|
||||
changes["in_reply_to"] = extract_in_reply_to(replace) or ""
|
||||
if "post-status" in replace:
|
||||
changes["published"] = extract_post_status(replace)
|
||||
|
||||
if "category" in add:
|
||||
known = {t.lower() for t in tags}
|
||||
tags += [t for t in extract_tags(add) if t.lower() not in known]
|
||||
if "photo" in add:
|
||||
photos += extract_photos(add)
|
||||
|
||||
# Text may go only if photos stay (or it's a like or repost) (v2.6.5)
|
||||
if not (body or "").strip() and not (photos or note.like_of or note.repost_of):
|
||||
raise MicropubValidationError("A post needs text or a photo")
|
||||
|
||||
new_content = add_title_heading(body, title)
|
||||
if new_content != note.content:
|
||||
changes["content"] = new_content
|
||||
if {"category"} & touched:
|
||||
changes["tags"] = tags
|
||||
|
||||
try:
|
||||
if changes:
|
||||
update_note(id=note.id, has_media=bool(photos), **changes)
|
||||
if "photo" in touched:
|
||||
_attach_photos_to_note(note.id, photos)
|
||||
note_changed(note.id)
|
||||
except InvalidNoteDataError as e:
|
||||
raise MicropubValidationError(str(e))
|
||||
except MicropubError:
|
||||
raise
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Failed to update note via Micropub: {e}")
|
||||
raise MicropubError("server_error", "Failed to update post", status_code=500)
|
||||
|
||||
return "", 204
|
||||
|
||||
|
||||
def handle_delete(data: dict, token_info: dict, undelete: bool = False):
|
||||
"""Handle Micropub delete / undelete (v2.4.0). Deletes are soft so they can be undone."""
|
||||
_require_scope(token_info, "delete")
|
||||
url = _first(data.get("url"))
|
||||
try:
|
||||
if undelete:
|
||||
undelete_note(_note_from_url(url, deleted=True))
|
||||
else:
|
||||
delete_note(id=_note_from_url(url).id, soft=True)
|
||||
except MicropubError:
|
||||
raise
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Micropub {'undelete' if undelete else 'delete'} failed: {e}")
|
||||
raise MicropubError("server_error", "Failed to change post", status_code=500)
|
||||
return "", 204
|
||||
|
||||
|
||||
def _syndicate_to() -> list:
|
||||
"""Configured crosspost targets in Micropub syndicate-to form"""
|
||||
from starpunk.syndication import get_targets
|
||||
|
||||
return [{"uid": t.uid, "name": t.name} for t in get_targets().values()]
|
||||
|
||||
|
||||
def note_to_mf2(note, wanted: Optional[list] = None) -> dict:
|
||||
"""
|
||||
A note as Microformats2 JSON, the inverse of create
|
||||
|
||||
A '# Heading' first line is the title, returned as name.
|
||||
"""
|
||||
from starpunk.syndication import get_note_syndications
|
||||
|
||||
title, content = split_title(note.content)
|
||||
props = {
|
||||
"content": [content],
|
||||
"published": [note.created_at.isoformat()],
|
||||
"url": [absolute_url_for("public.note", slug=note.slug)],
|
||||
"post-status": ["published" if note.published else "draft"],
|
||||
}
|
||||
if title:
|
||||
props["name"] = [title]
|
||||
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):
|
||||
props["like-of"] = [note.like_of]
|
||||
if getattr(note, "repost_of", None):
|
||||
props["repost-of"] = [note.repost_of]
|
||||
photos = _note_photos(note)
|
||||
if photos:
|
||||
props["photo"] = [{"value": p["url"], "alt": p["alt"]} for p in photos]
|
||||
synced = [s["remote_url"] for s in get_note_syndications(note.id) if s.get("remote_url")]
|
||||
if synced:
|
||||
props["syndication"] = synced
|
||||
|
||||
if wanted:
|
||||
return {"properties": {k: v for k, v in props.items() if k in wanted}}
|
||||
return {"type": ["h-entry"], "properties": props}
|
||||
|
||||
|
||||
def _all_categories(prefix: str = "") -> list:
|
||||
"""Tags in use, most used first (q=category)"""
|
||||
from starpunk.database import get_db
|
||||
|
||||
rows = get_db(current_app).execute(
|
||||
"""
|
||||
SELECT t.display_name, COUNT(nt.note_id) AS uses
|
||||
FROM tags t LEFT JOIN note_tags nt ON nt.tag_id = t.id
|
||||
GROUP BY t.id ORDER BY uses DESC, t.display_name
|
||||
"""
|
||||
).fetchall()
|
||||
prefix = prefix.lower()
|
||||
return [r["display_name"] for r in rows if not prefix or prefix in r["display_name"].lower()]
|
||||
|
||||
|
||||
QUERIES = ["config", "source", "syndicate-to", "category"]
|
||||
|
||||
|
||||
def handle_query(args, token_info: dict):
|
||||
"""
|
||||
Handle Micropub query endpoints
|
||||
|
||||
Supports:
|
||||
- q=config: Return server configuration
|
||||
- q=source: Return post source in Microformats2 JSON
|
||||
- q=syndicate-to: Return syndication targets (empty for V1)
|
||||
- q=config: Server configuration
|
||||
- q=source&url=...[&properties[]=...]: One post in Microformats2 JSON
|
||||
- q=source (no url) [&limit=N&offset=N]: Recent posts, drafts included
|
||||
- q=syndicate-to: Crosspost targets
|
||||
- q=category[&filter=...]: Tags in use
|
||||
|
||||
Args:
|
||||
args: Query string arguments
|
||||
token_info: Authenticated token information
|
||||
|
||||
Returns:
|
||||
Tuple of (response, status_code)
|
||||
args: Query arguments (werkzeug MultiDict or plain dict)
|
||||
"""
|
||||
def get_list(key):
|
||||
if hasattr(args, "getlist"):
|
||||
return args.getlist(key) + args.getlist(f"{key}[]")
|
||||
value = args.get(key) or args.get(f"{key}[]")
|
||||
return value if isinstance(value, list) else ([value] if value else [])
|
||||
|
||||
q = args.get("q")
|
||||
|
||||
if q == "config":
|
||||
# Return server configuration with media endpoint (v1.4.0)
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000").rstrip('/')
|
||||
config = {
|
||||
return jsonify({
|
||||
"media-endpoint": f"{site_url}/micropub/media",
|
||||
"syndicate-to": [], # No syndication targets in V1
|
||||
"syndicate-to": _syndicate_to(),
|
||||
"q": QUERIES,
|
||||
"post-types": [
|
||||
{"type": "note", "name": "Note", "properties": ["content"]},
|
||||
{"type": "photo", "name": "Photo", "properties": ["photo"]}
|
||||
{"type": "article", "name": "Article", "properties": ["name", "content"]},
|
||||
{"type": "photo", "name": "Photo", "properties": ["photo"]},
|
||||
{"type": "reply", "name": "Reply", "properties": ["in-reply-to", "content"]},
|
||||
{"type": "like", "name": "Like", "properties": ["like-of"]},
|
||||
{"type": "repost", "name": "Repost", "properties": ["repost-of", "content"]},
|
||||
],
|
||||
}
|
||||
return jsonify(config), 200
|
||||
}), 200
|
||||
|
||||
elif q == "source":
|
||||
# Return source of a specific post
|
||||
if q == "source":
|
||||
url = args.get("url")
|
||||
if not url:
|
||||
return error_response("invalid_request", "No URL provided")
|
||||
|
||||
# Extract slug from URL
|
||||
wanted = get_list("properties")
|
||||
if url:
|
||||
try:
|
||||
note = _note_from_url(url)
|
||||
except MicropubError as e:
|
||||
return error_response("invalid_request" if e.status_code == 400 else e.error,
|
||||
e.error_description, e.status_code)
|
||||
return jsonify(note_to_mf2(note, wanted)), 200
|
||||
try:
|
||||
# URL format: https://example.com/notes/{slug}
|
||||
slug = url.rstrip("/").split("/")[-1]
|
||||
note = get_note(slug)
|
||||
limit = max(1, min(int(args.get("limit", 10)), 50))
|
||||
offset = max(0, int(args.get("offset", 0)))
|
||||
except ValueError:
|
||||
return error_response("invalid_request", "limit and offset must be numbers")
|
||||
notes = list_notes(published_only=False, limit=limit, offset=offset)
|
||||
items = [note_to_mf2(n, wanted) for n in notes]
|
||||
return jsonify({"items": items}), 200
|
||||
|
||||
# Check if note exists
|
||||
if note is None:
|
||||
return error_response("invalid_request", "Post not found")
|
||||
if q == "syndicate-to":
|
||||
return jsonify({"syndicate-to": _syndicate_to()}), 200
|
||||
|
||||
except NoteNotFoundError:
|
||||
return error_response("invalid_request", "Post not found")
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Failed to get note source: {e}")
|
||||
return error_response("server_error", "Failed to retrieve post")
|
||||
if q == "category":
|
||||
return jsonify({"categories": _all_categories(args.get("filter") or "")}), 200
|
||||
|
||||
# Convert note to Micropub Microformats2 format
|
||||
# Note: SITE_URL is normalized to include trailing slash (for IndieAuth spec compliance)
|
||||
site_url = current_app.config.get("SITE_URL", "http://localhost:5000")
|
||||
mf2 = {
|
||||
"type": ["h-entry"],
|
||||
"properties": {
|
||||
"content": [note.content],
|
||||
"published": [note.created_at.isoformat()],
|
||||
"url": [f"{site_url}notes/{note.slug}"],
|
||||
},
|
||||
}
|
||||
|
||||
# Add optional properties
|
||||
if note.title:
|
||||
mf2["properties"]["name"] = [note.title]
|
||||
|
||||
# Add tags if present (v1.3.0)
|
||||
if note.tags:
|
||||
mf2["properties"]["category"] = [tag["display_name"] for tag in note.tags]
|
||||
|
||||
return jsonify(mf2), 200
|
||||
|
||||
elif q == "syndicate-to":
|
||||
# Return syndication targets (none for V1)
|
||||
return jsonify({"syndicate-to": []}), 200
|
||||
|
||||
else:
|
||||
return error_response("invalid_request", f"Unknown query: {q}")
|
||||
return error_response("invalid_request", f"Unknown query: {q}")
|
||||
+71
-4
@@ -34,13 +34,32 @@ DEFAULT_AUTH_STATE_EXPIRY_MINUTES = 5
|
||||
DEFAULT_TOKEN_EXPIRY_DAYS = 90
|
||||
|
||||
# Constants - Markdown rendering
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br"]
|
||||
from starpunk.markdown_ext import BareUrlExtension
|
||||
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br", BareUrlExtension()]
|
||||
|
||||
# Constants - Content limits
|
||||
MAX_TITLE_LENGTH = 200
|
||||
EXCERPT_LENGTH = 200
|
||||
|
||||
|
||||
def split_title(content: str) -> tuple[Optional[str], str]:
|
||||
"""
|
||||
Split a note's '# Heading' first line (its title, per Q22) off its content
|
||||
|
||||
Returns:
|
||||
(title, remaining content); title is None if the note has no heading
|
||||
"""
|
||||
stripped = content.lstrip()
|
||||
if not stripped.startswith("#"):
|
||||
return None, content
|
||||
first_line, _, rest = stripped.partition("\n")
|
||||
title = first_line.lstrip("#").strip()
|
||||
if not title:
|
||||
return None, content
|
||||
return title, rest.strip()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Note:
|
||||
"""
|
||||
@@ -115,6 +134,8 @@ class Note:
|
||||
deleted_at: Optional[datetime] = None
|
||||
content_hash: Optional[str] = None
|
||||
in_reply_to: Optional[str] = None # v1.6.0: URL being replied to
|
||||
like_of: Optional[str] = None # v2.5.0: URL liked (this note is a like)
|
||||
repost_of: Optional[str] = None # v2.5.0: URL reposted (this note is a repost)
|
||||
_cached_content: Optional[str] = field(
|
||||
default=None, repr=False, compare=False, init=False
|
||||
)
|
||||
@@ -173,6 +194,8 @@ class Note:
|
||||
_data_dir=data_dir,
|
||||
content_hash=data.get("content_hash"),
|
||||
in_reply_to=data.get("in_reply_to"),
|
||||
like_of=data.get("like_of"),
|
||||
repost_of=data.get("repost_of"),
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -242,6 +265,19 @@ class Note:
|
||||
|
||||
return self._cached_html
|
||||
|
||||
@property
|
||||
def body_html(self) -> str:
|
||||
"""
|
||||
Rendered HTML without the title heading
|
||||
|
||||
Pages show a titled note's title as their own heading, so the body
|
||||
leaves it out to avoid showing it twice. Untitled notes: same as html.
|
||||
"""
|
||||
title, body = split_title(self.content)
|
||||
if title is None:
|
||||
return self.html
|
||||
return markdown.markdown(body, extensions=MARKDOWN_EXTENSIONS)
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""
|
||||
@@ -309,10 +345,10 @@ class Note:
|
||||
'This is my note content. It has some interesting points...'
|
||||
"""
|
||||
try:
|
||||
# Get content
|
||||
content = self.content
|
||||
# Leave out the title; previews show it separately
|
||||
_, content = split_title(self.content)
|
||||
|
||||
# Remove markdown heading syntax from first line
|
||||
# Remove markdown heading syntax
|
||||
lines = content.split("\n")
|
||||
cleaned_lines = []
|
||||
for line in lines:
|
||||
@@ -335,12 +371,43 @@ class Note:
|
||||
excerpt = excerpt[:last_space]
|
||||
excerpt += "..."
|
||||
|
||||
if not excerpt and self.reacts_to:
|
||||
# A like or repost with no comment
|
||||
verb = "Liked" if self.kind == "like" else "Reposted"
|
||||
return f"{verb} {self.reacts_to}"
|
||||
|
||||
return excerpt
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
# If file doesn't exist, return slug
|
||||
return self.slug
|
||||
|
||||
@property
|
||||
def kind(self) -> str:
|
||||
"""'like', 'repost' or 'note' (v2.5.0)"""
|
||||
if self.like_of:
|
||||
return "like"
|
||||
if self.repost_of:
|
||||
return "repost"
|
||||
return "note"
|
||||
|
||||
@property
|
||||
def reacts_to(self) -> Optional[str]:
|
||||
"""The URL a like or repost points at"""
|
||||
return self.like_of or self.repost_of
|
||||
|
||||
@property
|
||||
def feed_html(self) -> str:
|
||||
"""HTML for feeds: a repost/like line, then the note's own HTML"""
|
||||
from html import escape
|
||||
|
||||
if not self.reacts_to:
|
||||
return self.html
|
||||
verb = "Liked" if self.kind == "like" else "Reposted"
|
||||
url = escape(self.reacts_to, quote=True)
|
||||
line = f'<p>{"★" if self.kind == "like" else "🔁"} {verb} <a href="{url}">{url}</a></p>'
|
||||
return line + (self.html or "")
|
||||
|
||||
@property
|
||||
def permalink(self) -> str:
|
||||
"""
|
||||
|
||||
+121
-21
@@ -139,7 +139,10 @@ def create_note(
|
||||
created_at: Optional[datetime] = None,
|
||||
custom_slug: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
in_reply_to: Optional[str] = None
|
||||
in_reply_to: Optional[str] = None,
|
||||
like_of: Optional[str] = None,
|
||||
repost_of: Optional[str] = None,
|
||||
has_media: bool = False,
|
||||
) -> Note:
|
||||
"""
|
||||
Create a new note
|
||||
@@ -203,7 +206,11 @@ def create_note(
|
||||
- created_at and updated_at set to same value initially
|
||||
"""
|
||||
# 1. VALIDATION (before any changes)
|
||||
if not content or not content.strip():
|
||||
# Likes and reposts may have no comment (v2.5.0), photo posts no text (v2.6.5)
|
||||
if like_of and repost_of:
|
||||
raise InvalidNoteDataError("like_of", like_of, "A note can't be both a like and a repost")
|
||||
content = content or ""
|
||||
if not content.strip() and not (like_of or repost_of or has_media):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
@@ -264,10 +271,10 @@ def create_note(
|
||||
try:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO notes (slug, file_path, published, created_at, updated_at, content_hash, in_reply_to)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO notes (slug, file_path, published, created_at, updated_at, content_hash, in_reply_to, like_of, repost_of)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(slug, file_path_rel, published, created_at, updated_at, content_hash, in_reply_to),
|
||||
(slug, file_path_rel, published, created_at, updated_at, content_hash, in_reply_to, like_of, repost_of),
|
||||
)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
@@ -322,33 +329,36 @@ def create_note(
|
||||
from starpunk.webmention_sender import queue_webmentions_for_note
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
note_url = f"{site_url}/note/{slug}"
|
||||
from starpunk.utils import absolute_url_for
|
||||
note_url = absolute_url_for("public.note", slug=slug)
|
||||
|
||||
# Get rendered HTML from note
|
||||
html_content = note.html
|
||||
|
||||
# Queue webmentions (best-effort, failures logged but don't fail note creation)
|
||||
# The reply/like/repost target is notified first (v2.5.0)
|
||||
queue_webmentions_for_note(
|
||||
db_path=db_path,
|
||||
note_id=note_id,
|
||||
note_url=note_url,
|
||||
content_html=html_content,
|
||||
in_reply_to=in_reply_to
|
||||
in_reply_to=in_reply_to or like_of or repost_of
|
||||
)
|
||||
except Exception as e:
|
||||
# Webmention queueing failure should not prevent note creation
|
||||
current_app.logger.warning(f"Failed to queue webmentions for note {slug}: {e}")
|
||||
|
||||
# 12. FETCH REPLY CONTEXT (v1.6.0 Phase 3)
|
||||
# Fetch context if this is a reply
|
||||
if in_reply_to:
|
||||
# Fetch context for the post being replied to, liked or reposted
|
||||
context_url = in_reply_to or like_of or repost_of
|
||||
if context_url:
|
||||
try:
|
||||
from starpunk.reply_context import fetch_reply_context
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
|
||||
# Fetch context (best-effort, failures logged but don't fail note creation)
|
||||
fetch_reply_context(db_path, in_reply_to, site_url)
|
||||
fetch_reply_context(db_path, context_url, site_url)
|
||||
except Exception as e:
|
||||
# Reply context fetch failure should not prevent note creation
|
||||
current_app.logger.warning(f"Failed to fetch reply context for {in_reply_to}: {e}")
|
||||
@@ -472,6 +482,7 @@ def list_notes(
|
||||
offset: int = 0,
|
||||
order_by: str = "created_at",
|
||||
order_dir: str = "DESC",
|
||||
likes: Optional[bool] = None,
|
||||
) -> list[Note]:
|
||||
"""
|
||||
List notes with filtering and pagination
|
||||
@@ -564,6 +575,11 @@ def list_notes(
|
||||
params = []
|
||||
if published_only:
|
||||
query += " AND published = 1"
|
||||
# likes: None = all notes, False = leave likes out, True = only likes (v2.5.0)
|
||||
if likes is True:
|
||||
query += " AND like_of IS NOT NULL"
|
||||
elif likes is False:
|
||||
query += " AND like_of IS NULL"
|
||||
|
||||
# Add ordering (safe because order_by validated above)
|
||||
query += f" ORDER BY {order_by} {order_dir}"
|
||||
@@ -589,7 +605,8 @@ def update_note(
|
||||
content: Optional[str] = None,
|
||||
published: Optional[bool] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
in_reply_to: Optional[str] = None
|
||||
in_reply_to: Optional[str] = None,
|
||||
has_media: bool = False,
|
||||
) -> Note:
|
||||
"""
|
||||
Update a note's content and/or published status
|
||||
@@ -663,12 +680,9 @@ def update_note(
|
||||
if content is None and published is None and tags is None and in_reply_to is None:
|
||||
raise ValueError("Must provide at least one of content, published, tags, or in_reply_to to update")
|
||||
|
||||
# Validate content if provided
|
||||
if content is not None:
|
||||
if not content or not content.strip():
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
# Validate content if provided (checked against the note below for
|
||||
# likes/reposts, whose comment may be empty)
|
||||
empty_content = content is not None and not content.strip()
|
||||
|
||||
# 2. GET EXISTING NOTE
|
||||
# Phase 6: Load content to get old HTML for webmention URL diffing
|
||||
@@ -678,6 +692,11 @@ def update_note(
|
||||
identifier = slug if slug is not None else id
|
||||
raise NoteNotFoundError(identifier)
|
||||
|
||||
if empty_content and not (existing_note.like_of or existing_note.repost_of or has_media):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
|
||||
# 3. SETUP
|
||||
updated_at = datetime.utcnow()
|
||||
data_dir = Path(current_app.config["DATA_PATH"])
|
||||
@@ -815,13 +834,14 @@ def update_note(
|
||||
from starpunk.webmention_sender import queue_webmentions_for_note
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
note_url = f"{site_url}/note/{updated_note.slug}"
|
||||
from starpunk.utils import absolute_url_for
|
||||
note_url = absolute_url_for("public.note", slug=updated_note.slug)
|
||||
|
||||
# Get rendered HTML from updated note (if published)
|
||||
html_content = updated_note.html if updated_note.published else None
|
||||
|
||||
# Determine in_reply_to (use new value if provided, otherwise existing)
|
||||
reply_to = in_reply_to if in_reply_to is not None else updated_note.in_reply_to
|
||||
reply_to = (in_reply_to if in_reply_to is not None else updated_note.in_reply_to) or updated_note.like_of or updated_note.repost_of
|
||||
|
||||
# Queue webmentions (best-effort)
|
||||
# Pass old_html for URL diffing (Phase 6)
|
||||
@@ -838,8 +858,8 @@ def update_note(
|
||||
current_app.logger.warning(f"Failed to queue webmentions for note {updated_note.slug}: {e}")
|
||||
|
||||
# 9. FETCH REPLY CONTEXT (v1.6.0 Phase 3)
|
||||
# Fetch context if in_reply_to changed and is not None
|
||||
if in_reply_to is not None:
|
||||
# Fetch context if in_reply_to changed and is set ("" clears it)
|
||||
if in_reply_to:
|
||||
try:
|
||||
from starpunk.reply_context import fetch_reply_context
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
@@ -851,6 +871,13 @@ def update_note(
|
||||
# Reply context fetch failure should not prevent note update
|
||||
current_app.logger.warning(f"Failed to fetch reply context for {in_reply_to}: {e}")
|
||||
|
||||
# Crossposts follow the note (v2.1.0, best-effort)
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(updated_note.id)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication update: {e}")
|
||||
|
||||
# 10. RETURN UPDATED NOTE
|
||||
return updated_note
|
||||
|
||||
@@ -962,6 +989,17 @@ def delete_note(
|
||||
"Path validation failed",
|
||||
)
|
||||
|
||||
# Re-send webmentions so receivers learn the post is gone (spec 3.1.5).
|
||||
# A hard delete would cascade the queue rows away, so detach them first.
|
||||
if existing_note.published:
|
||||
try:
|
||||
from starpunk.webmention_sender import requeue_previous_webmentions
|
||||
requeue_previous_webmentions(
|
||||
Path(current_app.config["DATABASE_PATH"]), existing_note.id, detach=not soft
|
||||
)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue webmentions for deleted note: {e}")
|
||||
|
||||
# 5. PERFORM DELETION
|
||||
db = get_db(current_app)
|
||||
|
||||
@@ -1018,5 +1056,67 @@ def delete_note(
|
||||
)
|
||||
# Don't fail - database record already deleted
|
||||
|
||||
# Delete crossposts too (v2.1.0, best-effort)
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(existing_note.id)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication delete: {e}")
|
||||
|
||||
# 6. RETURN (no value)
|
||||
return None
|
||||
|
||||
|
||||
def undelete_note(slug: str) -> Note:
|
||||
"""
|
||||
Restore a soft-deleted note (v2.4.0, Micropub undelete)
|
||||
|
||||
Moves the file back from .trash/ and clears deleted_at. Crossposts are
|
||||
queued again, so a note deleted from them is posted again.
|
||||
|
||||
Raises:
|
||||
NoteNotFoundError: If there is no soft-deleted note with this slug
|
||||
"""
|
||||
import shutil
|
||||
from starpunk.utils import TRASH_DIR_NAME
|
||||
|
||||
db = get_db(current_app)
|
||||
row = db.execute(
|
||||
"SELECT id, file_path, created_at FROM notes WHERE slug = ? AND deleted_at IS NOT NULL",
|
||||
(slug,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise NoteNotFoundError(slug)
|
||||
|
||||
data_dir = Path(current_app.config["DATA_PATH"])
|
||||
note_path = data_dir / row["file_path"]
|
||||
if not note_path.exists():
|
||||
# delete_note_file put it in .trash/<year>/<month>/<name>
|
||||
parts = Path(row["file_path"]).parts
|
||||
candidates = []
|
||||
if "notes" in parts and len(parts) > parts.index("notes") + 2:
|
||||
i = parts.index("notes")
|
||||
candidates.append(data_dir / TRASH_DIR_NAME / parts[i + 1] / parts[i + 2] / note_path.name)
|
||||
candidates += sorted((data_dir / TRASH_DIR_NAME).glob(f"*/*/{note_path.name}"))
|
||||
source = next((c for c in candidates if c.exists()), None)
|
||||
if source is None:
|
||||
raise NoteSyncError("undelete", f"Trashed file not found for {slug}", "Cannot restore note file")
|
||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(source), str(note_path))
|
||||
|
||||
db.execute("UPDATE notes SET deleted_at = NULL WHERE id = ?", (row["id"],))
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(row["id"])
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication after undelete: {e}")
|
||||
|
||||
try:
|
||||
from starpunk.webmention_sender import requeue_previous_webmentions
|
||||
requeue_previous_webmentions(Path(current_app.config["DATABASE_PATH"]), row["id"])
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue webmentions after undelete: {e}")
|
||||
|
||||
return get_note(slug=slug)
|
||||
@@ -97,7 +97,9 @@ def new_note_form():
|
||||
Decorator: @require_auth
|
||||
Template: templates/admin/new.html
|
||||
"""
|
||||
return render_template("admin/new.html")
|
||||
from starpunk.syndication import get_targets
|
||||
|
||||
return render_template("admin/new.html", syndication_targets=get_targets().values())
|
||||
|
||||
|
||||
@bp.route("/new", methods=["POST"])
|
||||
@@ -182,6 +184,11 @@ def create_note_submit():
|
||||
if errors:
|
||||
flash(f"Note created, but some images failed: {'; '.join(errors)}", "warning")
|
||||
|
||||
# Crosspost after media is attached so images go out too (v2.1.0)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
select_targets(note.id, request.form.getlist("syndicate_to"))
|
||||
|
||||
flash(f"Note created: {note.slug}", "success")
|
||||
return redirect(url_for("admin.dashboard"))
|
||||
|
||||
@@ -219,11 +226,21 @@ 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)
|
||||
|
||||
return render_template("admin/edit.html", note=note)
|
||||
from starpunk.syndication import get_note_syndications, get_targets
|
||||
|
||||
syndications = get_note_syndications(note.id)
|
||||
enabled = {s["target"] for s in syndications if s["enabled"]}
|
||||
return render_template(
|
||||
"admin/edit.html",
|
||||
note=note,
|
||||
syndication_targets=get_targets().values(),
|
||||
syndications=syndications,
|
||||
syndication_enabled=enabled,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/edit/<int:note_id>", methods=["POST"])
|
||||
@@ -260,7 +277,7 @@ def update_note_submit(note_id: int):
|
||||
published = "published" in request.form
|
||||
tags_input = request.form.get("tags", "")
|
||||
|
||||
if not content:
|
||||
if not content and not existing_note.reacts_to: # likes/reposts may have no comment
|
||||
flash("Content cannot be empty", "error")
|
||||
return redirect(url_for("admin.edit_note_form", note_id=note_id))
|
||||
|
||||
@@ -274,6 +291,9 @@ def update_note_submit(note_id: int):
|
||||
published=published,
|
||||
tags=tags if tags else None
|
||||
)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
select_targets(note.id, request.form.getlist("syndicate_to"))
|
||||
flash(f"Note updated: {note.slug}", "success")
|
||||
return redirect(url_for("admin.dashboard"))
|
||||
except ValueError as e:
|
||||
@@ -838,3 +858,14 @@ def revoke_token(token_id):
|
||||
revoke_token_by_id(token_id)
|
||||
flash("App access revoked", "success")
|
||||
return redirect(url_for("admin.tokens"))
|
||||
|
||||
|
||||
@bp.route("/syndication/<int:syndication_id>/retry", methods=["POST"])
|
||||
@require_auth
|
||||
def retry_syndication(syndication_id):
|
||||
"""Retry a crosspost now"""
|
||||
from starpunk.syndication import retry
|
||||
|
||||
retry(syndication_id)
|
||||
flash("Crosspost queued", "success")
|
||||
return redirect(request.referrer or url_for("admin.dashboard"))
|
||||
@@ -34,11 +34,11 @@ from starpunk.indieauth import (
|
||||
issuer,
|
||||
redeem_authorization_code,
|
||||
revoke_access_token,
|
||||
site_url_for,
|
||||
validate_authorization_request,
|
||||
verify_access_token,
|
||||
)
|
||||
from starpunk.micropub import extract_bearer_token
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
bp = Blueprint("indieauth", __name__)
|
||||
|
||||
@@ -89,9 +89,9 @@ def metadata():
|
||||
return _json(
|
||||
{
|
||||
"issuer": issuer(),
|
||||
"authorization_endpoint": site_url_for("indieauth.authorize"),
|
||||
"token_endpoint": site_url_for("indieauth.token"),
|
||||
"revocation_endpoint": site_url_for("indieauth.revoke"),
|
||||
"authorization_endpoint": absolute_url_for("indieauth.authorize"),
|
||||
"token_endpoint": absolute_url_for("indieauth.token"),
|
||||
"revocation_endpoint": absolute_url_for("indieauth.revoke"),
|
||||
"revocation_endpoint_auth_methods_supported": ["none"],
|
||||
"scopes_supported": SUPPORTED_SCOPES,
|
||||
"response_types_supported": ["code"],
|
||||
|
||||
+18
-18
@@ -27,6 +27,8 @@ from starpunk.micropub import (
|
||||
extract_bearer_token,
|
||||
error_response,
|
||||
handle_create,
|
||||
handle_delete,
|
||||
handle_update,
|
||||
handle_query,
|
||||
)
|
||||
from starpunk.indieauth import verify_access_token, check_scope
|
||||
@@ -49,8 +51,8 @@ def micropub_endpoint():
|
||||
POST requests:
|
||||
Handle action operations (form-encoded or JSON):
|
||||
- action=create (or no action): Create new post
|
||||
- action=update: Update existing post (not supported in V1)
|
||||
- action=delete: Delete post (not supported in V1)
|
||||
- action=update: Update existing post (JSON; replace/add/delete)
|
||||
- action=delete / action=undelete: Delete or restore a post
|
||||
|
||||
Authentication:
|
||||
Requires valid bearer token in Authorization header or
|
||||
@@ -79,7 +81,7 @@ def micropub_endpoint():
|
||||
# Handle query endpoints (GET requests)
|
||||
if request.method == "GET":
|
||||
try:
|
||||
return handle_query(request.args.to_dict(), token_info)
|
||||
return handle_query(request.args, token_info)
|
||||
except MicropubError as e:
|
||||
return error_response(e.error, e.error_description, e.status_code)
|
||||
except Exception as e:
|
||||
@@ -95,23 +97,19 @@ def micropub_endpoint():
|
||||
data = request.get_json() or {}
|
||||
action = data.get("action", "create")
|
||||
else:
|
||||
# Form-encoded or multipart (V1 only supports form-encoded)
|
||||
# Form-encoded or multipart (files handled separately)
|
||||
data = request.form.to_dict(flat=False)
|
||||
action = data.get("action", ["create"])[0]
|
||||
|
||||
# Route to appropriate handler
|
||||
if action == "create":
|
||||
return handle_create(data, token_info)
|
||||
return handle_create(data, token_info, files=request.files)
|
||||
elif action == "update":
|
||||
# V1: Update not supported
|
||||
return error_response(
|
||||
"invalid_request", "Update action not supported in V1", 400
|
||||
)
|
||||
elif action == "delete":
|
||||
# V1: Delete not supported
|
||||
return error_response(
|
||||
"invalid_request", "Delete action not supported in V1", 400
|
||||
)
|
||||
if "application/json" not in content_type:
|
||||
return error_response("invalid_request", "Updates must be sent as JSON", 400)
|
||||
return handle_update(data, token_info)
|
||||
elif action in ("delete", "undelete"):
|
||||
return handle_delete(data, token_info, undelete=(action == "undelete"))
|
||||
else:
|
||||
return error_response("invalid_request", f"Unknown action: {action}", 400)
|
||||
|
||||
@@ -122,7 +120,8 @@ def micropub_endpoint():
|
||||
return error_response("server_error", "An unexpected error occurred", 500)
|
||||
|
||||
|
||||
@bp.route('/media', methods=['POST'])
|
||||
@bp.route('/micropub/media', methods=['POST'])
|
||||
@bp.route('/media', methods=['POST']) # old path, kept for existing clients
|
||||
def media_endpoint():
|
||||
"""
|
||||
Micropub media endpoint for file uploads
|
||||
@@ -145,11 +144,12 @@ def media_endpoint():
|
||||
if not token_info:
|
||||
return error_response("unauthorized", "Invalid or expired access token", 401)
|
||||
|
||||
# Check scope (create scope allows media upload)
|
||||
if not check_scope("create", token_info.get("scope", "")):
|
||||
# Check scope: "media", or "create" for older clients
|
||||
scope = token_info.get("scope", "")
|
||||
if not (check_scope("media", scope) or check_scope("create", scope)):
|
||||
return error_response(
|
||||
"insufficient_scope",
|
||||
"Token lacks create scope",
|
||||
"Token lacks media or create scope",
|
||||
403
|
||||
)
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ def _get_cached_notes():
|
||||
|
||||
# Cache expired or empty, fetch fresh notes
|
||||
max_items = current_app.config.get("FEED_MAX_ITEMS", 50)
|
||||
notes = list_notes(published_only=True, limit=max_items)
|
||||
notes = list_notes(published_only=True, limit=max_items, likes=False)
|
||||
|
||||
# Batch load media and tags for all notes (v1.5.0 Phase 3)
|
||||
# Reduces query count from O(n) to O(1) for both media and tags
|
||||
@@ -249,8 +249,8 @@ def index():
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
# Get recent published notes (limit 20)
|
||||
notes = list_notes(published_only=True, limit=20)
|
||||
# Get recent published notes (limit 20); likes have their own page (v2.5.0)
|
||||
notes = list_notes(published_only=True, limit=20, likes=False)
|
||||
|
||||
# Attach media and tags to each note for display
|
||||
for note in notes:
|
||||
@@ -262,9 +262,59 @@ def index():
|
||||
tags = get_note_tags(note.id)
|
||||
object.__setattr__(note, '_cached_tags', tags)
|
||||
|
||||
attach_contexts(notes)
|
||||
return render_template("index.html", notes=notes)
|
||||
|
||||
|
||||
def attach_contexts(notes) -> None:
|
||||
"""Give likes and reposts the fetched context of the post they point at"""
|
||||
from starpunk.webmention_queries import get_reply_context
|
||||
|
||||
for note in notes:
|
||||
context = None
|
||||
if note.reacts_to:
|
||||
try:
|
||||
context = get_reply_context(note.reacts_to)
|
||||
except Exception:
|
||||
context = None
|
||||
object.__setattr__(note, 'context', context)
|
||||
|
||||
|
||||
@bp.route("/likes")
|
||||
def likes():
|
||||
"""Likes, kept off the home page and feeds (v2.5.0)"""
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
notes = list_notes(published_only=True, limit=50, likes=True)
|
||||
for note in notes:
|
||||
object.__setattr__(note, 'media', get_note_media(note.id))
|
||||
object.__setattr__(note, '_cached_tags', get_note_tags(note.id))
|
||||
attach_contexts(notes)
|
||||
return render_template("likes.html", notes=notes)
|
||||
|
||||
|
||||
def _note_was_deleted(slug: str) -> bool:
|
||||
"""
|
||||
True if this slug was a note that has been deleted
|
||||
|
||||
Soft-deleted notes keep their row; hard-deleted notes that sent
|
||||
webmentions keep their queue rows (detached from the note).
|
||||
"""
|
||||
from starpunk.database import get_db
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
db = get_db(current_app)
|
||||
if db.execute(
|
||||
"SELECT 1 FROM notes WHERE slug = ? AND deleted_at IS NOT NULL", (slug,)
|
||||
).fetchone():
|
||||
return True
|
||||
return db.execute(
|
||||
"SELECT 1 FROM webmention_queue WHERE source_url = ? AND source_note_id IS NULL",
|
||||
(absolute_url_for("public.note", slug=slug),),
|
||||
).fetchone() is not None
|
||||
|
||||
|
||||
@bp.route("/note/<slug>")
|
||||
def note(slug: str):
|
||||
"""
|
||||
@@ -290,8 +340,10 @@ def note(slug: str):
|
||||
# Get note by slug
|
||||
note_obj = get_note(slug=slug)
|
||||
|
||||
# Return 404 if note doesn't exist or isn't published
|
||||
# Deleted notes are 410 Gone (Webmention 3.1.5); unknown or draft 404
|
||||
if not note_obj or not note_obj.published:
|
||||
if not note_obj and _note_was_deleted(slug):
|
||||
abort(410)
|
||||
abort(404)
|
||||
|
||||
# Get media for note (v1.2.0 Phase 3)
|
||||
@@ -331,9 +383,15 @@ def note(slug: str):
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.error(f"Error loading webmentions for note {slug}: {e}")
|
||||
|
||||
from starpunk.syndication import get_note_syndications
|
||||
|
||||
syndications = [s for s in get_note_syndications(note_obj.id) if s["state"] == "synced"]
|
||||
attach_contexts([note_obj])
|
||||
|
||||
return render_template(
|
||||
"note.html",
|
||||
note=note_obj,
|
||||
syndications=syndications,
|
||||
webmentions=webmentions,
|
||||
reply_context=reply_context,
|
||||
**grouped if webmentions else {}
|
||||
@@ -379,6 +437,7 @@ def tag(tag: str):
|
||||
for note in notes:
|
||||
media = get_note_media(note.id)
|
||||
object.__setattr__(note, 'media', media)
|
||||
attach_contexts(notes)
|
||||
|
||||
return render_template(
|
||||
"tag.html",
|
||||
|
||||
@@ -108,7 +108,7 @@ def api_search():
|
||||
"title": r["title"] or f"Note from {r['created_at'][:10]}",
|
||||
"excerpt": r["snippet"], # Already has <mark> tags
|
||||
"published_at": r["created_at"],
|
||||
"url": f"/notes/{r['slug']}",
|
||||
"url": f"/note/{r['slug']}",
|
||||
}
|
||||
for r in results
|
||||
],
|
||||
@@ -176,7 +176,7 @@ def search_page():
|
||||
"title": r["title"] or f"Note from {r['created_at'][:10]}",
|
||||
"excerpt": Markup(safe_snippet), # Mark as safe since we've escaped it ourselves
|
||||
"published_at": r["created_at"],
|
||||
"url": f"/notes/{r['slug']}",
|
||||
"url": f"/note/{r['slug']}",
|
||||
})
|
||||
results = formatted_results
|
||||
except Exception as e:
|
||||
|
||||
@@ -11,7 +11,7 @@ References:
|
||||
"""
|
||||
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import urldefrag, urlparse
|
||||
from starpunk.database import get_db
|
||||
from starpunk.notes import get_note, NoteNotFoundError
|
||||
from starpunk.rate_limiter import check_rate_limit
|
||||
@@ -107,17 +107,24 @@ def validate_url(url: str) -> tuple[bool, str | None]:
|
||||
return False, "URL is malformed"
|
||||
|
||||
|
||||
def _host_port(url: str) -> tuple[str, int | None]:
|
||||
parsed = urlparse(url)
|
||||
default = {'http': 80, 'https': 443}.get(parsed.scheme.lower())
|
||||
try:
|
||||
port = parsed.port or default
|
||||
except ValueError:
|
||||
port = None
|
||||
return (parsed.hostname or '').lower(), port
|
||||
|
||||
|
||||
def is_target_on_site(target_url: str) -> bool:
|
||||
"""
|
||||
Check if target URL is on this site.
|
||||
|
||||
Compares hostname of target_url with SITE_URL configuration.
|
||||
Compares host (case-insensitively) and effective port with SITE_URL.
|
||||
"""
|
||||
site_url = current_app.config.get('SITE_URL', '')
|
||||
site_parsed = urlparse(site_url)
|
||||
target_parsed = urlparse(target_url)
|
||||
|
||||
return site_parsed.netloc == target_parsed.netloc
|
||||
return _host_port(site_url) == _host_port(target_url)
|
||||
|
||||
|
||||
def resolve_target_note(target_url: str) -> int | None:
|
||||
@@ -228,16 +235,17 @@ def receive_webmention():
|
||||
"error_description": "Target URL is not on this site"
|
||||
}), 400
|
||||
|
||||
# Resolve target to note (may be None for homepage/other pages)
|
||||
target_note_id = resolve_target_note(target)
|
||||
# The fragment is ignored (spec 3.2.1) and not stored, so target#a and
|
||||
# target#b are the same mention
|
||||
target = urldefrag(target)[0]
|
||||
|
||||
# If target looks like a note URL but doesn't resolve, it's not found
|
||||
target_parsed = urlparse(target)
|
||||
target_path = target_parsed.path.rstrip('/')
|
||||
if target_path.startswith('/note/') and target_note_id is None:
|
||||
# Only published notes accept webmentions (spec 3.2.1: target must be a
|
||||
# valid resource that accepts them)
|
||||
target_note_id = resolve_target_note(target)
|
||||
if target_note_id is None:
|
||||
return jsonify({
|
||||
"error": "invalid_target",
|
||||
"error_description": "Target URL not found"
|
||||
"error_description": "Target URL does not accept webmentions"
|
||||
}), 400
|
||||
|
||||
# Insert webmention into database (queued for validation)
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
+11
-6
@@ -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()
|
||||
|
||||
@@ -681,3 +681,15 @@ def parse_iso8601(date_string: str) -> datetime:
|
||||
|
||||
# Parse using fromisoformat
|
||||
return datetime.fromisoformat(date_string)
|
||||
|
||||
|
||||
def absolute_url_for(endpoint: str, **values) -> str:
|
||||
"""
|
||||
Absolute URL for an endpoint, built from SITE_URL
|
||||
|
||||
Use instead of url_for(..., _external=True): behind a reverse proxy the
|
||||
request arrives over plain HTTP, so _external URLs would say http://.
|
||||
"""
|
||||
from flask import current_app, url_for
|
||||
|
||||
return current_app.config["SITE_URL"].rstrip("/") + url_for(endpoint, **values)
|
||||
@@ -14,15 +14,18 @@ References:
|
||||
- W3C Webmention: https://www.w3.org/TR/webmention/
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import mf2py
|
||||
import nh3
|
||||
import httpx
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, Optional
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import urldefrag, urlparse
|
||||
|
||||
from starpunk.webmention_ssrf import validate_url_for_fetch, SSRFBlockedError
|
||||
from starpunk.webmention_ssrf import (
|
||||
FetchTooLargeError, SSRFBlockedError, safe_get, user_agent,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -33,103 +36,113 @@ class ValidationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def fetch_source(url: str, site_url: str, timeout: int = 10, max_size: int = 1048576) -> tuple[str, str]:
|
||||
ACCEPT = ('text/html, application/xhtml+xml;q=0.9, application/json;q=0.8, '
|
||||
'text/plain;q=0.7, */*;q=0.1')
|
||||
|
||||
|
||||
class TransientError(ValidationError):
|
||||
"""Source could not be checked right now (network error, 5xx)"""
|
||||
pass
|
||||
|
||||
|
||||
def fetch_source(url: str, site_url: str) -> tuple[str, str, str]:
|
||||
"""
|
||||
Fetch source URL content with SSRF protection.
|
||||
|
||||
Per spec 3.2.2: GET following redirects (limited), with an Accept header.
|
||||
|
||||
Args:
|
||||
url: Source URL to fetch
|
||||
site_url: Site URL for User-Agent header
|
||||
timeout: Request timeout in seconds (default: 10)
|
||||
max_size: Maximum content size in bytes (default: 1MB)
|
||||
|
||||
Returns:
|
||||
Tuple of (content, content_type)
|
||||
Tuple of (content, content_type, final_url)
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError: If URL points to private IP
|
||||
ValidationError: If fetch fails or content too large
|
||||
ValidationError: Source is blocked, too large, or returned 4xx
|
||||
TransientError: Network error, timeout or 5xx
|
||||
"""
|
||||
# SSRF protection
|
||||
try:
|
||||
validate_url_for_fetch(url)
|
||||
result = safe_get(url, headers={'User-Agent': user_agent(site_url), 'Accept': ACCEPT})
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
logger.warning(f"SSRF protection blocked URL {url}: {e}")
|
||||
raise ValidationError(f"Blocked request: {e}")
|
||||
|
||||
# Fetch URL
|
||||
try:
|
||||
# Use streaming to check size before downloading entire content
|
||||
with httpx.stream(
|
||||
'GET',
|
||||
url,
|
||||
timeout=timeout,
|
||||
headers={'User-Agent': f'StarPunk/1.6.0 (+{site_url})'},
|
||||
follow_redirects=True
|
||||
) as response:
|
||||
response.raise_for_status()
|
||||
|
||||
# Check content length if provided
|
||||
content_length = response.headers.get('Content-Length')
|
||||
if content_length and int(content_length) > max_size:
|
||||
raise ValidationError(f"Source exceeds size limit ({max_size} bytes)")
|
||||
|
||||
# Read content with size limit
|
||||
content = b''
|
||||
for chunk in response.iter_bytes(chunk_size=8192):
|
||||
content += chunk
|
||||
if len(content) > max_size:
|
||||
raise ValidationError(f"Source exceeds size limit ({max_size} bytes)")
|
||||
|
||||
content_type = response.headers.get('Content-Type', '')
|
||||
|
||||
return content.decode('utf-8', errors='replace'), content_type
|
||||
|
||||
except FetchTooLargeError as e:
|
||||
raise ValidationError(f"Source {e}")
|
||||
except httpx.TimeoutException:
|
||||
raise ValidationError("Source fetch timed out")
|
||||
except httpx.HTTPStatusError as e:
|
||||
if 400 <= e.response.status_code < 500:
|
||||
raise ValidationError(f"Source returned {e.response.status_code}")
|
||||
else:
|
||||
raise ValidationError(f"Source server error: {e.response.status_code}")
|
||||
raise TransientError("Source fetch timed out")
|
||||
except httpx.HTTPError as e:
|
||||
raise ValidationError(f"Failed to fetch source: {e}")
|
||||
except UnicodeDecodeError:
|
||||
raise ValidationError("Source content encoding is not supported")
|
||||
raise TransientError(f"Failed to fetch source: {e}")
|
||||
|
||||
if 400 <= result.status_code < 500:
|
||||
raise ValidationError(f"Source returned {result.status_code}")
|
||||
if result.status_code >= 300:
|
||||
raise TransientError(f"Source server error: {result.status_code}")
|
||||
|
||||
return result.text, result.headers.get('Content-Type', ''), result.url
|
||||
|
||||
|
||||
def verify_link_presence(content: str, target_url: str, content_type: str) -> bool:
|
||||
def _strip_fragment(url: str) -> str:
|
||||
return urldefrag(url.strip())[0]
|
||||
|
||||
|
||||
def _json_strings(value):
|
||||
"""Yield every string in a parsed JSON document"""
|
||||
if isinstance(value, str):
|
||||
yield value
|
||||
elif isinstance(value, dict):
|
||||
for item in value.values():
|
||||
yield from _json_strings(item)
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
yield from _json_strings(item)
|
||||
|
||||
|
||||
def verify_link_presence(content: str, target_url: str, content_type: str,
|
||||
source_url: str = '') -> bool:
|
||||
"""
|
||||
Verify that source content contains a link to target URL.
|
||||
Verify the source links to the target (spec 3.2.2).
|
||||
|
||||
The source MUST have an exact match of the target URL, checked per media
|
||||
type: in HTML, any link-bearing attribute (a/link href, img/video/audio
|
||||
src, ...) resolved against the source URL; in JSON, any string value; in
|
||||
other text, the URL anywhere. Fragments are ignored on both sides.
|
||||
|
||||
Args:
|
||||
content: Source content (HTML or text)
|
||||
content: Source content
|
||||
target_url: Target URL to find
|
||||
content_type: Content type of source
|
||||
source_url: Final source URL, for resolving relative links
|
||||
|
||||
Returns:
|
||||
True if target link found, False otherwise
|
||||
True if the target is linked, False otherwise
|
||||
"""
|
||||
# Normalize URLs for comparison (remove fragments, trailing slashes)
|
||||
target_normalized = urlparse(target_url)._replace(fragment='').geturl().rstrip('/')
|
||||
target = _strip_fragment(target_url)
|
||||
media_type = content_type.split(';')[0].strip().lower()
|
||||
|
||||
# For HTML, parse and check all links
|
||||
if 'html' in content_type.lower():
|
||||
if 'html' in media_type:
|
||||
from lxml import html as lxml_html
|
||||
try:
|
||||
from lxml import html as lxml_html
|
||||
doc = lxml_html.fromstring(content)
|
||||
links = doc.xpath('//a/@href')
|
||||
|
||||
for link in links:
|
||||
# Make absolute if relative
|
||||
link_normalized = urlparse(link)._replace(fragment='').geturl().rstrip('/')
|
||||
if link_normalized == target_normalized:
|
||||
return True
|
||||
doc = lxml_html.document_fromstring(content)
|
||||
except Exception as e:
|
||||
logger.debug(f"HTML parsing failed, falling back to string search: {e}")
|
||||
logger.debug(f"HTML parsing failed: {e}")
|
||||
return False
|
||||
if source_url:
|
||||
doc.make_links_absolute(source_url, resolve_base_href=True,
|
||||
handle_failures='ignore')
|
||||
return any(_strip_fragment(link) == target for _, _, link, _ in doc.iterlinks())
|
||||
|
||||
# Fallback: simple string search
|
||||
return target_normalized in content
|
||||
if media_type == 'application/json' or media_type.endswith('+json'):
|
||||
try:
|
||||
data = json.loads(content)
|
||||
except ValueError:
|
||||
return False
|
||||
return any(_strip_fragment(s) == target for s in _json_strings(data))
|
||||
|
||||
if media_type.startswith('text/'):
|
||||
return target in content
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def detect_mention_type(parsed_mf2: Dict, target_url: str) -> str:
|
||||
@@ -182,6 +195,28 @@ def detect_mention_type(parsed_mf2: Dict, target_url: str) -> str:
|
||||
return 'mention'
|
||||
|
||||
|
||||
def _first_str(props: Dict, name: str) -> Optional[str]:
|
||||
"""
|
||||
First value of an mf2 property as a string
|
||||
|
||||
mf2py gives u-photo with alt text as {'value': url, 'alt': ...} and nested
|
||||
microformats as dicts with a 'value'; reduce those to the plain value.
|
||||
"""
|
||||
values = props.get(name) or [None]
|
||||
value = values[0]
|
||||
if isinstance(value, dict):
|
||||
value = value.get('value')
|
||||
return value if isinstance(value, str) and value else None
|
||||
|
||||
|
||||
def _is_http_url(value: Optional[str]) -> bool:
|
||||
"""True if value is an absolute http or https URL"""
|
||||
if not value:
|
||||
return False
|
||||
parsed = urlparse(value.strip())
|
||||
return parsed.scheme.lower() in ('http', 'https') and bool(parsed.netloc)
|
||||
|
||||
|
||||
def extract_author(parsed_mf2: Dict, source_url: str) -> Dict[str, Optional[str]]:
|
||||
"""
|
||||
Extract author information from h-card.
|
||||
@@ -212,26 +247,29 @@ def extract_author(parsed_mf2: Dict, source_url: str) -> Dict[str, Optional[str]
|
||||
if isinstance(author_data, dict):
|
||||
# h-card object
|
||||
props = author_data.get('properties', {})
|
||||
author_name = props.get('name', [None])[0]
|
||||
author_url = props.get('url', [None])[0]
|
||||
author_photo = props.get('photo', [None])[0]
|
||||
author_name = _first_str(props, 'name')
|
||||
author_url = _first_str(props, 'url')
|
||||
author_photo = _first_str(props, 'photo')
|
||||
|
||||
# If no author in h-entry, find representative h-card
|
||||
if not author_name:
|
||||
for item in parsed_mf2.get('items', []):
|
||||
if 'h-card' in item.get('type', []):
|
||||
props = item.get('properties', {})
|
||||
author_name = props.get('name', [None])[0]
|
||||
author_url = props.get('url', [None])[0]
|
||||
author_photo = props.get('photo', [None])[0]
|
||||
author_name = _first_str(props, 'name')
|
||||
author_url = _first_str(props, 'url')
|
||||
author_photo = _first_str(props, 'photo')
|
||||
break
|
||||
|
||||
# Fallback to hostname if no author found
|
||||
if not author_name:
|
||||
author_name = urlparse(source_url).netloc
|
||||
|
||||
if not author_url:
|
||||
# Only http(s) URLs are rendered as links (blocks javascript: and data:)
|
||||
if not _is_http_url(author_url):
|
||||
author_url = source_url
|
||||
if not _is_http_url(author_photo):
|
||||
author_photo = None
|
||||
|
||||
return {
|
||||
'author_name': author_name,
|
||||
@@ -267,12 +305,7 @@ def extract_content(parsed_mf2: Dict) -> Optional[str]:
|
||||
content = content_data
|
||||
|
||||
if content:
|
||||
# Sanitize HTML
|
||||
sanitized = sanitize_content(content)
|
||||
# Truncate to 500 chars
|
||||
if len(sanitized) > 500:
|
||||
sanitized = sanitized[:497] + '...'
|
||||
return sanitized
|
||||
return _snippet(content)
|
||||
|
||||
# Try p-summary as fallback
|
||||
summary = props.get('summary', [])
|
||||
@@ -281,14 +314,20 @@ def extract_content(parsed_mf2: Dict) -> Optional[str]:
|
||||
if isinstance(content, dict):
|
||||
content = content.get('value', '')
|
||||
if content:
|
||||
sanitized = sanitize_content(content)
|
||||
if len(sanitized) > 500:
|
||||
sanitized = sanitized[:497] + '...'
|
||||
return sanitized
|
||||
return _snippet(content)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _snippet(html: str, limit: int = 500) -> str:
|
||||
"""Sanitized HTML of at most ~limit characters, with tags closed"""
|
||||
sanitized = sanitize_content(html)
|
||||
if len(sanitized) > limit:
|
||||
# Cutting can leave a tag or attribute open; sanitizing again closes it
|
||||
sanitized = sanitize_content(sanitized[:limit - 3] + '...')
|
||||
return sanitized
|
||||
|
||||
|
||||
def sanitize_content(html: str) -> str:
|
||||
"""
|
||||
Sanitize HTML content using nh3.
|
||||
@@ -337,17 +376,17 @@ def process_webmention(source_url: str, target_url: str, site_url: str) -> Dict[
|
||||
ValidationError: If validation fails with specific error
|
||||
"""
|
||||
# Fetch source content
|
||||
content, content_type = fetch_source(source_url, site_url)
|
||||
content, content_type, final_url = fetch_source(source_url, site_url)
|
||||
|
||||
# Verify target link presence
|
||||
if not verify_link_presence(content, target_url, content_type):
|
||||
if not verify_link_presence(content, target_url, content_type, final_url):
|
||||
raise ValidationError("Source does not link to target")
|
||||
|
||||
# Parse microformats (only for HTML)
|
||||
parsed_mf2 = None
|
||||
if 'html' in content_type.lower():
|
||||
try:
|
||||
parsed_mf2 = mf2py.parse(doc=content, url=source_url)
|
||||
parsed_mf2 = mf2py.parse(doc=content, url=final_url)
|
||||
except Exception as e:
|
||||
logger.debug(f"Microformat parsing failed for {source_url}: {e}")
|
||||
|
||||
|
||||
@@ -14,9 +14,8 @@ References:
|
||||
|
||||
import logging
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, Set
|
||||
from typing import Optional
|
||||
from lxml import html as lxml_html
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@@ -45,16 +44,16 @@ def extract_links_from_html(html_content: str, exclude_code_blocks: bool = True)
|
||||
return []
|
||||
|
||||
try:
|
||||
# Parse HTML
|
||||
doc = lxml_html.fromstring(html_content)
|
||||
# Parse HTML inside a wrapper, so a top-level <pre> can be removed too
|
||||
doc = lxml_html.fragment_fromstring(html_content, create_parent='div')
|
||||
|
||||
if exclude_code_blocks:
|
||||
# Remove <pre> and <code> elements before extracting links
|
||||
for element in doc.xpath('//pre | //code'):
|
||||
element.getparent().remove(element)
|
||||
for element in doc.xpath('.//pre | .//code'):
|
||||
element.drop_tree()
|
||||
|
||||
# Extract all <a href> attributes in document order
|
||||
links = doc.xpath('//a/@href')
|
||||
links = doc.xpath('.//a/@href')
|
||||
|
||||
# Filter to valid absolute URLs (http/https)
|
||||
absolute_links = []
|
||||
@@ -70,6 +69,66 @@ def extract_links_from_html(html_content: str, exclude_code_blocks: bool = True)
|
||||
return []
|
||||
|
||||
|
||||
def _dedupe(urls: list) -> list:
|
||||
"""Drop empty and repeated URLs, keeping the first occurrence"""
|
||||
seen = set()
|
||||
result = []
|
||||
for url in urls:
|
||||
if url and url not in seen:
|
||||
seen.add(url)
|
||||
result.append(url)
|
||||
return result
|
||||
|
||||
|
||||
def previous_targets(db_path: Path, note_id: int) -> list[str]:
|
||||
"""Targets this note has been queued to send webmentions to before"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT target_url FROM webmention_queue WHERE source_note_id = ? ORDER BY id",
|
||||
(note_id,),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [row[0] for row in rows]
|
||||
|
||||
|
||||
def requeue_previous_webmentions(db_path: Path, note_id: int, detach: bool = False) -> int:
|
||||
"""
|
||||
Re-send every webmention this note has sent (spec 3.1.5 deleted posts)
|
||||
|
||||
Args:
|
||||
db_path: Path to SQLite database
|
||||
note_id: Note being deleted or restored
|
||||
detach: Clear source_note_id so the rows survive a hard delete
|
||||
(the foreign key cascades)
|
||||
|
||||
Returns:
|
||||
Number of webmentions queued
|
||||
"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
try:
|
||||
cursor = conn.execute(f"""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'pending',
|
||||
retry_count = 0,
|
||||
next_retry_at = NULL,
|
||||
last_error = NULL,
|
||||
endpoint_url = NULL,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
{', source_note_id = NULL' if detach else ''}
|
||||
WHERE source_note_id = ?
|
||||
""", (note_id,))
|
||||
conn.commit()
|
||||
queued = cursor.rowcount
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if queued:
|
||||
logger.info(f"Re-queued {queued} webmentions for note {note_id}")
|
||||
return queued
|
||||
|
||||
|
||||
def queue_webmentions_for_note(
|
||||
db_path: Path,
|
||||
note_id: int,
|
||||
@@ -83,10 +142,9 @@ def queue_webmentions_for_note(
|
||||
|
||||
Per architect's answers (Phase 6):
|
||||
- Called from notes.py AFTER successful database commit
|
||||
- For updates: diff old vs new URLs, queue for new/existing/removed
|
||||
- For creates: queue for all URLs in content
|
||||
- Max 10 webmentions per operation
|
||||
- Priority: in_reply_to URL first, then document order
|
||||
- For creates: reply target first, then links in document order (max 10)
|
||||
- For updates: the same, plus every previously sent target and links
|
||||
removed by the edit (spec 3.1.4)
|
||||
- Webmention sending is best-effort; failures do not affect note creation
|
||||
|
||||
Args:
|
||||
@@ -100,58 +158,19 @@ def queue_webmentions_for_note(
|
||||
Returns:
|
||||
Number of webmentions queued
|
||||
"""
|
||||
# Phase 6: Implement URL diffing for updates
|
||||
# Targets for the current content: reply target first, then links in
|
||||
# document order, at most 10
|
||||
links = extract_links_from_html(content_html) if content_html else []
|
||||
current = _dedupe(([in_reply_to] if in_reply_to else []) + links)[:10]
|
||||
|
||||
if old_content_html is not None:
|
||||
# This is an update - compute URL diff
|
||||
old_links = set(extract_links_from_html(old_content_html))
|
||||
|
||||
# Handle unpublishing case (content_html is None)
|
||||
if content_html is None:
|
||||
# Unpublishing - queue for all old URLs (they're all removed)
|
||||
new_links = set()
|
||||
else:
|
||||
new_links = set(extract_links_from_html(content_html))
|
||||
|
||||
# Compute sets for URL diffing
|
||||
new_urls = new_links - old_links # In new, not in old
|
||||
existing_urls = new_links & old_links # In both (re-validate)
|
||||
removed_urls = old_links - new_links # In old, not in new
|
||||
|
||||
# Build target list: new + existing + removed
|
||||
# (All need webmentions per W3C spec)
|
||||
all_update_urls = list(new_urls) + list(existing_urls) + list(removed_urls)
|
||||
|
||||
# Add in_reply_to if changed
|
||||
target_urls = []
|
||||
if in_reply_to and in_reply_to not in all_update_urls:
|
||||
target_urls.append(in_reply_to)
|
||||
|
||||
# Add the diffed URLs
|
||||
target_urls.extend(all_update_urls)
|
||||
|
||||
# Limit to 10
|
||||
target_urls = target_urls[:10]
|
||||
|
||||
# Update (spec 3.1.4): also re-send to every target notified before,
|
||||
# and to links the edit removed, so they can update or remove the
|
||||
# mention. These are not capped.
|
||||
removed = [u for u in extract_links_from_html(old_content_html) if u not in links]
|
||||
target_urls = _dedupe(current + previous_targets(db_path, note_id) + removed)
|
||||
else:
|
||||
# This is a create (no old content) - use original logic
|
||||
links = extract_links_from_html(content_html) if content_html else []
|
||||
|
||||
# Build priority-ordered list of target URLs
|
||||
target_urls = []
|
||||
|
||||
# 1. in_reply_to gets highest priority
|
||||
if in_reply_to:
|
||||
target_urls.append(in_reply_to)
|
||||
|
||||
# 2. Add extracted links in document order (deduplicated)
|
||||
seen = {in_reply_to} if in_reply_to else set()
|
||||
for link in links:
|
||||
if link not in seen:
|
||||
target_urls.append(link)
|
||||
seen.add(link)
|
||||
|
||||
# 3. Limit to 10 webmentions per operation
|
||||
target_urls = target_urls[:10]
|
||||
target_urls = current
|
||||
|
||||
if not target_urls:
|
||||
logger.debug(f"No links to send webmentions for note {note_id}")
|
||||
|
||||
+142
-52
@@ -1,113 +1,203 @@
|
||||
"""
|
||||
SSRF protection utilities for webmention processing
|
||||
SSRF protection for webmention fetches
|
||||
|
||||
Per ADR-063 Decision 9 and background-worker.md:
|
||||
- Block private IP address ranges
|
||||
- Resolve hostname before making requests
|
||||
Per ADR-063 Decision 9 and Webmention sections 4.2, 4.3 and 4.5:
|
||||
- Every address a hostname resolves to must be public
|
||||
- Redirects are followed by hand so each hop is checked
|
||||
- Redirect count, response size and total time are limited
|
||||
- DEV_MODE exception for localhost testing
|
||||
- Applied to all outgoing HTTP requests
|
||||
|
||||
References:
|
||||
- ADR-063: Webmention Implementation
|
||||
- Background Worker Design: /docs/design/v1.6.0/background-worker.md
|
||||
- W3C Webmention: https://www.w3.org/TR/webmention/#security-considerations
|
||||
"""
|
||||
|
||||
import ipaddress
|
||||
import socket
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import httpx
|
||||
from flask import current_app
|
||||
|
||||
MAX_REDIRECTS = 10
|
||||
MAX_BYTES = 1024 * 1024
|
||||
TIMEOUT = 10
|
||||
|
||||
# Tests can set an httpx transport here (e.g. httpx.MockTransport)
|
||||
TRANSPORT = None
|
||||
|
||||
CGNAT = ipaddress.ip_network('100.64.0.0/10')
|
||||
THIS_NETWORK = ipaddress.ip_network('0.0.0.0/8')
|
||||
|
||||
|
||||
class SSRFBlockedError(Exception):
|
||||
"""Raised when SSRF protection blocks a request"""
|
||||
pass
|
||||
|
||||
|
||||
# Private IP ranges per RFC 1918 and link-local ranges
|
||||
BLOCKED_NETWORKS = [
|
||||
ipaddress.ip_network('10.0.0.0/8'), # RFC 1918 private
|
||||
ipaddress.ip_network('172.16.0.0/12'), # RFC 1918 private
|
||||
ipaddress.ip_network('192.168.0.0/16'), # RFC 1918 private
|
||||
ipaddress.ip_network('127.0.0.0/8'), # Localhost
|
||||
ipaddress.ip_network('169.254.0.0/16'), # Link-local
|
||||
ipaddress.ip_network('::1/128'), # IPv6 localhost
|
||||
ipaddress.ip_network('fc00::/7'), # IPv6 unique local
|
||||
ipaddress.ip_network('fe80::/10'), # IPv6 link-local
|
||||
]
|
||||
class FetchTooLargeError(Exception):
|
||||
"""Raised when a response is larger than the limit"""
|
||||
pass
|
||||
|
||||
|
||||
def user_agent(site_url: str) -> str:
|
||||
"""User-Agent for webmention requests (spec 3.1.2 suggests including 'Webmention')"""
|
||||
from starpunk import __version__
|
||||
return f"StarPunk/{__version__} Webmention (+{site_url})"
|
||||
|
||||
|
||||
def is_private_ip(ip: str) -> bool:
|
||||
"""
|
||||
Check if IP address is in a private/blocked range.
|
||||
Check if an IP address must not be fetched.
|
||||
|
||||
Blocks private, loopback, link-local, multicast, reserved, unspecified,
|
||||
carrier-grade NAT and IPv4-mapped forms of these.
|
||||
|
||||
Args:
|
||||
ip: IP address string (IPv4 or IPv6)
|
||||
|
||||
Returns:
|
||||
True if IP is private/blocked, False otherwise
|
||||
True if IP is blocked, False otherwise
|
||||
"""
|
||||
try:
|
||||
addr = ipaddress.ip_address(ip)
|
||||
return any(addr in network for network in BLOCKED_NETWORKS)
|
||||
addr = ipaddress.ip_address(ip.split('%')[0])
|
||||
except ValueError:
|
||||
# Invalid IP format, treat as blocked
|
||||
return True
|
||||
|
||||
if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped:
|
||||
addr = addr.ipv4_mapped
|
||||
|
||||
if isinstance(addr, ipaddress.IPv4Address) and (addr in CGNAT or addr in THIS_NETWORK):
|
||||
return True
|
||||
|
||||
return (
|
||||
addr.is_private
|
||||
or addr.is_loopback
|
||||
or addr.is_link_local
|
||||
or addr.is_multicast
|
||||
or addr.is_reserved
|
||||
or addr.is_unspecified
|
||||
)
|
||||
|
||||
|
||||
def safe_resolve(hostname: str) -> str:
|
||||
"""
|
||||
Resolve hostname and validate IP is not private.
|
||||
Resolve hostname and check every address is public.
|
||||
|
||||
Per background-worker.md: DEV_MODE exception for localhost testing.
|
||||
|
||||
Args:
|
||||
hostname: Hostname to resolve
|
||||
|
||||
Returns:
|
||||
Resolved IP address if safe
|
||||
First resolved IP address
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError: If hostname resolves to private IP
|
||||
SSRFBlockedError: If any address is blocked
|
||||
socket.gaierror: If hostname cannot be resolved
|
||||
"""
|
||||
# DEV_MODE exception for localhost
|
||||
if current_app.config.get('DEV_MODE') and hostname in ('localhost', '127.0.0.1', '::1'):
|
||||
return '127.0.0.1'
|
||||
|
||||
# Resolve hostname
|
||||
ip = socket.gethostbyname(hostname)
|
||||
addresses = sorted({info[4][0] for info in socket.getaddrinfo(hostname, None)})
|
||||
for ip in addresses:
|
||||
if is_private_ip(ip):
|
||||
raise SSRFBlockedError(f"Blocked request to private IP: {ip}")
|
||||
|
||||
# Check if resolved IP is private
|
||||
if is_private_ip(ip):
|
||||
raise SSRFBlockedError(f"Blocked request to private IP: {ip}")
|
||||
|
||||
return ip
|
||||
return addresses[0]
|
||||
|
||||
|
||||
def validate_url_for_fetch(url: str) -> None:
|
||||
"""
|
||||
Validate URL is safe to fetch (not private IP).
|
||||
|
||||
Args:
|
||||
url: URL to validate
|
||||
Validate URL is safe to fetch (http(s), public addresses only).
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError: If URL points to private IP
|
||||
ValueError: If URL is malformed
|
||||
SSRFBlockedError: If URL points to a blocked address
|
||||
ValueError: If URL is malformed or its host does not resolve
|
||||
"""
|
||||
from urllib.parse import urlparse
|
||||
|
||||
parsed = urlparse(url)
|
||||
|
||||
if not parsed.scheme in ('http', 'https'):
|
||||
if parsed.scheme not in ('http', 'https'):
|
||||
raise ValueError(f"Unsupported URL scheme: {parsed.scheme}")
|
||||
|
||||
if not parsed.netloc:
|
||||
if not parsed.hostname:
|
||||
raise ValueError("URL must include a hostname")
|
||||
|
||||
# Extract hostname (remove port if present)
|
||||
hostname = parsed.hostname
|
||||
if not hostname:
|
||||
raise ValueError("Could not extract hostname from URL")
|
||||
try:
|
||||
safe_resolve(parsed.hostname)
|
||||
except socket.gaierror as e:
|
||||
raise ValueError(f"Could not resolve {parsed.hostname}: {e}")
|
||||
|
||||
# Validate hostname resolves to public IP
|
||||
safe_resolve(hostname)
|
||||
|
||||
@dataclass
|
||||
class FetchResult:
|
||||
"""A fetched response, after redirects"""
|
||||
url: str
|
||||
status_code: int
|
||||
headers: httpx.Headers
|
||||
content: bytes
|
||||
encoding: str
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
try:
|
||||
return self.content.decode(self.encoding, errors='replace')
|
||||
except LookupError:
|
||||
return self.content.decode('utf-8', errors='replace')
|
||||
|
||||
|
||||
def safe_get(url: str, headers: dict, max_bytes: int = MAX_BYTES,
|
||||
max_redirects: int = MAX_REDIRECTS, timeout: float = TIMEOUT) -> FetchResult:
|
||||
"""
|
||||
GET a URL, following redirects by hand and checking each hop.
|
||||
|
||||
Args:
|
||||
url: URL to fetch
|
||||
headers: Request headers
|
||||
max_bytes: Response body limit
|
||||
max_redirects: Redirect limit
|
||||
timeout: Total time limit in seconds
|
||||
|
||||
Returns:
|
||||
FetchResult for the final response (any status code)
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError, ValueError: URL or a redirect target is not allowed
|
||||
FetchTooLargeError: Body exceeds max_bytes
|
||||
httpx.HTTPError: Network errors, too many redirects, time limit
|
||||
"""
|
||||
deadline = time.monotonic() + timeout
|
||||
|
||||
with httpx.Client(follow_redirects=False, transport=TRANSPORT) as client:
|
||||
for _ in range(max_redirects + 1):
|
||||
validate_url_for_fetch(url)
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise httpx.TimeoutException("Fetch exceeded time limit")
|
||||
|
||||
with client.stream('GET', url, headers=headers, timeout=remaining) as response:
|
||||
location = response.headers.get('Location')
|
||||
if response.is_redirect and location:
|
||||
url = urljoin(url, location)
|
||||
continue
|
||||
|
||||
content_length = response.headers.get('Content-Length', '')
|
||||
if content_length.isdigit() and int(content_length) > max_bytes:
|
||||
raise FetchTooLargeError(f"Response exceeds size limit ({max_bytes} bytes)")
|
||||
|
||||
content = b''
|
||||
for chunk in response.iter_bytes(chunk_size=8192):
|
||||
content += chunk
|
||||
if len(content) > max_bytes:
|
||||
raise FetchTooLargeError(f"Response exceeds size limit ({max_bytes} bytes)")
|
||||
if time.monotonic() > deadline:
|
||||
raise httpx.TimeoutException("Fetch exceeded time limit")
|
||||
|
||||
return FetchResult(
|
||||
url=url,
|
||||
status_code=response.status_code,
|
||||
headers=response.headers,
|
||||
content=content,
|
||||
encoding=response.charset_encoding or 'utf-8',
|
||||
)
|
||||
|
||||
raise httpx.TooManyRedirects(f"More than {max_redirects} redirects")
|
||||
+174
-147
@@ -12,30 +12,32 @@ References:
|
||||
- ADR-063: Webmention Implementation
|
||||
"""
|
||||
|
||||
import fcntl
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import sqlite3
|
||||
import httpx
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional, TypedDict
|
||||
from urllib.parse import urlparse
|
||||
from typing import Dict, Any, Optional
|
||||
from urllib.parse import urljoin
|
||||
from lxml import html as lxml_html
|
||||
|
||||
from starpunk.webmention_processor import process_webmention, ValidationError
|
||||
from starpunk.webmention_ssrf import validate_url_for_fetch, SSRFBlockedError
|
||||
from flask import has_app_context
|
||||
from starpunk.webmention_processor import process_webmention, TransientError, ValidationError
|
||||
from starpunk.webmention_ssrf import (
|
||||
FetchTooLargeError, SSRFBlockedError, safe_get, user_agent, validate_url_for_fetch,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Link header (RFC 8288): <uri> then ;-separated params; quoted values may
|
||||
# contain , and ;
|
||||
LINK_RE = re.compile(r'<([^>]*)>((?:\s*;\s*[^;,"=\s]+\s*(?:=\s*(?:"[^"]*"|[^;,]*))?)*)')
|
||||
LINK_PARAM_RE = re.compile(r';\s*([^;,"=\s]+)\s*(?:=\s*(?:"([^"]*)"|([^;,]*)))?')
|
||||
|
||||
# Discovery cache entry type
|
||||
class DiscoveryCacheEntry(TypedDict):
|
||||
"""Cache entry for endpoint discovery results"""
|
||||
endpoint: Optional[str] # None = negative cache (no endpoint found)
|
||||
expires_at: datetime
|
||||
|
||||
|
||||
class WebmentionWorker(threading.Thread):
|
||||
@@ -47,6 +49,7 @@ class WebmentionWorker(threading.Thread):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app,
|
||||
db_path: Path,
|
||||
site_url: str,
|
||||
poll_interval: int = 10,
|
||||
@@ -58,6 +61,7 @@ class WebmentionWorker(threading.Thread):
|
||||
Initialize webmention worker thread
|
||||
|
||||
Args:
|
||||
app: Flask app (the SSRF checks read its config)
|
||||
db_path: Path to SQLite database
|
||||
site_url: Site URL for User-Agent header
|
||||
poll_interval: Seconds between queue checks (default: 10)
|
||||
@@ -66,6 +70,7 @@ class WebmentionWorker(threading.Thread):
|
||||
auto_approve: Auto-approve valid webmentions (default: True)
|
||||
"""
|
||||
super().__init__(daemon=True, name="webmention-worker")
|
||||
self.app = app
|
||||
self.db_path = db_path
|
||||
self.site_url = site_url
|
||||
self.poll_interval = poll_interval
|
||||
@@ -74,10 +79,7 @@ class WebmentionWorker(threading.Thread):
|
||||
self.auto_approve = auto_approve
|
||||
self._stop_event = threading.Event()
|
||||
self._last_processed = None
|
||||
|
||||
# Phase 3: Discovery cache (worker instance variable)
|
||||
# Per architect's answer to Q4
|
||||
self._discovery_cache: Dict[str, DiscoveryCacheEntry] = {}
|
||||
self._lock_file = None
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
@@ -92,19 +94,23 @@ class WebmentionWorker(threading.Thread):
|
||||
try:
|
||||
logger.info("Webmention worker started")
|
||||
|
||||
# Cleanup orphaned items from previous crashes
|
||||
self._cleanup_orphaned_items()
|
||||
|
||||
# Main processing loop
|
||||
while not self._stop_event.is_set():
|
||||
# Every gunicorn process starts a worker; only the one holding
|
||||
# the lock processes queues, so nothing is sent twice
|
||||
if not self._acquire_lock():
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
continue
|
||||
|
||||
try:
|
||||
processed = 0
|
||||
|
||||
# Process incoming webmentions
|
||||
processed += self._process_pending_validations()
|
||||
with self.app.app_context():
|
||||
# Process incoming webmentions
|
||||
processed += self._process_pending_validations()
|
||||
|
||||
# Phase 3: Process outgoing webmentions
|
||||
processed += self._process_pending_sends()
|
||||
# Phase 3: Process outgoing webmentions
|
||||
processed += self._process_pending_sends()
|
||||
|
||||
# Update last processed time
|
||||
if processed > 0:
|
||||
@@ -121,8 +127,40 @@ class WebmentionWorker(threading.Thread):
|
||||
except Exception as e:
|
||||
logger.error(f"Webmention worker thread failed: {e}", exc_info=True)
|
||||
finally:
|
||||
self._release_lock()
|
||||
logger.info("Webmention worker stopped")
|
||||
|
||||
def _acquire_lock(self) -> bool:
|
||||
"""
|
||||
Take the cross-process worker lock (non-blocking)
|
||||
|
||||
On first acquisition, also reset items left mid-send by a previous
|
||||
holder. The OS releases the lock if this process dies.
|
||||
|
||||
Returns:
|
||||
True if this worker holds the lock
|
||||
"""
|
||||
if self._lock_file:
|
||||
return True
|
||||
|
||||
lock_file = open(Path(self.db_path).parent / "webmention-worker.lock", "w")
|
||||
try:
|
||||
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
lock_file.close()
|
||||
return False
|
||||
|
||||
self._lock_file = lock_file
|
||||
logger.info("Webmention worker holds the queue lock")
|
||||
self._cleanup_orphaned_items()
|
||||
return True
|
||||
|
||||
def _release_lock(self):
|
||||
"""Release the cross-process worker lock if held"""
|
||||
if self._lock_file:
|
||||
self._lock_file.close()
|
||||
self._lock_file = None
|
||||
|
||||
def _cleanup_orphaned_items(self):
|
||||
"""
|
||||
Clean up orphaned 'sending' status items on startup
|
||||
@@ -226,25 +264,34 @@ class WebmentionWorker(threading.Thread):
|
||||
|
||||
moderation_status = 'approved' if self.auto_approve else 'pending'
|
||||
|
||||
# An update to an existing mention (spec 3.2.4) keeps the admin's
|
||||
# moderation decision; only a first-time valid mention gets the
|
||||
# default. Previously valid = validated with no error.
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET validation_status = 'valid',
|
||||
SET moderation_status = CASE
|
||||
WHEN moderation_status IN ('hidden', 'deleted') THEN moderation_status
|
||||
WHEN validated_at IS NOT NULL AND validation_error IS NULL
|
||||
THEN moderation_status
|
||||
ELSE ?
|
||||
END,
|
||||
validation_status = 'valid',
|
||||
validation_error = NULL,
|
||||
validated_at = CURRENT_TIMESTAMP,
|
||||
mention_type = ?,
|
||||
author_name = ?,
|
||||
author_url = ?,
|
||||
author_photo = ?,
|
||||
content_snippet = ?,
|
||||
moderation_status = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""", (
|
||||
moderation_status,
|
||||
result['mention_type'],
|
||||
result['author_name'],
|
||||
result['author_url'],
|
||||
result['author_photo'],
|
||||
result['content_snippet'],
|
||||
moderation_status,
|
||||
webmention_id
|
||||
))
|
||||
|
||||
@@ -253,11 +300,17 @@ class WebmentionWorker(threading.Thread):
|
||||
|
||||
logger.info(
|
||||
f"Validated webmention: {source_url} -> {target_url} "
|
||||
f"(type={result['mention_type']}, status={moderation_status})"
|
||||
f"(type={result['mention_type']})"
|
||||
)
|
||||
|
||||
except TransientError as e:
|
||||
# Source unreachable right now: keep a previously valid mention
|
||||
self._mark_validation_failed(webmention_id, source_url, target_url, str(e),
|
||||
transient=True)
|
||||
|
||||
except ValidationError as e:
|
||||
# Validation failed - update with error
|
||||
# Validation failed (410/4xx, no link): the mention is removed
|
||||
# from display (spec 3.2.4)
|
||||
self._mark_validation_failed(webmention_id, source_url, target_url, str(e))
|
||||
|
||||
except Exception as e:
|
||||
@@ -275,11 +328,15 @@ class WebmentionWorker(threading.Thread):
|
||||
webmention_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
error: str
|
||||
error: str,
|
||||
transient: bool = False
|
||||
):
|
||||
"""
|
||||
Mark webmention validation as failed/invalid
|
||||
|
||||
A transient failure of a mention that was valid before restores it to
|
||||
valid with its previous data, rather than hiding it.
|
||||
|
||||
Args:
|
||||
webmention_id: Database ID
|
||||
source_url: Source URL
|
||||
@@ -289,7 +346,9 @@ class WebmentionWorker(threading.Thread):
|
||||
# Determine if error or invalid
|
||||
# Per background-worker.md: HTTP 4xx and "no link" are invalid
|
||||
# All other errors are 'error' status
|
||||
if any(x in error.lower() for x in ['does not link', 'returned 4']):
|
||||
if transient:
|
||||
status = 'error'
|
||||
elif any(x in error.lower() for x in ['does not link', 'returned 4']):
|
||||
status = 'invalid'
|
||||
else:
|
||||
status = 'error'
|
||||
@@ -298,6 +357,22 @@ class WebmentionWorker(threading.Thread):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
if transient:
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET validation_status = 'valid',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND validated_at IS NOT NULL AND validation_error IS NULL
|
||||
""", (webmention_id,))
|
||||
if cursor.rowcount:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logger.warning(
|
||||
f"Could not re-check webmention, keeping it: "
|
||||
f"{source_url} -> {target_url}: {error}"
|
||||
)
|
||||
return
|
||||
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET validation_status = ?,
|
||||
@@ -418,11 +493,13 @@ class WebmentionWorker(threading.Thread):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Send webmention POST
|
||||
# Send webmention POST (spec 3.1.3)
|
||||
try:
|
||||
# SSRF protection for endpoint URL
|
||||
# SSRF protection for endpoint URL (spec 4.3)
|
||||
validate_url_for_fetch(endpoint)
|
||||
|
||||
# Redirects are not followed: httpx would turn a 301/302 POST
|
||||
# into a GET without the body
|
||||
response = httpx.post(
|
||||
endpoint,
|
||||
data={
|
||||
@@ -430,25 +507,24 @@ class WebmentionWorker(threading.Thread):
|
||||
'target': target_url
|
||||
},
|
||||
timeout=10,
|
||||
headers={'User-Agent': f'StarPunk/1.6.0 (+{self.site_url})'},
|
||||
follow_redirects=True
|
||||
headers={'User-Agent': user_agent(self.site_url)},
|
||||
follow_redirects=False
|
||||
)
|
||||
|
||||
# Check response
|
||||
# Any 2xx is success (spec 3.1.3)
|
||||
if 200 <= response.status_code < 300:
|
||||
# Success
|
||||
self._mark_send_success(queue_id, source_url, target_url)
|
||||
|
||||
elif 400 <= response.status_code < 500:
|
||||
# Permanent failure (4xx)
|
||||
error = f"HTTP {response.status_code}: {response.text[:200]}"
|
||||
self._mark_send_failed(queue_id, source_url, target_url, error)
|
||||
|
||||
else:
|
||||
# Temporary failure (5xx) - retry
|
||||
elif response.status_code == 429 or response.status_code >= 500:
|
||||
# Rate limited or server error - retry
|
||||
error = f"HTTP {response.status_code}: {response.text[:200]}"
|
||||
self._mark_send_retry(queue_id, source_url, target_url, retry_count, error)
|
||||
|
||||
else:
|
||||
# Permanent failure (3xx, 4xx)
|
||||
error = f"HTTP {response.status_code}: {response.text[:200]}"
|
||||
self._mark_send_failed(queue_id, source_url, target_url, error)
|
||||
|
||||
except (httpx.TimeoutException, httpx.HTTPError) as e:
|
||||
# Connection error - retry
|
||||
self._mark_send_retry(queue_id, source_url, target_url, retry_count, str(e))
|
||||
@@ -464,100 +540,81 @@ class WebmentionWorker(threading.Thread):
|
||||
|
||||
def _discover_endpoint(self, target_url: str) -> Optional[str]:
|
||||
"""
|
||||
Discover webmention endpoint for target URL with caching
|
||||
Discover the webmention endpoint for a target URL (spec 3.1.2)
|
||||
|
||||
Per architect's answers to Q3, Q4, Q10:
|
||||
- GET only (no HEAD)
|
||||
- Check Link header → <link rel="webmention"> → <a rel="webmention">
|
||||
- First match wins
|
||||
- Cache results: 1h for found, 24h for not found
|
||||
- SSRF protection on target URL
|
||||
|
||||
Args:
|
||||
target_url: Target URL to discover endpoint for
|
||||
- GET the target, following redirects (each hop SSRF-checked)
|
||||
- The first Link header with rel=webmention wins, then the first
|
||||
<link> or <a> with rel=webmention and an href, in document order
|
||||
- rel is a space-separated token list, matched case-insensitively
|
||||
- Relative endpoints resolve against the final URL; an empty href
|
||||
is the target itself; query strings are kept
|
||||
- Not cached: an updated post must re-discover (spec 3.1.4)
|
||||
|
||||
Returns:
|
||||
Endpoint URL if found, None otherwise
|
||||
"""
|
||||
# Check cache first
|
||||
cached = self._discovery_cache.get(target_url)
|
||||
if cached and cached['expires_at'] > datetime.utcnow():
|
||||
logger.debug(f"Using cached endpoint for {target_url}: {cached['endpoint']}")
|
||||
return cached['endpoint']
|
||||
Endpoint URL, or None if the target has none (or is blocked/4xx)
|
||||
|
||||
# SSRF protection
|
||||
Raises:
|
||||
httpx.HTTPError: Network errors and 5xx, so the caller retries
|
||||
"""
|
||||
try:
|
||||
validate_url_for_fetch(target_url)
|
||||
result = safe_get(target_url, headers={'User-Agent': user_agent(self.site_url)})
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
logger.warning(f"SSRF protection blocked target {target_url}: {e}")
|
||||
# Cache negative result
|
||||
self._cache_endpoint(target_url, None, hours=24)
|
||||
return None
|
||||
except FetchTooLargeError:
|
||||
logger.info(f"Target too large for discovery: {target_url}")
|
||||
return None
|
||||
|
||||
# Fetch target URL
|
||||
try:
|
||||
response = httpx.get(
|
||||
target_url,
|
||||
timeout=10,
|
||||
headers={'User-Agent': f'StarPunk/1.6.0 (+{self.site_url})'},
|
||||
follow_redirects=True
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
# 1. Check Link header
|
||||
link_header = response.headers.get('Link', '')
|
||||
if link_header:
|
||||
endpoint = self._parse_link_header(link_header, response.url)
|
||||
if endpoint:
|
||||
self._cache_endpoint(target_url, endpoint, hours=1)
|
||||
return endpoint
|
||||
|
||||
# 2. Parse HTML for <link> and <a> elements
|
||||
content_type = response.headers.get('Content-Type', '')
|
||||
if 'html' in content_type.lower():
|
||||
endpoint = self._parse_html_for_endpoint(response.text, response.url)
|
||||
if endpoint:
|
||||
self._cache_endpoint(target_url, endpoint, hours=1)
|
||||
return endpoint
|
||||
|
||||
# No endpoint found - cache negative result
|
||||
logger.info(f"No webmention endpoint found for {target_url}")
|
||||
self._cache_endpoint(target_url, None, hours=24)
|
||||
if result.status_code >= 500:
|
||||
raise httpx.HTTPError(f"Target returned {result.status_code}")
|
||||
if result.status_code >= 400:
|
||||
logger.info(f"Target returned {result.status_code}: {target_url}")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to discover endpoint for {target_url}: {e}")
|
||||
# Don't cache on error - allow retry
|
||||
return None
|
||||
for link_header in result.headers.get_list('Link'):
|
||||
endpoint = self._parse_link_header(link_header, result.url)
|
||||
if endpoint is not None:
|
||||
return endpoint
|
||||
|
||||
content_type = result.headers.get('Content-Type', '')
|
||||
if 'html' in content_type.lower():
|
||||
endpoint = self._parse_html_for_endpoint(result.text, result.url)
|
||||
if endpoint is not None:
|
||||
return endpoint
|
||||
|
||||
logger.info(f"No webmention endpoint found for {target_url}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _has_webmention_rel(rel: str) -> bool:
|
||||
return 'webmention' in rel.lower().split()
|
||||
|
||||
def _parse_link_header(self, link_header: str, base_url: str) -> Optional[str]:
|
||||
"""
|
||||
Parse Link header for webmention endpoint
|
||||
Parse a Link header (RFC 8288) for a rel=webmention link
|
||||
|
||||
Args:
|
||||
link_header: Raw Link header value
|
||||
link_header: Link header value (may hold several links)
|
||||
base_url: Base URL for resolving relative URLs
|
||||
|
||||
Returns:
|
||||
Absolute endpoint URL if found, None otherwise
|
||||
Absolute endpoint URL of the first match, None otherwise
|
||||
"""
|
||||
# Simple parsing - look for rel="webmention"
|
||||
# Format: <URL>; rel="webmention"
|
||||
import re
|
||||
pattern = r'<([^>]+)>\s*;\s*rel\s*=\s*["\']?webmention["\']?'
|
||||
match = re.search(pattern, link_header, re.IGNORECASE)
|
||||
|
||||
if match:
|
||||
endpoint = match.group(1)
|
||||
# Make absolute if relative
|
||||
from urllib.parse import urljoin
|
||||
return urljoin(base_url, endpoint)
|
||||
for match in LINK_RE.finditer(link_header):
|
||||
uri, params = match.group(1), match.group(2)
|
||||
for param in LINK_PARAM_RE.finditer(params):
|
||||
if param.group(1).lower() != 'rel':
|
||||
continue
|
||||
rel = param.group(2) if param.group(2) is not None else (param.group(3) or '')
|
||||
if self._has_webmention_rel(rel):
|
||||
return urljoin(base_url, uri.strip())
|
||||
break # only the first rel parameter counts (RFC 8288)
|
||||
|
||||
return None
|
||||
|
||||
def _parse_html_for_endpoint(self, html_content: str, base_url: str) -> Optional[str]:
|
||||
"""
|
||||
Parse HTML for webmention endpoint in <link> or <a> elements
|
||||
Find the first <link> or <a> with rel=webmention, in document order
|
||||
|
||||
Args:
|
||||
html_content: HTML content
|
||||
@@ -567,48 +624,18 @@ class WebmentionWorker(threading.Thread):
|
||||
Absolute endpoint URL if found, None otherwise
|
||||
"""
|
||||
try:
|
||||
doc = lxml_html.fromstring(html_content)
|
||||
|
||||
# Check <link rel="webmention"> first
|
||||
links = doc.xpath('//link[@rel="webmention"]/@href')
|
||||
if links:
|
||||
from urllib.parse import urljoin
|
||||
return urljoin(base_url, links[0])
|
||||
|
||||
# Check <a rel="webmention">
|
||||
anchors = doc.xpath('//a[@rel="webmention"]/@href')
|
||||
if anchors:
|
||||
from urllib.parse import urljoin
|
||||
return urljoin(base_url, anchors[0])
|
||||
|
||||
doc = lxml_html.document_fromstring(html_content)
|
||||
except Exception as e:
|
||||
logger.debug(f"HTML parsing failed: {e}")
|
||||
return None
|
||||
|
||||
for element in doc.iter('link', 'a'):
|
||||
href = element.get('href')
|
||||
if href is not None and self._has_webmention_rel(element.get('rel', '')):
|
||||
return urljoin(base_url, href.strip())
|
||||
|
||||
return None
|
||||
|
||||
def _cache_endpoint(self, target_url: str, endpoint: Optional[str], hours: int):
|
||||
"""
|
||||
Cache endpoint discovery result
|
||||
|
||||
Args:
|
||||
target_url: Target URL
|
||||
endpoint: Discovered endpoint (None = negative cache)
|
||||
hours: TTL in hours
|
||||
"""
|
||||
self._discovery_cache[target_url] = {
|
||||
'endpoint': endpoint,
|
||||
'expires_at': datetime.utcnow() + timedelta(hours=hours)
|
||||
}
|
||||
|
||||
# Optional: Evict old entries (keep cache bounded)
|
||||
if len(self._discovery_cache) > 1000:
|
||||
# Remove expired entries
|
||||
now = datetime.utcnow()
|
||||
self._discovery_cache = {
|
||||
k: v for k, v in self._discovery_cache.items()
|
||||
if v['expires_at'] > now
|
||||
}
|
||||
|
||||
def _mark_send_success(self, queue_id: int, source_url: str, target_url: str):
|
||||
"""Mark webmention send as successful"""
|
||||
try:
|
||||
|
||||
+99
-27
@@ -1,36 +1,75 @@
|
||||
/* StarPunk CSS - Minimal responsive stylesheet */
|
||||
|
||||
:root {
|
||||
--color-text: #333; --color-text-light: #666; --color-bg: #fff; --color-bg-alt: #f5f5f5;
|
||||
--color-link: #0066cc; --color-link-hover: #004499; --color-border: #ddd;
|
||||
--color-text: #1f2328; --color-text-light: #59636e; --color-bg: #fff; --color-bg-alt: #f4f5f7;
|
||||
--color-link: #0a5cc2; --color-link-hover: #083f85; --color-border: #e3e6ea; --color-accent-soft: #e8f0fb;
|
||||
--color-success: #28a745; --color-error: #dc3545; --color-warning: #ffc107; --color-info: #17a2b8;
|
||||
--font-body: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
|
||||
--font-mono: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
|
||||
--font-body: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
--font-reading: Charter, 'Bitstream Charter', 'Sitka Text', Cambria, 'Noto Serif', Georgia, serif;
|
||||
--font-mono: ui-monospace, 'SF Mono', Monaco, Consolas, 'Liberation Mono', monospace;
|
||||
--spacing-xs: 0.25rem; --spacing-sm: 0.5rem; --spacing-md: 1rem; --spacing-lg: 2rem; --spacing-xl: 4rem;
|
||||
--max-width: 42rem; --border-radius: 4px;
|
||||
--max-width: 40rem; --border-radius: 6px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--color-text: #e4e6e9; --color-text-light: #9aa3ad; --color-bg: #121417; --color-bg-alt: #1c1f24;
|
||||
--color-link: #74b0ff; --color-link-hover: #a8ceff; --color-border: #2b3038; --color-accent-soft: #1a2738;
|
||||
}
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body { font-family: var(--font-body); font-size: 1rem; line-height: 1.6; color: var(--color-text); background: var(--color-bg); padding: var(--spacing-md); }
|
||||
body {
|
||||
font-family: var(--font-body); font-size: 1rem; line-height: 1.6; color: var(--color-text); background: var(--color-bg);
|
||||
padding: var(--spacing-md); min-height: 100vh; display: flex; flex-direction: column;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
h1, h2, h3 { margin-bottom: var(--spacing-md); line-height: 1.2; font-weight: 600; }
|
||||
h1 { font-size: 2rem; } h2 { font-size: 1.5rem; } h3 { font-size: 1.25rem; }
|
||||
h1, h2, h3 { margin-bottom: var(--spacing-md); line-height: 1.25; font-weight: 650; letter-spacing: -0.01em; }
|
||||
h1 { font-size: 1.75rem; } h2 { font-size: 1.375rem; } h3 { font-size: 1.125rem; }
|
||||
p { margin-bottom: var(--spacing-md); }
|
||||
a { color: var(--color-link); text-decoration: none; }
|
||||
a:hover { color: var(--color-link-hover); text-decoration: underline; }
|
||||
:focus-visible { outline: 2px solid var(--color-link); outline-offset: 2px; border-radius: 2px; }
|
||||
|
||||
code, pre { font-family: var(--font-mono); background: var(--color-bg-alt); padding: 0.125rem 0.25rem; border-radius: var(--border-radius); font-size: 0.875rem; }
|
||||
code, pre { font-family: var(--font-mono); background: var(--color-bg-alt); padding: 0.125rem 0.3rem; border-radius: var(--border-radius); font-size: 0.875em; }
|
||||
pre { padding: var(--spacing-md); overflow-x: auto; margin-bottom: var(--spacing-md); }
|
||||
pre code { padding: 0; background: none; }
|
||||
|
||||
header, main, footer { max-width: var(--max-width); margin: 0 auto; }
|
||||
header { margin-bottom: var(--spacing-lg); padding-bottom: var(--spacing-md); border-bottom: 2px solid var(--color-border); }
|
||||
header h1 { margin-bottom: var(--spacing-sm); }
|
||||
header h1 a { color: var(--color-text); text-decoration: none; }
|
||||
nav { display: flex; gap: var(--spacing-md); flex-wrap: wrap; }
|
||||
nav a { padding: var(--spacing-sm) var(--spacing-md); border-radius: var(--border-radius); transition: background 0.2s; }
|
||||
nav a:hover { background: var(--color-bg-alt); text-decoration: none; }
|
||||
main { min-height: 60vh; margin-bottom: var(--spacing-lg); }
|
||||
footer { padding-top: var(--spacing-lg); border-top: 1px solid var(--color-border); text-align: center; color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
|
||||
|
||||
/* Page frame */
|
||||
.site-header, main, .site-footer { width: 100%; max-width: var(--max-width); margin-left: auto; margin-right: auto; }
|
||||
main { flex: 1; margin-bottom: var(--spacing-xl); }
|
||||
|
||||
.site-header {
|
||||
display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: var(--spacing-sm) var(--spacing-lg);
|
||||
padding: var(--spacing-md) 0 var(--spacing-md); margin-bottom: var(--spacing-lg); border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
.site-title { font-size: 1.375rem; font-weight: 700; letter-spacing: -0.02em; color: var(--color-text); }
|
||||
.site-title:hover { color: var(--color-link); text-decoration: none; }
|
||||
.site-nav { display: flex; align-items: center; flex-wrap: wrap; gap: var(--spacing-md); font-size: 0.9375rem; }
|
||||
.site-nav > a { color: var(--color-text-light); }
|
||||
.site-nav > a:hover { color: var(--color-link); text-decoration: none; }
|
||||
.site-search { display: flex; }
|
||||
.site-search input[type="search"] {
|
||||
width: 10rem; padding: 0.3rem 0.6rem; font: inherit; font-size: 0.875rem; color: var(--color-text);
|
||||
background: var(--color-bg-alt); border: 1px solid var(--color-border); border-right: 0; border-radius: var(--border-radius) 0 0 var(--border-radius);
|
||||
}
|
||||
.site-search button {
|
||||
padding: 0.3rem 0.6rem; font: inherit; font-size: 0.875rem; cursor: pointer; color: var(--color-text-light);
|
||||
background: var(--color-bg-alt); border: 1px solid var(--color-border); border-radius: 0 var(--border-radius) var(--border-radius) 0;
|
||||
}
|
||||
.site-search button:hover { color: var(--color-link); }
|
||||
|
||||
.site-footer { padding: var(--spacing-md) 0; border-top: 1px solid var(--color-border); color: var(--color-text-light); font-size: 0.8125rem; }
|
||||
.site-footer p { margin: 0; }
|
||||
.site-footer a { color: var(--color-text-light); text-decoration: underline; text-decoration-color: var(--color-border); }
|
||||
.site-footer a:hover { color: var(--color-link); }
|
||||
|
||||
.page-title { font-size: 1.375rem; margin-bottom: var(--spacing-lg); }
|
||||
.tag-name { color: var(--color-link); }
|
||||
.tag-name::before { content: "#"; }
|
||||
|
||||
.dev-mode-warning { background: var(--color-error); color: white; padding: var(--spacing-md); text-align: center; font-weight: bold; margin: calc(-1 * var(--spacing-md)); margin-bottom: var(--spacing-lg); }
|
||||
|
||||
@@ -40,17 +79,46 @@ footer { padding-top: var(--spacing-lg); border-top: 1px solid var(--color-borde
|
||||
.flash-warning { background: #fff3cd; border-color: var(--color-warning); color: #856404; }
|
||||
.flash-info { background: #d1ecf1; border-color: var(--color-info); color: #0c5460; }
|
||||
|
||||
/* Notes */
|
||||
.h-entry { margin-bottom: var(--spacing-lg); padding-bottom: var(--spacing-lg); border-bottom: 1px solid var(--color-border); }
|
||||
.h-entry:last-child { border-bottom: none; }
|
||||
.note-preview .e-content { margin-bottom: var(--spacing-md); }
|
||||
.note-meta { color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.h-entry > .p-name a { color: var(--color-text); }
|
||||
.h-entry > .p-name a:hover { color: var(--color-link); }
|
||||
.e-content, .p-summary { font-family: var(--font-reading); font-size: 1.125rem; line-height: 1.65; overflow-wrap: anywhere; }
|
||||
.e-content > :last-child, .p-summary > :last-child { margin-bottom: 0; }
|
||||
.e-content a { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; text-decoration-color: color-mix(in srgb, currentColor 40%, transparent); }
|
||||
.e-content a:hover { text-decoration-color: currentColor; }
|
||||
.e-content blockquote { margin: 0 0 var(--spacing-md); padding-left: var(--spacing-md); border-left: 3px solid var(--color-border); color: var(--color-text-light); }
|
||||
.e-content ul, .e-content ol { margin: 0 0 var(--spacing-md) 1.25rem; }
|
||||
.e-content img { max-width: 100%; height: auto; }
|
||||
.e-content h1, .e-content h2, .e-content h3 { font-family: var(--font-body); }
|
||||
.reaction { margin-bottom: var(--spacing-sm); font-size: 0.9375rem; color: var(--color-text-light); overflow-wrap: anywhere; }
|
||||
.reaction-verb { font-weight: 600; margin-right: 0.25rem; }
|
||||
.reaction-like .reaction-verb { color: #c2410c; }
|
||||
.reaction-repost .reaction-verb { color: #15803d; }
|
||||
.reaction a { color: var(--color-text); font-weight: 500; }
|
||||
.reaction a:hover { color: var(--color-link); }
|
||||
.reaction-by { margin-left: 0.25rem; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.reaction-like .reaction-verb { color: #fb923c; }
|
||||
.reaction-repost .reaction-verb { color: #4ade80; }
|
||||
}
|
||||
.read-more { margin: var(--spacing-sm) 0 0; font-size: 0.9375rem; }
|
||||
|
||||
.note-meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 0.4rem; margin-top: var(--spacing-sm); color: var(--color-text-light); font-size: 0.8125rem; }
|
||||
.note-meta a { color: var(--color-text-light); }
|
||||
.note-nav { margin-top: var(--spacing-md); }
|
||||
.note-meta a:hover { color: var(--color-link); }
|
||||
.note-meta > :not(.visually-hidden) + :not(.visually-hidden)::before { content: "·"; margin-right: 0.4rem; color: var(--color-text-light); }
|
||||
.note-tags { display: inline-flex; flex-wrap: wrap; gap: 0 0.4rem; }
|
||||
.note-meta > .note-tags::before { margin-right: 0; }
|
||||
.note-tags .p-category::before { content: "#"; opacity: 0.7; }
|
||||
.h-entry:not(.note-preview) .note-meta { margin-top: var(--spacing-md); }
|
||||
.note-nav { margin-top: var(--spacing-lg); font-size: 0.9375rem; }
|
||||
.empty-state { text-align: center; padding: var(--spacing-xl); color: var(--color-text-light); }
|
||||
|
||||
.form-group { margin-bottom: var(--spacing-md); }
|
||||
label { display: block; margin-bottom: var(--spacing-sm); font-weight: 600; }
|
||||
input[type="text"], input[type="url"], input[type="email"], textarea { width: 100%; padding: var(--spacing-sm); border: 1px solid var(--color-border); border-radius: var(--border-radius); font-family: inherit; font-size: 1rem; }
|
||||
input[type="text"], input[type="url"], input[type="email"], input[type="search"], textarea { color: var(--color-text); background: var(--color-bg); width: 100%; padding: var(--spacing-sm); border: 1px solid var(--color-border); border-radius: var(--border-radius); font-family: inherit; font-size: 1rem; }
|
||||
textarea { font-family: var(--font-mono); resize: vertical; min-height: 10rem; }
|
||||
small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.form-checkbox { display: flex; align-items: center; gap: var(--spacing-sm); }
|
||||
@@ -68,7 +136,8 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
.button-warning { background: var(--color-warning); color: #333; border-color: var(--color-warning); }
|
||||
.button-small { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
|
||||
|
||||
.admin-container { max-width: 60rem; }
|
||||
.admin-page { --max-width: 60rem; }
|
||||
.table-scroll { overflow-x: auto; }
|
||||
.admin-nav { display: flex; gap: var(--spacing-md); padding: var(--spacing-md); background: var(--color-bg-alt); border-radius: var(--border-radius); margin-bottom: var(--spacing-lg); flex-wrap: wrap; align-items: center; }
|
||||
.admin-nav .logout-form { margin-left: auto; }
|
||||
.admin-nav .logout-form button { margin: 0; }
|
||||
@@ -79,7 +148,7 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
.note-table { width: 100%; border-collapse: collapse; margin-top: var(--spacing-md); }
|
||||
.note-table th, .note-table td { padding: var(--spacing-md); text-align: left; border-bottom: 1px solid var(--color-border); }
|
||||
.note-table th { background: var(--color-bg-alt); font-weight: 600; }
|
||||
.note-content-preview { max-width: 30rem; }
|
||||
.note-content-preview { min-width: 14rem; overflow-wrap: anywhere; }
|
||||
.note-excerpt { margin-bottom: var(--spacing-xs); }
|
||||
.note-slug { color: var(--color-text-light); font-family: var(--font-mono); font-size: 0.75rem; }
|
||||
.note-date { white-space: nowrap; color: var(--color-text-light); }
|
||||
@@ -171,11 +240,14 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
body { padding: var(--spacing-lg); }
|
||||
h1 { font-size: 2.5rem; } h2 { font-size: 2rem; } h3 { font-size: 1.5rem; }
|
||||
body { padding: var(--spacing-md) var(--spacing-lg); }
|
||||
h1 { font-size: 2rem; } h2 { font-size: 1.5rem; } h3 { font-size: 1.25rem; }
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.site-nav { width: 100%; }
|
||||
.site-search { flex: 1; justify-content: flex-end; }
|
||||
.site-search input[type="search"] { width: 100%; max-width: 12rem; }
|
||||
.note-table { font-size: 0.875rem; }
|
||||
.note-table th, .note-table td { padding: var(--spacing-sm); }
|
||||
.note-actions .button { font-size: 0.75rem; padding: 0.25rem 0.5rem; }
|
||||
@@ -426,7 +498,7 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
|
||||
.content-preview {
|
||||
padding: var(--spacing-sm);
|
||||
background: white;
|
||||
background: var(--color-bg);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 4px;
|
||||
margin-top: var(--spacing-xs);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Post Deleted - {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article class="error-page">
|
||||
<h1>410 - Post Deleted</h1>
|
||||
<p>This post has been deleted.</p>
|
||||
<p><a href="/">Return to homepage</a></p>
|
||||
</article>
|
||||
{% endblock %}
|
||||
@@ -1,5 +1,8 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{# Admin pages use a wider frame so tables fit under the header #}
|
||||
{% block body_class %} class="admin-page"{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="admin-container">
|
||||
<nav class="admin-nav">
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
</div>
|
||||
|
||||
{% if notes %}
|
||||
<div class="table-scroll">
|
||||
<table class="note-table">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -67,6 +68,7 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{# Pagination (v1.6.0) #}
|
||||
{% if total_pages > 1 %}
|
||||
|
||||
@@ -12,12 +12,15 @@
|
||||
|
||||
<form action="{{ url_for('admin.update_note_submit', note_id=note.id) }}" method="POST" class="note-form">
|
||||
<div class="form-group">
|
||||
<label for="content">Content (Markdown)</label>
|
||||
{% if note.reacts_to %}
|
||||
<p class="note-meta">{{ '★ Like of' if note.kind == 'like' else '🔁 Repost of' }} <a href="{{ note.reacts_to }}">{{ note.reacts_to }}</a></p>
|
||||
{% endif %}
|
||||
<label for="content">{% if note.reacts_to %}Comment (optional, Markdown){% else %}Content (Markdown){% endif %}</label>
|
||||
<textarea
|
||||
id="content"
|
||||
name="content"
|
||||
rows="20"
|
||||
required
|
||||
rows="{{ 6 if note.reacts_to else 20 }}"
|
||||
{% if not note.reacts_to %}required{% endif %}
|
||||
autofocus
|
||||
>{{ note.content }}</textarea>
|
||||
<small>Use Markdown syntax for formatting</small>
|
||||
@@ -52,15 +55,58 @@
|
||||
<label for="published">Published</label>
|
||||
</div>
|
||||
|
||||
{% if syndication_targets %}
|
||||
<input type="hidden" name="syndication_form" value="1">
|
||||
<fieldset class="form-group">
|
||||
<legend>Crosspost</legend>
|
||||
{% for target in syndication_targets %}
|
||||
<div class="form-checkbox">
|
||||
<input type="checkbox" id="syndicate-{{ target.id }}" name="syndicate_to" value="{{ target.id }}"{% if target.id in syndication_enabled %} checked{% endif %}>
|
||||
<label for="syndicate-{{ target.id }}">{{ target.name }}</label>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="button button-primary">Update Note</button>
|
||||
<a href="{{ url_for('admin.dashboard') }}" class="button button-secondary">Cancel</a>
|
||||
<form action="{{ url_for('admin.delete_note_submit', note_id=note.id) }}" method="POST" class="delete-form" style="display: inline;" onsubmit="return confirm('Are you sure you want to delete this note? This cannot be undone.');">
|
||||
<input type="hidden" name="confirm" value="yes">
|
||||
<button type="submit" class="button button-danger">Delete Note</button>
|
||||
</form>
|
||||
<button type="submit" form="delete-note-form" class="button button-danger">Delete Note</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{# Outside the edit form: nested forms are not allowed in HTML #}
|
||||
<form id="delete-note-form" action="{{ url_for('admin.delete_note_submit', note_id=note.id) }}" method="POST" class="delete-form" onsubmit="return confirm('Are you sure you want to delete this note? This cannot be undone.');">
|
||||
<input type="hidden" name="confirm" value="yes">
|
||||
</form>
|
||||
|
||||
{% if syndications %}
|
||||
<section class="syndication-status">
|
||||
<h3>Crossposts</h3>
|
||||
<ul>
|
||||
{% for s in syndications %}
|
||||
<li>
|
||||
<strong>{{ s.name }}</strong>:
|
||||
{% if s.state == 'synced' and s.remote_url %}
|
||||
<a href="{{ s.remote_url }}" rel="noopener">posted</a>
|
||||
{% elif s.state == 'synced' %}
|
||||
removed
|
||||
{% elif s.state == 'pending' %}
|
||||
pending{% if s.attempts %} (retry {{ s.attempts }}){% endif %}
|
||||
{% else %}
|
||||
failed
|
||||
{% endif %}
|
||||
{% if s.last_error %}<br><small>{{ s.last_error }}</small>{% endif %}
|
||||
{% if s.state != 'synced' %}
|
||||
<form action="{{ url_for('admin.retry_syndication', syndication_id=s.id) }}" method="POST" style="display: inline;">
|
||||
<button type="submit" class="button button-secondary">Retry now</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</section>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
@@ -65,6 +65,19 @@
|
||||
<label for="published">Publish immediately</label>
|
||||
</div>
|
||||
|
||||
{% if syndication_targets %}
|
||||
<input type="hidden" name="syndication_form" value="1">
|
||||
<fieldset class="form-group">
|
||||
<legend>Crosspost</legend>
|
||||
{% for target in syndication_targets %}
|
||||
<div class="form-checkbox">
|
||||
<input type="checkbox" id="syndicate-{{ target.id }}" name="syndicate_to" value="{{ target.id }}" checked>
|
||||
<label for="syndicate-{{ target.id }}">{{ target.name }}</label>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="button button-primary">Create Note</button>
|
||||
<a href="{{ url_for('admin.dashboard') }}" class="button button-secondary">Cancel</a>
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<p>Micropub apps you have approved. Revoke any you no longer use.</p>
|
||||
|
||||
{% if tokens %}
|
||||
<div class="table-scroll">
|
||||
<table class="note-table">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -37,6 +38,7 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<p>No apps connected.</p>
|
||||
{% endif %}
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="table-scroll">
|
||||
<table class="webmention-table">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -85,6 +86,7 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{# Pagination #}
|
||||
|
||||
+23
-28
@@ -3,18 +3,19 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{% block title %}StarPunk{% endblock %}</title>
|
||||
<title>{% block title %}{{ config.SITE_NAME }}{% endblock %}</title>
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
||||
<link rel="alternate" type="application/rss+xml" title="{{ config.SITE_NAME }} RSS Feed" href="{{ url_for('public.feed', _external=True) }}">
|
||||
<link rel="alternate" type="application/xml+opml" title="{{ config.SITE_NAME }} Feed Subscription List" href="{{ url_for('public.opml', _external=True) }}">
|
||||
<link rel="alternate" type="application/rss+xml" title="{{ config.SITE_NAME }} RSS Feed" href="{{ absolute_url_for('public.feed') }}">
|
||||
<link rel="alternate" type="application/xml+opml" title="{{ config.SITE_NAME }} Feed Subscription List" href="{{ absolute_url_for('public.opml') }}">
|
||||
|
||||
{# Webmention endpoint (v1.6.0) #}
|
||||
{% if config.WEBMENTION_ENABLED %}
|
||||
<link rel="webmention" href="/webmention">
|
||||
<link rel="micropub" href="{{ config.SITE_URL.rstrip('/') }}{{ url_for('micropub.micropub_endpoint') }}">
|
||||
<link rel="indieauth-metadata" href="{{ config.SITE_URL.rstrip('/') }}{{ url_for('indieauth.metadata') }}">
|
||||
<link rel="authorization_endpoint" href="{{ config.SITE_URL.rstrip('/') }}{{ url_for('indieauth.authorize') }}">
|
||||
<link rel="token_endpoint" href="{{ config.SITE_URL.rstrip('/') }}{{ url_for('indieauth.token') }}">
|
||||
<link rel="micropub" href="{{ absolute_url_for('micropub.micropub_endpoint') }}">
|
||||
<link rel="indieauth-metadata" href="{{ absolute_url_for('indieauth.metadata') }}">
|
||||
<link rel="authorization_endpoint" href="{{ absolute_url_for('indieauth.authorize') }}">
|
||||
<link rel="token_endpoint" href="{{ absolute_url_for('indieauth.token') }}">
|
||||
{% endif %}
|
||||
|
||||
{# rel-me links from discovered author profile (v1.2.0 Phase 2) #}
|
||||
@@ -26,36 +27,28 @@
|
||||
|
||||
{% block head %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<body{% block body_class %}{% endblock %}>
|
||||
{% if config.DEV_MODE %}
|
||||
<div class="dev-mode-warning">
|
||||
WARNING: DEVELOPMENT MODE - Authentication bypassed
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<header>
|
||||
<h1><a href="/">StarPunk</a></h1>
|
||||
<nav>
|
||||
<header class="site-header">
|
||||
<a class="site-title" href="/">{{ config.SITE_NAME }}</a>
|
||||
<nav class="site-nav">
|
||||
<a href="/">Home</a>
|
||||
<a href="{{ url_for('public.likes') }}">Likes</a>
|
||||
<a href="{{ url_for('public.feed') }}">RSS</a>
|
||||
{% if g.me %}
|
||||
<a href="{{ url_for('admin.dashboard') }}">Dashboard</a>
|
||||
<a href="{{ url_for('admin.dashboard') }}">Admin</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('auth.login_form') }}">Login</a>
|
||||
<a href="{{ url_for('auth.login_form') }}">Log in</a>
|
||||
{% endif %}
|
||||
<form action="/search" method="get" role="search" style="margin-left: auto; display: flex; gap: var(--spacing-sm);">
|
||||
<input
|
||||
type="search"
|
||||
name="q"
|
||||
placeholder="Search..."
|
||||
aria-label="Search"
|
||||
value="{{ request.args.get('q', '') }}"
|
||||
minlength="2"
|
||||
maxlength="100"
|
||||
required
|
||||
style="width: 200px; padding: var(--spacing-xs) var(--spacing-sm);"
|
||||
>
|
||||
<button type="submit" class="button button-small" style="padding: var(--spacing-xs) var(--spacing-sm);">🔍</button>
|
||||
<form class="site-search" action="/search" method="get" role="search">
|
||||
<input type="search" name="q" placeholder="Search" aria-label="Search"
|
||||
value="{{ request.args.get('q', '') }}" minlength="2" maxlength="100" required>
|
||||
<button type="submit" aria-label="Search">Search</button>
|
||||
</form>
|
||||
</nav>
|
||||
</header>
|
||||
@@ -72,8 +65,10 @@
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>StarPunk v{{ config.get('VERSION', '0.5.0') }}</p>
|
||||
<footer class="site-footer">
|
||||
<p>
|
||||
Powered by <a href="https://git.thesatelliteoflove.com/phil/StarPunk">StarPunk</a> v{{ config.get('VERSION', '0.5.0') }}
|
||||
</p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
+5
-48
@@ -1,15 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}StarPunk - Home{% endblock %}
|
||||
{% block title %}{{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
{# h-feed required properties per microformats.org/wiki/h-feed #}
|
||||
<h2 class="p-name">{{ config.SITE_NAME or 'Recent Notes' }}</h2>
|
||||
<h1 class="p-name visually-hidden">{{ config.SITE_NAME or 'Recent Notes' }}</h1>
|
||||
|
||||
{# u-url for feed (self-reference) #}
|
||||
<a class="u-url" href="{{ url_for('public.index', _external=True) }}" style="display: none;">Feed URL</a>
|
||||
<a class="u-url" href="{{ absolute_url_for('public.index') }}" style="display: none;">Feed URL</a>
|
||||
|
||||
{# Feed-level author h-card with all properties #}
|
||||
{# Hidden because it's semantic-only markup for parsers, not visual content #}
|
||||
@@ -34,50 +34,7 @@
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Detect if note has explicit title (starts with # heading) - per Q22 #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
<h3 class="p-name">{{ note.title }}</h3>
|
||||
{% endif %}
|
||||
|
||||
{# Media preview (if available) #}
|
||||
{{ display_media(note.media) }}
|
||||
|
||||
{# e-content: note content (preview) #}
|
||||
<div class="e-content">
|
||||
{{ note.html[:300]|safe }}{% if note.html|length > 300 %}...{% endif %}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url for permalink #}
|
||||
<a class="u-url" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# Tags in preview #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>
|
||||
{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card nested in each h-entry (per Q20) #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">No notes published yet. Check back soon!</p>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}Likes · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
<h1 class="p-name page-title">Likes</h1>
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">Nothing liked yet.</p>
|
||||
{% endif %}
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
</div>
|
||||
{% endblock %}
|
||||
+28
-41
@@ -1,7 +1,8 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/reaction.html" import reaction %}
|
||||
|
||||
{% block title %}{{ note.slug }} - StarPunk{% endblock %}
|
||||
{% block title %}{% if note.content.strip().startswith('#') %}{{ note.title }}{% elif note.excerpt.strip() %}{{ note.excerpt|truncate(60) }}{% elif note.repost_of %}Repost{% elif note.like_of %}Like{% elif note.in_reply_to %}Reply{% else %}Photo{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article class="h-entry">
|
||||
@@ -17,13 +18,14 @@
|
||||
{% if note.in_reply_to %}
|
||||
<div class="reply-context">
|
||||
<span class="reply-label">In reply to:</span>
|
||||
<blockquote class="h-cite">
|
||||
{# u-in-reply-to on the h-cite itself makes it the entry's property (Post Type Discovery) #}
|
||||
<blockquote class="u-in-reply-to h-cite">
|
||||
{% if reply_context and reply_context.fetch_status == 'fetched' %}
|
||||
{% if reply_context.content_snippet %}
|
||||
<p class="p-content">{{ reply_context.content_snippet }}</p>
|
||||
{% endif %}
|
||||
<cite>
|
||||
<a class="u-in-reply-to" href="{{ note.in_reply_to }}">
|
||||
<a class="u-url" href="{{ note.in_reply_to }}">
|
||||
{% if reply_context.author_name %}{{ reply_context.author_name }}{% endif %}
|
||||
{% if reply_context.site_name %} on {{ reply_context.site_name }}{% endif %}
|
||||
{% if not reply_context.author_name and not reply_context.site_name %}{{ note.in_reply_to }}{% endif %}
|
||||
@@ -31,67 +33,52 @@
|
||||
</cite>
|
||||
{% else %}
|
||||
<cite>
|
||||
<a class="u-in-reply-to" href="{{ note.in_reply_to }}">{{ note.in_reply_to }}</a>
|
||||
<a class="u-url" href="{{ note.in_reply_to }}">{{ note.in_reply_to }}</a>
|
||||
</cite>
|
||||
{% endif %}
|
||||
</blockquote>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{{ reaction(note) }}
|
||||
|
||||
{# u-photo placement: Per draft spec, u-photo must be direct child of h-entry, #}
|
||||
{# NOT inside e-content. Media is rendered ABOVE e-content to meet this requirement. #}
|
||||
{{ display_media(note.media) }}
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
|
||||
{# e-content: note content BELOW media (per ADR-057) #}
|
||||
<div class="e-content">
|
||||
{{ note.html|safe }}
|
||||
{{ note.body_html|safe }}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url and u-uid same for notes (per Q23) #}
|
||||
<a class="u-url u-uid" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y at %I:%M %p') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# dt-updated if note was modified #}
|
||||
<a class="u-url u-uid" href="{{ absolute_url_for('public.note', slug=note.slug) }}"><time class="dt-published" datetime="{{ note.created_at.isoformat() }}">{{ note.created_at.strftime('%B %-d, %Y · %-I:%M %p') }}</time></a>
|
||||
{% if note.updated_at and note.updated_at != note.created_at %}
|
||||
<span class="updated">
|
||||
(Updated: <time class="dt-updated" datetime="{{ note.updated_at.isoformat() }}">{{ note.updated_at.strftime('%B %d, %Y') }}</time>)
|
||||
<span class="updated">Updated <time class="dt-updated" datetime="{{ note.updated_at.isoformat() }}">{{ note.updated_at.strftime('%b %-d, %Y') }}</time></span>
|
||||
{% endif %}
|
||||
{# rel="tag" per microformats2 p-category #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Tags / Categories #}
|
||||
{# rel="tag" per microformats2 p-category specification #}
|
||||
{% if note.tags %}
|
||||
<div class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">
|
||||
{{ tag.display_name }}
|
||||
</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% set synced = (syndications or [])|selectattr('remote_url')|list %}
|
||||
{% if synced %}
|
||||
<span class="syndication">Also on {% for s in synced %}<a class="u-syndication" href="{{ s.remote_url }}" rel="syndication">{{ s.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card (nested within h-entry per Q20) #}
|
||||
{# Author h-card (nested within h-entry per Q20); hidden on a single-author site #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
{% if author.photo %}
|
||||
<img class="u-photo" src="{{ author.photo }}" alt="{{ author.name or 'Author' }}" width="48" height="48">
|
||||
{% endif %}
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
{% if author.note %}
|
||||
<span class="p-note" style="display: none;">{{ author.note }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
<span class="p-author h-card visually-hidden">
|
||||
{% if author.photo %}<img class="u-photo" src="{{ author.photo }}" alt="">{% endif %}
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a>
|
||||
{% if author.note %}<span class="p-note">{{ author.note }}</span>{% endif %}
|
||||
</span>
|
||||
{% endif %}
|
||||
</footer>
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">Back to all notes</a>
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
|
||||
{# Webmentions section (v1.6.0 Phase 5) - only shown if approved webmentions exist #}
|
||||
@@ -198,7 +185,7 @@
|
||||
<div class="webmention webmention-mention">
|
||||
<a href="{{ mention.source_url }}">{{ mention.author_name or mention.source_url }}</a>
|
||||
{% if mention.content_snippet %}
|
||||
<p>{{ mention.content_snippet|truncate(200) }}</p>
|
||||
<p>{{ mention.content_snippet|striptags|truncate(200) }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
{# Reusable media display macro #}
|
||||
{% macro display_media(media_items) %}
|
||||
{# fallback_alt: for photos without a caption, usually the post's text (v2.6.5) #}
|
||||
{% macro display_media(media_items, fallback_alt='') %}
|
||||
{% if media_items %}
|
||||
<div class="note-media">
|
||||
{% for item in media_items %}
|
||||
<figure class="media-item">
|
||||
<img src="{{ url_for('public.media_file', path=item.path) }}"
|
||||
alt="{{ item.caption or 'Image' }}"
|
||||
alt="{{ item.caption or fallback_alt or 'Image' }}"
|
||||
class="u-photo"
|
||||
loading="lazy">
|
||||
{# No figcaption - caption is for alt text only #}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
{# One note in a list (home, tag pages). v2.3.0 #}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/reaction.html" import reaction %}
|
||||
{% macro note_preview(note, author) %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Notes starting with a # heading have a title (Q22) #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
{% if has_explicit_title %}
|
||||
<h2 class="p-name"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">{{ note.title }}</a></h2>
|
||||
{% endif %}
|
||||
|
||||
{% if note.in_reply_to %}
|
||||
<p class="reaction reply-to"><span class="reaction-verb">↩ In reply to</span> <a class="u-in-reply-to" href="{{ note.in_reply_to }}">{{ note.in_reply_to }}</a></p>
|
||||
{% endif %}
|
||||
|
||||
{{ reaction(note) }}
|
||||
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
|
||||
{% if has_explicit_title %}
|
||||
{# Titled notes are articles: show a summary and link through #}
|
||||
<div class="p-summary">
|
||||
<p>{{ note.excerpt }}</p>
|
||||
</div>
|
||||
<p class="read-more"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">Read more →</a></p>
|
||||
{% else %}
|
||||
{# Always present, even empty: without it a text-free photo post gets an implied name (mf2) #}
|
||||
<div class="e-content">
|
||||
{{ note.html|safe }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<footer class="note-meta">
|
||||
<a class="u-url" href="{{ absolute_url_for('public.note', slug=note.slug) }}"><time class="dt-published" datetime="{{ note.created_at.isoformat() }}">{{ note.created_at.strftime('%b %-d, %Y') }}</time></a>
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
{% if author %}
|
||||
{# Single-author site: keep the h-card for parsers, hide it from readers #}
|
||||
<span class="p-author h-card visually-hidden"><a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a></span>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{% endmacro %}
|
||||
@@ -0,0 +1,12 @@
|
||||
{# "Liked" / "Reposted" line for likes and reposts (v2.5.0) #}
|
||||
{% macro reaction(note) %}
|
||||
{% if note.reacts_to %}
|
||||
{% set ctx = note.context if note.context is defined else None %}
|
||||
{% if ctx and ctx.title %}{% set label = ctx.title %}{% elif ctx and ctx.content_snippet %}{% set label = ctx.content_snippet|striptags|truncate(90) %}{% elif ctx and (ctx.author_name or ctx.site_name) %}{% set label = 'a post' %}{% else %}{% set label = note.reacts_to %}{% endif %}
|
||||
<p class="reaction reaction-{{ note.kind }}">
|
||||
<span class="reaction-verb">{% if note.kind == 'like' %}★ Liked{% else %}🔁 Reposted{% endif %}</span>
|
||||
<a class="{{ 'u-like-of' if note.kind == 'like' else 'u-repost-of' }}" href="{{ note.reacts_to }}">{{ label }}</a>
|
||||
{% if ctx and (ctx.author_name or ctx.site_name) %}<span class="reaction-by">by {{ ctx.author_name or ctx.site_name }}</span>{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{% if query %}Search: {{ query }}{% else %}Search{% endif %} - StarPunk{% endblock %}
|
||||
{% block title %}{% if query %}Search: {{ query }}{% else %}Search{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="search-container">
|
||||
|
||||
+5
-48
@@ -1,65 +1,22 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}{{ tag.display_name }} - StarPunk{% endblock %}
|
||||
{% block title %}{{ tag.display_name }} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
<h1 class="p-name">Notes tagged "{{ tag.display_name }}"</h1>
|
||||
<h1 class="p-name page-title">Notes tagged <span class="tag-name">{{ tag.display_name }}</span></h1>
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Detect if note has explicit title (starts with # heading) - per Q22 #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
<h3 class="p-name">{{ note.title }}</h3>
|
||||
{% endif %}
|
||||
|
||||
{# Media preview (if available) #}
|
||||
{{ display_media(note.media) }}
|
||||
|
||||
{# e-content: note content (preview) #}
|
||||
<div class="e-content">
|
||||
{{ note.html[:300]|safe }}{% if note.html|length > 300 %}...{% endif %}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url for permalink #}
|
||||
<a class="u-url" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# Tags in preview #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>
|
||||
{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card nested in each h-entry (per Q20) #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">No notes with this tag.</p>
|
||||
{% endif %}
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">Back to all notes</a>
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Absolute URLs come from SITE_URL, not the request (v2.0.1)
|
||||
|
||||
Behind a reverse proxy the app is reached over plain HTTP, so URLs built
|
||||
from the request said http:// on an https site.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from starpunk import create_app
|
||||
from starpunk.notes import create_note
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
data = tmp_path / "data"
|
||||
return create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_pages_use_site_url_behind_http_proxy(app):
|
||||
with app.app_context():
|
||||
note = create_note(content="Hello over the proxy", published=True, tags=["proxy"])
|
||||
client = app.test_client()
|
||||
|
||||
pages = ["/", f"/note/{note.slug}", "/tag/proxy"]
|
||||
for path in pages:
|
||||
# The test client makes plain http://localhost requests, like Caddy does
|
||||
response = client.get(path)
|
||||
assert response.status_code == 200, path
|
||||
html = response.data.decode()
|
||||
assert "http://localhost" not in html, path
|
||||
|
||||
home = client.get("/").data.decode()
|
||||
assert 'href="https://blog.example.com/feed"' in home
|
||||
assert 'href="https://blog.example.com/opml.xml"' in home
|
||||
|
||||
note_html = client.get(f"/note/{note.slug}").data.decode()
|
||||
assert f'href="https://blog.example.com/note/{note.slug}"' in note_html
|
||||
@@ -0,0 +1,223 @@
|
||||
"""
|
||||
Likes and reposts (v2.5.0, ADR-068)
|
||||
|
||||
Fake GoToSocial and Bluesky servers check that likes/reposts of their own
|
||||
posts become native favourites/boosts/likes/reposts, and everything else
|
||||
falls back (reposts) or is skipped (likes).
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from starpunk import create_app, syndication
|
||||
from starpunk.indieauth import create_access_token
|
||||
from starpunk.notes import delete_note, get_note
|
||||
from starpunk.syndication import BlueskyClient, MastodonClient, get_note_syndications, process_due
|
||||
|
||||
SITE = "https://blog.example.com"
|
||||
GTS = "https://social.example.com"
|
||||
FEDI_POST = "https://mastodon.example/@alice/111"
|
||||
BSKY_POST = "https://bsky.app/profile/alice.example/post/3abc"
|
||||
WEB_PAGE = "https://someblog.example/2026/a-post"
|
||||
|
||||
|
||||
class FakeGTS:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
self.statuses = {}
|
||||
|
||||
def __call__(self, request):
|
||||
path = request.url.path
|
||||
self.calls.append((request.method, path))
|
||||
if path == "/api/v2/search":
|
||||
q = request.url.params["q"]
|
||||
return httpx.Response(200, json={"statuses": [{"id": "s1"}] if q == FEDI_POST else []})
|
||||
if path in ("/api/v1/statuses/s1/favourite", "/api/v1/statuses/s1/unfavourite",
|
||||
"/api/v1/statuses/s1/reblog", "/api/v1/statuses/s1/unreblog"):
|
||||
return httpx.Response(200, json={"id": "s1"})
|
||||
if path == "/api/v1/statuses" and request.method == "POST":
|
||||
sid = f"p{len(self.statuses) + 1}"
|
||||
self.statuses[sid] = json.loads(request.content)
|
||||
return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"})
|
||||
if path.startswith("/api/v1/statuses/") and request.method == "DELETE":
|
||||
self.statuses.pop(path.rsplit("/", 1)[1], None)
|
||||
return httpx.Response(200, json={})
|
||||
return httpx.Response(500)
|
||||
|
||||
|
||||
class FakePDS:
|
||||
def __init__(self):
|
||||
self.records = {}
|
||||
self.calls = []
|
||||
|
||||
def __call__(self, request):
|
||||
nsid = request.url.path.removeprefix("/xrpc/")
|
||||
self.calls.append(nsid)
|
||||
if nsid == "com.atproto.server.createSession":
|
||||
return httpx.Response(200, json={"did": "did:plc:me", "handle": "me.example",
|
||||
"accessJwt": "a", "refreshJwt": "r"})
|
||||
if nsid == "com.atproto.identity.resolveHandle":
|
||||
return httpx.Response(200, json={"did": "did:plc:alice"})
|
||||
if nsid == "app.bsky.feed.getPosts":
|
||||
return httpx.Response(200, json={"posts": [{"uri": request.url.params["uris"], "cid": "cid1"}]})
|
||||
if nsid == "com.atproto.repo.createRecord":
|
||||
body = json.loads(request.content)
|
||||
uri = f"at://did:plc:me/{body['collection']}/r{len(self.records) + 1}"
|
||||
self.records[uri] = body["record"]
|
||||
return httpx.Response(200, json={"uri": uri, "cid": "c"})
|
||||
if nsid == "com.atproto.repo.deleteRecord":
|
||||
body = json.loads(request.content)
|
||||
self.records.pop(f"at://{body['repo']}/{body['collection']}/{body['rkey']}", None)
|
||||
return httpx.Response(200, json={})
|
||||
return httpx.Response(500, json={"error": nsid})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gts():
|
||||
return FakeGTS()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pds():
|
||||
return FakePDS()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path, gts, pds):
|
||||
data = tmp_path / "data"
|
||||
app = create_app({
|
||||
"TESTING": True, "SITE_URL": SITE, "ADMIN_ME": "https://example.com", "SESSION_SECRET": "x",
|
||||
"DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "starpunk.db",
|
||||
"GOTOSOCIAL_URL": GTS, "GOTOSOCIAL_ACCESS_TOKEN": "t",
|
||||
"BLUESKY_HANDLE": "me.example", "BLUESKY_APP_PASSWORD": "p", "BLUESKY_SERVICE": "https://pds.example",
|
||||
"FEED_CACHE_ENABLED": False,
|
||||
})
|
||||
# The feed's note list is cached at module level across apps
|
||||
from starpunk.routes import public
|
||||
public._feed_cache["notes"] = None
|
||||
public._feed_cache["timestamp"] = None
|
||||
syndication._client_cache[GTS] = MastodonClient(GTS, "t", http=httpx.Client(transport=httpx.MockTransport(gts)))
|
||||
syndication._client_cache["bluesky:https://pds.example:me.example"] = BlueskyClient(
|
||||
"https://pds.example", "me.example", "p", http=httpx.Client(transport=httpx.MockTransport(pds)))
|
||||
yield app
|
||||
syndication._client_cache.clear()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def post(app, monkeypatch):
|
||||
# Don't fetch contexts from the network
|
||||
monkeypatch.setattr("starpunk.reply_context.fetch_reply_context", lambda *a, **k: None)
|
||||
with app.test_request_context():
|
||||
token = create_access_token("https://quill.example/", "create update delete")["access_token"]
|
||||
client = app.test_client()
|
||||
|
||||
def _post(**props):
|
||||
response = client.post("/micropub", json={"type": ["h-entry"], "properties": props},
|
||||
headers={"Authorization": f"Bearer {token}"})
|
||||
assert response.status_code == 201, response.get_json()
|
||||
location = response.headers["Location"]
|
||||
with app.app_context():
|
||||
note = get_note(slug=location.rsplit("/", 1)[1])
|
||||
process_due(limit=10)
|
||||
rows = {r["target"]: r for r in get_note_syndications(note.id)}
|
||||
return note, rows
|
||||
return _post
|
||||
|
||||
|
||||
def page(app, path):
|
||||
return app.test_client().get(path).data.decode()
|
||||
|
||||
|
||||
class TestSite:
|
||||
def test_like_without_comment(self, app, post):
|
||||
note, _ = post(**{"like-of": [WEB_PAGE]})
|
||||
assert note.kind == "like" and note.content == ""
|
||||
assert note.slug not in page(app, "/")
|
||||
likes = page(app, "/likes")
|
||||
assert f'class="u-like-of" href="{WEB_PAGE}"' in likes and "★ Liked" in likes
|
||||
assert note.slug not in page(app, "/feed.json")
|
||||
|
||||
def test_repost_in_stream_and_feed(self, app, post):
|
||||
note, _ = post(**{"repost-of": [WEB_PAGE], "content": ["Worth a read"]})
|
||||
home = page(app, "/")
|
||||
assert f'class="u-repost-of" href="{WEB_PAGE}"' in home and "Worth a read" in home
|
||||
feed = json.loads(page(app, "/feed.json"))
|
||||
item = next(i for i in feed["items"] if note.slug in i["url"])
|
||||
assert "Reposted" in item["content_html"] and WEB_PAGE in item["content_html"]
|
||||
|
||||
def test_webmention_goes_to_target_with_clean_url(self, app, post, monkeypatch):
|
||||
sent = {}
|
||||
monkeypatch.setattr("starpunk.webmention_sender.queue_webmentions_for_note",
|
||||
lambda **kw: sent.update(kw) or 0)
|
||||
app.config["WEBMENTION_ENABLED"] = True
|
||||
note, _ = post(**{"like-of": [WEB_PAGE]})
|
||||
assert sent["in_reply_to"] == WEB_PAGE
|
||||
assert sent["note_url"] == f"{SITE}/note/{note.slug}" # no double slash
|
||||
|
||||
def test_both_kinds_rejected(self, app, post):
|
||||
with pytest.raises(AssertionError):
|
||||
post(**{"like-of": [WEB_PAGE], "repost-of": [WEB_PAGE]})
|
||||
|
||||
|
||||
class TestGoToSocial:
|
||||
def test_like_of_fediverse_post_favourites(self, app, post, gts):
|
||||
note, rows = post(**{"like-of": [FEDI_POST]})
|
||||
assert rows["gotosocial"]["remote_id"] == "favourite:s1"
|
||||
assert rows["gotosocial"]["remote_url"] is None
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id)
|
||||
process_due(limit=10)
|
||||
assert ("POST", "/api/v1/statuses/s1/unfavourite") in gts.calls
|
||||
|
||||
def test_like_of_web_page_is_not_crossposted(self, app, post, gts):
|
||||
_, rows = post(**{"like-of": [WEB_PAGE]})
|
||||
assert rows["gotosocial"]["state"] == "synced" and rows["gotosocial"]["remote_id"] is None
|
||||
assert gts.statuses == {}
|
||||
|
||||
def test_repost_boosts(self, app, post, gts):
|
||||
_, rows = post(**{"repost-of": [FEDI_POST]})
|
||||
assert rows["gotosocial"]["remote_id"] == "reblog:s1"
|
||||
|
||||
def test_repost_with_comment_posts_status(self, app, post, gts):
|
||||
_, rows = post(**{"repost-of": [FEDI_POST], "content": ["So true"]})
|
||||
(status,) = gts.statuses.values()
|
||||
assert status["status"].startswith("So true") and FEDI_POST in status["status"]
|
||||
assert ("POST", "/api/v1/statuses/s1/reblog") not in gts.calls
|
||||
|
||||
def test_repost_of_web_page_posts_link(self, app, post, gts):
|
||||
post(**{"repost-of": [WEB_PAGE]})
|
||||
(status,) = gts.statuses.values()
|
||||
assert status["status"].startswith("🔁") and WEB_PAGE in status["status"]
|
||||
|
||||
|
||||
class TestBluesky:
|
||||
def test_like_of_bluesky_post(self, app, post, pds):
|
||||
note, rows = post(**{"like-of": [BSKY_POST]})
|
||||
uri = rows["bluesky"]["remote_id"]
|
||||
assert "/app.bsky.feed.like/" in uri
|
||||
assert pds.records[uri]["subject"] == {"uri": "at://did:plc:alice/app.bsky.feed.post/3abc", "cid": "cid1"}
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id)
|
||||
process_due(limit=10)
|
||||
assert uri not in pds.records
|
||||
|
||||
def test_like_of_web_page_skipped(self, app, post, pds):
|
||||
_, rows = post(**{"like-of": [WEB_PAGE]})
|
||||
assert rows["bluesky"]["remote_id"] is None and pds.records == {}
|
||||
|
||||
def test_repost_and_quote(self, app, post, pds):
|
||||
_, rows = post(**{"repost-of": [BSKY_POST]})
|
||||
assert "/app.bsky.feed.repost/" in rows["bluesky"]["remote_id"]
|
||||
_, rows = post(**{"repost-of": [BSKY_POST], "content": ["Great thread"]})
|
||||
record = pds.records[rows["bluesky"]["remote_id"]]
|
||||
assert record["text"] == "Great thread"
|
||||
assert record["embed"] == {"$type": "app.bsky.embed.record",
|
||||
"record": {"uri": "at://did:plc:alice/app.bsky.feed.post/3abc", "cid": "cid1"}}
|
||||
|
||||
def test_repost_of_web_page_posts_link_card(self, app, post, pds):
|
||||
_, rows = post(**{"repost-of": [WEB_PAGE]})
|
||||
record = pds.records[rows["bluesky"]["remote_id"]]
|
||||
assert record["embed"]["external"]["uri"] == WEB_PAGE
|
||||
assert record["text"].startswith("🔁")
|
||||
+77
-29
@@ -167,6 +167,34 @@ def test_micropub_create_with_name(client, app, mock_valid_token):
|
||||
assert response.status_code == 201
|
||||
assert 'Location' in response.headers
|
||||
|
||||
# StarPunk titles are a '# Heading' first line, so name must land there
|
||||
slug = response.headers['Location'].rstrip('/').split('/')[-1]
|
||||
with app.app_context():
|
||||
note = get_note(slug)
|
||||
assert note.content == '# My Test Title\n\nContent goes here'
|
||||
assert note.title == 'My Test Title'
|
||||
|
||||
|
||||
def test_micropub_create_with_name_already_heading(client, app, mock_valid_token):
|
||||
"""A name that matches the content's heading is not added twice"""
|
||||
with patch('starpunk.routes.micropub.verify_access_token', mock_valid_token):
|
||||
response = client.post(
|
||||
'/micropub',
|
||||
json={
|
||||
'type': ['h-entry'],
|
||||
'properties': {
|
||||
'name': ['Same Title'],
|
||||
'content': ['# Same Title\n\nBody']
|
||||
}
|
||||
},
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
slug = response.headers['Location'].rstrip('/').split('/')[-1]
|
||||
with app.app_context():
|
||||
assert get_note(slug).content == '# Same Title\n\nBody'
|
||||
|
||||
|
||||
def test_micropub_create_with_categories(client, app, mock_valid_token):
|
||||
"""Test creating a note with tags (category property)"""
|
||||
@@ -206,7 +234,7 @@ def test_micropub_create_with_custom_slug_form(client, app, mock_valid_token):
|
||||
|
||||
# Verify the custom slug was used
|
||||
location = response.headers['Location']
|
||||
assert location.endswith('/notes/my-custom-slug')
|
||||
assert location.endswith('/note/my-custom-slug')
|
||||
|
||||
# Verify note exists with the custom slug
|
||||
with app.app_context():
|
||||
@@ -236,7 +264,7 @@ def test_micropub_create_with_custom_slug_json(client, app, mock_valid_token):
|
||||
|
||||
# Verify the custom slug was used
|
||||
location = response.headers['Location']
|
||||
assert location.endswith('/notes/json-custom-slug')
|
||||
assert location.endswith('/note/json-custom-slug')
|
||||
|
||||
# Verify note exists with the custom slug
|
||||
with app.app_context():
|
||||
@@ -290,6 +318,38 @@ def test_micropub_query_source(client, mock_valid_token):
|
||||
data = response.get_json()
|
||||
assert 'properties' in data
|
||||
assert data['properties']['content'][0] == 'Source test'
|
||||
# url must be the real permalink (was /notes/<slug>, a 404)
|
||||
assert data['properties']['url'] == [location]
|
||||
# A note without a heading has no name
|
||||
assert 'name' not in data['properties']
|
||||
|
||||
|
||||
def test_micropub_query_source_with_name(client, mock_valid_token):
|
||||
"""q=source returns name and content the way they were posted"""
|
||||
with patch('starpunk.routes.micropub.verify_access_token', mock_valid_token):
|
||||
create_response = client.post(
|
||||
'/micropub',
|
||||
json={
|
||||
'type': ['h-entry'],
|
||||
'properties': {
|
||||
'name': ['Round Trip'],
|
||||
'content': ['Body text']
|
||||
}
|
||||
},
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
location = create_response.headers['Location']
|
||||
|
||||
response = client.get(
|
||||
f'/micropub?q=source&url={location}',
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
properties = response.get_json()['properties']
|
||||
assert properties['name'] == ['Round Trip']
|
||||
assert properties['content'] == ['Body text']
|
||||
assert properties['url'] == [location]
|
||||
|
||||
|
||||
# Error Handling Tests
|
||||
@@ -312,33 +372,21 @@ def test_micropub_missing_content(client, mock_valid_token):
|
||||
assert data['error'] == 'invalid_request'
|
||||
|
||||
|
||||
def test_micropub_unsupported_action(client, mock_valid_token):
|
||||
"""Test unsupported actions (update, delete) return error"""
|
||||
def test_micropub_update_delete_need_scope(client, mock_valid_token):
|
||||
"""A create-only token can't update or delete (v2.4.0)"""
|
||||
with patch('starpunk.routes.micropub.verify_access_token', mock_valid_token):
|
||||
# Test update
|
||||
response = client.post(
|
||||
'/micropub',
|
||||
json={
|
||||
'action': 'update',
|
||||
'url': 'https://example.com/note/123'
|
||||
},
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
for action in ('update', 'delete', 'undelete'):
|
||||
response = client.post(
|
||||
'/micropub',
|
||||
json={'action': action, 'url': 'https://example.com/note/123', 'replace': {}},
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
assert response.status_code == 403, action
|
||||
assert response.get_json()['error'] == 'insufficient_scope'
|
||||
|
||||
|
||||
def test_micropub_unknown_action(client, mock_valid_token):
|
||||
with patch('starpunk.routes.micropub.verify_access_token', mock_valid_token):
|
||||
response = client.post('/micropub', json={'action': 'frobnicate'},
|
||||
headers={'Authorization': 'Bearer valid_token'})
|
||||
assert response.status_code == 400
|
||||
data = response.get_json()
|
||||
assert 'not supported' in data['error_description']
|
||||
|
||||
# Test delete
|
||||
response = client.post(
|
||||
'/micropub',
|
||||
json={
|
||||
'action': 'delete',
|
||||
'url': 'https://example.com/note/123'
|
||||
},
|
||||
headers={'Authorization': 'Bearer valid_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
data = response.get_json()
|
||||
assert 'not supported' in data['error_description']
|
||||
@@ -0,0 +1,206 @@
|
||||
"""
|
||||
Micropub round 1 (v2.4.0): update, delete/undelete, drafts, replies,
|
||||
multipart and remote photos, q=source lists and filters, q=category.
|
||||
|
||||
Uses real tokens from StarPunk's own IndieAuth server.
|
||||
"""
|
||||
|
||||
import io
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from starpunk import create_app
|
||||
from starpunk.indieauth import create_access_token
|
||||
from starpunk.notes import get_note
|
||||
|
||||
SITE = "https://blog.example.com"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
data = tmp_path / "data"
|
||||
return create_app({
|
||||
"TESTING": True, "SITE_URL": SITE, "ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
})
|
||||
|
||||
|
||||
def auth(app, scope="create update delete media"):
|
||||
with app.test_request_context():
|
||||
token = create_access_token("https://quill.example/", scope)["access_token"]
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def h(app):
|
||||
return auth(app)
|
||||
|
||||
|
||||
def jpeg(color="red"):
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (64, 48), color).save(buf, format="JPEG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def create(client, h, **props):
|
||||
body = {"type": ["h-entry"], "properties": {"content": ["Hello"], **props}}
|
||||
response = client.post("/micropub", json=body, headers=h)
|
||||
assert response.status_code == 201, response.get_json()
|
||||
return response.headers["Location"]
|
||||
|
||||
|
||||
def note_at(app, url):
|
||||
with app.app_context():
|
||||
return get_note(slug=url.rstrip("/").rsplit("/", 1)[1])
|
||||
|
||||
|
||||
def source(client, h, url, *props):
|
||||
query = f"/micropub?q=source&url={url}" + "".join(f"&properties[]={p}" for p in props)
|
||||
return client.get(query, headers=h).get_json()
|
||||
|
||||
|
||||
class TestCreate:
|
||||
def test_draft(self, app, client, h):
|
||||
url = create(client, h, **{"post-status": ["draft"]})
|
||||
assert note_at(app, url).published is False
|
||||
assert client.get(url.replace(SITE, "")).status_code == 404
|
||||
|
||||
def test_reply(self, app, client, h):
|
||||
url = create(client, h, **{"in-reply-to": ["https://other.example/post/1"]})
|
||||
assert note_at(app, url).in_reply_to == "https://other.example/post/1"
|
||||
|
||||
def test_multipart_photo(self, app, client, h):
|
||||
response = client.post(
|
||||
"/micropub",
|
||||
data={"h": "entry", "content": "With a file", "photo": (io.BytesIO(jpeg()), "pic.jpg")},
|
||||
headers=h, content_type="multipart/form-data",
|
||||
)
|
||||
assert response.status_code == 201
|
||||
assert "photo" in source(client, h, response.headers["Location"])["properties"]
|
||||
|
||||
def test_remote_photo_is_downloaded(self, app, client, h, monkeypatch):
|
||||
transport = httpx.MockTransport(lambda req: httpx.Response(200, content=jpeg("blue")))
|
||||
monkeypatch.setattr(httpx, "stream", lambda method, url, **kw: httpx.Client(transport=transport).stream(method, url))
|
||||
url = create(client, h, photo=[{"value": "https://photos.example/cat.jpg", "alt": "A cat"}])
|
||||
(photo,) = source(client, h, url)["properties"]["photo"]
|
||||
assert photo["value"].startswith(f"{SITE}/media/") and photo["alt"] == "A cat"
|
||||
|
||||
def test_only_h_entry(self, client, h):
|
||||
response = client.post("/micropub", data={"h": "event", "content": "x"}, headers=h)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
class TestUpdate:
|
||||
def update(self, client, h, url, **ops):
|
||||
return client.post("/micropub", json={"action": "update", "url": url, **ops}, headers=h)
|
||||
|
||||
def test_replace_content_keeps_title(self, app, client, h):
|
||||
url = create(client, h, name=["My Title"])
|
||||
assert self.update(client, h, url, replace={"content": ["New body"]}).status_code == 204
|
||||
props = source(client, h, url)["properties"]
|
||||
assert props["name"] == ["My Title"] and props["content"] == ["New body"]
|
||||
|
||||
def test_replace_and_delete_name(self, app, client, h):
|
||||
url = create(client, h)
|
||||
self.update(client, h, url, replace={"name": ["Added"]})
|
||||
assert note_at(app, url).content.startswith("# Added")
|
||||
self.update(client, h, url, delete=["name"])
|
||||
assert "name" not in source(client, h, url)["properties"]
|
||||
|
||||
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", "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"])
|
||||
assert "category" not in source(client, h, url)["properties"]
|
||||
|
||||
def test_post_status_and_reply(self, app, client, h):
|
||||
url = create(client, h)
|
||||
self.update(client, h, url, replace={"post-status": ["draft"], "in-reply-to": ["https://x.example/a"]})
|
||||
note = note_at(app, url)
|
||||
assert note.published is False and note.in_reply_to == "https://x.example/a"
|
||||
self.update(client, h, url, delete=["in-reply-to", "post-status"])
|
||||
note = note_at(app, url)
|
||||
assert note.published is True and not note.in_reply_to
|
||||
|
||||
def test_photos(self, app, client, h):
|
||||
uploaded = client.post("/micropub/media", data={"file": (io.BytesIO(jpeg()), "a.jpg")},
|
||||
headers=h, content_type="multipart/form-data").headers["Location"]
|
||||
url = create(client, h)
|
||||
self.update(client, h, url, add={"photo": [{"value": uploaded, "alt": "Red"}]})
|
||||
assert source(client, h, url)["properties"]["photo"] == [{"value": uploaded, "alt": "Red"}]
|
||||
self.update(client, h, url, delete={"photo": [uploaded]})
|
||||
assert "photo" not in source(client, h, url)["properties"]
|
||||
|
||||
def test_unsupported_property(self, client, h):
|
||||
url = create(client, h)
|
||||
response = self.update(client, h, url, replace={"published": ["2020-01-01T00:00:00Z"]})
|
||||
assert response.status_code == 400 and "published" in response.get_json()["error_description"]
|
||||
|
||||
def test_unknown_post(self, client, h):
|
||||
response = self.update(client, h, f"{SITE}/note/nope", replace={"content": ["x"]})
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_form_update_rejected(self, client, h):
|
||||
url = create(client, h)
|
||||
assert client.post("/micropub", data={"action": "update", "url": url}, headers=h).status_code == 400
|
||||
|
||||
|
||||
class TestDelete:
|
||||
def test_delete_and_undelete(self, app, client, h):
|
||||
url = create(client, h, category=["kept"])
|
||||
path = url.replace(SITE, "")
|
||||
assert client.post("/micropub", data={"action": "delete", "url": url}, headers=h).status_code == 204
|
||||
assert client.get(path).status_code == 410 # Gone (Webmention 3.1.5)
|
||||
response = client.post("/micropub", json={"action": "undelete", "url": url}, headers=h)
|
||||
assert response.status_code == 204
|
||||
assert client.get(path).status_code == 200
|
||||
assert source(client, h, url)["properties"]["category"] == ["kept"]
|
||||
|
||||
def test_undelete_needs_a_deleted_post(self, client, h):
|
||||
url = create(client, h)
|
||||
assert client.post("/micropub", json={"action": "undelete", "url": url}, headers=h).status_code == 404
|
||||
|
||||
def test_delete_scope(self, app, client):
|
||||
h = auth(app, "create")
|
||||
url = create(client, h)
|
||||
assert client.post("/micropub", data={"action": "delete", "url": url}, headers=h).status_code == 403
|
||||
|
||||
|
||||
class TestQueries:
|
||||
def test_source_properties_filter(self, client, h):
|
||||
url = create(client, h, name=["T"], category=["x"])
|
||||
data = source(client, h, url, "name", "category")
|
||||
assert data == {"properties": {"name": ["T"], "category": ["x"]}}
|
||||
|
||||
def test_source_list_includes_drafts(self, client, h):
|
||||
create(client, h, content=["first"])
|
||||
create(client, h, content=["second"], **{"post-status": ["draft"]})
|
||||
items = client.get("/micropub?q=source&limit=5", headers=h).get_json()["items"]
|
||||
assert {i["properties"]["post-status"][0] for i in items} == {"published", "draft"}
|
||||
|
||||
def test_category(self, client, h):
|
||||
create(client, h, category=["IndieWeb", "python"])
|
||||
create(client, h, category=["IndieWeb"])
|
||||
assert client.get("/micropub?q=category", headers=h).get_json()["categories"] == ["IndieWeb", "python"]
|
||||
assert client.get("/micropub?q=category&filter=py", headers=h).get_json()["categories"] == ["python"]
|
||||
|
||||
def test_config_lists_queries(self, client, h):
|
||||
config = client.get("/micropub?q=config", headers=h).get_json()
|
||||
assert "category" in config["q"] and "media-endpoint" in config
|
||||
|
||||
|
||||
def test_media_scope_alone_can_upload(app, client):
|
||||
response = client.post("/micropub/media", data={"file": (io.BytesIO(jpeg()), "a.jpg")},
|
||||
headers=auth(app, "media"), content_type="multipart/form-data")
|
||||
assert response.status_code == 201
|
||||
@@ -0,0 +1,105 @@
|
||||
"""
|
||||
Micropub media fixes (v2.6.5): photo posts without text, the same photo
|
||||
twice, and failed creates that left a post (and a database lock) behind.
|
||||
"""
|
||||
|
||||
import io
|
||||
|
||||
from starpunk.database.pool import ConnectionPool
|
||||
from starpunk.notes import list_notes
|
||||
from starpunk.syndication import compose_text
|
||||
from tests.test_micropub_v240 import SITE, app, client, h, jpeg, note_at, source # noqa: F401
|
||||
|
||||
|
||||
def upload(client, h, color="red"):
|
||||
response = client.post("/micropub/media", data={"file": (io.BytesIO(jpeg(color)), "a.jpg")},
|
||||
headers=h, content_type="multipart/form-data")
|
||||
assert response.status_code == 201
|
||||
return response.headers["Location"]
|
||||
|
||||
|
||||
def post(client, h, props):
|
||||
return client.post("/micropub", json={"type": ["h-entry"], "properties": props}, headers=h)
|
||||
|
||||
|
||||
def update(client, h, url, **ops):
|
||||
return client.post("/micropub", json={"action": "update", "url": url, **ops}, headers=h)
|
||||
|
||||
|
||||
class TestPhotoWithoutText:
|
||||
def test_json_photo_only(self, app, client, h):
|
||||
photo = upload(client, h)
|
||||
response = post(client, h, {"photo": [{"value": photo, "alt": "Red"}]})
|
||||
assert response.status_code == 201, response.get_json()
|
||||
url = response.headers["Location"]
|
||||
assert note_at(app, url).content == ""
|
||||
assert source(client, h, url, "photo")["properties"]["photo"] == [{"value": photo, "alt": "Red"}]
|
||||
page = client.get(url.replace(SITE, ""))
|
||||
assert page.status_code == 200 and b"<title>Photo \xc2\xb7 " in page.data
|
||||
|
||||
def test_multipart_photo_only(self, client, h):
|
||||
response = client.post("/micropub", data={"h": "entry", "photo": (io.BytesIO(jpeg()), "p.jpg")},
|
||||
headers=h, content_type="multipart/form-data")
|
||||
assert response.status_code == 201, response.get_json()
|
||||
|
||||
def test_nothing_at_all_is_still_rejected(self, client, h):
|
||||
assert post(client, h, {}).status_code == 400
|
||||
# A photo that can't be found doesn't count
|
||||
assert post(client, h, {"photo": [f"{SITE}/media/nope.jpg"]}).status_code == 400
|
||||
|
||||
def test_text_can_be_cleared_while_photos_remain(self, app, client, h):
|
||||
photo = upload(client, h)
|
||||
url = post(client, h, {"content": ["Hi"], "photo": [photo]}).headers["Location"]
|
||||
assert update(client, h, url, replace={"content": [""]}).status_code == 204
|
||||
assert note_at(app, url).content == ""
|
||||
assert update(client, h, url, replace={"content": ["Back"]}).status_code == 204
|
||||
assert update(client, h, url, delete=["content"]).status_code == 204
|
||||
assert note_at(app, url).content == ""
|
||||
# Without photos the text has to stay
|
||||
response = update(client, h, url, delete=["content", "photo"])
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_text_is_required_without_photos(self, client, h):
|
||||
url = post(client, h, {"content": ["Hi"]}).headers["Location"]
|
||||
assert update(client, h, url, replace={"content": [""]}).status_code == 400
|
||||
assert update(client, h, url, delete=["content"]).status_code == 400
|
||||
|
||||
def test_crosspost_text_starts_with_the_link(self, app, client, h):
|
||||
url = post(client, h, {"photo": [upload(client, h)]}).headers["Location"]
|
||||
with app.app_context():
|
||||
note = note_at(app, url)
|
||||
assert compose_text(note, url, ["cats"], 500) == f"{url}\n\n#cats"
|
||||
|
||||
|
||||
class TestFailures:
|
||||
def test_same_photo_twice_is_shown_once(self, client, h):
|
||||
photo = upload(client, h)
|
||||
response = post(client, h, {"content": ["Twice"], "photo": [photo, photo]})
|
||||
assert response.status_code == 201, response.get_json()
|
||||
assert source(client, h, response.headers["Location"], "photo")["properties"]["photo"] == [{"value": photo, "alt": ""}]
|
||||
|
||||
def test_failed_create_leaves_no_post_and_no_lock(self, app, client, h, monkeypatch):
|
||||
import starpunk.syndication as syndication
|
||||
|
||||
def boom(*args, **kwargs):
|
||||
raise RuntimeError("crosspost store down")
|
||||
|
||||
monkeypatch.setattr(syndication, "select_targets", boom)
|
||||
response = post(client, h, {"content": ["Doomed"], "photo": [upload(client, h)]})
|
||||
assert response.status_code == 500
|
||||
with app.app_context():
|
||||
assert list_notes(published_only=False) == []
|
||||
monkeypatch.undo()
|
||||
# The database is usable straight away
|
||||
assert post(client, h, {"content": ["Next"]}).status_code == 201
|
||||
|
||||
def test_pool_rolls_back_open_transactions(self, tmp_path):
|
||||
pool = ConnectionPool(str(tmp_path / "x.db"), pool_size=1, timeout=1)
|
||||
conn = pool.get_connection()
|
||||
conn.execute("CREATE TABLE t (x)")
|
||||
conn.commit()
|
||||
conn.execute("INSERT INTO t VALUES (1)")
|
||||
assert conn.in_transaction
|
||||
pool.return_connection(conn)
|
||||
assert not conn.in_transaction
|
||||
assert pool.get_connection().execute("SELECT count(*) FROM t").fetchone()[0] == 0
|
||||
+38
-1
@@ -11,7 +11,7 @@ Organized by model:
|
||||
import pytest
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from starpunk.models import Note, Session, Token, AuthState
|
||||
from starpunk.models import Note, Session, Token, AuthState, split_title
|
||||
from starpunk.utils import calculate_content_hash
|
||||
|
||||
|
||||
@@ -245,6 +245,43 @@ class TestNoteModel:
|
||||
|
||||
assert note.excerpt == "my-note"
|
||||
|
||||
def _note_with(self, tmp_path, content):
|
||||
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
||||
note_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
note_file.write_text(content)
|
||||
return Note(
|
||||
id=1,
|
||||
slug="test",
|
||||
file_path="notes/2024/11/test.md",
|
||||
published=True,
|
||||
created_at=datetime.utcnow(),
|
||||
updated_at=datetime.utcnow(),
|
||||
_data_dir=tmp_path,
|
||||
)
|
||||
|
||||
def test_split_title(self):
|
||||
"""A '# Heading' first line is split off as the title"""
|
||||
assert split_title("# My Title\n\nBody") == ("My Title", "Body")
|
||||
assert split_title("\n## Sub\nBody") == ("Sub", "Body")
|
||||
assert split_title("No heading\n# later") == (None, "No heading\n# later")
|
||||
assert split_title("#\nBody") == (None, "#\nBody")
|
||||
|
||||
def test_body_html_leaves_out_title(self, tmp_path):
|
||||
"""Pages show the title themselves, so the body must not repeat it"""
|
||||
note = self._note_with(tmp_path, "# My Title\n\nBody text.")
|
||||
assert "My Title" not in note.body_html
|
||||
assert "<p>Body text.</p>" in note.body_html
|
||||
assert "<h1>My Title</h1>" in note.html # html is unchanged
|
||||
|
||||
def test_body_html_untitled_matches_html(self, tmp_path):
|
||||
note = self._note_with(tmp_path, "Plain note.")
|
||||
assert note.body_html == note.html
|
||||
|
||||
def test_excerpt_leaves_out_title(self, tmp_path):
|
||||
"""Previews show the title above the excerpt, so it is not repeated"""
|
||||
note = self._note_with(tmp_path, "# Title\n\nShort content.")
|
||||
assert note.excerpt == "Short content."
|
||||
|
||||
def test_permalink(self):
|
||||
"""Test permalink generation"""
|
||||
note = Note(
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
Rendered posts read as the right post type (v2.6.5).
|
||||
|
||||
Parses note pages and the home page with mf2py, then runs W3C Post Type
|
||||
Discovery (https://www.w3.org/TR/post-type-discovery/) on what a consumer
|
||||
sees. A photo post is any post with a photo, unless it's first a repost,
|
||||
like or reply.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import mf2py
|
||||
|
||||
import starpunk.reply_context as reply_context
|
||||
from tests.test_micropub_v240 import SITE, app, client, h, jpeg # noqa: F401
|
||||
from tests.test_micropub_v265 import upload
|
||||
|
||||
|
||||
def post_type(props):
|
||||
"""W3C Post Type Discovery, section 5 (no events or RSVPs here)."""
|
||||
for prop, kind in [("repost-of", "repost"), ("like-of", "like"), ("in-reply-to", "reply"),
|
||||
("video", "video"), ("photo", "photo")]:
|
||||
if props.get(prop):
|
||||
return kind
|
||||
|
||||
def first(prop):
|
||||
for v in props.get(prop, []):
|
||||
v = v.get("value", "") if isinstance(v, dict) else v
|
||||
if v.strip():
|
||||
return " ".join(v.split())
|
||||
return ""
|
||||
|
||||
content = first("content") or first("summary")
|
||||
name = first("name")
|
||||
if not content or not name or content.startswith(name):
|
||||
return "note"
|
||||
return "article"
|
||||
|
||||
|
||||
def entries(items):
|
||||
for item in items:
|
||||
if "h-entry" in item.get("type", []):
|
||||
yield item
|
||||
yield from entries(item.get("children", []))
|
||||
|
||||
|
||||
def value(v):
|
||||
"""A u-* value, or the url of a nested microformat (h-cite)."""
|
||||
return v.get("value") or v.get("properties", {}).get("url", [None])[0] if isinstance(v, dict) else v
|
||||
|
||||
|
||||
CASES = {
|
||||
"note": ({"content": ["Just a note"]}, "note"),
|
||||
"article": ({"name": ["A Title"], "content": ["Body text here"]}, "article"),
|
||||
"photo without text": ({"photo": ["PHOTO"]}, "photo"),
|
||||
"photo with caption": ({"content": ["Sunset"], "photo": [{"value": "PHOTO", "alt": "Orange sky"}]}, "photo"),
|
||||
"photo with title": ({"name": ["Trip"], "content": ["Day one"], "photo": ["PHOTO"]}, "photo"),
|
||||
"reply": ({"content": ["Agreed"], "in-reply-to": ["https://other.example/post/1"]}, "reply"),
|
||||
"reply with photo": ({"content": ["Mine"], "in-reply-to": ["https://other.example/post/2"],
|
||||
"photo": ["PHOTO"]}, "reply"),
|
||||
"repost": ({"repost-of": ["https://other.example/post/4"]}, "repost"),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", CASES)
|
||||
def test_post_type(app, client, h, monkeypatch, case):
|
||||
monkeypatch.setattr(reply_context, "fetch_reply_context", lambda *a, **k: None, raising=False)
|
||||
props, want = CASES[case]
|
||||
photo = upload(client, h) if "PHOTO" in str(props) else None
|
||||
props = {k: [photo if v == "PHOTO" else ({**v, "value": photo} if isinstance(v, dict) else v)
|
||||
for v in vs] for k, vs in props.items()}
|
||||
response = client.post("/micropub", json={"type": ["h-entry"], "properties": props}, headers=h)
|
||||
assert response.status_code == 201, response.get_json()
|
||||
url = response.headers["Location"]
|
||||
|
||||
(entry,) = entries(mf2py.parse(doc=client.get(url.replace(SITE, "")).data.decode(), url=url)["items"])
|
||||
home = mf2py.parse(doc=client.get("/").data.decode(), url=f"{SITE}/")
|
||||
(listed,) = [e for e in entries(home["items"]) if e["properties"].get("url") == [url]]
|
||||
|
||||
for e in (entry, listed):
|
||||
assert post_type(e["properties"]) == want
|
||||
if want == "reply":
|
||||
assert value(e["properties"]["in-reply-to"][0]) == props["in-reply-to"][0]
|
||||
if want == "photo" and "name" not in props:
|
||||
assert "name" not in e["properties"], "no implied name to show as a caption"
|
||||
|
||||
|
||||
def test_photo_alt_text(app, client, h):
|
||||
"""Alt text is the caption, else the post's text, never made up (v2.6.5)."""
|
||||
def alts(props):
|
||||
url = client.post("/micropub", json={"type": ["h-entry"], "properties": props},
|
||||
headers=h).headers["Location"]
|
||||
(entry,) = entries(mf2py.parse(doc=client.get(url.replace(SITE, "")).data.decode(), url=url)["items"])
|
||||
return [p["alt"] for p in entry["properties"]["photo"]]
|
||||
|
||||
assert alts({"content": ["Beach day"], "photo": [{"value": upload(client, h), "alt": "Waves"}]}) == ["Waves"]
|
||||
assert alts({"content": ["Beach day"], "photo": [upload(client, h, "blue")]}) == ["Beach day"]
|
||||
assert alts({"name": ["Trip"], "content": ["Day one"], "photo": [upload(client, h, "green")]}) == ["Day one"]
|
||||
assert alts({"photo": [upload(client, h, "gray")]}) == ["Image"]
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Public page markup from the v2.3.0 style pass"""
|
||||
|
||||
import markdown
|
||||
import pytest
|
||||
|
||||
from starpunk import create_app
|
||||
from starpunk.models import MARKDOWN_EXTENSIONS
|
||||
from starpunk.notes import create_note
|
||||
|
||||
|
||||
def md(text):
|
||||
return markdown.markdown(text, extensions=MARKDOWN_EXTENSIONS)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("text, expected", [
|
||||
("see https://sqlite.org/x.html.", '<a href="https://sqlite.org/x.html">https://sqlite.org/x.html</a>.'),
|
||||
("(https://a.example/p)", '(<a href="https://a.example/p">https://a.example/p</a>)'),
|
||||
("[label](https://a.example)", '<a href="https://a.example">label</a>'),
|
||||
("`https://code.example`", "<code>https://code.example</code>"),
|
||||
])
|
||||
def test_bare_urls_are_linked(text, expected):
|
||||
assert expected in md(text)
|
||||
|
||||
|
||||
def test_url_as_link_label_is_not_nested():
|
||||
assert md("[https://b.example](https://b.example)").count("<a ") == 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
data = tmp_path / "data"
|
||||
return create_app({
|
||||
"TESTING": True, "SITE_URL": "https://blog.example.com", "SITE_NAME": "My Notes",
|
||||
"ADMIN_ME": "https://example.com", "SESSION_SECRET": "x",
|
||||
"DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "starpunk.db",
|
||||
})
|
||||
|
||||
|
||||
def test_home_shows_full_notes_and_site_name(app):
|
||||
long_text = "word " * 120 + "THE-END"
|
||||
with app.app_context():
|
||||
create_note(content=long_text, published=True, tags=["thoughts"])
|
||||
html = app.test_client().get("/").data.decode()
|
||||
assert "THE-END" in html # not cut at 300 characters
|
||||
assert '<a class="site-title" href="/">My Notes</a>' in html
|
||||
assert 'class="p-category" rel="tag"' in html
|
||||
# Notes' metadata is not styled as the page footer
|
||||
assert html.count('<footer class="site-footer">') == 1
|
||||
assert '<footer class="note-meta">' in html
|
||||
nav = html.split('class="site-nav"', 1)[1].split("</nav>", 1)[0]
|
||||
assert 'href="/auth/login"' in nav # login is in the top bar
|
||||
|
||||
|
||||
def test_note_page_meta_line(app):
|
||||
with app.app_context():
|
||||
note = create_note(content="Hello", published=True)
|
||||
html = app.test_client().get(f"/note/{note.slug}").data.decode()
|
||||
assert "<title>Hello · My Notes</title>" in html
|
||||
assert 'class="u-url u-uid"' in html
|
||||
|
||||
|
||||
def test_nav_shows_admin_when_logged_in(app):
|
||||
from starpunk.auth import create_session
|
||||
client = app.test_client()
|
||||
with app.test_request_context():
|
||||
client.set_cookie("starpunk_session", create_session("https://example.com"))
|
||||
nav = client.get("/").data.decode().split('class="site-nav"', 1)[1].split("</nav>", 1)[0]
|
||||
assert ">Admin</a>" in nav
|
||||
assert "/auth/login" not in nav
|
||||
@@ -274,7 +274,8 @@ class TestVersionDisplay:
|
||||
"""Test version number appears in footer"""
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert b"0.5.0" in response.data or b"StarPunk v" in response.data
|
||||
from starpunk import __version__
|
||||
assert f"v{__version__}".encode() in response.data
|
||||
|
||||
|
||||
# OAuth metadata endpoint tests removed in Phase 1 of IndieAuth server removal
|
||||
|
||||
@@ -220,8 +220,8 @@ def test_search_api_generates_correct_urls(client, test_notes):
|
||||
|
||||
if data["count"] > 0:
|
||||
result = data["results"][0]
|
||||
assert result["url"].startswith("/notes/")
|
||||
assert result["url"] == f"/notes/{result['slug']}"
|
||||
assert result["url"].startswith("/note/")
|
||||
assert result["url"] == f"/note/{result['slug']}"
|
||||
|
||||
|
||||
def test_search_api_provides_fallback_title(client, app):
|
||||
|
||||
@@ -185,8 +185,8 @@ def test_search_page_links_to_notes(client, test_notes):
|
||||
response = client.get("/search?q=test")
|
||||
assert response.status_code == 200
|
||||
|
||||
# Should contain links to /notes/
|
||||
assert b'href="/notes/' in response.data
|
||||
# Should contain links to /note/
|
||||
assert b'href="/note/' in response.data
|
||||
|
||||
|
||||
def test_search_form_validation(client):
|
||||
|
||||
@@ -0,0 +1,396 @@
|
||||
"""
|
||||
Tests for crossposting to GoToSocial (starpunk/syndication.py, v2.1.0)
|
||||
|
||||
A fake Mastodon-API server (httpx.MockTransport) records what StarPunk sends.
|
||||
"""
|
||||
|
||||
import io
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from starpunk import create_app, syndication
|
||||
from starpunk.auth import create_session
|
||||
from starpunk.indieauth import create_access_token
|
||||
from starpunk.notes import create_note, delete_note, update_note
|
||||
from starpunk.syndication import (
|
||||
MastodonClient,
|
||||
compose_text,
|
||||
get_note_syndications,
|
||||
note_changed,
|
||||
process_due,
|
||||
select_targets,
|
||||
)
|
||||
|
||||
GTS = "https://social.example.com"
|
||||
|
||||
|
||||
class FakeServer:
|
||||
def __init__(self):
|
||||
self.statuses = {}
|
||||
self.calls = []
|
||||
self.media = 0
|
||||
self.uploads = [] # raw multipart bodies
|
||||
self.fail_next = None # status code to return once
|
||||
|
||||
def __call__(self, request: httpx.Request) -> httpx.Response:
|
||||
self.calls.append((request.method, request.url.path))
|
||||
assert request.headers["Authorization"] == "Bearer secret-token"
|
||||
if self.fail_next:
|
||||
code, self.fail_next = self.fail_next, None
|
||||
return httpx.Response(code, json={"error": "nope"})
|
||||
path = request.url.path
|
||||
if path == "/api/v2/media":
|
||||
self.media += 1
|
||||
self.uploads.append(request.read())
|
||||
return httpx.Response(200, json={"id": f"m{self.media}"})
|
||||
if path == "/api/v1/statuses" and request.method == "POST":
|
||||
body = json.loads(request.content)
|
||||
sid = str(len(self.statuses) + 1)
|
||||
self.statuses[sid] = body
|
||||
return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"})
|
||||
if path.startswith("/api/v1/statuses/"):
|
||||
sid = path.rsplit("/", 1)[1]
|
||||
if sid not in self.statuses:
|
||||
return httpx.Response(404, json={"error": "not found"})
|
||||
if request.method == "PUT":
|
||||
self.statuses[sid] = json.loads(request.content)
|
||||
return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"})
|
||||
if request.method == "DELETE":
|
||||
del self.statuses[sid]
|
||||
return httpx.Response(200, json={"id": sid})
|
||||
return httpx.Response(500)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server():
|
||||
return FakeServer()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path, server):
|
||||
data = tmp_path / "data"
|
||||
app = create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
"GOTOSOCIAL_URL": GTS,
|
||||
"GOTOSOCIAL_ACCESS_TOKEN": "secret-token",
|
||||
}
|
||||
)
|
||||
syndication._client_cache[GTS] = MastodonClient(
|
||||
GTS, "secret-token", http=httpx.Client(transport=httpx.MockTransport(server))
|
||||
)
|
||||
yield app
|
||||
syndication._client_cache.clear()
|
||||
|
||||
|
||||
def rows(app, note_id):
|
||||
with app.app_context():
|
||||
return get_note_syndications(note_id)
|
||||
|
||||
|
||||
def publish(app, content="Hello fediverse", tags=None, targets=("gotosocial",)):
|
||||
with app.test_request_context():
|
||||
note = create_note(content=content, published=True, tags=tags)
|
||||
select_targets(note.id, list(targets))
|
||||
process_due()
|
||||
return note
|
||||
|
||||
|
||||
class TestCompose:
|
||||
def test_full_note_with_link_and_hashtags(self, app):
|
||||
with app.app_context():
|
||||
note = create_note(content="# Title\n\nSome *markdown*", published=True)
|
||||
text = compose_text(note, "https://blog.example.com/note/x", ["IndieWeb", "Self Hosting"], 5000)
|
||||
assert text == "# Title\n\nSome *markdown*\n\nhttps://blog.example.com/note/x\n\n#IndieWeb #SelfHosting"
|
||||
|
||||
def test_long_note_is_shortened_but_keeps_link(self, app):
|
||||
with app.app_context():
|
||||
note = create_note(content="word " * 2000, published=True)
|
||||
text = compose_text(note, "https://blog.example.com/note/x", [], 500)
|
||||
assert len(text) <= 500
|
||||
assert text.endswith("https://blog.example.com/note/x")
|
||||
assert "…" in text
|
||||
|
||||
|
||||
class TestLifecycle:
|
||||
def test_publish_creates_status(self, app, server):
|
||||
note = publish(app, tags=["Test"])
|
||||
(row,) = rows(app, note.id)
|
||||
assert row["state"] == "synced"
|
||||
assert row["remote_url"] == f"{GTS}/@phil/statuses/1"
|
||||
body = server.statuses["1"]
|
||||
assert body["content_type"] == "text/markdown"
|
||||
assert body["visibility"] == "public"
|
||||
assert f"https://blog.example.com/note/{note.slug}" in body["status"]
|
||||
assert "#Test" in body["status"]
|
||||
|
||||
def test_edit_updates_status(self, app, server):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, content="Edited text")
|
||||
process_due()
|
||||
assert server.statuses["1"]["status"].startswith("Edited text")
|
||||
assert ("PUT", "/api/v1/statuses/1") in server.calls
|
||||
|
||||
def test_delete_deletes_status(self, app, server):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id)
|
||||
process_due()
|
||||
assert server.statuses == {}
|
||||
(row,) = rows(app, note.id)
|
||||
assert row["remote_id"] is None and row["state"] == "synced"
|
||||
|
||||
def test_hard_delete_deletes_status(self, app, server):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id, soft=False)
|
||||
process_due()
|
||||
assert server.statuses == {}
|
||||
|
||||
def test_unpublish_then_republish(self, app, server):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, published=False)
|
||||
process_due()
|
||||
assert server.statuses == {}
|
||||
update_note(id=note.id, published=True)
|
||||
process_due()
|
||||
assert len(server.statuses) == 1
|
||||
|
||||
def test_opt_out_sends_nothing(self, app, server):
|
||||
note = publish(app, targets=())
|
||||
assert server.calls == []
|
||||
assert rows(app, note.id) == []
|
||||
|
||||
def test_unticking_later_deletes_status(self, app, server):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
select_targets(note.id, [])
|
||||
process_due()
|
||||
assert server.statuses == {}
|
||||
|
||||
def test_drafts_are_not_sent(self, app, server):
|
||||
with app.test_request_context():
|
||||
note = create_note(content="draft", published=False)
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
process_due()
|
||||
assert server.statuses == {}
|
||||
|
||||
def test_remote_deleted_by_hand_is_reposted_on_edit(self, app, server):
|
||||
note = publish(app)
|
||||
server.statuses.clear()
|
||||
with app.test_request_context():
|
||||
note_changed(note.id)
|
||||
process_due()
|
||||
assert len(server.statuses) == 1
|
||||
|
||||
def test_edit_during_send_is_not_lost(self, app, server):
|
||||
with app.test_request_context():
|
||||
note = create_note(content="v1", published=True)
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
# Simulate an edit arriving while the first send is in flight
|
||||
original = syndication._reconcile
|
||||
|
||||
def edit_mid_send(row, target):
|
||||
result = original(row, target)
|
||||
update_note(id=note.id, content="v2")
|
||||
return result
|
||||
|
||||
syndication._reconcile = edit_mid_send
|
||||
try:
|
||||
process_due()
|
||||
finally:
|
||||
syndication._reconcile = original
|
||||
assert rows(app, note.id)[0]["state"] == "pending"
|
||||
process_due()
|
||||
assert server.statuses["1"]["status"].startswith("v2")
|
||||
|
||||
|
||||
class TestFailures:
|
||||
def test_server_error_retries_later(self, app, server):
|
||||
server.fail_next = 503
|
||||
note = publish(app)
|
||||
(row,) = rows(app, note.id)
|
||||
assert row["state"] == "pending"
|
||||
assert row["attempts"] == 1
|
||||
assert "503" in row["last_error"]
|
||||
|
||||
def test_client_error_fails_permanently(self, app, server):
|
||||
server.fail_next = 422
|
||||
note = publish(app)
|
||||
(row,) = rows(app, note.id)
|
||||
assert row["state"] == "failed"
|
||||
|
||||
def test_retry_button(self, app, server):
|
||||
server.fail_next = 422
|
||||
note = publish(app)
|
||||
(row,) = rows(app, note.id)
|
||||
client = app.test_client()
|
||||
with app.test_request_context():
|
||||
client.set_cookie("starpunk_session", create_session("https://example.com"))
|
||||
client.post(f"/admin/syndication/{row['id']}/retry")
|
||||
with app.test_request_context():
|
||||
process_due()
|
||||
assert rows(app, note.id)[0]["state"] == "synced"
|
||||
|
||||
def test_no_targets_configured_does_nothing(self, tmp_path):
|
||||
data = tmp_path / "d"
|
||||
app = create_app({
|
||||
"TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example",
|
||||
"SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "s.db",
|
||||
})
|
||||
with app.test_request_context():
|
||||
note = create_note(content="x", published=True)
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
assert process_due() == 0
|
||||
assert get_note_syndications(note.id) == []
|
||||
|
||||
|
||||
class TestMedia:
|
||||
def test_images_are_uploaded_with_alt_text(self, app, server):
|
||||
from starpunk.media import attach_media_to_note, save_media
|
||||
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (800, 600), "red").save(buf, format="JPEG")
|
||||
with app.test_request_context():
|
||||
note = create_note(content="With a picture", published=True)
|
||||
media = save_media(buf.getvalue(), "red.jpg")
|
||||
attach_media_to_note(note.id, [media["id"]], ["A red square"])
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
process_due()
|
||||
assert ("POST", "/api/v2/media") in server.calls
|
||||
assert server.statuses["1"]["media_ids"] == ["m1"]
|
||||
|
||||
|
||||
class TestInlineImages:
|
||||
"""Clients like OutPost upload a photo and embed it in the content (v2.6.6)"""
|
||||
|
||||
def _upload(self, app):
|
||||
from starpunk.media import save_media
|
||||
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (800, 600), "orange").save(buf, format="JPEG")
|
||||
with app.test_request_context():
|
||||
return save_media(buf.getvalue(), "cat.jpg")
|
||||
|
||||
def test_embedded_own_image_is_attached(self, app, server):
|
||||
media = self._upload(app)
|
||||
url = f"https://blog.example.com/media/{media['path']}"
|
||||
publish(app, content=f"")
|
||||
status = server.statuses["1"]
|
||||
assert status["media_ids"] == ["m1"]
|
||||
assert b"Tortie cat on a chair" in server.uploads[0]
|
||||
assert "![" not in status["status"] and url not in status["status"]
|
||||
assert status["status"].startswith("https://blog.example.com/note/")
|
||||
|
||||
def test_text_around_image_is_kept(self, app, server):
|
||||
media = self._upload(app)
|
||||
publish(app, content=f"Look\n\n<img src=\"/media/{media['path']}\" alt=\"cat\">\n\nNice")
|
||||
status = server.statuses["1"]
|
||||
assert status["media_ids"] == ["m1"]
|
||||
assert status["status"].startswith("Look\n\nNice")
|
||||
|
||||
def test_image_both_attached_and_embedded_is_sent_once(self, app, server):
|
||||
from starpunk.media import attach_media_to_note
|
||||
|
||||
media = self._upload(app)
|
||||
with app.test_request_context():
|
||||
note = create_note(content=f"", published=True)
|
||||
attach_media_to_note(note.id, [media["id"]], ["caption"])
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
process_due()
|
||||
assert server.statuses["1"]["media_ids"] == ["m1"]
|
||||
|
||||
def test_other_sites_images_are_ignored(self, app, server):
|
||||
publish(app, content=" hi")
|
||||
assert server.statuses["1"]["media_ids"] == []
|
||||
assert server.uploads == []
|
||||
|
||||
|
||||
class TestEntryPoints:
|
||||
@pytest.fixture
|
||||
def admin(self, app):
|
||||
client = app.test_client()
|
||||
with app.test_request_context():
|
||||
client.set_cookie("starpunk_session", create_session("https://example.com"))
|
||||
return client
|
||||
|
||||
def test_new_note_form_ticks_target_by_default(self, admin):
|
||||
html = admin.get("/admin/new").data.decode()
|
||||
assert 'name="syndicate_to" value="gotosocial" checked' in html
|
||||
|
||||
def test_admin_create_queues(self, app, admin):
|
||||
admin.post("/admin/new", data={
|
||||
"content": "From the editor", "published": "on",
|
||||
"syndication_form": "1", "syndicate_to": "gotosocial",
|
||||
})
|
||||
with app.app_context():
|
||||
from starpunk.database import get_db
|
||||
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 1
|
||||
|
||||
def test_admin_create_opt_out(self, app, admin):
|
||||
admin.post("/admin/new", data={"content": "Not this one", "published": "on", "syndication_form": "1"})
|
||||
with app.app_context():
|
||||
from starpunk.database import get_db
|
||||
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 0
|
||||
|
||||
def _token(self, app):
|
||||
with app.test_request_context():
|
||||
return create_access_token("https://quill.example/", "create")["access_token"]
|
||||
|
||||
def test_micropub_lists_target(self, app):
|
||||
token = self._token(app)
|
||||
data = app.test_client().get(
|
||||
"/micropub?q=syndicate-to", headers={"Authorization": f"Bearer {token}"}
|
||||
).get_json()
|
||||
assert data["syndicate-to"] == [{"uid": f"{GTS}/", "name": "GoToSocial"}]
|
||||
|
||||
def test_micropub_default_is_all_targets(self, app):
|
||||
token = self._token(app)
|
||||
response = app.test_client().post(
|
||||
"/micropub", data={"h": "entry", "content": "via quill"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
assert "/note/" in response.headers["Location"]
|
||||
assert response.headers["Location"].startswith("https://blog.example.com/note/")
|
||||
with app.app_context():
|
||||
from starpunk.database import get_db
|
||||
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 1
|
||||
|
||||
def test_micropub_explicit_empty_choice(self, app):
|
||||
token = self._token(app)
|
||||
app.test_client().post(
|
||||
"/micropub", json={"type": ["h-entry"], "properties": {"content": ["no crosspost"]},
|
||||
"mp-syndicate-to": []},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
with app.app_context():
|
||||
from starpunk.database import get_db
|
||||
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 0
|
||||
|
||||
def test_note_page_links_to_crosspost(self, app, server):
|
||||
note = publish(app)
|
||||
html = app.test_client().get(f"/note/{note.slug}").data.decode()
|
||||
assert f'class="u-syndication" href="{GTS}/@phil/statuses/1"' in html
|
||||
|
||||
def test_edit_page_shows_status_and_separate_delete_form(self, app, admin, server):
|
||||
note = publish(app)
|
||||
html = admin.get(f"/admin/edit/{note.id}").data.decode()
|
||||
assert 'value="gotosocial" checked' in html
|
||||
assert f"{GTS}/@phil/statuses/1" in html
|
||||
# Delete button belongs to its own form, not the edit form
|
||||
assert 'form="delete-note-form"' in html
|
||||
edit_form = html.split('class="note-form"', 1)[1].split("</form>", 1)[0]
|
||||
assert "<form" not in edit_form
|
||||
@@ -0,0 +1,392 @@
|
||||
"""
|
||||
Tests for crossposting to Bluesky (starpunk/syndication.py, v2.2.0)
|
||||
|
||||
A fake PDS (httpx.MockTransport) records what StarPunk sends.
|
||||
"""
|
||||
|
||||
import io
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from starpunk import create_app, syndication
|
||||
from starpunk.notes import create_note, delete_note, update_note
|
||||
from starpunk.syndication import (
|
||||
BlueskyClient,
|
||||
bluesky_facets,
|
||||
get_note_syndications,
|
||||
plain_text,
|
||||
process_due,
|
||||
select_targets,
|
||||
)
|
||||
|
||||
SERVICE = "https://bsky.example"
|
||||
PDS = "https://pds.example"
|
||||
DID = "did:plc:abc123"
|
||||
|
||||
|
||||
class FakePDS:
|
||||
def __init__(self):
|
||||
self.records = {}
|
||||
self.blobs = []
|
||||
self.calls = []
|
||||
self.logins = 0
|
||||
self.expire_next = False
|
||||
self.lose_next_reply = False
|
||||
self.fail_next = None
|
||||
|
||||
def _json(self, code, body):
|
||||
return httpx.Response(code, json=body)
|
||||
|
||||
def __call__(self, request: httpx.Request) -> httpx.Response:
|
||||
nsid = request.url.path.removeprefix("/xrpc/")
|
||||
self.calls.append((request.url.host, nsid))
|
||||
if nsid == "com.atproto.server.createSession":
|
||||
body = json.loads(request.content)
|
||||
if body["password"] != "app-pass":
|
||||
return self._json(401, {"error": "AuthenticationRequired"})
|
||||
self.logins += 1
|
||||
return self._json(200, {
|
||||
"did": DID, "handle": "phil.example", "accessJwt": f"access{self.logins}",
|
||||
"refreshJwt": "refresh",
|
||||
"didDoc": {"service": [{"id": "#atproto_pds", "serviceEndpoint": PDS}]},
|
||||
})
|
||||
if nsid == "com.atproto.server.refreshSession":
|
||||
self.logins += 1
|
||||
return self._json(200, {"did": DID, "handle": "phil.example",
|
||||
"accessJwt": f"access{self.logins}", "refreshJwt": "refresh"})
|
||||
assert request.url.host == "pds.example"
|
||||
if request.headers.get("Authorization") != f"Bearer access{self.logins}" or self.expire_next:
|
||||
self.expire_next = False
|
||||
return self._json(400, {"error": "ExpiredToken"})
|
||||
if self.fail_next:
|
||||
code, self.fail_next = self.fail_next, None
|
||||
return self._json(code, {"error": "Nope"})
|
||||
if nsid == "com.atproto.repo.uploadBlob":
|
||||
self.blobs.append(len(request.content))
|
||||
assert len(request.content) <= 1_000_000
|
||||
return self._json(200, {"blob": {"$type": "blob", "ref": {"$link": f"b{len(self.blobs)}"},
|
||||
"mimeType": request.headers["Content-Type"],
|
||||
"size": len(request.content)}})
|
||||
if nsid == "com.atproto.repo.createRecord":
|
||||
body = json.loads(request.content)
|
||||
uri = f"at://{DID}/app.bsky.feed.post/r{len(self.records) + 1}"
|
||||
self.records[uri] = body["record"]
|
||||
if self.lose_next_reply:
|
||||
self.lose_next_reply = False
|
||||
raise httpx.ReadTimeout("lost reply")
|
||||
return self._json(200, {"uri": uri, "cid": "c"})
|
||||
if nsid == "com.atproto.repo.listRecords":
|
||||
return self._json(200, {"records": [{"uri": u, "value": v} for u, v in self.records.items()]})
|
||||
if nsid == "com.atproto.repo.deleteRecord":
|
||||
body = json.loads(request.content)
|
||||
self.records.pop(f"at://{body['repo']}/{body['collection']}/{body['rkey']}", None)
|
||||
return self._json(200, {})
|
||||
return self._json(500, {"error": "unknown"})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pds():
|
||||
return FakePDS()
|
||||
|
||||
|
||||
class FakeWeb:
|
||||
"""Other people's pages, for link cards: url -> (status, headers, body)"""
|
||||
def __init__(self):
|
||||
self.pages = {}
|
||||
self.fetched = []
|
||||
|
||||
def __call__(self, request):
|
||||
url = str(request.url)
|
||||
self.fetched.append(url)
|
||||
status, headers, body = self.pages.get(url, (404, {}, b"nope"))
|
||||
return httpx.Response(status, headers=headers, content=body)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def web():
|
||||
return FakeWeb()
|
||||
|
||||
|
||||
def routed(pds, web):
|
||||
return httpx.MockTransport(
|
||||
lambda r: pds(r) if r.url.host in ("bsky.example", "pds.example") else web(r))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path, pds, web):
|
||||
data = tmp_path / "data"
|
||||
app = create_app({
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
"BLUESKY_HANDLE": "phil.example",
|
||||
"BLUESKY_APP_PASSWORD": "app-pass",
|
||||
"BLUESKY_SERVICE": SERVICE,
|
||||
})
|
||||
syndication._client_cache[f"bluesky:{SERVICE}:phil.example"] = BlueskyClient(
|
||||
SERVICE, "phil.example", "app-pass", http=httpx.Client(transport=routed(pds, web))
|
||||
)
|
||||
yield app
|
||||
syndication._client_cache.clear()
|
||||
|
||||
|
||||
def publish(app, content="Hello Bluesky", tags=None):
|
||||
with app.test_request_context():
|
||||
note = create_note(content=content, published=True, tags=tags)
|
||||
select_targets(note.id, ["bluesky"])
|
||||
process_due()
|
||||
return note
|
||||
|
||||
|
||||
def only_record(pds):
|
||||
(record,) = pds.records.values()
|
||||
return record
|
||||
|
||||
|
||||
def row(app, note):
|
||||
with app.app_context():
|
||||
(r,) = get_note_syndications(note.id)
|
||||
return r
|
||||
|
||||
|
||||
class TestText:
|
||||
def test_markdown_becomes_plain_text(self):
|
||||
md = "# Title\n\nSome *emphasis* and a [link](https://x.example).\n\n- one\n- two\n\n"
|
||||
assert plain_text(md) == "Title\n\nSome emphasis and a link.\n\n• one\n• two"
|
||||
|
||||
def test_facets_use_utf8_byte_offsets(self):
|
||||
text = "Café ☕ https://blog.example.com/note/x #IndieWeb"
|
||||
link, tag = bluesky_facets(text)
|
||||
raw = text.encode()
|
||||
assert raw[link["index"]["byteStart"]:link["index"]["byteEnd"]] == b"https://blog.example.com/note/x"
|
||||
assert raw[tag["index"]["byteStart"]:tag["index"]["byteEnd"]] == b"#IndieWeb"
|
||||
assert tag["features"][0]["tag"] == "IndieWeb"
|
||||
|
||||
def test_fragment_in_url_is_not_a_tag(self):
|
||||
facets = bluesky_facets("see https://x.example/page#section and #42 too")
|
||||
assert [f["features"][0]["$type"] for f in facets] == ["app.bsky.richtext.facet#link"]
|
||||
|
||||
|
||||
class TestLifecycle:
|
||||
def test_publish_posts_with_link_card(self, app, pds):
|
||||
note = publish(app, content="Hello **Bluesky**", tags=["IndieWeb"])
|
||||
record = only_record(pds)
|
||||
permalink = f"https://blog.example.com/note/{note.slug}"
|
||||
assert record["text"] == f"Hello Bluesky\n\n{permalink}\n\n#IndieWeb"
|
||||
assert record["embed"]["external"]["uri"] == permalink
|
||||
kinds = {f["features"][0]["$type"] for f in record["facets"]}
|
||||
assert kinds == {"app.bsky.richtext.facet#link", "app.bsky.richtext.facet#tag"}
|
||||
r = row(app, note)
|
||||
assert r["state"] == "synced"
|
||||
assert r["remote_id"] == f"at://{DID}/app.bsky.feed.post/r1"
|
||||
assert r["remote_url"] == "https://bsky.app/profile/phil.example/post/r1"
|
||||
|
||||
def test_long_note_fits_300(self, app, pds):
|
||||
note = publish(app, content="word " * 500)
|
||||
text = only_record(pds)["text"]
|
||||
assert len(text) <= 300
|
||||
assert text.endswith(f"https://blog.example.com/note/{note.slug}")
|
||||
|
||||
def test_edit_leaves_post_alone(self, app, pds):
|
||||
note = publish(app, content="first")
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, content="second")
|
||||
process_due()
|
||||
assert only_record(pds)["text"].startswith("first")
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_delete_and_unpublish_remove_post(self, app, pds):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, published=False)
|
||||
process_due()
|
||||
assert pds.records == {}
|
||||
update_note(id=note.id, published=True)
|
||||
process_due()
|
||||
assert len(pds.records) == 1
|
||||
delete_note(id=note.id)
|
||||
process_due()
|
||||
assert pds.records == {}
|
||||
|
||||
|
||||
class TestSession:
|
||||
def test_logs_in_once_and_uses_pds(self, app, pds):
|
||||
publish(app, content="one")
|
||||
publish(app, content="two")
|
||||
assert pds.logins == 1
|
||||
assert ("bsky.example", "com.atproto.server.createSession") in pds.calls
|
||||
assert ("pds.example", "com.atproto.repo.createRecord") in pds.calls
|
||||
|
||||
def test_expired_token_is_refreshed(self, app, pds):
|
||||
publish(app, content="one")
|
||||
pds.expire_next = True
|
||||
publish(app, content="two")
|
||||
assert len(pds.records) == 2
|
||||
assert ("bsky.example", "com.atproto.server.refreshSession") in pds.calls
|
||||
|
||||
def test_bad_password_fails_permanently(self, app, pds):
|
||||
syndication._client_cache[f"bluesky:{SERVICE}:phil.example"].password = "wrong"
|
||||
note = publish(app)
|
||||
r = row(app, note)
|
||||
assert r["state"] == "failed" and "401" in r["last_error"]
|
||||
|
||||
def test_lost_reply_does_not_double_post(self, app, pds):
|
||||
pds.lose_next_reply = True
|
||||
note = publish(app)
|
||||
assert row(app, note)["state"] == "pending"
|
||||
with app.test_request_context():
|
||||
from starpunk.syndication import retry
|
||||
retry(row(app, note)["id"])
|
||||
# retry() resets attempts; simulate the scheduled retry instead
|
||||
from starpunk.database import get_db
|
||||
get_db(app).execute("UPDATE syndications SET attempts = 1")
|
||||
get_db(app).commit()
|
||||
process_due()
|
||||
assert len(pds.records) == 1
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_server_error_retries(self, app, pds):
|
||||
publish(app, content="warm up") # log in first
|
||||
pds.fail_next = 502
|
||||
note = publish(app, content="later")
|
||||
assert row(app, note)["state"] == "pending"
|
||||
|
||||
|
||||
class TestMedia:
|
||||
def test_large_image_is_shrunk_with_alt_and_aspect_ratio(self, app, pds):
|
||||
from starpunk.media import attach_media_to_note, save_media
|
||||
import os
|
||||
|
||||
img = Image.frombytes("RGB", (2400, 1800), os.urandom(2400 * 1800 * 3))
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="PNG")
|
||||
assert buf.tell() > 1_000_000
|
||||
with app.test_request_context():
|
||||
note = create_note(content="noisy", published=True)
|
||||
media = save_media(buf.getvalue(), "noise.png")
|
||||
attach_media_to_note(note.id, [media["id"]], ["Static"])
|
||||
select_targets(note.id, ["bluesky"])
|
||||
process_due()
|
||||
record = only_record(pds)
|
||||
(image,) = record["embed"]["images"]
|
||||
assert record["embed"]["$type"] == "app.bsky.embed.images"
|
||||
assert image["alt"] == "Static"
|
||||
assert image["aspectRatio"]["width"] > 0
|
||||
|
||||
|
||||
ARTICLE = "https://news.example/story"
|
||||
ARTICLE_HTML = b"""<html><head><title>Fallback title</title>
|
||||
<meta property="og:title" content="A Great Story">
|
||||
<meta property="og:description" content="What happened, in short.">
|
||||
<meta property="og:image" content="/img/story.png">
|
||||
</head><body>...</body></html>"""
|
||||
|
||||
|
||||
def png_bytes():
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (60, 40), (10, 120, 200)).save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
class TestLinkCards:
|
||||
def test_note_link_gets_the_card(self, app, pds, web):
|
||||
web.pages[ARTICLE] = (200, {"content-type": "text/html"}, ARTICLE_HTML)
|
||||
web.pages["https://news.example/img/story.png"] = (200, {"content-type": "image/png"}, png_bytes())
|
||||
note = publish(app, content=f"Read [this story]({ARTICLE}) now")
|
||||
record = only_record(pds)
|
||||
external = record["embed"]["external"]
|
||||
assert external["uri"] == ARTICLE
|
||||
assert external["title"] == "A Great Story"
|
||||
assert external["description"] == "What happened, in short."
|
||||
assert external["thumb"]["$type"] == "blob"
|
||||
# The link text is clickable and the note's permalink is still in the text
|
||||
permalink = f"https://blog.example.com/note/{note.slug}"
|
||||
assert record["text"].startswith("Read this story now") and permalink in record["text"]
|
||||
raw = record["text"].encode()
|
||||
links = {raw[f["index"]["byteStart"]:f["index"]["byteEnd"]].decode(): f["features"][0]["uri"]
|
||||
for f in record["facets"]}
|
||||
assert links["this story"] == ARTICLE
|
||||
assert links[permalink] == permalink
|
||||
|
||||
def test_bare_url_gets_the_card(self, app, pds, web):
|
||||
web.pages[ARTICLE] = (200, {"content-type": "text/html"}, ARTICLE_HTML)
|
||||
publish(app, content=f"See {ARTICLE}.")
|
||||
assert only_record(pds)["embed"]["external"]["uri"] == ARTICLE
|
||||
|
||||
def test_unreachable_page_still_gets_a_card(self, app, pds, web):
|
||||
note = publish(app, content=f"See {ARTICLE}")
|
||||
external = only_record(pds)["embed"]["external"]
|
||||
assert external == {"uri": ARTICLE, "title": ARTICLE, "description": ""}
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_redirect_to_private_address_is_not_followed(self, app, pds, web, monkeypatch):
|
||||
web.pages[ARTICLE] = (302, {"location": "http://internal.example/admin"}, b"")
|
||||
monkeypatch.setattr("starpunk.micropub._public_address", lambda host: host != "internal.example")
|
||||
app.config["TESTING"] = False
|
||||
try:
|
||||
publish(app, content=f"See {ARTICLE}")
|
||||
finally:
|
||||
app.config["TESTING"] = True
|
||||
assert "http://internal.example/admin" not in web.fetched
|
||||
assert only_record(pds)["embed"]["external"]["title"] == ARTICLE
|
||||
|
||||
def test_images_win_over_link_card(self, app, pds, web):
|
||||
from starpunk.media import attach_media_to_note, save_media
|
||||
with app.test_request_context():
|
||||
note = create_note(content=f"Look {ARTICLE}", published=True)
|
||||
media = save_media(png_bytes(), "a.png")
|
||||
attach_media_to_note(note.id, [media["id"]], ["pic"])
|
||||
select_targets(note.id, ["bluesky"])
|
||||
process_due()
|
||||
assert only_record(pds)["embed"]["$type"] == "app.bsky.embed.images"
|
||||
assert web.fetched == []
|
||||
|
||||
|
||||
class TestLinkText:
|
||||
def test_note_links_in_reading_order(self):
|
||||
from starpunk.syndication import note_links
|
||||
md = "a [x](https://one.example) b https://two.example/p. c <https://three.example> [rel](/local)"
|
||||
assert note_links(md) == ["https://one.example", "https://two.example/p", "https://three.example"]
|
||||
|
||||
def test_link_modes(self):
|
||||
from starpunk.syndication import plain_text_links
|
||||
md = "[a story](https://s.example) and [https://u.example](https://u.example)"
|
||||
assert plain_text_links(md)[0] == "a story and https://u.example"
|
||||
assert plain_text_links(md, "inline")[0] == "a story (https://s.example) and https://u.example"
|
||||
assert plain_text_links(md, "anchor") == ("a story and https://u.example",
|
||||
[("a story", "https://s.example")])
|
||||
|
||||
def test_inline_url_budget(self):
|
||||
from starpunk.syndication import plain_text_links
|
||||
md = "https://bare.example then [one](https://1.example) [two](https://2.example) [one again](https://1.example)"
|
||||
text = plain_text_links(md, "inline", max_urls=2)[0]
|
||||
assert text == "https://bare.example then one (https://1.example) two one again (https://1.example)"
|
||||
|
||||
def test_anchor_facet_byte_offsets(self):
|
||||
text = "Café ☕ this story and more"
|
||||
(facet,) = bluesky_facets(text, [("this story", "https://s.example")])
|
||||
raw = text.encode()
|
||||
assert raw[facet["index"]["byteStart"]:facet["index"]["byteEnd"]] == b"this story"
|
||||
|
||||
|
||||
def test_both_targets_listed_for_micropub(tmp_path):
|
||||
data = tmp_path / "d"
|
||||
app = create_app({
|
||||
"TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example",
|
||||
"SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "s.db",
|
||||
"GOTOSOCIAL_URL": "https://social.example", "GOTOSOCIAL_ACCESS_TOKEN": "t",
|
||||
"BLUESKY_HANDLE": "phil.example", "BLUESKY_APP_PASSWORD": "p",
|
||||
})
|
||||
with app.app_context():
|
||||
from starpunk.syndication import get_targets, resolve_targets
|
||||
assert set(get_targets()) == {"gotosocial", "bluesky"}
|
||||
assert resolve_targets(["https://bsky.app/profile/phil.example"]) == ["bluesky"]
|
||||
syndication._client_cache.clear()
|
||||
@@ -0,0 +1,418 @@
|
||||
"""
|
||||
Tests for crossposting to Threads (starpunk/syndication.py, v2.6.0)
|
||||
|
||||
A fake Graph API (httpx.MockTransport) records what StarPunk sends.
|
||||
"""
|
||||
|
||||
import io
|
||||
import json
|
||||
import time
|
||||
from urllib.parse import parse_qsl
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from starpunk import create_app, syndication
|
||||
from starpunk.notes import create_note, delete_note, update_note
|
||||
from starpunk.syndication import (
|
||||
THREADS_REFRESH_EVERY,
|
||||
ThreadsClient,
|
||||
get_note_syndications,
|
||||
process_due,
|
||||
select_targets,
|
||||
threads_length,
|
||||
threads_topic,
|
||||
)
|
||||
|
||||
API = "https://graph.threads.example/v1.0"
|
||||
UID = "1789"
|
||||
|
||||
|
||||
class FakeThreads:
|
||||
def __init__(self):
|
||||
self.containers = {}
|
||||
self.posts = {}
|
||||
self.calls = []
|
||||
self.token = "tok1"
|
||||
self.refreshes = 0
|
||||
self.refresh_fails = False
|
||||
self.pending_polls = 0
|
||||
self.lose_next_publish = False
|
||||
self.fail_next = None
|
||||
self.fail_next_publish = False
|
||||
|
||||
def _json(self, code, body):
|
||||
return httpx.Response(code, json=body)
|
||||
|
||||
def _error(self, code, message, err_code, subcode=None):
|
||||
error = {"message": message, "type": "OAuthException", "code": err_code}
|
||||
if subcode:
|
||||
error["error_subcode"] = subcode
|
||||
return self._json(code, {"error": error})
|
||||
|
||||
def __call__(self, request: httpx.Request) -> httpx.Response:
|
||||
params = dict(request.url.params)
|
||||
if request.content:
|
||||
params.update(parse_qsl(request.content.decode()))
|
||||
path = request.url.path
|
||||
self.calls.append((request.method, path, params))
|
||||
|
||||
if path == "/refresh_access_token":
|
||||
assert params["grant_type"] == "th_refresh_token"
|
||||
if self.refresh_fails or params.get("access_token") != self.token:
|
||||
return self._error(400, "token too new", 190)
|
||||
self.refreshes += 1
|
||||
self.token = f"tok{self.refreshes + 1}"
|
||||
return self._json(200, {"access_token": self.token, "token_type": "bearer",
|
||||
"expires_in": 5184000})
|
||||
|
||||
if params.get("access_token") != self.token:
|
||||
return self._error(400, "Invalid OAuth access token", 190)
|
||||
if self.fail_next:
|
||||
code, err, self.fail_next = *self.fail_next, None
|
||||
return self._error(code, "nope", err)
|
||||
|
||||
path = path.removeprefix("/v1.0/")
|
||||
if path == "me":
|
||||
return self._json(200, {"id": UID, "username": "phil"})
|
||||
if path == f"{UID}/threads" and request.method == "POST":
|
||||
cid = f"c{len(self.containers) + 1}"
|
||||
self.containers[cid] = params
|
||||
return self._json(200, {"id": cid})
|
||||
if path == f"{UID}/threads" and request.method == "GET":
|
||||
return self._json(200, {"data": [
|
||||
{"id": pid, "text": p["text"], "permalink": f"https://www.threads.com/@phil/post/{pid}"}
|
||||
for pid, p in self.posts.items()]})
|
||||
if path == f"{UID}/threads_publish" and self.fail_next_publish:
|
||||
self.fail_next_publish = False
|
||||
return self._error(400, "Fatal", 24, 4279009)
|
||||
if path == f"{UID}/threads_publish":
|
||||
container = self.containers[params["creation_id"]]
|
||||
pid = f"p{len(self.posts) + 1}"
|
||||
self.posts[pid] = container
|
||||
if self.lose_next_publish:
|
||||
self.lose_next_publish = False
|
||||
raise httpx.ReadTimeout("lost reply")
|
||||
return self._json(200, {"id": pid})
|
||||
if path in self.containers and request.method == "GET":
|
||||
if self.pending_polls:
|
||||
self.pending_polls -= 1
|
||||
return self._json(200, {"status": "IN_PROGRESS"})
|
||||
return self._json(200, {"status": "FINISHED"})
|
||||
if path in self.posts and request.method == "GET":
|
||||
return self._json(200, {"id": path, "permalink": f"https://www.threads.com/@phil/post/{path}"})
|
||||
if request.method == "DELETE":
|
||||
if self.posts.pop(path, None) is None:
|
||||
return self._error(400, "Object does not exist", 100, 33)
|
||||
return self._json(200, {"success": True, "deleted_id": path})
|
||||
return self._json(500, {"error": {"message": "unknown", "code": 2}})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api():
|
||||
return FakeThreads()
|
||||
|
||||
|
||||
def make_client(app, api, token="tok1"):
|
||||
return ThreadsClient(token, app.config["DATA_PATH"] / "threads_token.json", api=API,
|
||||
http=httpx.Client(transport=httpx.MockTransport(api)), sleep=lambda s: None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path, api):
|
||||
data = tmp_path / "data"
|
||||
app = create_app({
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
"THREADS_ACCESS_TOKEN": "tok1",
|
||||
"THREADS_USERNAME": "phil",
|
||||
})
|
||||
syndication._client_cache["threads"] = make_client(app, api)
|
||||
yield app
|
||||
syndication._client_cache.clear()
|
||||
|
||||
|
||||
def publish(app, content="Hello Threads", tags=None):
|
||||
with app.test_request_context():
|
||||
note = create_note(content=content, published=True, tags=tags)
|
||||
select_targets(note.id, ["threads"])
|
||||
process_due()
|
||||
return note
|
||||
|
||||
|
||||
def only_post(api):
|
||||
(post,) = api.posts.values()
|
||||
return post
|
||||
|
||||
|
||||
def row(app, note):
|
||||
with app.app_context():
|
||||
(r,) = get_note_syndications(note.id)
|
||||
return r
|
||||
|
||||
|
||||
def image_bytes(fmt="JPEG", size=(800, 600)):
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", size, (200, 30, 30)).save(buf, format=fmt)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
class TestText:
|
||||
def test_emoji_count_as_bytes(self):
|
||||
assert threads_length("abc") == 3
|
||||
assert threads_length("☕") == 3
|
||||
|
||||
def test_topic_is_first_usable_tag(self):
|
||||
assert threads_topic(["...", "Indie.Web & Co", "x"]) == "IndieWeb Co"
|
||||
assert threads_topic([]) == ""
|
||||
|
||||
|
||||
class TestLifecycle:
|
||||
def test_publish_text_post(self, app, api):
|
||||
note = publish(app, content="Hello **Threads**", tags=["IndieWeb", "Second"])
|
||||
post = only_post(api)
|
||||
permalink = f"https://blog.example.com/note/{note.slug}"
|
||||
assert post["media_type"] == "TEXT"
|
||||
assert post["text"] == f"Hello Threads\n\n{permalink}"
|
||||
assert "link_attachment" not in post # Threads previews the first link itself
|
||||
assert post["topic_tag"] == "IndieWeb"
|
||||
r = row(app, note)
|
||||
assert r["state"] == "synced"
|
||||
assert r["remote_id"] == "p1"
|
||||
assert r["remote_url"] == "https://www.threads.com/@phil/post/p1"
|
||||
|
||||
def test_long_note_fits_500(self, app, api):
|
||||
note = publish(app, content="☕ word " * 300)
|
||||
text = only_post(api)["text"]
|
||||
assert threads_length(text) <= 500
|
||||
assert text.endswith(f"https://blog.example.com/note/{note.slug}")
|
||||
|
||||
def test_waits_for_container(self, app, api):
|
||||
api.pending_polls = 2
|
||||
note = publish(app)
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_edit_leaves_post_alone(self, app, api):
|
||||
note = publish(app, content="first")
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, content="second")
|
||||
process_due()
|
||||
assert only_post(api)["text"].startswith("first")
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_delete_and_unpublish_remove_post(self, app, api):
|
||||
note = publish(app)
|
||||
with app.test_request_context():
|
||||
update_note(id=note.id, published=False)
|
||||
process_due()
|
||||
assert api.posts == {}
|
||||
update_note(id=note.id, published=True)
|
||||
process_due()
|
||||
assert len(api.posts) == 1
|
||||
delete_note(id=note.id)
|
||||
process_due()
|
||||
assert api.posts == {}
|
||||
|
||||
def test_post_deleted_on_threads_is_fine(self, app, api):
|
||||
note = publish(app)
|
||||
api.posts.clear()
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id)
|
||||
process_due()
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
|
||||
class TestErrors:
|
||||
def test_lost_reply_does_not_double_post(self, app, api):
|
||||
api.lose_next_publish = True
|
||||
note = publish(app)
|
||||
assert row(app, note)["state"] == "pending"
|
||||
with app.test_request_context():
|
||||
from starpunk.database import get_db
|
||||
get_db(app).execute("UPDATE syndications SET next_attempt_at = datetime('now')")
|
||||
get_db(app).commit()
|
||||
process_due()
|
||||
assert len(api.posts) == 1
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_publish_fatal_is_retried(self, app, api):
|
||||
api.fail_next_publish = True
|
||||
note = publish(app)
|
||||
r = row(app, note)
|
||||
assert r["state"] == "pending" and "Fatal" in r["last_error"] and "subcode 4279009" in r["last_error"]
|
||||
with app.test_request_context():
|
||||
from starpunk.database import get_db
|
||||
get_db(app).execute("UPDATE syndications SET next_attempt_at = datetime('now')")
|
||||
get_db(app).commit()
|
||||
process_due()
|
||||
assert len(api.posts) == 1
|
||||
assert row(app, note)["state"] == "synced"
|
||||
|
||||
def test_bad_token_fails_permanently(self, app, api):
|
||||
api.token = "other"
|
||||
note = publish(app)
|
||||
r = row(app, note)
|
||||
assert r["state"] == "failed" and "Invalid OAuth" in r["last_error"]
|
||||
assert "tok1" not in r["last_error"]
|
||||
|
||||
def test_rate_limit_retries(self, app, api):
|
||||
publish(app, content="warm up")
|
||||
api.fail_next = (400, 4) # Graph "application request limit reached"
|
||||
note = publish(app, content="later")
|
||||
assert row(app, note)["state"] == "pending"
|
||||
|
||||
|
||||
class TestLinks:
|
||||
def test_markdown_link_comes_first_for_the_preview(self, app, api):
|
||||
note = publish(app, content="Read [this story](https://news.example/story) now")
|
||||
text = only_post(api)["text"]
|
||||
assert text.startswith("Read this story (https://news.example/story) now")
|
||||
assert text.index("https://news.example/story") < text.index(f"/note/{note.slug}")
|
||||
|
||||
def test_stays_within_five_links(self, app, api):
|
||||
import re
|
||||
links = " ".join(f"[l{i}](https://l{i}.example)" for i in range(7))
|
||||
publish(app, content=f"Lots: {links}")
|
||||
text = only_post(api)["text"]
|
||||
assert len(set(re.findall(r"https?://[^\s)]+", text))) == 5
|
||||
assert "l6" in text # the extra links keep their text
|
||||
|
||||
|
||||
class TestMedia:
|
||||
def _note_with(self, app, images):
|
||||
from starpunk.media import attach_media_to_note, save_media
|
||||
with app.test_request_context():
|
||||
note = create_note(content="pics", published=True)
|
||||
ids = [save_media(data, name)["id"] for data, name in images]
|
||||
attach_media_to_note(note.id, ids, [f"alt {i}" for i in range(len(ids))])
|
||||
select_targets(note.id, ["threads"])
|
||||
process_due()
|
||||
return note
|
||||
|
||||
def test_single_image(self, app, api):
|
||||
self._note_with(app, [(image_bytes(), "a.jpg")])
|
||||
post = only_post(api)
|
||||
assert post["media_type"] == "IMAGE"
|
||||
assert post["image_url"].startswith("https://blog.example.com/media/")
|
||||
assert post["alt_text"] == "alt 0"
|
||||
assert "link_attachment" not in post
|
||||
|
||||
def test_carousel(self, app, api):
|
||||
self._note_with(app, [(image_bytes(), "a.jpg"), (image_bytes("PNG"), "b.png")])
|
||||
post = only_post(api)
|
||||
assert post["media_type"] == "CAROUSEL"
|
||||
children = [api.containers[c] for c in post["children"].split(",")]
|
||||
assert [c["is_carousel_item"] for c in children] == ["true", "true"]
|
||||
assert [c["alt_text"] for c in children] == ["alt 0", "alt 1"]
|
||||
|
||||
def test_unsupported_format_is_left_off(self, app, api):
|
||||
self._note_with(app, [(image_bytes("WEBP"), "a.webp")])
|
||||
assert only_post(api)["media_type"] == "TEXT"
|
||||
|
||||
|
||||
class TestToken:
|
||||
def test_refreshes_daily_and_saves(self, app, api):
|
||||
client = syndication._client_cache["threads"]
|
||||
with app.app_context():
|
||||
client.maintain()
|
||||
assert api.refreshes == 1
|
||||
client.maintain() # same day: nothing
|
||||
assert api.refreshes == 1
|
||||
saved = json.loads((app.config["DATA_PATH"] / "threads_token.json").read_text())
|
||||
assert saved["token"] == "tok2"
|
||||
# A restart picks up the saved token
|
||||
assert make_client(app, api)._token == "tok2"
|
||||
publish(app)
|
||||
assert len(api.posts) == 1
|
||||
|
||||
def test_other_worker_refresh_is_reused(self, app, api):
|
||||
first, second = make_client(app, api), make_client(app, api)
|
||||
with app.app_context():
|
||||
first.maintain()
|
||||
second.maintain()
|
||||
assert api.refreshes == 1
|
||||
assert second._token == "tok2"
|
||||
|
||||
def test_refresh_due_after_a_day(self, app, api, monkeypatch):
|
||||
client = syndication._client_cache["threads"]
|
||||
with app.app_context():
|
||||
client.maintain()
|
||||
later = time.time() + THREADS_REFRESH_EVERY + 1
|
||||
monkeypatch.setattr(syndication.time, "time", lambda: later)
|
||||
client.maintain()
|
||||
assert api.refreshes == 2
|
||||
|
||||
def test_failed_refresh_keeps_token(self, app, api):
|
||||
api.refresh_fails = True
|
||||
client = syndication._client_cache["threads"]
|
||||
with app.app_context():
|
||||
client.maintain()
|
||||
assert client._token == "tok1"
|
||||
publish(app)
|
||||
assert len(api.posts) == 1
|
||||
|
||||
def test_new_config_token_replaces_saved_one(self, app, api):
|
||||
with app.app_context():
|
||||
syndication._client_cache["threads"].maintain()
|
||||
api.token = "fresh"
|
||||
assert make_client(app, api, token="fresh")._token == "fresh"
|
||||
|
||||
def test_worker_runs_upkeep(self, app, api):
|
||||
with app.app_context():
|
||||
process_due()
|
||||
assert api.refreshes == 1
|
||||
|
||||
|
||||
def test_all_targets_listed_for_micropub(tmp_path):
|
||||
data = tmp_path / "d"
|
||||
app = create_app({
|
||||
"TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example",
|
||||
"SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "s.db",
|
||||
"GOTOSOCIAL_URL": "https://social.example", "GOTOSOCIAL_ACCESS_TOKEN": "t",
|
||||
"BLUESKY_HANDLE": "phil.example", "BLUESKY_APP_PASSWORD": "p",
|
||||
"THREADS_ACCESS_TOKEN": "x", "THREADS_USERNAME": "@phil",
|
||||
})
|
||||
with app.app_context():
|
||||
from starpunk.syndication import get_targets, resolve_targets
|
||||
assert set(get_targets()) == {"gotosocial", "bluesky", "threads"}
|
||||
assert resolve_targets(["https://www.threads.com/@phil"]) == ["threads"]
|
||||
syndication._client_cache.clear()
|
||||
|
||||
|
||||
class TestLikesReposts:
|
||||
PAGE = "https://other.example/post/1"
|
||||
|
||||
def _post(self, app, monkeypatch, content="", **kind):
|
||||
monkeypatch.setattr("starpunk.reply_context.fetch_reply_context", lambda *a, **k: None)
|
||||
with app.test_request_context():
|
||||
note = create_note(content=content, published=True, **kind)
|
||||
select_targets(note.id, ["threads"])
|
||||
process_due()
|
||||
return note
|
||||
|
||||
def test_like_is_not_crossposted(self, app, api, monkeypatch):
|
||||
note = self._post(app, monkeypatch, like_of=self.PAGE)
|
||||
assert api.posts == {}
|
||||
r = row(app, note)
|
||||
assert r["state"] == "synced" and r["remote_id"] is None
|
||||
|
||||
def test_repost_posts_link(self, app, api, monkeypatch):
|
||||
self._post(app, monkeypatch, repost_of=self.PAGE)
|
||||
post = only_post(api)
|
||||
assert post["text"].startswith("🔁") and self.PAGE in post["text"]
|
||||
assert "link_attachment" not in post
|
||||
|
||||
def test_repost_with_comment_keeps_comment(self, app, api, monkeypatch):
|
||||
note = self._post(app, monkeypatch, content="So **true**", repost_of=self.PAGE)
|
||||
assert only_post(api)["text"].startswith("So true")
|
||||
with app.test_request_context():
|
||||
delete_note(id=note.id)
|
||||
process_due()
|
||||
assert api.posts == {}
|
||||
@@ -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
|
||||
@@ -0,0 +1,283 @@
|
||||
"""
|
||||
W3C Webmention conformance tests (v2.7.0)
|
||||
|
||||
https://www.w3.org/TR/webmention/ — section numbers in test names.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from starpunk import webmention_ssrf
|
||||
from starpunk.webmention_processor import (
|
||||
TransientError, ValidationError, fetch_source, verify_link_presence,
|
||||
)
|
||||
from starpunk.webmention_worker import WebmentionWorker
|
||||
|
||||
PUBLIC = [(2, 1, 6, '', ('93.184.216.34', 0))]
|
||||
BASE = 'https://t.example/test/1?x=1'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def worker(app):
|
||||
return WebmentionWorker(app, db_path=app.config["DATABASE_PATH"],
|
||||
site_url=app.config["SITE_URL"])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_web():
|
||||
"""Route safe_get through a MockTransport; all hosts resolve public"""
|
||||
routes = {}
|
||||
|
||||
def handler(request):
|
||||
return routes[str(request.url)](request)
|
||||
|
||||
with patch("starpunk.webmention_ssrf.socket.getaddrinfo", return_value=PUBLIC), \
|
||||
patch("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(handler)):
|
||||
yield routes
|
||||
|
||||
|
||||
# 3.1.2 Sender discovers receiver Webmention endpoint
|
||||
|
||||
@pytest.mark.parametrize("header,expected", [
|
||||
('<https://e.example/wm>; rel="webmention"', 'https://e.example/wm'),
|
||||
('</wm>; rel=webmention', 'https://t.example/wm'),
|
||||
('<wm>; rel="other webmention"', 'https://t.example/test/wm'),
|
||||
('<wm>; title="a;b,c"; rel="webmention"', 'https://t.example/test/wm'),
|
||||
('<wm>; REL="WebMention"', 'https://t.example/test/wm'),
|
||||
('<a>; rel="other", </wm?q=1>; rel="webmention"', 'https://t.example/wm?q=1'),
|
||||
('<x>; rel="webmention-not"', None),
|
||||
('<x>; rel="not-webmention"', None),
|
||||
])
|
||||
def test_3_1_2_link_header(worker, header, expected):
|
||||
assert worker._parse_link_header(header, BASE) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("html,expected", [
|
||||
# <a> before <link>: first in document order wins
|
||||
('<a rel="webmention" href="/a">x</a><link rel="webmention" href="/l">', 'https://t.example/a'),
|
||||
('<link rel="webmention somethingelse" href="/l">', 'https://t.example/l'),
|
||||
('<link rel="Webmention" href="/l">', 'https://t.example/l'),
|
||||
# empty href is the target itself
|
||||
('<link rel="webmention" href="">', BASE),
|
||||
# no href: skipped
|
||||
('<link rel="webmention"><a rel="webmention" href="/a">', 'https://t.example/a'),
|
||||
# comments, escaped markup and other rels are ignored
|
||||
('<!-- <link rel="webmention" href="/bad"> -->'
|
||||
'<a rel="not-webmention" href="/bad"></a>'
|
||||
'<code><a rel="webmention" href="/bad"></code>'
|
||||
'<link rel="webmention" href="/ok">', 'https://t.example/ok'),
|
||||
('<p>nothing</p>', None),
|
||||
])
|
||||
def test_3_1_2_html(worker, html, expected):
|
||||
assert worker._parse_html_for_endpoint(html, BASE) == expected
|
||||
|
||||
|
||||
def test_3_1_2_header_before_html_and_relative_to_final_url(app, worker, mock_web):
|
||||
mock_web['https://t.example/old'] = lambda r: httpx.Response(
|
||||
301, headers={'Location': 'https://t.example/new/page'})
|
||||
mock_web['https://t.example/new/page'] = lambda r: httpx.Response(
|
||||
200, headers={'Link': '<wm>; rel="webmention"', 'Content-Type': 'text/html'},
|
||||
text='<link rel="webmention" href="/html">')
|
||||
with app.app_context():
|
||||
assert worker._discover_endpoint('https://t.example/old') == 'https://t.example/new/wm'
|
||||
|
||||
|
||||
def test_3_1_2_multiple_link_headers(app, worker, mock_web):
|
||||
mock_web['https://t.example/p'] = lambda r: httpx.Response(
|
||||
200, headers=[('Link', '</other>; rel="other"'), ('Link', '</wm>; rel="webmention"')])
|
||||
with app.app_context():
|
||||
assert worker._discover_endpoint('https://t.example/p') == 'https://t.example/wm'
|
||||
|
||||
|
||||
def test_3_1_2_server_error_is_retried_not_final(app, worker, mock_web):
|
||||
mock_web['https://t.example/p'] = lambda r: httpx.Response(503)
|
||||
with app.app_context(), pytest.raises(httpx.HTTPError):
|
||||
worker._discover_endpoint('https://t.example/p')
|
||||
|
||||
|
||||
# 4.5 SSRF: every redirect hop is checked
|
||||
|
||||
def test_4_5_redirect_to_private_address_blocked(app):
|
||||
def resolve(host, *a, **k):
|
||||
ip = '169.254.169.254' if host == 'metadata.internal' else '93.184.216.34'
|
||||
return [(2, 1, 6, '', (ip, 0))]
|
||||
|
||||
def handler(request):
|
||||
return httpx.Response(302, headers={'Location': 'http://metadata.internal/'})
|
||||
|
||||
with app.app_context(), \
|
||||
patch("starpunk.webmention_ssrf.socket.getaddrinfo", side_effect=resolve), \
|
||||
patch("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(handler)):
|
||||
with pytest.raises(ValidationError, match="Blocked"):
|
||||
fetch_source('https://evil.example/', 'http://localhost:5000')
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ip", [
|
||||
'127.0.0.1', '10.1.2.3', '169.254.169.254', '0.0.0.0', '100.64.0.1',
|
||||
'::1', '::ffff:127.0.0.1', 'fe80::1', '224.0.0.1',
|
||||
])
|
||||
def test_4_5_blocked_addresses(ip):
|
||||
assert webmention_ssrf.is_private_ip(ip)
|
||||
|
||||
|
||||
def test_4_5_any_blocked_address_blocks(app):
|
||||
mixed = PUBLIC + [(10, 1, 6, '', ('::1', 0, 0, 0))]
|
||||
with app.app_context(), patch("starpunk.webmention_ssrf.socket.getaddrinfo", return_value=mixed):
|
||||
with pytest.raises(webmention_ssrf.SSRFBlockedError):
|
||||
webmention_ssrf.validate_url_for_fetch('https://x.example/')
|
||||
|
||||
|
||||
# 3.2.2 Webmention verification
|
||||
|
||||
TARGET = 'https://me.example/note/abc'
|
||||
|
||||
|
||||
@pytest.mark.parametrize("content,content_type,expected", [
|
||||
('<a href="/note/abc#c">x</a>', 'text/html', True),
|
||||
('<img src="https://me.example/note/abc">', 'text/html; charset=utf-8', True),
|
||||
('<a href="https://me.example/note/abcdef">x</a>', 'text/html', False),
|
||||
('<p>https://me.example/note/abc</p>', 'text/html', False),
|
||||
('<!-- <a href="https://me.example/note/abc"> -->', 'text/html', False),
|
||||
('{"a": ["https://me.example/note/abc"]}', 'application/json', True),
|
||||
('{"a": "https://me.example/note/abcdef"}', 'application/json', False),
|
||||
('see https://me.example/note/abc', 'text/plain', True),
|
||||
])
|
||||
def test_3_2_2_exact_match(content, content_type, expected):
|
||||
assert verify_link_presence(content, TARGET, content_type, 'https://me.example/x') is expected
|
||||
|
||||
|
||||
def test_3_2_2_accept_header_and_status(app, mock_web):
|
||||
seen = {}
|
||||
|
||||
def page(request):
|
||||
seen['accept'] = request.headers.get('Accept')
|
||||
return httpx.Response(410)
|
||||
|
||||
mock_web['https://s.example/p'] = page
|
||||
with app.app_context():
|
||||
with pytest.raises(ValidationError, match="410"):
|
||||
fetch_source('https://s.example/p', 'http://localhost:5000')
|
||||
assert 'text/html' in seen['accept']
|
||||
|
||||
mock_web['https://s.example/p'] = lambda r: httpx.Response(500)
|
||||
with app.app_context(), pytest.raises(TransientError):
|
||||
fetch_source('https://s.example/p', 'http://localhost:5000')
|
||||
|
||||
|
||||
# 3.2.1 Request verification
|
||||
|
||||
def _post(client, source, target):
|
||||
return client.post('/webmention', data={'source': source, 'target': target})
|
||||
|
||||
|
||||
def test_3_2_1_target_must_be_a_note(app, client, sample_note):
|
||||
note_url = f"http://localhost:5000/note/{sample_note.slug}"
|
||||
assert _post(client, 'https://s.example/a', note_url).status_code == 202
|
||||
assert _post(client, 'https://s.example/a', 'http://localhost:5000/nope').status_code == 400
|
||||
assert _post(client, 'https://s.example/a', 'http://localhost:5000/').status_code == 400
|
||||
assert _post(client, 'https://s.example/a', 'https://other.example/note/x').status_code == 400
|
||||
assert _post(client, note_url, note_url).status_code == 400
|
||||
assert _post(client, 'ftp://s.example/a', note_url).status_code == 400
|
||||
|
||||
|
||||
def test_3_2_1_host_case_and_fragment(app, client, sample_note):
|
||||
note_url = f"http://LOCALHOST:5000/note/{sample_note.slug}"
|
||||
assert _post(client, 'https://s.example/a', note_url + '#one').status_code == 202
|
||||
assert _post(client, 'https://s.example/a', note_url + '#two').status_code == 202
|
||||
db = sqlite3.connect(app.config['DATABASE_PATH'])
|
||||
rows = db.execute("SELECT target_url FROM webmentions").fetchall()
|
||||
assert rows == [(note_url,)]
|
||||
|
||||
|
||||
# 3.2.4 Updating existing Webmentions
|
||||
|
||||
def _insert_mention(app, note_id, **cols):
|
||||
db = sqlite3.connect(app.config['DATABASE_PATH'])
|
||||
fields = {'source_url': 'https://s.example/a', 'target_url': 'http://localhost:5000/n',
|
||||
'target_note_id': note_id, **cols}
|
||||
db.execute(f"INSERT INTO webmentions ({', '.join(fields)}) VALUES ({', '.join('?' * len(fields))})",
|
||||
tuple(fields.values()))
|
||||
db.commit()
|
||||
return db
|
||||
|
||||
|
||||
VALID = {'valid': True, 'mention_type': 'reply', 'author_name': 'A', 'author_url': 'https://a.example/',
|
||||
'author_photo': None, 'content_snippet': 'hi'}
|
||||
|
||||
|
||||
def test_3_2_4_update_keeps_moderation(app, worker, sample_note):
|
||||
db = _insert_mention(app, sample_note.id, validation_status='valid', moderation_status='hidden',
|
||||
validated_at='2026-01-01')
|
||||
with patch("starpunk.webmention_worker.process_webmention", return_value=VALID):
|
||||
worker._validate_webmention(1, 'https://s.example/a', 'http://localhost:5000/n', sample_note.id)
|
||||
assert db.execute("SELECT validation_status, moderation_status FROM webmentions").fetchone() == ('valid', 'hidden')
|
||||
|
||||
|
||||
def test_3_2_4_new_mention_gets_default_moderation(app, worker, sample_note):
|
||||
db = _insert_mention(app, sample_note.id)
|
||||
with patch("starpunk.webmention_worker.process_webmention", return_value=VALID):
|
||||
worker._validate_webmention(1, 'https://s.example/a', 'http://localhost:5000/n', sample_note.id)
|
||||
assert db.execute("SELECT validation_status, moderation_status FROM webmentions").fetchone() == ('valid', 'approved')
|
||||
|
||||
|
||||
def test_3_2_4_transient_error_keeps_valid_mention(app, worker, sample_note):
|
||||
db = _insert_mention(app, sample_note.id, validation_status='pending', moderation_status='approved',
|
||||
validated_at='2026-01-01', content_snippet='kept')
|
||||
with patch("starpunk.webmention_worker.process_webmention", side_effect=TransientError("timeout")):
|
||||
worker._validate_webmention(1, 'https://s.example/a', 'http://localhost:5000/n', sample_note.id)
|
||||
assert db.execute("SELECT validation_status, content_snippet FROM webmentions").fetchone() == ('valid', 'kept')
|
||||
|
||||
|
||||
def test_3_2_4_gone_source_removes_mention(app, worker, sample_note):
|
||||
db = _insert_mention(app, sample_note.id, validation_status='pending', moderation_status='approved',
|
||||
validated_at='2026-01-01')
|
||||
with patch("starpunk.webmention_worker.process_webmention",
|
||||
side_effect=ValidationError("Source returned 410")):
|
||||
worker._validate_webmention(1, 'https://s.example/a', 'http://localhost:5000/n', sample_note.id)
|
||||
assert db.execute("SELECT validation_status FROM webmentions").fetchone() == ('invalid',)
|
||||
|
||||
|
||||
# 3.1.4 Updated posts / 3.1.5 Deleted posts
|
||||
|
||||
def _queue(app):
|
||||
db = sqlite3.connect(app.config['DATABASE_PATH'])
|
||||
return db.execute(
|
||||
"SELECT target_url, status, source_note_id FROM webmention_queue ORDER BY target_url"
|
||||
).fetchall()
|
||||
|
||||
|
||||
def _sent(app):
|
||||
db = sqlite3.connect(app.config['DATABASE_PATH'])
|
||||
db.execute("UPDATE webmention_queue SET status = 'sent'")
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_3_1_4_update_resends_previous_targets(app):
|
||||
from starpunk.notes import create_note, update_note
|
||||
|
||||
with app.test_request_context():
|
||||
note = create_note("Reply", published=True, in_reply_to="https://a.example/post")
|
||||
_sent(app)
|
||||
update_note(id=note.id, content="Now about [b](https://b.example/x)", in_reply_to="")
|
||||
targets = {t: s for t, s, _ in _queue(app)}
|
||||
assert targets == {'https://a.example/post': 'pending', 'https://b.example/x': 'pending'}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("soft", [True, False])
|
||||
def test_3_1_5_delete_resends_and_returns_410(app, client, soft):
|
||||
from starpunk.notes import create_note, delete_note
|
||||
|
||||
with app.test_request_context():
|
||||
note = create_note("See [a](https://a.example/x)", published=True)
|
||||
_sent(app)
|
||||
delete_note(id=note.id, soft=soft)
|
||||
|
||||
rows = _queue(app)
|
||||
assert [(t, s) for t, s, _ in rows] == [('https://a.example/x', 'pending')]
|
||||
if not soft:
|
||||
assert rows[0][2] is None # detached, so it survives the delete
|
||||
assert client.get(f"/note/{note.slug}").status_code == 410
|
||||
assert client.get("/note/never-existed").status_code == 404
|
||||
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
Webmention worker regression tests (v2.6.8)
|
||||
|
||||
The worker thread used Flask's current_app (via the SSRF checks) without an
|
||||
app context, so every send and validation failed. It was also stopped
|
||||
whenever any app context ended.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
|
||||
import httpx
|
||||
import time
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from starpunk.webmention_worker import WebmentionWorker
|
||||
|
||||
PUBLIC = [(2, 1, 6, '', ('93.184.216.34', 0))]
|
||||
|
||||
|
||||
def _wait_for(predicate, timeout=5):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
if predicate():
|
||||
return True
|
||||
time.sleep(0.05)
|
||||
return False
|
||||
|
||||
|
||||
def test_worker_sends_queued_webmention(app):
|
||||
db_path = app.config["DATABASE_PATH"]
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.execute(
|
||||
"INSERT INTO webmention_queue (source_url, target_url, status) "
|
||||
"VALUES (?, ?, 'pending')",
|
||||
("http://localhost:5000/note/1", "https://example.org/post"),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def target_site(request):
|
||||
return httpx.Response(200, headers={"Link": '</wm>; rel="webmention"'})
|
||||
|
||||
accepted = MagicMock(status_code=202, text="")
|
||||
|
||||
worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"],
|
||||
poll_interval=0.05)
|
||||
with patch("starpunk.webmention_ssrf.socket.getaddrinfo", return_value=PUBLIC), \
|
||||
patch("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(target_site)), \
|
||||
patch("starpunk.webmention_worker.httpx.post", return_value=accepted) as post:
|
||||
worker.start()
|
||||
try:
|
||||
def status():
|
||||
c = sqlite3.connect(db_path)
|
||||
row = c.execute("SELECT status, last_error FROM webmention_queue").fetchone()
|
||||
c.close()
|
||||
return row
|
||||
assert _wait_for(lambda: status()[0] != "pending"), status()
|
||||
assert status() == ("sent", None)
|
||||
post.assert_called_once()
|
||||
assert post.call_args.args[0] == "https://example.org/wm"
|
||||
finally:
|
||||
worker.stop()
|
||||
worker.join(timeout=2)
|
||||
|
||||
|
||||
def test_worker_survives_app_context_teardown(app):
|
||||
worker = WebmentionWorker(app, db_path=app.config["DATABASE_PATH"],
|
||||
site_url=app.config["SITE_URL"], poll_interval=0.05)
|
||||
worker.start()
|
||||
try:
|
||||
with app.app_context():
|
||||
pass
|
||||
app.test_client().get("/")
|
||||
time.sleep(0.2)
|
||||
assert worker.is_alive()
|
||||
finally:
|
||||
worker.stop()
|
||||
worker.join(timeout=2)
|
||||
|
||||
|
||||
def test_only_one_worker_processes_queues(app):
|
||||
"""Each gunicorn process starts a worker; only one may hold the queue lock"""
|
||||
kwargs = dict(db_path=app.config["DATABASE_PATH"], site_url=app.config["SITE_URL"])
|
||||
first = WebmentionWorker(app, **kwargs)
|
||||
second = WebmentionWorker(app, **kwargs)
|
||||
try:
|
||||
assert first._acquire_lock()
|
||||
assert not second._acquire_lock()
|
||||
first._release_lock()
|
||||
assert second._acquire_lock()
|
||||
finally:
|
||||
first._release_lock()
|
||||
second._release_lock()
|
||||
|
||||
|
||||
def test_author_photo_with_alt_text_is_a_string():
|
||||
"""mf2py returns u-photo with alt as a dict; the database needs a URL string"""
|
||||
import mf2py
|
||||
from starpunk.webmention_processor import extract_author
|
||||
|
||||
html = """
|
||||
<div class="h-entry">
|
||||
<span class="p-author h-card">
|
||||
<a class="u-url p-name" href="https://a.example/">A Person</a>
|
||||
<img class="u-photo" src="https://a.example/me.jpg" alt=""></span>
|
||||
<a href="https://site.example/note/1">target</a>
|
||||
</div>"""
|
||||
author = extract_author(mf2py.parse(doc=html, url="https://src.example/"),
|
||||
"https://src.example/")
|
||||
assert author == {
|
||||
"author_name": "A Person",
|
||||
"author_url": "https://a.example/",
|
||||
"author_photo": "https://a.example/me.jpg",
|
||||
}
|
||||
|
||||
|
||||
def test_author_urls_must_be_http():
|
||||
"""A javascript: author URL would be rendered as a clickable link (XSS)"""
|
||||
import mf2py
|
||||
from starpunk.webmention_processor import extract_author
|
||||
|
||||
html = """
|
||||
<div class="h-entry">
|
||||
<span class="p-author h-card">
|
||||
<a class="u-url p-name" href="javascript:alert(1)">Evil</a>
|
||||
<img class="u-photo" src="data:image/svg+xml,x" alt=""></span>
|
||||
</div>"""
|
||||
author = extract_author(mf2py.parse(doc=html, url="https://src.example/"),
|
||||
"https://src.example/")
|
||||
assert author["author_url"] == "https://src.example/"
|
||||
assert author["author_photo"] is None
|
||||
Reference in new issue
Block a user