2022-12-24 17:38:30 +00:00
#!/bin/sh
#
# Script to fetch all approved webmentions from webmentiond as a big json response.
# Uses POSIX and cURL in CI, also uses any pass/pash-compatible pwmngr otherwise
2023-11-24 02:45:03 +00:00
# The response is cached for 90 minutes. If fetching fresh webmentions fails, fall back to the cached version for up to one day (1440 minutes). If cached webmentions are older than a day, error out.
2022-12-24 17:38:30 +00:00
set -e -u
2023-11-24 02:45:03 +00:00
dirname = " $( dirname " $0 " ) "
curl_wrapper = " $dirname /curl-wrapper.sh "
2022-12-24 17:38:30 +00:00
auth_url = 'https://seirdy.one/webmentions/authenticate/access-key'
webmentions_url = 'https://seirdy.one/webmentions/manage/mentions?limit=9999&status=approved'
2023-11-24 02:45:03 +00:00
webmentions_file = " $dirname /../data/webmentions.json "
2022-12-24 17:38:30 +00:00
2023-11-24 02:45:03 +00:00
stale_after_minutes = 90
fallback_stale_after_minutes = 1440
2022-12-24 17:38:30 +00:00
2023-11-24 02:45:03 +00:00
skip_check = 0
2023-11-24 03:03:40 +00:00
times_run = 0
2023-11-24 02:45:03 +00:00
check_cached_webmentions( ) {
2023-11-24 03:03:40 +00:00
times_run = " $(( times_run + 1 )) "
2023-11-24 02:45:03 +00:00
if [ " $skip_check " = 0 ] ; then
expires_in = " $fallback_stale_after_minutes "
if [ $# -gt 0 ] ; then
expires_in = " $1 "
fi
exit_status = 1
if [ -f " $webmentions_file " ] ; then
old_webmention_file = " $( find " $webmentions_file " -mmin +" $expires_in " ) "
if [ " $old_webmention_file " = "" ] ; then
echo 'Using cached webmentions'
skip_check = 1
exit_status = 0
2023-11-24 04:01:54 +00:00
return 0
2023-11-24 02:45:03 +00:00
fi
2023-11-24 04:01:54 +00:00
return 1
2023-11-24 02:45:03 +00:00
fi
2023-11-24 03:03:40 +00:00
if [ " $exit_status " = 1 ] && [ " $times_run " = 2 ] ; then
echo "Webmentions are outdated. failed to fetch for over a day."
2023-11-24 04:01:54 +00:00
exit " $exit_status "
2023-11-24 02:45:03 +00:00
fi
fi
}
trap check_cached_webmentions EXIT
# Grab my long-lived key (password). We will use this to authenticate.
2022-12-24 17:38:30 +00:00
key( ) {
set +u
if [ -n " $BUILD_SUBMITTER " ] ; then
cat ~/.webmentiond-key
else
pash show webmentiond-ci-key
fi
set -u
}
2023-11-24 02:45:03 +00:00
# Fetch a short-lived access token from my webmention receiver.
2022-12-24 17:38:30 +00:00
token( ) {
2023-11-15 10:31:06 +00:00
key_response = " $( key) "
2023-11-24 02:45:03 +00:00
" $curl_wrapper " -sX POST " $auth_url " -d " key= $key_response "
}
# Verify that the webmentions file has downloaded succesfully by ensuring that it starts and ends with a given substring.
# The total number of webmentions should be at least a 3-digit number.
verify_webmentions( ) {
grep -E '^\{"items":\[\{"id":".*,"total":[0-9]{3}([0-9]*)?\}$' " $webmentions_file .tmp " >/dev/null
2022-12-24 17:38:30 +00:00
}
2023-11-24 02:45:03 +00:00
# use the token to fetch all webmentions.
2022-12-24 17:38:30 +00:00
fetch_webmentions( ) {
2023-11-24 02:45:03 +00:00
echo 'Fetching webmentions'
2023-11-15 10:31:06 +00:00
token_response = " $( token) "
2023-11-24 02:45:03 +00:00
" $curl_wrapper " --compressed -H " Authorization: Bearer $token_response " " $webmentions_url " -o " $webmentions_file .tmp " || return 1
if verify_webmentions; then
mv " $webmentions_file .tmp " " $webmentions_file "
else
echo 'Error: webmentions failed to verify'
exit 1
fi
2022-12-24 17:38:30 +00:00
}
# fetch webmentions if we don't have a fresh copy already.
2023-11-24 02:45:03 +00:00
if ! check_cached_webmentions " $stale_after_minutes " ; then
2023-11-15 07:01:46 +00:00
fetch_webmentions
2022-12-24 17:38:30 +00:00
fi