From 9c439e653e19472c242fc0bff10e31a57b1ea45f Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 3 Apr 2017 22:23:57 -0300 Subject: Initial commit --- lib/Matrix/Client/Common.pm6 | 3 +++ lib/Matrix/Client/Requester.pm6 | 52 +++++++++++++++++++++++++++++++++++++++++ lib/Matrix/Client/Room.pm6 | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 lib/Matrix/Client/Common.pm6 create mode 100644 lib/Matrix/Client/Requester.pm6 create mode 100644 lib/Matrix/Client/Room.pm6 (limited to 'lib/Matrix/Client') diff --git a/lib/Matrix/Client/Common.pm6 b/lib/Matrix/Client/Common.pm6 new file mode 100644 index 0000000..670f8e3 --- /dev/null +++ b/lib/Matrix/Client/Common.pm6 @@ -0,0 +1,3 @@ +unit module Matrix::Client::Common; + +our $TXN-ID = 0; diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 new file mode 100644 index 0000000..c2ba865 --- /dev/null +++ b/lib/Matrix/Client/Requester.pm6 @@ -0,0 +1,52 @@ +use HTTP::UserAgent; +use HTTP::Request::Common; +use URI::Encode; +use JSON::Tiny; + +unit role Matrix::Client::Requester; + +has $!ua = HTTP::UserAgent.new; +has $.home-server is required; +has $!client-endpoint = "/_matrix/client/r0"; +has $!url-prefix = ""; +has $!access-token = ""; +has $!sync-since = ""; + +method get(Str $path, *%data) { + my $q = "$path?access_token=$!access-token"; + for %data.kv -> $k,$v { + $q ~= "&$k=$v" unless $v eq ""; + } + my $uri = uri_encode($.base-url ~ $q); + + $!ua.history = []; + $!ua.get($uri) +} + +method base-url(--> Str) { + "$.home-server$!client-endpoint$!url-prefix" +} + +multi method post(Str $path, Str $json) { + my $req = HTTP::Request.new(POST => $.base-url() ~ $path ~ "?access_token=$!access-token", + Content-Type => 'application/json'); + $req.add-content($json); + $!ua.history = []; + $!ua.request($req) +} + +multi method post(Str $path, *%params) { + self.post($path, to-json(%params)) +} + +multi method put(Str $path,Str $json) { + my $req = HTTP::Request.new(PUT => $.base-url() ~ $path ~ "?access_token=$!access-token", + Content-Type => 'application/json'); + $req.add-content($json); + $!ua.history = []; + $!ua.request($req) +} + +multi method put(Str $path, *%params) { + self.put($path, to-json(%params)) +} diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 new file mode 100644 index 0000000..5601f33 --- /dev/null +++ b/lib/Matrix/Client/Room.pm6 @@ -0,0 +1,46 @@ +use JSON::Tiny; +use Matrix::Client::Common; +use Matrix::Client::Requester; + +unit class Matrix::Client::Room does Matrix::Client::Requester; + +has $.name is rw; +has $.id is rw; +has $!prev-batch; + +submethod BUILD(Str :$id!, :$json, :$home-server!) { + $!home-server = $home-server; + $!id = $id; + $!url-prefix = "/rooms/$!id"; + $!prev-batch = $json; + + if so $json { + my @events = $json.clone; + for @events -> $ev { + if $ev eq "m.room.name" { + $!name = $ev; + } + } + } + + # FIXME: Should be a 1:1 conversation + unless $!name { + $!name = "Unknown"; + } +} + +method messages() { + my $res = $.get("/messages"); + my $data = from-json($res.content); + + return $data.clone; +} + +method send($room-id, Str $body!, Str :$type? = "m.text") { + $Matrix::Client::Common::TXN-ID++; + $.put("/send/m.room.message/{$Matrix::Client::Common::TXN-ID}", msgtype => $type, body => $body) +} + +method gist(--> Str) { + "Room" +} -- cgit v1.2.3-70-g09d2