Skip to content

Commit

Permalink
add automatic Mastodon toot expiration
Browse files Browse the repository at this point in the history
Merge pull request #7 from sdr-enthusiasts/dev
  • Loading branch information
kx1t authored May 1, 2024
2 parents 9fb3c52 + 349a92c commit 5c90d50
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ For predictions on Tropo conditions for your area, please visit the [DX Info Cen
| `MASTODON_LINK_SHIPXPLORER` | If set to `on`, the Mastodon notification will include a link to the vessel on ShipXplorer | empty | no |
| `MASTODON_LINK_MARINETRAFFIC` | If set to `on`, the Mastodon notification will include a link to the vessel on MarineTraffic | empty | no |
| `MASTODON_LINK_VESSELFINDER` | If set to `on`, the Mastodon notification will include a link to the vessel on VesselFinder | empty | no |
| `MASTODON_RETENTION_TIME` | Time (in days) that any Toots to Mastodon will be retained. *) Default if omitted is `7` days. Set to `off` to disable | `7`(days) | no |

*) If you are currently sending notifications to Mastodon, the system will automatically delete any Toots you made to your Mastodon account that are older than 7 days, or whatever value you have set this parameter to. If you don't want this to happen, you MUST set MASTODON_RETENTION_TIME=off in your planefence.config file. Note that ALL posts to your Mastodon account are affected by this, and not just the posts made by VesselAlert. The reasoning behind this parameter: many Mastodon servers are owned and operated by individuals, and the disk storage costs, which can be substantial due to the number of images we are attaching, are often borne out of their own pockets. This is VesselAlert being social and cost-conscious, and we really appreciate your cooperation!

## Discord notifications related parameters

Expand Down
5 changes: 4 additions & 1 deletion rootfs/etc/s6-overlay/scripts/cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ fi
# write data back to file:
source "/usr/share/vesselalert/save_databases"


# we can now safely unlock the VesselDB because we're done changing it:
rm -f "$VESSELDBLOCK"

# clean up Mastodon toots:
/usr/share/vesselalert/masto_expire

"${s6wrap[@]}" echo "[INFO] Cleanup finished. Next cleanup run at $(date -d @$(( $(date +%s) + CLEANUP_EVERY )) ). "

sleep "${CLEANUP_EVERY}"
95 changes: 95 additions & 0 deletions rootfs/usr/share/vesselalert/masto_expire
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/command/with-contenv bash
#shellcheck shell=bash disable=SC1091,SC2174,SC2015,SC2154,SC2207
# -----------------------------------------------------------------------------------
# Copyright 2024 Ramon F. Kolb - licensed under the terms and conditions
# of GPLv3. The terms and conditions of this license are included with the Github
# distribution of this package, and are also available here:
# https://github.com/kx1t/docker-planefence/
#
# This package may incorporate other software and license terms.
# -----------------------------------------------------------------------------------

source /scripts/common

ACCESS_TOKEN="$MASTODON_ACCESS_TOKEN"

MASTODON_SERVER="${MASTODON_SERVER,,}"
[[ -z "${MASTODON_SERVER}" ]] && MASTODON_SERVER="airwaves.social"
[[ "${MASTODON_SERVER:0:7}" == "http://" ]] && MASTODON_SERVER="${MASTODON_SERVER:7}" || true
[[ "${MASTODON_SERVER:0:8}" == "https://" ]] && MASTODON_SERVER="${MASTODON_SERVER:8}" || true
INSTANCE_URL="https://$MASTODON_SERVER"

RETENTION_DAYS="${MASTODON_RETENTION_TIME:-7}"

delete_toot() {
local toot_id="$1"
local result
if ! result="$(curl -s --fail -X DELETE -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/statuses/$toot_id" 2>&1)"; then
echo ""
"${s6wrap[@]}" echo "error deleting $toot_id: $result"
fi
}

if chk_disabled "$MASTODON_RETENTION_TIME"; then
"${s6wrap[@]}" echo "MASTODON_RETENTION_TIME is set to $MASTODON_RETENTION_TIME (disabled); nothing to do!"
exit 0
fi

if [[ -z "$MASTODON_RETENTION_TIME" ]]; then
"${s6wrap[@]}" echo "Warning: MASTODON_RETENTION_TIME not set. Defaulting to $RETENTION_DAYS days."
fi

if [[ -z "$MASTODON_ACCESS_TOKEN" ]]; then
"${s6wrap[@]}" echo "MASTODON_ACCESS_TOKEN not set. Exiting."
exit 1
fi

if [[ -z "$MASTODON_SERVER" ]]; then
"${s6wrap[@]}" echo "MASTODON_SERVER not set. Exiting."
exit 1
fi

unset toot_dates counter last_id
declare -A toot_dates

now="$(date +%s)"

masto_id="$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/accounts/verify_credentials" | jq -r '.id')"
counter=0
while : ; do
expired=0
unexpired=0
oldest=33000000000
newest=0
output=("Indexing Toots")
toots="$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/accounts/$masto_id/statuses?limit=40${last_id:+&max_id=}${last_id}")"
toot_ids=($(jq -r '.[] | .id' <<< "$toots" 2>/dev/null))
if (( ${#toot_ids[@]} == 0)); then
"${s6wrap[@]}" echo "No more Toots; done!"
exit 0
fi
last_id="${toot_ids[-1]}"

output+=("$((counter+1)) - $((counter+${#toot_ids[@]})) (${#toot_ids[@]} toots).")
(( counter+=${#toot_ids[@]} )) || true
for t in "${toot_ids[@]}"; do
if [[ -z "${toot_dates[$t]}" ]]; then
toot_dates[$t]="$(date -d "$(jq -r 'map(select(.id == "'"$t"'"))[].created_at' <<< "$toots")" +%s)"
if (( toot_dates[$t] < oldest )); then oldest="${toot_dates[$t]}"; fi
if (( toot_dates[$t] > newest )); then newest="${toot_dates[$t]}"; fi
if (( (now - toot_dates[$t])/(60*60*24) > RETENTION_DAYS )); then
(( expired++ )) || true
if [[ "$1" == "delete" ]]; then
delete_toot "$t";
fi
else
(( unexpired++ )) || true
fi
else
"${s6wrap[@]}" echo "No more Toots; done!"
exit
fi
done
output+=("($unexpired unexpired; $expired expired; oldest $(date -d "@$oldest") ($(( (now - oldest)/(60*60*24) )) days); newest $(date -d "@$newest") ($(( (now - newest)/(60*60*24) )) days))")
"${s6wrap[@]}" echo "${output[@]}"
done

0 comments on commit 5c90d50

Please sign in to comment.