aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2018-01-28 18:09:30 -0300
committerMatias Linares <matiaslina@openmailbox.org>2018-01-28 18:09:30 -0300
commit6af45e001a3dffe83178eacb85c18cf62c6485ce (patch)
treef2bd99d143297d71f9efbef65b0d4dbed2259e4c
parent43f17e1e6db343a35ae40bf937e970c85d7f295f (diff)
downloadperl6-matrix-client-6af45e001a3dffe83178eacb85c18cf62c6485ce.tar.gz
Refactor Matrix::Client::Room
* Add name() method * Add leave() endpoint * Better BUILD and gist
-rw-r--r--lib/Matrix/Client/Room.pm630
1 files 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<timeline><prev_batch>;
if so $json {
my @events = $json<state><events>.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)<name>
+ } 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<name: $.name, id: $.id>"
+ my $name = self.name();
+ "Room<name: {self.name()}, id: {self.id}>"
}