Files
MatrixHelpers/notification_purge.sh

101 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Maintenance script for Continuwuity
# 1. Clears all pushers (they don't clear on sign-out)
# 2. Marks all rooms as read
if [ -z "$MATRIX_BASE_URL" ] || [ -z "$MATRIX_TOKEN" ]; then
echo "Error: MATRIX_BASE_URL and MATRIX_TOKEN environment variables must be set."
exit 1
fi
BASE_URL="${MATRIX_BASE_URL}/_matrix/client/v3"
TOKEN="${MATRIX_TOKEN}"
echo "=== Removing dangling pushers ==="
response=$(curl -s -X GET "${BASE_URL}/pushers" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json")
if [ -n "$response" ] && echo "$response" | jq -e '.pushers' >/dev/null 2>&1; then
count=$(echo "$response" | jq '.pushers | length')
echo "Found ${count} pusher(s)."
echo "$response" | jq -c '.pushers[]' | while read -r pusher; do
app_id=$(echo "$pusher" | jq -r '.app_id')
pushkey=$(echo "$pusher" | jq -r '.pushkey')
echo "Removing pusher: ${app_id} (${pushkey})..."
result=$(curl -s -X POST "${BASE_URL}/pushers/set" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg id "$app_id" --arg key "$pushkey" \
'{app_id: $id, pushkey: $key, kind: null}')")
errcode=$(echo "$result" | jq -r '.errcode // empty' 2>/dev/null)
if [ -n "$errcode" ]; then
echo " Failed: ${errcode} - $(echo "$result" | jq -r '.error // "Unknown"')"
else
echo " Done."
fi
done
# Verify all pushers are gone
verify=$(curl -s -X GET "${BASE_URL}/pushers" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json")
remaining=$(echo "$verify" | jq '.pushers | length' 2>/dev/null)
echo "Remaining pushers: ${remaining:-unknown}"
else
echo "No pushers found or failed to fetch pushers."
fi
echo ""
echo "=== Clearing all notifications (marking rooms as read) ==="
rooms_response=$(curl -s -X GET "${BASE_URL}/joined_rooms" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json")
if [ -n "$rooms_response" ] && echo "$rooms_response" | jq -e '.joined_rooms' >/dev/null 2>&1; then
room_count=$(echo "$rooms_response" | jq '.joined_rooms | length')
echo "Found ${room_count} joined room(s)."
process_room() {
local room_id="$1"
local BASE_URL="$2"
local TOKEN="$3"
local ENCODED_ROOM_ID
ENCODED_ROOM_ID=$(echo -n "$room_id" | jq -sRr @uri)
LATEST_EVENT=$(curl -s -X GET "${BASE_URL}/rooms/${ENCODED_ROOM_ID}/messages?dir=b&limit=1" \
-H "Authorization: Bearer ${TOKEN}" | jq -r '.chunk[0].event_id // empty')
if [ -n "$LATEST_EVENT" ]; then
ENCODED_LATEST=$(echo -n "$LATEST_EVENT" | jq -sRr @uri)
curl -s -X POST "${BASE_URL}/rooms/${ENCODED_ROOM_ID}/read_markers" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"m.fully_read\": \"${LATEST_EVENT}\", \"m.read\": \"${LATEST_EVENT}\", \"m.read.private\": \"${LATEST_EVENT}\"}" > /dev/null
curl -s -X POST "${BASE_URL}/rooms/${ENCODED_ROOM_ID}/receipt/m.read/${ENCODED_LATEST}" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d "{}" > /dev/null
echo "Marked as read: ${room_id}"
else
echo "No events in: ${room_id}"
fi
}
export -f process_room
echo "$rooms_response" | jq -r '.joined_rooms[]' | \
xargs -P 10 -I {} bash -c 'process_room "$@"' _ {} "$BASE_URL" "$TOKEN"
else
echo "No joined rooms found or failed to fetch rooms."
fi
echo ""
echo "Process finished."