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