From 5dcec83e6430da3abb0e5725b58fab85bd0adb8f Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Wed, 12 Apr 2017 19:20:38 -0300 Subject: Rework login/BUILD methods --- lib/Matrix/Client.pm6 | 42 ++++++++++++++++++++++++++--------------- lib/Matrix/Client/Requester.pm6 | 2 +- 2 files changed, 28 insertions(+), 16 deletions(-) (limited to 'lib') 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; + $!user-id = $data; + $!device-id = $data; + $Matrix::Client::Common::TXN-ID = $data // 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; - $!user-id = $data; - $!device-id = $data; - $Matrix::Client::Common::TXN-ID = $data // 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; $!user-id = $data; @@ -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.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 diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index ec9a2d1..f6244e3 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -17,7 +17,7 @@ method get(Str $path, :$media = False, *%data) { for %data.kv -> $k,$v { $q ~= "&$k=$v" unless $v eq ""; } - my $uri = uri_encode($.base-url($media) ~ $q); + my $uri = uri_encode($.base-url(:$media) ~ $q); $!ua.history = []; $!ua.get($uri) -- cgit v1.2.3-54-g00ecf