Skip to content

Commit c46625f

Browse files
committed
notifs: Live-update state of whether realm has enabled notifications
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: #5805
1 parent 03e3157 commit c46625f

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

src/account/__tests__/accountsReducer-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ZulipVersion } from '../../utils/zulipVersion';
1515

1616
import * as eg from '../../__tests__/lib/exampleData';
1717
import { identityOfAccount } from '../accountMisc';
18+
import { EventTypes } from '../../api/eventTypes';
1819

1920
describe('accountsReducer', () => {
2021
describe('REGISTER_COMPLETE', () => {
@@ -219,4 +220,69 @@ describe('accountsReducer', () => {
219220
).toEqual(prevState);
220221
});
221222
});
223+
224+
describe('EventTypes.realm, op update_dict', () => {
225+
const stateWithDismissedNotice = [
226+
{ ...eg.plusReduxState.accounts[0], lastDismissedServerPushSetupNotice: new Date() },
227+
];
228+
const stateWithoutDismissedNotice = [
229+
{ ...eg.plusReduxState.accounts[0], lastDismissedServerPushSetupNotice: null },
230+
];
231+
232+
const eventCommon = { id: 0, type: EventTypes.realm, op: 'update_dict', property: 'default' };
233+
234+
test('data.push_notifications_enabled is true, on state with dismissed notice', () => {
235+
expect(
236+
accountsReducer(stateWithDismissedNotice, {
237+
type: EVENT,
238+
event: { ...eventCommon, data: { push_notifications_enabled: true } },
239+
}),
240+
).toEqual(stateWithoutDismissedNotice);
241+
});
242+
243+
test('data.push_notifications_enabled is true, on state without dismissed notice', () => {
244+
expect(
245+
accountsReducer(stateWithoutDismissedNotice, {
246+
type: EVENT,
247+
event: { ...eventCommon, data: { push_notifications_enabled: true } },
248+
}),
249+
).toEqual(stateWithoutDismissedNotice);
250+
});
251+
252+
test('data.push_notifications_enabled is false, on state with dismissed notice', () => {
253+
expect(
254+
accountsReducer(stateWithDismissedNotice, {
255+
type: EVENT,
256+
event: { ...eventCommon, data: { push_notifications_enabled: false } },
257+
}),
258+
).toEqual(stateWithDismissedNotice);
259+
});
260+
261+
test('data.push_notifications_enabled is false, on state without dismissed notice', () => {
262+
expect(
263+
accountsReducer(stateWithoutDismissedNotice, {
264+
type: EVENT,
265+
event: { ...eventCommon, data: { push_notifications_enabled: false } },
266+
}),
267+
).toEqual(stateWithoutDismissedNotice);
268+
});
269+
270+
test('data.push_notifications_enabled is absent, on state with dismissed notice', () => {
271+
expect(
272+
accountsReducer(stateWithDismissedNotice, {
273+
type: EVENT,
274+
event: { ...eventCommon, data: {} },
275+
}),
276+
).toEqual(stateWithDismissedNotice);
277+
});
278+
279+
test('data.push_notifications_enabled is absent, on state without dismissed notice', () => {
280+
expect(
281+
accountsReducer(stateWithoutDismissedNotice, {
282+
type: EVENT,
283+
event: { ...eventCommon, data: {} },
284+
}),
285+
).toEqual(stateWithoutDismissedNotice);
286+
});
287+
});
222288
});

src/account/accountsReducer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ export default (state: AccountsState = initialState, action: Action): AccountsSt
151151
zulipFeatureLevel: zulip_feature_level,
152152
});
153153
}
154+
155+
case EventTypes.realm: {
156+
if (event.op === 'update_dict') {
157+
return updateActiveAccount(state, {
158+
lastDismissedServerPushSetupNotice:
159+
event.data.push_notifications_enabled === true
160+
? null
161+
: state[0].lastDismissedServerPushSetupNotice,
162+
});
163+
}
164+
165+
// (We've converted any `op: 'update'` events to
166+
// `op: 'update_dict'` events near the edge.)
167+
return state;
168+
}
169+
154170
default:
155171
return state;
156172
}

src/realm/__tests__/realmReducer-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ describe('realmReducer', () => {
409409
check(900, 300);
410410
});
411411

412+
describe('pushNotificationsEnabled / push_notifications_enabled', () => {
413+
const check = mkCheck('pushNotificationsEnabled', 'push_notifications_enabled');
414+
check(true, true);
415+
check(true, false);
416+
check(false, true);
417+
check(false, false);
418+
});
419+
412420
describe('create{Private,Public}StreamPolicy / create_stream_policy', () => {
413421
// TODO(server-5.0): Stop expecting create_stream_policy; remove.
414422

src/realm/realmReducer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export default (
258258
if (data.message_content_edit_limit_seconds !== undefined) {
259259
result.messageContentEditLimitSeconds = data.message_content_edit_limit_seconds;
260260
}
261+
if (data.push_notifications_enabled !== undefined) {
262+
result.pushNotificationsEnabled = data.push_notifications_enabled;
263+
}
261264
if (data.create_stream_policy !== undefined) {
262265
// TODO(server-5.0): Stop expecting create_stream_policy; simplify.
263266
result.createPublicStreamPolicy = data.create_stream_policy;

0 commit comments

Comments
 (0)