summaryrefslogtreecommitdiff
path: root/src/Controllers/Client/UserController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controllers/Client/UserController.php')
-rwxr-xr-xsrc/Controllers/Client/UserController.php113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/Controllers/Client/UserController.php b/src/Controllers/Client/UserController.php
index 038caba..63442ec 100755
--- a/src/Controllers/Client/UserController.php
+++ b/src/Controllers/Client/UserController.php
@@ -3,10 +3,14 @@
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;
@@ -68,4 +72,113 @@ class UserController
"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();
+ }
}