mattermost-mobile/app/actions/views/login.js
Anurag Shivarathri 2cd2ddbf5f
MM-33580 Granular Data Retention (#5330)
* Unable to open previews from search, pinned and mentions

* Granular data retention initialised

* Added server version check, logic to remove posts & test cases

* Restructured reducer & runs the job only one time a day

* Updated minimum server version with 5.36

* Reverting client.ts changes

* Added client rest calls & removed config check for data retention enabled or not & fixed test case

* Added check for license

* Update app/actions/views/login.js

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* Update app/store/middlewares/helpers.js

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* Update app/store/middlewares/helpers.js

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* Fixed lint error

* Update app/client/rest/general.ts

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Update app/client/rest/general.ts

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Added redux migration

* Updated server version to 5.37

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2021-07-15 18:47:48 +05:30

110 lines
4.1 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import moment from 'moment-timezone';
import {getDataRetentionPolicy} from '@mm-redux/actions/general';
import {GeneralTypes} from '@mm-redux/action_types';
import {getSessions} from '@mm-redux/actions/users';
import {autoUpdateTimezone} from '@mm-redux/actions/timezone';
import {Client4} from '@client/rest';
import {getConfig, getLicense} from '@mm-redux/selectors/entities/general';
import {isTimezoneEnabled} from '@mm-redux/selectors/entities/timezone';
import {getCurrentUserId} from '@mm-redux/selectors/entities/users';
import {setAppCredentials} from 'app/init/credentials';
import PushNotifications from '@init/push_notifications';
import {getDeviceTimezone} from 'app/utils/timezone';
import {setCSRFFromCookie} from 'app/utils/security';
import {loadConfigAndLicense} from 'app/actions/views/root';
export function handleSuccessfulLogin() {
return async (dispatch, getState) => {
await dispatch(loadConfigAndLicense());
const state = getState();
const license = getLicense(state);
const token = Client4.getToken();
const url = Client4.getUrl();
const deviceToken = state.entities.general.deviceToken;
const currentUserId = getCurrentUserId(state);
await setCSRFFromCookie(url);
setAppCredentials(deviceToken, currentUserId, token, url);
const enableTimezone = isTimezoneEnabled(state);
if (enableTimezone) {
const timezone = getDeviceTimezone();
dispatch(autoUpdateTimezone(timezone));
}
dispatch({
type: GeneralTypes.RECEIVED_APP_CREDENTIALS,
data: {
url,
},
});
if (license?.IsLicensed === 'true' && license?.DataRetention === 'true') {
dispatch(getDataRetentionPolicy());
} else {
dispatch({type: GeneralTypes.RECEIVED_DATA_RETENTION_POLICY, data: {}});
}
return true;
};
}
export function scheduleExpiredNotification(intl) {
return (dispatch, getState) => {
const state = getState();
const {currentUserId} = state.entities.users;
const {deviceToken} = state.entities.general;
const config = getConfig(state);
// Once the user logs in we are going to wait for 10 seconds
// before retrieving the session that belongs to this device
// to ensure that we get the actual session without issues
// then we can schedule the local notification for the session expired
setTimeout(async () => {
let sessions;
try {
sessions = await dispatch(getSessions(currentUserId));
} catch (e) {
console.warn('Failed to get current session', e); // eslint-disable-line no-console
return;
}
if (!Array.isArray(sessions.data)) {
return;
}
const session = sessions.data.find((s) => s.device_id === deviceToken);
const expiresAt = session?.expires_at || 0; //eslint-disable-line camelcase
const expiresInDays = parseInt(Math.ceil(Math.abs(moment.duration(moment().diff(expiresAt)).asDays())), 10);
const message = intl.formatMessage({
id: 'mobile.session_expired',
defaultMessage: 'Session Expired: Please log in to continue receiving notifications. Sessions for {siteName} are configured to expire every {daysCount, number} {daysCount, plural, one {day} other {days}}.',
}, {
siteName: config.SiteName,
daysCount: expiresInDays,
});
if (expiresAt) {
PushNotifications.scheduleNotification({
fireDate: expiresAt,
body: message,
userInfo: {
local: true,
},
});
}
}, 10000);
};
}
export default {
handleSuccessfulLogin,
scheduleExpiredNotification,
};