Skip to content

Commit

Permalink
Merge pull request #9216 from RocketChat/add-mark-as-unread
Browse files Browse the repository at this point in the history
[NEW] Sidebar menu option to mark room as unread
  • Loading branch information
rodrigok authored Jan 15, 2018
2 parents e962634 + d8054b3 commit 18db56c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import logger from './logger';
Meteor.methods({
unreadMessages(firstUnreadMessage) {
if (!Meteor.userId()) {
unreadMessages(firstUnreadMessage, room) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'unreadMessages'
});
}

if (room) {
const lastMessage = RocketChat.models.Messages.findVisibleByRoomId(room, {limit: 1, sort: {ts: -1}}).fetch()[0];

if (lastMessage == null) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages'
});
}

return RocketChat.models.Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);
}

const originalMessage = RocketChat.models.Messages.findOneById(firstUnreadMessage._id, {
fields: {
u: 1,
Expand All @@ -14,17 +29,17 @@ Meteor.methods({
ts: 1
}
});
if (originalMessage == null || Meteor.userId() === originalMessage.u._id) {
if (originalMessage == null || userId === originalMessage.u._id) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages'
});
}
const lastSeen = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(originalMessage.rid, Meteor.userId()).ls;
const lastSeen = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(originalMessage.rid, userId).ls;
if (firstUnreadMessage.ts >= lastSeen) {
return logger.connection.debug('Provided message is already marked as unread');
}
logger.connection.debug(`Updating unread message of ${ originalMessage.ts } as the first unread`);
return RocketChat.models.Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, Meteor.userId(), originalMessage.ts);
return RocketChat.models.Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);
}
});
7 changes: 7 additions & 0 deletions packages/rocketchat-ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Template.sidebarItem.events({
type: 'sidebar-item',
id: 'read'
});
} else {
items.push({
icon: 'flag',
name: t('Mark_as_unread'),
type: 'sidebar-item',
id: 'unread'
});
}

if (canFavorite) {
Expand Down
27 changes: 23 additions & 4 deletions packages/rocketchat-ui/client/views/app/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ Template.popover.events({
'click [data-type="sidebar-item"]'(e, instance) {
popover.close();
const { rid, name, template } = instance.data.data;
const action = e.currentTarget.dataset.id;

if (e.currentTarget.dataset.id === 'hide') {
if (action === 'hide') {
const warnText = RocketChat.roomTypes.roomTypes[template].getUiText(UiTextContext.HIDE_WARNING);

modal.open({
Expand Down Expand Up @@ -214,7 +215,7 @@ Template.popover.events({
return false;
}

if (e.currentTarget.dataset.id === 'leave') {
if (action === 'leave') {
let warnText;
switch (template) {
case 'c': warnText = 'Leave_Room_Warning'; break;
Expand Down Expand Up @@ -258,12 +259,30 @@ Template.popover.events({
return false;
}

if (e.currentTarget.dataset.id === 'read') {
if (action === 'read') {
Meteor.call('readMessages', rid);
return false;
}

if (e.currentTarget.dataset.id === 'favorite') {
if (action === 'unread') {
Meteor.call('unreadMessages', null, rid, function(error) {
if (error) {
return handleError(error);
}

const subscription = ChatSubscription.findOne({rid});
if (subscription == null) {
return;
}
RoomManager.close(subscription.t + subscription.name);

FlowRouter.go('home');
});

return false;
}

if (action === 'favorite') {
Meteor.call('toggleFavorite', rid, !$(e.currentTarget).hasClass('rc-popover__item--star-filled'), function(err) {
popover.close();
if (err) {
Expand Down

0 comments on commit 18db56c

Please sign in to comment.