瀏覽代碼

room expire script for matrix

Bob Mottram 7 年之前
父節點
當前提交
7b8c8aef58
共有 1 個文件被更改,包括 31 次插入0 次删除
  1. 31
    0
      src/freedombone-app-matrix

+ 31
- 0
src/freedombone-app-matrix 查看文件

@@ -54,14 +54,42 @@ MATRIX_REPO="https://github.com/matrix-org/synapse"
54 54
 MATRIX_COMMIT='c45dc6c62aa2a2e83a10d8116a709dfd8c144e3c'
55 55
 REPORT_STATS="no"
56 56
 MATRIX_SECRET=
57
+MATRIX_EXPIRE_MONTHS=1
57 58
 
58 59
 matrix_variables=(ONION_ONLY
59 60
                   MY_USERNAME
60 61
                   MATRIX_SECRET
61 62
                   DEFAULT_DOMAIN_NAME
62 63
                   MATRIX_DOMAIN_NAME
64
+                  MATRIX_EXPIRE_MONTHS
63 65
                   MATRIX_CODE)
64 66
 
67
+function matrix_expire_old_posts {
68
+    read_config_param MY_USERNAME
69
+    read_config_param MATRIX_DOMAIN_NAME
70
+    read_config_param MATRIX_EXPIRE_MONTHS
71
+    read_config_param MATRIX_PORT
72
+
73
+    matrix_remove_posts=/usr/bin/matrix-remove
74
+    echo '#!/bin/bash' > $matrix_remove_posts
75
+    echo "cd $MATRIX_DATA_DIR" >> $matrix_remove_posts
76
+    echo 'ROOM=$1' >> $matrix_remove_posts
77
+    echo "ADMIN=\"@${MY_USERNAME}:$MATRIX_DOMAIN_NAME\"" >> $matrix_remove_posts
78
+    echo "TIME='$MATRIX_EXPIRE_MONTHS months ago'" >> $matrix_remove_posts
79
+    echo "UNIX_TIMESTAMP=\$(date +%s%3N --date='TZ=\"UTC+0\" '\"\$TIME\")" >> $matrix_remove_posts
80
+    echo 'BUSY="pragma busy_timeout=20000"' >> $matrix_remove_posts
81
+    echo "BUFFER=\$(sqlite3 homeserver.db \"\$BUSY;select event_id from events where type='m.room.message' and received_ts<'\$UNIX_TIMESTAMP' and room_id='\$ROOM' order by received_ts desc limit 1;\")" >> $matrix_remove_posts
82
+    echo "EVENT_ID=\$(echo \$BUFFER|awk '{print \$2}')" >> $matrix_remove_posts
83
+
84
+    echo "BUFFER=\$(sqlite3 homeserver.db \"\$BUSY;select token from access_tokens where user_id like '\$ADMIN' order by id desc limit 1;\")" >> $matrix_remove_posts
85
+    echo "TOKEN=\$(echo \$BUFFER|awk '{print \$2}')" >> $matrix_remove_posts
86
+
87
+    echo 'set -x' >> $matrix_remove_posts
88
+    echo "curl -v -X POST 'https://$MATRIX_DOMAIN_NAME/_matrix/client/r0/admin/purge_history/'\$ROOM'/'\$EVENT_ID'?access_token='\$TOKEN" >> $matrix_remove_posts
89
+
90
+    chmod +x $matrix_remove_posts
91
+}
92
+
65 93
 function logging_on_matrix {
66 94
     if [ -f /var/lib/matrix/homeserver.yaml ]; then
67 95
         if ! grep -q "log_file: /etc/matrix/homeserver.log" /var/lib/matrix/homeserver.yaml; then
@@ -651,6 +679,9 @@ function install_home_server {
651 679
     fi
652 680
     chmod -R 700 $MATRIX_DATA_DIR/homeserver.db
653 681
 
682
+    cd $MATRIX_DATA_DIR
683
+    sqlite3 homeserver.db "PRAGMA auto_vacuum = FULL;"
684
+
654 685
     MATRIX_ONION_HOSTNAME=$(add_onion_service matrix ${MATRIX_PORT} ${MATRIX_ONION_PORT})
655 686
     echo "HiddenServicePort ${MATRIX_HTTP_PORT} 127.0.0.1:${MATRIX_FEDERATION_ONION_PORT}" >> /etc/tor/torrc
656 687
     systemctl restart tor