Skip to content

Commit 71ee597

Browse files
committed
typing api: Use recipients format based on server version.
Zulip server versions >= 2.0.0-rc1 require user ID arrays in the 'to' argument for the typing endpoint, whereas we currently send email arrays. This change was made in zulip/zulip commit 2f634f8. Check the Zulip version and use the appropriate format of the argument, i.e., for versions >= 2.0.0-rc1 or when the Zulip server version is undetermined [1], use user ID arrays, otherwise, use email arrays. [1] The server started providing its version in 1.5.0-547-gd9b10727fa, in early 2017; it's more likely that we failed to parse a new, exotic version-string format than that someone's actually trying to connect to a Zulip server that doesn't provide one. Fixes #3732.
1 parent 83ec41d commit 71ee597

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/users/usersActions.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* @flow strict-local */
22
import * as typing_status from '@zulip/shared/js/typing_status';
33

4-
import type { Dispatch, GetState, Narrow } from '../types';
4+
import type { Auth, Account, Dispatch, GetState, GlobalState, Narrow } from '../types';
55
import * as api from '../api';
66
import { PRESENCE_RESPONSE } from '../actionConstants';
7-
import { getAuth, tryGetAuth } from '../selectors';
7+
import { getAuth, tryGetAuth, tryGetActiveAccount } from '../selectors';
88
import { isPrivateOrGroupNarrow, caseNarrowPartial } from '../utils/narrow';
9-
import { getAllUsersByEmail } from './userSelectors';
9+
import { getAllUsersByEmail, getUserForId } from './userSelectors';
10+
import { ZulipVersion } from '../utils/zulipVersion';
1011

1112
export const reportPresence = (isActive: boolean = true, newUserInput: boolean = false) => async (
1213
dispatch: Dispatch,
@@ -25,17 +26,42 @@ export const reportPresence = (isActive: boolean = true, newUserInput: boolean =
2526
});
2627
};
2728

28-
const typingWorker = auth => ({
29-
get_current_time: () => new Date().getTime(),
29+
const typingWorker = (state: GlobalState) => {
30+
const auth: Auth = getAuth(state);
3031

31-
notify_server_start: (user_ids_array: number[]) => {
32-
api.typing(auth, JSON.stringify(user_ids_array), 'start');
33-
},
32+
// User ID arrays are only supported in server versions >= 2.0.0-rc1, for
33+
// versions before this, email arrays are used. If current server version is
34+
// undetermined, user ID arrays are optimistically used.
35+
// Support for user ID arrays was added in zulip/zulip commit 2f634f8.
36+
let useEmailArrays: boolean;
37+
const activeAccount: Account | void = tryGetActiveAccount(state);
38+
if (!activeAccount || activeAccount.zulipVersion === undefined) {
39+
useEmailArrays = false;
40+
} else {
41+
useEmailArrays = !new ZulipVersion('activeAccount.zulipVersion').isAtLeast(
42+
new ZulipVersion('2.0.0-rc1'),
43+
);
44+
}
45+
46+
const getRecipients = user_ids_array => {
47+
if (useEmailArrays) {
48+
return JSON.stringify(user_ids_array.map(userId => getUserForId(state, userId).email));
49+
}
50+
return JSON.stringify(user_ids_array);
51+
};
52+
53+
return {
54+
get_current_time: () => new Date().getTime(),
3455

35-
notify_server_stop: (user_ids_array: number[]) => {
36-
api.typing(auth, JSON.stringify(user_ids_array), 'stop');
37-
},
38-
});
56+
notify_server_start: (user_ids_array: number[]) => {
57+
api.typing(auth, getRecipients(user_ids_array), 'start');
58+
},
59+
60+
notify_server_stop: (user_ids_array: number[]) => {
61+
api.typing(auth, getRecipients(user_ids_array), 'stop');
62+
},
63+
};
64+
};
3965

4066
export const sendTypingStart = (narrow: Narrow) => async (
4167
dispatch: Dispatch,
@@ -56,9 +82,7 @@ export const sendTypingStart = (narrow: Narrow) => async (
5682
}
5783
return user.user_id;
5884
});
59-
60-
const auth = getAuth(getState());
61-
typing_status.update(typingWorker(auth), recipientIds);
85+
typing_status.update(typingWorker(getState()), recipientIds);
6286
};
6387

6488
// TODO call this on more than send: blur, navigate away,
@@ -71,6 +95,5 @@ export const sendTypingStop = (narrow: Narrow) => async (
7195
return;
7296
}
7397

74-
const auth = getAuth(getState());
75-
typing_status.update(typingWorker(auth), null);
98+
typing_status.update(typingWorker(getState()), null);
7699
};

0 commit comments

Comments
 (0)