Skip to content

Commit

Permalink
Merge pull request #8144 from RocketChat/fix-new-room-sound
Browse files Browse the repository at this point in the history
[FIX] Fix new room sound being played too much
  • Loading branch information
rodrigok committed Sep 16, 2017
2 parents c3b9cb4 + ffb2480 commit 92f8b14
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 46 deletions.
24 changes: 23 additions & 1 deletion client/notifications/notification.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/* globals KonchatNotification, fireGlobalEvent, readMessage */
/* globals KonchatNotification, fireGlobalEvent, readMessage, CachedChatSubscription */

// Show notifications and play a sound for new messages.
// We trust the server to only send notifications for interesting messages, e.g. direct messages or
// group messages in which the user is mentioned.

function notifyNewRoom(sub) {

// Do not play new room sound if user is busy
if (Session.equals(`user_${ Meteor.userId() }_status`, 'busy')) {
return;
}

if ((!FlowRouter.getParam('name') || FlowRouter.getParam('name') !== sub.name) && !sub.ls && sub.alert === true) {
return KonchatNotification.newRoom(sub.rid);
}
}

Meteor.startup(function() {
Tracker.autorun(function() {
if (Meteor.userId()) {
Expand Down Expand Up @@ -55,6 +67,16 @@ Meteor.startup(function() {
KonchatNotification.newMessage(notification.payload.rid);
}
});

CachedChatSubscription.onSyncData = function(action, sub) {
if (action !== 'removed') {
notifyNewRoom(sub);
}
};

RocketChat.Notifications.onUser('subscriptions-changed', (action, sub) => {
notifyNewRoom(sub);
});
}
});
});
10 changes: 9 additions & 1 deletion packages/rocketchat-lib/client/lib/cachedCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class CachedCollection {
useCache = true,
debug = false,
version = 6,
maxCacheTime = 60*60*24*30
maxCacheTime = 60*60*24*30,
onSyncData = (/* action, record */) => {}
}) {
this.collection = collection || new Mongo.Collection(null);

Expand All @@ -116,6 +117,7 @@ class CachedCollection {
this.userRelated = userRelated;
this.updatedAt = new Date(0);
this.maxCacheTime = maxCacheTime;
this.onSyncData = onSyncData;

RocketChat.CachedCollectionManager.register(this);

Expand Down Expand Up @@ -206,6 +208,8 @@ class CachedCollection {
delete record.$loki;
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));

this.onSyncData('changed', record);

if (record._updatedAt && record._updatedAt > this.updatedAt) {
this.updatedAt = record._updatedAt;
}
Expand Down Expand Up @@ -268,12 +272,16 @@ class CachedCollection {
if (record._deletedAt) {
this.collection.remove({ _id: record._id });

this.onSyncData('removed', record);

if (record._deletedAt && record._deletedAt > this.updatedAt) {
this.updatedAt = record._deletedAt;
}
} else {
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));

this.onSyncData('changed', record);

if (record._updatedAt && record._updatedAt > this.updatedAt) {
this.updatedAt = record._updatedAt;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-sidenav/client/accountBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Template.accountBox.helpers({
},

isAnonymous() {
if (Meteor.user() == null && RocketChat.settings.get('Accounts_AllowAnonymousRead')) {
if (Meteor.userId() == null && RocketChat.settings.get('Accounts_AllowAnonymousRead')) {
return 'disabled';
}
}
Expand Down
12 changes: 0 additions & 12 deletions packages/rocketchat-ui-sidenav/client/chatRoomItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* globals KonchatNotification */

Template.chatRoomItem.helpers({
roomData() {
let name = this.name;
Expand All @@ -25,10 +23,6 @@ Template.chatRoomItem.helpers({
if (!this.hideUnreadStatus && (FlowRouter.getParam('_id') !== this.rid || !document.hasFocus()) && this.alert) {
alertClass = 'sidebar-item__link--active';
}
// Sound notification
if (!(FlowRouter.getParam('name') === this.name) && !this.ls && this.alert === true) {
KonchatNotification.newRoom(this.rid);
}

const icon = RocketChat.roomTypes.getIcon(this.t);
const avatar = !icon;
Expand All @@ -47,9 +41,3 @@ Template.chatRoomItem.helpers({
};
}
});

Template.chatRoomItem.onRendered = function() {
if (!(FlowRouter.getParam('name') && (FlowRouter.getParam('name') === this.name)) && !this.ls && (this.alert === true)) {
return KonchatNotification.newRoom(this.rid);
}
};
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-sidenav/client/sideNav.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{> toolbar}}
</header>

{{#if currentUser}}
{{#if loggedInUser}}
<div class="unread-rooms background-primary-action-color color-primary-action-contrast top-unread-rooms hidden">
{{_ "More_unreads"}} <i class="icon-up-big"></i>
</div>
Expand Down
19 changes: 10 additions & 9 deletions packages/rocketchat-ui-sidenav/client/sideNav.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
/* globals menu*/

Template.sideNav.helpers({
hasUnread() {
const user = Meteor.user();
return user && user.settings && user.settings.preferences && user.settings.preferences.roomsListExhibitionMode === 'unread';
},
sortByActivity() {
const user = Meteor.user();
return user && user.settings && user.settings.preferences && user.settings.preferences.roomsListExhibitionMode === 'activity';
},
flexTemplate() {
return SideNav.getFlex().template;
},
Expand All @@ -23,6 +15,10 @@ Template.sideNav.helpers({

roomType() {
return RocketChat.roomTypes.getTypes();
},

loggedInUser() {
return !!Meteor.userId();
}
});

Expand Down Expand Up @@ -69,7 +65,12 @@ Template.sideNav.onCreated(function() {
this.mergedChannels = new ReactiveVar(false);

this.autorun(() => {
const user = Meteor.user();
const user = RocketChat.models.Users.findOne(Meteor.userId(), {
fields: {
'settings.preferences.roomsListExhibitionMode': 1,
'settings.preferences.mergeChannels': 1
}
});
let userPref = null;
if (user && user.settings && user.settings.preferences) {
userPref = user.settings.preferences.roomsListExhibitionMode === 'category' && user.settings.preferences.mergeChannels;
Expand Down
49 changes: 28 additions & 21 deletions packages/rocketchat-ui/client/lib/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,38 @@ const KonchatNotification = {
}
};

Tracker.autorun(function() {
const user = Meteor.user();
const newRoomNotification = user && user.settings && user.settings.preferences && user.settings.preferences.newRoomNotification || 'door';
const audioVolume = user && user.settings && user.settings.preferences && user.settings.preferences.notificationsSoundVolume || 100;
Meteor.startup(() => {
Tracker.autorun(function() {
const user = RocketChat.models.Users.findOne(Meteor.userId(), {
fields: {
'settings.preferences.newRoomNotification': 1,
'settings.preferences.notificationsSoundVolume': 1
}
});
const newRoomNotification = user && user.settings && user.settings.preferences && user.settings.preferences.newRoomNotification || 'door';
const audioVolume = user && user.settings && user.settings.preferences && user.settings.preferences.notificationsSoundVolume || 100;

if ((Session.get('newRoomSound') || []).length > 0) {
Tracker.nonreactive(function() {
if (!Session.equals(`user_${ Meteor.userId() }_status`, 'busy') && newRoomNotification !== 'none') {
const [audio] = $(`audio#${ newRoomNotification }`);
if (audio && audio.play) {
audio.volume = Number((audioVolume/100).toPrecision(2));
return audio.play();
if ((Session.get('newRoomSound') || []).length > 0) {
Meteor.defer(function() {
if (newRoomNotification !== 'none') {
const [audio] = $(`audio#${ newRoomNotification }`);
if (audio && audio.play) {
audio.volume = Number((audioVolume/100).toPrecision(2));
return audio.play();
}
}
});
} else {
const [room] = $(`audio#${ newRoomNotification }`);
if (!room) {
return;
}
if (room.pause) {
room.pause();
return room.currentTime = 0;
}
});
} else {
const [room] = $(`audio#${ newRoomNotification }`);
if (!room) {
return;
}
if (room.pause) {
room.pause();
return room.currentTime = 0;
}
}
});
});
export { KonchatNotification };
this.KonchatNotification = KonchatNotification;

0 comments on commit 92f8b14

Please sign in to comment.