update matrix-remove-empty-rooms.sh

This commit is contained in:
creme 2024-12-26 16:10:00 +01:00
parent df1d28aba7
commit baecabda4d
No known key found for this signature in database
GPG Key ID: C147C3B7FBDF08D0

View File

@ -1,11 +1,9 @@
#!/usr/bin/env bash
# this script removed all room's without local members.
# you dont need to stop synapse.
#
DOMAIN=''
TOKEN=''
tmp_file='/tmp/matrix_rooms_to_purge.txt'
domain=''
token=''
print_usage() {
printf '%s\n\n' "${0##*/}"
@ -20,27 +18,29 @@ print_usage() {
while getopts ":d:t:h" opt "${@}"; do
case $opt in
d) DOMAIN="$OPTARG" ;;
t) TOKEN="$OPTARG" ;;
d) domain="$OPTARG" ;;
t) token="$OPTARG" ;;
\?) printf 'Invalid option: -%s\n\n' "$OPTARG" ; print_usage ;;
h|*) print_usage ;;
esac
done
if [ -z "$DOMAIN" ] || [ "$DOMAIN" == '-t' ]; then print_usage; fi
if [ -z "$TOKEN" ] || [ "$TOKEN" == '-d' ]; then print_usage; fi
if [ -z "$domain" ] || [ "$domain" == '-t' ]; then print_usage; fi
if [ -z "$token" ] || [ "$token" == '-d' ]; then print_usage; fi
curl -s -X GET -H "Authorization: Bearer $TOKEN" "https://$DOMAIN/_synapse/admin/v1/rooms?limit=1000000" | \
jq -Mr '.rooms[] | select(.joined_local_members == 0) | .room_id' > $tmp_file
TOPURGE=$(curl -s -X GET -H "Authorization: Bearer $token" -H "Content-Type: application/json" -d '{}' \
'https://'"$domain"'/_synapse/admin/v1/rooms?limit=1000000' | jq -Mr '.rooms[] | select(.joined_local_members == 0) | .room_id')
while IFS= read -r room_id
for room in $TOPURGE
do
printf 'remove room %s ' "$room_id"
curl -s -X DELETE -H "Authorization: Bearer $TOKEN" "https://$DOMAIN/_synapse/admin/v1/rooms/$room_id" \
-H "Content-Type: application/json" -d "{}"
printf ' done.\n'
done < $tmp_file
printf 'processing room %s ..\n' "$room"
curl -w "\nResponse code: %{response_code}\n\n" -s \
-X DELETE \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d '{}' \
'https://'"$domain"'/_synapse/admin/v1/rooms/'"$room"''
done
rm $tmp_file
#
exit 0