app/components/message_thread/index.js
// Retrieve the relevant messages based on sender/receiver key pairing.
Retrieve the relevant messages based on sender/receiver key pairing.
Retrieve the relevant messages based on sender / receiver key pairing .
retrieveMessages() {
        let sender = this.props.pair.sender;
        let receiver = this.props.pair.receiver;
        let messageList = [];
        let portrait = require('../../assets/default_avatar.png');
        this.props.messages.forEach((item, index, array) => {
            if((item.from === receiver && item.to === sender) ||
            (item.to === receiver && item.from === sender)) {
                console.log(item.time);
                let message = {
                    _id: item.id,
                    text: item.body,
                    createdAt: new Date(item.time * 1000),
                    user: {
                        _id: 0,
                    },
                };
                if(item.from === receiver) {
                    message.user._id = 2;
                    if(this.props.pair.senderCard.image !== null) {
                        message.user.avatar = this.props.pair.receiverCard.image;
                    } else {
                        message.user.avatar = portrait;
                    }
                }
                else
                    message.user._id = 1;
                messageList.push(message);
            }
        });
        messageList.sort((a, b) => {
            return a.createdAt.getTime() < b.createdAt.getTime();
        });
        this.setState({messages : messageList});
    }
retrieveMessages ( ) { let sender = this . props . pair . sender ; let receiver = this . props . pair . receiver ; let messageList = [ ] ; let portrait = require ( '../../assets/default_avatar.png' ) ; this . props . messages . forEach ( ( item , index , array ) => { if ( ( item . from === receiver && item . to === sender ) || ( item . to === receiver && item . from === sender ) ) { console . log ( item . time ) ; let message = { _id : item . id , text : item . body , createdAt : new Date ( item . time * 1000 ) , user : { _id : 0 , } , } ; if ( item . from === receiver ) { message . user . _id = 2 ; if ( this . props . pair . senderCard . image !== null ) { message . user . avatar = this . props . pair . receiverCard . image ; } else { message . user . avatar = portrait ; } } else message . user . _id = 1 ; messageList . push ( message ) ; } } ) ; messageList . sort ( ( a , b ) => { return a . createdAt . getTime ( ) < b . createdAt . getTime ( ) ; } ) ; this . setState ( { messages : messageList } ) ; }
Retrieve the relevant messages based on sender/receiver key pairing.
Retrieve the relevant messages based on sender / receiver key pairing .
// If the sender/receiver pairing matches the to/from section of a message, add to our list. // User _id: 1 for application user, 2 for other party // Sort the messages by time // Update the message list