mirror of
https://github.com/nerzhul/ocsms.git
synced 2025-06-08 00:16:24 +00:00
Implement Refresh conversation list and set new messages count into list
This commit is contained in:
parent
a09faa67ad
commit
6d26edb727
@ -21,5 +21,6 @@ $application->registerRoutes($this, array('routes' => array(
|
|||||||
array('name' => 'sms#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'),
|
array('name' => 'sms#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'),
|
||||||
array('name' => 'sms#retrieve_all_peers', 'url' => '/get/peerlist', 'verb' => 'GET'),
|
array('name' => 'sms#retrieve_all_peers', 'url' => '/get/peerlist', 'verb' => 'GET'),
|
||||||
array('name' => 'sms#get_conversation', 'url' => '/get/conversation', 'verb' => 'GET'),
|
array('name' => 'sms#get_conversation', 'url' => '/get/conversation', 'verb' => 'GET'),
|
||||||
|
array('name' => 'sms#check_new_messages', 'url' => '/get/new_messages', 'verb' => 'GET'),
|
||||||
array('name' => 'sms#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'),
|
array('name' => 'sms#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'),
|
||||||
)));
|
)));
|
||||||
|
@ -170,6 +170,28 @@ class SmsController extends Controller {
|
|||||||
"phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount));
|
"phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*/
|
||||||
|
public function checkNewMessages($lastDate) {
|
||||||
|
$phoneList = $this->smsMapper->getNewMessagesCountForAllPhonesNumbers($this->userId, $lastDate);
|
||||||
|
$contactsSrc = $this->app->getContacts();
|
||||||
|
$contacts = array();
|
||||||
|
|
||||||
|
$countPhone = count($phoneList);
|
||||||
|
foreach ($phoneList as $number => $ts) {
|
||||||
|
$fmtPN = preg_replace("#[ ]#","/", $number);
|
||||||
|
if (isset($contactsSrc[$fmtPN])) {
|
||||||
|
$fmtPN2 = preg_replace("#\/#","", $fmtPN);
|
||||||
|
$contacts[$fmtPN] = $contactsSrc[$fmtPN];
|
||||||
|
$contacts[$fmtPN2] = $contactsSrc[$fmtPN];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
|
@ -133,6 +133,23 @@ class SmsMapper extends Mapper {
|
|||||||
return $phoneList;
|
return $phoneList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) {
|
||||||
|
$sql = 'SELECT sms_address,count(sms_date) as ct FROM ' .
|
||||||
|
'*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?) ' .
|
||||||
|
'AND sms_date > ? GROUP BY sms_address';
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array($userId, 0, 1, $lastDate));
|
||||||
|
|
||||||
|
$phoneList = array();
|
||||||
|
while ($row = $result->fetchRow()) {
|
||||||
|
$phoneNumber = preg_replace("#[ ]#", "/", $row["sms_address"]);
|
||||||
|
if (!in_array($phoneNumber, $phoneList)) {
|
||||||
|
$phoneList[$phoneNumber] = $row["ct"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $phoneList;
|
||||||
|
}
|
||||||
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
||||||
\OCP\DB::beginTransaction();
|
\OCP\DB::beginTransaction();
|
||||||
|
|
||||||
|
57
js/script.js
57
js/script.js
@ -63,6 +63,53 @@ var refreshConversation = function() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var checkNewMessages = function() {
|
||||||
|
$.getJSON(OC.generateUrl('/apps/ocsms/get/new_messages'),
|
||||||
|
{ 'lastDate': lastMsgDate },
|
||||||
|
function(jsondata, status) {
|
||||||
|
var peerListBuf = '';
|
||||||
|
var bufferedContacts = [];
|
||||||
|
|
||||||
|
$.each(jsondata['phonelist'], function(id, val) {
|
||||||
|
var fn, peerLabel, idxVal;
|
||||||
|
idxVal = id.replace(/\//g,' ');
|
||||||
|
idxVal2 = idxVal.replace('/ /g','');
|
||||||
|
if (typeof jsondata['contacts'][id] == 'undefined') {
|
||||||
|
fn = '';
|
||||||
|
peerLabel = id;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fn = jsondata['contacts'][id];
|
||||||
|
peerLabel = fn;
|
||||||
|
}
|
||||||
|
if ($.inArray(peerLabel, bufferedContacts) == -1) {
|
||||||
|
$("a[mailbox-label='" + peerLabel + "']").remove();
|
||||||
|
peerListBuf = '<li><a href="#" mailbox-navigation="' + idxVal2 + '" style="font-weight: bold;" mailbox-label="' + peerLabel + '">' + peerLabel + ' (' + val + ')</a></li>';
|
||||||
|
$('#app-mailbox-peers ul').prepend(peerListBuf);
|
||||||
|
bufferedContacts.push(peerLabel);
|
||||||
|
|
||||||
|
if (idxVal == curPhoneNumber) {
|
||||||
|
changeSelectedConversation($("a[mailbox-navigation='" + idxVal + "']"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now bind the events when we click on the phone number
|
||||||
|
$("a[mailbox-navigation='" + idxVal + "']").on('click', function (event) {
|
||||||
|
var phoneNumber = $(this).attr('mailbox-navigation');
|
||||||
|
OC.Util.History.pushState('phonenumber=' + phoneNumber);
|
||||||
|
|
||||||
|
// phoneNumber must exist
|
||||||
|
if (phoneNumber != null) {
|
||||||
|
fetchConversation(phoneNumber);
|
||||||
|
changeSelectedConversation($(this));
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
function setMessageCountInfo(jsondata) {
|
function setMessageCountInfo(jsondata) {
|
||||||
if (typeof jsondata['msgCount'] != 'undefined') {
|
if (typeof jsondata['msgCount'] != 'undefined') {
|
||||||
if (jsondata['msgCount'] == 1) {
|
if (jsondata['msgCount'] == 1) {
|
||||||
@ -153,7 +200,8 @@ function formatConversation(jsondata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Store the greater msg date for refresher
|
// Store the greater msg date for refresher
|
||||||
if (id > lastMsgDate) {
|
// Note: we divide by 100 because number compare too large integers
|
||||||
|
if ((id/100) > (lastMsgDate/100)) {
|
||||||
lastMsgDate = id;
|
lastMsgDate = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,6 +236,8 @@ function changeSelectedConversation(item) {
|
|||||||
}
|
}
|
||||||
selectedConversation = item;
|
selectedConversation = item;
|
||||||
selectedConversation.parent().addClass('selected');
|
selectedConversation.parent().addClass('selected');
|
||||||
|
selectedConversation.css("font-weight", "normal");
|
||||||
|
selectedConversation.html(selectedConversation.attr("mailbox-label"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchInitialPeerList(jsondata) {
|
function fetchInitialPeerList(jsondata) {
|
||||||
@ -209,7 +259,7 @@ function fetchInitialPeerList(jsondata) {
|
|||||||
peerLabel = fn;
|
peerLabel = fn;
|
||||||
}
|
}
|
||||||
if ($.inArray(peerLabel, bufferedContacts) == -1) {
|
if ($.inArray(peerLabel, bufferedContacts) == -1) {
|
||||||
peerListBuf += '<li><a href="#" mailbox-navigation="' + idxVal + '">' + peerLabel + '</a></li>';
|
peerListBuf += '<li><a href="#" mailbox-navigation="' + idxVal2 + '" mailbox-label="' + peerLabel + '">' + peerLabel + '</a></li>';
|
||||||
bufferedContacts.push(peerLabel);
|
bufferedContacts.push(peerLabel);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -260,8 +310,6 @@ function desktopNotify(msg) {
|
|||||||
$('#app-mailbox-peers').find('a[mailbox-navigation]').on('click', function (event) {
|
$('#app-mailbox-peers').find('a[mailbox-navigation]').on('click', function (event) {
|
||||||
var phoneNumber = $(this).attr('mailbox-navigation');
|
var phoneNumber = $(this).attr('mailbox-navigation');
|
||||||
OC.Util.History.pushState('phonenumber=' + phoneNumber);
|
OC.Util.History.pushState('phonenumber=' + phoneNumber);
|
||||||
// Reset it for refreshConversation
|
|
||||||
lastMsgDate = 0;
|
|
||||||
|
|
||||||
// phoneNumber must exist
|
// phoneNumber must exist
|
||||||
if (phoneNumber != null) {
|
if (phoneNumber != null) {
|
||||||
@ -293,6 +341,7 @@ function desktopNotify(msg) {
|
|||||||
});
|
});
|
||||||
initDesktopNotifies();
|
initDesktopNotifies();
|
||||||
setInterval(refreshConversation, 10000);
|
setInterval(refreshConversation, 10000);
|
||||||
|
setInterval(checkNewMessages, 10000);
|
||||||
});
|
});
|
||||||
|
|
||||||
// reset count and title
|
// reset count and title
|
||||||
|
Loading…
x
Reference in New Issue
Block a user