From c5e9a5bc9355c875052da765cb61ca81ac2956ad Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:30:56 -0500 Subject: [PATCH] Support rendering of user-URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That is, rendering recent posts from a user’s profile! --- fedi2html.sh | 118 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 101 insertions(+), 17 deletions(-) diff --git a/fedi2html.sh b/fedi2html.sh index 821e73e..533fda8 100755 --- a/fedi2html.sh +++ b/fedi2html.sh @@ -65,10 +65,18 @@ render_post() { local responses_data="$2" local POST_TREE_LEVEL="$3" - local ACCOUNT_URL="$(echo "$post_data" | jq -r .account.url)" - local ACCOUNT_ID="$(echo "$post_data" | jq -r .account.fqn)" - local ACCOUNT_NAME="$(echo "$post_data" | jq -r .account.display_name | replace_emojis "$(echo "$post_data" | jq -r '.account')")" - local ACCOUNT_AVATAR="$(echo "$post_data" | jq -r .account.avatar)" + local reblog="" + local acct_data="$(echo "$post_data" | jq -r .reblog.account)" + if test "$acct_data" = "null"; then + acct_data="$(echo "$post_data" | jq -r .account)" + else + reblog="1" + fi + + local ACCOUNT_URL="$(echo "$acct_data" | jq -r .url)" + local ACCOUNT_ID="$(echo "$acct_data" | jq -r .fqn)" + local ACCOUNT_NAME="$(echo "$acct_data" | jq -r .display_name | replace_emojis "$acct_data")" + local ACCOUNT_AVATAR="$(echo "$acct_data" | jq -r .avatar)" local POST_URL="$(echo "$post_data" | jq -r .url)" local POST_DATE="$(echo "$post_data" | jq -r .created_at)" local POST_CONTENT="$(echo "$post_data" | jq -r .content | replace_emojis "$post_data")" @@ -150,7 +158,7 @@ media_attachments() { # Pass a post’s context JSON along stdin; out comes the response_data in JSON. # fetch_post_context $url | context_to_responses context_to_responses() { - jq '.descendants' \ + jq '.descendants' 2> /dev/null \ | jq 'sort_by(.created_at)' \ | maybe_jq_reverse \ | jq -cr '.[]' @@ -166,10 +174,10 @@ statuses_api_request() { api_request="/$api_request" fi - local id="$(url_id "$url")" - local server="$(url_server "$url")" + local id="$(url_post_id "$post_url")" + local server="$(url_server "$post_url")" curl --location --header 'Accept: application/json,application/activity+json' \ - "$server/api/v1/statuses/${id}${api_request}" + "${server}/api/v1/statuses/${id}${api_request}" } @@ -177,7 +185,8 @@ statuses_api_request() { # fetch_post_context $url fetch_post_context() { local url="$1" - statuses_api_request "$url" "context" + statuses_api_request "$url" "context" \ + | context_to_responses } @@ -189,18 +198,50 @@ fetch_post() { } +# Given a user-account URL, request JSON of its posts. +# fetch_post $url +fetch_user_posts() { + local url="$1" + local server="$(url_server "$url")" + curl --location --header 'Accept: application/json,application/activity+json' \ + "$server/api/v1/accounts/$(url_user_id "$url")/statuses?exclude_reblogs" \ + | jq -c '.[]' +} + + +# Return the ID of a user, based on its URL. +# url_user_id $url +url_user_id() { + local url="$1" + # Pleroma-style URLs: https://jam.xwx.moe/users/Tirifto + # Mastodon-style URLs: https://esperanto.masto.host/@jubiloEO + if echo "$url" | grep "/users/" > /dev/null; then + echo "$url" \ + | sed 's%.*/users/%%' + elif echo "$url" | grep -E "/@[[:alnum:]]+[/]*$" > /dev/null; then + echo "$url" \ + | sed 's%.*/@%%' \ + | sed 's%/$%%' + else + return 1 + fi +} + + # Return the ID of a post, based on its URL. -# url_id $url -url_id() { +# url_post_id $url +url_post_id() { local url="$1" # Pleroma-style URLs: https://jam.xwx.moe/notice/Ac6PIZAP0ZzkMTYBBg # Mastodon-style URLs: https://esperanto.masto.host/@minjo/111461250815264185 if echo "$url" | grep "/notice/" > /dev/null; then echo "$url" \ | sed 's%.*/notice/%%' - else + elif echo "$url" | grep -E "/@[[:alnum:]]+/[[:digit:]]+" > /dev/null; then echo "$url" \ | sed 's%.*/@[[:alnum:]]*/%%' + else + return 1 fi } @@ -255,6 +296,44 @@ maybe_jq_reverse() { } +# Render a user’s posts, one-by-one, taking into account cli arguments. +# handle_user_url https://jam.xwx.moe/users/tirifto +handle_user_url() { + local url="$1" + local user_posts="$(fetch_user_posts "$url")" + local IFS=" +" + echo "$user_posts" > jadedctrl.json + for post in $user_posts; do + local url="$(echo "$post" | jq -r '.url')" + if test -z "$NO_RESPONSES"; then + context="$(fetch_post_context "$url")" + if test -n "$NO_PARENT"; then + render_responses "$post" "$context" 0 + else + render_post "$post" "$context" 0 + fi + else + render_post "$post" "" 0 + fi + done +} + + +# Render a post and/or its responses, taking into account cli arguments. +handle_post_url() { + local url="$1" + local post="$(fetch_post "$url")" + local context="$(fetch_post_context "$url")" + + if test -n "$NO_PARENT"; then + render_responses "$post" "$context" 0 + else + render_post "$post" "$context" 0 + fi +} + + usage() { echo "usage: $(basename "$0") [-h] [-IR] POST_URL" echo @@ -304,12 +383,17 @@ if test -z "$URL"; then fi -POST="$(fetch_post "$URL")" -RESPONSES="$(fetch_post_context "$URL" | context_to_responses)" -if test -n "$NO_PARENT"; then - render_responses "$POST" "$RESPONSES" 0 + +USER_ID="$(url_user_id "$URL")" +POST_ID="$(url_post_id "$URL")" +if test -n "$POST_ID"; then + echo "POST" + handle_post_url "$URL" +elif test -n "$USER_ID"; then + handle_user_url "$URL" else - render_post "$POST" "$RESPONSES" 0 + echo "That URL is not recognized as a post or user URL." 1>&2 + exit 3 fi