diff --git a/index.html b/index.html index 07eacfac..1bb283d4 100644 --- a/index.html +++ b/index.html @@ -32,14 +32,14 @@ - + - - + + - + diff --git a/js/app.js b/js/app.js index 828de512..385303fb 100644 --- a/js/app.js +++ b/js/app.js @@ -48,7 +48,7 @@ config(['$locationProvider', '$routeProvider', '$compileProvider', function($loc // $locationProvider.html5Mode(true); $routeProvider.when('/', {templateUrl: 'partials/welcome.html', controller: 'AppWelcomeController'}); - $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'AppLoginController'}); + $routeProvider.when('/login', {templateUrl: 'partials/login.html?1', controller: 'AppLoginController'}); $routeProvider.when('/im', {templateUrl: 'partials/im.html', controller: 'AppIMController', reloadOnSearch: false}); $routeProvider.otherwise({redirectTo: '/'}); diff --git a/js/controllers.js b/js/controllers.js index 38595c1c..acd0fc38 100644 --- a/js/controllers.js +++ b/js/controllers.js @@ -25,6 +25,7 @@ angular.module('myApp.controllers', []) var dcID = 1; $scope.credentials = {}; + $scope.progress = {}; function saveAuth (result) { MtpApiManager.setUserAuth(dcID, { @@ -36,18 +37,21 @@ angular.module('myApp.controllers', []) }; $scope.sendCode = function () { + $scope.progress.enabled = true; MtpApiManager.invokeApi('auth.sendCode', { phone_number: $scope.credentials.phone_number, sms_type: 0, api_id: 2496, api_hash: '8da85b0d5bfe62527e5b244c209159c3' }, {dcID: dcID}).then(function (sentCode) { + $scope.progress.enabled = false; $scope.credentials.phone_code_hash = sentCode.phone_code_hash; $scope.credentials.phone_occupied = sentCode.phone_registered; $scope.error = {}; }, function (error) { + $scope.progress.enabled = false; dLog('sendCode', error); if (error.code == 303) { var newDcID = error.type.match(/^(PHONE_MIGRATE_|NETWORK_MIGRATE_)(\d+)/)[2]; @@ -79,13 +83,16 @@ angular.module('myApp.controllers', []) }); } + $scope.progress.enabled = true; MtpApiManager.invokeApi(method, params, {dcID: dcID}).then(saveAuth, function (error) { + $scope.progress.enabled = false; if (error.code == 400 && error.type == 'PHONE_NUMBER_UNOCCUPIED') { return $scope.logIn(true); } else if (error.code == 400 && error.type == 'PHONE_NUMBER_UNOCCUPIED') { return $scope.logIn(false); } + switch (error.type) { case 'FIRSTNAME_INVALID': $scope.error = {field: 'first_name'}; diff --git a/js/directives.js b/js/directives.js index d99ec109..9b778576 100644 --- a/js/directives.js +++ b/js/directives.js @@ -331,8 +331,7 @@ angular.module('myApp.directives', ['myApp.filters']) function link (scope, element, attrs) { scope.$watch('thumb.location', function (newVal) { - if (!scope.thumb) dLog(222, scope); - if (!scope.thumb.location) { + if (!scope.thumb || !scope.thumb.location) { element.attr('src', scope.thumb.placeholder || ''); return; } diff --git a/js/lib/mtproto.js b/js/lib/mtproto.js index 0b029e9b..75315d8b 100644 --- a/js/lib/mtproto.js +++ b/js/lib/mtproto.js @@ -2117,14 +2117,23 @@ factory('MtpApiManager', function (AppConfigManager, MtpAuthorizer, MtpNetworker }; function mtpInvokeApi (method, params, options) { - var deferred = $q.defer(); - options = options || {}; - var dcID = options.dcID || baseDcID || 1; + + var deferred = $q.defer(), + dcID, + networkerPromise; + + if (dcID = options.dcID) { + networkerPromise = mtpGetNetworker(dcID); + } else { + networkerPromise = AppConfigManager.get('dc').then(function (baseDcID) { + return mtpGetNetworker(dcID = baseDcID || 1); + }); + } var cachedNetworker; - mtpGetNetworker(dcID).then(function (networker) { + networkerPromise.then(function (networker) { return (cachedNetworker = networker).wrapApiCall(method, params, options).then( function (result) { deferred.resolve(result); @@ -2175,13 +2184,9 @@ factory('MtpApiManager', function (AppConfigManager, MtpAuthorizer, MtpNetworker }; function mtpGetUserID () { - var deferred = $q.defer(); - - AppConfigManager.get('user_auth').then(function (auth) { - deferred.resolve(auth.id || 0); + return AppConfigManager.get('user_auth').then(function (auth) { + return auth.id || 0; }); - - return deferred.promise; } return { diff --git a/js/services.js b/js/services.js index 709f7224..8fbf48eb 100644 --- a/js/services.js +++ b/js/services.js @@ -927,6 +927,76 @@ angular.module('myApp.services', []) return []; } + function notifyAboutMessage (message) { + var peerID = getMessagePeer(message); + var fromUser = AppUsersManager.getUser(message.from_id); + var fromPhoto = AppUsersManager.getUserPhoto(message.from_id, 'User'); + var peerString; + var notification = {}, + notificationMessage = false, + notificationPhoto; + + if (message.message) { + notificationMessage = message.message; + } else if (message.media && message.media._ != 'messageMediaEmpty') { + switch (message.media._) { + case 'messageMediaPhoto': notificationMessage = 'Photo'; break; + case 'messageMediaVideo': notificationMessage = 'Video'; break; + case 'messageMediaDocument': notificationMessage = 'Document'; break; + case 'messageMediaGeo': notificationMessage = 'Location'; break; + case 'messageMediaContact': notificationMessage = 'Contact'; break; + default: notificationMessage = 'Attachment'; break; + } + } else if (message._ == 'messageService') { + switch (message.action._) { + case 'messageActionChatCreate': notificationMessage = 'created the group'; break; + case 'messageActionChatEditTitle': notificationMessage = 'changed group name'; break; + case 'messageActionChatEditPhoto': notificationMessage = 'changed group photo'; break; + case 'messageActionChatDeletePhoto': notificationMessage = 'removed group photo'; break; + case 'messageActionChatAddUser': notificationMessage = 'invited user'; break; + case 'messageActionChatDeleteUser': notificationMessage = 'kicked user'; break; + } + } + + if (peerID > 0) { + notification.title = (fromUser.first_name || '') + + (fromUser.first_name && fromUser.last_name ? ' ' : '') + + (fromUser.last_name || ''); + + notificationPhoto = fromPhoto; + + peerString = AppUsersManager.getUserString(peerID); + + } else { + notification.title = (fromUser.first_name || fromUser.last_name || 'Somebody') + + ' @ ' + + (AppChatsManager.getChat(-peerID).title || 'Unknown chat'); + + notificationPhoto = AppChatsManager.getChatPhoto(-peerID, 'Chat'); + + peerString = AppChatsManager.getChatString(-peerID); + } + + notification.onclick = function () { + $location.url('/im?p=' + peerString); + }; + + notification.message = notificationMessage; + notification.image = notificationPhoto.placeholder; + + if (notificationPhoto.location) { + MtpApiFileManager.downloadSmallFile(notificationPhoto.location, notificationPhoto.size).then(function (url) { + notification.image = url; + + if (message.unread) { + NotificationsManager.notify(notification); + } + }); + } else { + NotificationsManager.notify(notification); + } + } + $rootScope.$on('apiUpdate', function (e, update) { dLog('on apiUpdate', update); switch (update._) { @@ -989,54 +1059,12 @@ angular.module('myApp.services', []) if ($rootScope.idle.isIDLE && !message.out && message.unread) { - var fromUser = AppUsersManager.getUser(message.from_id); - var fromPhoto = AppUsersManager.getUserPhoto(message.from_id, 'User'); - var peerString; - var notification = {}, - notificationPhoto; - - if (peerID > 0) { - notification.title = (fromUser.first_name || '') + - (fromUser.first_name && fromUser.last_name ? ' ' : '') + - (fromUser.last_name || ''); - - notification.message = message.message; - - notificationPhoto = fromPhoto; - - peerString = AppUsersManager.getUserString(peerID); - - } else { - notification.title = fromUser.first_name || fromUser.last_name || 'Somebody' + - ' @ ' + - AppChatsManager.getChat(-peerID).title || 'Unknown chat'; - - notification.message = message.message; - - notificationPhoto = AppChatsManager.getChatPhoto(-peerID); - - peerString = AppChatsManager.getChatString(-peerID); - } - - notification.onclick = function () { - $location.url('/im?p=' + peerString); - }; - - notification.image = notificationPhoto.placeholder; - - if (notificationPhoto.location) { - MtpApiFileManager.downloadSmallFile(notificationPhoto.location, notificationPhoto.size).then(function (url) { - notification.image = url; - - if (message.unread) { - // dLog(111, notification); - NotificationsManager.notify(notification); - } - }); - } else { - // dLog(222, notification); - NotificationsManager.notify(notification); - } + NotificationsManager.getPeerSettings(peerID).then(function (muted) { + if (!message.unread || muted) { + return; + } + notifyAboutMessage(message); + }); } break; @@ -1787,11 +1815,12 @@ angular.module('myApp.services', []) }) -.service('NotificationsManager', function ($rootScope, $window, $timeout, $interval, IdleManager) { +.service('NotificationsManager', function ($rootScope, $window, $timeout, $interval, MtpApiManager, AppPeersManager, IdleManager) { var notificationsUiSupport = window.webkitNotifications !== undefined; var notificationsShown = []; var notificationsCount = 0; + var peerSettings = {}; var titleBackup = document.title, titlePromise; @@ -1820,9 +1849,27 @@ angular.module('myApp.services', []) return { start: start, - notify: notify + notify: notify, + getPeerSettings: getPeerSettings }; + function getPeerSettings (peerID) { + if (peerSettings[peerID] !== undefined) { + return peerSettings[peerID]; + } + + return peerSettings[peerID] = MtpApiManager.invokeApi('account.getNotifySettings', { + peer: { + _: 'inputNotifyPeer', + peer: AppPeersManager.getInputPeerByID(peerID) + } + }).then(function (peerNotifySettings) { + // dLog('got settings', peerID, peerNotifySettings); + return peerNotifySettings._ == 'peerNotifySettings' && + peerNotifySettings.mute_until * 1000 > (+new Date()); + }); + } + function start () { if (!notificationsUiSupport) { return false; diff --git a/partials/dialog.html b/partials/dialog.html index e81014e9..a259d300 100644 --- a/partials/dialog.html +++ b/partials/dialog.html @@ -32,7 +32,7 @@ Photo - Video + Video Document diff --git a/partials/login.html b/partials/login.html index 0b616d4c..974e0e89 100644 --- a/partials/login.html +++ b/partials/login.html @@ -11,7 +11,9 @@ - +
@@ -23,6 +25,8 @@ - +
\ No newline at end of file