# This file describes the forwarding of (almost) every endpoint to a worker or pool of
# workers. This file should go in /etc/nginx/snippets, because we need to load it once, on
# the right place in our site-definition.

# Account-data
location ~ ^(/_matrix/client/(r0|v3|unstable)/.*/tags|/_matrix/client/(r0|v3|unstable)/.*/account_data) {
    include snippets/proxy.conf;
    proxy_pass http://account_data;
}

# Typing
location ~ ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/typing {
    include snippets/proxy.conf;
    proxy_pass http://typing;
}

# Receipts
location ~ ^(/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt|/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers) {
    include snippets/proxy.conf;
    proxy_pass http://receipts;
}

# Presence
location ~ ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/ {
    include snippets/proxy.conf;
    proxy_pass http://presence;
}

# To device
location ~ ^/_matrix/client/(r0|v3|unstable)/sendToDevice/ {
    include snippets/proxy.conf;
    proxy_pass http://todevice;
}

# Push rules
location ~ ^/_matrix/client/(api/v1|r0|v3|unstable)/pushrules/ {
    include snippets/proxy.conf;
    proxy_pass http://push_rules;
}

# Userdir
location ~ ^/_matrix/client/(r0|v3|unstable)/user_directory/search$ {
    include snippets/proxy.conf;
    proxy_pass http://userdir;
}

# Media, users1
location ~* ^/_matrix/((client|federation)/[^/]+/)media/ {
    include snippets/proxy.conf;
    proxy_pass http://media;
}
# Media, users2
location ~* ^/_matrix/media/v3/upload {
    include snippets/proxy.conf;
    proxy_pass http://media;
}

# Media, admin
location ~ ^/_synapse/admin/v1/(purge_)?(media(_cache)?|room|user|quarantine_media|users)/[\s\S]+|media$ {
    include snippets/private.conf;
    include snippets/proxy.conf;
    proxy_pass http://media;
}

# Login
location ~ ^(/_matrix/client/(api/v1|r0|v3|unstable)/login|/_matrix/client/(r0|v3|unstable)/register|/_matrix/client/(r0|v3|unstable)/register/available|/_matrix/client/v1/register/m.login.registration_token/validity|/_matrix/client/(r0|v3|unstable)/password_policy)$ {
    include snippets/proxy.conf;
    proxy_pass http://login;
}

# Normal/initial sync:
# To which upstream to pass the request depends on the map "$sync"
location ~ ^/_matrix/client/(r0|v3)/sync$ {
    include snippets/proxy.conf;
    proxy_pass http://$sync;
}
# Normal sync:
# These endpoints are used for normal syncs
location ~ ^/_matrix/client/(api/v1|r0|v3)/events$ {
    include snippets/proxy.conf;
    proxy_pass http://normal_sync;
}
# Initial sync:
# These endpoints are used for initial syncs
location ~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$ {
    include snippets/proxy.conf;
    proxy_pass http://initial_sync;
}
location ~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ {
    include snippets/proxy.conf;
    proxy_pass http://initial_sync;
}

# Federation
# All the "normal" federation stuff:
location ~ ^(/_matrix/federation/v1/event/|/_matrix/federation/v1/state/|/_matrix/federation/v1/state_ids/|/_matrix/federation/v1/backfill/|/_matrix/federation/v1/get_missing_events/|/_matrix/federation/v1/publicRooms|/_matrix/federation/v1/query/|/_matrix/federation/v1/make_join/|/_matrix/federation/v1/make_leave/|/_matrix/federation/(v1|v2)/send_join/|/_matrix/federation/(v1|v2)/send_leave/|/_matrix/federation/v1/make_knock/|/_matrix/federation/v1/send_knock/|/_matrix/federation/(v1|v2)/invite/|/_matrix/federation/v1/event_auth/|/_matrix/federation/v1/timestamp_to_event/|/_matrix/federation/v1/exchange_third_party_invite/|/_matrix/federation/v1/user/devices/|/_matrix/key/v2/query|/_matrix/federation/v1/hierarchy/) {
    include snippets/proxy.conf;
    proxy_pass http://incoming_federation;
}
# Inbound federation transactions:
location ~ ^/_matrix/federation/v1/send/ {
    include snippets/proxy.conf;
    proxy_pass http://federation_requests;
}


# Main thread for all the rest
location / {
    include snippets/proxy.conf;
    proxy_pass http://inbound_main;

