From 96567dad0d93ffcc1b4962350e6b7b0ae04d791b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 25 Aug 2016 16:55:09 +0100 Subject: [PATCH] Fix NPE when we don't know the sender of an event Fixes a bug introduced in https://github.com/matrix-org/matrix-react-sdk/pull/426. Particularly when we are showing search results, we may not recognise the sender of an event; attempting to create a MemberAvatar for it will lead to null-reference errors. Also a bit of untangling of the logic of needsSenderProfile. Since https://github.com/matrix-org/matrix-react-sdk/pull/422, EventTileType.needsSenderProfile was only being called on MessageEvents, and therefore only returned true. It's a shame to see all this logic going into EventTile rather than the individual EventTileTypes, but since it's there, let's not leave the unused logic lying around in the EventTileType implementations. --- src/components/views/messages/MessageEvent.js | 6 --- src/components/views/messages/TextualEvent.js | 7 --- src/components/views/rooms/EventTile.js | 53 ++++++++++++------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/components/views/messages/MessageEvent.js b/src/components/views/messages/MessageEvent.js index 0863fe0842..26658c3005 100644 --- a/src/components/views/messages/MessageEvent.js +++ b/src/components/views/messages/MessageEvent.js @@ -22,12 +22,6 @@ var sdk = require('../../../index'); module.exports = React.createClass({ displayName: 'MessageEvent', - statics: { - needsSenderProfile: function() { - return true; - } - }, - propTypes: { /* the MatrixEvent to show */ mxEvent: React.PropTypes.object.isRequired, diff --git a/src/components/views/messages/TextualEvent.js b/src/components/views/messages/TextualEvent.js index 7cab98ea84..8319dbd434 100644 --- a/src/components/views/messages/TextualEvent.js +++ b/src/components/views/messages/TextualEvent.js @@ -24,12 +24,6 @@ import sdk from '../../../index'; module.exports = React.createClass({ displayName: 'TextualEvent', - statics: { - needsSenderProfile: function() { - return false; - } - }, - render: function() { const EmojiText = sdk.getComponent('elements.EmojiText'); var text = TextForEvent.textForEvent(this.props.mxEvent); @@ -39,4 +33,3 @@ module.exports = React.createClass({ ); }, }); - diff --git a/src/components/views/rooms/EventTile.js b/src/components/views/rooms/EventTile.js index b33860b7b3..b1df3f3267 100644 --- a/src/components/views/rooms/EventTile.js +++ b/src/components/views/rooms/EventTile.js @@ -62,7 +62,7 @@ var MAX_READ_AVATARS = 5; // '----------------------------------------------------------' module.exports = React.createClass({ - displayName: 'Event', + displayName: 'EventTile', statics: { haveTileForEvent: function(e) { @@ -368,7 +368,7 @@ module.exports = React.createClass({ // room, or emote messages var isInfoMessage = (msgtype === 'm.emote' || eventType !== 'm.room.message'); - var EventTileType = sdk.getComponent(eventTileTypes[this.props.mxEvent.getType()]); + var EventTileType = sdk.getComponent(eventTileTypes[eventType]); // This shouldn't happen: the caller should check we support this type // before trying to instantiate us if (!EventTileType) { @@ -395,31 +395,44 @@ module.exports = React.createClass({ - var aux = null; - if (msgtype === 'm.image') aux = "sent an image"; - else if (msgtype === 'm.video') aux = "sent a video"; - else if (msgtype === 'm.file') aux = "uploaded a file"; - var readAvatars = this.getReadAvatars(); var avatar, sender; + let avatarSize; + let needsSenderProfile; + if (isInfoMessage) { + // a small avatar, with no sender profile, for emotes and + // joins/parts/etc + avatarSize = 14; + needsSenderProfile = false; + } else if (this.props.continuation) { + // no avatar or sender profile for continuation messages + avatarSize = 0; + needsSenderProfile = false; + } else { + avatarSize = 30; + needsSenderProfile = true; + } + + if (this.props.mxEvent.sender && avatarSize) { avatar = ( -
- -
- ); - } else if (!this.props.continuation) { - if (this.props.mxEvent.sender) { - avatar = (
- +
- ); - } - if (EventTileType.needsSenderProfile()) { - sender = ; - } + ); + } + + if (needsSenderProfile) { + let aux = null; + if (msgtype === 'm.image') aux = "sent an image"; + else if (msgtype === 'm.video') aux = "sent a video"; + else if (msgtype === 'm.file') aux = "uploaded a file"; + + sender = ; } var editButton = (