Skip to content

Commit

Permalink
notifs: Live-update state of whether realm has enabled notifications
Browse files Browse the repository at this point in the history
Making sure that when the realm goes from disabling to enabling
notifications, we clear out the state of whether the user has
dismissed the banner on the home screen -- just like we do when the
/register response informs us of this change. That's so that if
notifications become disabled again, a new banner will be shown.

Fixes: zulip#5805
  • Loading branch information
chrisbobbe committed Dec 20, 2023
1 parent a2ba056 commit 4152ab7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/account/__tests__/accountsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ZulipVersion } from '../../utils/zulipVersion';

import * as eg from '../../__tests__/lib/exampleData';
import { identityOfAccount } from '../accountMisc';
import { EventTypes } from '../../api/eventTypes';

describe('accountsReducer', () => {
describe('REGISTER_COMPLETE', () => {
Expand Down Expand Up @@ -219,4 +220,69 @@ describe('accountsReducer', () => {
).toEqual(prevState);
});
});

describe('EventTypes.realm, op update_dict', () => {
const stateWithDismissedNotice = [
{ ...eg.plusReduxState.accounts[0], lastDismissedServerPushSetupNotice: new Date() },
];
const stateWithoutDismissedNotice = [
{ ...eg.plusReduxState.accounts[0], lastDismissedServerPushSetupNotice: null },
];

const eventCommon = { id: 0, type: EventTypes.realm, op: 'update_dict', property: 'default' };

test('data.push_notifications_enabled is true, on state with dismissed notice', () => {
expect(
accountsReducer(stateWithDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: { push_notifications_enabled: true } },
}),
).toEqual(stateWithoutDismissedNotice);
});

test('data.push_notifications_enabled is true, on state without dismissed notice', () => {
expect(
accountsReducer(stateWithoutDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: { push_notifications_enabled: true } },
}),
).toEqual(stateWithoutDismissedNotice);
});

test('data.push_notifications_enabled is false, on state with dismissed notice', () => {
expect(
accountsReducer(stateWithDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: { push_notifications_enabled: false } },
}),
).toEqual(stateWithDismissedNotice);
});

test('data.push_notifications_enabled is false, on state without dismissed notice', () => {
expect(
accountsReducer(stateWithoutDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: { push_notifications_enabled: false } },
}),
).toEqual(stateWithoutDismissedNotice);
});

test('data.push_notifications_enabled is absent, on state with dismissed notice', () => {
expect(
accountsReducer(stateWithDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: {} },
}),
).toEqual(stateWithDismissedNotice);
});

test('data.push_notifications_enabled is absent, on state without dismissed notice', () => {
expect(
accountsReducer(stateWithoutDismissedNotice, {
type: EVENT,
event: { ...eventCommon, data: {} },
}),
).toEqual(stateWithoutDismissedNotice);
});
});
});
14 changes: 14 additions & 0 deletions src/account/accountsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ export default (state: AccountsState = initialState, action: Action): AccountsSt
zulipFeatureLevel: zulip_feature_level,
});
}
case EventTypes.realm: {
if (event.op === 'update_dict') {
return updateActiveAccount(state, {
lastDismissedServerPushSetupNotice:
event.data.push_notifications_enabled === true
? null
: state[0].lastDismissedServerPushSetupNotice,
});
}

// (We've converted any `op: 'update'` events to
// `op: 'update_dict'` events near the edge.)
return state;
}
default:
return state;
}
Expand Down
8 changes: 8 additions & 0 deletions src/realm/__tests__/realmReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ describe('realmReducer', () => {
check(Moderators, Members);
check(Moderators, Nobody);
});

describe('pushNotificationsEnabled / push_notifications_enabled', () => {
const check = mkCheck('pushNotificationsEnabled', 'push_notifications_enabled');
check(true, true);
check(true, false);
check(false, true);
check(false, false);
});
});
});
});
3 changes: 3 additions & 0 deletions src/realm/realmReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ export default (
if (data.email_address_visibility !== undefined) {
result.emailAddressVisibility = data.email_address_visibility;
}
if (data.push_notifications_enabled !== undefined) {
result.pushNotificationsEnabled = data.push_notifications_enabled;
}

return result;
}
Expand Down

0 comments on commit 4152ab7

Please sign in to comment.