Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Stop assuming fetched messages' reactions have deprecated user field #5773

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions src/api/__tests__/rawModelTypes-test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/* @flow strict-local */
import {
transformFetchedMessage,
type FetchedMessage,
type FetchedReaction,
} from '../rawModelTypes';
import { transformFetchedMessage, type FetchedMessage } from '../rawModelTypes';
import { identityOfAuth } from '../../account/accountMisc';
import * as eg from '../../__tests__/lib/exampleData';
import type { Message } from '../modelTypes';
import { GravatarURL } from '../../utils/avatar';

describe('transformFetchedMessage', () => {
const reactingUser = eg.makeUser();

const serverReaction: FetchedReaction = {
emoji_name: '+1',
reaction_type: 'unicode_emoji',
emoji_code: '1f44d',
user: {
email: reactingUser.email,
full_name: reactingUser.full_name,
id: reactingUser.user_id,
},
};

const fetchedMessage: FetchedMessage = {
...eg.streamMessage(),
reactions: [serverReaction],
reactions: [eg.unicodeEmojiReaction],
avatar_url: null,
edit_history: [
{
Expand All @@ -46,14 +29,6 @@ describe('transformFetchedMessage', () => {

const expectedOutput: Message = {
...fetchedMessage,
reactions: [
{
user_id: reactingUser.user_id,
emoji_name: serverReaction.emoji_name,
reaction_type: serverReaction.reaction_type,
emoji_code: serverReaction.emoji_code,
},
],
avatar_url: GravatarURL.validateAndConstructInstance({ email: fetchedMessage.sender_email }),
edit_history:
// $FlowIgnore[incompatible-cast] - See MessageEdit type
Expand All @@ -67,10 +42,6 @@ describe('transformFetchedMessage', () => {
true,
);

test('In reactions, replace user object with `user_id`', () => {
expect(actualOutput.reactions).toEqual(expectedOutput.reactions);
});

test('Converts avatar_url correctly', () => {
expect(actualOutput.avatar_url).toEqual(expectedOutput.avatar_url);
});
Expand Down
36 changes: 1 addition & 35 deletions src/api/rawModelTypes.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
/* @flow strict-local */

import type { Identity } from '../types';
import type {
PmMessage,
StreamMessage,
Message,
MessageEdit,
Reaction,
UserId,
} from './modelTypes';
import type { PmMessage, StreamMessage, Message, MessageEdit, UserId } from './modelTypes';
import { AvatarURL } from '../utils/avatar';

/**
* The variant of `Reaction` found in a fetch-message(s) response.
*
* Note that reaction events have a *different* variation; see their
* handling in `eventToAction`.
*/
// We shouldn't have to rely on this format on servers at feature
// level 2+; those newer servers include a top-level `user_id` field
// in addition to the `user` object. See #4072.
// TODO(server-3.0): Simplify this away.
export type FetchedReaction = $ReadOnly<{|
...$Diff<Reaction, {| user_id: mixed |}>,
user: $ReadOnly<{|
email: string,
full_name: string,
id: UserId,
|}>,
|}>;

/**
* The elements of Message.edit_history found in a fetch-message(s) response.
*
Expand All @@ -56,7 +30,6 @@ export type FetchedMessageEdit = $ReadOnly<{|
type FetchedMessageOf<M: Message> = $ReadOnly<{|
...$Exact<M>,
avatar_url: string | null,
reactions: $ReadOnlyArray<FetchedReaction>,

// Unlike Message['edit_history'], this can't be `null`.
edit_history?: $ReadOnlyArray<FetchedMessageEdit>,
Expand All @@ -80,13 +53,6 @@ export const transformFetchedMessage = <M: Message>(
userId: message.sender_id,
realm: identity.realm,
}),
reactions: message.reactions.map(reaction => {
const { user, ...restReaction } = reaction;
return {
...restReaction,
user_id: user.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof. Yeah, what we should have done long ago would be to make this say user.id ?? restReaction.user_id (or perhaps, equivalently, user.id ?? user_id after saying const { user_id, user, ...restReaction } above… or perhaps better yet, write the fallback the other way around with modern first, like user_id ?? user.id).

Then it would have already been forward-compatible with an eventual server version that dropped the old user property, without really being materially more complicated.

};
}),

// Why condition on allowEditHistory? See MessageBase['edit_history'].
// Why FL 118 condition? See MessageEdit type.
Expand Down
35 changes: 13 additions & 22 deletions src/message/__tests__/fetchActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,48 +340,39 @@ describe('fetchActions', () => {
});

test("rejects when validation-at-the-edge can't handle data, dispatches MESSAGE_FETCH_ERROR", async () => {
// Regression test for #4156. There was a server bug that caused
// message data to be malformed, and though it was only briefly in
// main, a user did run into it in real life:
// https://github.com/zulip/zulip-mobile/issues/4156#issuecomment-655905093
//
// This validation is done in transformFetchedMessages in
// rawModelTypes.
//
// Simulate #4156, a real-life problem that a user at server commit
// 0af2f9d838 ran into [1], by having `user` be missing on reactions
// on a message:
// https://github.com/zulip/zulip-mobile/issues/4156#issuecomment-655905093
const store = mockStore<GlobalState, Action>(baseState);

// Missing `user` (and `user_id`, for good measure), to
// simulate #4156.
const faultyReaction = {
reaction_type: 'unicode_emoji',
emoji_code: '1f44d',
emoji_name: 'thumbs_up',
};
const store = mockStore<GlobalState, Action>(baseState);

const response = {
// Flow would complain at `faultyReaction` if it
// Flow would complain at `sender_email` if it
// type-checked `response`, but we should ignore it if that
// day comes. It's badly shaped on purpose.
messages: [{ ...fetchedMessage1, reactions: [faultyReaction] }],
// day comes. It's badly typed on purpose.
messages: [{ ...fetchedMessage1, avatar_url: null, sender_email: undefined }],
result: 'success',
};
// $FlowFixMe[prop-missing]: See mock in jest/globalFetch.js.
fetch.mockResponseSuccess(JSON.stringify(response));

const expectedError = Error("Cannot read properties of undefined (reading 'toLowerCase')");

await expect(
store.dispatch(
fetchMessages({ narrow: HOME_NARROW, anchor: 0, numBefore: 1, numAfter: 1 }),
),
).rejects.toThrow(
// Update this with changes to the error type.
TypeError,
);
).rejects.toThrow(expectedError);

const actions = store.getActions();

expect(actions[actions.length - 1]).toMatchObject({
type: 'MESSAGE_FETCH_ERROR',
// Update this with changes to the error type.
error: expect.any(TypeError),
error: expectedError,
});
});

Expand Down
Loading