Merge pull request #3000 from dpalou/MOBILE-3695

MOBILE-3695 push: Allow unregister device when site logged out
main
Pau Ferrer Ocaña 2021-11-17 18:01:55 +01:00 committed by GitHub
commit 4fa1d72a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View File

@ -51,6 +51,11 @@ export enum CoreSiteQRCodeType {
QR_CODE_LOGIN = 2, // QR code type login value QR_CODE_LOGIN = 2, // QR code type login value
} }
// WS that we allow to call even if the site is logged out.
const ALLOWED_LOGGEDOUT_WS = [
'core_user_remove_user_device',
];
/** /**
* Class that represents a site (combination of site + user). * Class that represents a site (combination of site + user).
* It will have all the site data and provide utility functions regarding a site. * It will have all the site data and provide utility functions regarding a site.
@ -496,7 +501,7 @@ export class CoreSite {
*/ */
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
async request<T = unknown>(method: string, data: any, preSets: CoreSiteWSPreSets, retrying?: boolean): Promise<T> { async request<T = unknown>(method: string, data: any, preSets: CoreSiteWSPreSets, retrying?: boolean): Promise<T> {
if (this.isLoggedOut()) { if (this.isLoggedOut() && !ALLOWED_LOGGEDOUT_WS.includes(method)) {
// Site is logged out, it cannot call WebServices. // Site is logged out, it cannot call WebServices.
CoreEvents.trigger(CoreEvents.SESSION_EXPIRED, {}, this.id); CoreEvents.trigger(CoreEvents.SESSION_EXPIRED, {}, this.id);

View File

@ -519,20 +519,10 @@ export class CorePushNotificationsProvider {
appid: CoreConstants.CONFIG.app_id, appid: CoreConstants.CONFIG.app_id,
uuid: Device.uuid, uuid: Device.uuid,
}; };
let response: CoreUserRemoveUserDeviceWSResponse;
try { try {
const response = await site.write<CoreUserRemoveUserDeviceWSResponse>('core_user_remove_user_device', data); response = await site.write<CoreUserRemoveUserDeviceWSResponse>('core_user_remove_user_device', data);
if (!response || !response.removed) {
throw new CoreError('Cannot unregister device');
}
await CoreUtils.ignoreErrors(Promise.all([
// Remove the device from the local DB.
site.getDb().deleteRecords(REGISTERED_DEVICES_TABLE_NAME, this.getRegisterData()),
// Remove pending unregisters for this site.
db.deleteRecords(PENDING_UNREGISTER_TABLE_NAME, { siteid: site.getId() }),
]));
} catch (error) { } catch (error) {
if (CoreUtils.isWebServiceError(error)) { if (CoreUtils.isWebServiceError(error)) {
throw error; throw error;
@ -546,7 +536,20 @@ export class CorePushNotificationsProvider {
info: JSON.stringify(site.getInfo()), info: JSON.stringify(site.getInfo()),
}; };
await db.insertRecord(PENDING_UNREGISTER_TABLE_NAME, entry); await db.insertRecord(PENDING_UNREGISTER_TABLE_NAME, entry);
return;
} }
if (!response.removed) {
throw new CoreError('Cannot unregister device');
}
await CoreUtils.ignoreErrors(Promise.all([
// Remove the device from the local DB.
site.getDb().deleteRecords(REGISTERED_DEVICES_TABLE_NAME, this.getRegisterData()),
// Remove pending unregisters for this site.
db.deleteRecords(PENDING_UNREGISTER_TABLE_NAME, { siteid: site.getId() }),
]));
} }
/** /**