/* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ 'use strict'; import ReplyThread from "../elements/ReplyThread"; const React = require('react'); import PropTypes from 'prop-types'; const classNames = require("classnames"); import { _t, _td } from '../../../languageHandler'; const Modal = require('../../../Modal'); const sdk = require('../../../index'); const TextForEvent = require('../../../TextForEvent'); import withMatrixClient from '../../../wrappers/withMatrixClient'; import dis from '../../../dispatcher'; import SettingsStore from "../../../settings/SettingsStore"; import {EventStatus} from 'matrix-js-sdk'; const ObjectUtils = require('../../../ObjectUtils'); const eventTileTypes = { 'm.room.message': 'messages.MessageEvent', 'm.sticker': 'messages.MessageEvent', 'm.call.invite': 'messages.TextualEvent', 'm.call.answer': 'messages.TextualEvent', 'm.call.hangup': 'messages.TextualEvent', }; const stateEventTileTypes = { 'm.room.aliases': 'messages.TextualEvent', // 'm.room.aliases': 'messages.RoomAliasesEvent', // too complex 'm.room.canonical_alias': 'messages.TextualEvent', 'm.room.create': 'messages.RoomCreate', 'm.room.member': 'messages.TextualEvent', 'm.room.name': 'messages.TextualEvent', 'm.room.avatar': 'messages.RoomAvatarEvent', 'm.room.third_party_invite': 'messages.TextualEvent', 'm.room.history_visibility': 'messages.TextualEvent', 'm.room.encryption': 'messages.TextualEvent', 'm.room.topic': 'messages.TextualEvent', 'm.room.power_levels': 'messages.TextualEvent', 'm.room.pinned_events': 'messages.TextualEvent', 'm.room.server_acl': 'messages.TextualEvent', 'im.vector.modular.widgets': 'messages.TextualEvent', 'm.room.tombstone': 'messages.TextualEvent', 'm.room.join_rules': 'messages.TextualEvent', 'm.room.guest_access': 'messages.TextualEvent', 'm.room.related_groups': 'messages.TextualEvent', }; function getHandlerTile(ev) { const type = ev.getType(); return ev.isState() ? stateEventTileTypes[type] : eventTileTypes[type]; } const MAX_READ_AVATARS = 5; // Our component structure for EventTiles on the timeline is: // // .-EventTile------------------------------------------------. // | MemberAvatar (SenderProfile) TimeStamp | // | .-{Message,Textual}Event---------------. Read Avatars | // | | .-MFooBody-------------------. | | // | | | (only if MessageEvent) | | | // | | '----------------------------' | | // | '--------------------------------------' | // '----------------------------------------------------------' module.exports = withMatrixClient(React.createClass({ displayName: 'EventTile', propTypes: { /* MatrixClient instance for sender verification etc */ matrixClient: PropTypes.object.isRequired, /* the MatrixEvent to show */ mxEvent: PropTypes.object.isRequired, /* true if mxEvent is redacted. This is a prop because using mxEvent.isRedacted() * might not be enough when deciding shouldComponentUpdate - prevProps.mxEvent * references the same this.props.mxEvent. */ isRedacted: PropTypes.bool, /* true if this is a continuation of the previous event (which has the * effect of not showing another avatar/displayname */ continuation: PropTypes.bool, /* true if this is the last event in the timeline (which has the effect * of always showing the timestamp) */ last: PropTypes.bool, /* true if this is search context (which has the effect of greying out * the text */ contextual: PropTypes.bool, /* a list of words to highlight, ordered by longest first */ highlights: PropTypes.array, /* link URL for the highlights */ highlightLink: PropTypes.string, /* should show URL previews for this event */ showUrlPreview: PropTypes.bool, /* is this the focused event */ isSelectedEvent: PropTypes.bool, /* callback called when dynamic content in events are loaded */ onHeightChanged: PropTypes.func, /* a list of read-receipts we should show. Each object has a 'roomMember' and 'ts'. */ readReceipts: PropTypes.arrayOf(React.PropTypes.object), /* opaque readreceipt info for each userId; used by ReadReceiptMarker * to manage its animations. Should be an empty object when the room * first loads */ readReceiptMap: PropTypes.object, /* A function which is used to check if the parent panel is being * unmounted, to avoid unnecessary work. Should return true if we * are being unmounted. */ checkUnmounting: PropTypes.func, /* the status of this event - ie, mxEvent.status. Denormalised to here so * that we can tell when it changes. */ eventSendStatus: PropTypes.string, /* the shape of the tile. by default, the layout is intended for the * normal room timeline. alternative values are: "file_list", "file_grid" * and "notif". This could be done by CSS, but it'd be horribly inefficient. * It could also be done by subclassing EventTile, but that'd be quite * boiilerplatey. So just make the necessary render decisions conditional * for now. */ tileShape: PropTypes.string, // show twelve hour timestamps isTwelveHour: PropTypes.bool, // helper function to access relations for an event getRelationsForEvent: PropTypes.func, }, getDefaultProps: function() { return { // no-op function because onHeightChanged is optional yet some sub-components assume its existence onHeightChanged: function() {}, }; }, getInitialState: function() { return { // Whether the action bar is focused. actionBarFocused: false, // Whether all read receipts are being displayed. If not, only display // a truncation of them. allReadAvatars: false, // Whether the event's sender has been verified. verified: null, // Whether onRequestKeysClick has been called since mounting. previouslyRequestedKeys: false, // The Relations model from the JS SDK for reactions to `mxEvent` reactions: this.getReactions(), }; }, componentWillMount: function() { // don't do RR animations until we are mounted this._suppressReadReceiptAnimation = true; this._verifyEvent(this.props.mxEvent); }, componentDidMount: function() { this._suppressReadReceiptAnimation = false; const client = this.props.matrixClient; client.on("deviceVerificationChanged", this.onDeviceVerificationChanged); this.props.mxEvent.on("Event.decrypted", this._onDecrypted); if (SettingsStore.isFeatureEnabled("feature_reactions")) { this.props.mxEvent.on("Event.relationsCreated", this._onReactionsCreated); } }, componentWillReceiveProps: function(nextProps) { // re-check the sender verification as outgoing events progress through // the send process. if (nextProps.eventSendStatus !== this.props.eventSendStatus) { this._verifyEvent(nextProps.mxEvent); } }, shouldComponentUpdate: function(nextProps, nextState) { if (!ObjectUtils.shallowEqual(this.state, nextState)) { return true; } return !this._propsEqual(this.props, nextProps); }, componentWillUnmount: function() { const client = this.props.matrixClient; client.removeListener("deviceVerificationChanged", this.onDeviceVerificationChanged); this.props.mxEvent.removeListener("Event.decrypted", this._onDecrypted); if (SettingsStore.isFeatureEnabled("feature_reactions")) { this.props.mxEvent.removeListener("Event.relationsCreated", this._onReactionsCreated); } }, /** called when the event is decrypted after we show it. */ _onDecrypted: function() { // we need to re-verify the sending device. // (we call onHeightChanged in _verifyEvent to handle the case where decryption // has caused a change in size of the event tile) this._verifyEvent(this.props.mxEvent); this.forceUpdate(); }, onDeviceVerificationChanged: function(userId, device) { if (userId === this.props.mxEvent.getSender()) { this._verifyEvent(this.props.mxEvent); } }, _verifyEvent: async function(mxEvent) { if (!mxEvent.isEncrypted()) { return; } const verified = await this.props.matrixClient.isEventSenderVerified(mxEvent); this.setState({ verified: verified, }, () => { // Decryption may have caused a change in size this.props.onHeightChanged(); }); }, _propsEqual: function(objA, objB) { const keysA = Object.keys(objA); const keysB = Object.keys(objB); if (keysA.length !== keysB.length) { return false; } for (let i = 0; i < keysA.length; i++) { const key = keysA[i]; if (!objB.hasOwnProperty(key)) { return false; } // need to deep-compare readReceipts if (key === 'readReceipts') { const rA = objA[key]; const rB = objB[key]; if (rA === rB) { continue; } if (!rA || !rB) { return false; } if (rA.length !== rB.length) { return false; } for (let j = 0; j < rA.length; j++) { if (rA[j].userId !== rB[j].userId) { return false; } // one has a member set and the other doesn't? if (rA[j].roomMember !== rB[j].roomMember) { return false; } } } else { if (objA[key] !== objB[key]) { return false; } } } return true; }, shouldHighlight: function() { const actions = this.props.matrixClient.getPushActionsForEvent(this.props.mxEvent); if (!actions || !actions.tweaks) { return false; } // don't show self-highlights from another of our clients if (this.props.mxEvent.getSender() === this.props.matrixClient.credentials.userId) { return false; } return actions.tweaks.highlight; }, toggleAllReadAvatars: function() { this.setState({ allReadAvatars: !this.state.allReadAvatars, }); }, getReadAvatars: function() { // return early if there are no read receipts if (!this.props.readReceipts || this.props.readReceipts.length === 0) { return (); } const ReadReceiptMarker = sdk.getComponent('rooms.ReadReceiptMarker'); const avatars = []; const receiptOffset = 15; let left = 0; const receipts = this.props.readReceipts || []; for (let i = 0; i < receipts.length; ++i) { const receipt = receipts[i]; let hidden = true; if ((i < MAX_READ_AVATARS) || this.state.allReadAvatars) { hidden = false; } // TODO: we keep the extra read avatars in the dom to make animation simpler // we could optimise this to reduce the dom size. // If hidden, set offset equal to the offset of the final visible avatar or // else set it proportional to index left = (hidden ? MAX_READ_AVATARS - 1 : i) * -receiptOffset; const userId = receipt.userId; let readReceiptInfo; if (this.props.readReceiptMap) { readReceiptInfo = this.props.readReceiptMap[userId]; if (!readReceiptInfo) { readReceiptInfo = {}; this.props.readReceiptMap[userId] = readReceiptInfo; } } // add to the start so the most recent is on the end (ie. ends up rightmost) avatars.unshift(