summaryrefslogtreecommitdiff
path: root/lib/Matrix/Client/Room.pm6
blob: 6897b8d5d62e0d1902fe0c35d6e534ac4222680f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use JSON::Tiny;
use Matrix::Client::Common;
use Matrix::Client::Requester;

unit class Matrix::Client::Room does Matrix::Client::Requester;

has $!name;
has $.id;

multi submethod BUILD(Str :$!id!, :$!home-server!, :$!access-token!, :$json?) {
    $!url-prefix = "/rooms/$!id";
    
    if so $json {
        my @events = $json<state><events>.clone;
        for @events -> $ev {
            if $ev<type> eq "m.room.name" {
                $!name = $ev<content><name>;
            }
        }
    }
}

method !get-name() {
    my $res = $.get('/state/m.room.name');
    if $res.is-success {
        $!name = from-json($res.content)<name>
    } else {
        warn "Error {$res.status-line}, content {$res.content}";
    }
}

method name() {
    unless $!name.so { self!get-name() }
    $!name
}

method messages() {
    my $res = $.get("/messages");
    my $data = from-json($res.content);

    return $data<chunk>.clone;
}

method send(Str $body!, Str :$type? = "m.text") {
    $Matrix::Client::Common::TXN-ID++;
    my $res = $.put(
        "/send/m.room.message/{$Matrix::Client::Common::TXN-ID}",
        msgtype => $type, body => $body
    );

    from-json($res.content)<event_id>
}

method leave() {
    $.post('/leave')
}

method gist(--> Str) {
    "Room<id: {self.id}>"
}

method Str(--> Str) {
    "Room<id: {self.id}>"
}