#!/bin/bash # This script is meant to help deal with several issues with Continuwuity # 1. Clears all pushers... as of now they do not clear when one signs absolutely # 2. Sets all notifications to read # Config 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 export BASE_URL="${MATRIX_BASE_URL}/_matrix/client/v3" export TOKEN="${MATRIX_TOKEN}" echo "=== Removing dangling pushers ===" # Fetch pushers response=$(curl -s -X GET "${BASE_URL}/pushers" \ -H "Authorization: Bearer ${TOKEN}" \ -H "Content-Type: application/json") # Loop over pushers if [ -n "$response" ] && echo "$response" | jq -e '.pushers' >/dev/null; then 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}..." # Delete pusher 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}')" > /dev/null echo " Done." done else echo "No pushers found or failed to fetch pushers." fi echo "" echo "=== Clearing all notifications ===" # Get all rooms 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; then export -f process_room || true process_room() { room_id="$1" ENCODED_ROOM_ID=$(echo -n "$room_id" | jq -sRr @uri) echo "Processing room: ${room_id}" CURRENT_RULE=$(curl -s -X GET "${BASE_URL}/pushrules/global/room/${ENCODED_ROOM_ID}" \ -H "Authorization: Bearer ${TOKEN}") CURRENT_ACTION=$(echo "$CURRENT_RULE" | jq -c '.actions' 2>/dev/null) if [ -z "$CURRENT_ACTION" ] || [ "$CURRENT_ACTION" = "null" ]; then CURRENT_ACTION="default (not explicitly set)" fi echo " [Before] Current setting: $CURRENT_ACTION" PUT_RESULT=$(curl -s -X PUT "${BASE_URL}/pushrules/global/room/${ENCODED_ROOM_ID}" \ -H "Authorization: Bearer ${TOKEN}" \ -H "Content-Type: application/json" \ -d "{\"actions\": [\"dont_notify\"]}") ERRCODE=$(echo "$PUT_RESULT" | jq -r '.errcode // empty' 2>/dev/null) if [ -n "$ERRCODE" ]; then ERROR_MSG=$(echo "$PUT_RESULT" | jq -r '.error // "Unknown error"' 2>/dev/null) echo " [Error] FAILED to set rule: $ERRCODE - $ERROR_MSG" else NEW_RULE=$(curl -s -X GET "${BASE_URL}/pushrules/global/room/${ENCODED_ROOM_ID}" \ -H "Authorization: Bearer ${TOKEN}") NEW_ACTION=$(echo "$NEW_RULE" | jq -c '.actions' 2>/dev/null) echo " [After] New setting verified: $NEW_ACTION" fi 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 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}\"}" > /dev/null ENCODED_LATEST=$(echo -n "$LATEST_EVENT" | jq -sRr @uri) 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 fi } export -f process_room echo "$rooms_response" | jq -r '.joined_rooms[]' | xargs -P 10 -I {} bash -c 'process_room "$@"' _ {} else echo "No joined rooms found or failed to fetch rooms." fi echo "" echo "Process finished."