bundlewrap/bundles/matrix-synapse/files/synapse-purge-unused-rooms

64 lines
1.6 KiB
Python

#!/usr/bin/env python3
from os import environ
from sys import argv, exit
from requests import get, post
SYNAPSE_MAX_ROOMS_TO_GET = 20000
SYNAPSE_HOST = 'http://[::1]:20080/'
if 'MATRIX_AUTH_TOKEN' in environ:
SYNAPSE_AUTH_TOKEN = environ['MATRIX_AUTH_TOKEN']
else:
print('Usage: MATRIX_AUTH_TOKEN=\'your_token_here\' {}'.format(argv[0]))
exit(255)
json = get(
SYNAPSE_HOST + '_synapse/admin/v1/rooms?limit={}'.format(SYNAPSE_MAX_ROOMS_TO_GET),
headers={
'Authorization': 'Bearer {}'.format(SYNAPSE_AUTH_TOKEN),
},
).json()
if 'rooms' not in json:
print(json)
exit(1)
print('Found a total of {} rooms on {}'.format(len(json['rooms']), SYNAPSE_HOST))
print()
for room in json['rooms']:
print(
'Checking {} ({}) ... '.format(
room['room_id'],
room['canonical_alias'],
),
end='',
)
if room['joined_local_members'] > 0:
print('room has local members!')
continue
else:
print('no local members')
print('Room info:')
print(' Name: {}'.format(room['name']))
print(' Members: {}'.format(room['joined_members']))
print(' Creator: {}'.format(room['creator']))
if input('Do you wish to remove this room? [yN]') in ['y', 'Y']:
print(post(
SYNAPSE_HOST + '_synapse/admin/v1/rooms/{}/delete'.format(room['room_id']),
headers={
'Authorization': 'Bearer {}'.format(SYNAPSE_AUTH_TOKEN),
},
json={
'purge': True,
},
).json())
else:
print('Declined.')
print()