Skip to content

Latest commit

 

History

History
150 lines (91 loc) · 5.34 KB

matrix.md

File metadata and controls

150 lines (91 loc) · 5.34 KB

tomesh.net Chat

Toronto Mesh is running a Matrix homeserver for our decentralized chat. The Matrix protocol allows us to communicate on the same homeserver, as well as reach users and rooms on other homeservers via homeserver federation. Users are free to connect to our homeserver with any compatible desktop or mobile client, or the web client we host.

Matrix homeserver: https://matrix.tomesh.net

Web client: https://chat.tomesh.net

This page describes how both these components are set up.

Set Up Matrix Homeserver

We currently run the Python-implemented Synapse homeserver at matrix.tomesh.net.

Install Synapse Homeserver

We are using Terraform to provision our Synapse Homeserver. The instructions can be found on our mesh-services repository

Update Synapse Version

  1. SSH into matrix.tomesh.net

  2. Update Synapse using Debian's apt command

    sudo apt update && sudo apt dist-upgrade -y
    

Synapse Performance Bug

Our version of Synapse homeserver accumulates forward extremities over time due to a known bug. Without the workaround the performance will suffer noticably. The current workaround is enabling cleanup_extremities_with_dummy_events but if that is not working follow steps below:

  1. SSH into matrix.tomesh.net

  2. Stop the synapse service sudo systemctl stop matrix-synapse

  3. Switch to Synapse Postgres user sudo -i -u postgres

  4. Load CLI and connect to Synapse database psql -d synapse

  5. Run query and look for rooms with more than 3 forward extremities:

    select room_id, count(*) c from event_forward_extremities group by room_id order by c desc limit 30;
    
  6. Run the following for each of the high extremities room:

    DELETE FROM event_forward_extremities AS e
    USING ( 
        SELECT DISTINCT ON (room_id)
        room_id,
        last_value(event_id) OVER w AS event_id
        FROM event_forward_extremities
        NATURAL JOIN events
        WINDOW w AS (
            PARTITION BY room_id
            ORDER BY stream_ordering
            range between unbounded preceding and unbounded following
        )
        ORDER BY room_id, stream_ordering
    ) AS s
    WHERE
        s.room_id = e.room_id
        AND e.event_id != s.event_id
        AND e.room_id = '!jpZMojebDLgJdJzFWn:matrix.org';
    
  7. Run the select again to confirm that forward extremities counts are cleared up for all those rooms.

  8. Start Synapse sudo systemctl start matrix-synapse and the you should see improved performance and system load of the VM will drop.

Grant Synapse User Admin Rights

  1. SSH into matrix.tomesh.net

  2. Switch to Postgres user sudo -i -u postgres

  3. Load CLI and connect to Synapse database psql -d synapse

  4. Run the query to make the user an admin replace USERNAME with the username of the user:

    UPDATE users SET admin=1 WHERE name LIKE '@USERNAME:tomesh.net';
    

Purging Old Posts and Media Files From One Year Ago

  1. Login as an admin user at https://chat.tomesh.net and copy your Access token

  2. SSH into matrix.tomesh.net

  3. Put your Access token into a variable called access_token:

    access_token=ABCD1234...
    
  4. Run the API call to purge old posts (e.g. #tomesh:tomesh.net channel with the Internal room ID: !FsFLbKGMcUXEMBxZdu:tomesh.net). To purge another room, replace the ID with that room's ID:

    curl -XPOST -d '{"delete_local_events": true, "purge_up_to_ts": '$(echo $(($(date --date="1 year ago" -u +%s%N)/1000000)))' }' 'http://localhost:8008/_matrix/client/r0/admin/purge_history/!FsFLbKGMcUXEMBxZdu:tomesh.net?access_token='$access_token
    
  5. Optionally you can remove all remote content by running:

    curl -XPOST -d '{}' "http://localhost:8008/_matrix/client/r0/admin/purge_media_cache?before_ts=$(echo $(($(date -u +%s%N)/1000000)))&access_token=$access_token"
    
  6. Switch to Postgres user sudo -i -u postgres

  7. Load CLI and connect to Synapse database psql -d synapse

  8. Run the command VACUUM;

  9. Logout of the database and the Postgres user and return back to your shell

  10. Switch to the root user sudo -i

  11. Go into Synapse's media storage directory

    cd /var/lib/matrix-synapse/media/local_content/
    
  12. Delete old media files by running the following commands:

    cd /var/lib/matrix-synapse/media/local_content/
    find * -mindepth 1 -mtime +365 -delete
    

Update Riot Web Client

  1. SSH into matrix.tomesh.net

  2. Get a root shell with sudo -i

  3. Download the pre-compiled Riot Web release:

    wget https://github.com/vector-im/riot-web/releases/download/v1.5.0/riot-v1.5.0.tar.gz
    
  4. Backup config file

    cp /var/www/chat.tomesh.net/public/config.json /root/riot-config.json
    
  5. Remove old Riot client:

    rm -r /var/www/chat.tomesh.net/public/*
    
  6. Extract riot-v1.5.0.tar.gz into /var/www/chat.tomesh.net/public:

    tar xf riot-v1.5.0.tar.gz -C /var/www/chat.tomesh.net/public --strip-components 1
    
  7. Restore config file

    cp /root/riot-config.json /var/www/chat.tomesh.net/public/config.json
    
  8. Run chown -R www-data:www-data /var/www/ to ensure that www-data have full access