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 @@
- 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
......
......@@ -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()
},
......
<?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,7 +48,26 @@ 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,
......@@ -64,7 +83,8 @@ class MessageModel extends Db
conversation_id
HAVING
idstring = "' . implode(':', $recips) . '"';
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,
SELECT
fs.id AS id,
CONCAT(fs.name," ",fs.nachname ) AS value
FROM
fs_foodsaver fs
WHERE
fs.rolle >= 1
AND
CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%"
GROUP BY
fs.id
';
......@@ -160,6 +185,7 @@ class MessageModel extends Db
AND
CONCAT(fs.name," ",fs.nachname ) LIKE "%' . $this->safe($term) . '%"
AND
fs.deleted_at IS NULL
';
......@@ -184,6 +210,7 @@ class MessageModel extends Db
FROM
`fs_foodsaver_has_conversation` hc
INNER JOIN
`fs_foodsaver` fs ON fs.id = hc.foodsaver_id
......@@ -240,6 +267,9 @@ class MessageModel extends Db
AND
hc.foodsaver_id = ' . (int)$this->func->fsId() . '
AND
c.last_message <> ""
ORDER BY
hc.unread DESC,
c.`last` 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;
......
......@@ -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)) {
/*
......
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