Skip to content

Commit

Permalink
getMessages [nfc]: Plumb identity down to migrateMessages.
Browse files Browse the repository at this point in the history
Soon, we'll need this to construct AvatarURL instances as part of
our validation-at-the-edge in the crunchy shell pattern [1]. In
particular, it gives us the realm, which we'll use to point to
uploaded avatars.

[1] https://github.com/zulip/zulip-mobile/blob/master/docs/architecture/crunchy-shell.md
  • Loading branch information
chrisbobbe authored and gnprice committed Aug 13, 2020
1 parent 99f959f commit c72ec2c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/api/messages/__tests__/migrateMessages-test.js
Expand Up @@ -2,6 +2,7 @@
import omit from 'lodash.omit';

import { migrateMessages } from '../getMessages';
import { identityOfAuth } from '../../../account/accountMisc';
import * as eg from '../../../__tests__/lib/exampleData';
import type { ServerMessage, ServerReaction } from '../getMessages';
import type { Message } from '../../modelTypes';
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('migrateMessages', () => {
},
];

const actualOutput: Message[] = migrateMessages(input);
const actualOutput: Message[] = migrateMessages(input, identityOfAuth(eg.selfAuth));

test('Replace user object with `user_id`', () => {
expect(actualOutput.map(m => m.reactions)).toEqual(expectedOutput.map(m => m.reactions));
Expand Down
10 changes: 6 additions & 4 deletions src/api/messages/getMessages.js
@@ -1,8 +1,10 @@
/* @flow strict-local */
import type { Auth, ApiResponseSuccess } from '../transportTypes';
import type { Identity } from '../../types';
import type { Message, Narrow } from '../apiTypes';
import type { Reaction } from '../modelTypes';
import { apiGet } from '../apiFetch';
import { identityOfAuth } from '../../account/accountMisc';

type ApiResponseMessages = {|
...ApiResponseSuccess,
Expand Down Expand Up @@ -41,7 +43,7 @@ type ServerApiResponseMessages = {|
|};

/** Exported for tests only. */
export const migrateMessages = (messages: ServerMessage[]): Message[] =>
export const migrateMessages = (messages: ServerMessage[], identity: Identity): Message[] =>
messages.map(message => {
const { reactions, ...restMessage } = message;
return {
Expand All @@ -56,11 +58,11 @@ export const migrateMessages = (messages: ServerMessage[]): Message[] =>
};
});

const migrateResponse = response => {
const migrateResponse = (response, identity: Identity) => {
const { messages, ...restResponse } = response;
return {
...restResponse,
messages: migrateMessages(messages),
messages: migrateMessages(messages, identity),
};
};

Expand Down Expand Up @@ -91,5 +93,5 @@ export default async (
apply_markdown: true,
use_first_unread_anchor: useFirstUnread,
});
return migrateResponse(response);
return migrateResponse(response, identityOfAuth(auth));
};

0 comments on commit c72ec2c

Please sign in to comment.