Files
raucao 6750c61d87 Add Nostr photo feeds and wallpaper selection
Read the curated photo feeds from wss://nostr.kosmos.org, verify events, cache originals and thumbnails, and let users pick a wallpaper from the Photos section. Nature is the default feed; City Nights is opt-in. Adds the manual publishing scripts, real event fixtures, protocol documentation, and tests with stubbed network connections.
2026-11-08 21:51:23 +04:00

159 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sign and publish the replaceable kind-30006 feed events.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib.sh"
require_publish_tools
validate_manifest
ensure_workdirs
parse_common_args "$@"
PHOTO_STATE="$STATE_DIR/photos.json"
FEED_STATE="$STATE_DIR/feeds.json"
state_init "$PHOTO_STATE"
state_init "$FEED_STATE"
info "Building event drafts (feed drafts require published photo IDs)."
"$SCRIPT_DIR/build-events.sh"
to_publish=()
for feed in $(feed_ids); do
draft="$EVENTS_DIR/feeds/$(feed_slug "$feed").json"
[[ -f "$draft" ]] || die "missing feed draft for $feed; publish the photos first"
# The e list may only contain photo event IDs confirmed published on the relay.
while IFS= read -r order_file; do
photo_id="$(jq -r --arg f "$order_file" '.[$f].id // empty' "$PHOTO_STATE")"
[[ -n "$photo_id" ]] || die "feed $feed references a photo with no published event ID: $order_file"
[[ -n "$(relay_photo_by_id "$photo_id")" ]] || die "feed $feed references a photo that is not on the relay: $order_file ($photo_id)"
done < <(jq -r --arg f "$feed" '.feeds[] | select(.id == $f) | .order[]' "$MANIFEST")
existing="$(relay_feed "$feed")"
if [[ -n "$existing" && "$FORCE" != "1" ]]; then
current_summary="$(jq -S -c '{
title: ([.tags[] | select(.[0] == "title")][0][1] // ""),
description: ([.tags[] | select(.[0] == "description")][0][1] // ""),
image: ([.tags[] | select(.[0] == "image")][0][1] // ""),
e: [.tags[] | select(.[0] == "e") | .[1]]
}' <<<"$existing")"
draft_summary="$(jq -S -c '{
title: ([.tags[] | select(.[0] == "title")][0][1] // ""),
description: ([.tags[] | select(.[0] == "description")][0][1] // ""),
image: ([.tags[] | select(.[0] == "image")][0][1] // ""),
e: [.tags[] | select(.[0] == "e") | .[1]]
}' "$draft")"
if [[ "$current_summary" == "$draft_summary" ]]; then
id="$(jq -r '.id' <<<"$existing")"
created_at="$(jq -r '.created_at' <<<"$existing")"
e_ids="$(jq -c '[.tags[] | select(.[0] == "e") | .[1]]' <<<"$existing")"
tmp="$FEED_STATE.tmp"
jq --arg f "$feed" --arg id "$id" --arg pubkey "$CURATOR" \
--argjson created_at "$created_at" --argjson e_ids "$e_ids" \
'.[$f] = {id: $id, pubkey: $pubkey, created_at: $created_at, e_ids: $e_ids}' \
"$FEED_STATE" > "$tmp"
mv "$tmp" "$FEED_STATE"
info "skip (already current): $feed -> $id"
continue
fi
fi
to_publish+=("$feed")
done
if [[ "${#to_publish[@]}" -eq 0 ]]; then
info "Nothing to publish. All feeds are already current."
exit 0
fi
info ""
info "Will publish ${#to_publish[@]} feed event(s) as $CURATOR to $RELAY:"
for feed in "${to_publish[@]}"; do
info " $feed ($(feed_field "$feed" title))"
done
info ""
if [[ "${#NAK_PROMPT_ARGS[@]}" -gt 0 ]]; then
info "nak will prompt for the curator secret key for each feed signature, and again to authenticate the relay connection."
info "Paste the nsec/hex when prompted; it is used only by nak and never stored."
else
info "NOSTR_SECRET_KEY is set; nak will use it from the environment (no prompts)."
fi
if ! confirm "Publish these feed events now?"; then
info "Aborted."
exit 1
fi
for feed in "${to_publish[@]}"; do
slug="$(feed_slug "$feed")"
draft="$EVENTS_DIR/feeds/$slug.json"
signed="$EVENTS_DIR/feeds/$slug.signed.json"
published="$EVENTS_DIR/feeds/$slug.published.json"
[[ -f "$draft" ]] || die "missing feed draft: $draft"
if [[ "$FORCE" == "1" ]]; then
rm -f "$signed" "$published"
fi
if [[ -f "$signed" ]]; then
if ! jq -e --arg pk "$CURATOR" '.kind == 30006 and .pubkey == $pk and (.id | length > 0)' "$signed" >/dev/null 2>&1; then
warn "ignoring invalid/foreign signed feed: $signed"
rm -f "$signed"
elif [[ "$(event_body "$signed")" != "$(event_body "$draft")" ]]; then
warn "signed feed no longer matches the draft; re-signing: $feed"
rm -f "$signed" "$published"
fi
fi
if [[ ! -f "$signed" ]]; then
info ""
info "Signing feed: $feed ($(feed_field "$feed" title))"
if [[ "${#NAK_PROMPT_ARGS[@]}" -gt 0 ]]; then
info "nak is asking for the curator secret key now."
fi
if [[ "$DRY_RUN" == "1" ]]; then
print_cmd nak event "${NAK_PROMPT_ARGS[@]}" "<" "$draft" ">" "$signed"
continue
fi
if ! nak event "${NAK_PROMPT_ARGS[@]}" < "$draft" > "$signed"; then
rm -f "$signed"
die "signing failed for feed $feed"
fi
if ! jq -e --arg pk "$CURATOR" '.kind == 30006 and .pubkey == $pk and (.id | length > 0)' "$signed" >/dev/null; then
rm -f "$signed"
die "signed feed $feed is not kind 30006 or was not signed by the curator ($CURATOR); nothing was published"
fi
fi
id="$(jq -r '.id' "$signed")"
info "Publishing $id ..."
if [[ "$DRY_RUN" == "1" ]]; then
print_cmd nak event --auth "${NAK_PROMPT_ARGS[@]}" "$RELAY" "<" "$signed" ">" "$published"
continue
fi
if ! nak event --auth "${NAK_PROMPT_ARGS[@]}" "$RELAY" < "$signed" > "$published"; then
die "relay publish failed for feed $feed ($id)"
fi
if [[ -z "$(relay_feed "$feed")" ]]; then
die "relay verification failed for feed $feed ($id)"
fi
created_at="$(jq -r '.created_at' "$signed")"
e_ids="$(jq -c '[.tags[] | select(.[0] == "e") | .[1]]' "$signed")"
tmp="$FEED_STATE.tmp"
jq --arg f "$feed" --arg id "$id" --arg pubkey "$CURATOR" \
--argjson created_at "$created_at" --argjson e_ids "$e_ids" \
'.[$f] = {id: $id, pubkey: $pubkey, created_at: $created_at, e_ids: $e_ids}' \
"$FEED_STATE" > "$tmp"
mv "$tmp" "$FEED_STATE"
info "published: $feed -> $id"
done
info ""
info "Feed publishing complete."