Skip to content

Commit

Permalink
ios notif: Require realm_uri in payload, present since server 1.9.
Browse files Browse the repository at this point in the history
This release has been out for nearly three years (since 2018-11-07),
so it's long past time for anyone running an older version to upgrade.
We decided earlier this year to stop supporting older than 2.1.x; and
since the mobile release v27.165 of a couple of months ago, anyone
using a server older than 2.0.x has gotten a warning banner about it.

As of this commit, our post-parse `Notification` type still sees
`realm_uri` as optional.  We'll tighten that after making it similarly
required in the Android-native parsing code.
  • Loading branch information
gnprice committed Sep 13, 2021
1 parent 2bd7794 commit 144dbb1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
45 changes: 26 additions & 19 deletions src/notification/__tests__/notification-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import * as eg from '../../__tests__/lib/exampleData';
import { fromAPNsImpl as extractIosNotificationData } from '../extract';
import objectEntries from '../../utils/objectEntries';

const realm_uri = eg.realm.toString();

describe('getNarrowFromNotificationData', () => {
const ownUserId = eg.selfUser.user_id;

Expand Down Expand Up @@ -61,9 +63,9 @@ describe('getNarrowFromNotificationData', () => {

describe('extract iOS notification data', () => {
const barebones = deepFreeze({
stream: { recipient_type: 'stream', stream: 'announce', topic: 'New channel' },
'1:1 PM': { recipient_type: 'private', sender_email: 'nobody@example.com' },
'group PM': { recipient_type: 'private', pm_users: '54,321' },
stream: { recipient_type: 'stream', stream: 'announce', topic: 'New channel', realm_uri },
'1:1 PM': { recipient_type: 'private', sender_email: 'nobody@example.com', realm_uri },
'group PM': { recipient_type: 'private', pm_users: '54,321', realm_uri },
});

describe('success', () => {
Expand All @@ -72,7 +74,7 @@ describe('extract iOS notification data', () => {

for (const [type, data] of objectEntries(barebones)) {
test(`${type} notification`, () => {
// barebones 1.8.0-style message is accepted
// barebones 1.9.0-style message is accepted
const msg = data;
expect(verify(msg)).toEqual(msg);

Expand All @@ -83,10 +85,6 @@ describe('extract iOS notification data', () => {
// unknown fields are ignored and not copied
const msg2a = { ...msg, unknown_data: ['unknown_data'] };
expect(verify(msg2a)).toEqual(msg);

// realm_uri is copied if present
const msg3 = { ...msg, realm_uri: 'https://zulip.example.org' };
expect(verify(msg3)).toEqual(msg3);
});
}
});
Expand All @@ -106,24 +104,33 @@ describe('extract iOS notification data', () => {
test('very-old-style messages', () => {
const sender_email = 'nobody@example.com';
// baseline
expect(make({ recipient_type: 'private', sender_email })).toBeTruthy();
expect(make({ realm_uri, recipient_type: 'private', sender_email })).toBeTruthy();
// missing recipient_type
expect(make({ sender_email })).toThrow(/archaic/);
expect(make({ realm_uri, sender_email })).toThrow(/archaic/);
// missing realm_uri
expect(make({ recipient_type: 'private', sender_email })).toThrow(/archaic/);
});

test('broken or partial messages', () => {
expect(make({ recipient_type: 'huddle' })).toThrow(/invalid/);
expect(make({ recipient_type: 'stream' })).toThrow(/invalid/);
expect(make({ recipient_type: 'stream', stream: 'stream name' })).toThrow(/invalid/);
expect(make({ recipient_type: 'stream', subject: 'topic' })).toThrow(/invalid/);
expect(make({ recipient_type: 'private', subject: 'topic' })).toThrow(/invalid/);
expect(make({ realm_uri, recipient_type: 'huddle' })).toThrow(/invalid/);
expect(make({ realm_uri, recipient_type: 'stream' })).toThrow(/invalid/);
expect(make({ realm_uri, recipient_type: 'stream', stream: 'stream name' })).toThrow(
/invalid/,
);
expect(make({ realm_uri, recipient_type: 'stream', subject: 'topic' })).toThrow(/invalid/);
expect(make({ realm_uri, recipient_type: 'private', subject: 'topic' })).toThrow(/invalid/);
});

test('values of incorrect type', () => {
expect(make({ recipient_type: 'private', pm_users: [1, 2, 3] })).toThrow(/invalid/);
expect(make({ recipient_type: 'stream', stream: [], topic: 'yes' })).toThrow(/invalid/);
expect(make({ realm_uri, recipient_type: 'private', pm_users: [1, 2, 3] })).toThrow(
/invalid/,
);
expect(make({ realm_uri, recipient_type: 'stream', stream: [], topic: 'yes' })).toThrow(
/invalid/,
);
expect(
make({
realm_uri,
recipient_type: 'stream',
stream: { name: 'somewhere' },
topic: 'no',
Expand All @@ -134,15 +141,15 @@ describe('extract iOS notification data', () => {
test('optional data is typechecked', () => {
expect(
make({
realm_uri: null,
...barebones.stream,
realm_uri: null,
}),
).toThrow(/invalid/);

expect(
make({
realm_uri: ['array', 'of', 'string'],
...barebones['group PM'],
realm_uri: ['array', 'of', 'string'],
}),
).toThrow(/invalid/);
});
Expand Down
12 changes: 7 additions & 5 deletions src/notification/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ export const fromAPNsImpl = (rawData: ?JSONableDict): Notification | void => {
}

const { realm_uri } = zulip;
if (realm_uri !== undefined && typeof realm_uri !== 'string') {
if (realm_uri === undefined) {
throw err('archaic (pre-1.9.x)');
}
if (typeof realm_uri !== 'string') {
throw err('invalid');
}
const realm_uri_obj = Object.freeze(realm_uri === undefined ? {} : { realm_uri });

if (recipient_type === 'stream') {
const { stream, topic } = zulip;
Expand All @@ -182,7 +184,7 @@ export const fromAPNsImpl = (rawData: ?JSONableDict): Notification | void => {
recipient_type: 'stream',
stream,
topic,
...realm_uri_obj,
realm_uri,
};
} else {
/* recipient_type === 'private' */
Expand All @@ -199,14 +201,14 @@ export const fromAPNsImpl = (rawData: ?JSONableDict): Notification | void => {
return {
recipient_type: 'private',
pm_users: ids.sort((a, b) => a - b).join(','),
...realm_uri_obj,
realm_uri,
};
}

if (typeof sender_email !== 'string') {
throw err('invalid');
}
return { recipient_type: 'private', sender_email, ...realm_uri_obj };
return { recipient_type: 'private', sender_email, realm_uri };
}

/* unreachable */
Expand Down

0 comments on commit 144dbb1

Please sign in to comment.