Skip to content

Commit 0af4af2

Browse files
committed
api: Make getMessages return full response
We use often use response processing functions to extract the meaningful data out of a response. Previously we only cared about the `messages` property. Since the adding of `found_newest` and `found_oldest` fields we now care about the whole response. This reworks the `getMessages` to return the whole response and any call to the function to use the `messages` property correctly.
1 parent 233d68c commit 0af4af2

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/api/messages/getMessages.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
/* @flow strict-local */
2-
import type { Auth, Message, Narrow } from '../apiTypes';
2+
import type { Auth, ApiResponseSuccess, Message, Narrow } from '../apiTypes';
33
import { apiGet } from '../apiFetch';
44

5+
type ApiResponseMessages = {|
6+
...ApiResponseSuccess,
7+
anchor: number,
8+
found_anchor: boolean,
9+
found_newest: boolean,
10+
found_oldest: boolean,
11+
messages: Message[],
12+
|};
13+
14+
/** See https://zulipchat.com/api/get-messages */
515
export default async (
616
auth: Auth,
717
narrow: Narrow,
818
anchor: number,
919
numBefore: number,
1020
numAfter: number,
1121
useFirstUnread: boolean = false,
12-
): Promise<Message[]> =>
13-
apiGet(auth, 'messages', res => res.messages, {
22+
): Promise<ApiResponseMessages> =>
23+
apiGet(auth, 'messages', (res: ApiResponseMessages): ApiResponseMessages => res, {
1424
narrow: JSON.stringify(narrow),
1525
anchor,
1626
num_before: numBefore,

src/message/fetchActions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const fetchMessages = (
7474
useFirstUnread: boolean = false,
7575
) => async (dispatch: Dispatch, getState: GetState) => {
7676
dispatch(messageFetchStart(narrow, numBefore, numAfter));
77-
const messages = await getMessages(
77+
const { messages } = await getMessages(
7878
getAuth(getState()),
7979
narrow,
8080
anchor,
@@ -183,7 +183,7 @@ export const fetchRestOfInitialData = () => async (dispatch: Dispatch, getState:
183183
const auth = getAuth(getState());
184184

185185
timing.start('Rest of server data');
186-
const [messages, streams] = await Promise.all([
186+
const [{ messages }, streams] = await Promise.all([
187187
await tryUntilSuccessful(() =>
188188
getMessages(auth, ALL_PRIVATE_NARROW, LAST_MESSAGE_ANCHOR, 100, 0),
189189
),

src/search/SearchMessagesCard.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SearchMessagesCard extends PureComponent<Props, State> {
4343

4444
throttle(async () => {
4545
this.setState({ isFetching: true });
46-
const messages = await getMessages(
46+
const { messages } = await getMessages(
4747
auth,
4848
SEARCH_NARROW(query),
4949
LAST_MESSAGE_ANCHOR,

0 commit comments

Comments
 (0)