From c5e5856fab53af27e085bb65ba9eeacd2def1e6e Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Wed, 5 Jun 2019 00:50:35 -0300 Subject: Upgrade documentation --- docs/client.pod6 | 58 ++++++++++++++++----- docs/requester.pod6 | 66 ++++++++++++++++++++++++ docs/responses.pod6 | 122 ++++++++++++++++++++++++++++++++++++++++++++ docs/usage.pod6 | 41 +++++++++++++++ docs/x-matrix-response.pod6 | 23 +++++++++ 5 files changed, 296 insertions(+), 14 deletions(-) create mode 100644 docs/requester.pod6 create mode 100644 docs/responses.pod6 create mode 100644 docs/usage.pod6 create mode 100644 docs/x-matrix-response.pod6 diff --git a/docs/client.pod6 b/docs/client.pod6 index 302e99b..ba1f2e1 100644 --- a/docs/client.pod6 +++ b/docs/client.pod6 @@ -2,25 +2,55 @@ =TITLE class Matrix::Client -=SUBTITLE Client API for Matrix +=SUBTITLE matrix.org client - class Matrix::Client does Matrix::Client::Requester {} +=head1 NAME + +Matrix::Client — Client API for Matrix.org + +=head1 SYNOPSIS + + use Matrix::Client; + + my Matrix::Client $client .= new( + :home-server, + :access-token + ); + + my $room = $client.joined-rooms.first; + $room.send("Hello from Matrix::Client"); + + + my $sync = $client.sync; + + for $response.joined-rooms -> $room { + say "Messages from {$room.name}" + for $room.timeline + .events + .grep(*.type eq 'm.room.message') -> $msg { + say $msg.content; + } + } + +=head1 DESCRIPTION + + Class Matrix::Client does Matrix::Client::Requester {} The main object in the module. The C is used to talk to a Matrix home server abstracting the HTTP requests. The client is used for example to join rooms, receive/send messages, etc. -On server errors, all methods will throw a L exception. +On server errors, all methods will throw a L exception. -=head1 Methods +=head1 METHODS =head2 new sub new(Str :$home-server, Str :$access-token?, Str :$device-id?) Creates a C pointing to a home server. If no C<$access-token> is -passed to the constructor, the client needs to call L to make authorized +passed to the constructor, the client needs to call L<#login> to make authorized calls to the API. =head2 login @@ -33,7 +63,7 @@ before the C call, it will register that C in the server invalidating any previously associated access token to this C. Otherwise the home server will auto-generate one. -On a failed login attempt, a L is raised with a code +On a failed login attempt, a L is raised with a code of C<“M_FORBIDDEN”> =head2 logout @@ -51,7 +81,7 @@ Register a B account in the home server. If C<$bind-email> is true, the server binds the email used for authentication to the Matrix ID with the ID Server. -In case there's an error with the registration, a L is raised +In case there's an error with the registration, a L is raised with one of the following Cs: =item C The desired user ID is already taken. @@ -63,7 +93,7 @@ with one of the following Cs: method profile(Str :$user-id?) Get the combined profile information for this user. With no C<$user-id> -L will provide the profile data associated with the client +L<#profile> will provide the profile data associated with the client account. It returns a C that can contains C and/or C. @@ -125,10 +155,10 @@ must be C<“online”>, C<“offline”> or C<“unavailable”>. multi method sync(Hash :$sync-filter is copy, :$since = "") Gets the client's state with the latest state on the server. It returns -a L with the initial snapshot or delta. +a L with the initial snapshot or delta. C<$since> is necessary to get the incremental deltas to the states. The C<$since> -value is retrieved from the C in the L. +value is retrieved from the C in the L. The C is the filter that will be applied to the sync. It will encode it to a JSON string if it isn't a C already. For more information about @@ -217,11 +247,11 @@ L. The room alias must be in the form -C, otherwise it will raise a L with +C, otherwise it will raise a L with the proper message and C error code. If there's no room with the C<$room-alias> in the server directory, it will -raise a L with a C code. +raise a L with a C code. =head2 add-room-alias @@ -248,8 +278,8 @@ Uploads a file to the server. It returns the MXC URI to the uploaded content. Returns a C that emits L with the last events. The C<$sleep> parameter is to sleep for that amount of seconds before -making a L request again. The C<$sync-filter> is the same parameter that -will be passed to L method to filter out the useful events. +making a L<#sync> request again. The C<$sync-filter> is the same parameter that +will be passed to L<#sync> method to filter out the useful events. This can be useful to turn something like: diff --git a/docs/requester.pod6 b/docs/requester.pod6 new file mode 100644 index 0000000..6710948 --- /dev/null +++ b/docs/requester.pod6 @@ -0,0 +1,66 @@ +=begin pod + +=TITLE role Matrix::Client::Requester + +=SUBTITLE Role for HTTP requests + + + role Matrix::Client::Requester { } + +Role that gives the base API for objects that interacts to the matrix server. The +attributes that can be set can be: + +=head1 Attributes + +=head2 Str home-server + +The url of the home-server. + +=head2 Str access-token + +access token to make authorized calls to the matrix server. + +=head2 Str url-prefix + +Prefix to all the paths used in the methods. + +=head1 Methods + +=head2 get + + method get(Str $path, :$media = False, *%data) + +Do a GET to C<$path>. + +All the C<*%data> is used to build the query params for the url. + +=head2 post + + multi method post(Str $path, Str $params, :$media = False) + multi method post(Str $path, :$media = False, *%params) + +Do a POST to C<$path> with C<$params> as JSON body. With the +named C<*%params>, those are parameters are converted into JSON. + +=head2 post-bin + + method post-bin(Str $path, Buf $buf, :$content-type) + +Do a POST to C<$path> with binary data in the body. + +=head2 put + + +multi method put(Str $path, Str $params) +multi method put(Str $path, *%params) + +Do a PUT to C<$path> with C<$params> as JSON body. With the named +C<*%params>, those parameters are converted into JSON. + +=head2 delete + + method delete(Str $path) + +Do a DELETE to C<$path>. + +=end pod \ No newline at end of file diff --git a/docs/responses.pod6 b/docs/responses.pod6 new file mode 100644 index 0000000..7515d39 --- /dev/null +++ b/docs/responses.pod6 @@ -0,0 +1,122 @@ +=begin pod + +=TITLE Matrix Responses + +=SUBTITLE Wrappers for HTTP responses + +=head1 Event + + + class Matrix::Response::Event { } + +Common contents of a response. + +=head2 Mapped keys + +=item content +=item type + +=head1 RoomEvent + + + class Matrix::Response::RoomEvent is Matrix::Response::Event { } + +A single event for a room + +=head2 Mapped keys + +=item sender +=item origin_server_ts +=item event_id +=item room_id + +=head2 Methods + +=head3 id + + method id + +Returns the event_id + +=head3 timestamp + + method timestamp + +Returns the origin_server_ts + +=head3 room-id + + method room-id + +returns the room_id + + +=head1 StateEvent + + + class Matrix::Response::StateEvent is Matrix::Response::RoomEvent { } + +=head2 Mapped keys + +=item C +=item C + +=head1 Timeline + + + class Matrix::Response::Timeline { } + +=head2 Mapped keys + +=item events — Return a list of L +=item limited +=item prev-batch + + +=head1 RoomInfo + + + class Matrix::Response::RoomInfo { } + +=head2 Mapped keys + +=item room-id — Str with the room id +=item state — List of L +=item Timeine — A L + +=head1 InviteInfo + + + class Matrix::Response::InviteInfo { } + +=head2 Mapped keys + +=item room-id — Str with the room id +=item events — List of L + +=head1 Sync + + + class Matrix::Response::Sync { } + +=head2 Mapped keys + +=item next-batch — Str with the hash for the next sync batch +=item presence — List of L +=item joined-rooms — List of L +=item invited-rooms — List of L + + +=head1 Presence + + + class Matrix::Response::Presence { } + +=head2 Mapped keys + +=item presence +=item last-active-ago +=item status-message +=item currently-active + +=end pod \ No newline at end of file diff --git a/docs/usage.pod6 b/docs/usage.pod6 new file mode 100644 index 0000000..03454d1 --- /dev/null +++ b/docs/usage.pod6 @@ -0,0 +1,41 @@ +=begin pod +=head1 Before we begin… + +This guide will be a port from L +by the Matrix team. + +=head1 Making a Matrix Client + +Let's explore how we would make a very simple Matrix client, with the only ability to perform an initial +sync, and get the member list and the timeline for rooms of our choice. + +This guide will cover: + +=item Login +=item Simple syncing +=item Listening for timeline events +=item Sending messages to rooms +=item How to respond to specific messages + +=head2 Preparing the project + +Before we start, make sure you have perl6 and zef installed. My sugestion is to install +L and follow the instructions. + +Once you're ready, the first thing is to install +L with C + +=begin code +zef install Matrix::Client +=end code + +And now we can include it on our source exaclty as expected. + +=begin code +use Matrix::Client; +=end code + +=head2 Login with an access token + + +=end pod \ No newline at end of file diff --git a/docs/x-matrix-response.pod6 b/docs/x-matrix-response.pod6 new file mode 100644 index 0000000..d93c263 --- /dev/null +++ b/docs/x-matrix-response.pod6 @@ -0,0 +1,23 @@ +=begin pod + +=TITLE X::Matrix::Response + +=SUBTITLE Error querying the matrix server + + + class X::Matrix::Response is Exception {} + +Error class when the matrix server returns an error code (4XX). + +=head1 METHODS + +=head2 code + +Returns the HTTP error code. + +=head2 error + +Returns a C with the matrix error. A full list of error codes can be +found in the L. + +=end pod \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 989e6afb0b9c4b43cac48918ee08d0e24c606c80 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 17 Jun 2019 23:58:29 -0300 Subject: Add ban and unban endpoints --- endpoints.md | 4 ++-- lib/Matrix/Client.pm6 | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/endpoints.md b/endpoints.md index 9abcf1a..2ee313e 100644 --- a/endpoints.md +++ b/endpoints.md @@ -82,14 +82,14 @@ from matrix.org. This will give you an overview about what's implemented in the - [X] GET - /_matrix/client/r0/joined_rooms - [X] POST - /_matrix/client/r0/join/{roomIdOrAlias} -- [ ] POST - /_matrix/client/r0/rooms/{roomId}/ban +- [X] POST - /_matrix/client/r0/rooms/{roomId}/ban - [ ] POST - /_matrix/client/r0/rooms/{roomId}/forget - [ ] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/join - [ ] POST - /_matrix/client/r0/rooms/{roomId}/kick - [X] POST - /_matrix/client/r0/rooms/{roomId}/leave -- [ ] POST - /_matrix/client/r0/rooms/{roomId}/unban +- [X] POST - /_matrix/client/r0/rooms/{roomId}/unban ## Room participation diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 2fdbf4b..a19918b 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -202,6 +202,23 @@ 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}/leave method leave-room($room-id) { $.post("/rooms/$room-id/leave"); -- cgit v1.2.3-54-g00ecf From a090293f1fb64375f5ba16b6a5e53c829eb41add Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Tue, 18 Jun 2019 00:02:08 -0300 Subject: Add invite endpoint --- endpoints.md | 2 +- lib/Matrix/Client.pm6 | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/endpoints.md b/endpoints.md index 2ee313e..5fbb63c 100644 --- a/endpoints.md +++ b/endpoints.md @@ -84,7 +84,7 @@ from matrix.org. This will give you an overview about what's implemented in the - [X] POST - /_matrix/client/r0/join/{roomIdOrAlias} - [X] POST - /_matrix/client/r0/rooms/{roomId}/ban - [ ] POST - /_matrix/client/r0/rooms/{roomId}/forget -- [ ] POST - /_matrix/client/r0/rooms/{roomId}/invite +- [X] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/join - [ ] POST - /_matrix/client/r0/rooms/{roomId}/kick diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index a19918b..86de2e8 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -219,6 +219,14 @@ method unban(Str $room-id, Str $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}/leave method leave-room($room-id) { $.post("/rooms/$room-id/leave"); -- cgit v1.2.3-54-g00ecf From 86d4f5b939010f716e6e0e4768e6a0da3700adfd Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Tue, 18 Jun 2019 00:06:07 -0300 Subject: Add kick and forget endpoints --- endpoints.md | 4 ++-- lib/Matrix/Client.pm6 | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/endpoints.md b/endpoints.md index 5fbb63c..8b22289 100644 --- a/endpoints.md +++ b/endpoints.md @@ -83,11 +83,11 @@ from matrix.org. This will give you an overview about what's implemented in the - [X] GET - /_matrix/client/r0/joined_rooms - [X] POST - /_matrix/client/r0/join/{roomIdOrAlias} - [X] POST - /_matrix/client/r0/rooms/{roomId}/ban -- [ ] POST - /_matrix/client/r0/rooms/{roomId}/forget +- [X] POST - /_matrix/client/r0/rooms/{roomId}/forget - [X] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/invite - [ ] POST - /_matrix/client/r0/rooms/{roomId}/join -- [ ] POST - /_matrix/client/r0/rooms/{roomId}/kick +- [X] POST - /_matrix/client/r0/rooms/{roomId}/kick - [X] POST - /_matrix/client/r0/rooms/{roomId}/leave - [X] POST - /_matrix/client/r0/rooms/{roomId}/unban diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 86de2e8..6d60660 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -227,6 +227,20 @@ method invite(Str $room-id, Str $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"); -- cgit v1.2.3-54-g00ecf From e328870a0fca2f0717ba46886b3d16109320a3ba Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Tue, 18 Jun 2019 00:10:10 -0300 Subject: Update version to 0.4.0 --- META6.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/META6.json b/META6.json index 0788805..7329156 100644 --- a/META6.json +++ b/META6.json @@ -34,5 +34,5 @@ "Test", "Test::META" ], - "version" : "0.3.0" + "version" : "0.4.0" } -- cgit v1.2.3-54-g00ecf From 5bb94eac4f7613cc5275a68eaa095e0be37003fd Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 12:13:57 -0300 Subject: Add generic send event method --- lib/Matrix/Client.pm6 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 6d60660..1a24790 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -275,6 +275,17 @@ method send(Str $room-id, Str $body, :$type? = "m.text") { from-json($res.content) } +#| 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) +} + #| GET - /_matrix/client/r0/directory/room/{roomAlias} method get-room-id($room-alias) { my $res = $.get("/directory/room/$room-alias"); @@ -309,10 +320,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 { -- cgit v1.2.3-54-g00ecf From 4f9159b228cb2533eccab11f82690171a1484ee5 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 19:03:28 -0300 Subject: Use Authorization header instead query param --- lib/Matrix/Client/Requester.pm6 | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index 1a60014..edaf2da 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -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)) } -- cgit v1.2.3-54-g00ecf From 82a19be633aafd305fab93c7e3ea660656ae441a Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 19:04:10 -0300 Subject: Add device management endpoints Missing delete methods only --- endpoints.md | 20 +++++++++++++------- lib/Matrix/Client.pm6 | 25 +++++++++++++++++++++++++ lib/Matrix/Response.pm6 | 15 +++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/endpoints.md b/endpoints.md index 8b22289..2ddf2c3 100644 --- a/endpoints.md +++ b/endpoints.md @@ -8,13 +8,17 @@ from matrix.org. This will give you an overview about what's implemented in the - [ ] PUT - /_matrix/client/r0/directory/list/appservice/{networkId}/{roomId} +## Capabilities + +- [ ] GET - /_matrix/client/r0/capabilities + ## Device management - [ ] DELETE - /_matrix/client/r0/devices/{deviceId} -- [ ] GET - /_matrix/client/r0/devices -- [ ] GET - /_matrix/client/r0/devices/{deviceId} +- [X] GET - /_matrix/client/r0/devices +- [X] GET - /_matrix/client/r0/devices/{deviceId} - [ ] POST - /_matrix/client/r0/delete_devices -- [ ] PUT - /_matrix/client/r0/devices/{deviceId} +- [X] PUT - /_matrix/client/r0/devices/{deviceId} ## End-to-end encryption @@ -38,9 +42,7 @@ from matrix.org. This will give you an overview about what's implemented in the ## Presence -- [ ] GET - /_matrix/client/r0/presence/list/{userId} - [X] GET - /_matrix/client/r0/presence/{userId}/status -- [ ] POST - /_matrix/client/r0/presence/list/{userId} - [X] PUT - /_matrix/client/r0/presence/{userId}/status ## Push notifications @@ -103,7 +105,6 @@ from matrix.org. This will give you an overview about what's implemented in the - [ ] GET - /_matrix/client/r0/rooms/{roomId}/members - [X] GET - /_matrix/client/r0/rooms/{roomId}/messages - [ ] GET - /_matrix/client/r0/rooms/{roomId}/state -- [ ] GET - /_matrix/client/r0/rooms/{roomId}/state/{eventType} - [ ] GET - /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey} - [ ] GET - /_matrix/client/r0/sync - [ ] GET - /_matrix/client/r0/user/{userId}/filter/{filterId} @@ -111,10 +112,13 @@ from matrix.org. This will give you an overview about what's implemented in the - [ ] POST - /_matrix/client/r0/user/{userId}/filter - [ ] PUT - /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId} - [X] PUT - /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} -- [ ] PUT - /_matrix/client/r0/rooms/{roomId}/state/{eventType} - [X] PUT - /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey} - [ ] PUT - /_matrix/client/r0/rooms/{roomId}/typing/{userId} +## Room ugprades + +- [ ] POST - /_matrix/client/r0/rooms/{roomId}/upgrade + ## Search - [ ] POST - /_matrix/client/r0/search @@ -145,6 +149,8 @@ from matrix.org. This will give you an overview about what's implemented in the - [X] GET - /_matrix/client/r0/profile/{userId}/avatar_url - [X] GET - /_matrix/client/r0/profile/{userId}/displayname - [ ] GET - /_matrix/client/r0/register/available +- [ ] GET - /_matrix/client/r0/user/{userId}/account_data/{type} +- [ ] GET - /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type} - [ ] GET - /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags - [ ] POST - /_matrix/client/r0/account/3pid - [ ] POST - /_matrix/client/r0/account/3pid/delete diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 1a24790..d7206b0 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -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.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; diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index edd4d51..14e2b21 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -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)? + ) { } +} -- cgit v1.2.3-54-g00ecf From f4275ca847f6d7021ad2d720b88a592d03436997 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 19:07:56 -0300 Subject: Bump version --- META6.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/META6.json b/META6.json index 7329156..7d9d358 100644 --- a/META6.json +++ b/META6.json @@ -34,5 +34,5 @@ "Test", "Test::META" ], - "version" : "0.4.0" + "version" : "0.5.0" } -- cgit v1.2.3-54-g00ecf From f1cae9e3a68fc5aecbe5656bc812a758d8d62fee Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 20:16:48 -0300 Subject: Update readme --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 43c1aac..bdb8067 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,53 @@ -# Matrix client +# Matrix::Client A perl 6 library for [Matrix](https://matrix.org). -## Status +## Synopsis -This project is in early development. A lot of methods return a raw -`HTTP::Response` and not something from this library. -## Examples + use Matrix::Client; -From the `examples` directory: + my $client = Matrix::Client.new( + :home-server, + :device-id + ); - use v6; - use Matrix::Client; + $client.login(:username, :password); + + # Check my user + say $client.whoami; # @myuser:matrix.org + + # Send a message to a random room that I'm in + my $room = $client.joined-rooms.pick; + say "Sending a message to {$room.name}"; + $room.send("Hello from perl6!"); + +## Description + +Matrix is an open network for secure, decentralized communication. + +This module provides an interface to interact with a Matrix homeserver through +the *Client-Server API*. It's currenlty on active development but it's mostly +stable for day to day use. + +Here's a not complete list of things that can be done: + +* Login/logout +* Registration +* Synchronization of events/messages +* Send events +* Send messages +* Upload files to a home-server + + +There are many missing endpoints (you can check a complete checklist +[here](https://github.com/matiaslina/perl6-matrix-client/blob/master/endpoints.md)). + +## Documentation - # Instantiate a new client for a given home-server - my $client = Matrix::Client.new: :home-server - # Login - $client.login: @*ARGS[0], @*ARGS[1]; +There's a couple of pages of documentation on the `docs/` directory. This +includes API documentation, basic usage, examples, etc. - # Show all joined rooms - say $client.rooms(:sync); +## Author - # And finally logout. - $client.logout +Matías Linares | Matrix ID: `@matias:matrix.deprecated.org` -- cgit v1.2.3-54-g00ecf From ec4393cb81952fffa2273e313818a57c9611c5a2 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Thu, 11 Jul 2019 19:53:05 -0300 Subject: Use JSON::Fast --- META6.json | 4 ++-- lib/Matrix/Client.pm6 | 2 +- lib/Matrix/Client/Requester.pm6 | 2 +- lib/Matrix/Client/Room.pm6 | 2 +- lib/Matrix/Response.pm6 | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/META6.json b/META6.json index 7d9d358..d31ee73 100644 --- a/META6.json +++ b/META6.json @@ -4,7 +4,7 @@ ], "build-depends" : [ ], "depends" : [ - "JSON::Tiny", + "JSON::Fast", "HTTP::UserAgent", "URI::Encode", "IO::Socket::SSL" @@ -34,5 +34,5 @@ "Test", "Test::META" ], - "version" : "0.5.0" + "version" : "0.5.1" } diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index d7206b0..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; diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index edaf2da..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; 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 14e2b21..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.List -> $ev { @presence.push(Matrix::Response::Event.new(|$ev)); } -- cgit v1.2.3-54-g00ecf