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') 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