Skip to content

Commit

Permalink
Merge pull request #7477 from RocketChat/improvements/unread-count-op…
Browse files Browse the repository at this point in the history
…tions

[NEW] Option to select unread count behavior
  • Loading branch information
rodrigok committed Jul 12, 2017
2 parents 5cb30b4 + 649ad69 commit 1f3fc29
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 19 deletions.
4 changes: 3 additions & 1 deletion packages/rocketchat-cas/cas_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ Accounts.registerLoginHandler(function(options) {
ts: new Date(),
open: true,
alert: true,
unread: 1
unread: 1,
userMentions: 1,
groupMentions: 0
});
}
});
Expand Down
6 changes: 5 additions & 1 deletion packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@
"GoogleCloudStorage": "Google Cloud Storage",
"GoogleNaturalLanguage_ServiceAccount_Description": "Service account key JSON file. More information can be found [here](https://cloud.google.com/natural-language/docs/common/auth#set_up_a_service_account)",
"GoogleTagManager_id": "Google Tag Manager Id",
"Group_mentions_only": "Group mentions only",
"Guest_Pool": "Guest Pool",
"Hash": "Hash",
"Header": "Header",
Expand Down Expand Up @@ -1712,6 +1713,7 @@
"Unmute_user": "Unmute user",
"Unnamed": "Unnamed",
"Unpin_Message": "Unpin Message",
"Unread_Count": "Unread Count",
"Unread_Tray_Icon_Alert": "Unread Tray Icon Alert",
"Unread_Messages": "Unread Messages",
"Unread_Rooms": "Unread Rooms",
Expand Down Expand Up @@ -1741,6 +1743,7 @@
"User_added": "User added",
"User_added_by": "User <em>__user_added__</em> added by <em>__user_by__</em>.",
"User_added_successfully": "User added successfully",
"User_and_group_mentions_only": "User and group mentions only",
"User_doesnt_exist": "No user exists by the name of `@%s`.",
"User_has_been_activated": "User has been activated",
"User_has_been_deactivated": "User has been deactivated",
Expand All @@ -1761,6 +1764,7 @@
"User_left_male": "Has left the channel.",
"User_logged_out": "User is logged out",
"User_management": "User Management",
"User_mentions_only": "User mentions only",
"User_muted": "User Muted",
"User_muted_by": "User <em>__user_muted__</em> muted by <em>__user_by__</em>.",
"User_not_found": "User not found",
Expand Down Expand Up @@ -1877,4 +1881,4 @@
"your_message_optional": "your message (optional)",
"Your_password_is_wrong": "Your password is wrong!",
"Your_push_was_sent_to_s_devices": "Your push was sent to %s devices"
}
}
2 changes: 2 additions & 0 deletions packages/rocketchat-irc/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ class IrcClient {
open: false,
alert: false,
unread: 0,
userMentions: 0,
groupMentions: 0,
u: { _id: target._id, username: target.username }}
});
return { t: 'd', _id: rid };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ RocketChat.addUserToDefaultChannels = function(user, silenced) {
ts: new Date(),
open: true,
alert: true,
unread: 1
unread: 1,
userMentions: 1,
groupMentions: 0
});

// Insert user joined message
Expand Down
4 changes: 3 additions & 1 deletion packages/rocketchat-lib/server/functions/addUserToRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ RocketChat.addUserToRoom = function(rid, user, inviter, silenced) {
ts: now,
open: true,
alert: true,
unread: 1
unread: 1,
userMentions: 1,
groupMentions: 0
});

if (!silenced) {
Expand Down
21 changes: 15 additions & 6 deletions packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
return has;
}

if (room.t != null && room.t === 'd') {
// Update the other subscriptions
RocketChat.models.Subscriptions.incUnreadOfDirectForRoomIdExcludingUserId(message.rid, message.u._id, 1);
} else {
if (room != null) {
let toAll = false;
let toHere = false;
const mentionIds = [];
Expand Down Expand Up @@ -71,10 +68,22 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
}
});

const unreadCount = RocketChat.settings.get('Unread_Count');

if (toAll || toHere) {
RocketChat.models.Subscriptions.incUnreadForRoomIdExcludingUserId(room._id, message.u._id);
let incUnread = 0;
if (['all_messages', 'group_mentions_only', 'user_and_group_mentions_only'].includes(unreadCount)) {
incUnread = 1;
}
RocketChat.models.Subscriptions.incGroupMentionsAndUnreadForRoomIdExcludingUserId(room._id, message.u._id, 1, incUnread);
} else if ((mentionIds && mentionIds.length > 0) || (highlightsIds && highlightsIds.length > 0)) {
RocketChat.models.Subscriptions.incUnreadForRoomIdAndUserIds(room._id, _.compact(_.unique(mentionIds.concat(highlightsIds))));
let incUnread = 0;
if (['all_messages', 'user_mentions_only', 'user_and_group_mentions_only'].includes(unreadCount)) {
incUnread = 1;
}
RocketChat.models.Subscriptions.incUserMentionsAndUnreadForRoomIdAndUserIds(room._id, _.compact(_.unique(mentionIds.concat(highlightsIds))), 1, incUnread);
} else if (unreadCount === 'all_messages') {
RocketChat.models.Subscriptions.incUnreadForRoomIdExcludingUserId(room._id, message.u._id);
}
}

