1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 16:06:15 +00:00

Set last read date per conversation

The next step will be to get unread messages per conversation when loading interface at first time, not globally
This commit is contained in:
Loic Blot 2014-10-23 16:23:11 +00:00
parent 8f4dfd217f
commit 690421713d
2 changed files with 25 additions and 10 deletions

View File

@ -93,7 +93,6 @@ class SmsController extends Controller {
$contacts = array(); $contacts = array();
$countPhone = count($phoneList); $countPhone = count($phoneList);
$maxTS = 0;
foreach ($phoneList as $number => $ts) { foreach ($phoneList as $number => $ts) {
$fmtPN = preg_replace("#[ ]#","/", $number); $fmtPN = preg_replace("#[ ]#","/", $number);
if (isset($contactsSrc[$fmtPN])) { if (isset($contactsSrc[$fmtPN])) {
@ -101,15 +100,11 @@ class SmsController extends Controller {
$contacts[$fmtPN] = $contactsSrc[$fmtPN]; $contacts[$fmtPN] = $contactsSrc[$fmtPN];
$contacts[$fmtPN2] = $contactsSrc[$fmtPN]; $contacts[$fmtPN2] = $contactsSrc[$fmtPN];
} }
if ($ts > $maxTS) {
$maxTS = $ts;
}
} }
$this->smsMapper->setLastReadDate($this->userId, $maxTS); $lastRead = $this->smsMapper->getLastReadDate($this->userId);
return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts, "lastRead" => $maxTS)); return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts, "lastRead" => $lastRead));
} }
/** /**
@ -171,6 +166,14 @@ class SmsController extends Controller {
// Order by id (date) // Order by id (date)
ksort($messages); ksort($messages);
// Set the last read message for the conversation (all phone numbers)
if (count($messages) > 0) {
$maxDate = max(array_keys($messages));
for ($i=0;$i<count($phoneNumbers);$i++) {
$this->smsMapper->setLastReadDate($this->userId, $phoneNumbers[$i], $maxDate);
}
}
// @ TODO: filter correctly // @ TODO: filter correctly
return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName,

View File

@ -151,16 +151,28 @@ class SmsMapper extends Mapper {
return $phoneList; return $phoneList;
} }
public function setLastReadDate ($userId, $lastDate) { public function getLastReadDate ($userId) {
$sql = 'SELECT MAX(datavalue) as mx FROM ' .
'*PREFIX*ocsms_user_datas WHERE user_id = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($userId));
if ($row = $result->fetchRow()) {
return $row["mx"];
}
}
public function setLastReadDate ($userId, $phoneNumber, $lastDate) {
\OCP\DB::beginTransaction(); \OCP\DB::beginTransaction();
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_user_datas ' . $query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_user_datas ' .
'WHERE user_id = ? AND datakey = ?'); 'WHERE user_id = ? AND datakey = ?');
$query->execute(array($userId, 'lastReadDate')); $query->execute(array($userId, 'lastReadDate-' . $phoneNumber));
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_user_datas' . $query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_user_datas' .
'(user_id, datakey, datavalue) VALUES ' . '(user_id, datakey, datavalue) VALUES ' .
'(?,?,?)'); '(?,?,?)');
$query->execute(array($userId, 'lastReadDate', $lastDate)); $query->execute(array($userId, 'lastReadDate-' . $phoneNumber, $lastDate));
\OCP\DB::commit(); \OCP\DB::commit();
} }