Use ?min_id parameter for timeline requests

This lets the server return only posts created
since our latest-quoted post in saved history.
This commit is contained in:
Jaidyn Ann 2024-04-10 18:41:47 -05:00
parent 7f82bb79ee
commit 4b306d34cb

View File

@ -43,6 +43,22 @@ usage() {
} }
# Fetch a JSON array of posts of a hashtag. If a minimum post ID is provided,
# only posts older than that one will be returned.
# fetch_hashtag_posts $server $hashtag $minimum_id
fetch_hashtag_posts() {
local server="$1"
local hashtag="$2"
local minimum_id="$3"
if test -n "$most_recent_post_id"; then
curl --fail "$server/api/v1/timelines/tag/$hashtag?min_id=$minimum_id"
else
curl --fail "$server/api/v1/timelines/tag/$hashtag"
fi
}
# Given a posts JSON, create and submit a quote-post for it. # Given a posts JSON, create and submit a quote-post for it.
# quote_post $tagged_post_json # quote_post $tagged_post_json
quote_post() { quote_post() {
@ -77,6 +93,7 @@ post_json() {
local tagged_post_json="$1" local tagged_post_json="$1"
printf '{ "content_type": "text/html", "visibility": "unlisted",' printf '{ "content_type": "text/html", "visibility": "unlisted",'
printf '"quote_id": "%s",' "$(echo "$tagged_post_json" | jq -r .id)" printf '"quote_id": "%s",' "$(echo "$tagged_post_json" | jq -r .id)"
printf '"expires_in": %s,' 864000
printf '"status": "%s" }\n' "$(post_body "$tagged_post_json")" printf '"status": "%s" }\n' "$(post_body "$tagged_post_json")"
} }
@ -96,14 +113,15 @@ post_body() {
} }
# Given a “filter” file — a newline-delimited list of post IDs that should not # Receives posts in a JSON array over stdin. Given a newline-delimited list of
# be quoted — filter out all according posts send over stdin. # post IDs ($ids_to_filter), all matching posts from the JSON array will be
# echo $post_array_json | filter_posts $history_file # filted out.# sent over stdin that match one of these posts IDs will be filtered out.
# echo $post_array_json | filter_posts $ids_to_filter
filter_posts() { filter_posts() {
local history_file="$1" local ids_to_filter="$1"
local ids_to_filter="$(history_post_ids "$history_file")" # If the list has more than give characters, use it to filter… (sanity check)
# If the history file exists and has more than five characters, use it to filter… (sanity check) if test -n "$ids_to_filter" \
if test -n "$ids_to_filter" -a "$(wc --bytes "$ids_to_filter" | awk '{print $1}')" -gt 5; then -a "$(echo "$ids_to_filter" | wc --bytes | awk '{print $1}')" -gt 5; then
jq -cr .[] \ jq -cr .[] \
| grep --invert-match $(echo "$ids_to_filter" | sed 's%^%-e %') | grep --invert-match $(echo "$ids_to_filter" | sed 's%^%-e %')
else else
@ -115,9 +133,7 @@ filter_posts() {
history_post_ids() { history_post_ids() {
local history_file="$1" local history_file="$1"
if test -f "$history_file"; then if test -f "$history_file"; then
sort -n "$history_file" \ grep -v '^[[:space:]]*$' "$history_file" \
| uniq \
| grep -v '^[[:space:]]*$' \
| awk '{ print $2 }' | awk '{ print $2 }'
fi fi
} }
@ -129,7 +145,7 @@ history_post_ids() {
# echo $newline_delimited_post_jsons | update_filter $history_file # echo $newline_delimited_post_jsons | update_filter $history_file
update_filter() { update_filter() {
local history_file="$1" local history_file="$1"
if test -f "$history_file"; then if test -n "$history_file"; then
jq -r '(.created_at + "\t" + .id)' \ jq -r '(.created_at + "\t" + .id)' \
| sort -n \ | sort -n \
>> "$history_file" >> "$history_file"
@ -160,7 +176,6 @@ env_subst() {
} }
while getopts 'hH:' arg; do while getopts 'hH:' arg; do
case $arg in case $arg in
h) h)
@ -202,7 +217,10 @@ if test -z "$FEDI_SERVER"; then
fi fi
curl "$FEDI_SERVER/api/v1/timelines/tag/$HASHTAG" \ POST_IDS_TO_IGNORE="$(history_post_ids "$HISTORY_FILE")"
| filter_posts "$HISTORY_FILE" \ MOST_RECENT_POST="$(echo "$POST_IDS_TO_IGNORE" | tail -1)"
fetch_hashtag_posts "$FEDI_SERVER" "$HASHTAG" "$MOST_RECENT_POST" \
| filter_posts "$POST_IDS_TO_IGNORE" \
| quote_posts \ | quote_posts \
| update_filter "$HISTORY_FILE" | update_filter "$HISTORY_FILE"