Skip to content
Snippets Groups Projects
Commit d8054b38 authored by Karl Prieb's avatar Karl Prieb
Browse files

Add mark as unread option to the room menu on sidebar

parent 981c366f
No related branches found
No related tags found
No related merge requests found
import logger from './logger'; import logger from './logger';
Meteor.methods({ Meteor.methods({
unreadMessages(firstUnreadMessage) { unreadMessages(firstUnreadMessage, room) {
if (!Meteor.userId()) { const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'unreadMessages' 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, { const originalMessage = RocketChat.models.Messages.findOneById(firstUnreadMessage._id, {
fields: { fields: {
u: 1, u: 1,
...@@ -14,17 +29,17 @@ Meteor.methods({ ...@@ -14,17 +29,17 @@ Meteor.methods({
ts: 1 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', { throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages', method: 'unreadMessages',
action: 'Unread_messages' 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) { if (firstUnreadMessage.ts >= lastSeen) {
return logger.connection.debug('Provided message is already marked as unread'); return logger.connection.debug('Provided message is already marked as unread');
} }
logger.connection.debug(`Updating unread message of ${ originalMessage.ts } as the first 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({ ...@@ -107,6 +107,13 @@ Template.sidebarItem.events({
type: 'sidebar-item', type: 'sidebar-item',
id: 'read' id: 'read'
}); });
} else {
items.push({
icon: 'flag',
name: t('Mark_as_unread'),
type: 'sidebar-item',
id: 'unread'
});
} }
if (canFavorite) { if (canFavorite) {
......
...@@ -183,8 +183,9 @@ Template.popover.events({ ...@@ -183,8 +183,9 @@ Template.popover.events({
'click [data-type="sidebar-item"]'(e, instance) { 'click [data-type="sidebar-item"]'(e, instance) {
popover.close(); popover.close();
const { rid, name, template } = instance.data.data; 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); const warnText = RocketChat.roomTypes.roomTypes[template].getUiText(UiTextContext.HIDE_WARNING);
modal.open({ modal.open({
...@@ -214,7 +215,7 @@ Template.popover.events({ ...@@ -214,7 +215,7 @@ Template.popover.events({
return false; return false;
} }
if (e.currentTarget.dataset.id === 'leave') { if (action === 'leave') {
let warnText; let warnText;
switch (template) { switch (template) {
case 'c': warnText = 'Leave_Room_Warning'; break; case 'c': warnText = 'Leave_Room_Warning'; break;
...@@ -258,12 +259,30 @@ Template.popover.events({ ...@@ -258,12 +259,30 @@ Template.popover.events({
return false; return false;
} }
if (e.currentTarget.dataset.id === 'read') { if (action === 'read') {
Meteor.call('readMessages', rid); Meteor.call('readMessages', rid);
return false; 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) { Meteor.call('toggleFavorite', rid, !$(e.currentTarget).hasClass('rc-popover__item--star-filled'), function(err) {
popover.close(); popover.close();
if (err) { if (err) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment