Commit 0f5765a5 authored by Harshil Sharma's avatar Harshil Sharma Committed by Elias Nahum
Browse files

Used user TOS data embedded in user object itself (#797)

* Used user TOS data embedded in user object itself

* #MI-372 rmeoved unused user TOS functions and fields

* #MI-372 used original event name

* Fixed a data key name

* #MI-372 fixed a bug causing TOS to be shown on profile update

* #MI-372 removed unused code
parent bc3df042
......@@ -168,7 +168,6 @@ function completeLogin(data: UserProfile): ActionFunc {
dispatch(getMyPreferences()),
dispatch(getMyTeams()),
dispatch(getClientConfig()),
dispatch(getMyTermsOfServiceStatus()),
];
const serverVersion = Client4.getServerVersion();
......@@ -231,10 +230,6 @@ export function loadMe(): ActionFunc {
dispatch(getMyTeamUnreads()),
];
if (config.EnableCustomTermsOfService === 'true') {
promises.push(dispatch(getMyTermsOfServiceStatus()));
}
// Sometimes the server version is set in one or the other
const serverVersion = Client4.getServerVersion() || getState().entities.general.serverVersion;
if (!isMinimumServerVersion(serverVersion, 4, 7) && config.EnableCustomEmoji === 'true') {
......@@ -541,13 +536,6 @@ export function getMe(): ActionFunc {
};
}
export function getMyTermsOfServiceStatus(): ActionFunc {
return bindClientFunc({
clientFunc: Client4.getMyTermsOfServiceStatus,
onSuccess: UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS,
});
}
export function updateMyTermsOfServiceStatus(termsOfServiceId: string, accepted: boolean): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const response: $Subtype<ActionResult> = await dispatch(bindClientFunc({
......@@ -559,14 +547,16 @@ export function updateMyTermsOfServiceStatus(termsOfServiceId: string, accepted:
}));
const {data, error} = response;
if (data) {
dispatch({
type: UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS,
data: {
create_at: new Date().getTime(),
terms_of_service_id: accepted ? termsOfServiceId : null,
user_id: getCurrentUserId(getState()),
},
});
if (accepted) {
dispatch({
type: UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS,
data: {
terms_of_service_create_at: new Date().getTime(),
terms_of_service_id: accepted ? termsOfServiceId : null,
user_id: getCurrentUserId(getState()),
},
});
}
return {data};
}
return {error};
......@@ -1393,7 +1383,6 @@ export default {
switchLdapToEmail,
getTermsOfService,
createTermsOfService,
getMyTermsOfServiceStatus,
updateMyTermsOfServiceStatus,
createUserAccessToken,
getUserAccessToken,
......
......@@ -122,27 +122,6 @@ describe('Actions.Users', () => {
assert.deepEqual(data, response);
});
it('getMyTermsOfServiceStatus', async () => {
const response = {
create_at: 1537880148600,
terms_of_service_id: '123',
user_id: 'abcd',
};
nock(Client4.getUserRoute('me')).
get('/terms_of_service').
reply(200, response);
await Actions.getMyTermsOfServiceStatus()(store.dispatch, store.getState);
const {myAcceptedTermsOfServiceData} = store.getState().entities.users;
assert.ok(myAcceptedTermsOfServiceData.id);
assert.ok(myAcceptedTermsOfServiceData.time);
assert.equal(myAcceptedTermsOfServiceData.id, '123');
assert.equal(myAcceptedTermsOfServiceData.time, 1537880148600);
});
it('updateMyTermsOfServiceStatus accept terms', async () => {
const user = TestHelper.basicUser;
nock(Client4.getUsersRoute()).
......@@ -158,12 +137,13 @@ describe('Actions.Users', () => {
await Actions.updateMyTermsOfServiceStatus(1, true)(store.dispatch, store.getState);
const {currentUserId, myAcceptedTermsOfServiceData} = store.getState().entities.users;
const {currentUserId} = store.getState().entities.users;
const currentUser = store.getState().entities.users.profiles[currentUserId];
assert.ok(currentUserId);
assert.ok(myAcceptedTermsOfServiceData.id);
assert.ok(myAcceptedTermsOfServiceData.time);
assert.equal(myAcceptedTermsOfServiceData.id, 1);
assert.ok(currentUser.terms_of_service_id);
assert.ok(currentUser.terms_of_service_create_at);
assert.equal(currentUser.terms_of_service_id, 1);
});
it('updateMyTermsOfServiceStatus reject terms', async () => {
......
......@@ -444,13 +444,6 @@ export default class Client4 {
);
}
getMyTermsOfServiceStatus = async () => {
return this.doFetch(
`${this.getUserRoute('me')}/terms_of_service`,
{method: 'get'}
);
}
updateMyTermsOfServiceStatus = async (termsOfServiceId, accepted) => {
return this.doFetch(
`${this.getUserRoute('me')}/terms_of_service`,
......
......@@ -87,22 +87,6 @@ function currentUserId(state = '', action) {
return state;
}
function myAcceptedTermsOfServiceData(state = {id: '', time: 0}, action) {
switch (action.type) {
case UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS:
return {
id: action.data.terms_of_service_id,
time: action.data.create_at,
};
case UserTypes.LOGOUT_SUCCESS:
return {id: '', time: 0};
default:
return state;
}
}
function mySessions(state = [], action) {
switch (action.type) {
case UserTypes.RECEIVED_SESSIONS:
......@@ -156,9 +140,16 @@ function profiles(state = {}, action) {
case UserTypes.RECEIVED_ME:
case UserTypes.RECEIVED_PROFILE: {
const data = action.data || action.payload;
const user = {...data};
const oldUser = state[data.id];
if (oldUser) {
user.terms_of_service_id = oldUser.terms_of_service_id;
user.terms_of_service_create_at = oldUser.terms_of_service_create_at;
}
return {
...state,
[data.id]: {...data},
[data.id]: user,
};
}
case UserTypes.RECEIVED_PROFILES_LIST:
......@@ -168,6 +159,17 @@ function profiles(state = {}, action) {
case UserTypes.LOGOUT_SUCCESS:
return {};
case UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS: {
const data = action.data || action.payload;
return {
...state,
[data.user_id]: {
...state[data.user_id],
terms_of_service_id: data.terms_of_service_id,
terms_of_service_create_at: data.terms_of_service_create_at,
},
};
}
default:
return state;
......@@ -397,9 +399,6 @@ export default combineReducers({
// the current selected user
currentUserId,
// the current user's accepted terms of service id and acceptance timestamp
myAcceptedTermsOfServiceData,
// array with the user's sessions
mySessions,
......
......@@ -392,20 +392,14 @@ function removeCurrentUserFromList(profiles: Array<UserProfile>, currentUserId:
}
}
export function getMyAcceptedTermsOfServiceData(state: GlobalState): Object {
return state.entities.users.myAcceptedTermsOfServiceData;
}
export const shouldShowTermsOfService: (GlobalState) => boolean = createSelector(
getConfig,
getCurrentUser,
getLicense,
getMyAcceptedTermsOfServiceData,
(config, user, license, myAcceptedTermsOfServiceData) => {
(config, user, license) => {
// Defaults to false if the user is not logged in or the setting doesn't exist
const acceptedTermsId = myAcceptedTermsOfServiceData.id;
const acceptedAt = myAcceptedTermsOfServiceData.time;
const acceptedTermsId = user ? user.terms_of_service_id : '';
const acceptedAt = user ? user.terms_of_service_create_at : 0;
const featureEnabled = license.IsLicensed === 'true' && config.EnableCustomTermsOfService === 'true';
const reacceptanceTime = config.CustomTermsOfServiceReAcceptancePeriod * 1000 * 60 * 60 * 24;
......
......@@ -491,9 +491,6 @@ describe('Selectors.Users', () => {
},
users: {
currentUserId: userId,
myAcceptedTermsOfServiceData: {
id: '0',
},
profiles: {
[userId]: {id: userId, username: 'user', first_name: 'First', last_name: 'Last'},
},
......@@ -515,9 +512,6 @@ describe('Selectors.Users', () => {
},
users: {
currentUserId: userId,
myAcceptedTermsOfServiceData: {
id: '1',
},
profiles: {
[userId]: {id: userId, username: 'user', first_name: 'First', last_name: 'Last'},
},
......@@ -539,9 +533,6 @@ describe('Selectors.Users', () => {
},
users: {
currentUserId: userId,
myAcceptedTermsOfServiceData: {
id: '1',
},
profiles: {
[userId]: {id: userId, username: 'user', first_name: 'First', last_name: 'Last'},
},
......@@ -563,11 +554,8 @@ describe('Selectors.Users', () => {
},
users: {
currentUserId: userId,
myAcceptedTermsOfServiceData: {
id: '1',
},
profiles: {
[userId]: {id: userId, username: 'user', first_name: 'First', last_name: 'Last'},
[userId]: {id: userId, username: 'user', first_name: 'First', last_name: 'Last', terms_of_service_id: '1', terms_of_service_create_at: new Date().getTime()},
},
},
},
......@@ -587,10 +575,6 @@ describe('Selectors.Users', () => {
},
users: {
currentUserId: userId,
myAcceptedTermsOfServiceData: {
id: '',
time: 0,
},
profiles: {},
},
},
......
......@@ -18,10 +18,6 @@ const state: GlobalState = {
},
users: {
currentUserId: '',
myAcceptedTermsOfServiceData: {
id: '',
time: 0,
},
mySessions: [],
myAudits: [],
profiles: {},
......
......@@ -37,6 +37,8 @@ export type UserProfile = {|
roles: string,
locale: string,
notify_props: UserNotifyProps,
terms_of_service_id: string,
terms_of_service_create_at: number,
timezone?: {|
useAutomaticTimezone: string,
automaticTimezone: string,
......@@ -46,7 +48,6 @@ export type UserProfile = {|
export type UsersState = {|
currentUserId: string,
myAcceptedTermsOfServiceData: Object,
mySessions: Array<Object>,
myAudits: Array<Object>,
profiles: IDMappedObjects<UserProfile>,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment