|
| 1 | +/* @flow strict-local */ |
| 2 | + |
| 3 | +import type { Auth, ApiResponseSuccess } from '../transportTypes'; |
| 4 | +import type { Message } from '../apiTypes'; |
| 5 | +import { transformFetchedMessage, type FetchedMessage } from '../rawModelTypes'; |
| 6 | +import { apiGet } from '../apiFetch'; |
| 7 | +import { identityOfAuth } from '../../account/accountMisc'; |
| 8 | + |
| 9 | +// The actual response from the server. We convert the message to a proper |
| 10 | +// Message before returning it to application code. |
| 11 | +type ServerApiResponseSingleMessage = {| |
| 12 | + ...$Exact<ApiResponseSuccess>, |
| 13 | + -raw_content: string, // deprecated |
| 14 | + |
| 15 | + // Until we narrow FetchedMessage into its FL 120+ form, FetchedMessage |
| 16 | + // will be a bit less precise than we could be here. That's because we |
| 17 | + // only get this field from servers FL 120+. |
| 18 | + // TODO(server-5.0): Make this field required, and remove FL-120 comment. |
| 19 | + +message?: FetchedMessage, |
| 20 | +|}; |
| 21 | + |
| 22 | +/** |
| 23 | + * See https://zulip.com/api/get-message |
| 24 | + * |
| 25 | + * Gives undefined if the `message` field is missing, which it will be for |
| 26 | + * FL <120. |
| 27 | + */ |
| 28 | +// TODO(server-5.0): Simplify FL-120 condition in jsdoc and implementation. |
| 29 | +export default async ( |
| 30 | + auth: Auth, |
| 31 | + args: {| |
| 32 | + +message_id: number, |
| 33 | + |}, |
| 34 | + |
| 35 | + // TODO(#4659): Don't get this from callers. |
| 36 | + zulipFeatureLevel: number, |
| 37 | + |
| 38 | + // TODO(#4659): Don't get this from callers? |
| 39 | + allowEditHistory: boolean, |
| 40 | +): Promise<Message | void> => { |
| 41 | + const { message_id } = args; |
| 42 | + const response: ServerApiResponseSingleMessage = await apiGet(auth, `messages/${message_id}`, { |
| 43 | + apply_markdown: true, |
| 44 | + }); |
| 45 | + |
| 46 | + return ( |
| 47 | + response.message |
| 48 | + && transformFetchedMessage<Message>( |
| 49 | + response.message, |
| 50 | + identityOfAuth(auth), |
| 51 | + zulipFeatureLevel, |
| 52 | + allowEditHistory, |
| 53 | + ) |
| 54 | + ); |
| 55 | +}; |
0 commit comments