diff options
Diffstat (limited to 'lib/Matrix/Client')
-rw-r--r-- | lib/Matrix/Client/Common.rakumod (renamed from lib/Matrix/Client/Common.pm6) | 0 | ||||
-rw-r--r-- | lib/Matrix/Client/Exception.rakumod (renamed from lib/Matrix/Client/Exception.pm6) | 9 | ||||
-rw-r--r-- | lib/Matrix/Client/MediaStore.rakumod | 6 | ||||
-rw-r--r-- | lib/Matrix/Client/Requester.rakumod (renamed from lib/Matrix/Client/Requester.pm6) | 0 | ||||
-rw-r--r-- | lib/Matrix/Client/Response.rakumod | 156 | ||||
-rw-r--r-- | lib/Matrix/Client/Room.rakumod (renamed from lib/Matrix/Client/Room.pm6) | 16 |
6 files changed, 172 insertions, 15 deletions
diff --git a/lib/Matrix/Client/Common.pm6 b/lib/Matrix/Client/Common.rakumod index 83cf65d..83cf65d 100644 --- a/lib/Matrix/Client/Common.pm6 +++ b/lib/Matrix/Client/Common.rakumod diff --git a/lib/Matrix/Client/Exception.pm6 b/lib/Matrix/Client/Exception.rakumod index e02f572..6fb4c75 100644 --- a/lib/Matrix/Client/Exception.pm6 +++ b/lib/Matrix/Client/Exception.rakumod @@ -1,14 +1,15 @@ -package X::Matrix { - class Response is Exception { +module Matrix::Client::Exception { + + class X::Matrix::Response is Exception { has $.code; has $.error; - method message { + method message(--> Str) { "$!code: $!error" } } - class MXCParse is Exception { + class X::Matrix::MXCParse is Exception { has $.uri; method message { "Cannot parse '$!uri'" } diff --git a/lib/Matrix/Client/MediaStore.rakumod b/lib/Matrix/Client/MediaStore.rakumod index 9779db2..afea9e5 100644 --- a/lib/Matrix/Client/MediaStore.rakumod +++ b/lib/Matrix/Client/MediaStore.rakumod @@ -3,7 +3,7 @@ use URI::Escape; use Matrix::Client::Requester; use Matrix::Client::Exception; -use Matrix::Response; +use Matrix::Client::Response; unit class Matrix::Client::MediaStore does Matrix::Client::Requester; @@ -110,7 +110,7 @@ multi method thumbnail(Str $server-name, Str $media-id, } #| GET - /_matrix/media/r0/config -method config(--> Matrix::Response::MediaStore::Config) { +method config(--> Matrix::Client::Response::MediaStore::Config) { my $response = $.get("/config"); - Matrix::Response::MediaStore::Config.new(from-json($response.content)) + Matrix::Client::Response::MediaStore::Config.new(from-json($response.content)) } diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.rakumod index 36a9f69..36a9f69 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.rakumod diff --git a/lib/Matrix/Client/Response.rakumod b/lib/Matrix/Client/Response.rakumod new file mode 100644 index 0000000..3836a20 --- /dev/null +++ b/lib/Matrix/Client/Response.rakumod @@ -0,0 +1,156 @@ +use JSON::Fast; + +unit module Matrix::Client::Response; + +class Event { + has %.content; + has $.type is required; +} + +class RoomEvent is Event { + has Str $.sender; + has Int $.origin_server_ts; + has $.event_id; + has Str $.room_id; + + method id { $.event_id } + method timestamp { $!origin_server_ts } + method room-id { $.room_id } +} + +class StateEvent is RoomEvent { + has $.prev_content; + has $.state_key; +} + +class MemberEvent is StateEvent { + has $.type is required where 'm.room.member'; +} + +class Timeline { + has Event @.events; + has Bool $limited; + has Str $prev-batch; +} + +class RoomInfo { + has $.room-id is required; + has Event @.state; + has Timeline $.timeline; + + method gist(--> Str) { + "<RoomInfo: $.room-id>" + } +} + +class InviteInfo { + has $.room-id is required; + has Event @.events; + + method gist(--> Str) { + "<InviteState: $.room-id>" + } +} + +sub gather-events($room-id, $from) { + gather for $from<events>.List -> $ev { + take StateEvent.new(:room_id($room-id), |$ev); + } +} + +class Messages { + has $.start; + has $.end; + has RoomEvent @.messages; +} + +class Sync { + has Str $.next-batch; + has Event @.presence; + has RoomInfo @.joined-rooms; + has InviteInfo @.invited-rooms; + + multi method new(Str $json) { + return self.new(from-json($json)); + } + + multi method new(Hash $json) { + my $next-batch = $json<next_batch>; + my Event @presence; + my RoomInfo @joined-rooms; + my InviteInfo @invited-rooms; + + for $json<presence><events>.List -> $ev { + @presence.push(Event.new(|$ev)); + } + + for $json<rooms><join>.kv -> $room-id, $data { + my @state = gather-events($room-id, $data<state>); + + my $timeline = Timeline.new( + limited => $data<timeline><limited>, + prev-batch => $data<timeline><prev_batch>, + events => gather-events($room-id, $data<timeline>) + ); + + @joined-rooms.push(RoomInfo.new( + :$room-id, :$timeline, :@state + )); + } + + for $json<rooms><invite>.kv -> $room-id, $data { + my @events = gather-events($room-id, $data<invite_state>); + @invited-rooms.push(InviteInfo.new( + :$room-id, :@events + )); + } + + return self.bless(:$next-batch, :@presence, + :@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 + ) { } +} + +class Tag { + has @.tags; + + method new(%json) { + my @tags = %json<tags>.keys; + self.bless(:@tags) + } +} + +class Device { + has Str $.device-id; + has $.display-name; + has $.last-seen-ip; + has $.last-seen-ts; + + submethod BUILD( + Str :device_id(:$!device-id), + :display_name(:$!display-name)?, + :last_seen_ip(:$!last-seen-ip)?, + :last_seen_ts(:$!last-seen-ts)? + ) { } +} + +class MediaStore::Config { + has Int $.upload-size; + + method new(%config) { + self.bless(:upload-size(%config<m.upload.size> // Int)); + } +} diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.rakumod index 3d6ecaa..1e8c582 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.rakumod @@ -1,7 +1,7 @@ use JSON::Fast; use Matrix::Client::Common; use Matrix::Client::Requester; -use Matrix::Response; +use Matrix::Client::Response; unit class Matrix::Client::Room does Matrix::Client::Requester; @@ -56,9 +56,9 @@ method aliases(--> List) { ## Getting events for a room #| GET - /_matrix/client/r0/rooms/{roomId}/event/{eventId} -method event(Str $event-id --> Matrix::Response::RoomEvent) { +method event(Str $event-id --> Matrix::Client::Response::RoomEvent) { my %data = from-json($.get("/event/$event-id").content); - Matrix::Response::RoomEvent.new(|%data) + Matrix::Client::Response::RoomEvent.new(|%data) } #| GET - /_matrix/client/r0/rooms/{roomId}/state @@ -66,7 +66,7 @@ multi method state(--> Seq) { my $data = from-json($.get('/state').content); gather for $data.List -> $event { - take Matrix::Response::StateEvent.new(:room-id($.id), |$event) + take Matrix::Client::Response::StateEvent.new(:room-id($.id), |$event) } } @@ -86,7 +86,7 @@ method messages( Str:D :$from!, Str :$to, Str :$dir where * eq 'f'|'b' = 'f', Int :$limit = 10, :%filter, - --> Matrix::Response::Messages + --> Matrix::Client::Response::Messages ) { my $res = $.get( "/messages", :$from, :$to, :$dir, :$limit, :%filter @@ -95,10 +95,10 @@ method messages( my @messages = $data<chunk>.map(-> $ev { - Matrix::Response::RoomEvent.new(|$ev) + Matrix::Client::Response::RoomEvent.new(|$ev) }); - Matrix::Response::Messages.new( + Matrix::Client::Response::Messages.new( start => $data<start>, end => $data<end>, messages => @messages @@ -116,7 +116,7 @@ method members(:$at, Str :$membership, Str :$not-membership --> Seq) { my %data = from-json($.get('/members', |%query).content); gather for %data<chunk>.List -> $ev { - take Matrix::Response::MemberEvent.new(|$ev) + take Matrix::Client::Response::MemberEvent.new(|$ev) } } |