Expand Down
19 changes: 11 additions & 8 deletions packages/rocketchat-lib/server/models/Subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class ModelSubscriptions extends RocketChat.models._Base {
open: true,
alert: false,
unread: 0,
userMentions: 0,
groupMentions: 0,
ls: new Date
}
};
Expand Down Expand Up @@ -336,11 +338,10 @@ class ModelSubscriptions extends RocketChat.models._Base {
return this.update(query, update, { multi: true });
}

incUnreadOfDirectForRoomIdExcludingUserId(roomId, userId, inc) {
incUnreadForRoomIdExcludingUserId(roomId, userId, inc) {
if (inc == null) { inc = 1; }
const query = {
rid: roomId,
t: 'd',
'u._id': {
$ne: userId
}
Expand All @@ -359,8 +360,7 @@ class ModelSubscriptions extends RocketChat.models._Base {
return this.update(query, update, { multi: true });
}

incUnreadForRoomIdExcludingUserId(roomId, userId, inc) {
if (inc == null) { inc = 1; }
incGroupMentionsAndUnreadForRoomIdExcludingUserId(roomId, userId, incGroup = 1, incUnread = 1) {
const query = {
rid: roomId,
'u._id': {
Expand All @@ -374,15 +374,15 @@ class ModelSubscriptions extends RocketChat.models._Base {
open: true
},
$inc: {
unread: inc
unread: incUnread,
groupMentions: incGroup
}
};

return this.update(query, update, { multi: true });
}

incUnreadForRoomIdAndUserIds(roomId, userIds, inc) {
if (inc == null) { inc = 1; }
incUserMentionsAndUnreadForRoomIdAndUserIds(roomId, userIds, incUser = 1, incUnread = 1) {
const query = {
rid: roomId,
'u._id': {
Expand All @@ -396,7 +396,8 @@ class ModelSubscriptions extends RocketChat.models._Base {
open: true
},
$inc: {
unread: inc
unread: incUnread,
userMentions: incUser
}
};

Expand Down Expand Up @@ -537,6 +538,8 @@ class ModelSubscriptions extends RocketChat.models._Base {
open: false,
alert: false,
unread: 0,
userMentions: 0,
groupMentions: 0,
ts: room.ts,
rid: room._id,
name: room.name,
Expand Down
19 changes: 19 additions & 0 deletions packages/rocketchat-lib/server/startup/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,25 @@ RocketChat.settings.addGroup('General', function() {
type: 'boolean',
'public': true
});
this.add('Unread_Count', 'user_and_group_mentions_only', {
type: 'select',
values: [
{
key: 'all_messages',
i18nLabel: 'All_messages'
}, {
key: 'user_mentions_only',
i18nLabel: 'User_mentions_only'
}, {
key: 'group_mentions_only',
i18nLabel: 'Group_mentions_only'
}, {
key: 'user_and_group_mentions_only',
i18nLabel: 'User_and_group_mentions_only'
}
],
'public': true
});
this.add('CDN_PREFIX', '', {
type: 'string',
'public': true
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ RocketChat.Livechat = {
alert: true,
open: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
code: room.code,
u: {
_id: agent.agentId,
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-livechat/server/lib/QueueMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ RocketChat.QueueMethods = {
alert: true,
open: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
code: roomCode,
u: {
_id: agent.agentId,
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-livechat/server/methods/takeInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Meteor.methods({
alert: true,
open: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
code: inquiry.code,
u: {
_id: agent.agentId,
Expand Down
4 changes: 3 additions & 1 deletion server/methods/addAllUserToRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Meteor.methods({
ts: now,
open: true,
alert: true,
unread: 1
unread: 1,
userMentions: 1,
groupMentions: 0
});
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(rid, user, {
ts: now
Expand Down
4 changes: 4 additions & 0 deletions server/methods/createDirectMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Meteor.methods({
t: 'd',
alert: false,
unread: 0,
userMentions: 0,
groupMentions: 0,
u: {
_id: me._id,
username: me.username
Expand All @@ -92,6 +94,8 @@ Meteor.methods({
open: false,
alert: false,
unread: 0,
userMentions: 0,
groupMentions: 0,
u: {
_id: to._id,
username: to.username
Expand Down
2 changes: 2 additions & 0 deletions server/publications/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const fields = {
alert: 1,
roles: 1,
unread: 1,
userMentions: 1,
groupMentions: 1,
archived: 1,
audioNotification: 1,
desktopNotifications: 1,
Expand Down

0 comments on commit 1f3fc29

Please sign in to comment.