From aff2a2eeb6fba35636a863fdf48a3ad08c335877 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 18 Mar 2019 16:11:30 -0300 Subject: Better room name --- lib/Matrix/Client/Room.pm6 | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'lib/Matrix/Client') diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index fa4501a..4c51be6 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -12,11 +12,26 @@ submethod TWEAK { $!url-prefix = "/rooms/$!id"; } +method !format-name-from-members { + my @members = $.joined-members.kv.map( + -> $k, %v { + %v // $k + } + ); + + $!name = do given @members.elems { + when 1 { @members.first } + when 2 { @members[0] ~ " and " ~ @members[1] } + when * > 2 { @members.first ~ " and {@members.elems - 1} others" } + default { "Empty room" } + }; +} + method !get-name() { CATCH { when X::Matrix::Response { .code ~~ /M_NOT_FOUND/ - ?? ($!name = '') + ?? self!format-name-from-members !! fail } default { fail } @@ -25,8 +40,18 @@ method !get-name() { $!name = $data; } +method joined-members { + my %data = from-json($.get("/joined_members").content); + %data +} + method name() { - unless $!name.so { self!get-name() } + if $!name { + return $!name; + } + + self!get-name; + $!name } @@ -67,7 +92,6 @@ method send-state(Str:D $event-type, :$state-key = "", *%args --> Str) { from-json($res.content) } - method leave() { $.post('/leave') } -- cgit v1.2.3-70-g09d2 From 22e9276301e5c9b1a9a38148a5c9ba4194218cea Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 18 Mar 2019 17:12:04 -0300 Subject: Separate fallback name from name. --- lib/Matrix/Client/Room.pm6 | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'lib/Matrix/Client') diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index 4c51be6..e0b14a2 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -12,26 +12,11 @@ submethod TWEAK { $!url-prefix = "/rooms/$!id"; } -method !format-name-from-members { - my @members = $.joined-members.kv.map( - -> $k, %v { - %v // $k - } - ); - - $!name = do given @members.elems { - when 1 { @members.first } - when 2 { @members[0] ~ " and " ~ @members[1] } - when * > 2 { @members.first ~ " and {@members.elems - 1} others" } - default { "Empty room" } - }; -} - method !get-name() { CATCH { when X::Matrix::Response { .code ~~ /M_NOT_FOUND/ - ?? self!format-name-from-members + ?? ($!name = '') !! fail } default { fail } @@ -45,16 +30,27 @@ method joined-members { %data } -method name() { - if $!name { - return $!name; - } - +method name { self!get-name; $!name } +method fallback-name(--> Str) { + my @members = $.joined-members.kv.map( + -> $k, %v { + %v // $k + } + ); + + $!name = do given @members.elems { + when 1 { @members.first } + when 2 { @members[0] ~ " and " ~ @members[1] } + when * > 2 { @members.first ~ " and {@members.elems - 1} others" } + default { "Empty room" } + }; +} + method messages() { my $res = $.get("/messages"); my $data = from-json($res.content); -- cgit v1.2.3-70-g09d2 From ecf602241f38552132e16d44a8871232b23f2b1e Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Wed, 5 Jun 2019 00:30:56 -0300 Subject: Add POD declarators --- lib/Matrix/Client.pm6 | 27 ++++++++++++++++++++++++++- lib/Matrix/Client/Room.pm6 | 7 +++++++ 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'lib/Matrix/Client') diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index bf3935c..2fdbf4b 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -17,11 +17,12 @@ submethod TWEAK { $Matrix::Client::Common::TXN-ID = now.Int; } - +#| POST - /_matrix/client/r0/login multi method login(Str $username, Str $password) { $.login(:$username, :$password); } +#| POST - /_matrix/client/r0/login multi method login(Str :$username, Str :$password) { my $post-data = { type => "m.login.password", @@ -41,10 +42,12 @@ multi method login(Str :$username, Str :$password) { $!device-id = $data; } +#| POST - /_matrix/client/r0/logout method logout() { $.post("/logout") } +#| POST - /_matrix/client/r0/register method register($username, $password, Bool :$bind-email? = False) { my $res = $.post("/register", username => $username, password => $password, @@ -59,11 +62,13 @@ method register($username, $password, Bool :$bind-email? = False) { # User Data +#| GET - /_matrix/client/r0/profile/{userId} method profile(Str :$user-id?) { my $id = $user-id // $.whoami; from-json($.get("/profile/" ~ $id).content); } +#| GET - /_matrix/client/r0/profile/{userId}/displayname method display-name(Str :$user-id?) { my $id = $user-id // $.whoami; my $res = $.get("/profile/" ~ $id ~ "/displayname"); @@ -73,11 +78,13 @@ method display-name(Str :$user-id?) { $data // "" } +#| PUT - /_matrix/client/r0/profile/{userId}/displayname method change-display-name(Str:D $display-name!) { so $.put("/profile/" ~ $.whoami ~ "/displayname", displayname => $display-name) } +#| GET - /_matrix/client/r0/profile/{userId}/avatar_url method avatar-url(Str :$user-id?) { my $id = $user-id // $.whoami; my $res = $.get("/profile/" ~ $id ~ "/avatar_url"); @@ -86,16 +93,19 @@ method avatar-url(Str :$user-id?) { $data // "" } +#| PUT - /_matrix/client/r0/profile/{userId}/avatar_url multi method change-avatar(IO::Path $avatar) { my $mxc-url = $.upload($avatar.IO); samewith($mxc-url); } +#| PUT - /_matrix/client/r0/profile/{userId}/avatar_url multi method change-avatar(Str:D $mxc-url!) { $.put("/profile/" ~ $.whoami ~ "/avatar_url", avatar_url => $mxc-url); } +#| GET - /_matrix/client/r0/account/whoami method whoami { unless $!user-id { my $res = $.get('/account/whoami'); @@ -106,12 +116,14 @@ method whoami { $!user-id } +#| GET - /_matrix/client/r0/presence/{userId}/status method presence(Matrix::Client:D: $user-id? --> Matrix::Response::Presence) { my $id = $user-id // $.whoami; my $data = from-json($.get("/presence/$id/status").content); Matrix::Response::Presence.new(|$data) } +#| PUT - /_matrix/client/r0/presence/{userId}/status method set-presence(Matrix::Client:D: Str $presence, Str :$status-message = "") { $.put("/presence/$.whoami/status", :$presence, :status_msg($status-message)); @@ -137,10 +149,12 @@ method remove-tag(Str $room-id, Str:D $tag) { # Syncronization +#| GET - /_matrix/client/r0/sync multi method sync(Hash :$sync-filter is copy, :$since = "") { $.sync(sync-filter => to-json($sync-filter), since => $since) } +#| GET - /_matrix/client/r0/sync multi method sync(Str:D :$sync-filter, Str :$since = "") { my $res = $.get("/sync", timeout => 30000, @@ -151,6 +165,7 @@ multi method sync(Str:D :$sync-filter, Str :$since = "") { Matrix::Response::Sync.new($res.content) } +#| GET - /_matrix/client/r0/sync multi method sync(:$since = "") { my $res = $.get("/sync", timeout => 30000, since => $since); Matrix::Response::Sync.new($res.content) @@ -158,6 +173,7 @@ multi method sync(:$since = "") { # Rooms +#| POST - /_matrix/client/r0/createRoom method create-room( Bool :$public = False, *%args --> Matrix::Client::Room @@ -181,14 +197,17 @@ method create-room( ) } +#| POST - /_matrix/client/r0/join/{roomIdOrAlias} method join-room($room-id!) { $.post("/join/$room-id") } +#| POST - /_matrix/client/r0/rooms/{roomId}/leave method leave-room($room-id) { $.post("/rooms/$room-id/leave"); } +#| GET - /_matrix/client/r0/joined_rooms method joined-rooms(--> Seq) { my $res = $.get('/joined_rooms'); my $data = from-json($res.content); @@ -201,10 +220,12 @@ method joined-rooms(--> Seq) { }); } +#| GET - /_matrix/client/r0/publicRooms method public-rooms() { $.get('/publicRooms') } +#| PUT - /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} method send(Str $room-id, Str $body, :$type? = "m.text") { $Matrix::Client::Common::TXN-ID++; my $res = $.put( @@ -215,23 +236,27 @@ method send(Str $room-id, Str $body, :$type? = "m.text") { from-json($res.content) } +#| GET - /_matrix/client/r0/directory/room/{roomAlias} method get-room-id($room-alias) { my $res = $.get("/directory/room/$room-alias"); from-json($res.content) } +#| PUT - /_matrix/client/r0/directory/room/{roomAlias} method add-room-alias($room-id, $room-alias) { $.put("/directory/room/$room-alias", room_id => $room-id); } +#| DELETE - /_matrix/client/r0/directory/room/{roomAlias} method remove-room-alias($room-alias) { $.delete("/directory/room/$room-alias"); } # Media +#| POST - /_matrix/media/r0/upload method upload(IO::Path $path, Str $filename?) { my $buf = slurp $path, :bin; my $fn = $filename ?? $filename !! $path.basename; diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index e0b14a2..4ec774f 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -25,6 +25,7 @@ method !get-name() { $!name = $data; } +#| GET - /_matrix/client/r0/rooms/{roomId}/joined_members method joined-members { my %data = from-json($.get("/joined_members").content); %data @@ -51,6 +52,7 @@ method fallback-name(--> Str) { }; } +#| GET - /_matrix/client/r0/rooms/{roomId}/messages method messages() { my $res = $.get("/messages"); my $data = from-json($res.content); @@ -58,6 +60,7 @@ method messages() { return $data.clone; } +#| PUT - /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} method send(Str $body!, Str :$type? = "m.text") { $Matrix::Client::Common::TXN-ID++; my $res = $.put( @@ -68,6 +71,7 @@ method send(Str $body!, Str :$type? = "m.text") { from-json($res.content) } +#| GET - /_matrix/client/r0/rooms/{roomId}/state multi method state(--> Seq) { my $data = from-json($.get('/state').content); @@ -76,10 +80,12 @@ multi method state(--> Seq) { } } +#| GET - /_matrix/client/r0/rooms/{roomId}/state/{eventType} multi method state(Str $event-type) { from-json($.get("/state/$event-type").content) } +#| PUT - /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey} method send-state(Str:D $event-type, :$state-key = "", *%args --> Str) { my $res = $.put( "/state/$event-type/$state-key", @@ -88,6 +94,7 @@ method send-state(Str:D $event-type, :$state-key = "", *%args --> Str) { from-json($res.content) } +#| POST - /_matrix/client/r0/rooms/{roomId}/leave method leave() { $.post('/leave') } -- cgit v1.2.3-70-g09d2