From 00f7229e3d1dd535f24183561df78b2cd365ab4e Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 5 Feb 2016 18:35:11 +0000 Subject: [PATCH] Added recent inline bots on '@' --- app/js/controllers.js | 45 ++++++++++++--- app/js/directives.js | 2 +- app/js/lib/utils.js | 2 + app/js/message_composer.js | 4 ++ app/js/services.js | 57 ++++++++++++++++++- app/less/app.less | 6 +- .../desktop/message_attach_webpage.html | 4 +- 7 files changed, 107 insertions(+), 13 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 70aa3a3a..5901d994 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, AppInlineBotsManager, MtpApiFileManager, RichTextProcessor) { + .controller('AppImSendController', function ($q, $scope, $timeout, MtpApiManager, Storage, AppProfileManager, AppChatsManager, AppUsersManager, AppPeersManager, AppDocsManager, AppMessagesManager, AppInlineBotsManager, MtpApiFileManager, RichTextProcessor) { $scope.$watch('curDialog.peer', resetDraft); $scope.$on('user_update', angular.noop); @@ -2117,6 +2117,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.$broadcast('ui_peer_draft', {focus: true}); }); + $scope.$on('inline_bots_popular', updateMentions); + $scope.replyKeyboardToggle = replyKeyboardToggle; $scope.toggleSlash = toggleSlash; @@ -2163,19 +2165,45 @@ angular.module('myApp.controllers', ['myApp.i18n']) function updateMentions () { var peerID = $scope.curDialog.peerID; - if (!peerID || peerID > 0) { + if (!peerID) { safeReplaceObject($scope.mentions, {}); $scope.$broadcast('mentions_update'); return; } - AppProfileManager.getChatFull(-peerID).then(function (chatFull) { - var participantsVector = (chatFull.participants || {}).participants || []; - var mentionUsers = []; - var mentionIndex = SearchIndexManager.createIndex(); + var mentionUsers = []; + var mentionIndex = SearchIndexManager.createIndex(); + + var inlineBotsPromise = AppInlineBotsManager.getPopularBots().then(function (inlineBots) { + var ids = []; + angular.forEach(inlineBots, function (bot) { + ids.push(bot.id); + }); + return ids; + }); + var chatParticipantsPromise; + if (peerID < 0) { + chatParticipantsPromise = AppProfileManager.getChatFull(-peerID).then(function (chatFull) { + var participantsVector = (chatFull.participants || {}).participants || []; + var ids = []; + angular.forEach(participantsVector, function (participant) { + ids.push(participant.user_id); + }); + return ids; + }); + } else { + chatParticipantsPromise = $q.when([]); + } - angular.forEach(participantsVector, function (participant) { - var user = AppUsersManager.getUser(participant.user_id); + $q.all({pop: inlineBotsPromise, chat: chatParticipantsPromise}).then(function (result) { + var done = {}; + var ids = result.pop.concat(result.chat); + angular.forEach(ids, function (userID) { + if (done[userID]) { + return; + } + done[userID] = true; + var user = AppUsersManager.getUser(userID); if (user.username) { mentionUsers.push(user); SearchIndexManager.indexObject(user.id, AppUsersManager.getUserSearchText(user.id), mentionIndex); @@ -2187,6 +2215,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) index: mentionIndex }); $scope.$broadcast('mentions_update'); + }); } diff --git a/app/js/directives.js b/app/js/directives.js index be395a0f..ca5c6fe9 100755 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -1539,7 +1539,7 @@ angular.module('myApp.directives', ['myApp.filters']) } $scope.$on('inline_results', function (e, inlineResults) { - var w = ((richTextarea || messageField).offsetWidth || 382) - 2; + var w = (messageFieldWrap.offsetWidth || 382) - 2; var h = 80; if (inlineResults) { AppInlineBotsManager.regroupWrappedResults(inlineResults.results, w, h); diff --git a/app/js/lib/utils.js b/app/js/lib/utils.js index a55a83ff..9cc565ce 100644 --- a/app/js/lib/utils.js +++ b/app/js/lib/utils.js @@ -55,6 +55,8 @@ function cancelEvent (event) { if (event.stopPropagation) event.stopPropagation(); if (event.preventDefault) event.preventDefault(); + event.returnValue = false; + event.cancelBubble = true; } return false; diff --git a/app/js/message_composer.js b/app/js/message_composer.js index 9b47249d..76b78388 100644 --- a/app/js/message_composer.js +++ b/app/js/message_composer.js @@ -1465,6 +1465,10 @@ MessageComposer.prototype.showInlineSuggestions = function (botResults) { return; } var self = this; + if (self.autoCompleteScope.type == 'inline' && + self.autoCompleteScope.botResults == botResults) { + return; + } setZeroTimeout(function () { self.autoCompleteScope.$apply(function () { self.autoCompleteScope.type = 'inline'; diff --git a/app/js/services.js b/app/js/services.js index 18f5876c..86d147e1 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -2596,16 +2596,68 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } }) -.service('AppInlineBotsManager', function (MtpApiManager, AppMessagesManager, AppDocsManager, AppPhotosManager, RichTextProcessor, AppUsersManager) { +.service('AppInlineBotsManager', function ($rootScope, Storage, MtpApiManager, AppMessagesManager, AppDocsManager, AppPhotosManager, RichTextProcessor, AppUsersManager) { var inlineResults = {}; return { sendInlineResult: sendInlineResult, regroupWrappedResults: regroupWrappedResults, - getInlineResults: getInlineResults + getInlineResults: getInlineResults, + getPopularBots: getPopularBots }; + function getPopularBots () { + return Storage.get('inline_bots_popular').then(function (bots) { + var result = []; + var i, len, userID; + if (bots && bots.length) { + var now = tsNow(true); + for (i = 0, len = bots.length; i < len; i++) { + if ((now - bots[i][3]) > 14 * 86400) { + continue; + } + userID = bots[i][0]; + if (!AppUsersManager.hasUser(userID)) { + AppUsersManager.saveApiUser(bots[i][1]); + } + result.push({id: userID, rate: bots[i][2], date: bots[i][3]}); + } + }; + return result; + }); + } + + function pushPopularBot (id) { + getPopularBots().then(function (bots) { + var exists = false; + var count = bots.length; + var result = []; + for (var i = 0; i < count; i++) { + if (bots[i].id == id) { + exists = true; + bots[i].rate++; + bots[i].date = tsNow(true); + } + var user = AppUsersManager.getUser(bots[i].id); + result.push([bots[i].id, user, bots[i].rate, bots[i].date]); + } + if (exists) { + result.sort(function (a, b) { + return b[2] - a[2]; + }); + } else { + if (result.length > 15) { + result = result.slice(0, 15); + } + result.push([id, AppUsersManager.getUser(id), 1, tsNow(true)]); + } + ConfigStorage.set({inline_bots_popular: result}); + + $rootScope.$broadcast('inline_bots_popular'); + }); + } + function getInlineResults (botID, query, offset) { return MtpApiManager.invokeApi('messages.getInlineBotResults', { bot: AppUsersManager.getUserInput(botID), @@ -2716,6 +2768,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) if (inlineResult === undefined) { return false; } + pushPopularBot(inlineResult.botID); var splitted = qID.split('_'); var queryID = splitted.shift(); var resultID = splitted.join('_'); diff --git a/app/less/app.less b/app/less/app.less index 882c09f9..30c8e91c 100644 --- a/app/less/app.less +++ b/app/less/app.less @@ -1662,6 +1662,10 @@ div.im_message_video_thumb { .im_message_twitter_embed > blockquote { visibility: hidden; } +.im_message_webpage_gif .img_gif_with_progress_wrap { + margin-top: 5px; + display: block; +} .im_message_gif_wrap { position: relative; @@ -2878,7 +2882,7 @@ a.composer_emoji_btn { border-radius: 0; margin-top: -5px; margin-left: -1px; - width: 180px; + width: 380px; } .composer_dropdown { diff --git a/app/partials/desktop/message_attach_webpage.html b/app/partials/desktop/message_attach_webpage.html index 1c261773..eec02431 100644 --- a/app/partials/desktop/message_attach_webpage.html +++ b/app/partials/desktop/message_attach_webpage.html @@ -1,6 +1,5 @@
-
@@ -35,6 +34,9 @@
+
+ +