Skip to content

Commit 8689ab8

Browse files
committed
getMessages [nfc]: Change migrate-messages function to just do one message
1 parent 9f75da8 commit 8689ab8

File tree

2 files changed

+69
-73
lines changed

2 files changed

+69
-73
lines changed
Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @flow strict-local */
2-
import { migrateMessages } from '../getMessages';
2+
import { migrateMessage } from '../getMessages';
33
import { identityOfAuth } from '../../../account/accountMisc';
44
import * as eg from '../../../__tests__/lib/exampleData';
55
import type { ServerMessage, ServerReaction } from '../getMessages';
@@ -38,75 +38,70 @@ const serverMessage: ServerMessage = {
3838
};
3939

4040
describe('recent server', () => {
41-
const input: $ReadOnlyArray<ServerMessage> = [serverMessage];
41+
const input: ServerMessage = serverMessage;
4242

43-
const expectedOutput: $ReadOnlyArray<Message> = [
44-
{
45-
...serverMessage,
46-
reactions: [
47-
{
48-
user_id: reactingUser.user_id,
49-
emoji_name: serverReaction.emoji_name,
50-
reaction_type: serverReaction.reaction_type,
51-
emoji_code: serverReaction.emoji_code,
52-
},
53-
],
54-
avatar_url: GravatarURL.validateAndConstructInstance({ email: serverMessage.sender_email }),
55-
edit_history:
56-
// $FlowIgnore[incompatible-cast] - See MessageEdit type
57-
(serverMessage.edit_history: Message['edit_history']),
58-
},
59-
];
43+
const expectedOutput: Message = {
44+
...serverMessage,
45+
reactions: [
46+
{
47+
user_id: reactingUser.user_id,
48+
emoji_name: serverReaction.emoji_name,
49+
reaction_type: serverReaction.reaction_type,
50+
emoji_code: serverReaction.emoji_code,
51+
},
52+
],
53+
avatar_url: GravatarURL.validateAndConstructInstance({ email: serverMessage.sender_email }),
54+
edit_history:
55+
// $FlowIgnore[incompatible-cast] - See MessageEdit type
56+
(serverMessage.edit_history: Message['edit_history']),
57+
};
6058

61-
const actualOutput: $ReadOnlyArray<Message> = migrateMessages(
59+
const actualOutput: Message = migrateMessage<Message>(
6260
input,
6361
identityOfAuth(eg.selfAuth),
6462
eg.recentZulipFeatureLevel,
6563
true,
6664
);
6765

6866
test('In reactions, replace user object with `user_id`', () => {
69-
expect(actualOutput.map(m => m.reactions)).toEqual(expectedOutput.map(m => m.reactions));
67+
expect(actualOutput.reactions).toEqual(expectedOutput.reactions);
7068
});
7169

7270
test('Converts avatar_url correctly', () => {
73-
expect(actualOutput.map(m => m.avatar_url)).toEqual(expectedOutput.map(m => m.avatar_url));
71+
expect(actualOutput.avatar_url).toEqual(expectedOutput.avatar_url);
7472
});
7573

7674
test('Keeps edit_history, if allowEditHistory is true', () => {
77-
expect(actualOutput.map(m => m.edit_history)).toEqual(expectedOutput.map(m => m.edit_history));
75+
expect(actualOutput.edit_history).toEqual(expectedOutput.edit_history);
7876
});
7977

8078
test('Drops edit_history, if allowEditHistory is false', () => {
8179
expect(
82-
migrateMessages(input, identityOfAuth(eg.selfAuth), eg.recentZulipFeatureLevel, false).map(
83-
m => m.edit_history,
84-
),
85-
).toEqual(expectedOutput.map(m => null));
80+
migrateMessage<Message>(input, identityOfAuth(eg.selfAuth), eg.recentZulipFeatureLevel, false)
81+
.edit_history,
82+
).toEqual(null);
8683
});
8784
});
8885

