diff --git a/app/lib/realm.js b/app/lib/realm.js index 17d30e26a..b67661669 100644 --- a/app/lib/realm.js +++ b/app/lib/realm.js @@ -49,6 +49,7 @@ const roomsSchema = { properties: { _id: 'string', t: 'string', + lastMessage: 'messages', _updatedAt: { type: 'date', optional: true } } }; diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index dc0bf1bd6..188178a8f 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -432,10 +432,17 @@ const RocketChat = { } return subscription; }); + database.write(() => { data.forEach(subscription => database.create('subscriptions', subscription, true)); }); + + database.write(() => { + rooms.forEach(room => + database.create('rooms', room, true)); + }); + this.ddp.subscribe('stream-notify-user', `${ login.user.id }/subscriptions-changed`, false); this.ddp.subscribe('stream-notify-user', `${ login.user.id }/rooms-changed`, false); return data; diff --git a/app/presentation/RoomItem.js b/app/presentation/RoomItem.js index d198f91fc..82bd362a0 100644 --- a/app/presentation/RoomItem.js +++ b/app/presentation/RoomItem.js @@ -52,6 +52,7 @@ export default class RoomItem extends React.PureComponent { type: PropTypes.string.isRequired, name: PropTypes.string.isRequired, _updatedAt: PropTypes.instanceOf(Date), + lastMessage: PropTypes.object, favorite: PropTypes.bool, alert: PropTypes.bool, unread: PropTypes.number, @@ -94,11 +95,13 @@ export default class RoomItem extends React.PureComponent { render() { const { - favorite, alert, unread, userMentions, name, _updatedAt + favorite, alert, unread, userMentions, name, lastMessage, _updatedAt } = this.props; const date = this.formatDate(_updatedAt); + console.log("do we have a last message?", lastMessage); + let accessibilityLabel = name; if (unread === 1) { accessibilityLabel += `, ${ unread } alert`; @@ -118,6 +121,7 @@ export default class RoomItem extends React.PureComponent { {this.icon} { name } + {lastMessage ? {lastMessage.u.username}: {lastMessage.msg} : No Message} {_updatedAt ? { date } : null} {this.renderNumber(unread, userMentions)} diff --git a/app/views/RoomsListView/index.js b/app/views/RoomsListView/index.js index f22926723..c1d63f6ec 100644 --- a/app/views/RoomsListView/index.js +++ b/app/views/RoomsListView/index.js @@ -48,6 +48,7 @@ export default class RoomsListView extends React.Component { searchText: '' }; this.data = database.objects('subscriptions').sorted('roomUpdatedAt', true); + } componentDidMount() { @@ -79,6 +80,12 @@ export default class RoomsListView extends React.Component { this.search(text); } + getLastMessage = (subscription) => { + const room = database.objects('rooms').sorted('_updatedAt', true).slice().find(({ _id }) => _id === subscription.rid); + console.log('ROOM', room); + return room.lastMessage; + } + search(text) { const searchText = text.trim(); if (searchText === '') { @@ -202,6 +209,7 @@ export default class RoomsListView extends React.Component { alert={item.alert} unread={item.unread} userMentions={item.userMentions} + lastMessage={this.getLastMessage(item)} favorite={item.f} name={item.name} _updatedAt={item.roomUpdatedAt}