diff options
author | Marvin Borner | 2018-05-18 16:41:47 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-18 16:41:47 +0200 |
commit | 083c3778b81f22fb4982f6d5b09b1524340efd6a (patch) | |
tree | eeb89db4fb1b38ecbc456b441e0802860f1bacce /main | |
parent | bfa33fe456049e1dbc2f8240549baa6062457574 (diff) |
Much beautifying - rewritten message processor
Diffstat (limited to 'main')
6 files changed, 78 insertions, 70 deletions
diff --git a/main/app/sprinkles/admin/src/Controller/UserController.php b/main/app/sprinkles/admin/src/Controller/UserController.php index 45bedf5..ede9523 100644 --- a/main/app/sprinkles/admin/src/Controller/UserController.php +++ b/main/app/sprinkles/admin/src/Controller/UserController.php @@ -1006,7 +1006,7 @@ class UserController extends SimpleController if ((Capsule::table('public_keys') ->where('user_id', "=", $requestedUser->id) - ->exists()) === TRUE) { + ->exists())) { $RawPublicKey = Capsule::table('public_keys') ->where('user_id', "=", $requestedUser->id) diff --git a/main/app/sprinkles/admin/src/Controller/WormholeController.php b/main/app/sprinkles/admin/src/Controller/WormholeController.php index 68bbca7..a2775f1 100644 --- a/main/app/sprinkles/admin/src/Controller/WormholeController.php +++ b/main/app/sprinkles/admin/src/Controller/WormholeController.php @@ -82,13 +82,17 @@ class WormholeController extends SimpleController ->select("user_follow.user_id as id", "users.user_name as username") ->get(); - $UsersFriends = DB::select("SELECT id FROM (SELECT user_id AS id FROM user_follow WHERE followed_by_id = $user->id UNION ALL SELECT followed_by_id FROM user_follow WHERE user_id = $user->id) t GROUP BY id HAVING COUNT(id) > 1"); + $UsersFriends = Capsule::select("SELECT id FROM (SELECT user_id AS id FROM user_follow WHERE followed_by_id = $user->id UNION ALL SELECT followed_by_id FROM user_follow WHERE user_id = $user->id) t GROUP BY id HAVING COUNT(id) > 1"); + /** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */ + $classMapper = $this->ci->classMapper; foreach ($UsersFriends as $Key => $UsersFriendId) { // NOT THAT EFFICIENT... - $UsersFriendInformation = DB::table('users') - ->where('id', "=", $UsersFriendId->id) - ->select("users.id", "users.user_name as username") + $UsersFriendInformation = $classMapper->createInstance('user')// select doesnt work with instance + ->where('id', $UsersFriendId->id) ->get(); - $UsersFriends[$Key] = $UsersFriendInformation[0]; + $UsersFriends[$Key]->id = $UsersFriendInformation[0]->id; + $UsersFriends[$Key]->username = $UsersFriendInformation[0]->user_name; + $UsersFriends[$Key]->avatar = $UsersFriendInformation[0]->avatar; + $UsersFriends[$Key]->full_name = $UsersFriendInformation[0]->full_name; } $result = $user->toArray(); diff --git a/main/app/sprinkles/core/assets/SiteAssets/css/main.css b/main/app/sprinkles/core/assets/SiteAssets/css/main.css index f649882..880d33c 100644 --- a/main/app/sprinkles/core/assets/SiteAssets/css/main.css +++ b/main/app/sprinkles/core/assets/SiteAssets/css/main.css @@ -327,10 +327,6 @@ hr.ChatHeaderDivider { border-radius: 25px; } -#ChatTextInput { - display: none; -} - /* TYPING INDICATOR ANIMATION */ .spinner { width: 70px; diff --git a/main/app/sprinkles/core/assets/SiteAssets/js/chat.js b/main/app/sprinkles/core/assets/SiteAssets/js/chat.js index 2dd97ce..115d5fa 100644 --- a/main/app/sprinkles/core/assets/SiteAssets/js/chat.js +++ b/main/app/sprinkles/core/assets/SiteAssets/js/chat.js @@ -57,7 +57,21 @@ function InitializeChatServer() { var WasHimself = MessageObject.WasHimself; var ServerMessageType = MessageObject.ServerMessageType; var Granted = MessageObject.Granted; - ReceiversUsername = MessageObject.Receiver; + var Success = MessageObject.Success; + + + // GET OWN PUBLIC KEY FIRST + if (!(current_username in PublicKey)) { + $.ajax({ + type: 'GET', + url: site.uri.public + '/api/users/u/' + current_username + '/publickey', + dataType: "json", + success: function (response) { + PublicKey[current_username] = response.PublicKey; + console.log("%c[ENCRYPTION LOGGER]\nPublickey of " + current_username + ": \n\n" + PublicKey[current_username].substr(96).slice(0, -35), "font-family: monospace; white-space: pre; display: inline-block; border-radius: 10px; padding: 5px; color: #20c20e; background-color: black;") + } + }); + } // GET PUBLIC KEY IF NOT ALREADY DID if (!(ReceiversUsername in PublicKey) && ReceiversUsername !== null && ReceiversUsername !== undefined) { @@ -76,7 +90,7 @@ function InitializeChatServer() { // DECRYPT MESSAGE options = { message: openpgp.message.readArmored("-----BEGIN PGP MESSAGE-----\r\nVersion: OpenPGP.js v3.0.9\r\nComment: https://openpgpjs.org\r\n\r\n" + Message + "\r\n\-----END PGP MESSAGE-----\r\n"), - publicKeys: openpgp.key.readArmored(PublicKey[Username]).keys, // FOR VERIFICATION + //publicKeys: openpgp.key.readArmored(PublicKey[Username]).keys, // FOR VERIFICATION privateKeys: [privKeyObj] }; openpgp.decrypt(options).then(function (plaintext) { @@ -154,6 +168,13 @@ function InitializeChatServer() { } else if (Granted === false) { console.log("%c[CHATSOCKET LOGGER] Chat access denied!", "color: red"); } + } else if (ServerMessageType === "SetReceiver") { // TYPE: SERVER CHECKED ACCESS -- MOSTLY HANDLED IN BACKEND + if (Success === true) { + console.log("%c[CHATSOCKET LOGGER] Setting receiver succeeded!", "color: green"); + $(".SelectedReceiver").show(); + } else if (Success === false) { + console.log("%c[CHATSOCKET LOGGER] Setting receiver failed!", "color: red"); + } } } // SCROLL TO BOTTOM ON NEW MESSAGE OF ANY KIND @@ -208,13 +229,12 @@ function InitializeChatServer() { subscribe(SubscribeTextInput.val()); } } else { - console.log("%c[CHATSOCKET LOGGER] Not connected to Websocket anymore! Trying to connect again...", "color: red"); - InitializeChatServer(); + NotConnectedAnymore(); } }); function subscribe(channel) { - ChatSocket.send(JSON.stringify({ClientMessageType: "Subscribe", Channel: channel})); + //ChatSocket.send(JSON.stringify({ClientMessageType: "Subscribe", Channel: channel})); SubscribeTextInput.hide(); ChatTextInput.show(); } @@ -245,7 +265,7 @@ function InitializeChatServer() { // ENCRYPT AND SEND MESSAGE WITH OWN PUBLIC KEY options = { data: ChatTextInput.val(), - publicKeys: openpgp.key.readArmored(PublicKey[ReceiversUsername]).keys, + publicKeys: openpgp.key.readArmored(PublicKey[current_username]).keys, privateKeys: [privKeyObj] // FOR SIGNING }; openpgp.encrypt(options).then(function (Encrypted) { @@ -254,8 +274,7 @@ function InitializeChatServer() { ChatSocket.send(JSON.stringify({ ClientMessageType: "ChatMessage", - MessageType: "Private", - EncryptedWithKey: ReceiversUsername, + EncryptedWithKey: current_username, Message: EncryptedMessage })); ChatTextInput.val(""); @@ -274,20 +293,23 @@ function InitializeChatServer() { ChatSocket.send(JSON.stringify({ ClientMessageType: "ChatMessage", - MessageType: "Private", EncryptedWithKey: ReceiversUsername, Message: EncryptedMessage })); ChatTextInput.val(""); ChatTextInput.val(""); }); - } else { - console.log("%c[CHATSOCKET LOGGER] Not connected to Websocket anymore! Trying to connect again...", "color: red"); - InitializeChatServer(); } + } else { + NotConnectedAnymore(); } }); + function NotConnectedAnymore() { + console.log("%c[CHATSOCKET LOGGER] Not connected to Websocket anymore! Trying to connect again...", "color: red"); + InitializeChatServer(); + } + // SET RECEIVER $(document).on("click", ".ReceiverSelector", function () { ReceiversUsername = $(this).attr("data-username"); diff --git a/main/app/sprinkles/core/assets/SiteAssets/php/Chatserver/src/ChatProcessor.php b/main/app/sprinkles/core/assets/SiteAssets/php/Chatserver/src/ChatProcessor.php index 6ae111a..fc40429 100644 --- a/main/app/sprinkles/core/assets/SiteAssets/php/Chatserver/src/ChatProcessor.php +++ b/main/app/sprinkles/core/assets/SiteAssets/php/Chatserver/src/ChatProcessor.php @@ -16,6 +16,7 @@ class ChatProcessor implements MessageComponentInterface private $userInfo; private $verifiedUsers; private $ReceiversId; + private $ResourceId; public function __construct() { $this->clients = new \SplObjectStorage; @@ -24,6 +25,7 @@ class ChatProcessor implements MessageComponentInterface $this->userID = []; // USER ID WHICH IS DECLARED IN DB $this->userInfo = []; // JSON CONTAINING ALL INFO OF USER FROM DB $this->ReceiversId = []; + $this->ResourceId = []; $this->verifiedUsers = []; } @@ -47,24 +49,21 @@ class ChatProcessor implements MessageComponentInterface if ($KeyVerifierCode === "200") { // VERIFICATION SUCCEEDED $this->userInfo[$conn->resourceId] = json_decode(file_get_contents("https://beam-messenger.de/wormhole/" . file("/AccessToken.txt", FILE_IGNORE_NEW_LINES)["0"] . "/user/" . $data->UserID)); $this->userID[$conn->resourceId] = $this->userInfo[$conn->resourceId]->id; + $this->ResourceId[$this->userInfo[$conn->resourceId]->id] = $conn->resourceId; + + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = TRUE; + if (isset($this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name)) $MessageObject->Receiver = $this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name; + $MessageObject->ServerMessageType = "Verify"; if (isset($this->userInfo[$conn->resourceId]->id)) { // USER FOUND - $MessageObject = new \stdClass(); - $MessageObject->ServerMessage = TRUE; - $MessageObject->ServerMessageType = "Verify"; $MessageObject->Granted = TRUE; - if (isset($this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name)) $MessageObject->Receiver = $this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name; $this->verifiedUsers[$conn->resourceId] = TRUE; - $this->users[$conn->resourceId]->send(json_encode($MessageObject, TRUE)); } else { - $MessageObject = new \stdClass(); - $MessageObject->ServerMessage = TRUE; - $MessageObject->ServerMessageType = "Verify"; $MessageObject->Granted = FALSE; - if (isset($this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name)) $MessageObject->Receiver = $this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name; $this->verifiedUsers[$conn->resourceId] = FALSE; - $this->users[$conn->resourceId]->send(json_encode($MessageObject, TRUE)); $this->onClose($conn); } + $this->users[$conn->resourceId]->send(json_encode($MessageObject, TRUE)); } else { $MessageObject = new \stdClass(); $MessageObject->ServerMessage = TRUE; @@ -79,8 +78,7 @@ class ChatProcessor implements MessageComponentInterface } if ($this->verifiedUsers[$conn->resourceId]) { switch ($data->ClientMessageType) { - case "Subscribe": // USER SUBSCRIBED - //if (!in_array(array_flip($this->userID)[$this->userID[$conn->resourceId]], (isset(array_flip($this->channels)[$data->Channel]) ? array_flip($this->channels)[$data->Channel] : array()))) { // ONLY JOIN IF NOT ALREADY JOINED + /*case "Subscribe": // USER SUBSCRIBED -- DON'T NEED THIS CURRENTLY $this->channels[$conn->resourceId] = $data->Channel; foreach ($this->channels as $id => $channel) { if ($this->channels[$conn->resourceId] == $channel) { @@ -101,37 +99,36 @@ class ChatProcessor implements MessageComponentInterface $this->users[$id]->send($MessageJson); } } - break; + break;*/ case "SetReceiver": // USER CLICKED ON NEW CHAT foreach ($this->userInfo[$conn->resourceId]->friends as $friend) { if ($data->ReceiversId === $friend->id) { $this->ReceiversId[$conn->resourceId] = $data->ReceiversId; } } + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = TRUE; + $MessageObject->ServerMessageType = "SetReceiver"; + $this->ReceiversId[$conn->resourceId] !== $data->ReceiversId ? $MessageObject->Success = FALSE : $MessageObject->Success = TRUE; + $this->users[$conn->resourceId]->send(json_encode($MessageObject, TRUE)); break; case "ChatMessage": // MESSAGE RECEIVED - if (isset($this->channels[$conn->resourceId])) { - $target = $this->channels[$conn->resourceId]; // target = ALL CHANNELS TO SEND THE MESSAGE - foreach ($this->channels as $id => $channel) { - if ($channel == $target) { - $MessageObject = new \stdClass(); - $MessageObject->ServerMessage = FALSE; - $MessageObject->GroupName = $channel; - if (isset($this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name)) $MessageObject->Receiver = $this->userInfo[array_flip($this->channels)[$this->channels[$conn->resourceId]]]->user_name; - $MessageObject->Username = $this->userInfo[$conn->resourceId]->user_name; - $MessageObject->Fullname = $this->userInfo[$conn->resourceId]->full_name; - $MessageObject->Avatar = $this->userInfo[$conn->resourceId]->avatar; - $MessageObject->Message = htmlspecialchars($data->Message); - if ($id === $conn->resourceId) { - $MessageObject->WasHimself = TRUE; - } else { - $MessageObject->WasHimself = FALSE; - } - $MessageJson = json_encode($MessageObject, TRUE); - $this->users[$id]->send($MessageJson); - $this->getHttpCode("https://beam-messenger.de/wormhole/" . file("/AccessToken.txt", FILE_IGNORE_NEW_LINES)["0"] . "/new/message/" . $this->userInfo[$conn->resourceId]->id . "/" . $this->userInfo[array_flip($this->channels)[$target]]->id . "/" . $data->Message); - } - } + $ReceiversId = $this->ReceiversId[$conn->resourceId]; + $ReceiversResourceId = $this->ResourceId[$ReceiversId]; + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = FALSE; + $MessageObject->Username = $this->userInfo[$conn->resourceId]->user_name; + $MessageObject->Fullname = $this->userInfo[$conn->resourceId]->full_name; + $MessageObject->Avatar = $this->userInfo[$conn->resourceId]->avatar; + $MessageObject->Message = htmlspecialchars($data->Message); + $MessageJson = json_encode($MessageObject, TRUE); + + if ($data->EncryptedWithKey === $this->userInfo[$ReceiversResourceId]->user_name) { + $MessageObject->WasHimself = FALSE; + $this->users[$ReceiversResourceId]->send($MessageJson); // SEND TO RECEIVER + } else if ($data->EncryptedWithKey === $MessageObject->Username) { + $MessageObject->WasHimself = TRUE; + $this->users[$conn->resourceId]->send($MessageJson); // SEND TO SENDER (FOR VERIFICATION) } break; case "GroupMessage": // GROUP MESSAGE RECEIVED -- RESERVED FOR LATER USE (CHANNEL BASED RIGHT NOW) @@ -147,11 +144,7 @@ class ChatProcessor implements MessageComponentInterface $MessageObject->Fullname = $this->userInfo[$conn->resourceId]->full_name; $MessageObject->Avatar = $this->userInfo[$conn->resourceId]->avatar; $MessageObject->Message = htmlspecialchars($data->Message); - if ($id === $conn->resourceId) { - $MessageObject->WasHimself = TRUE; - } else { - $MessageObject->WasHimself = FALSE; - } + $id === $conn->resourceId ? $MessageObject->WasHimself = TRUE : $MessageObject->WasHimself = FALSE; $MessageJson = json_encode($MessageObject, TRUE); $this->users[$id]->send($MessageJson); $this->getHttpCode("https://beam-messenger.de/wormhole/" . file("/AccessToken.txt", FILE_IGNORE_NEW_LINES)["0"] . "/new/message/" . $this->userInfo[$conn->resourceId]->id . "/" . $this->userInfo[array_flip($this->channels)[$target]]->id . "/" . $data->Message); @@ -173,11 +166,7 @@ class ChatProcessor implements MessageComponentInterface $MessageObject->Fullname = $this->userInfo[$conn->resourceId]->full_name; $MessageObject->Avatar = $this->userInfo[$conn->resourceId]->avatar; $MessageObject->State = $data->State; - if ($id === $conn->resourceId) { - $MessageObject->WasHimself = TRUE; - } else { - $MessageObject->WasHimself = FALSE; - } + $id === $conn->resourceId ? $MessageObject->WasHimself = TRUE : $MessageObject->WasHimself = FALSE; $MessageJson = json_encode($MessageObject, TRUE); $this->users[$id]->send($MessageJson); } diff --git a/main/app/sprinkles/core/templates/pages/index.html.twig b/main/app/sprinkles/core/templates/pages/index.html.twig index 306dac1..fc29294 100644 --- a/main/app/sprinkles/core/templates/pages/index.html.twig +++ b/main/app/sprinkles/core/templates/pages/index.html.twig @@ -71,9 +71,6 @@ <div id="ChatMessages" class="ChatMessages"> <!-- Messages --> </div> - <input title="{{ translate("CHAT_MESSAGES.JOIN_GROUP_PH") }}" - placeholder="{{ translate("CHAT_MESSAGES.JOIN_GROUP_PH") }}" - id="SubscribeTextInput" class="ChatInput" type="text" /> <input title="{{ translate("CHAT_MESSAGES.WRITE_MESSAGE_PH") }}" placeholder="{{ translate("CHAT_MESSAGES.WRITE_MESSAGE_PH") }}" id="ChatTextInput" class="ChatInput" type="text" /> |