From 38e13c421dbe82aede7cf9958031943ce50c781b Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 12 Jul 2018 13:31:30 +0200 Subject: [PATCH] MOBILE-2478 login: Fix SSO and notifications errors --- .../notifications/providers/notifications.ts | 10 ++++---- .../emulator/classes/inappbrowserobject.ts | 23 +++++++++++-------- src/core/emulator/providers/inappbrowser.ts | 13 ++++++++--- .../emulator/providers/local-notifications.ts | 6 ++++- src/providers/update-manager.ts | 4 ---- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/addon/notifications/providers/notifications.ts b/src/addon/notifications/providers/notifications.ts index 3fefc469c..6a6c5646b 100644 --- a/src/addon/notifications/providers/notifications.ts +++ b/src/addon/notifications/providers/notifications.ts @@ -58,10 +58,12 @@ export class AddonNotificationsProvider { if (cid && cid[1]) { notification.courseid = cid[1]; } - // Try to get the profile picture of the user. - this.userProvider.getProfile(notification.useridfrom, notification.courseid, true).then((user) => { - notification.profileimageurlfrom = user.profileimageurl; - }); + if (notification.useridfrom > 0) { + // Try to get the profile picture of the user. + this.userProvider.getProfile(notification.useridfrom, notification.courseid, true).then((user) => { + notification.profileimageurlfrom = user.profileimageurl; + }); + } }); } diff --git a/src/core/emulator/classes/inappbrowserobject.ts b/src/core/emulator/classes/inappbrowserobject.ts index b557003f7..d4e199683 100644 --- a/src/core/emulator/classes/inappbrowserobject.ts +++ b/src/core/emulator/classes/inappbrowserobject.ts @@ -16,21 +16,20 @@ import { CoreAppProvider } from '@providers/app'; import { CoreFileProvider } from '@providers/file'; import { CoreUrlUtilsProvider } from '@providers/utils/url'; import { Observable, Observer } from 'rxjs'; -import { InAppBrowserObject, InAppBrowserEvent } from '@ionic-native/in-app-browser'; +import { InAppBrowserEvent } from '@ionic-native/in-app-browser'; /** * Emulates the Cordova InAppBrowserObject in desktop apps. + * We aren't extending InAppBrowserObject because its constructor also opens a window, so we'd end up with 2 windows. */ -export class InAppBrowserObjectMock extends InAppBrowserObject { +export class InAppBrowserObjectMock { protected window; protected browserWindow; protected screen; protected isSSO: boolean; - protected isLinux: boolean; constructor(appProvider: CoreAppProvider, private fileProvider: CoreFileProvider, private urlUtils: CoreUrlUtilsProvider, - private url: string, target?: string, options: string = '') { - super(url, target, options); + private url: string, target?: string, options: string = 'location=yes') { if (!appProvider.isDesktop()) { // This plugin is only supported in desktop. @@ -40,7 +39,6 @@ export class InAppBrowserObjectMock extends InAppBrowserObject { this.browserWindow = require('electron').remote.BrowserWindow; this.screen = require('electron').screen; this.isSSO = !!(url && url.match(/\/launch\.php\?service=.+&passport=/)); - this.isLinux = appProvider.isLinux(); let width = 800, height = 600, @@ -71,7 +69,7 @@ export class InAppBrowserObjectMock extends InAppBrowserObject { this.window = new this.browserWindow(bwOptions); this.window.loadURL(url); - if (this.isLinux && this.isSSO) { + if (this.isSSO) { // SSO in Linux. Simulate it's an iOS device so we can retrieve the launch URL. // This is needed because custom URL scheme is not supported in Linux. const userAgent = 'Mozilla/5.0 (iPad) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60'; @@ -113,11 +111,16 @@ export class InAppBrowserObjectMock extends InAppBrowserObject { * @return {Promise} Promise resolved with the launch URL. */ protected getLaunchUrl(retry: number = 0): Promise { + + if (this.window.isDestroyed()) { + // Window is destroyed, stop. + return Promise.reject(null); + } + return new Promise((resolve, reject): void => { // Execute Javascript to retrieve the launch link. const jsCode = 'var el = document.querySelector("#launchapp"); el && el.href;'; let found = false; - this.window.webContents.executeJavaScript(jsCode).then((launchUrl) => { found = true; resolve(launchUrl); @@ -179,7 +182,7 @@ export class InAppBrowserObjectMock extends InAppBrowserObject { // Helper functions to handle events. const received = (event, url): void => { try { - event.url = url || this.window.getURL(); + event.url = url || (this.window.isDestroyed() ? '' : this.window.getURL()); event.type = name; observer.next(event); } catch (ex) { @@ -203,7 +206,7 @@ export class InAppBrowserObjectMock extends InAppBrowserObject { case 'loadstart': this.window.webContents.on('did-start-loading', received); - if (this.isLinux && this.isSSO) { + if (this.isSSO) { // Linux doesn't support custom URL Schemes. Check if launch page is loaded. this.window.webContents.on('did-finish-load', finishLoad); } diff --git a/src/core/emulator/providers/inappbrowser.ts b/src/core/emulator/providers/inappbrowser.ts index 21c18d3f2..e991e6a90 100644 --- a/src/core/emulator/providers/inappbrowser.ts +++ b/src/core/emulator/providers/inappbrowser.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { InAppBrowser, InAppBrowserObject } from '@ionic-native/in-app-browser'; +import { InAppBrowser } from '@ionic-native/in-app-browser'; import { CoreAppProvider } from '@providers/app'; import { CoreFileProvider } from '@providers/file'; import { CoreUrlUtilsProvider } from '@providers/utils/url'; @@ -36,9 +36,16 @@ export class InAppBrowserMock extends InAppBrowser { * @param {string} url The URL to load. * @param {string} [target] The target in which to load the URL, an optional parameter that defaults to _self. * @param {string} [options] Options for the InAppBrowser. - * @return {InAppBrowserObject} The new instance. + * @return {any} The new instance. */ - create(url: string, target?: string, options: string = ''): InAppBrowserObject { + create(url: string, target?: string, options: string = 'location=yes'): any { + if (options && typeof options !== 'string') { + // Convert to string. + options = Object.keys(options).map((key) => { + return key + '=' + options[key]; + }).join(','); + } + if (!this.appProvider.isDesktop()) { return super.create(url, target, options); } diff --git a/src/core/emulator/providers/local-notifications.ts b/src/core/emulator/providers/local-notifications.ts index 47ba21038..d1685ec80 100644 --- a/src/core/emulator/providers/local-notifications.ts +++ b/src/core/emulator/providers/local-notifications.ts @@ -148,6 +148,10 @@ export class LocalNotificationsMock extends LocalNotifications { * @return {Void} */ protected cancelNotification(id: number, omitEvent: boolean, eventName: string): void { + if (!this.scheduled[id]) { + return; + } + const notification = this.scheduled[id].notification; clearTimeout(this.scheduled[id].timeout); @@ -698,7 +702,7 @@ export class LocalNotificationsMock extends LocalNotifications { id : notification.id, title: notification.title, text: notification.text, - at: notification.at ? notification.at.getTime() : 0, + at: notification.at ? (typeof notification.at == 'object' ? notification.at.getTime() : notification.at) : 0, data: notification.data ? JSON.stringify(notification.data) : '{}', triggered: triggered ? 1 : 0 }; diff --git a/src/providers/update-manager.ts b/src/providers/update-manager.ts index fb451a9f9..b4017eb1f 100644 --- a/src/providers/update-manager.ts +++ b/src/providers/update-manager.ts @@ -105,10 +105,6 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { { name: 'desktop_local_notifications', fields: [ - { - name: 'at', - type: 'date' - }, { name: 'data', type: 'object'