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
|
<?php
namespace App\Controllers\Client;
use App\Database;
use App\Errors\AppException;
use App\Errors\NotFoundError;
use App\Errors\UnauthorizedError;
use App\Models\Device;
use App\Models\User;
use App\Support\RequestValidator;
use Cassandra\Exception\UnauthorizedException;
use Matrix\Enums\ErrorCode;
use Matrix\Responses\ClientAccountWhoamiGetResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
class UserController
{
#[Route(path: "/_matrix/client/v3/account/whoami", methods: ["GET"])]
public function whoami(Request $request): Response
{
$user = User::authenticateWithRequest($request);
$device = Device::fetch(userId: $user->getId());
return new JsonResponse(new ClientAccountWhoamiGetResponse(
userId: $user->getId(),
deviceId: $device->getId(),
));
}
/**
* @see https://spec.matrix.org/v1.16/client-server-api/#post_matrixclientv3useruseridfilter
*/
#[Route(path: "/_matrix/client/r0/user/{userId}/filter", methods: ["POST"])]
#[Route(path: "/_matrix/client/v3/user/{userId}/filter", methods: ["POST"])]
public function uploadFilter(Request $request): Response
{
$accessToken = str_replace("Bearer ", "", $request->headers->get("authorization") ?: "");
$user = User::fetchWithAccessToken($accessToken);
if (empty($user)) {
throw new UnauthorizedError();
}
$userId = $request->get("userId");
if ($user->getId() !== $userId) {
throw new UnauthorizedError();
}
$body = json_decode($request->getContent(), true);
RequestValidator::validateJson();
$filterId = md5($userId . random_bytes(512));
Database::getInstance()->query(<<<SQL
insert into filters (id, account_data, event_fields, event_format, presence, room, user_id)
values (:id, :account_data, :event_fields, :event_format, :presence, :room, :user_id)
SQL, [
"id" => $filterId,
"account_data" => isset($body["account_data"]) ? json_encode($body["account_data"]) : null,
"event_fields" => isset($body["event_fields"]) ? json_encode($body["event_fields"]) : null,
"event_format" => isset($body["event_format"]) ? json_encode($body["event_format"]) : null,
"presence" => isset($body["presence"]) ? json_encode($body["presence"]) : null,
"room" => isset($body["room"]) ? json_encode($body["room"]) : null,
"user_id" => $userId,
]);
return new JsonResponse([
"filter_id" => $filterId,
]);
}
#[Route(path: "/_matrix/client/v3/user/{userId}/{filter}/{filterId}", methods: ["GET"])]
public function getFilter(Request $request): Response
{
$user = User::authenticateWithRequest($request);
$userId = $request->attributes->get("userId");
$filterId = $request->attributes->get("filterId");
$filter = Database::getInstance()
->query("select * from filters where id=:id and user_id=:user_id", [
"id" => $filterId,
"user_id" => $userId,
])
->fetch();
if (empty($filter)) {
throw new AppException(
ErrorCode::NOT_FOUND,
"Unknown filter.",
Response::HTTP_NOT_FOUND
);
}
return new JsonResponse([
"account_data" => json_decode($filter["account_data"] ?? ""),
"event_fields" => json_decode($filter["event_fields"] ?? ""),
"event_format" => $filter["event_format"] ?? "",
"presence" => json_decode($filter["presence"] ?? ""),
"room" => json_decode($filter["room"] ?? ""),
]);
}
#[Route(path: "/_matrix/client/v3/profile/{userId}", methods: ["GET"])]
public function getProfile(Request $request): Response
{
$userId = $request->attributes->get("userId");
$user = Database::getInstance()->query("select * from users where id=:user_id", ["user_id" => $userId])->fetch();
if (empty($user)) {
throw new AppException(
ErrorCode::NOT_FOUND,
"There is no profile information for this user or this user does not exist.",
Response::HTTP_NOT_FOUND
);
}
return new JsonResponse();
}
#[Route(path: "/_matrix/client/v3/user/{userId}/account_data/{type}", methods: ["GET"])]
public function getAccountData(Request $request): Response
{
$user = User::authenticateWithRequest($request);
$userId = $request->attributes->get("userId");
$type = $request->attributes->get("type");
if ($user->getId() !== $userId) {
throw new UnauthorizedError("Cannot get account data for other users.");
}
$value = Database::getInstance()
->query("select value from account_data where user_id=:user_id and key=:key", [
"user_id" => $userId,
"key" => $type,
])
->fetchColumn();
if (empty($value)) {
throw new NotFoundError("Account data not found.");
}
return new JsonResponse([
$type => $value,
]);
}
#[Route(path: "/_matrix/client/v3/user/{userId}/account_data/{type}", methods: ["PUT"])]
public function setAccountData(Request $request): Response
{
$user = User::authenticateWithRequest($request);
$body = json_decode($request->getContent(), true);
RequestValidator::validateJson();
$userId = $request->attributes->get("userId");
$type = $request->attributes->get("type");
if ($user->getId() !== $userId) {
throw new UnauthorizedError("Cannot add account data for other users.");
}
Database::getInstance()
->query(
<<<SQL
insert into account_data (key, value, user_id) values (:key, :value, :user_id)
on conflict (user_id, key) do update set
value = excluded.value
SQL,
[
"key" => $type,
"value" => $request->getContent(),
"user_id" => $userId,
]
);
return new JsonResponse();
}
}
|