This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclean_matrix
executable file
·148 lines (129 loc) · 5.89 KB
/
clean_matrix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# get token by command:
# curl -XPOST -d '{"type":"m.login.password", "user":"@USER:server.matrix4ulm.de", "password":"PASS"}' "http://server.matrix4ulm.de:8008/_matrix/client/r0/login"
# user must be HS admin
# KEEP SECRET!
token=""
db_password=""
# KEEP SECRET!
# ******************
# abort script on first error
set -e
set -u
# get date one year back in UNIX millis
purge_ts_1y=`date --date='1 year ago' +%s%3N`
# get date three months back in UNIX millis
purge_ts_3m=`date --date='3 months ago' +%s%3N`
# config output colors
red=`tput setaf 1`
green=`tput setaf 2`
col_reset=`tput sgr0`
# check for https://github.com/matrix-org/rust-synapse-compress-state/ binary
if ! [ -f synapse-compress-state ]; then
echo "(E) ${red}binary "synapse-compress-state" missing. Stop.${col_reset}"
echo "(I) Go to https://github.com/matrix-org/rust-synapse-compress-state/"
exit 1
fi
# Get rooms
echo "${green}(I) get roomlist ${col_reset}"
curl --header "Authorization: Bearer ${token}" 'http://localhost:8008/_synapse/admin/v1/rooms?limit=700' > roomlist.json
# Filter for 0-user-rooms
echo "${green}(I) filter for 0-user-rooms ${col_reset}"
jq '.rooms[] | select(.joined_local_members == 0) | .room_id' < roomlist.json > local_0user_rooms.txt
# count lines
zerouserrooms=`wc -l local_0user_rooms.txt | awk '{ print $1 }'`
echo "${green}(I) there are ${zerouserrooms} 0-user-rooms ${col_reset}"
# recurse through file list
while read line;
do
# remove quotes
line=${line:1: -1}
echo "${green}(I) deleting 0-user-room ${line} ${col_reset}"
curl --header "Authorization: Bearer ${token}" -XPOST -H "Content-Type: application/json" -d '{ }' "http://localhost:8008/_synapse/admin/v1/rooms/${line}/delete"
echo
done < local_0user_rooms.txt
# find all local rooms
jq -r '.rooms[] | select(.room_id | endswith("server.matrix4ulm.de")) | [.room_id, .name] | @tsv' < roomlist.json > local_rooms.txt
local_rooms_count=`wc -l local_rooms.txt | awk '{ print $1 }'`
echo "${green}(I) there are ${local_rooms_count} local rooms ${col_reset}"
# purge history of local rooms
# recurse through room list
while read line;
do
echo "${green}(I) deleting history 1 y back of local rooms ${line} ${col_reset}"
room_id=`echo ${line} | awk '{ print $1 }'`
curl --header "Authorization: Bearer ${token}" -X POST -H "Content-Type: application/json" -d '{ "delete_local_events": true, "purge_up_to_ts": '${purge_ts_1y}' }' "http://localhost:8008/_synapse/admin/v1/purge_history/${room_id}"
echo
done < local_rooms.txt
# purge local media
echo "${green}(I) deleting all local media older than 1 year${col_reset}"
curl --header "Authorization: Bearer ${token}" -XPOST "http://localhost:8008/_synapse/admin/v1/media/server.matrix4ulm.de/delete?before_ts=${purge_ts_1y}"
echo "${green}(I) deleting all local media older than 3 months and bigger than 5 MB${col_reset}"
curl --header "Authorization: Bearer ${token}" -XPOST "http://localhost:8008/_synapse/admin/v1/media/server.matrix4ulm.de/delete?before_ts=${purge_ts_3m}&size_gt=5000000"
# purge remote media
echo "${green}(I) deleting all remote media older than 3 months${col_reset}"
curl --header "Authorization: Bearer ${token}" -XPOST "http://localhost:8008/_synapse/admin/v1/purge_media_cache?before_ts=${purge_ts_3m}"
# calculate db updates
echo "${green}(I) writing necessary db changes to db_change.sql${col_reset}"
# init
>db_change.sql
while read line;
do
echo "${green}(I) ... for room ${line} ${col_reset}"
room_id=`echo ${line} | awk '{ print $1 }'`
echo ${room_id}
./synapse-compress-state -p "host=localhost user=synapse_user password=${db_password} dbname=synapse" -r "${room_id}" -o out.sql -t
# append
cat out.sql >> db_change.sql
done < local_rooms.txt
# get all local users
curl --header "Authorization: Bearer ${token}" -XGET "http://localhost:8008/_synapse/admin/v2/users?guests=false" > local_users.json
jq '.users[].name' < local_users.json > local_users.txt
local_users_count=`wc -l local_users.txt | awk '{ print $1 }'`
echo "${green}(I) there are ${local_users_count} local users${col_reset}"
# init
>inactive_users.txt
# recurse through user list
while read line;
do
# remove quotes
line=${line:1: -1}
echo "${green}(I) Get info on ${line} ${col_reset}"
curl --header "Authorization: Bearer ${token}" -XGET "http://localhost:8008/_synapse/admin/v2/users/${line}/devices" > user.json
# check for number of devices
if [ `jq '.total' user.json` == 0 ]
then
echo "${red}user ${line} has NO DEVICES${col_reset}"
echo ${line} - no devices >> inactive_users.txt
# erase user with no devices
#curl --header "Authorization: Bearer ${token}" -XPOST -H "Content-Type: application/json" -d '{ "erase": true }' "http://localhost:8008/_synapse/admin/v1/deactivate/${line}"
else
# extract last_seen_ts and sort highest number to the top
jq -e '.devices[].last_seen_ts' user.json | sort -nr > last_seen.txt
# take first value and compare to unix millis
read -r firstline<last_seen.txt
if (( firstline > purge_ts_1y ))
then
echo "${green}${firstline} > ${purge_ts_1y}: user ${line} is active${col_reset}"
else
echo "${red}${firstline} < ${purge_ts_1y}: user ${line} is INACTIVE${col_reset}"
echo ${line} - inactive >> inactive_users.txt
# erase inactive users
#curl --header "Authorization: Bearer ${token}" -XPOST -H "Content-Type: application/json" -d '{ "erase": true }' "http://localhost:8008/_synapse/admin/v1/deactivate/${line}"
fi
fi
done < local_users.txt
echo
echo "############"
echo "run the following as root (with synapse running):"
echo "su - postgres"
echo "psql -U synapse_user -h 127.0.0.1 synapse < db_change.sql"
echo
echo "############"
echo "STOP synapse: systemctl stop matrix-synapse"
echo "...and run the following commands as postgres superuser:"
echo "REINDEX (VERBOSE) DATABASE synapse;"
echo "VACUUM FULL VERBOSE;"
echo
echo "Might take HOURS to complete! Matrix is not available during that period !"
exit 0