From d86b0a01abbb726cb17870803da38592fbbb575e Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Mon, 15 Dec 2014 18:05:38 -0800 Subject: [PATCH] Added telegram.me support --- app/js/controllers.js | 3 +- app/js/lib/mtproto_wrapper.js | 18 +++++++- app/js/services.js | 85 ++++++++++++++++++++++++++++------- 3 files changed, 88 insertions(+), 18 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 09d96ae1..f95a4578 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -309,7 +309,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) LayoutSwitchService.start(); }) - .controller('AppIMController', function ($scope, $location, $routeParams, $modal, $rootScope, $modalStack, MtpApiManager, AppUsersManager, AppChatsManager, AppPeersManager, ContactsSelectService, ChangelogNotifyService, ErrorService, AppRuntimeManager, HttpsMigrateService, LayoutSwitchService) { + .controller('AppIMController', function ($scope, $location, $routeParams, $modal, $rootScope, $modalStack, MtpApiManager, AppUsersManager, AppChatsManager, AppPeersManager, ContactsSelectService, ChangelogNotifyService, ErrorService, AppRuntimeManager, HttpsMigrateService, LayoutSwitchService, LocationParamsService) { $scope.$on('$routeUpdate', updateCurDialog); @@ -459,6 +459,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) ChangelogNotifyService.checkUpdate(); HttpsMigrateService.start(); LayoutSwitchService.start(); + LocationParamsService.start(); }) .controller('AppImDialogsController', function ($scope, $location, $q, $timeout, $routeParams, MtpApiManager, AppUsersManager, AppChatsManager, AppMessagesManager, AppPeersManager, PhonebookContactsService, ErrorService) { diff --git a/app/js/lib/mtproto_wrapper.js b/app/js/lib/mtproto_wrapper.js index 06908685..5632c8ba 100644 --- a/app/js/lib/mtproto_wrapper.js +++ b/app/js/lib/mtproto_wrapper.js @@ -7,12 +7,14 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) -.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, ErrorService, qSync, $q) { +.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, ErrorService, qSync, $q, TelegramMeWebService) { var cachedNetworkers = {}, cachedUploadNetworkers = {}, cachedExportPromise = {}, baseDcID = false; + var telegramMeNotified; + MtpSingleInstanceService.start(); Storage.get('dc').then(function (dcID) { @@ -21,11 +23,19 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) } }); + function telegramMeNotify (newValue) { + if (telegramMeNotified != newValue) { + telegramMeNotified = newValue; + TelegramMeWebService.setAuthorized(telegramMeNotified); + } + } + function mtpSetUserAuth (dcID, userAuth) { Storage.set({ dc: dcID, user_auth: angular.extend({dcID: dcID}, userAuth) }); + telegramMeNotify(true); baseDcID = dcID; } @@ -45,11 +55,13 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) return $q.all(logoutPromises).then(function () { Storage.remove('dc', 'user_auth'); baseDcID = false; + telegramMeNotify(false); }, function (error) { Storage.remove.apply(storageKeys); Storage.remove('dc', 'user_auth'); baseDcID = false; error.handled = true; + telegramMeNotify(false); }); }); } @@ -147,6 +159,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) console.error(dT(), 'Error', error.code, error.type, baseDcID, dcID); if (error.code == 401 && baseDcID == dcID) { Storage.remove('dc', 'user_auth'); + telegramMeNotify(false); rejectPromise(error); } else if (error.code == 401 && baseDcID && dcID != baseDcID) { @@ -210,6 +223,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) function mtpGetUserID () { return Storage.get('user_auth').then(function (auth) { + telegramMeNotify(auth && auth.id > 0 || false); return auth.id || 0; }); } @@ -422,7 +436,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) errorHandler = function (error) { deferred.reject(error); errorHandler = angular.noop; - if (cacheFileWriter && + if (cacheFileWriter && (!error || error.type != 'DOWNLOAD_CANCELED')) { cacheFileWriter.truncate(0); } diff --git a/app/js/services.js b/app/js/services.js index 43e4e7a3..a38e1fe8 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -4103,19 +4103,74 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } }) +.service('TelegramMeWebService', function (Storage) { -// .service('LocationParamsService', function ($routeParams) { - -// var started = false; -// function start () { -// if (started) { -// return; -// } -// started = true; -// navigator.registerProtocolHandler('web+tg', '#im?tgaddr=%s', 'Telegram Web'); -// }; - -// return { -// start: start -// }; -// }) + var disabled = Config.Modes.test || + Config.App.domains.indexOf(location.hostname) == -1 || + location.protocol != 'http:' && location.protocol != 'https:' || + location.protocol == 'https:' && location.hostname != 'web.telegram.org'; + + function sendAsyncRequest (canRedirect) { + canRedirect = false; + if (disabled) { + return false; + } + Storage.get('tgme_sync').then(function (curValue) { + var ts = tsNow(true); + if (curValue && + curValue.canRedirect == canRedirect && + curValue.ts + 86400 > ts) { + return false; + } + Storage.set({tgme_sync: {canRedirect: canRedirect, ts: ts}}); + + var script = $('