Support rendering of user-URLs

That is, rendering recent posts from a user’s
profile!
This commit is contained in:
Jaidyn Ann 2024-04-03 18:30:56 -05:00
parent b6153d4df2
commit c5e9a5bc93

View File

@ -65,10 +65,18 @@ render_post() {
local responses_data="$2" local responses_data="$2"
local POST_TREE_LEVEL="$3" local POST_TREE_LEVEL="$3"
local ACCOUNT_URL="$(echo "$post_data" | jq -r .account.url)" local reblog=""
local ACCOUNT_ID="$(echo "$post_data" | jq -r .account.fqn)" local acct_data="$(echo "$post_data" | jq -r .reblog.account)"
local ACCOUNT_NAME="$(echo "$post_data" | jq -r .account.display_name | replace_emojis "$(echo "$post_data" | jq -r '.account')")" if test "$acct_data" = "null"; then
local ACCOUNT_AVATAR="$(echo "$post_data" | jq -r .account.avatar)" 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_URL="$(echo "$post_data" | jq -r .url)"
local POST_DATE="$(echo "$post_data" | jq -r .created_at)" local POST_DATE="$(echo "$post_data" | jq -r .created_at)"
local POST_CONTENT="$(echo "$post_data" | jq -r .content | replace_emojis "$post_data")" local POST_CONTENT="$(echo "$post_data" | jq -r .content | replace_emojis "$post_data")"
@ -150,7 +158,7 @@ media_attachments() {
# Pass a posts context JSON along stdin; out comes the response_data in JSON. # Pass a posts context JSON along stdin; out comes the response_data in JSON.
# fetch_post_context $url | context_to_responses # fetch_post_context $url | context_to_responses
context_to_responses() { context_to_responses() {
jq '.descendants' \ jq '.descendants' 2> /dev/null \
| jq 'sort_by(.created_at)' \ | jq 'sort_by(.created_at)' \
| maybe_jq_reverse \ | maybe_jq_reverse \
| jq -cr '.[]' | jq -cr '.[]'
@ -166,10 +174,10 @@ statuses_api_request() {
api_request="/$api_request" api_request="/$api_request"
fi fi
local id="$(url_id "$url")" local id="$(url_post_id "$post_url")"
local server="$(url_server "$url")" local server="$(url_server "$post_url")"
curl --location --header 'Accept: application/json,application/activity+json' \ 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 $url
fetch_post_context() { fetch_post_context() {
local url="$1" 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. # Return the ID of a post, based on its URL.
# url_id $url # url_post_id $url
url_id() { url_post_id() {
local url="$1" local url="$1"
# Pleroma-style URLs: https://jam.xwx.moe/notice/Ac6PIZAP0ZzkMTYBBg # Pleroma-style URLs: https://jam.xwx.moe/notice/Ac6PIZAP0ZzkMTYBBg
# Mastodon-style URLs: https://esperanto.masto.host/@minjo/111461250815264185 # Mastodon-style URLs: https://esperanto.masto.host/@minjo/111461250815264185
if echo "$url" | grep "/notice/" > /dev/null; then if echo "$url" | grep "/notice/" > /dev/null; then
echo "$url" \ echo "$url" \
| sed 's%.*/notice/%%' | sed 's%.*/notice/%%'
else elif echo "$url" | grep -E "/@[[:alnum:]]+/[[:digit:]]+" > /dev/null; then
echo "$url" \ echo "$url" \
| sed 's%.*/@[[:alnum:]]*/%%' | sed 's%.*/@[[:alnum:]]*/%%'
else
return 1
fi fi
} }
@ -255,6 +296,44 @@ maybe_jq_reverse() {
} }
# Render a users 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() { usage() {
echo "usage: $(basename "$0") [-h] [-IR] POST_URL" echo "usage: $(basename "$0") [-h] [-IR] POST_URL"
echo echo
@ -304,12 +383,17 @@ if test -z "$URL"; then
fi 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 else
render_post "$POST" "$RESPONSES" 0 echo "That URL is not recognized as a post or user URL." 1>&2
exit 3
fi fi