diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Matrix/Client.pm6 | 81 | ||||
-rw-r--r-- | lib/Matrix/Client/Requester.pm6 | 43 | ||||
-rw-r--r-- | lib/Matrix/Client/Room.pm6 | 2 | ||||
-rw-r--r-- | lib/Matrix/Response.pm6 | 19 |
4 files changed, 130 insertions, 15 deletions
diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 2fdbf4b..9022c65 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -1,6 +1,6 @@ use HTTP::Request::Common; use URI::Encode; -use JSON::Tiny; +use JSON::Fast; use Matrix::Response; use Matrix::Client::Common; use Matrix::Client::Room; @@ -116,6 +116,31 @@ method whoami { $!user-id } +## Device management + +#| GET - /_matrix/client/r0/devices +method devices(Matrix::Client:D: --> Seq) { + my $data = from-json($.get("/devices").content); + $data<devices>.map(-> $device-data { + Matrix::Response::Device.new(|$device-data) + }) +} + +#| GET - /_matrix/client/r0/devices/{deviceId} +method device(Matrix::Client:D: Str $device-id where *.chars > 0 --> Matrix::Response::Device) { + my $device-data = from-json($.get("/devices/$device-id").content); + Matrix::Response::Device.new(|$device-data) +} + +#| PUT - /_matrix/client/r0/devices/{deviceId} +method update-device(Matrix::Client:D: + Str $device-id where *.chars > 0, + Str $display-name) { + $.put("/devices/$device-id", :display_name($display-name)); +} + +## Presence + #| GET - /_matrix/client/r0/presence/{userId}/status method presence(Matrix::Client:D: $user-id? --> Matrix::Response::Presence) { my $id = $user-id // $.whoami; @@ -202,6 +227,45 @@ method join-room($room-id!) { $.post("/join/$room-id") } +#| POST - /_matrix/client/r0/rooms/{roomId}/ban +method ban(Str $room-id, Str $user-id, $reason = "") { + $.post( + "/rooms/$room-id/ban", + :$user-id, + :$reason + ); +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/unban +method unban(Str $room-id, Str $user-id) { + $.post( + "/rooms/$room-id/unban", + :$user-id + ); +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/invite +method invite(Str $room-id, Str $user-id) { + $.post( + "/rooms/$room-id/invite", + :$user-id + ) +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/forget +method forget(Str $room-id) { + $.post("/rooms/$room-id/forget") +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/kick +method kick(Str $room-id, Str $user-id, $reason = "") { + $.post( + "/rooms/$room-id/kick", + :$user-id, + :$reason + ); +} + #| POST - /_matrix/client/r0/rooms/{roomId}/leave method leave-room($room-id) { $.post("/rooms/$room-id/leave"); @@ -236,6 +300,17 @@ method send(Str $room-id, Str $body, :$type? = "m.text") { from-json($res.content)<event_id> } +#| PUT - /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} +method send-event(Str $room-id, Str :$event-type, :$content, :$txn-id? is copy, :$timestamp?) { + unless $txn-id.defined { + $txn-id = $Matrix::Client::Common::TXN-ID++; + } + + my $path = "/rooms/$room-id/send/$event-type/$txn-id"; + my $res = $.put($path, |$content); + from-json($res.content)<event_id> +} + #| GET - /_matrix/client/r0/directory/room/{roomAlias} method get-room-id($room-alias) { my $res = $.get("/directory/room/$room-alias"); @@ -270,10 +345,10 @@ method upload(IO::Path $path, Str $filename?) { # Misc -method run(Int :$sleep = 10, :$sync-filter? --> Supply) { +method run(Int :$sleep = 10, :$sync-filter?, :$start-since? --> Supply) { my $s = Supplier.new; my $supply = $s.Supply; - my $since = ""; + my $since = $start-since // ""; start { loop { diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index 1a60014..ff543c9 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -1,7 +1,7 @@ use HTTP::UserAgent; use HTTP::Request::Common; use URI::Encode; -use JSON::Tiny; +use JSON::Fast; use Matrix::Client::Exception; unit role Matrix::Client::Requester; @@ -27,13 +27,22 @@ method !access-token-arg { } method get(Str $path, :$media = False, *%data) { - my $query = "?{self!access-token-arg}"; + my $query = "?"; for %data.kv -> $k,$v { $query ~= "&$k=$v" if $v.so; } my $encoded-path = $path.subst('#', '%23'); my $uri = $.base-url(:$media) ~ $encoded-path ~ uri_encode($query); - return self!handle-error($!ua.get($uri)); + + my $req = HTTP::Request.new(GET => $uri); + + if $!access-token.so { + $req.header.field(Authorization => "Bearer {$!access-token}"); + } + + return self!handle-error( + $!ua.request($req) + ); } method base-url(Bool :$media? = False --> Str) { @@ -46,9 +55,12 @@ method base-url(Bool :$media? = False --> Str) { multi method post(Str $path, Str $json, :$media = False) { my $encoded-path = $path.subst('#', '%23'); - my $url = $.base-url(:$media) ~ $encoded-path ~ "?{self!access-token-arg}"; + my $url = $.base-url(:$media) ~ $encoded-path; my $req = HTTP::Request.new(POST => $url, Content-Type => 'application/json'); + if $!access-token.so { + $req.header.field(Authorization => "Bearer {$!access-token}"); + } $req.add-content($json); return self!handle-error($!ua.request($req)); } @@ -60,19 +72,27 @@ multi method post(Str $path, :$media = False, *%params) { method post-bin(Str $path, Buf $buf, :$content-type) { my $encoded-path = $path.subst('#', '%23'); - my $req = POST($.base-url(:media) - ~ $encoded-path - ~ "?{self!access-token-arg}", + my $req = POST( + $.base-url(:media) ~ $encoded-path, content => $buf, Content-Type => $content-type ); + + if $!access-token.so { + $req.header.field(Authorization => "Bearer {$!access-token}"); + } + return self!handle-error($!ua.request($req)); } multi method put(Str $path, Str $json) { my $encoded-path = $path.subst('#', '%23'); - my $req = HTTP::Request.new(PUT => $.base-url() ~ $encoded-path ~ "?{self!access-token-arg}", + my $req = HTTP::Request.new(PUT => $.base-url() ~ $encoded-path, Content-Type => 'application/json'); + if $!access-token.so { + $req.header.field(Authorization => "Bearer {$!access-token}"); + } + $req.add-content($json); return self!handle-error($!ua.request($req)) } @@ -84,7 +104,12 @@ multi method put(Str $path, *%params) { method delete(Str $path) { my $encoded-path = $path.subst('#', '%23'); my $req = HTTP::Request.new( - DELETE => $.base-url ~ $encoded-path ~ "?{self!access-token-arg}", + DELETE => $.base-url ~ $encoded-path, Content-Type => 'application/json'); + if $!access-token.so { + $req.header.field( + Authorization => "Bearer $!access-token" + ); + } return self!handle-error($!ua.request($req)) } diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index 4ec774f..ca97d2f 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -1,4 +1,4 @@ -use JSON::Tiny; +use JSON::Fast; use Matrix::Client::Common; use Matrix::Client::Requester; use Matrix::Response; diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index edd4d51..268be84 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -1,4 +1,4 @@ -use JSON::Tiny; +use JSON::Fast; unit module Matrix::Response; @@ -69,7 +69,7 @@ class Matrix::Response::Sync { my Matrix::Response::Event @presence; my Matrix::Response::RoomInfo @joined-rooms; my Matrix::Response::InviteInfo @invited-rooms; - + for $json<presence><events>.List -> $ev { @presence.push(Matrix::Response::Event.new(|$ev)); } @@ -122,3 +122,18 @@ class Tag { self.bless(:@tags) } } + + +class Matrix::Response::Device { + has Str $.device-id; + has $.display-name; + has $.last-seen-ip; + has $.last-seen-ts; + + submethod BUILD( + Str :device_id(:$!device-id), + :display_name(:$!display-name)?, + :last_seen_ip(:$!last-seen-ip)?, + :last_seen_ts(:$!last-seen-ts)? + ) { } +} |