Skip to content

Commit

Permalink
Offline last message updated in sidenav
Browse files Browse the repository at this point in the history
  • Loading branch information
bhardwajaditya committed Dec 12, 2019
1 parent 15fe0e6 commit 07db7f6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/lib/client/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TimeSync } from 'meteor/mizzao:timesync';
import s from 'underscore.string';
import toastr from 'toastr';

import { ChatMessage, CachedChatMessage } from '../../../models';
import { ChatMessage, CachedChatMessage, ChatRoom, ChatSubscription } from '../../../models';
import { settings } from '../../../settings';
import { callbacks } from '../../../callbacks';
import { promises } from '../../../promises/client';
Expand Down Expand Up @@ -36,6 +36,8 @@ Meteor.methods({
promises.run('onClientMessageReceived', message).then(function(message) {
ChatMessage.insert(message);
CachedChatMessage.save();
ChatRoom.setLastMessage(message.rid, message);
ChatSubscription.setLastMessage(message.rid, message);
return callbacks.run('afterSaveMessage', message);
});
},
Expand Down
6 changes: 6 additions & 0 deletions app/models/client/models/ChatRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ ChatRoom.setReactionsInLastMessage = function(roomId, lastMessage) {
ChatRoom.unsetReactionsInLastMessage = function(roomId) {
return this.update({ _id: roomId }, { $unset: { lastMessage: { reactions: 1 } } });
};

ChatRoom.setLastMessage = function(roomId, lastMessage) {
const update = this.update({ _id: roomId }, { $set: { lastMessage } });
CachedChatRoom.save();
return update;
};
6 changes: 6 additions & 0 deletions app/models/client/models/ChatSubscription.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { CachedChatSubscription } from './CachedChatSubscription';

export const ChatSubscription = CachedChatSubscription.collection;

ChatSubscription.setLastMessage = function(roomId, lastMessage) {
const update = this.update({ rid: roomId }, { $set: { lastMessage } });
CachedChatSubscription.save();
return update;
};
9 changes: 9 additions & 0 deletions app/ui-sidenav/client/sidebarItem.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
<div class="sidebar-item__last-message">
<span class="message-body--unstyled">{{{lastMessage}}}</span>
</div>
{{#if lastMessageSentByUser}}
<div class="sidebar-item__time">
{{#if lastMessageDelivered}}
{{>icon icon="check"}}
{{else}}
{{>icon icon="clock"}}
{{/if}}
</div>
{{/if}}
{{/if}}
{{/if}}
{{#if unread}}
Expand Down
8 changes: 7 additions & 1 deletion app/ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Template.sidebarItem.helpers({
lastMessageTs() {
return this.lastMessage && Template.instance().lastMessageTs.get();
},
lastMessageDelivered() {
return this.lastMessage && !this.lastMessage.temp;
},
lastMessageSentByUser() {
return this.lastMessage && this.lastMessage.u && (this.lastMessage.u._id === Meteor.userId());
},
mySelf() {
return this.t === 'd' && this.name === Template.instance().user.username;
},
Expand Down Expand Up @@ -86,7 +92,7 @@ Template.sidebarItem.onCreated(function() {
}
}

if (!currentData.lastMessage._id) {
if (currentData.lastMessage && !currentData.lastMessage._id) {
this.renderedMessage = currentData.lastMessage.msg;
return;
}
Expand Down

0 comments on commit 07db7f6

Please sign in to comment.