Commit b14981bd authored by Michael Aydinbas's avatar Michael Aydinbas Committed by Peter Tönnies

List of conversations no longer contains empty conversations

parent 45b915bc
Pipeline #43299099 passed with stages
in 20 minutes and 56 seconds
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
- Improve/correct user profile badge count !612 @pmayd - Improve/correct user profile badge count !612 @pmayd
- Datepicker for fetch slots in stores increased to fit 6 week rows #417 !621 @peter.reutlingen - 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 ## Refactoring
- Removed scrollbars from event view !608 @jofranz - Removed scrollbars from event view !608 @jofranz
- Restructured / cleaned up CSS for Boostrap / Topbar, leaving Bootstrap defaults where possible !616 @colomar - Restructured / cleaned up CSS for Boostrap / Topbar, leaving Bootstrap defaults where possible !616 @colomar
......
...@@ -268,6 +268,13 @@ const conv = { ...@@ -268,6 +268,13 @@ const conv = {
this.chatCount-- this.chatCount--
// this.rearrange(); // this.rearrange();
// delete empty conv
ajax.req('msg', 'deleteEmptyConversation', {
data: {
cid: cid
}
})
// re register polling service // re register polling service
this.registerPollingService() this.registerPollingService()
}, },
......
<?php
/**
* standard gateway for the message component providing database queries. Available in other classes through symfony's autowiring.
*
* @see https://symfony.com/doc/current/service_container/autowiring.html Defining Services Dependencies Automatically (Autowiring)
* User: pmayd
* Created: 2019-01-13
* Last Change: 2019-01-14
*/
namespace Foodsharing\Modules\Message;
use Foodsharing\Modules\Core\BaseGateway;
final class MessageGateway extends BaseGateway
{
/**
* delete a conversation if the last message is empty.
*
* @param $cid int conversation id
*
* @return int
*/
public function deleteEmptyConversation(int $cid): int
{
$deletedRows = 0;
$countEmpty = (int)$this->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;
}
}
...@@ -48,23 +48,43 @@ class MessageModel extends Db ...@@ -48,23 +48,43 @@ class MessageModel extends Db
$conversation_id = false; $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 = ' $sql = '
SELECT SELECT
conversation_id, conversation_id,
GROUP_CONCAT(foodsaver_id ORDER BY foodsaver_id SEPARATOR ":") AS idstring GROUP_CONCAT(foodsaver_id ORDER BY foodsaver_id SEPARATOR ":") AS idstring
FROM FROM
fs_foodsaver_has_conversation fs_foodsaver_has_conversation
WHERE WHERE
conversation_id IN (' . implode(',', $cids) . ') conversation_id IN (' . implode(',', $cids) . ')
GROUP BY GROUP BY
conversation_id conversation_id
HAVING HAVING
idstring = "' . implode(':', $recips) . '"'; idstring = "' . implode(':', $recips) . '"
';
if ($conv = $this->qRow($sql)) { if ($conv = $this->qRow($sql)) {
$conversation_id = $conv['conversation_id']; $conversation_id = $conv['conversation_id'];
...@@ -122,14 +142,19 @@ class MessageModel extends Db ...@@ -122,14 +142,19 @@ class MessageModel extends Db
// for orga and bot-welcome team, allow to contact everyone who is foodsaver // 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']))) { if ($this->session->may('orga') || (isset($_SESSION['client']['bezirke']) && is_array($_SESSION['client']['bezirke']) && in_array(813, $_SESSION['client']['bezirke']))) {
$sql = ' $sql = '
SELECT fs.id AS id, SELECT
CONCAT(fs.name," ",fs.nachname ) AS value fs.id AS id,
CONCAT(fs.name," ",fs.nachname ) AS value
FROM FROM
fs_foodsaver fs fs_foodsaver fs
WHERE WHERE
fs.rolle >= 1 fs.rolle >= 1
AND AND
CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%"
GROUP BY GROUP BY
fs.id fs.id
'; ';
...@@ -159,7 +184,8 @@ class MessageModel extends Db ...@@ -159,7 +184,8 @@ class MessageModel extends Db
hb.bezirk_id IN(' . implode(',', $ids) . ') hb.bezirk_id IN(' . implode(',', $ids) . ')
AND AND
CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%" CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%"
AND AND
fs.deleted_at IS NULL fs.deleted_at IS NULL
'; ';
...@@ -183,7 +209,8 @@ class MessageModel extends Db ...@@ -183,7 +209,8 @@ class MessageModel extends Db
fs.geschlecht fs.geschlecht
FROM FROM
`fs_foodsaver_has_conversation` hc `fs_foodsaver_has_conversation` hc
INNER JOIN INNER JOIN
`fs_foodsaver` fs ON fs.id = hc.foodsaver_id `fs_foodsaver` fs ON fs.id = hc.foodsaver_id
...@@ -239,6 +266,9 @@ class MessageModel extends Db ...@@ -239,6 +266,9 @@ class MessageModel extends Db
AND AND
hc.foodsaver_id = ' . (int)$this->func->fsId() . ' hc.foodsaver_id = ' . (int)$this->func->fsId() . '
AND
c.last_message <> ""
ORDER BY ORDER BY
hc.unread DESC, hc.unread DESC,
...@@ -434,8 +464,28 @@ class MessageModel extends Db ...@@ -434,8 +464,28 @@ class MessageModel extends Db
} else { } else {
$sender_id = (int)$sender_id; $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); $this->updateConversation($cid, $sender_id, $body, $mid);
return $mid; return $mid;
......
...@@ -12,11 +12,13 @@ class MessageXhr extends Control ...@@ -12,11 +12,13 @@ class MessageXhr extends Control
* @var WebSocketSender * @var WebSocketSender
*/ */
private $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->model = $model;
$this->view = $view; $this->view = $view;
$this->messageGateway = $messageGateway;
$this->webSocketSender = $webSocketSender; $this->webSocketSender = $webSocketSender;
parent::__construct(); parent::__construct();
...@@ -62,6 +64,26 @@ class MessageXhr extends Control ...@@ -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. * ajax call to load an existing conversation.
*/ */
...@@ -287,7 +309,7 @@ class MessageXhr extends Control ...@@ -287,7 +309,7 @@ class MessageXhr extends Control
if (!empty($recip) && $body != '') { 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)) { if ($cid = $this->model->addConversation($recip, $body)) {
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment