summaryrefslogtreecommitdiff
path: root/lib/Matrix/Client.pm6
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2017-04-12 19:20:38 -0300
committerMatias Linares <matiaslina@openmailbox.org>2017-04-12 19:20:38 -0300
commit5dcec83e6430da3abb0e5725b58fab85bd0adb8f (patch)
treedfb559eac191cdbdc988ee7f95b865fa33471943 /lib/Matrix/Client.pm6
parent2af030ffb9dd31f1fd4d1b0c6fbb901b23575a81 (diff)
downloadperl6-matrix-client-5dcec83e6430da3abb0e5725b58fab85bd0adb8f.tar.gz
Rework login/BUILD methods
Diffstat (limited to 'lib/Matrix/Client.pm6')
-rw-r--r--lib/Matrix/Client.pm642
1 files changed, 27 insertions, 15 deletions
diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6
index 8fc9e6a..1708407 100644
--- a/lib/Matrix/Client.pm6
+++ b/lib/Matrix/Client.pm6
@@ -9,10 +9,22 @@ unit class Matrix::Client does Matrix::Client::Requester;
has Str $!user-id;
has Str $!device-id;
-has Str $.state-file = 'state';
+has Str $!auth-file;
+has $!logged = False;
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 = $data<txn_id> // 0;
+ $!logged = True;
+ }
+}
+
method user-id() {
$!user-id
}
@@ -22,14 +34,7 @@ method device-id() {
}
method login(Str $username, Str $pass) returns Bool {
- if $.state-file.IO.e {
- my $data = from-json(slurp $.state-file);
- $!access-token = $data<access_token>;
- $!user-id = $data<user_id>;
- $!device-id = $data<device_id>;
- $Matrix::Client::Common::TXN-ID = $data<txn_id> // 0;
- return True
- }
+ return True if $!logged;
# Handle POST
my $data = to-json {
@@ -40,7 +45,7 @@ method login(Str $username, Str $pass) returns Bool {
my $res = $.post("/login", $data);
if $res.is-success {
- spurt $.state-file, $res.content;
+ spurt $!auth-file, $res.content;
my $data = from-json($res.content);
$!access-token = $data<access_token>;
$!user-id = $data<user_id>;
@@ -51,18 +56,18 @@ method login(Str $username, Str $pass) returns Bool {
}
}
-method finish() {
+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 $.state-file, to-json(%data);
+ spurt $!auth-file, to-json(%data);
}
method logout() {
- unlink $.state-file;
+ unlink $!auth-file;
$.post("/logout")
}
@@ -171,13 +176,20 @@ method join-room($room-id!) {
$.post("/join/" ~ $room-id)
}
-method rooms() {
+method rooms(Bool :$sync = False) {
+ return @!rooms unless $sync;
my $res = $.get("/sync", timeout => "30000");
return () unless $res.is-success;
+ @!rooms = ();
my $data = from-json($res.content);
for $data<rooms><join>.kv -> $id, $json {
- @!rooms.push(Matrix::Client::Room.new(id => $id, json => $json, home-server => $!home-server));
+ @!rooms.push(Matrix::Client::Room.new(
+ id => $id,
+ json => $json,
+ home-server => $!home-server,
+ access-token => $!access-token
+ ));
}
@!rooms