From f12fa74c8610ccd327e52b4f111ec572f63d5e28 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 2 May 2020 05:06:00 +0000 Subject: [PATCH] playing with matrix via curl --- testdata/matrix-sandbox/.reference.yaml | 51 +++++++++++ testdata/matrix-sandbox/build.sh | 113 ++++++++++++++++++++++++ testdata/matrix-sandbox/config.yaml | 52 +++++++++++ 3 files changed, 216 insertions(+) create mode 100644 testdata/matrix-sandbox/.reference.yaml create mode 100644 testdata/matrix-sandbox/build.sh create mode 100644 testdata/matrix-sandbox/config.yaml diff --git a/testdata/matrix-sandbox/.reference.yaml b/testdata/matrix-sandbox/.reference.yaml new file mode 100644 index 0000000..242811d --- /dev/null +++ b/testdata/matrix-sandbox/.reference.yaml @@ -0,0 +1,51 @@ +server_name: scratch.com +pid_file: "/var/run/matrix-synapse.pid" +federation_ip_range_blacklist: + - '127.0.0.0/8' + - '10.0.0.0/8' + - '172.16.0.0/12' + - '192.168.0.0/16' + - '100.64.0.0/10' + - '169.254.0.0/16' + - '::1/128' + - 'fe80::/64' + - 'fc00::/7' +listeners: + - port: 39987 + tls: false + type: http + x_forwarded: false + bind_addresses: ['::'] + resources: + - names: [client, federation] + compress: false +retention: +acme: + enabled: false + port: 80 + bind_addresses: ['::', '0.0.0.0'] + reprovision_threshold: 30 + domain: matrix.example.com + account_key_file: /var/lib/matrix-synapse/acme_account.key +database: + name: "sqlite3" + args: + database: "/mnt/homeserver.db" +log_config: "/etc/matrix-synapse/log.yaml" +media_store_path: "/var/lib/matrix-synapse/media" +enable_registration: true +account_validity: +account_threepid_delegates: +metrics_flags: +signing_key_path: "/etc/matrix-synapse/homeserver.signing.key" +old_signing_keys: +trusted_key_servers: + - server_name: "matrix.org" +saml2_config: + user_mapping_provider: + config: +sso: +password_config: +email: +enable_group_creation: true +opentracing: diff --git a/testdata/matrix-sandbox/build.sh b/testdata/matrix-sandbox/build.sh new file mode 100644 index 0000000..bea59f2 --- /dev/null +++ b/testdata/matrix-sandbox/build.sh @@ -0,0 +1,113 @@ +#! /bin/bash + +if ! ps aux | grep -v grep | grep homeserver; then + homeserver -c config.yaml & +fi + +# pick a username +username="$(cat /proc/sys/kernel/random/uuid)" + +# register +out="$( + curl -sS http://192.168.0.86:39487/_matrix/client/r0/register \ + -X POST \ + -H 'content-type: application/json' \ + -d "$( + printf '{ + "username": "%s", + "password": "%s", + "auth": { + "type": "m.login.dummy" + } + }' "$username" + )" +)" +printf "%s\n" "$out" +access_token="$(echo "$out" | jq -r .access_token)" +user_id="$(echo "$out" | jq -r .user_id)" +home_server="$(echo "$out" | jq -r .home_server)" + +# create a room +room="$(cat /proc/sys/kernel/random/uuid)" + +out="$( + curl -sS http://192.168.0.86:39487/_matrix/client/r0/createRoom\ + -X POST \ + -H 'content-type: application/json' \ + -H 'Authorization: Bearer '"$access_token" \ + -d "$( + printf '{ + "room_alias_name": "%s" + }' "$room" + )" +)" +printf "%s\n" "$out" +room_id="$(echo "$out" | jq -r .room_id)" + +# join a room by name +# curl -XPOST -d '{}' "https://localhost:8448/_matrix/client/r0/join/%21asfLdzLnOdGRkdPZWu:localhost?access_token=YOUR_ACCESS_TOKEN" +curl -sS "http://192.168.0.86:39487/_matrix/client/r0/join/$room_id" \ + -X POST \ + -H 'Authorization: Bearer '"$access_token" \ + -d '{}' + +# send message +# curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "https://localhost:8448/_matrix/client/r0/rooms/%21asfLdzLnOdGRkdPZWu:localhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN" +#PUT /_matrix/client/r0/rooms/%21636q39766251%3Aexample.com/send/m.room.message/35 HTTP/1.1 Content-Type: application/json {"msgtype": "m.text","body": "hello"} +txid="$(cat /proc/sys/kernel/random/uuid)" +message="hello wurl" + +out="$( + curl -sS "http://192.168.0.86:39487/_matrix/client/r0/rooms/$room_id/send/m.room.message/$txid" \ + -X PUT \ + -H 'content-type: application/json' \ + -H 'Authorization: Bearer '"$access_token" \ + -d "$( + printf '{ + "msgtype": "m.text", + "body": "%s" + }' "$message" + )" +)" +printf "%s\n" "$out" + +# get recent messages +# curl --globoff -XGET 'https://localhost:8448/_matrix/client/r0/sync?filter={"room":{"timeline":{"limit":1}}}&access_token=YOUR_ACCESS_TOKEN' +out="$( + curl -sS "http://192.168.0.86:39487/_matrix/client/r0/sync" \ + -X GET \ + -H 'content-type: application/json' \ + -H 'Authorization: Bearer '"$access_token" \ + | jq '{"timeline": .rooms.join[].timeline.events[], "next": .next_batch}' \ + | jq '{"content": .timeline.content.body, "sender": .timeline.sender, "next": .next}' +)" +printf "%s\n" "$out" +since="$(echo "$out" | jq -r .next | tail -n 1)" + +# send new message +txid="$(cat /proc/sys/kernel/random/uuid)" +message="hello wurl2" + +out="$( + curl -sS "http://192.168.0.86:39487/_matrix/client/r0/rooms/$room_id/send/m.room.message/$txid" \ + -X PUT \ + -H 'content-type: application/json' \ + -H 'Authorization: Bearer '"$access_token" \ + -d "$( + printf '{ + "msgtype": "m.text", + "body": "%s" + }' "$message" + )" +)" +printf "%s\n" "$out" + +# get second recent messages +# curl --globoff -XGET 'https://localhost:8448/_matrix/client/r0/sync?filter={"room":{"timeline":{"limit":1}}}&access_token=YOUR_ACCESS_TOKEN' +curl -sS "http://192.168.0.86:39487/_matrix/client/r0/sync?since=$since" \ + -X GET \ + -H 'content-type: application/json' \ + -H 'Authorization: Bearer '"$access_token" \ + | jq '{"timeline": .rooms.join[].timeline.events[], "next": .next_batch}' \ + | jq '{"content": .timeline.content.body, "sender": .timeline.sender, "next": .next}' + diff --git a/testdata/matrix-sandbox/config.yaml b/testdata/matrix-sandbox/config.yaml new file mode 100644 index 0000000..f8f787e --- /dev/null +++ b/testdata/matrix-sandbox/config.yaml @@ -0,0 +1,52 @@ +server_name: 192.168.0.86 +pid_file: "/tmp/synapse-test.pid" +federation_ip_range_blacklist: + - '127.0.0.0/8' + - '10.0.0.0/8' + - '172.16.0.0/12' + - '192.168.0.0/16' + - '100.64.0.0/10' + - '169.254.0.0/16' + - '::1/128' + - 'fe80::/64' + - 'fc00::/7' +listeners: + - port: 39487 + tls: false + type: http + x_forwarded: false + bind_addresses: ['::'] + resources: + - names: [client, federation] + compress: false +retention: +acme: + enabled: false + port: 80 + bind_addresses: ['::', '0.0.0.0'] + reprovision_threshold: 30 + domain: matrix.example.com + account_key_file: /var/lib/matrix-synapse/acme_account.key +database: + name: "sqlite3" + args: + database: "/tmp/synapse-test.db" +log_config: "/tmp/synapse-test-log.yaml" +media_store_path: "/tmp/synapse-test-media" +enable_registration: true +account_validity: +account_threepid_delegates: +metrics_flags: +signing_key_path: "/tmp/synapse-test.key" +old_signing_keys: +trusted_key_servers: + - server_name: "matrix.org" +saml2_config: + user_mapping_provider: + config: +sso: +password_config: +email: +enable_group_creation: true +opentracing: +report_stats: false