Skip to content

Commit

Permalink
On message touch, do a narrow
Browse files Browse the repository at this point in the history
Implements #306
  • Loading branch information
borisyankov committed Feb 14, 2017
1 parent ab387ac commit 5813243
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 32 deletions.
9 changes: 2 additions & 7 deletions src/message-list/renderMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,11 @@ export default ({ auth, subscriptions, messages, narrow, mute, doNarrow }) => {
list.push(
<MessageContainer
type="message"
id={item.id}
key={item.id}
auth={auth}
messageId={item.id}
message={item}
isBrief={shouldGroupWithPrev}
fromName={item.sender_full_name}
fromEmail={item.sender_email}
message={item.content}
timestamp={item.timestamp}
reactions={item.reactions}
doNarrow={doNarrow}
avatarUrl={getFullUrl(item.avatar_url, auth.realm)}
/>
);
Expand Down
16 changes: 11 additions & 5 deletions src/message/MessageBrief.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { StyleSheet, View } from 'react-native';

import { Touchable } from '../common';
import ReactionList from '../reactions/ReactionList';

const styles = StyleSheet.create({
Expand All @@ -17,19 +18,24 @@ const styles = StyleSheet.create({
export default class MessageBrief extends React.PureComponent {

props: {
message: string,
message: {},
selfEmail: string,
reactions: [],
};

render() {
const { messageId, children, reactions, selfEmail } = this.props;
const { message, children, selfEmail, onPress } = this.props;

return (
<View style={styles.message}>
{children}
<Touchable onPress={onPress}>
<View>
{children}
</View>
</Touchable>
<ReactionList
messageId={messageId}
reactions={reactions}
messageId={message.id}
reactions={message.reactions}
selfEmail={selfEmail}
/>
</View>
Expand Down
20 changes: 12 additions & 8 deletions src/message/MessageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import htmlparser from 'htmlparser2';

import renderHtmlChildren from './html/renderHtmlChildren';
import { narrowFromMessage } from '../utils/narrow';
import MessageFull from './MessageFull';
import MessageBrief from './MessageBrief';

Expand All @@ -16,22 +17,25 @@ const htmlToDomTree = html => {
};

export default class MessageContainer extends React.PureComponent {

handlePress = () => {
const { message, doNarrow } = this.props;
doNarrow(narrowFromMessage(message));
}

render() {
const { auth, avatarUrl, timestamp, twentyFourHourTime, messageId,
fromName, message, fromEmail, isBrief, reactions } = this.props;
const { message, auth, avatarUrl, twentyFourHourTime, isBrief, doNarrow } = this.props;
const MessageComponent = isBrief ? MessageBrief : MessageFull;
const dom = htmlToDomTree(message);
const dom = htmlToDomTree(message.content);

return (
<MessageComponent
messageId={messageId}
message={message}
avatarUrl={avatarUrl}
fromName={fromName}
fromEmail={fromEmail}
timestamp={timestamp}
twentyFourHourTime={twentyFourHourTime}
reactions={reactions}
selfEmail={auth.email}
doNarrow={doNarrow}
onPress={this.handlePress}
>
{renderHtmlChildren({ childrenNodes: dom, auth })}
</MessageComponent>
Expand Down
26 changes: 14 additions & 12 deletions src/message/MessageFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StyleSheet, View } from 'react-native';
import { connect } from 'react-redux';

import boundActions from '../boundActions';
import { Avatar } from '../common';
import { Avatar, Touchable } from '../common';
import Subheader from './Subheader';
import ReactionList from '../reactions/ReactionList';

Expand All @@ -24,37 +24,39 @@ class MessageFull extends React.PureComponent {

props: {
avatarUrl: string,
fromName: string,
fromEmail: string,
selfEmail: string,
timestamp: number,
reactions: [],
twentyFourHourTime: bool,
};

handleAvatarPress = () =>
this.props.pushRoute('account-details', this.props.fromEmail);
this.props.pushRoute('account-details', this.props.message.sender_email);

render() {
const { messageId, children, avatarUrl, timestamp, twentyFourHourTime,
fromName, reactions, selfEmail } = this.props;
const { message, children, avatarUrl, twentyFourHourTime, selfEmail, onPress } = this.props;

return (
<View style={styles.message}>
<Avatar
avatarUrl={avatarUrl}
name={fromName}
name={message.sender_full_name}
onPress={this.handleAvatarPress}
/>
<View style={styles.content}>
<Subheader
from={fromName}
timestamp={timestamp}
from={message.sender_full_name}
timestamp={message.timestamp}
twentyFourHourTime={twentyFourHourTime}
/>
{children}
<Touchable onPress={onPress}>
<View>
{children}
</View>
</Touchable>
<ReactionList
messageId={messageId}
reactions={reactions}
messageId={message.id}
reactions={message.reactions}
selfEmail={selfEmail}
/>
</View>
Expand Down
48 changes: 48 additions & 0 deletions src/utils/__tests__/narrow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
topicNarrow, isTopicNarrow,
searchNarrow, isSearchNarrow,
isMessageInNarrow,
narrowFromMessage,
} from '../narrow';

describe('homeNarrow', () => {
Expand Down Expand Up @@ -208,3 +209,50 @@ describe('isMessageInNarrow', () => {
expect(isMessageInNarrow(message, narrow)).toBe(true);
});
});

describe('narrowFromMessage', () => {
test('message with single recipient, returns a private narrow', () => {
const message = {
display_recipient: ['bob@example.com'],
};
const expectedNarrow = privateNarrow('bob@example.com');

const actualNarrow = narrowFromMessage(message);

expect(actualNarrow).toEqual(expectedNarrow);
});

test('for message with multiple recipients, return a group narrow', () => {
const message = {
display_recipient: ['bob@example.com', 'john@example.com'],
};
const expectedNarrow = groupNarrow(['bob@example.com', 'john@example.com']);

const actualNarrow = narrowFromMessage(message);

expect(actualNarrow).toEqual(expectedNarrow);
});

test('if recipient of a message is string, returns a stream narrow', () => {
const message = {
display_recipient: 'stream',
};
const expectedNarrow = streamNarrow('stream');

const actualNarrow = narrowFromMessage(message);

expect(actualNarrow).toEqual(expectedNarrow);
});

test('if recipient is a string and there is a subject returns a topic narrow', () => {
const message = {
display_recipient: 'stream',
subject: 'subject'
};
const expectedNarrow = topicNarrow('stream', 'subject');

const actualNarrow = narrowFromMessage(message);

expect(actualNarrow).toEqual(expectedNarrow);
});
});
12 changes: 12 additions & 0 deletions src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ export const canSendToNarrow = (narrow) =>
isGroupNarrow(narrow) ||
isStreamNarrow(narrow) ||
isTopicNarrow(narrow);

export const narrowFromMessage = (message) => {
if (Array.isArray(message.display_recipient)) {
return groupNarrow(message.display_recipient);
}

if (message.subject && message.subject.length) {
return topicNarrow(message.display_recipient, message.subject);
}

return streamNarrow(message.display_recipient);
};

0 comments on commit 5813243

Please sign in to comment.