From 6af45e001a3dffe83178eacb85c18cf62c6485ce Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 28 Jan 2018 18:09:30 -0300 Subject: Refactor Matrix::Client::Room * Add name() method * Add leave() endpoint * Better BUILD and gist --- lib/Matrix/Client/Room.pm6 | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index 08caf0f..c264a80 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -4,13 +4,11 @@ use Matrix::Client::Requester; unit class Matrix::Client::Room does Matrix::Client::Requester; -has $.name is rw; -has $.id is rw; -has $!prev-batch; +has $!name; +has $.id; -submethod BUILD(Str :$!id!, :$json!, :$!home-server!, :$!access-token!) { +multi submethod BUILD(Str :$!id!, :$!home-server!, :$!access-token!, :$json?) { $!url-prefix = "/rooms/$!id"; - $!prev-batch = $json; if so $json { my @events = $json.clone; @@ -20,13 +18,22 @@ submethod BUILD(Str :$!id!, :$json!, :$!home-server!, :$!access-token!) { } } } +} - # FIXME: Should be a 1:1 conversation - unless $!name { - $!name = "Unknown"; +method !get-name() { + my $res = $.get('/state/m.room.name'); + if $res.is-success { + $!name = from-json($res.content) + } else { + warn "Error {$res.status-line}, content {$res.content}"; } } +method name() { + unless $!name.so { self!get-name() } + $!name +} + method messages() { my $res = $.get("/messages"); my $data = from-json($res.content); @@ -39,6 +46,11 @@ method send(Str $body!, Str :$type? = "m.text") { $.put("/send/m.room.message/{$Matrix::Client::Common::TXN-ID}", msgtype => $type, body => $body) } +method leave() { + $.post('/leave') +} + method gist(--> Str) { - "Room" + my $name = self.name(); + "Room" } -- cgit v1.2.3-70-g09d2