2018-02-09 10:02:14 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-14 10:32:05 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-02-09 10:02:14 +01:00
|
|
|
import { AddonMessagesProvider } from './messages';
|
2018-03-15 15:24:22 +01:00
|
|
|
import { CoreMainMenuHandler, CoreMainMenuHandlerToDisplay } from '@core/mainmenu/providers/delegate';
|
2018-02-23 13:02:56 +01:00
|
|
|
import { CoreCronHandler } from '@providers/cron';
|
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
|
|
|
import { CoreAppProvider } from '@providers/app';
|
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
2018-02-23 13:02:56 +01:00
|
|
|
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
|
2018-02-23 14:44:17 +01:00
|
|
|
import { AddonPushNotificationsProvider } from '@addon/pushnotifications/providers/pushnotifications';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { AddonPushNotificationsDelegate } from '@addon/pushnotifications/providers/delegate';
|
2018-04-09 11:44:12 +02:00
|
|
|
import { CoreEmulatorHelperProvider } from '@core/emulator/providers/helper';
|
2018-02-09 10:02:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to inject an option into main menu.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
2018-02-22 15:21:22 +01:00
|
|
|
export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCronHandler {
|
2018-02-09 10:02:14 +01:00
|
|
|
name = 'AddonMessages';
|
2018-02-28 17:02:39 +01:00
|
|
|
priority = 800;
|
2018-03-14 11:29:22 +01:00
|
|
|
protected handler: CoreMainMenuHandlerToDisplay = {
|
|
|
|
icon: 'chatbubbles',
|
|
|
|
title: 'addon.messages.messages',
|
|
|
|
page: 'AddonMessagesIndexPage',
|
|
|
|
class: 'addon-messages-handler',
|
|
|
|
showBadge: true, // Do not check isMessageCountEnabled because we'll use fallback it not enabled.
|
|
|
|
badge: '',
|
|
|
|
loading: true
|
|
|
|
};
|
2018-02-09 10:02:14 +01:00
|
|
|
|
2018-12-12 15:40:31 +01:00
|
|
|
protected unreadCount = 0;
|
|
|
|
protected contactRequestsCount = 0;
|
|
|
|
protected orMore = false;
|
2018-12-07 13:30:45 +01:00
|
|
|
|
2018-02-22 15:21:22 +01:00
|
|
|
constructor(private messagesProvider: AddonMessagesProvider, private sitesProvider: CoreSitesProvider,
|
2018-12-12 15:40:31 +01:00
|
|
|
eventsProvider: CoreEventsProvider, private appProvider: CoreAppProvider,
|
2018-02-23 14:44:17 +01:00
|
|
|
private localNotificationsProvider: CoreLocalNotificationsProvider, private textUtils: CoreTextUtilsProvider,
|
2018-03-01 16:55:49 +01:00
|
|
|
private pushNotificationsProvider: AddonPushNotificationsProvider, utils: CoreUtilsProvider,
|
2018-04-09 11:44:12 +02:00
|
|
|
pushNotificationsDelegate: AddonPushNotificationsDelegate, private emulatorHelper: CoreEmulatorHelperProvider) {
|
2018-02-22 15:21:22 +01:00
|
|
|
|
2018-12-12 15:40:31 +01:00
|
|
|
eventsProvider.on(AddonMessagesProvider.UNREAD_CONVERSATION_COUNTS_EVENT, (data) => {
|
|
|
|
this.unreadCount = data.favourites + data.individual + data.group;
|
|
|
|
this.orMore = data.orMore;
|
2018-02-22 15:21:22 +01:00
|
|
|
this.updateBadge(data.siteId);
|
|
|
|
});
|
|
|
|
|
2018-12-07 13:30:45 +01:00
|
|
|
eventsProvider.on(AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, (data) => {
|
2018-12-12 15:40:31 +01:00
|
|
|
this.contactRequestsCount = data.count;
|
|
|
|
this.updateBadge(data.siteId);
|
2018-12-07 13:30:45 +01:00
|
|
|
});
|
|
|
|
|
2018-02-22 15:21:22 +01:00
|
|
|
// Reset info on logout.
|
|
|
|
eventsProvider.on(CoreEventsProvider.LOGOUT, (data) => {
|
2018-12-12 15:40:31 +01:00
|
|
|
this.unreadCount = 0;
|
|
|
|
this.contactRequestsCount = 0;
|
|
|
|
this.orMore = false;
|
2018-03-14 11:29:22 +01:00
|
|
|
this.handler.badge = '';
|
|
|
|
this.handler.loading = true;
|
2018-02-22 15:21:22 +01:00
|
|
|
});
|
2018-03-01 16:55:49 +01:00
|
|
|
|
|
|
|
// If a message push notification is received, refresh the count.
|
2018-03-08 13:25:20 +01:00
|
|
|
pushNotificationsDelegate.on('receive').subscribe((notification) => {
|
2018-03-01 16:55:49 +01:00
|
|
|
// New message received. If it's from current site, refresh the data.
|
|
|
|
if (utils.isFalseOrZero(notification.notif) && this.sitesProvider.isCurrentSite(notification.site)) {
|
2018-12-12 15:40:31 +01:00
|
|
|
this.refreshBadge(notification.site);
|
2018-03-01 16:55:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register Badge counter.
|
2018-03-08 13:25:20 +01:00
|
|
|
pushNotificationsDelegate.registerCounterHandler('AddonMessages');
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
2018-02-09 10:02:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the handler is enabled on a site level.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether or not the handler is enabled on a site level.
|
|
|
|
*/
|
|
|
|
isEnabled(): boolean | Promise<boolean> {
|
|
|
|
return this.messagesProvider.isPluginEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data needed to render the handler.
|
|
|
|
*
|
2018-02-22 15:21:22 +01:00
|
|
|
* @return {CoreMainMenuHandlerToDisplay} Data needed to render the handler.
|
2018-02-09 10:02:14 +01:00
|
|
|
*/
|
2018-02-22 15:21:22 +01:00
|
|
|
getDisplayData(): CoreMainMenuHandlerToDisplay {
|
2018-11-15 10:34:13 +01:00
|
|
|
this.handler.page = this.messagesProvider.isGroupMessagingEnabled() ?
|
|
|
|
'AddonMessagesGroupConversationsPage' : 'AddonMessagesIndexPage';
|
|
|
|
|
2018-03-14 11:29:22 +01:00
|
|
|
if (this.handler.loading) {
|
2018-12-12 15:40:31 +01:00
|
|
|
this.refreshBadge();
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 11:29:22 +01:00
|
|
|
return this.handler;
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-12 15:40:31 +01:00
|
|
|
* Refreshes badge number.
|
2018-02-22 15:21:22 +01:00
|
|
|
*
|
2018-12-07 13:30:45 +01:00
|
|
|
* @param {string} [siteId] Site ID or current Site if undefined.
|
2018-12-12 15:40:31 +01:00
|
|
|
* @param {boolean} [unreadOnly] If true only the unread conversations count is refreshed.
|
|
|
|
* @return {Promise<any>} Resolve when done.
|
2018-02-22 15:21:22 +01:00
|
|
|
*/
|
2018-12-12 15:40:31 +01:00
|
|
|
refreshBadge(siteId?: string, unreadOnly?: boolean): Promise<any> {
|
2018-02-22 15:21:22 +01:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
if (!siteId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:30:45 +01:00
|
|
|
const promises = [];
|
2018-12-12 15:40:31 +01:00
|
|
|
|
|
|
|
promises.push(this.messagesProvider.refreshUnreadConversationCounts(siteId).catch(() => {
|
|
|
|
this.unreadCount = 0;
|
|
|
|
this.orMore = false;
|
2018-12-07 13:30:45 +01:00
|
|
|
}));
|
|
|
|
|
2018-12-12 15:40:31 +01:00
|
|
|
// Refresh the number of contact requests in 3.6+ sites.
|
|
|
|
if (!unreadOnly && this.messagesProvider.isGroupMessagingEnabled()) {
|
|
|
|
promises.push(this.messagesProvider.refreshContactRequestsCount(siteId).catch(() => {
|
|
|
|
this.contactRequestsCount = 0;
|
2018-12-07 13:30:45 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:40:31 +01:00
|
|
|
return Promise.all(promises).finally(() => {
|
|
|
|
this.updateBadge(siteId);
|
2018-03-14 11:29:22 +01:00
|
|
|
this.handler.loading = false;
|
2018-02-22 15:21:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:40:31 +01:00
|
|
|
/**
|
|
|
|
* Update badge number and push notifications counter from loaded data.
|
|
|
|
*
|
|
|
|
* @param {string} siteId Site ID.
|
|
|
|
*/
|
|
|
|
updateBadge(siteId: string): void {
|
|
|
|
const totalCount = this.unreadCount + (this.contactRequestsCount || 0);
|
|
|
|
if (totalCount > 0) {
|
|
|
|
this.handler.badge = totalCount + (this.orMore ? '+' : '');
|
|
|
|
} else {
|
|
|
|
this.handler.badge = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update push notifications badge.
|
|
|
|
this.pushNotificationsProvider.updateAddonCounter('AddonMessages', totalCount, siteId);
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:21:22 +01:00
|
|
|
/**
|
|
|
|
* Execute the process.
|
|
|
|
* Receives the ID of the site affected, undefined for all sites.
|
|
|
|
*
|
|
|
|
* @param {string} [siteId] ID of the site affected, undefined for all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved when done, rejected if failure.
|
|
|
|
*/
|
|
|
|
execute(siteId?: string): Promise<any> {
|
|
|
|
if (this.sitesProvider.isCurrentSite(siteId)) {
|
2018-12-12 15:40:31 +01:00
|
|
|
this.refreshBadge();
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.appProvider.isDesktop() && this.localNotificationsProvider.isAvailable()) {
|
2018-04-09 11:44:12 +02:00
|
|
|
this.emulatorHelper.checkNewNotifications(
|
|
|
|
AddonMessagesProvider.PUSH_SIMULATION_COMPONENT,
|
|
|
|
this.fetchMessages.bind(this), this.getTitleAndText.bind(this), siteId);
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the time between consecutive executions.
|
|
|
|
*
|
|
|
|
* @return {number} Time between consecutive executions (in ms).
|
|
|
|
*/
|
|
|
|
getInterval(): number {
|
|
|
|
return this.appProvider.isDesktop() ? 60000 : 600000; // 1 or 10 minutes.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it's a synchronization process or not.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if is a sync process, false otherwise.
|
|
|
|
*/
|
|
|
|
isSync(): boolean {
|
|
|
|
// This is done to use only wifi if using the fallback function.
|
2018-12-12 15:40:31 +01:00
|
|
|
|
|
|
|
if (this.appProvider.isDesktop()) {
|
|
|
|
// In desktop it is always sync, since it fetches messages to see if there's a new one.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !this.messagesProvider.isMessageCountEnabled() && !this.messagesProvider.isGroupMessagingEnabled();
|
2018-02-22 15:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the process should be executed during a manual sync.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if is a manual sync process, false otherwise.
|
|
|
|
*/
|
|
|
|
canManualSync(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the latest unread received messages from a site.
|
|
|
|
*
|
|
|
|
* @param {string} [siteId] Site ID. Default current.
|
|
|
|
* @return {Promise<any>} Promise resolved with the notifications.
|
|
|
|
*/
|
|
|
|
protected fetchMessages(siteId?: string): Promise<any> {
|
|
|
|
return this.messagesProvider.getUnreadReceivedMessages(true, false, true, siteId).then((response) => {
|
|
|
|
return response.messages;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a message, return the title and the text for the message.
|
|
|
|
*
|
|
|
|
* @param {any} message Message.
|
|
|
|
* @return {Promise<any>} Promise resolved with an object with title and text.
|
|
|
|
*/
|
|
|
|
protected getTitleAndText(message: any): Promise<any> {
|
|
|
|
const data = {
|
|
|
|
title: message.userfromfullname,
|
2018-02-09 10:02:14 +01:00
|
|
|
};
|
2018-02-22 15:21:22 +01:00
|
|
|
|
|
|
|
return this.textUtils.formatText(message.text, true, true).catch(() => {
|
|
|
|
return message.text;
|
|
|
|
}).then((formattedText) => {
|
|
|
|
data['text'] = formattedText;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
2018-02-09 10:02:14 +01:00
|
|
|
}
|
|
|
|
}
|