aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Matrix')
-rw-r--r--lib/Matrix/Client.pm645
-rw-r--r--lib/Matrix/Client/Room.pm633
-rw-r--r--lib/Matrix/Response.pm69
3 files changed, 83 insertions, 4 deletions
diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6
index 5f52779..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<device_id>;
}
+#| 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<displayname> // ""
}
+#| 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<avatar_url> // ""
}
+#| 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,23 +116,45 @@ 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));
}
+#| PUT - /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}
+multi method tags(Str $room-id, Str:D $tag, $order) {
+ my $id = $.whoami;
+ from-json($.put("/user/$id/rooms/$room-id/tags/$tag", :$order).content)
+}
+
+#| GET - /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags
+multi method tags(Str $room-id) {
+ my $id = $.whoami;
+ Matrix::Response::Tag.new(from-json($.get("/user/$id/rooms/$room-id/tags").content))
+}
+
+#| DELETE - /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}
+method remove-tag(Str $room-id, Str:D $tag) {
+ my $id = $.whoami;
+ $.delete("/user/$id/rooms/$room-id/tags/$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,
@@ -133,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)
@@ -140,6 +173,7 @@ multi method sync(:$since = "") {
# Rooms
+#| POST - /_matrix/client/r0/createRoom
method create-room(
Bool :$public = False,
*%args --> Matrix::Client::Room
@@ -163,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);
@@ -183,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(
@@ -197,23 +236,27 @@ method send(Str $room-id, Str $body, :$type? = "m.text") {
from-json($res.content)<event_id>
}
+#| GET - /_matrix/client/r0/directory/room/{roomAlias}
method get-room-id($room-alias) {
my $res = $.get("/directory/room/$room-alias");
from-json($res.content)<room_id>
}
+#| 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 fa4501a..4ec774f 100644
--- a/lib/Matrix/Client/Room.pm6
+++ b/lib/Matrix/Client/Room.pm6
@@ -25,11 +25,34 @@ method !get-name() {
$!name = $data<name>;
}
-method name() {
- unless $!name.so { self!get-name() }
+#| GET - /_matrix/client/r0/rooms/{roomId}/joined_members
+method joined-members {
+ my %data = from-json($.get("/joined_members").content);
+ %data<joined>
+}
+
+method name {
+ self!get-name;
+
$!name
}
+method fallback-name(--> Str) {
+ my @members = $.joined-members.kv.map(
+ -> $k, %v {
+ %v<display_name> // $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" }
+ };
+}
+
+#| GET - /_matrix/client/r0/rooms/{roomId}/messages
method messages() {
my $res = $.get("/messages");
my $data = from-json($res.content);
@@ -37,6 +60,7 @@ method messages() {
return $data<chunk>.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(
@@ -47,6 +71,7 @@ method send(Str $body!, Str :$type? = "m.text") {
from-json($res.content)<event_id>
}
+#| GET - /_matrix/client/r0/rooms/{roomId}/state
multi method state(--> Seq) {
my $data = from-json($.get('/state').content);
@@ -55,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",
@@ -67,7 +94,7 @@ method send-state(Str:D $event-type, :$state-key = "", *%args --> Str) {
from-json($res.content)<event_id>
}
-
+#| POST - /_matrix/client/r0/rooms/{roomId}/leave
method leave() {
$.post('/leave')
}
diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6
index d8f68f0..edd4d51 100644
--- a/lib/Matrix/Response.pm6
+++ b/lib/Matrix/Response.pm6
@@ -113,3 +113,12 @@ class Presence {
:currently_active(:$!currently-active) = False
) { }
}
+
+class Tag {
+ has @.tags;
+
+ method new(%json) {
+ my @tags = %json<tags>.keys;
+ self.bless(:@tags)
+ }
+}