Compare commits

..

2 Commits

Author SHA1 Message Date
Nathan McCarty 7df5311859
Setup room cleaning automation 2023-06-10 20:50:52 -04:00
Nathan McCarty 5c5b12cfe6
Add matrix room cleaning script 2023-06-10 20:39:02 -04:00
3 changed files with 70 additions and 1 deletions

View File

@ -0,0 +1,36 @@
{ config, lib, pkgs, ... }:
let
path = with pkgs; [ jq postgresql_15 matrix-synapse-tools.synadm ];
environment = {
HOME = "/home/nathan";
XDG_CONFIG_DIRS =
"/etc/xdg:/home/nathan/.nix-profile/etc/xdg:/etc/profiles/per-user/nathan/etc/xdg:/nix/var/nix/profiles/default/etc/xdg:/run/current-system/sw/etc/xdg";
XDG_CONFIG_HOME = "/home/nathan/.config";
};
synapse-script = { name, script, schedule }: {
systemd.services."synapse-script-${name}" = {
inherit environment;
inherit path;
inherit script;
serviceConfig = {
Type = "oneshot";
User = "nathan";
Group = "users";
};
};
systemd.timers."synapse-script-${name}" = {
wantedBy = [ "timers.target" ];
partOf = [ "synapse-script-${name}.service" ];
timerConfig = {
OnCalendar = schedule;
Unit = "synapse-script-${name}.service";
};
};
};
in lib.mkMerge [
(synapse-script {
name = "clean-vacant-rooms";
schedule = "23:00";
script = builtins.readFile ../../scripts/matrix/clean-old-rooms.sh;
})
]

View File

@ -1,7 +1,7 @@
{ config, lib, pkgs, inputs, ... }:
{
imports = [ ./matrix.nix ];
imports = [ ./matrix.nix ./automation.nix ];
# Sops setup for this machine
sops.secrets = {
"borg-ssh-key" = {

View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -eou pipefail
# Get the list of rooms with no local users
empty_rooms() {
psql -d synapse -c "SELECT room_id FROM room_stats_current WHERE local_users_in_room = 0 \
ORDER BY current_state_events DESC LIMIT 1;" -t --csv
}
# Delete a room and purge its history
delete_room() {
details=$(synadm room details $1)
name=$(echo $details | jq -r '.name')
canonical_alias=$(echo $details | jq -r '.canonical_aliacanonical_alias')
topic=$(echo $details | jq -r '.topic')
joined_local_members=$(echo $details | jq -r '.joined_local_members')
state_events=$(echo $details | jq -r '.state_events')
echo "Deleting room $1 (name: $name, canonical alias: $canonical_alias, topic: $topic)[$joined_local_members]"
echo "This will remove $state_events state events"
if [[ $joined_local_members == "0" ]];
then
time synadm --yes room delete $1
else
echo "Room still had active members!!! $1"
fi
echo ""
}
log_file="empty-removal-$(date -Iminutes).log"
touch $log_file
for room in $(empty_rooms); do
delete_room $room | tee -a $log_file
done