From 8f4dfd217fc861458fc826c45f7e213cd341486b Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 23 Oct 2014 15:36:55 +0000 Subject: [PATCH] Added table to store last read date. This permit to set unread messages on first refresh of contact list. We need to handle it at first load too Note: this is an experimental thing. We need to set this cursor for every conversation to get better precision --- appinfo/database.xml | 24 ++++++++++++++++++++++++ appinfo/info.xml | 2 +- controller/smscontroller.php | 9 ++++++++- db/smsmapper.php | 14 ++++++++++++++ js/script.js | 2 ++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 5085676..4304bea 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -5,6 +5,30 @@ false utf8 + + *dbprefix*ocsms_user_datas + + + user_id + text + true + 64 + + + datakey + text + true + 64 + + + datavalue + text + 64 + true + + + +
*dbprefix*ocsms_smsdatas diff --git a/appinfo/info.xml b/appinfo/info.xml index e9d70a2..4c1ff67 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,6 +5,6 @@ Owncloud SMS app AGPL Loic Blot - 1.2.0 + 1.2.3 7 diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 11e4d2d..8cedb60 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -93,6 +93,7 @@ class SmsController extends Controller { $contacts = array(); $countPhone = count($phoneList); + $maxTS = 0; foreach ($phoneList as $number => $ts) { $fmtPN = preg_replace("#[ ]#","/", $number); if (isset($contactsSrc[$fmtPN])) { @@ -100,9 +101,15 @@ class SmsController extends Controller { $contacts[$fmtPN] = $contactsSrc[$fmtPN]; $contacts[$fmtPN2] = $contactsSrc[$fmtPN]; } + + if ($ts > $maxTS) { + $maxTS = $ts; + } } - return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts)); + $this->smsMapper->setLastReadDate($this->userId, $maxTS); + + return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts, "lastRead" => $maxTS)); } /** diff --git a/db/smsmapper.php b/db/smsmapper.php index c3535fe..d492c87 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -150,6 +150,20 @@ class SmsMapper extends Mapper { } return $phoneList; } + + public function setLastReadDate ($userId, $lastDate) { + \OCP\DB::beginTransaction(); + $query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_user_datas ' . + 'WHERE user_id = ? AND datakey = ?'); + $query->execute(array($userId, 'lastReadDate')); + + $query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_user_datas' . + '(user_id, datakey, datavalue) VALUES ' . + '(?,?,?)'); + $query->execute(array($userId, 'lastReadDate', $lastDate)); + \OCP\DB::commit(); + } + public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { \OCP\DB::beginTransaction(); diff --git a/js/script.js b/js/script.js index 06e363f..83c6f03 100644 --- a/js/script.js +++ b/js/script.js @@ -264,6 +264,8 @@ function fetchInitialPeerList(jsondata) { } }); + lastMsgDate = jsondata["lastRead"]; + // Only modify peerList if there is peers if (peerListBuf != '') { $('#app-mailbox-peers ul').html(peerListBuf);