From 68c67426a7b9df8c0546faa27564d8a6e424170e Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 4 Mar 2021 15:39:40 -0500 Subject: [PATCH] getMessages types: Handle `Message` being `PmMessage` | `StreamMessage`. In the same basic way as in the previous commit, but with a bit of added complexity: the function isn't `(Message) => Message`; it's `(ServerMessage) => Message`. So, start by expressing how `ServerMessage` relates to `Message`, in a way that applies uniformly to `PmMessage` and `StreamMessage`. Then, annotate the function's param by applying that expression to `M`, which is a subtype of `Message`, and leave the return value as `M`. See discussion at https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.60PmMessage.60.2C.20.60StreamMessage.60.20types.3F/near/1126688. --- src/api/messages/getMessages.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/api/messages/getMessages.js b/src/api/messages/getMessages.js index 03cb198e07b..e366f424ca7 100644 --- a/src/api/messages/getMessages.js +++ b/src/api/messages/getMessages.js @@ -2,7 +2,7 @@ import type { Auth, ApiResponseSuccess } from '../transportTypes'; import type { Identity } from '../../types'; import type { Message, ApiNarrow } from '../apiTypes'; -import type { Reaction, UserId } from '../modelTypes'; +import type { PmMessage, StreamMessage, Reaction, UserId } from '../modelTypes'; import { apiGet } from '../apiFetch'; import { identityOfAuth } from '../../account/accountMisc'; import { AvatarURL } from '../../utils/avatar'; @@ -31,11 +31,15 @@ export type ServerReaction = $ReadOnly<{| |}>, |}>; -export type ServerMessage = $ReadOnly<{| - ...$Exact, +// How `ServerMessage` relates to `Message`, in a way that applies +// uniformly to `Message`'s subtypes. +type ServerMessageOf = {| + ...$Exact, avatar_url: string | null, reactions: $ReadOnlyArray, -|}>; +|}; + +export type ServerMessage = ServerMessageOf | ServerMessageOf; // The actual response from the server. We convert the data from this to // `ApiResponseMessages` before returning it to application code. @@ -49,8 +53,10 @@ export const migrateMessages = ( messages: $ReadOnlyArray, identity: Identity, ): Message[] => - messages.map(message => ({ - ...message, + messages.map((message: ServerMessageOf): M => ({ + // (This casting looks unnecessary, but seems to nudge Flow in the + // right direction.) + ...(message: ServerMessageOf), avatar_url: AvatarURL.fromUserOrBotData({ rawAvatarUrl: message.avatar_url, email: message.sender_email,