From d5b3361c9ceeac3ed3a90dab35b12b0bd4ab7612 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 26 Dec 2020 22:33:31 -0300 Subject: Implement membership endpoints for Matrix::Client::Room This implements for room endpoints: * Join a room * Get a single event * Get states * Get room aliases * Get room members --- lib/Matrix/Client/Room.pm6 | 116 ++++++++++++++++++++++++++++++++++++--------- lib/Matrix/Response.pm6 | 4 ++ 2 files changed, 97 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index ca97d2f..72dfb62 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -25,15 +25,8 @@ 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 -} - -method name { +method name(--> Str) { self!get-name; - $!name } @@ -52,6 +45,42 @@ method fallback-name(--> Str) { }; } +#| GET - /_matrix/client/r0/rooms/{roomId}/aliases +method aliases(--> List) { + my %data = from-json($.get('/aliases').content); + %data.List +} + +# Events + +## Getting events for a room + +#| GET - /_matrix/client/r0/rooms/{roomId}/event/{eventId} +method event(Str $event-id --> Matrix::Response::RoomEvent) { + my %data = from-json($.get("/event/$event-id").content); + Matrix::Response::RoomEvent.new(|%data) +} + +#| GET - /_matrix/client/r0/rooms/{roomId}/state +multi method state(--> Seq) { + my $data = from-json($.get('/state').content); + + gather for $data.List -> $event { + take Matrix::Response::StateEvent.new(:room-id($.id), |$event) + } +} + +#| GET - /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey} +multi method state(Str $event-type, Str $state-key = "") { + from-json($.get("/state/$event-type/$state-key").content) +} + +#| GET - /_matrix/client/r0/rooms/{roomId}/joined_members +method joined-members { + my %data = from-json($.get("/joined_members").content); + %data +} + #| GET - /_matrix/client/r0/rooms/{roomId}/messages method messages() { my $res = $.get("/messages"); @@ -60,6 +89,23 @@ method messages() { return $data.clone; } +#| GET - /_matrix/client/r0/rooms/{roomId}/members +method members(:$at, Str :$membership, Str :$not-membership) { + my %query; + + %query = $at with $at; + %query = $membership with $membership; + %query = $not-membership with $not-membership; + + my %data = from-json($.get('/members', |%query).content); + + gather for %data.List -> $ev { + take Matrix::Response::MemberEvent.new(|$ev) + } +} + +## Sending events to a room + #| PUT - /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} method send(Str $body!, Str :$type? = "m.text") { $Matrix::Client::Common::TXN-ID++; @@ -71,20 +117,6 @@ 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); - - gather for $data.List -> $event { - take Matrix::Response::StateEvent.new(:room-id($.id), |$event) - } -} - -#| 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( @@ -94,11 +126,49 @@ method send-state(Str:D $event-type, :$state-key = "", *%args --> Str) { from-json($res.content) } +# Room membership! + +## Joining rooms + +#| POST - /_matrix/client/r0/rooms/{roomId}/invite +method invite(Str $user-id) { + $.post('/invite', :$user-id) +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/join +method join { + $.post('/join') +} + +## Leaving rooms + #| POST - /_matrix/client/r0/rooms/{roomId}/leave -method leave() { +method leave { $.post('/leave') } +#| POST - /_matrix/client/r0/rooms/{roomId}/forget +method forget { + $.post('/forget') +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/kick +method kick(Str $user-id, Str $reason = "") { + $.post('/kick', :$user-id, :$reason) +} + +## Banning users + +#| POST - /_matrix/client/r0/rooms/{roomId}/ban +method ban(Str $user-id, $reason = "") { + $.post('/ban', :$user-id, :$reason) +} + +#| POST - /_matrix/client/r0/rooms/{roomId}/unban +method unban(Str $user-id) { + $.post('/unban', :$user-id) +} + method Str(--> Str) { "Room" } diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index ec93cf3..cc9e7a8 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -23,6 +23,10 @@ class Matrix::Response::StateEvent is Matrix::Response::RoomEvent { has $.state_key; } +class Matrix::Response::MemberEvent is Matrix::Response::StateEvent { + has $.type is required where 'm.room.member'; +} + class Matrix::Response::Timeline { has Matrix::Response::Event @.events; has Bool $limited; -- cgit v1.2.3-70-g09d2