From 70e39704e13132dc31c12cedb5feb9dd992a992a Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 29 Jan 2016 15:20:47 +0300 Subject: [PATCH] Improved inline bots --- app/js/controllers.js | 7 +-- app/js/filters.js | 3 ++ app/js/message_composer.js | 11 ++-- app/js/messages_manager.js | 61 ++-------------------- app/js/services.js | 102 +++++++++++++++++++++++++++++++++++-- app/less/app.less | 10 ++-- 6 files changed, 124 insertions(+), 70 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 2adc6ac4..1347fdba 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -2081,7 +2081,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.$on('user_update', angular.noop); }) - .controller('AppImSendController', function ($scope, $timeout, MtpApiManager, Storage, AppProfileManager, AppChatsManager, AppUsersManager, AppPeersManager, AppDocsManager, AppMessagesManager, MtpApiFileManager, RichTextProcessor) { + .controller('AppImSendController', function ($scope, $timeout, MtpApiManager, Storage, AppProfileManager, AppChatsManager, AppUsersManager, AppPeersManager, AppDocsManager, AppMessagesManager, AppInlineBotsManager, MtpApiFileManager, RichTextProcessor) { $scope.$watch('curDialog.peer', resetDraft); $scope.$on('user_update', angular.noop); @@ -2421,7 +2421,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) prefix: '@' + username + matches[2], placeholder: inlineBot.placeholder }); - AppMessagesManager.getInlineResults(inlineBot.id, matches[3], '').then(function (botResults) { + AppInlineBotsManager.getInlineResults(inlineBot.id, matches[3], '').then(function (botResults) { $scope.$broadcast('inline_results', botResults); delete $scope.draftMessage.inlineProgress; }, function () { @@ -2523,7 +2523,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) var options = { replyToMsgID: $scope.draftMessage.replyToMessage && $scope.draftMessage.replyToMessage.mid }; - AppMessagesManager.sendInlineResult($scope.curDialog.peerID, qID, options); + AppInlineBotsManager.sendInlineResult($scope.curDialog.peerID, qID, options); + fwdsSend(); resetDraft(); delete $scope.draftMessage.sticker; delete $scope.draftMessage.text; diff --git a/app/js/filters.js b/app/js/filters.js index 30869b5d..b840ce32 100644 --- a/app/js/filters.js +++ b/app/js/filters.js @@ -187,6 +187,9 @@ angular.module('myApp.filters', ['myApp.i18n']) .filter('formatSizeProgress', function ($filter, _) { var formatSizeFilter = $filter('formatSize'); return function (progress) { + if (!progress.total) { + return ''; + } var done = formatSizeFilter(progress.done, true), doneParts = done.split(' '), total = formatSizeFilter(progress.total), diff --git a/app/js/message_composer.js b/app/js/message_composer.js index b17d3c5a..cc9a273f 100644 --- a/app/js/message_composer.js +++ b/app/js/message_composer.js @@ -727,13 +727,14 @@ MessageComposer.prototype.setInlinePlaceholder = function (prefix, placeholder) this.inlinePlaceholderPrefix = prefix this.inlinePlaceholderPrefixEl.html(encodeEntities(prefix)); this.inlinePlaceholderEl.html(encodeEntities(placeholder)); + this.onChange(); } MessageComposer.prototype.updateInlinePlaceholder = function () { var prefix = this.inlinePlaceholderPrefix; if (prefix) { var value = this.textareaEl.val(); - this.inlinePlaceholderWrap.toggle(value == prefix); + this.inlinePlaceholderWrap.toggleClass('active', value == prefix); } } @@ -868,7 +869,7 @@ MessageComposer.prototype.onKeyEvent = function (e) { currentSel = $(this.autoCompleteEl).find('li:first'); } currentSel = currentSel.find('a:first'); - var code, mention, command; + var code, mention, command, inlineID; if (code = currentSel.attr('data-code')) { this.onEmojiSelected(code, true); EmojiHelper.pushPopularEmoji(code); @@ -884,7 +885,7 @@ MessageComposer.prototype.onKeyEvent = function (e) { } return cancelEvent(e); } - if (inlineID = target.attr('data-inlineid')) { + if (inlineID = currentSel.attr('data-inlineid')) { if (self.onInlineResultSend) { self.onInlineResultSend(inlineID); } @@ -953,6 +954,9 @@ MessageComposer.prototype.restoreSelection = function () { MessageComposer.prototype.checkAutocomplete = function (forceFull) { + if (this.autocompleteShown && this.autoCompleteScope.type == 'inline') { + return; + } var pos, value; if (this.richTextareaEl) { var textarea = this.richTextareaEl[0]; @@ -1469,6 +1473,7 @@ MessageComposer.prototype.updatePosition = function () { } MessageComposer.prototype.hideSuggestions = function () { + // console.trace(); // return; this.autoCompleteWrapEl.hide(); delete this.autocompleteShown; diff --git a/app/js/messages_manager.js b/app/js/messages_manager.js index 8cab9292..547bedcd 100644 --- a/app/js/messages_manager.js +++ b/app/js/messages_manager.js @@ -1370,7 +1370,6 @@ angular.module('myApp.services') } var apiPromise; if (options.viaBotID) { - console.warn(options); apiPromise = MtpApiManager.invokeApi('messages.sendInlineBotResult', { flags: flags, peer: AppPeersManager.getInputPeerByID(peerID), @@ -1441,31 +1440,6 @@ angular.module('myApp.services') pendingByRandomID[randomIDS] = [peerID, messageID]; }; - function sendInlineResult (peerID, qID, options) { - var inlineResult = inlineResults[qID]; - if (inlineResult === undefined) { - return false; - } - var splitted = qID.split('_'); - var queryID = splitted.shift(); - var resultID = splitted.join('_'); - options = options || {}; - options.viaBotID = inlineResult.botID; - options.queryID = queryID; - options.resultID = resultID; - - if (inlineResult.send_message._ == 'botInlineMessageText') { - options.entities = inlineResult.send_message.entities; - sendText(peerID, inlineResult.send_message.message, options); - } else { - sendOther(peerID, { - _: 'messageMediaPending', - size: 0, - progress: {percent: 33, total: 0} - }, options); - } - } - function sendFile(peerID, file, options) { options = options || {}; var messageID = tempID--, @@ -1682,7 +1656,8 @@ angular.module('myApp.services') case 'inputMediaPhoto': media = { _: 'messageMediaPhoto', - photo: AppPhotosManager.getPhoto(inputMedia.id.id) + photo: AppPhotosManager.getPhoto(inputMedia.id.id), + caption: inputMedia.caption || '' }; break; @@ -1693,7 +1668,8 @@ angular.module('myApp.services') }; media = { _: 'messageMediaDocument', - 'document': doc + 'document': doc, + caption: inputMedia.caption || '' }; break; @@ -1770,7 +1746,6 @@ angular.module('myApp.services') var apiPromise; if (options.viaBotID) { - console.warn(options); apiPromise = MtpApiManager.invokeApi('messages.sendInlineBotResult', { flags: flags, peer: AppPeersManager.getInputPeerByID(peerID), @@ -3073,32 +3048,6 @@ angular.module('myApp.services') }) }); - var inlineResults = {}; - function getInlineResults (botID, query, offset) { - return MtpApiManager.invokeApi('messages.getInlineBotResults', { - bot: AppUsersManager.getUserInput(botID), - query: query, - offset: offset - }).then(function(botResults) { - var queryID = botResults.query_id; - delete botResults._; - delete botResults.flags; - delete botResults.query_id; - angular.forEach(botResults.results, function (result) { - var qID = queryID + '_' + result.id; - result.qID = qID; - result.botID = botID; - - result.rTitle = RichTextProcessor.wrapRichText(result.title, {noLinebreaks: true, noLinks: true}); - result.rDescription = RichTextProcessor.wrapRichText(result.description, {noLinebreaks: true, noLinks: true}); - result.initials = (result.url || result.title || result.type || '').substr(0, 1) - - inlineResults[qID] = result; - }); - return botResults; - }); - } - return { getConversations: getConversations, getHistory: getHistory, @@ -3120,8 +3069,6 @@ angular.module('myApp.services') getMessagePeer: getMessagePeer, getMessageThumb: getMessageThumb, clearDialogCache: clearDialogCache, - getInlineResults: getInlineResults, - sendInlineResult: sendInlineResult, wrapForDialog: wrapForDialog, wrapForHistory: wrapForHistory, wrapReplyMarkup: wrapReplyMarkup, diff --git a/app/js/services.js b/app/js/services.js index 75e879ab..aa5284a2 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -11,7 +11,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) -.service('AppUsersManager', function ($rootScope, $modal, $modalStack, $filter, $q, qSync, MtpApiFileManager, MtpApiManager, RichTextProcessor, ErrorService, Storage, _) { +.service('AppUsersManager', function ($rootScope, $modal, $modalStack, $filter, $q, qSync, MtpApiManager, RichTextProcessor, ErrorService, Storage, _) { var users = {}, usernames = {}, userAccess = {}, @@ -575,7 +575,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) }) -.service('AppChatsManager', function ($q, $rootScope, $modal, _, MtpApiFileManager, MtpApiManager, AppUsersManager, AppPhotosManager, RichTextProcessor) { +.service('AppChatsManager', function ($q, $rootScope, $modal, _, MtpApiManager, AppUsersManager, AppPhotosManager, RichTextProcessor) { var chats = {}, usernames = {}, channelAccess = {}, @@ -2297,7 +2297,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } }) -.service('AppStickersManager', function ($q, $rootScope, $modal, _, FileManager, MtpApiManager, MtpApiFileManager, AppDocsManager, Storage, ApiUpdatesManager) { +.service('AppStickersManager', function ($q, $rootScope, $modal, _, FileManager, MtpApiManager, AppDocsManager, Storage, ApiUpdatesManager) { var started = false; var applied = false; @@ -2594,6 +2594,100 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } }) +.service('AppInlineBotsManager', function (MtpApiManager, AppMessagesManager, AppDocsManager, AppPhotosManager, RichTextProcessor, AppUsersManager) { + + var inlineResults = {}; + + return { + sendInlineResult: sendInlineResult, + getInlineResults: getInlineResults + }; + + function getInlineResults (botID, query, offset) { + return MtpApiManager.invokeApi('messages.getInlineBotResults', { + bot: AppUsersManager.getUserInput(botID), + query: query, + offset: offset + }).then(function(botResults) { + var queryID = botResults.query_id; + delete botResults._; + delete botResults.flags; + delete botResults.query_id; + angular.forEach(botResults.results, function (result) { + var qID = queryID + '_' + result.id; + result.qID = qID; + result.botID = botID; + + result.rTitle = RichTextProcessor.wrapRichText(result.title, {noLinebreaks: true, noLinks: true}); + result.rDescription = RichTextProcessor.wrapRichText(result.description, {noLinebreaks: true, noLinks: true}); + result.initials = (result.url || result.title || result.type || '').substr(0, 1) + + if (result._ == 'botInlineMediaResultDocument') { + AppDocsManager.saveDoc(result.document); + } + else if (result._ == 'botInlineMediaResultPhoto') { + AppPhotosManager.savePhoto(result.photo); + } + + inlineResults[qID] = result; + }); + return botResults; + }); + } + + function sendInlineResult (peerID, qID, options) { + var inlineResult = inlineResults[qID]; + if (inlineResult === undefined) { + return false; + } + var splitted = qID.split('_'); + var queryID = splitted.shift(); + var resultID = splitted.join('_'); + options = options || {}; + options.viaBotID = inlineResult.botID; + options.queryID = queryID; + options.resultID = resultID; + + if (inlineResult.send_message._ == 'botInlineMessageText') { + options.entities = inlineResult.send_message.entities; + AppMessagesManager.sendText(peerID, inlineResult.send_message.message, options); + } else { + var caption = ''; + if (inlineResult.send_message._ == 'botInlineMessageMediaAuto') { + caption = inlineResult.send_message.caption; + } + var inputMedia = false; + if (inlineResult._ == 'botInlineMediaResultDocument') { + var doc = inlineResult.document; + inputMedia = { + _: 'inputMediaDocument', + id: {_: 'inputDocument', id: doc.id, access_hash: doc.access_hash}, + caption: caption + }; + } + else if (inlineResult._ == 'botInlineMediaResultPhoto') { + var photo = inlineResult.photo; + inputMedia = { + _: 'inputMediaPhoto', + id: {_: 'inputPhoto', id: photo.id, access_hash: photo.access_hash}, + caption: caption + }; + } + if (!inputMedia) { + inputMedia = { + _: 'messageMediaPending', + type: inlineResult.type, + file_name: inlineResult.title || inlineResult.content_url || inlineResult.url, + size: 0, + progress: {percent: 30, total: 0} + }; + } + AppMessagesManager.sendOther(peerID, inputMedia, options); + } + } + +}) + .service('ApiUpdatesManager', function ($rootScope, MtpNetworkerFactory, AppUsersManager, AppChatsManager, AppPeersManager, MtpApiManager) { var updatesState = { @@ -4204,4 +4298,4 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) start: start, shareUrl: shareUrl }; -}) +}) \ No newline at end of file diff --git a/app/less/app.less b/app/less/app.less index 2c578ce0..9c7bab0a 100644 --- a/app/less/app.less +++ b/app/less/app.less @@ -2372,15 +2372,19 @@ textarea.im_message_field { } .im_inline_placeholder_wrap { position: absolute; - left: 0; margin-top: 2px; - white-space: nowrap + white-space: nowrap; + pointer-events: none; + display: none; +} +.im_inline_placeholder_wrap.active { + display: block; } .im_inline_placeholder_prefix { visibility: hidden; } .im_inline_placeholder { - color: #999; + color: #9aa2ab; } .icon-online {