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: #5805
  • Loading branch information
chrisbobbe committed Jan 4, 2024
1 parent b20205a commit cbdf900
Show file tree
Hide file tree
Showing 4 changed files with 93 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);
});
});
});
16 changes: 16 additions & 0 deletions src/account/accountsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ 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 @@ -409,6 +409,14 @@ describe('realmReducer', () => {
check(900, 300);
});

describe('pushNotificationsEnabled / push_notifications_enabled', () => {
const check = mkCheck('pushNotificationsEnabled', 'push_notifications_enabled');
check(true, true);
check(true, false);
check(false, true);
check(false, false);
});

describe('create{Private,Public}StreamPolicy / create_stream_policy', () => {
// TODO(server-5.0): Stop expecting create_stream_policy; remove.

Expand Down
3 changes: 3 additions & 0 deletions src/realm/realmReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ export default (
if (data.message_content_edit_limit_seconds !== undefined) {
result.messageContentEditLimitSeconds = data.message_content_edit_limit_seconds;
}
if (data.push_notifications_enabled !== undefined) {
result.pushNotificationsEnabled = data.push_notifications_enabled;
}
if (data.create_stream_policy !== undefined) {
// TODO(server-5.0): Stop expecting create_stream_policy; simplify.
result.createPublicStreamPolicy = data.create_stream_policy;
Expand Down

0 comments on commit cbdf900

Please sign in to comment.