diff --git a/CHANGELOG.md b/CHANGELOG.md index 36da6e4c582081ff37c811a0f3961fe47f082681..1aac6e8a0a1f0fd65dc540b3d0b3595dcbb9dd91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ - Improve/correct user profile badge count !612 @pmayd - Datepicker for fetch slots in stores increased to fit 6 week rows #417 !621 @peter.reutlingen +- List of conversations no longer contains empty conversations #420 @pmayd +- List of conversations no longer contains empty conversations #420 !619 @pmayd +- ## Refactoring - Removed scrollbars from event view !608 @jofranz - Restructured / cleaned up CSS for Boostrap / Topbar, leaving Bootstrap defaults where possible !616 @colomar diff --git a/client/src/conv.js b/client/src/conv.js index 0e5d033d6458499adb41ed8f688c9e84aac0f8a5..4247c03908f5e7281029c634b29f9a204b23cd3b 100644 --- a/client/src/conv.js +++ b/client/src/conv.js @@ -268,6 +268,13 @@ const conv = { this.chatCount-- // this.rearrange(); + // delete empty conv + ajax.req('msg', 'deleteEmptyConversation', { + data: { + cid: cid + } + }) + // re register polling service this.registerPollingService() }, diff --git a/src/Modules/Message/MessageGateway.php b/src/Modules/Message/MessageGateway.php new file mode 100644 index 0000000000000000000000000000000000000000..42e5f3ac4590a541a19c3ab6e05369579f58fb3c --- /dev/null +++ b/src/Modules/Message/MessageGateway.php @@ -0,0 +1,39 @@ +db->count('fs_conversation', [ + 'id' => $cid, + 'last_message' => null]); + + if ($countEmpty > 0) { + $deletedRows = $this->db->delete('fs_foodsaver_has_conversation', ['conversation_id' => $cid]); + $this->db->delete('fs_conversation', ['id' => $cid]); + } + + return $deletedRows; + } +} diff --git a/src/Modules/Message/MessageModel.php b/src/Modules/Message/MessageModel.php index b3c84f0a3ac352d916d615c6dcaa1cdc35bf7df4..dd796afd2a8870eb2eac9bc0eacbabc6940d0ea0 100644 --- a/src/Modules/Message/MessageModel.php +++ b/src/Modules/Message/MessageModel.php @@ -48,23 +48,43 @@ class MessageModel extends Db $conversation_id = false; - if ($cids = $this->qCol('SELECT hc.conversation_id FROM `fs_foodsaver_has_conversation` hc LEFT JOIN `fs_conversation` c ON c.id = hc.conversation_id WHERE hc.`foodsaver_id` = ' . (int)$this->func->fsId() . ' AND c.locked = 0')) { + $cids = $this->qCol(' + SELECT + hc.conversation_id + + FROM + `fs_foodsaver_has_conversation` hc + + LEFT JOIN + `fs_conversation` c + + ON + c.id = hc.conversation_id + + WHERE + hc.`foodsaver_id` = ' . (int)$this->func->fsId() . ' + + AND + c.locked = 0 + '); + if ($cids) { $sql = ' - SELECT - conversation_id, - GROUP_CONCAT(foodsaver_id ORDER BY foodsaver_id SEPARATOR ":") AS idstring - - FROM - fs_foodsaver_has_conversation - - WHERE - conversation_id IN (' . implode(',', $cids) . ') - - GROUP BY - conversation_id - - HAVING - idstring = "' . implode(':', $recips) . '"'; + SELECT + conversation_id, + GROUP_CONCAT(foodsaver_id ORDER BY foodsaver_id SEPARATOR ":") AS idstring + + FROM + fs_foodsaver_has_conversation + + WHERE + conversation_id IN (' . implode(',', $cids) . ') + + GROUP BY + conversation_id + + HAVING + idstring = "' . implode(':', $recips) . '" + '; if ($conv = $this->qRow($sql)) { $conversation_id = $conv['conversation_id']; @@ -122,14 +142,19 @@ class MessageModel extends Db // for orga and bot-welcome team, allow to contact everyone who is foodsaver if ($this->session->may('orga') || (isset($_SESSION['client']['bezirke']) && is_array($_SESSION['client']['bezirke']) && in_array(813, $_SESSION['client']['bezirke']))) { $sql = ' - SELECT fs.id AS id, - CONCAT(fs.name," ",fs.nachname ) AS value + SELECT + fs.id AS id, + CONCAT(fs.name," ",fs.nachname ) AS value + FROM - fs_foodsaver fs + fs_foodsaver fs + WHERE - fs.rolle >= 1 + fs.rolle >= 1 + AND - CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" + CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" + GROUP BY fs.id '; @@ -159,7 +184,8 @@ class MessageModel extends Db hb.bezirk_id IN(' . implode(',', $ids) . ') AND - CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" + CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" + AND fs.deleted_at IS NULL '; @@ -183,7 +209,8 @@ class MessageModel extends Db fs.geschlecht FROM - `fs_foodsaver_has_conversation` hc + `fs_foodsaver_has_conversation` hc + INNER JOIN `fs_foodsaver` fs ON fs.id = hc.foodsaver_id @@ -239,6 +266,9 @@ class MessageModel extends Db AND hc.foodsaver_id = ' . (int)$this->func->fsId() . ' + + AND + c.last_message <> "" ORDER BY hc.unread DESC, @@ -434,8 +464,28 @@ class MessageModel extends Db } else { $sender_id = (int)$sender_id; } - if ($mid = $this->insert('INSERT INTO `fs_msg`(`conversation_id`, `foodsaver_id`, `body`, `time`) VALUES (' . (int)$cid . ',' . $sender_id . ',' . $this->strval($body) . ',NOW())')) { - $this->update('UPDATE `fs_foodsaver_has_conversation` SET unread = 1 WHERE conversation_id = ' . (int)$cid . ' AND `foodsaver_id` != ' . (int)$sender_id); + + $mid = $this->insert(' + INSERT INTO + `fs_msg`(`conversation_id`, `foodsaver_id`, `body`, `time`) + + VALUES + (' . (int)$cid . ',' . $sender_id . ',' . $this->strval($body) . ',NOW()) + '); + if ($mid) { + $this->update(' + UPDATE + `fs_foodsaver_has_conversation` + + SET + unread = 1 + + WHERE + conversation_id = ' . (int)$cid . ' + + AND + `foodsaver_id` != ' . (int)$sender_id + ); $this->updateConversation($cid, $sender_id, $body, $mid); return $mid; diff --git a/src/Modules/Message/MessageXhr.php b/src/Modules/Message/MessageXhr.php index 771902a729fbf6b105a8e0ca69708af47cb87889..c64f2228ba76300e55c8264ff5be8482975d42a5 100644 --- a/src/Modules/Message/MessageXhr.php +++ b/src/Modules/Message/MessageXhr.php @@ -12,11 +12,13 @@ class MessageXhr extends Control * @var WebSocketSender */ private $webSocketSender; + private $messageGateway; - public function __construct(MessageModel $model, MessageView $view, WebSocketSender $webSocketSender) + public function __construct(MessageModel $model, MessageView $view, MessageGateway $messageGateway, WebSocketSender $webSocketSender) { $this->model = $model; $this->view = $view; + $this->messageGateway = $messageGateway; $this->webSocketSender = $webSocketSender; parent::__construct(); @@ -62,6 +64,26 @@ class MessageXhr extends Control } } + /** + * ajax call to delete an empty conversation after closing chat. + */ + final public function deleteEmptyConversation(): void + { + $cid = $_GET['cid']; + $xhr = new Xhr(); + $xhr->setStatus(0); + + if ($this->mayConversation($cid) && !$this->model->conversationLocked($cid)) { + $rowCount = $this->messageGateway->deleteEmptyConversation($cid); + if ($$rowCount > 0) { + $xhr->addData('rowCount', $rowCount); + $xhr->setStatus(1); + } + } + + $xhr->send(); + } + /** * ajax call to load an existing conversation. */ @@ -287,7 +309,7 @@ class MessageXhr extends Control if (!empty($recip) && $body != '') { /* - * add conversation if successfull send an success message otherwise error + * add conversation if successful send an success message otherwise error */ if ($cid = $this->model->addConversation($recip, $body)) { /*