aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix/Client.pm6
blob: 9867c7783b9e40084d5a5acf2c6057621bbd8ab9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use HTTP::Request::Common;
use URI::Encode;
use JSON::Tiny;
use Matrix::Response;
use Matrix::Client::Common;
use Matrix::Client::Room;
use Matrix::Client::Requester;

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

has Str $.user-id;
has Str $.device-id;
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<access_token>;
        $!user-id = $data<user_id>;
        $!device-id = $data<device_id>;
        $Matrix::Client::Common::TXN-ID = $data<txn_id> // 0;
        $!logged = True;
    }
}

method login(Str $username, Str $pass) returns Bool {
    return if $!logged;

    # Handle POST
    my $data = to-json {
        type => "m.login.password",
        user => $username,
        password => $pass
    };

    my $res = $.post("/login", $data);
    spurt $!auth-file, $res.content;

    my $data = from-json($res.content);
    $!access-token = $data<access_token>;
    $!user-id = $data<user_id>;
    $!device-id = $data<device_id>;
}

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 $!auth-file, to-json(%data);
}

method logout() {
    unlink $!auth-file;
    $.post("/logout")
}

method register($username, $password, Bool :$bind-email? = False) {
    my $res = $.post("/register",
                     username => $username, password => $password,
                     bind_email => $bind-email,
                     auth => {
                            type => "m.login.dummy"
                    });
    my $data = from-json $res.content;
    $!access-token = $data<access_token>;
    $.user-id = $data<user_id>;
}

# User Data

method profile(Str :$user-id?) {
    my $id = $user-id // $.user-id;
    $.get("/profile/" ~ $id)
}

method display-name(Str :$user-id?) {
    my $id = $user-id // $.user-id;
    my $res = $.get("/profile/" ~ $id ~ "/displayname");

    my $data = from-json($res.content);

    $data<displayname> // ""
}

method change-display-name(Str:D $display-name!) {
    $.put("/profile/" ~ $.user-id ~ "/displayname",
          displayname => $display-name)
}

method avatar-url(Str :$user-id?) {
    my $id = $user-id // $.user-id;
    my $res = $.get("/profile/" ~ $id ~ "/avatar_url");
    my $data = from-json($res.content);

    $data<avatar_url> // ""
}

multi method change-avatar(IO::Path $avatar) {
    my $mxc-url = $.upload($avatar.IO);
    samewith($mxc-url);
}

multi method change-avatar(Str:D $mxc-url!) {
    $.put("/profile/" ~ $.user-id ~ "/avatar_url",
          avatar_url => $mxc-url);
}

# Syncronization

multi method sync() {
    my $res = $.get("/sync", timeout => 30000);

    Matrix::Response::Sync.new($res.content)
}

multi method sync(Str :$sync-filter, Str :$since = "") {
    my $res = $.get("/sync",
        timeout => 30000,
        filter => $sync-filter,
        since => $since
    );

    Matrix::Response::Sync.new($res.content)
}

multi method sync(:$sync-filter is copy, :$since = "") {
    $.sync(sync-filter => to-json($sync-filter), since => $since)
}

# Rooms

method join-room($room-id!) {
    $.post("/join/$room-id")
}

method leave-room($room-id) {
    $.post("/rooms/$room-id/leave");
}

method rooms(Bool :$sync = False) {
    return @!rooms unless $sync;
    my $res = $.get("/sync", timeout => "30000");

    @!rooms = ();
    my $data = from-json($res.content);
    for $data<rooms><join>.kv -> $id, $json {
        @!rooms.push(Matrix::Client::Room.new(
            id => $id,
            json => $json,
            home-server => $!home-server,
            access-token => $!access-token
        ));
    }

    @!rooms
}

method joined-rooms() {
    my $res = $.get('/joined_rooms');

    my $data = from-json($res.content);
    return $data<joined_rooms>.Seq.map(-> $room-id {
        Matrix::Client::Room.new(
            id => $room-id,
            home-server => $!home-server,
            access-token => $!access-token
        )
    });
}

method public-rooms() {
    $.get('/publicRooms')
}

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

# Media

method upload(IO::Path $path, Str $filename?) {
    my $buf = slurp $path, :bin;
    my $fn = $filename ?? $filename !! $path.basename;
    my $res = $.post-bin("/upload", $buf,
        content-type => "image/png",
        filename => $fn,
    );
    my $data = from-json($res.content);
    $data<content_uri> // "";
}