diff options
Diffstat (limited to 'src/Controllers/Client/ClientController.php')
| -rw-r--r-- | src/Controllers/Client/ClientController.php | 108 |
1 files changed, 106 insertions, 2 deletions
diff --git a/src/Controllers/Client/ClientController.php b/src/Controllers/Client/ClientController.php index 053d288..c9eb1fa 100644 --- a/src/Controllers/Client/ClientController.php +++ b/src/Controllers/Client/ClientController.php @@ -4,11 +4,14 @@ namespace App\Controllers\Client; use App\Database; use App\Errors\AppException; +use App\Errors\ErrorResponse; +use App\Errors\UnauthorizedError; use App\Errors\UnknownError; use App\Models\Device; use App\Models\RoomEvent; use App\Models\Tokens; use App\Models\User; +use App\Support\Id; use App\Support\Logger; use App\Support\Parser; use App\Support\RequestValidator; @@ -16,6 +19,8 @@ use Matrix\Data\AccountData; use Matrix\Data\DeviceLists; use Matrix\Data\LoginFlow; use Matrix\Data\Presence; +use Matrix\Data\PushCondition; +use Matrix\Data\PushRule; use Matrix\Data\Room\Ephemeral; use Matrix\Data\Room\JoinedRoom; use Matrix\Data\Room\RoomSummary; @@ -23,11 +28,14 @@ use Matrix\Data\Room\Rooms; use Matrix\Data\Room\State; use Matrix\Data\Room\Timeline; use Matrix\Data\Room\UnreadNotificationCounts; +use Matrix\Data\Ruleset; use Matrix\Data\ToDevice; +use Matrix\Enums\AuthenticationType; use Matrix\Enums\ErrorCode; use Matrix\Enums\LoginType; use Matrix\Enums\MembershipState; use Matrix\Enums\PresenceState; +use Matrix\Enums\PushConditionKind; use Matrix\Enums\UserRegistrationKind; use Matrix\Events\PresenceEvent; use Matrix\Responses\ClientLoginGetResponse; @@ -131,6 +139,21 @@ class ClientController $body = json_decode($request->getContent(), true); RequestValidator::validateJson(); + if (empty($body)) { + return new JsonResponse([ + "completed" => [], + "flows" => [ + [ + "stages" => [ + AuthenticationType::DUMMY, + ], + ], + ], + "params" => [], + "session" => Id::generate(), + ], Response::HTTP_UNAUTHORIZED); + } + // validate kind $kind = null; try { @@ -139,11 +162,24 @@ class ClientController throw new UnknownError("Bad registration kind.", Response::HTTP_BAD_REQUEST); } - $username = $body["username"]; + $username = $body["username"] ?? ""; + if (empty($username)) { + $username = crc32(time() . random_bytes(64)); + } + $userId = "@$username:$_ENV[DOMAIN]"; - Database::getInstance()->query("insert into users (id, password) values (:id, :password)", [ + if (empty($body["password"])) { + throw new AppException(ErrorCode::MISSING_PARAM, "missing password", Response::HTTP_BAD_REQUEST); + } + + if (Database::getInstance()->query("select id from users where id=:id", ["id" => $userId])->fetchColumn()) { + throw new AppException(ErrorCode::USER_IN_USE, "The desired user ID is already taken.", Response::HTTP_BAD_REQUEST); + } + + Database::getInstance()->query("insert into users (id, name, password) values (:id, :name, :password)", [ "id" => $userId, + "name" => $username, "password" => $body["password"], ]); @@ -289,4 +325,72 @@ class ClientController refreshToken: $newTokens->getRefreshToken(), )); } + + #[Route(path: "/_matrix/client/v3/pushrules", methods: ["GET"])] + public function pushRules(Request $request): Response + { + $user = User::authenticateWithRequest($request); + + return new JsonResponse([ + "global" => new Ruleset( + content: [ + new PushRule( + actions: [ + "notify", + [ + "set_tweak" => "sound", + "value" => "default", + ], + [ + "set_tweak" => "highlight", + ], + ], + default: true, + enabled: true, + pattern: "alice", + ruleId: ".m.rule.contains_user_name", + ), + ], + + override: [ + new PushRule( + actions: [], + conditions: [], + default: true, + enabled: false, + ruleId: ".m.rule.master", + ), + ], + + room: [], + sender: [], + + underride: [ + new PushRule( + actions: [ + "notify", + [ + "set_tweak" => "sound", + "value" => "ring", + ], + [ + "set_tweak" => "highlight", + "value" => false, + ], + ], + conditions: [ + new PushCondition( + kind: PushConditionKind::EVENT_MATCH, + key: "type", + pattern: "m.call.invite", + ) + ], + default: true, + enabled: true, + ruleId: ".m.rule.master", + ), + ], + ), + ]); + } } |
