From 5cd77060fb9f9b29fc266ea245299ef9284fda26 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 5 Nov 2020 11:17:40 +0100 Subject: [PATCH] MOBILE-3523 push: Allow push with siteurl or no site --- .../providers/pushnotifications.ts | 107 +++++++----------- src/providers/sites.ts | 18 +++ 2 files changed, 57 insertions(+), 68 deletions(-) diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts index f5fa633d4..af57a959b 100644 --- a/src/core/pushnotifications/providers/pushnotifications.ts +++ b/src/core/pushnotifications/providers/pushnotifications.ts @@ -457,8 +457,19 @@ export class CorePushNotificationsProvider { */ onMessageReceived(notification: any): void { const data = notification ? notification.additionalData : {}; + let promise; - this.sitesProvider.getSite(data.site).then((site) => { + if (data.site) { + promise = this.sitesProvider.getSite(data.site); + } else if (data.siteurl) { + promise = this.sitesProvider.getSiteByUrl(data.siteurl); + } else { + // Notification not related to any site. + promise = Promise.resolve(); + } + + promise.then((site: CoreSite | undefined) => { + data.site = site && site.getId(); if (typeof data.customdata == 'string') { data.customdata = this.textUtils.parseJSON(data.customdata, {}); @@ -468,80 +479,40 @@ export class CorePushNotificationsProvider { // If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves. if (this.localNotificationsProvider.isAvailable()) { const localNotif: ILocalNotification = { - id: data.notId || 1, - data: data, - title: '', - text: '', - channel: 'PushPluginChannel' - }, - options = { - clean: true, - singleLine: true, - contextLevel: 'system', - instanceId: 0, - filter: true - }, - isAndroid = CoreApp.instance.isAndroid(), - extraFeatures = this.utils.isTrueOrOne(data.extrafeatures); + id: data.notId || 1, + data: data, + title: notification.title, + text: notification.message, + channel: 'PushPluginChannel' + }; + const isAndroid = CoreApp.instance.isAndroid(); + const extraFeatures = this.utils.isTrueOrOne(data.extrafeatures); - // Get the filters to apply to text and message. Don't use FIlterHelper to prevent circular dependencies. - this.filterProvider.canGetFilters(site.getId()).then((canGet) => { - if (!canGet) { - // We cannot check which filters are available, apply them all. - return this.filterDelegate.getEnabledFilters(options.contextLevel, options.instanceId); - } - - return this.filterProvider.getAvailableInContext(options.contextLevel, options.instanceId, site.getId()); - }).catch(() => { - return []; - }).then((filters) => { - const promises = []; - - // Apply formatText to title and message. - promises.push(this.filterProvider.formatText(notification.title, options, filters, site.getId()) - .then((title) => { - localNotif.title = title; - }).catch(() => { - localNotif.title = notification.title; - })); - - promises.push(this.filterProvider.formatText(notification.message, options, filters, site.getId()) - .catch(() => { - // Error formatting, use the original message. - return notification.message; - }).then((formattedMessage) => { - if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) { - // It's a message, use messaging style. Ionic Native doesn't specify this option. - ( localNotif).text = [ - { - message: formattedMessage, - person: data.conversationtype == 2 ? data.userfromfullname : '' - } - ]; - } else { - localNotif.text = formattedMessage; + if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) { + // It's a message, use messaging style. Ionic Native doesn't specify this option. + ( localNotif).text = [ + { + message: notification.message, + person: data.conversationtype == 2 ? data.userfromfullname : '' } - })); + ]; + } - if (extraFeatures && isAndroid) { - // Use a different icon if needed. - localNotif.icon = notification.image; - // This feature isn't supported by the official plugin, we use a fork. - ( localNotif).iconType = data['image-type']; + if (extraFeatures && isAndroid) { + // Use a different icon if needed. + localNotif.icon = notification.image; + // This feature isn't supported by the official plugin, we use a fork. + ( localNotif).iconType = data['image-type']; - localNotif.summary = data.summaryText; + localNotif.summary = data.summaryText; - if (data.picture) { - localNotif.attachments = [data.picture]; - } + if (data.picture) { + localNotif.attachments = [data.picture]; } + } - Promise.all(promises).then(() => { - this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site, - true); - }); - - }); + this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site || '', + true); } // Trigger a notification received event. diff --git a/src/providers/sites.ts b/src/providers/sites.ts index c86dc4f29..1af6ef55e 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -1306,6 +1306,24 @@ export class CoreSitesProvider { } } + /** + * Finds a site with a certain URL. It will return the first site found. + * + * @param siteUrl The site URL. + * @return Promise resolved with the site. + */ + async getSiteByUrl(siteUrl: string): Promise { + await this.dbReady; + + const data = await this.appDB.getRecord(CoreSitesProvider.SITES_TABLE, { siteUrl }); + + if (typeof this.sites[data.id] != 'undefined') { + return this.sites[data.id]; + } + + return this.makeSiteFromSiteListEntry(data); + } + /** * Create a site from an entry of the sites list DB. The new site is added to the list of "cached" sites: this.sites. *