From 7a62a2213c4cd489175bab9bf10543061a7ccdf6 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 24 Jan 2014 19:04:31 +0400 Subject: [PATCH] Added message drafts saved in localStorage --- app/index.html | 6 +++--- app/js/controllers.js | 29 ++++++++++++++++++++++++----- app/js/directives.js | 9 ++++++++- app/js/lib/mtproto.js | 36 +++++++++++++++++++++++++++--------- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/app/index.html b/app/index.html index 825dbb1e..8d09fec6 100644 --- a/app/index.html +++ b/app/index.html @@ -37,14 +37,14 @@ - + - + - + diff --git a/app/js/controllers.js b/app/js/controllers.js index aeede4f0..ecf8c623 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -329,6 +329,7 @@ angular.module('myApp.controllers', []) $scope.$broadcast('ui_history_change'); AppMessagesManager.readHistory($scope.curDialog.inputPeer); + }, function () { $scope.state = {error: true}; }); @@ -408,7 +409,7 @@ angular.module('myApp.controllers', []) $scope.$on('user_update', angular.noop); }) - .controller('AppImSendController', function ($scope, $timeout, MtpApiManager, AppPeersManager, AppMessagesManager, ApiUpdatesManager, MtpApiFileManager) { + .controller('AppImSendController', function ($scope, $timeout, MtpApiManager, AppConfigManager, AppPeersManager, AppMessagesManager, ApiUpdatesManager, MtpApiFileManager) { $scope.$watch('curDialog.peer', resetDraft); $scope.$on('user_update', angular.noop); @@ -419,6 +420,16 @@ angular.module('myApp.controllers', []) $scope.$watch('draftMessage.text', function (newVal) { AppMessagesManager.readHistory($scope.curDialog.inputPeer); + if (newVal.length) { + var backupDraftObj = {}; + backupDraftObj['draft' + $scope.curDialog.peerID] = newVal; + AppConfigManager.set(backupDraftObj); + // dLog('draft save', backupDraftObj); + } else { + AppConfigManager.remove('draft' + $scope.curDialog.peerID); + // dLog('draft delete', 'draft' + $scope.curDialog.peerID); + } + var now = +new Date(); if (newVal === undefined || !newVal.length || now - lastTyping < 6000) { return; @@ -461,10 +472,18 @@ angular.module('myApp.controllers', []) } - function resetDraft () { - $scope.draftMessage = { - text: '' - }; + function resetDraft (newPeer) { + if (newPeer) { + AppConfigManager.get('draft' + $scope.curDialog.peerID).then(function (draftText) { + // dLog('Restore draft', 'draft' + $scope.curDialog.peerID, draftText); + $scope.draftMessage.text = draftText || ''; + // dLog('send broadcast', $scope.draftMessage); + $scope.$broadcast('ui_peer_draft'); + }); + } else { + // dLog('Reset peer'); + $scope.draftMessage.text = ''; + } } function onFilesSelected (newVal) { diff --git a/app/js/directives.js b/app/js/directives.js index 7788c4bd..9d5e62f2 100644 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -282,20 +282,27 @@ angular.module('myApp.directives', ['myApp.filters']) if (richTextarea) { scope.$watch('draftMessage.text', function (newVal) { + dLog('on update', newVal); if (!newVal.length && !messageField.value.length) { $timeout(function () { - $(richTextarea).html(''); + updateField(); }, 0); } }); } + function updateField () { + dLog(scope.draftMessage); + dLog('update field', scope.draftMessage.text); + $(richTextarea).text(scope.draftMessage.text || ''); + } $('body').on('dragenter dragleave dragover drop', onDragDropEvent); scope.$on('ui_peer_change', focusField); scope.$on('ui_history_change', focusField); scope.$on('ui_message_send', focusField); + scope.$on('ui_peer_draft', updateField); scope.$on('$destroy', function cleanup() { $('body').off('dragenter dragleave dragover drop', onDragDropEvent); diff --git a/app/js/lib/mtproto.js b/app/js/lib/mtproto.js index a0e0babf..ae2c4b53 100644 --- a/app/js/lib/mtproto.js +++ b/app/js/lib/mtproto.js @@ -351,7 +351,7 @@ function TLSerialization (options) { this.createBuffer(); - this.debug = options.debug !== undefined ? options.debug : window._debugMode; + // this.debug = options.debug !== undefined ? options.debug : window._debugMode; this.mtproto = options.mtproto || false; return this; } @@ -631,7 +631,7 @@ function TLDeserialization (buffer, options) { this.intView = new Uint32Array(this.buffer); this.byteView = new Uint8Array(this.buffer); - this.debug = options.debug !== undefined ? options.debug : window._debugMode; + // this.debug = options.debug !== undefined ? options.debug : window._debugMode; this.mtproto = options.mtproto || false; return this; } @@ -1021,7 +1021,7 @@ factory('MtpMessageIdGenerator', function (AppConfigManager) { }); function generateMessageID () { - var timeTicks = +new Date() + timeOffset, + var timeTicks = +new Date() + (timeOffset * 1000), timeSec = Math.floor(timeTicks / 1000), timeMSec = timeTicks % 1000, random = nextRandomInt(0xFFFF); @@ -1042,6 +1042,7 @@ factory('MtpMessageIdGenerator', function (AppConfigManager) { function applyServerTime (serverTime, localTime) { timeOffset = serverTime - Math.floor((localTime || +new Date()) / 1000); + dLog('Apply server time', serverTime, localTime, timeOffset); AppConfigManager.set({server_time_offset: timeOffset}); }; @@ -1787,7 +1788,9 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato this.sentMessages[message.msg_id] = containerSentMessage; - // dLog('Container', innerMessages, message.msg_id, message.seq_no); + if (window._debugMode) { + dLog('Container', innerMessages, message.msg_id, message.seq_no); + } } else { if (message.noResponse) { noResponseMsgs.push(message.msg_id); @@ -2035,7 +2038,7 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato break; case 'bad_server_salt': - dLog('bad server salt', message); + dLog('Bad server salt', message); var sentMsg = this.sentMessages[message.bad_msg_id]; if (!sentMsg || sentMsg.seq_no != message.bad_msg_seqno) { dLog(message.bad_msg_id, message.bad_msg_seqno); @@ -2047,6 +2050,21 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato this.ackMessage(messageID); break; + case 'bad_msg_notification': + dLog('Bad msg notification', message); + var sentMsg = this.sentMessages[message.bad_msg_id]; + if (!sentMsg || sentMsg.seq_no != message.bad_msg_seqno) { + dLog(message.bad_msg_id, message.bad_msg_seqno); + throw new Error('Bad msg notification for invalid message'); + } + + if (message.error_code == 16 || message.error_code == 17) { + MtpMessageIdGenerator.applyServerTime((new BigInteger(messageID, 10)).shiftRight(32).toString(10)); + this.pushResend(message.bad_msg_id); + this.ackMessage(messageID); + } + break; + case 'message': this.serverMessages.push(message.msg_id); this.processMessage(message.body, message.msg_id, sessionID); @@ -2439,7 +2457,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { cacheFileWriter, errorHandler = function (error) { deferred.reject(error); - if (cacheFileWriter) cacheFileWriter.truncate(); + if (cacheFileWriter) cacheFileWriter.truncate(0); errorHandler = angular.noop; }; @@ -2474,7 +2492,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { cacheFileWriter, errorHandler = function (error) { deferred.reject(error); - if (cacheFileWriter) cacheFileWriter.truncate(); + if (cacheFileWriter) cacheFileWriter.truncate(0); errorHandler = angular.noop; }, doDownload = function () { @@ -2543,7 +2561,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { console.error(error); // dLog('fail'); deferred.reject(error); - if (cacheFileWriter) cacheFileWriter.truncate(); + if (cacheFileWriter) cacheFileWriter.truncate(0); errorHandler = angular.noop; }, saveToFileEntry = function (fileEntry) { @@ -2685,7 +2703,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { errorHandler = function (error) { dLog('fail'); deferred.reject(error); - if (cacheFileWriter) cacheFileWriter.truncate(); + if (cacheFileWriter) cacheFileWriter.truncate(0); errorHandler = angular.noop; };