From d8de607edcf06112ec82983e5f46538e1b3762f2 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 15 Jan 2019 09:20:43 -0600 Subject: [PATCH] Update RoomTile on status change --- src/components/views/rooms/RoomTile.js | 65 ++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/components/views/rooms/RoomTile.js b/src/components/views/rooms/RoomTile.js index 8778340601..1f9c0c1523 100644 --- a/src/components/views/rooms/RoomTile.js +++ b/src/components/views/rooms/RoomTile.js @@ -62,6 +62,7 @@ module.exports = React.createClass({ notifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId), notificationCount: this.props.room.getUnreadNotificationCount(), selected: this.props.room.roomId === ActiveRoomObserver.getActiveRoomId(), + statusMessage: this._getStatusMessage(), }); }, @@ -79,6 +80,33 @@ module.exports = React.createClass({ return Boolean(dmRooms); }, + _shouldShowStatusMessage() { + if (!SettingsStore.isFeatureEnabled("feature_custom_status")) { + return false; + } + const isInvite = this.props.room.getMyMembership() === "invite"; + const isJoined = this.props.room.getMyMembership() === "join"; + const looksLikeDm = this.props.room.getInvitedAndJoinedMemberCount() === 2; + return !isInvite && isJoined && looksLikeDm; + }, + + _getStatusMessageUser() { + const selfId = MatrixClientPeg.get().getUserId(); + const otherMember = this.props.room.currentState.getMembersExcept([selfId])[0]; + if (!otherMember) { + return null; + } + return otherMember.user; + }, + + _getStatusMessage() { + const statusUser = this._getStatusMessageUser(); + if (!statusUser) { + return ""; + } + return statusUser._unstable_statusMessage; + }, + onRoomTimeline: function(ev, room) { if (room !== this.props.room) return; this.setState({ @@ -134,6 +162,16 @@ module.exports = React.createClass({ MatrixClientPeg.get().on("Room.name", this.onRoomName); ActiveRoomObserver.addListener(this.props.room.roomId, this._onActiveRoomChange); this.dispatcherRef = dis.register(this.onAction); + + if (this._shouldShowStatusMessage()) { + const statusUser = this._getStatusMessageUser(); + if (statusUser) { + statusUser.on( + "User._unstable_statusMessage", + this._onStatusMessageCommitted, + ); + } + } }, componentWillUnmount: function() { @@ -145,6 +183,16 @@ module.exports = React.createClass({ } ActiveRoomObserver.removeListener(this.props.room.roomId, this._onActiveRoomChange); dis.unregister(this.dispatcherRef); + + if (this._shouldShowStatusMessage()) { + const statusUser = this._getStatusMessageUser(); + if (statusUser) { + statusUser.removeListener( + "User._unstable_statusMessage", + this._onStatusMessageCommitted, + ); + } + } }, componentWillReceiveProps: function(props) { @@ -172,6 +220,13 @@ module.exports = React.createClass({ return false; }, + _onStatusMessageCommitted() { + // The status message `User` object has observed a message change. + this.setState({ + statusMessage: this._getStatusMessage(), + }); + }, + onClick: function(ev) { if (this.props.onClick) { this.props.onClick(this.props.room.roomId, ev); @@ -257,15 +312,9 @@ module.exports = React.createClass({ const mentionBadges = this.props.highlight && this._shouldShowMentionBadge(); const badges = notifBadges || mentionBadges; - const isJoined = this.props.room.getMyMembership() === "join"; - const looksLikeDm = this.props.room.getInvitedAndJoinedMemberCount() === 2; let subtext = null; - if (!isInvite && isJoined && looksLikeDm && SettingsStore.isFeatureEnabled("feature_custom_status")) { - const selfId = MatrixClientPeg.get().getUserId(); - const otherMember = this.props.room.currentState.getMembersExcept([selfId])[0]; - if (otherMember && otherMember.user && otherMember.user._unstable_statusMessage) { - subtext = otherMember.user._unstable_statusMessage; - } + if (this._shouldShowStatusMessage()) { + subtext = this.state.statusMessage; } const classes = classNames({