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
|
<?php
namespace App\Controllers\Client;
use App\Database;
use App\Errors\AppException;
use App\Errors\UnauthorizedError;
use App\Models\Device;
use App\Models\User;
use App\Support\RequestValidator;
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();
}
}
|