Add mark as unread option to the room menu on sidebar

pull/9216/head
Karl Prieb 8 years ago
parent 981c366ff2
commit d8054b38c5
  1. 25
      packages/rocketchat-message-mark-as-unread/server/unreadMessages.js
  2. 7
      packages/rocketchat-ui-sidenav/client/sidebarItem.js
  3. 27
      packages/rocketchat-ui/client/views/app/popover.js

@ -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,
@ -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);
}
});

@ -107,6 +107,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) {

@ -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({
@ -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;
@ -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) {

Loading…
Cancel
Save