Skip to content

Commit 6d1e588

Browse files
committed
api: Use 'direct' instead of 'private' in POST /messages, when supported
Fixes: zulip#5725
1 parent 205f882 commit 6d1e588

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/api/messages/sendMessage.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ import { apiPost } from '../apiFetch';
77
export default async (
88
auth: Auth,
99
params: {|
10-
type: 'private' | 'stream',
10+
type: 'direct' | 'stream',
1111
to: string,
1212
// TODO(server-2.0): Say "topic", not "subject"
1313
subject?: string,
1414
content: string,
1515
localId?: number,
1616
eventQueueId?: string,
1717
|},
18-
): Promise<ApiResponse> =>
19-
apiPost(auth, 'messages', {
20-
type: params.type,
18+
zulipFeatureLevel: number, // TODO(#4659): Don't get this from callers.
19+
): Promise<ApiResponse> => {
20+
let { type } = params;
21+
if (type === 'direct' && zulipFeatureLevel < 174) {
22+
// TODO(server-7.0): Simplify.
23+
type = 'private';
24+
}
25+
return apiPost(auth, 'messages', {
26+
type,
2127
to: params.to,
2228
subject: params.subject,
2329
content: params.content,
2430
local_id: params.localId,
2531
queue_id: params.eventQueueId,
2632
});
33+
};

src/outbox/outboxActions.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { makeUserId } from '../api/idTypes';
3030
import { caseNarrowPartial, isConversationNarrow } from '../utils/narrow';
3131
import { BackoffMachine } from '../utils/async';
3232
import { recipientsOfPrivateMessage, streamNameOfStreamMessage } from '../utils/recipient';
33+
import { getZulipFeatureLevel } from '../account/accountsSelectors';
3334

3435
export const messageSendStart = (outbox: Outbox): PerAccountAction => ({
3536
type: MESSAGE_SEND_START,
@@ -54,6 +55,7 @@ export const messageSendComplete = (localMessageId: number): PerAccountAction =>
5455
const trySendMessages = (dispatch, getState): boolean => {
5556
const state = getState();
5657
const auth = getAuth(state);
58+
const zulipFeatureLevel = getZulipFeatureLevel(state);
5759
const outboxToSend = state.outbox.filter(outbox => !outbox.isSent);
5860
const oneWeekAgoTimestamp = Date.now() / 1000 - 60 * 60 * 24 * 7;
5961
try {
@@ -69,6 +71,8 @@ const trySendMessages = (dispatch, getState): boolean => {
6971
return; // i.e., continue
7072
}
7173

74+
const type = item.type === 'private' ? 'direct' : item.type;
75+
7276
// prettier-ignore
7377
const to =
7478
item.type === 'private'
@@ -79,14 +83,18 @@ const trySendMessages = (dispatch, getState): boolean => {
7983
// CSV, then a literal. To avoid misparsing, always use JSON.
8084
: JSON.stringify([streamNameOfStreamMessage(item)]);
8185

82-
await api.sendMessage(auth, {
83-
type: item.type,
84-
to,
85-
subject: item.subject,
86-
content: item.markdownContent,
87-
localId: item.timestamp,
88-
eventQueueId: state.session.eventQueueId ?? undefined,
89-
});
86+
await api.sendMessage(
87+
auth,
88+
{
89+
type,
90+
to,
91+
subject: item.subject,
92+
content: item.markdownContent,
93+
localId: item.timestamp,
94+
eventQueueId: state.session.eventQueueId ?? undefined,
95+
},
96+
zulipFeatureLevel,
97+
);
9098
dispatch(messageSendComplete(item.timestamp));
9199
});
92100
return true;

src/sharing/ShareWrapper.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ensureUnreachable } from '../generics';
2323
import { IconAttachment, IconCancel } from '../common/Icons';
2424
import type { AppNavigationMethods } from '../nav/AppNavigator';
2525
import { ApiError, RequestError } from '../api/apiErrors';
26+
import { getZulipFeatureLevel } from '../account/accountsSelectors';
2627

2728
type SendTo =
2829
| {| type: 'pm', selectedRecipients: $ReadOnlyArray<UserId> |}
@@ -80,6 +81,7 @@ type OuterProps = $ReadOnly<{|
8081
type SelectorProps = $ReadOnly<{|
8182
auth: Auth,
8283
ownUserId: UserId,
84+
zulipFeatureLevel: number,
8385
|}>;
8486

8587
type Props = $ReadOnly<{|
@@ -124,7 +126,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
124126
*/
125127
handleSend = async () => {
126128
const _ = this.context;
127-
const { auth, sendTo, sharedData, getValidationErrors } = this.props;
129+
const { auth, sendTo, sharedData, getValidationErrors, zulipFeatureLevel } = this.props;
128130
let messageToSend = this.state.message;
129131

130132
const validationErrors = getValidationErrors(messageToSend);
@@ -193,7 +195,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
193195
sendTo.type === 'pm'
194196
? {
195197
content: messageToSend,
196-
type: 'private',
198+
type: 'direct',
197199
to: JSON.stringify(sendTo.selectedRecipients),
198200
}
199201
: {
@@ -206,7 +208,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
206208
};
207209

208210
try {
209-
await api.sendMessage(auth, messageData);
211+
await api.sendMessage(auth, messageData, zulipFeatureLevel);
210212
} catch (err) {
211213
showToast(_('Failed to send message'));
212214
logging.error(err);
@@ -333,6 +335,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
333335
const ShareWrapper: ComponentType<OuterProps> = connect(state => ({
334336
auth: getAuth(state),
335337
ownUserId: getOwnUserId(state),
338+
zulipFeatureLevel: getZulipFeatureLevel(state),
336339
}))(ShareWrapperInner);
337340

338341
export default ShareWrapper;

0 commit comments

Comments
 (0)