8986
describe('drops edit_history from pre-118 server', () => {
9087
expect(
91-
migrateMessages(
92-
[
93-
{
94-
...serverMessage,
95-
edit_history: [
96-
{
97-
prev_content: 'foo',
98-
prev_rendered_content: '<p>foo</p>',
99-
prev_stream: eg.stream.stream_id,
100-
prev_subject: 'bar',
101-
timestamp: 0,
102-
user_id: eg.selfUser.user_id,
103-
},
104-
],
105-
},
106-
],
88+
migrateMessage<Message>(
89+
{
90+
...serverMessage,
91+
edit_history: [
92+
{
93+
prev_content: 'foo',
94+
prev_rendered_content: '<p>foo</p>',
95+
prev_stream: eg.stream.stream_id,
96+
prev_subject: 'bar',
97+
timestamp: 0,
98+
user_id: eg.selfUser.user_id,
99+
},
100+
],
101+
},
107102
identityOfAuth(eg.selfAuth),
108103
117,
109104
true,
110-
).map(m => m.edit_history),
111-
).toEqual([null]);
105+
).edit_history,
106+
).toEqual(null);
112107
});

src/api/messages/getMessages.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,43 +77,44 @@ type ServerApiResponseMessages = {|
7777
|};
7878

7979
/** Exported for tests only. */
80-
export const migrateMessages = (
81-
messages: $ReadOnlyArray<ServerMessage>,
80+
export const migrateMessage = <M: Message>(
81+
message: ServerMessageOf<M>,
8282
identity: Identity,
8383
zulipFeatureLevel: number,
8484
allowEditHistory: boolean,
85-
): Message[] =>
86-
messages.map(<M: Message>(message: ServerMessageOf<M>): M => ({
87-
...message,
88-
avatar_url: AvatarURL.fromUserOrBotData({
89-
rawAvatarUrl: message.avatar_url,
90-
email: message.sender_email,
91-
userId: message.sender_id,
92-
realm: identity.realm,
93-
}),
94-
reactions: message.reactions.map(reaction => {
95-
const { user, ...restReaction } = reaction;
96-
return {
97-
...restReaction,
98-
user_id: user.id,
99-
};
100-
}),
85+
): M => ({
86+
...message,
87+
avatar_url: AvatarURL.fromUserOrBotData({
88+
rawAvatarUrl: message.avatar_url,
89+
email: message.sender_email,
90+
userId: message.sender_id,
91+
realm: identity.realm,
92+
}),
93+
reactions: message.reactions.map(reaction => {
94+
const { user, ...restReaction } = reaction;
95+
return {
96+
...restReaction,
97+
user_id: user.id,
98+
};
99+
}),
101100

102-
// Why condition on allowEditHistory? See MessageBase['edit_history'].
103-
// Why FL 118 condition? See MessageEdit type.
104-
edit_history:
105-
/* eslint-disable operator-linebreak */
106-
allowEditHistory && zulipFeatureLevel >= 118
107-
? // $FlowIgnore[incompatible-cast] - See MessageEdit type
108-
(message.edit_history: $ReadOnlyArray<MessageEdit> | void)
109-
: null,
110-
}));
101+
// Why condition on allowEditHistory? See MessageBase['edit_history'].
102+
// Why FL 118 condition? See MessageEdit type.
103+
edit_history:
104+
/* eslint-disable operator-linebreak */
105+
allowEditHistory && zulipFeatureLevel >= 118
106+
? // $FlowIgnore[incompatible-cast] - See MessageEdit type
107+
(message.edit_history: $ReadOnlyArray<MessageEdit> | void)
108+
: null,
109+
});
111110

112111
const migrateResponse = (response, identity: Identity, zulipFeatureLevel, allowEditHistory) => {
113112
const { messages, ...restResponse } = response;
114113
return {
115114
...restResponse,
116-
messages: migrateMessages(messages, identity, zulipFeatureLevel, allowEditHistory),
115+
messages: messages.map(<M: Message>(message: ServerMessageOf<M>): M =>
116+
migrateMessage(message, identity, zulipFeatureLevel, allowEditHistory),
117+
),
117118
};
118119
};
119120

0 commit comments

Comments
 (0)