From fcfe6133390774550fd9a6ad0e5d101cd532375d Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Wed, 4 Jun 2014 19:40:27 +0400 Subject: [PATCH] Added userpic history --- app/css/app.css | 6 +- app/js/controllers.js | 177 +++++++++++++++++++++++++++---- app/js/services.js | 99 ++++++++++++----- app/partials/confirm_modal.html | 2 +- app/partials/error_modal.html | 3 + app/partials/peer_select.html | 2 +- app/partials/photo_modal.html | 6 +- app/partials/settings_modal.html | 4 +- app/partials/user_modal.html | 4 +- 9 files changed, 244 insertions(+), 59 deletions(-) diff --git a/app/css/app.css b/app/css/app.css index ddd4510e..7bb51300 100644 --- a/app/css/app.css +++ b/app/css/app.css @@ -38,6 +38,10 @@ a:hover { a:hover { cursor: pointer; } +a.disabled { + cursor: default; + pointer-events: none; +} .form-control { color: #000; border: 1px solid #d9dbde; @@ -2039,9 +2043,9 @@ img.img_fullsize { margin-bottom: 15px; } .user_modal_image_wrap { + display: block; width: 100px; margin-right: 22px; - display: block; overflow: hidden; border-radius: 3px; } diff --git a/app/js/controllers.js b/app/js/controllers.js index 0974a908..0a8f3833 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -1017,6 +1017,7 @@ angular.module('myApp.controllers', []) if (searchCachedResult.history.indexOf($scope.messageID) >= 0) { list = searchCachedResult.history; maxID = list[list.length - 1]; + hasMore = list.length < searchCachedResult.count; updatePrevNext(); } @@ -1052,7 +1053,7 @@ angular.module('myApp.controllers', []) maxID = searchResult.history[searchResult.history.length - 1]; list = list.concat(searchResult.history); - hasMore = searchResult.history.length || list.length < searchResult.count; + hasMore = list.length < searchResult.count; updatePrevNext(); loadingPromise = false; }); @@ -1062,6 +1063,7 @@ angular.module('myApp.controllers', []) var index = list.indexOf($scope.messageID); $scope.nav.hasNext = index > 0; $scope.nav.hasPrev = hasMore || index < list.length - 1; + $scope.canForward = $scope.canDelete = $scope.messageID > 0; }; $scope.nav.next = function () { @@ -1125,6 +1127,116 @@ angular.module('myApp.controllers', []) }) + .controller('UserpicModalController', function ($q, $scope, $rootScope, $modalInstance, AppPhotosManager, AppUsersManager, AppPeersManager, AppMessagesManager, PeersSelectService, ErrorService) { + + $scope.photo = AppPhotosManager.wrapForFull($scope.photoID); + $scope.nav = {}; + $scope.canForward = true; + + var inputUser = AppUsersManager.getUserInput($scope.userID), + list = [$scope.photoID], + maxID = $scope.photoID, + hasMore = true; + + updatePrevNext(); + + AppPhotosManager.getUserPhotos(inputUser, 0, 1000).then(function (userpicCachedResult) { + if (userpicCachedResult.photos.indexOf($scope.photoID) >= 0) { + list = userpicCachedResult.photos; + maxID = list[list.length - 1]; + hasMore = list.length < userpicCachedResult.count; + + updatePrevNext(); + } + }); + + + var jump = 0; + function movePosition (sign) { + var curIndex = list.indexOf($scope.photoID), + index = curIndex >= 0 ? curIndex + sign : 0, + curJump = ++jump; + + var promise = index >= list.length ? loadMore() : $q.when(); + promise.then(function () { + if (curJump != jump) { + return; + } + + $scope.photoID = list[index]; + $scope.photo = AppPhotosManager.wrapForFull($scope.photoID); + + updatePrevNext(); + }); + }; + + var loadingPromise = false; + function loadMore () { + if (loadingPromise) return loadingPromise; + + return loadingPromise = AppPhotosManager.getUserPhotos(inputUser, maxID).then(function (userpicResult) { + maxID = userpicResult.photos[userpicResult.photos.length - 1]; + list = list.concat(userpicResult.photos); + + hasMore = list.length < userpicResult.count; + + updatePrevNext(); + loadingPromise = false; + }, function () { + loadingPromise = false; + }); + }; + + function updatePrevNext () { + var index = list.indexOf($scope.photoID); + $scope.nav.hasNext = index > 0; + $scope.nav.hasPrev = hasMore || index < list.length - 1; + }; + + $scope.nav.next = function () { + if (!$scope.nav.hasNext) { + return false; + } + + movePosition(-1); + }; + + $scope.nav.prev = function () { + if (!$scope.nav.hasPrev) { + return false; + } + movePosition(+1); + }; + + $scope.forward = function () { + var messageID = $scope.photoID; + PeersSelectService.selectPeer({confirm_type: 'FORWARD_PEER'}).then(function (peerString) { + var peerID = AppPeersManager.getPeerID(peerString); + AppMessagesManager.sendOther(peerID, { + _: 'inputMediaPhoto', + id: { + _: 'inputPhoto', + id: $scope.photoID, + access_hash: $scope.photo.access_hash, + } + }); + $rootScope.$broadcast('history_focus', {peerString: peerString}); + }); + }; + + $scope.delete = function () { + var messageID = $scope.photoID; + ErrorService.confirm({type: 'MESSAGE_DELETE'}).then(function () { + AppMessagesManager.deleteMessages([messageID]); + }); + }; + + $scope.download = function () { + AppPhotosManager.downloadPhoto($scope.photoID); + }; + + }) + .controller('VideoModalController', function ($scope, $rootScope, $modalInstance, PeersSelectService, AppMessagesManager, AppVideoManager, AppPeersManager, ErrorService) { $scope.video = AppVideoManager.wrapForFull($scope.videoID); @@ -1156,7 +1268,7 @@ angular.module('myApp.controllers', []) }); }) - .controller('UserModalController', function ($scope, $location, $rootScope, $modal, AppUsersManager, NotificationsManager, AppMessagesManager, AppPeersManager, PeersSelectService, ErrorService) { + .controller('UserModalController', function ($scope, $location, $rootScope, $modal, AppUsersManager, MtpApiManager, NotificationsManager, AppPhotosManager, AppMessagesManager, AppPeersManager, PeersSelectService, ErrorService) { var peerString = AppUsersManager.getUserString($scope.userID); @@ -1165,20 +1277,31 @@ angular.module('myApp.controllers', []) $scope.settings = {notifications: true}; - NotificationsManager.getPeerMuted($scope.userID).then(function (muted) { - $scope.settings.notifications = !muted; + MtpApiManager.invokeApi('users.getFullUser', { + id: AppUsersManager.getUserInput($scope.userID) + }).then(function (userFullResult) { + AppUsersManager.saveApiUser(userFullResult.user); + AppPhotosManager.savePhoto(userFullResult.profile_photo); + if (userFullResult.profile_photo._ != 'photoEmpty') { + $scope.userPhoto.id = userFullResult.profile_photo.id; + } - $scope.$watch('settings.notifications', function(newValue, oldValue) { - if (newValue === oldValue) { - return false; - } - NotificationsManager.getPeerSettings($scope.userID).then(function (settings) { - if (newValue) { - settings.mute_until = 0; - } else { - settings.mute_until = 2000000000; + NotificationsManager.savePeerSettings($scope.userID, userFullResult.notify_settings); + NotificationsManager.getPeerMuted($scope.userID).then(function (muted) { + $scope.settings.notifications = !muted; + + $scope.$watch('settings.notifications', function(newValue, oldValue) { + if (newValue === oldValue) { + return false; } - NotificationsManager.updatePeerSettings($scope.userID, settings); + NotificationsManager.getPeerSettings($scope.userID).then(function (settings) { + if (newValue) { + settings.mute_until = 0; + } else { + settings.mute_until = 2000000000; + } + NotificationsManager.updatePeerSettings($scope.userID, settings); + }); }); }); }); @@ -1409,7 +1532,7 @@ angular.module('myApp.controllers', []) }) - .controller('SettingsModalController', function ($rootScope, $scope, $timeout, $modal, AppUsersManager, AppChatsManager, MtpApiManager, AppConfigManager, NotificationsManager, MtpApiFileManager, ApiUpdatesManager, ChangelogNotifyService, ErrorService) { + .controller('SettingsModalController', function ($rootScope, $scope, $timeout, $modal, AppUsersManager, AppChatsManager, AppPhotosManager, MtpApiManager, AppConfigManager, NotificationsManager, MtpApiFileManager, ApiUpdatesManager, ChangelogNotifyService, ErrorService) { $scope.profile = {}; $scope.photo = {}; @@ -1420,6 +1543,16 @@ angular.module('myApp.controllers', []) $scope.photo = AppUsersManager.getUserPhoto(id, 'User'); }); + MtpApiManager.invokeApi('users.getFullUser', { + id: {_: 'inputUserSelf'} + }).then(function (userFullResult) { + AppUsersManager.saveApiUser(userFullResult.user); + AppPhotosManager.savePhoto(userFullResult.profile_photo); + if (userFullResult.profile_photo._ != 'photoEmpty') { + $scope.photo.id = userFullResult.profile_photo.id; + } + }); + $scope.notify = {}; $scope.send = {}; @@ -1550,11 +1683,11 @@ angular.module('myApp.controllers', []) } $rootScope.$broadcast('settings_changed'); } - - $scope.openChangelog = function () { - ChangelogNotifyService.showChangelog(false); - } }); + + $scope.openChangelog = function () { + ChangelogNotifyService.showChangelog(false); + } }) .controller('ProfileEditModalController', function ($rootScope, $scope, $timeout, $modal, $modalInstance, AppUsersManager, AppChatsManager, MtpApiManager, AppConfigManager, NotificationsManager, MtpApiFileManager, ApiUpdatesManager) { @@ -1599,7 +1732,7 @@ angular.module('myApp.controllers', []) } }) - .controller('ContactsModalController', function ($scope, $modal, $modalInstance, AppUsersManager) { + .controller('ContactsModalController', function ($scope, $modal, $modalInstance, AppUsersManager, ErrorService) { $scope.contacts = []; $scope.search = {}; @@ -1678,6 +1811,10 @@ angular.module('myApp.controllers', []) }).result.then(function (foundUserID) { if (foundUserID) { updateContacts($scope.search && $scope.search.query || ''); + } else { + ErrorService.show({ + error: {code: 404, type: 'USER_NOT_USING_TELEGRAM'} + }); } }); }; diff --git a/app/js/services.js b/app/js/services.js index a4225a1f..fff1c4e1 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -380,6 +380,7 @@ angular.module('myApp.services', []) saveApiUsers: saveApiUsers, saveApiUser: saveApiUser, getUser: getUser, + getUserInput: getUserInput, getUserPhoto: getUserPhoto, getUserString: getUserString, getUserSearchText: getUserSearchText, @@ -1410,6 +1411,10 @@ angular.module('myApp.services', []) case 'inputMediaContact': media = angular.extend({}, inputMedia, {_: 'messageMediaContact'}); break; + + case 'inputMediaPhoto': + media = {photo: AppPhotosManager.getPhoto(inputMedia.id.id)}; + break; } var message = { @@ -1982,7 +1987,7 @@ angular.module('myApp.services', []) } }) -.service('AppPhotosManager', function ($modal, $window, $timeout, $rootScope, MtpApiFileManager, AppUsersManager) { +.service('AppPhotosManager', function ($modal, $window, $timeout, $rootScope, MtpApiManager, MtpApiFileManager, AppUsersManager) { var photos = {}; function savePhoto (apiPhoto) { @@ -2016,6 +2021,56 @@ angular.module('myApp.services', []) return bestPhotoSize; } + function getUserPhotos (inputUser, maxID, limit) { + return MtpApiManager.invokeApi('photos.getUserPhotos', { + user_id: inputUser, + offset: 0, + limit: limit || 20, + max_id: maxID || 0 + }).then(function (photosResult) { + AppUsersManager.saveApiUsers(photosResult.users); + var photoIDs = []; + for (var i = 0; i < photosResult.photos.length; i++) { + savePhoto(photosResult.photos[i]); + photoIDs.push(photosResult.photos[i].id) + } + + return { + count: photosResult.count || photosResult.photos.length, + photos: photoIDs + }; + }); + } + + function preloadPhoto (photoID) { + if (!photos[photoID]) { + return; + } + var photo = photos[photoID], + fullWidth = $(window).width() - 36, + fullHeight = $($window).height() - 150, + fullPhotoSize = choosePhotoSize(photo, fullWidth, fullHeight); + + if (fullPhotoSize && !fullPhotoSize.preloaded) { + fullPhotoSize.preloaded = true; + if (fullPhotoSize.size) { + MtpApiFileManager.downloadFile(fullPhotoSize.location.dc_id, { + _: 'inputFileLocation', + volume_id: fullPhotoSize.location.volume_id, + local_id: fullPhotoSize.location.local_id, + secret: fullPhotoSize.location.secret + }, fullPhotoSize.size); + } else { + MtpApiFileManager.downloadSmallFile(fullPhotoSize.location); + } + } + }; + $rootScope.preloadPhoto = preloadPhoto; + + function getPhoto (photoID) { + return photos[photoID] || {_: 'photoEmpty'}; + } + function wrapForHistory (photoID) { var photo = angular.copy(photos[photoID]) || {_: 'photoEmpty'}, width = 260, @@ -2047,31 +2102,6 @@ angular.module('myApp.services', []) return photo; } - function preloadPhoto (photoID) { - if (!photos[photoID]) { - return; - } - var photo = photos[photoID], - fullWidth = $(window).width() - 36, - fullHeight = $($window).height() - 150, - fullPhotoSize = choosePhotoSize(photo, fullWidth, fullHeight); - - if (fullPhotoSize && !fullPhotoSize.preloaded) { - fullPhotoSize.preloaded = true; - if (fullPhotoSize.size) { - MtpApiFileManager.downloadFile(fullPhotoSize.location.dc_id, { - _: 'inputFileLocation', - volume_id: fullPhotoSize.location.volume_id, - local_id: fullPhotoSize.location.local_id, - secret: fullPhotoSize.location.secret - }, fullPhotoSize.size); - } else { - MtpApiFileManager.downloadSmallFile(fullPhotoSize.location); - } - } - }; - $rootScope.preloadPhoto = preloadPhoto; - function wrapForFull (photoID) { var photo = wrapForHistory(photoID), fullWidth = $(window).width() - 36, @@ -2117,14 +2147,23 @@ angular.module('myApp.services', []) return photo; } - function openPhoto (photoID, messageID) { + function openPhoto (photoID, peerListID) { + if (!photoID || photoID === '0') { + return false; + } + var scope = $rootScope.$new(true); + scope.photoID = photoID; - scope.messageID = messageID; + if (peerListID < 0) { + scope.userID = -peerListID; + } else{ + scope.messageID = peerListID; + } var modalInstance = $modal.open({ templateUrl: 'partials/photo_modal.html', - controller: 'PhotoModalController', + controller: scope.userID ? 'UserpicModalController' : 'PhotoModalController', scope: scope, windowClass: 'photo_modal_window' }); @@ -2195,6 +2234,8 @@ angular.module('myApp.services', []) return { savePhoto: savePhoto, preloadPhoto: preloadPhoto, + getUserPhotos: getUserPhotos, + getPhoto: getPhoto, wrapForHistory: wrapForHistory, wrapForFull: wrapForFull, openPhoto: openPhoto, diff --git a/app/partials/confirm_modal.html b/app/partials/confirm_modal.html index a4eaf2d7..69fe94ca 100644 --- a/app/partials/confirm_modal.html +++ b/app/partials/confirm_modal.html @@ -42,7 +42,7 @@ Cancel -