aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Matrix')
-rw-r--r--lib/Matrix/Client.pm657
-rw-r--r--lib/Matrix/Client/Requester.pm617
2 files changed, 29 insertions, 45 deletions
diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6
index fe3e259..b9848a2 100644
--- a/lib/Matrix/Client.pm6
+++ b/lib/Matrix/Client.pm6
@@ -8,57 +8,36 @@ use Matrix::Client::Requester;
unit class Matrix::Client does Matrix::Client::Requester;
-has Str $.user-id;
has Str $.device-id;
-has Str $!auth-file;
-has $!logged = False;
+has Str $!user-id;
has @!rooms;
has @!users;
-submethod BUILD(:$!home-server!, :$!auth-file = 'auth') {
- if $!auth-file.IO.e {
- my $data = from-json(slurp $!auth-file);
- $!access-token = $data<access_token>;
- $!user-id = $data<user_id>;
- $!device-id = $data<device_id>;
- $Matrix::Client::Common::TXN-ID = now.Int;
- $!logged = True;
- }
+submethod TWEAK {
+ $Matrix::Client::Common::TXN-ID = now.Int;
}
-method login(Str $username, Str $pass) returns Bool {
- return if $!logged;
- # Handle POST
- my $post-data = to-json {
+method login(Str :$username, Str :$password, Str :$device-id?) {
+ my $post-data = {
type => "m.login.password",
user => $username,
- password => $pass
+ password => $password
};
- my $res = $.post("/login", $post-data);
- spurt $!auth-file, $res.content;
+ if $!device-id {
+ $post-data<device_id> = $!device-id;
+ }
+ my $res = $.post("/login", to-json($post-data));
my $data = from-json($res.content);
+
$!access-token = $data<access_token>;
$!user-id = $data<user_id>;
$!device-id = $data<device_id>;
-
- True
-}
-
-method save-auth-data() {
- my %data =
- access_token => $!access-token,
- user_id => $.user-id,
- device_id => $.device-id,
- txn_id => $Matrix::Client::Common::TXN-ID;
-
- spurt $!auth-file, to-json(%data);
}
method logout() {
- unlink $!auth-file;
$.post("/logout")
}
@@ -71,18 +50,18 @@ method register($username, $password, Bool :$bind-email? = False) {
});
my $data = from-json $res.content;
$!access-token = $data<access_token>;
- $.user-id = $data<user_id>;
+ $!user-id = $data<user_id>;
}
# User Data
method profile(Str :$user-id?) {
- my $id = $user-id // $.user-id;
- $.get("/profile/" ~ $id)
+ my $id = $user-id // $!user-id;
+ from-json($.get("/profile/" ~ $id).content);
}
method display-name(Str :$user-id?) {
- my $id = $user-id // $.user-id;
+ my $id = $user-id // $!user-id;
my $res = $.get("/profile/" ~ $id ~ "/displayname");
my $data = from-json($res.content);
@@ -91,12 +70,12 @@ method display-name(Str :$user-id?) {
}
method change-display-name(Str:D $display-name!) {
- $.put("/profile/" ~ $.user-id ~ "/displayname",
+ so $.put("/profile/" ~ $!user-id ~ "/displayname",
displayname => $display-name)
}
method avatar-url(Str :$user-id?) {
- my $id = $user-id // $.user-id;
+ my $id = $user-id // $!user-id;
my $res = $.get("/profile/" ~ $id ~ "/avatar_url");
my $data = from-json($res.content);
@@ -109,7 +88,7 @@ multi method change-avatar(IO::Path $avatar) {
}
multi method change-avatar(Str:D $mxc-url!) {
- $.put("/profile/" ~ $.user-id ~ "/avatar_url",
+ $.put("/profile/" ~ $!user-id ~ "/avatar_url",
avatar_url => $mxc-url);
}
diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6
index ca1f0e1..4bf6b14 100644
--- a/lib/Matrix/Client/Requester.pm6
+++ b/lib/Matrix/Client/Requester.pm6
@@ -6,11 +6,12 @@ use Matrix::Client::Exception;
unit role Matrix::Client::Requester;
-has $!ua = HTTP::UserAgent.new;
has $.home-server is required;
+has $.access-token = "";
+
+has $!ua = HTTP::UserAgent.new;
has $!client-endpoint = "/_matrix/client/r0";
has $!url-prefix = "";
-has $!access-token = "";
has $!sync-since = "";
method !handle-error($response) {
@@ -21,8 +22,12 @@ method !handle-error($response) {
$response
}
+method !access-token-arg {
+ $!access-token ?? "access_token=$!access-token" !! ''
+}
+
method get(Str $path, :$media = False, *%data) {
- my $q = "$path?access_token=$!access-token";
+ my $q = "$path?{self!access-token-arg}";
for %data.kv -> $k,$v {
$q ~= "&$k=$v" if $v.so;
}
@@ -40,7 +45,7 @@ method base-url(Bool :$media? = False --> Str) {
}
multi method post(Str $path, Str $json, :$media = False) {
- my $url = $.base-url(:$media) ~ $path ~ "?access_token=$!access-token";
+ my $url = $.base-url(:$media) ~ $path ~ "?{self!access-token-arg}";
my $req = HTTP::Request.new(POST => $url,
Content-Type => 'application/json');
$req.add-content($json);
@@ -53,12 +58,12 @@ multi method post(Str $path, :$media = False, *%params) {
}
method post-bin(Str $path, Buf $buf, :$content-type) {
- my $req = POST($.base-url(:media) ~ $path ~ "?access_token=$!access-token", content => $buf, Content-Type => $content-type);
+ my $req = POST($.base-url(:media) ~ $path ~ "?{self!access-token-arg}", content => $buf, Content-Type => $content-type);
return self!handle-error($!ua.request($req));
}
multi method put(Str $path, Str $json) {
- my $req = HTTP::Request.new(PUT => $.base-url() ~ $path ~ "?access_token=$!access-token",
+ my $req = HTTP::Request.new(PUT => $.base-url() ~ $path ~ "?{self!access-token-arg}",
Content-Type => 'application/json');
$req.add-content($json);
return self!handle-error($!ua.request($req))