Skip to content

Commit 06ef92b

Browse files
committed
Added media upload and made username optional on registration
1 parent 250da11 commit 06ef92b

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

src/Matrix.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Updivision\Matrix\Exceptions\RateLimitExceededException;
1212
use Updivision\Matrix\Exceptions\UnsupportedContentTypeException;
1313
use Updivision\Matrix\Resources\Room;
14+
use Updivision\Matrix\Resources\Media;
1415
use Updivision\Matrix\Resources\UserData;
1516
use Updivision\Matrix\Resources\UserSession;
1617

@@ -45,6 +46,12 @@ class Matrix
4546
*/
4647
private $baseUrl;
4748

49+
/**
50+
* @internal
51+
* @var string
52+
*/
53+
private $domain;
54+
4855
/**
4956
* Constructs a new matrix api instance
5057
*
@@ -56,7 +63,7 @@ public function __construct($domain)
5663
{
5764
$this->validateConstructorArgs($domain);
5865

59-
$this->baseUrl = $domain.'_matrix/client/r0';
66+
$this->domain = $domain;
6067

6168
$this->client = new Client();
6269

@@ -78,10 +85,14 @@ public function __construct($domain)
7885
* @throws RateLimitExceededException
7986
* @throws UnsupportedContentTypeException
8087
*/
81-
public function request($method, $endpoint, array $data = null, array $query = null)
88+
public function request($method, $endpoint, array $data = null, array $query = null, $rawData = false)
8289
{
8390
$options = ['json' => $data];
8491

92+
if ($rawData) {
93+
$options = $data;
94+
}
95+
8596
if (isset($query)) {
8697
$options['query'] = $query;
8798
}
@@ -155,11 +166,19 @@ public function session()
155166

156167
public function user()
157168
{
169+
$this->baseUrl = $this->domain.'_matrix/client/r0';
158170
return new UserData($this);
159171
}
160172

161173
public function room()
162174
{
175+
$this->baseUrl = $this->domain.'_matrix/client/r0';
163176
return new Room($this);
164177
}
178+
179+
public function media()
180+
{
181+
$this->baseUrl = $this->domain.'_matrix/media/r0';
182+
return new Media($this);
183+
}
165184
}

src/Resources/Media.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Updivision\Matrix\Resources;
4+
5+
use Updivision\Matrix\Resources\AbstractResource;
6+
7+
/**
8+
* Media management
9+
*
10+
* This provides methods to create and update media
11+
*
12+
* @package Matrix\Resources
13+
*/
14+
class Media extends AbstractResource
15+
{
16+
/**
17+
* The resource endpoint
18+
*
19+
* @internal
20+
* @var string
21+
*/
22+
protected $endpoint = '';
23+
24+
public function upload($file, $contentType)
25+
{
26+
if ($this->check()) {
27+
$data = file_get_contents($file);
28+
return $this->matrix()->request('POST', $this->endpoint('upload'), [
29+
'body' => $data,
30+
'headers' => [
31+
'Content-Type' => $contentType,
32+
'Content-Length' => strlen($data)
33+
]
34+
], [
35+
'filename' => basename($file),
36+
'access_token' => $this->data['access_token']
37+
], true);
38+
}
39+
throw new \Exception('Not authenticated');
40+
}
41+
}

src/Resources/UserData.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function getProfile($userId = null)
113113
}
114114
$userId = $this->data['user_id'];
115115
}
116-
return $this->matrix()->request('GET', $this->endpoint('profile/'.$userId));
116+
return $this->matrix()->request('GET', $this->endpoint('profile/'.urlencode($userId)));
117117
}
118118

119119
/**
@@ -132,7 +132,7 @@ public function getAvatar($userId = null)
132132
}
133133
$userId = $this->data['user_id'];
134134
}
135-
return $this->matrix()->request('GET', $this->endpoint('profile/'.$userId.'/avatar_url'));
135+
return $this->matrix()->request('GET', $this->endpoint('profile/'.urlencode($userId).'/avatar_url'));
136136
}
137137

138138
/**
@@ -147,7 +147,7 @@ public function setAvatar($avatarUrl)
147147
{
148148
if ($this->check()) {
149149
$userId = $this->data['user_id'];
150-
return $this->matrix()->request('PUT', $this->endpoint('profile/'.$userId.'/avatar_url'), [
150+
return $this->matrix()->request('PUT', $this->endpoint('profile/'.urlencode($userId).'/avatar_url'), [
151151
'avatar_url' => $avatarUrl
152152
], [
153153
'access_token' => $this->data['access_token']
@@ -172,7 +172,7 @@ public function getDisplayName($userId = null)
172172
}
173173
$userId = $this->data['user_id'];
174174
}
175-
return $this->matrix()->request('GET', $this->endpoint('profile/'.$userId.'/displayname'));
175+
return $this->matrix()->request('GET', $this->endpoint('profile/'.urlencode($userId).'/displayname'));
176176
}
177177

178178
/**
@@ -187,7 +187,7 @@ public function setDisplayName($displayName)
187187
{
188188
if ($this->check()) {
189189
$userId = $this->data['user_id'];
190-
return $this->matrix()->request('PUT', $this->endpoint('profile/'.$userId.'/displayname'), [
190+
return $this->matrix()->request('PUT', $this->endpoint('profile/'.urlencode($userId).'/displayname'), [
191191
'displayname' => $displayName
192192
], [
193193
'access_token' => $this->data['access_token']
@@ -204,16 +204,21 @@ public function setDisplayName($displayName)
204204
*/
205205
public function register($username, $password)
206206
{
207-
$data = $this->matrix()->request('POST', $this->endpoint('register'), [
207+
$userData = [
208208
'auth' => [
209209
'type' => 'm.login.dummy'
210210
],
211-
'bind_email' => true,
212-
'bind_msisdn' => true,
211+
'bind_email' => false,
212+
'bind_msisdn' => false,
213213
'password' => $password,
214-
'username' => $username,
215-
'x_show_msisdn' => true
216-
], [
214+
'x_show_msisdn' => false
215+
];
216+
217+
if ($username) {
218+
$userData['username'] = $username;
219+
}
220+
221+
$data = $this->matrix()->request('POST', $this->endpoint('register'), $userData, [
217222
'kind' => 'user'
218223
]);
219224
$this->setData($data);

0 commit comments

Comments
 (0)