Skip to content

Commit

Permalink
api: Add getSingleMessage binding for GET messages/{message_id}
Browse files Browse the repository at this point in the history
We'll use this for zulip#5306; see the plan in discussion:
  https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M5306.20Follow.20.2Fnear.2F.20links.20through.20topic.20moves.2Frenames/near/1407930

In particular, we want the stream and topic for a stream message
that might not be in our data structures. We'll use this endpoint to
fetch that information.
  • Loading branch information
chrisbobbe committed Sep 13, 2022
1 parent 04d2c95 commit c35b883
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import deleteMessage from './messages/deleteMessage';
import deleteTopic from './messages/deleteTopic';
import getRawMessageContent from './messages/getRawMessageContent';
import getMessages from './messages/getMessages';
import getSingleMessage from './messages/getSingleMessage';
import getMessageHistory from './messages/getMessageHistory';
import messagesFlags from './messages/messagesFlags';
import sendMessage from './messages/sendMessage';
Expand Down Expand Up @@ -78,6 +79,7 @@ export {
deleteTopic,
getRawMessageContent,
getMessages,
getSingleMessage,
getMessageHistory,
messagesFlags,
sendMessage,
Expand Down
55 changes: 55 additions & 0 deletions src/api/messages/getSingleMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* @flow strict-local */

import type { Auth, ApiResponseSuccess } from '../transportTypes';
import type { Message } from '../apiTypes';
import { transformFetchedMessage, type FetchedMessage } from '../rawModelTypes';
import { apiGet } from '../apiFetch';
import { identityOfAuth } from '../../account/accountMisc';

// The actual response from the server. We convert the message to a proper
// Message before returning it to application code.
type ServerApiResponseSingleMessage = {|
...$Exact<ApiResponseSuccess>,
-raw_content: string, // deprecated

// Until we narrow FetchedMessage into its FL 120+ form, FetchedMessage
// will be a bit less precise than we could be here. That's because we
// only get this field from servers FL 120+.
// TODO(server-5.0): Make this field required, and remove FL-120 comment.
+message?: FetchedMessage,
|};

/**
* See https://zulip.com/api/get-message
*
* Gives undefined if the `message` field is missing, which it will be for
* FL <120.
*/
// TODO(server-5.0): Simplify FL-120 condition in jsdoc and implementation.
export default async (
auth: Auth,
args: {|
+message_id: number,
|},

// TODO(#4659): Don't get this from callers.
zulipFeatureLevel: number,

// TODO(#4659): Don't get this from callers?
allowEditHistory: boolean,
): Promise<Message | void> => {
const { message_id } = args;
const response: ServerApiResponseSingleMessage = await apiGet(auth, `messages/${message_id}`, {
apply_markdown: true,
});

return (
response.message
&& transformFetchedMessage<Message>(
response.message,
identityOfAuth(auth),
zulipFeatureLevel,
allowEditHistory,
)
);
};

0 comments on commit c35b883

Please sign in to comment.