Skip to content

Commit

Permalink
api: Use 'direct' instead of 'private' in POST /messages, when supported
Browse files Browse the repository at this point in the history
Fixes: zulip#5725
  • Loading branch information
chrisbobbe committed Apr 27, 2023
1 parent 205f882 commit 6d1e588
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/api/messages/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ import { apiPost } from '../apiFetch';
export default async (
auth: Auth,
params: {|
type: 'private' | 'stream',
type: 'direct' | 'stream',
to: string,
// TODO(server-2.0): Say "topic", not "subject"
subject?: string,
content: string,
localId?: number,
eventQueueId?: string,
|},
): Promise<ApiResponse> =>
apiPost(auth, 'messages', {
type: params.type,
zulipFeatureLevel: number, // TODO(#4659): Don't get this from callers.
): Promise<ApiResponse> => {
let { type } = params;
if (type === 'direct' && zulipFeatureLevel < 174) {
// TODO(server-7.0): Simplify.
type = 'private';
}
return apiPost(auth, 'messages', {
type,
to: params.to,
subject: params.subject,
content: params.content,
local_id: params.localId,
queue_id: params.eventQueueId,
});
};
24 changes: 16 additions & 8 deletions src/outbox/outboxActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { makeUserId } from '../api/idTypes';
import { caseNarrowPartial, isConversationNarrow } from '../utils/narrow';
import { BackoffMachine } from '../utils/async';
import { recipientsOfPrivateMessage, streamNameOfStreamMessage } from '../utils/recipient';
import { getZulipFeatureLevel } from '../account/accountsSelectors';

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

const type = item.type === 'private' ? 'direct' : item.type;

// prettier-ignore
const to =
item.type === 'private'
Expand All @@ -79,14 +83,18 @@ const trySendMessages = (dispatch, getState): boolean => {
// CSV, then a literal. To avoid misparsing, always use JSON.
: JSON.stringify([streamNameOfStreamMessage(item)]);

await api.sendMessage(auth, {
type: item.type,
to,
subject: item.subject,
content: item.markdownContent,
localId: item.timestamp,
eventQueueId: state.session.eventQueueId ?? undefined,
});
await api.sendMessage(
auth,
{
type,
to,
subject: item.subject,
content: item.markdownContent,
localId: item.timestamp,
eventQueueId: state.session.eventQueueId ?? undefined,
},
zulipFeatureLevel,
);
dispatch(messageSendComplete(item.timestamp));
});
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/sharing/ShareWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ensureUnreachable } from '../generics';
import { IconAttachment, IconCancel } from '../common/Icons';
import type { AppNavigationMethods } from '../nav/AppNavigator';
import { ApiError, RequestError } from '../api/apiErrors';
import { getZulipFeatureLevel } from '../account/accountsSelectors';

type SendTo =
| {| type: 'pm', selectedRecipients: $ReadOnlyArray<UserId> |}
Expand Down Expand Up @@ -80,6 +81,7 @@ type OuterProps = $ReadOnly<{|
type SelectorProps = $ReadOnly<{|
auth: Auth,
ownUserId: UserId,
zulipFeatureLevel: number,
|}>;

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

const validationErrors = getValidationErrors(messageToSend);
Expand Down Expand Up @@ -193,7 +195,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
sendTo.type === 'pm'
? {
content: messageToSend,
type: 'private',
type: 'direct',
to: JSON.stringify(sendTo.selectedRecipients),
}
: {
Expand All @@ -206,7 +208,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
};

try {
await api.sendMessage(auth, messageData);
await api.sendMessage(auth, messageData, zulipFeatureLevel);
} catch (err) {
showToast(_('Failed to send message'));
logging.error(err);
Expand Down Expand Up @@ -333,6 +335,7 @@ class ShareWrapperInner extends React.PureComponent<Props, State> {
const ShareWrapper: ComponentType<OuterProps> = connect(state => ({
auth: getAuth(state),
ownUserId: getOwnUserId(state),
zulipFeatureLevel: getZulipFeatureLevel(state),
}))(ShareWrapperInner);

export default ShareWrapper;

0 comments on commit 6d1e588

Please sign in to comment.