From 5611f785dc62d7af68adc3ba9bd2c5199c38d96a Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 9 Jun 2018 16:43:35 -0300 Subject: Add presence methods. This adds a way to change and query the presence for users. Closes #4 --- docs/client.pod6 | 14 ++++++++++++++ lib/Matrix/Client.pm6 | 11 +++++++++++ lib/Matrix/Response.pm6 | 14 ++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/docs/client.pod6 b/docs/client.pod6 index a441b7c..0ff1e95 100644 --- a/docs/client.pod6 +++ b/docs/client.pod6 @@ -104,6 +104,20 @@ and then set that uploaded image as avatar. Returns the user id of the client account. +=head2 presence + + method presence(Matrix::Client:D: $user-id? --> Matrix::Response::Presence) + +Query the presence status for an user. if no C<$user-id> is passed as argument, +it will return the presence of the user associated with the client. + +=head2 set-presence + + method set-presence(Matrix::Client:D: Str $presence, Str :$status-message = "") + +Sets the manually the presence of the client account. The C<$presence> argument +must be C<“online”>, C<“offline”> or C<“unavailable”>. + =head2 sync multi method sync(:$since = "") diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 56be81c..0d4f400 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -106,6 +106,17 @@ method whoami { $!user-id } +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) +} + +method set-presence(Matrix::Client:D: Str $presence, Str :$status-message = "") { + $.put("/presence/$.whoami/status", + :$presence, :status_msg($status-message)); +} + # Syncronization multi method sync(Hash :$sync-filter is copy, :$since = "") { diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index 57417b7..d8f68f0 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -99,3 +99,17 @@ class Matrix::Response::Sync { :@joined-rooms, :@invited-rooms); } } + +class Presence { + has Str $.presence is required; + has Int $.last-active-ago; + has Str $.status-message; + has Bool $.currently-active; + + submethod BUILD( + Str :$!presence, + :last_active_ago(:$!last-active-ago) = 0, + :status_message(:$!status-message) = "", + :currently_active(:$!currently-active) = False + ) { } +} -- cgit v1.2.3-54-g00ecf