From 34cd37f7afbe77ca754e320146abfd495a92ccf3 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:24:12 +0200 Subject: [PATCH 1/7] PC-4 Add messages fixtures --- fixtures/messages.php | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 fixtures/messages.php diff --git a/fixtures/messages.php b/fixtures/messages.php new file mode 100644 index 0000000..20d665e --- /dev/null +++ b/fixtures/messages.php @@ -0,0 +1,66 @@ + "message000000001", + "chatId" => $MaxAndIlyaChat["id"], + "userId" => $MaxDmitriev["id"], + "content" => "Hi there, it's our first message" + ]; + + $IlyaMessageInChatWithMax = [ + "id" => "message000000002", + "chatId" => $MaxAndIlyaChat["id"], + "userId" => $IlyaMehof["id"], + "content" => "Hi Max! Nice to meet you here!" + ]; + + $MaxPhotoMessageInChatWithIlya = [ + "id" => "message000000003", + "chatId" => $MaxAndIlyaChat["id"], + "userId" => $MaxDmitriev["id"], + "contentType" => 1, + "content" => "https://ixbt.online/live/images/original/04/19/16/2022/01/23/14e5fd3c80.jpg" + ]; + + $MaxMessageInDeletedChatWithMatvey = [ + "id" => "message000000004", + "chatId" => $DeletedChatWithMaxAndMatvey["id"], + "userId" => $MaxDmitriev["id"], + "content" => "Hi Matvey, how are you?" + ]; + + $MatveyMessageInDeletedChatWithMax = [ + "id" => "message000000005", + "chatId" => $DeletedChatWithMaxAndMatvey["id"], + "userId" => $MatveyGorelik["id"], + "content" => "Hi, I'm fine, can you call me now? I'll delete this chat now" + ]; + + $MaxMessageInGymChat = [ + "id" => "message000000006", + "chatId" => $GymPartyPublicChat["id"], + "userId" => $MaxDmitriev["id"], + "content" => "Salam aleykum! Todzhiko ikei vatan, duri mi duhom padat nakin" + ]; + + $IlyaMessageInGymChat = [ + "id" => "message000000007", + "chatId" => $GymPartyPublicChat["id"], + "userId" => $IlyaMehof["id"], + "content" => "Nas nikogda nikto ne slomaet" + ]; + + $messagesFixtures = [ + $MaxMessageInChatWithIlya, + $IlyaMessageInChatWithMax, + $MaxPhotoMessageInChatWithIlya, + $MaxMessageInDeletedChatWithMatvey, + $MatveyMessageInDeletedChatWithMax, + $IlyaMessageInGymChat, + $MaxMessageInGymChat + ]; From b964eb92533110a45802bf20fd1cf67d48e56e43 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:25:17 +0200 Subject: [PATCH 2/7] PC-4 Add drop and seed messages, unused drop db method, messages db schema --- db/db.config.php | 2 +- db/db.controller.php | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/db/db.config.php b/db/db.config.php index ed7301f..ff7d99b 100644 --- a/db/db.config.php +++ b/db/db.config.php @@ -7,5 +7,5 @@ "user" => "root", "pass" => "root", "name" => "web_chat", - "charset" => "utf8", + "charset" => "utf8mb4", ]; diff --git a/db/db.controller.php b/db/db.controller.php index af6e0c0..73045fc 100644 --- a/db/db.controller.php +++ b/db/db.controller.php @@ -3,6 +3,7 @@ @include_once("./fixtures/users.php"); @include_once("./fixtures/chats.php"); @include_once("./fixtures/chatParticipants.php"); + @include_once("./fixtures/messages.php"); class DbController { @@ -13,15 +14,17 @@ public function __construct($dbConfig) $this->connectToDb($dbConfig); // TODO: Don't run this if we are in production mode - $this->dropTables(['ChatParticipants', 'Users', 'Chats']); + $this->dropTables(['ChatParticipants', 'Messages', 'Users', 'Chats']); + // $this->dropDB($dbConfig["name"]); - $this->initialize(); + $this->initialize($dbConfig["name"]); # Include fixtures from global scope here - global $usersFixtures, $chatsFixtures, $chatParticipantsFixtures; + global $usersFixtures, $chatsFixtures, $chatParticipantsFixtures, $messagesFixtures; $this->seed('Users', $usersFixtures); $this->seed('Chats', $chatsFixtures); $this->seed('ChatParticipants', $chatParticipantsFixtures); + $this->seed('Messages', $messagesFixtures); } private function connectToDb(array $dbConfig) @@ -46,22 +49,39 @@ private function dropTables($tables) { try { foreach ($tables as $table) { - $sql = "DROP TABLE $table"; + $sql = "DROP TABLE IF EXISTS $table"; $this->conn->exec($sql); } } catch (Exception $ex) { httpException("Failed to drop db tables", 500); } } + + private function dropDB($dbName) + { + try { + $sql = "DROP DATABASE IF EXISTS $dbName"; + $this->conn->exec($sql); + } catch (Exception $ex) { + httpException("Failed to drop db", 500); + } + } /** * Create all tables with hardcoded sql script */ - private function initialize() + private function initialize($dbName) { try { # Create Users table $users = <<conn->prepare($sql)->execute($fixture); } } catch (Exception $ex) { + var_dump($ex); httpException("Failed to seed db, table '$table'", 500)['end'](); } } From c767a4fb9309ae92cbae537d264daebf7268309a Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:56:32 +0200 Subject: [PATCH 3/7] PC-4 Add get chat messages, send message to chat routes --- app/app.controller.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/app.controller.php b/app/app.controller.php index 45807f3..dc80ab7 100644 --- a/app/app.controller.php +++ b/app/app.controller.php @@ -3,11 +3,12 @@ @include_once "./vendor/autoload.php"; @include_once "./utils/httpException.php"; @include_once "./utils/request.php"; + @include_once "./guards/jwtAuthGuard.php"; @include_once "./users/users.controller.php"; @include_once "./db/db.controller.php"; @include_once "./chats/chats.controller.php"; + @include_once "./messages/messages.controller.php"; @include_once "./auth/auth.controller.php"; - @include_once "./guards/jwtAuthGuard.php"; class AppController { @@ -23,6 +24,7 @@ class AppController private UsersController $usersController; private ChatsController $chatsController; private AuthController $authController; + private MessagesController $messagesController; # Guards private JwtAuthGuard $jwtAuthGuard; @@ -39,6 +41,7 @@ function __construct($dbConfig) $this->usersController = new UsersController($this->conn); $this->authController = new AuthController($this->conn); $this->chatsController = new ChatsController($this->conn); + $this->messagesController = new MessagesController($this->conn); # Initialize guards $this->jwtAuthGuard = new JwtAuthGuard(new UsersService($this->conn)); @@ -94,6 +97,12 @@ private function router() $this->chatsController->getChatParticipants($this->_req->getRequest()); return; } + if (preg_match("/\/api\/chats\/(?'chatId'[a-z0-9]+)\/messages/", $this->req['resource'])) + { + $this->_req->useGuard($this->jwtAuthGuard); + $this->messagesController->getChatMessages($this->_req->getRequest()); + return; + } # /chats/:chatId if (strpos($this->req['resource'], '/api/chats/') === 0) { @@ -116,6 +125,12 @@ private function router() $this->chatsController->addUserToChat($this->_req->getRequest(), $reqBody["data"]); return; } + if (preg_match("/\/api\/chats\/(?'chatId'[a-z0-9]+)\/messages/", $this->req['resource'])) + { + $this->_req->useGuard($this->jwtAuthGuard); + $this->messagesController->createMessage($this->_req->getRequest(), $reqBody["data"]); + return; + } if ($this->req['resource'] === '/api/chats') { $this->_req->useGuard($this->jwtAuthGuard); From f0ecaf042ffd0e987b28ccf5a5d3f272a1c6e797 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:56:48 +0200 Subject: [PATCH 4/7] PC-4 Fix messages controllers --- messages/messages.controller.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/messages/messages.controller.php b/messages/messages.controller.php index 67224fa..6ce240e 100644 --- a/messages/messages.controller.php +++ b/messages/messages.controller.php @@ -34,18 +34,14 @@ function createMessage($req, $messageDto) httpException($messages["chat_not_found"], 404)['end'](); } - $initiatorParticipant = $this->chatsService->getChatParticipantByUserId($req["user"]["id"], $chat["id"]); + $initiatorParticipant = $this->chatsService->isUserChatParticipant($req["user"]["id"], $chat["id"]); - if (is_null($initiatorParticipant)) + if (!$initiatorParticipant) { httpException($messages["not_enough_permission"], 403)['end'](); } - $message = $messageDto; - - $message["id"] = randomId(); - - $this->messagesService->createMessage($chat["id"], ); + $this->messagesService->createMessage(randomId(), $chat["id"], $req["user"]["id"], $messageDto["content"], $messageDto["contentType"]); $response = [ "message" => $messages["message_sent"], @@ -72,9 +68,9 @@ function getChatMessages($req) httpException($messages["chat_not_found"], 404)['end'](); } - $initiatorParticipant = $this->chatsService->getChatParticipantByUserId($req["user"]["id"], $chat["id"]); + $initiatorParticipant = $this->chatsService->isUserChatParticipant($req["user"]["id"], $chat["id"]); - if (is_null($initiatorParticipant)) + if (!$initiatorParticipant) { httpException($messages["not_enough_permission"], 403)['end'](); } From f13c11432b6184dfff1b68dfced67fba28fc6c66 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:56:58 +0200 Subject: [PATCH 5/7] PC-4 Fix messages services --- messages/messages.service.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/messages/messages.service.php b/messages/messages.service.php index eab6d3c..d576174 100644 --- a/messages/messages.service.php +++ b/messages/messages.service.php @@ -9,21 +9,29 @@ function __construct($conn) $this->conn = $conn; } - function createMessage($id, ) + function createMessage($id, $chatId, $userId, $content, $contentType) { - $sql = "INSERT INTO Chats (id, ) VALUES (:id, )"; + $sql = "INSERT INTO Messages (id, chatId, userId, content, contentType) VALUES (:id, :chatId, :userId, :content, :contentType)"; $stmt = $this->conn->prepare($sql); $stmt->bindValue(":id", $id); + $stmt->bindValue(":chatId", $chatId); + $stmt->bindValue(":userId", $userId); + $stmt->bindValue(":content", $content); + $stmt->bindValue(":contentType", $contentType); $stmt->execute(); } - function getChatMessages($chatId) + function getChatMessages($chatId): array { $sql = "SELECT * FROM Messages WHERE chatId=:chatId"; $stmt = $this->conn->prepare($sql); $stmt->bindValue(":chatId", $chatId); $stmt->execute(); - - return $stmt->rowCount() > 0; + + if ($stmt->rowCount() > 0) { + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } else { + return []; + } } } \ No newline at end of file From ae34e44ab9f3fa1012265c66b4a389656a462731 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 03:57:13 +0200 Subject: [PATCH 6/7] PC-4 Fix messages tests --- tests/messages-e2e.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/messages-e2e.php b/tests/messages-e2e.php index 038db87..0b51be8 100644 --- a/tests/messages-e2e.php +++ b/tests/messages-e2e.php @@ -45,7 +45,8 @@ function sendMessageToChat($chatId, $user = null): array } $body = [ - "message" => "Hello world" + "content" => "Hello world", + "contentType" => 0 ]; $response = request( @@ -67,6 +68,7 @@ function sendMessageToChat($chatId, $user = null): array [$response, $json] = getChatMessages($MaxAndIlyaChat["id"], $MaxDmitriev); assertStrict($response['info']['http_code'], 200); + assertStrict(count($json->data->messages), 3); }); it("should return error when trying to get private chat messages for NOT a chat participant", function () { @@ -84,6 +86,7 @@ function sendMessageToChat($chatId, $user = null): array [$response, $json] = getChatMessages($GymPartyPublicChat["id"], $MaxDmitriev); assertStrict($response['info']['http_code'], 200); + assertStrict(count($json->data->messages), 2); }); it("should return not authorized when trying to get chat messages without authorization", function () { @@ -116,11 +119,12 @@ function sendMessageToChat($chatId, $user = null): array describe("[POST] /api/chats/:chatId/messages", function () { it("should send message to private chat for chat participant", function () { - global $MaxDmitriev, $MaxAndIlyaChat; + global $MaxDmitriev, $MaxAndIlyaChat, $messages; [$response, $json] = sendMessageToChat($MaxAndIlyaChat["id"], $MaxDmitriev); - - assertStrict($response['info']['http_code'], 200); + + assertStrict($response['info']['http_code'], 201); + assertStrict($json->data->message, $messages["message_sent"]); }); it("should return error when trying to send message to private chat for NOT a chat participant", function () { @@ -133,9 +137,9 @@ function sendMessageToChat($chatId, $user = null): array }); it("should return error when trying to send message to public chat for NOT a chat participant", function () { - global $messages, $MaxDmitriev, $GymPartyPublicChat; + global $messages, $MatveyGorelik, $GymPartyPublicChat; - [$response, $json] = sendMessageToChat($GymPartyPublicChat["id"], $MaxDmitriev); + [$response, $json] = sendMessageToChat($GymPartyPublicChat["id"], $MatveyGorelik); assertStrict($response['info']['http_code'], 403); assertStrict($json->data->error, $messages["not_enough_permission"]); From 4f19fa38b69e35460160c20099ab82199cfbcdf8 Mon Sep 17 00:00:00 2001 From: itmaxxx Date: Tue, 25 Jan 2022 04:48:17 +0200 Subject: [PATCH 7/7] PC-4 Add last seen message to Ilya in chat with Max fixture --- fixtures/chatParticipants.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fixtures/chatParticipants.php b/fixtures/chatParticipants.php index 1f28bc3..e2cd3c2 100644 --- a/fixtures/chatParticipants.php +++ b/fixtures/chatParticipants.php @@ -2,8 +2,9 @@ @include_once __DIR__ . "/chats.php"; @include_once __DIR__ . "/users.php"; + @include_once __DIR__ . "/messages.php"; - global $MaxAndIlyaChat, $MaxDmitriev, $IlyaMehof, $DeletedChatWithMaxAndMatvey, $MatveyGorelik, $GymPartyPublicChat; + global $MaxAndIlyaChat, $MaxDmitriev, $IlyaMehof, $DeletedChatWithMaxAndMatvey, $MatveyGorelik, $GymPartyPublicChat, $IlyaMessageInChatWithMax; $MaxInChatWithIlya = [ "chatId" => $MaxAndIlyaChat["id"], @@ -14,7 +15,8 @@ $IlyaInChatWithMax = [ "chatId" => $MaxAndIlyaChat["id"], "userId" => $IlyaMehof["id"], - "permission" => 2 + "permission" => 2, + "lastSeenMessageId" => $IlyaMessageInChatWithMax["id"] ]; $MaxInDeletedChatWithMatvey = [