From d30e7798087be76b9375d3cd489da438f7b7ebcf Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Wed, 30 Sep 2015 22:07:17 +0300 Subject: [PATCH] Added updateChannel handler Added bottom panel --- app/js/controllers.js | 138 +++++++++++++++++++++++++++++-------- app/js/locales/en-us.json | 3 + app/js/messages_manager.js | 80 ++++++++++++++------- app/js/services.js | 6 ++ 4 files changed, 177 insertions(+), 50 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index a5ff2fcb..e4d9160c 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -451,6 +451,11 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.historyPeer = {}; $scope.historyState = { selectActions: false, + botActions: false, + channelActions: false, + actions: function () { + return $scope.historyState.selectActions ? 'selected' : ($scope.historyState.botActions ? 'bot' : ($scope.historyState.channelActions ? 'channel' : false)); + }, typing: [], missedCount: 0, skipped: false @@ -694,13 +699,20 @@ angular.module('myApp.controllers', ['myApp.i18n']) }); - $scope.$on('dialog_flush', function (e, dialog) { + function deleteDialog (peerID) { for (var i = 0; i < $scope.dialogs.length; i++) { - if ($scope.dialogs[i].peerID == dialog.peerID) { + if ($scope.dialogs[i].peerID == peerID) { $scope.dialogs.splice(i, 1); break; } } + } + + $scope.$on('dialog_flush', function (e, dialog) { + deleteDialog(dialog.peerID); + }); + $scope.$on('dialog_drop', function (e, dialog) { + deleteDialog(dialog.peerID); }); $scope.$on('history_delete', function (e, historyUpdate) { @@ -982,7 +994,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) }) - .controller('AppImHistoryController', function ($scope, $location, $timeout, $modal, $rootScope, MtpApiManager, AppUsersManager, AppChatsManager, AppMessagesManager, AppPeersManager, ApiUpdatesManager, PeersSelectService, IdleManager, StatusManager, ErrorService) { + .controller('AppImHistoryController', function ($scope, $location, $timeout, $modal, $rootScope, MtpApiManager, AppUsersManager, AppChatsManager, AppMessagesManager, AppPeersManager, ApiUpdatesManager, PeersSelectService, IdleManager, StatusManager, NotificationsManager, ErrorService) { $scope.$watchCollection('curDialog', applyDialogSelect); @@ -994,6 +1006,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.selectedMsgs = {}; $scope.selectedCount = 0; $scope.historyState.selectActions = false; + $scope.historyState.botActions = false; + $scope.historyState.channelActions = false; $scope.historyState.missedCount = 0; $scope.historyState.skipped = false; $scope.state = {}; @@ -1004,7 +1018,12 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.selectedReply = selectedReply; $scope.selectedCancel = selectedCancel; $scope.selectedFlush = selectedFlush; - $scope.botStart = botStart; + + $scope.startBot = startBot; + $scope.cancelBot = cancelBot; + $scope.joinChannel = joinChannel; + $scope.togglePeerMuted = togglePeerMuted; + $scope.toggleEdit = toggleEdit; $scope.toggleMedia = toggleMedia; @@ -1056,7 +1075,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.curDialog.inputPeer = AppPeersManager.getInputPeer(newPeer); $scope.historyFilter.mediaType = false; - updateStartBot(); + updateBotActions(); selectedCancel(true); if (oldDialog.peer && @@ -1150,14 +1169,14 @@ angular.module('myApp.controllers', ['myApp.i18n']) } } - function updateStartBot () { - var wasStartBot = $scope.historyState.startBot; + function updateBotActions () { + var wasBotActions = $scope.historyState.botActions; if (!peerID || peerID < 0 || !AppUsersManager.isBot(peerID) || $scope.historyFilter.mediaType || $scope.curDialog.messageID) { - $scope.historyState.startBot = false; + $scope.historyState.botActions = false; } else if ( $scope.state.empty || ( @@ -1167,15 +1186,43 @@ angular.module('myApp.controllers', ['myApp.i18n']) peerHistory.messages[0].action._ == 'messageActionBotIntro' ) ) { - $scope.historyState.startBot = 2; + $scope.historyState.botActions = 'start'; } else if ($scope.curDialog.startParam) { - $scope.historyState.startBot = 1; + $scope.historyState.botActions = 'param'; + } + else { + $scope.historyState.botActions = false; + } + if (wasBotActions != $scope.historyState.botActions) { + $scope.$broadcast('ui_panel_update'); + } + } + + function updateChannelActions () { + var wasChannelActions = $scope.historyState.channelActions; + var channel; + if (peerID && + AppPeersManager.isChannel(peerID) && + (channel = AppChatsManager.getChat(-peerID)) && + !channel.pFlags.creator && + !channel.pFlags.editor) { + + if (channel.pFlags.left) { + $scope.historyState.channelActions = 'join'; + } else { + if (!$scope.historyState.channelActions) { + $scope.historyState.channelActions = 'mute'; + } + NotificationsManager.getPeerMuted(peerID).then(function (muted) { + $scope.historyState.channelActions = muted ? 'unmute' : 'mute'; + }); + } } else { - $scope.historyState.startBot = false; + $scope.historyState.channelActions = false; } - if (wasStartBot != $scope.historyState.startBot) { + if (wasChannelActions != $scope.historyState.channelActions) { $scope.$broadcast('ui_panel_update'); } } @@ -1382,7 +1429,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) AppMessagesManager.readHistory($scope.curDialog.inputPeer); - updateStartBot(); + updateBotActions(); + updateChannelActions(); }, function () { safeReplaceObject($scope.state, {error: true}); @@ -1399,13 +1447,32 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.$broadcast('ui_history_change'); } - function botStart () { + function startBot () { AppMessagesManager.startBot(peerID, 0, $scope.curDialog.startParam); $scope.curDialog.startParam = false; } + function cancelBot () { + delete $scope.curDialog.startParam; + } + + function joinChannel () { + MtpApiManager.invokeApi('channels.joinChannel', { + channel: AppChatsManager.getChannelInput(-peerID) + }).then(function (result) { + ApiUpdatesManager.processUpdateMessage(result); + }); + } + + function togglePeerMuted (muted) { + NotificationsManager.getPeerSettings(peerID).then(function (settings) { + settings.mute_until = !muted ? 0 : 2000000000; + NotificationsManager.updatePeerSettings(peerID, settings); + }); + } + function toggleMessage (messageID, $event) { - if ($scope.historyState.startBot || + if ($scope.historyState.botActions || $rootScope.idle.afterFocus) { return false; } @@ -1462,7 +1529,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) break; case 'select': - $scope.historyState.selectActions = true; + $scope.historyState.selectActions = 'selected'; $scope.$broadcast('ui_panel_update'); toggleMessage(messageID); break; @@ -1514,7 +1581,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.selectedMsgs[messageID] = true; $scope.selectedCount++; if (!$scope.historyState.selectActions) { - $scope.historyState.selectActions = true; + $scope.historyState.selectActions = 'selected'; $scope.$broadcast('ui_panel_update'); } } @@ -1522,11 +1589,6 @@ angular.module('myApp.controllers', ['myApp.i18n']) } function selectedCancel (noBroadcast) { - if (!noBroadcast && - $scope.curDialog.startParam) { - delete $scope.curDialog.startParam; - return; - } $scope.selectedMsgs = {}; $scope.selectedCount = 0; $scope.historyState.selectActions = false; @@ -1605,7 +1667,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) if ($scope.historyState.selectActions) { selectedCancel(); } else { - $scope.historyState.selectActions = true; + $scope.historyState.selectActions = 'selected'; $scope.$broadcast('ui_panel_update'); } } @@ -1660,6 +1722,26 @@ angular.module('myApp.controllers', ['myApp.i18n']) } }); + $scope.$on('history_forbidden', function (e, updPeerID) { + if (updPeerID == $scope.curDialog.peerID) { + $rootScope.$apply(function () { + $location.url('/im'); + }) + } + }); + + $scope.$on('notify_settings', function (e, data) { + if (data.peerID == $scope.curDialog.peerID) { + updateChannelActions(); + } + }); + + $scope.$on('channel_settings', function (e, data) { + if (data.channelID == -$scope.curDialog.peerID) { + updateChannelActions(); + } + }); + var typingTimeouts = {}; $scope.$on('history_append', function (e, addedMessage) { var history = historiesQueueFind(addedMessage.peerID); @@ -1719,7 +1801,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) }); } - updateStartBot(); + updateBotActions(); + updateChannelActions(); } }); @@ -1815,7 +1898,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) }); } - updateStartBot(); + updateBotActions(); + updateChannelActions(); } }); @@ -1845,7 +1929,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.$broadcast('messages_regroup'); if (historyUpdate.peerID == $scope.curDialog.peerID) { $scope.state.empty = !newMessages.length; - updateStartBot(); + updateBotActions(); } }); @@ -1856,7 +1940,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) history.ids = []; if (dialog.peerID == $scope.curDialog.peerID) { $scope.state.empty = true; - updateStartBot(); + updateBotActions(); } } }); diff --git a/app/js/locales/en-us.json b/app/js/locales/en-us.json index 4df24d72..45a2c7de 100644 --- a/app/js/locales/en-us.json +++ b/app/js/locales/en-us.json @@ -433,6 +433,9 @@ "im_forward": "Forward {count}", "im_reply": "Reply", "im_start": "Start", + "im_channel_join": "+Join", + "im_channel_mute": "Mute", + "im_channel_unmute": "Unmute", "im_reply_loading": "Loading{dots}", "im_photos_drop_text": "Drop photos here to send", "im_message_field_placeholder": "Write a message...", diff --git a/app/js/messages_manager.js b/app/js/messages_manager.js index ee86ae06..102432d6 100644 --- a/app/js/messages_manager.js +++ b/app/js/messages_manager.js @@ -2448,42 +2448,76 @@ angular.module('myApp.services') }); break; + case 'updateChannel': + var channelID = update.channel_id; + var peerID = -channelID; + var channel = AppChatsManager.getChat(channelID); + + var needDialog = channel._ == 'channel' && (!channel.pFlags.left && !channel.pFlags.kicked); + var foundDialog = getDialogByPeerID(peerID); + var hasDialog = foundDialog.length > 0; + + var canViewHistory = channel._ == 'channel' && (channel.username || !channel.pFlags.left && !channel.pFlags.kicked); + var hasHistory = historiesStorage[peerID] !== undefined; + + if (canViewHistory != hasHistory) { + delete historiesStorage[peerID]; + $rootScope.$broadcast('history_forbidden', peerID); + } + if (hasDialog != needDialog) { + if (needDialog) { + reloadChannelDialog(channelID); + } else { + if (foundDialog[0]) { + dialogsStorage.dialogs.splice(foundDialog[1], 1); + $rootScope.$broadcast('dialog_drop', {peerID: peerID}); + } + } + } + break; + case 'updateChannelReload': var channelID = update.channel_id; var peerID = -channelID; - delete historiesStorage[peerID]; var foundDialog = getDialogByPeerID(peerID); if (foundDialog[0]) { dialogsStorage.dialogs.splice(foundDialog[1], 1); } - - var inputPeer = AppPeersManager.getInputPeerByID(peerID); - $q.all([ - getHistory(inputPeer, 0), - AppChatsManager.getChannelFull(channelID, true) - ]).then(function (results) { - var historyResult = results[0]; - var channelResult = results[1]; - var dialog = { - _: 'dialogChannel', - peer: AppPeersManager.getOutputPeer(peerID), - top_message: historyResult.history[0], - top_important_message: historyResult.history[0], - read_inbox_max_id: channelResult.read_inbox_max_id, - unread_count: channelResult.unread_count, - unread_important_count: channelResult.unread_important_count - }; - saveChannelDialog(channelID, dialog); - - var updatedDialogs = {}; - updatedDialogs[peerID] = dialog; - $rootScope.$broadcast('dialogs_multiupdate', updatedDialogs); + delete historiesStorage[peerID]; + reloadChannelDialog(channelID).then(function () { $rootScope.$broadcast('history_reload', peerID); }); break; } }); + function reloadChannelDialog (channelID) { + var peerID = -channelID; + var inputPeer = AppPeersManager.getInputPeerByID(peerID); + return $q.all([ + AppChatsManager.getChannelFull(channelID, true), + getHistory(inputPeer, 0) + ]).then(function (results) { + var channelResult = results[0]; + var historyResult = results[1]; + var topMsgID = historyResult.history[0]; + var dialog = { + _: 'dialogChannel', + peer: AppPeersManager.getOutputPeer(peerID), + top_message: topMsgID, + top_important_message: topMsgID, + read_inbox_max_id: channelResult.read_inbox_max_id, + unread_count: channelResult.unread_count, + unread_important_count: channelResult.unread_important_count + }; + saveChannelDialog(channelID, dialog); + + var updatedDialogs = {}; + updatedDialogs[peerID] = dialog; + $rootScope.$broadcast('dialogs_multiupdate', updatedDialogs); + }); + } + $rootScope.$on('webpage_updated', function (e, eventData) { angular.forEach(eventData.msgs, function (msgID) { var historyMessage = messagesForHistory[msgID]; diff --git a/app/js/services.js b/app/js/services.js index 7007b585..4757bceb 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -877,6 +877,11 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } } break; + + case 'updateChannel': + var channelID = update.channel_id; + $rootScope.$broadcast('channel_settings', {channelID: channelID}); + break; } }); @@ -2926,6 +2931,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) function savePeerSettings (peerID, settings) { // console.trace(dT(), 'peer settings', peerID, settings); peerSettings[peerID] = $q.when(settings); + $rootScope.$broadcast('notify_settings', {peerID: peerID}); } function updatePeerSettings (peerID, settings) {