Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Added GET/POST channels.notifications #10128

Merged
merged 3 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,37 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, {
return RocketChat.API.v1.success();
}
});

RocketChat.API.v1.addRoute('channels.notifications', { authRequired: true }, {
get() {
const { roomId } = this.requestParams();

if (!roomId) {
return RocketChat.API.v1.failure('The \'roomId\' param is required');
}

const subscription = RocketChat.models.Subscriptions.findOne({}, {fields: {_room: 0, _user: 0, $loki: 0}});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong find, please revert to the old one


return RocketChat.API.v1.success({
subscription
});
},
post() {
const saveNotifications = (notifications, roomId) => {
Object.keys(notifications).map((notificationKey) => {
Meteor.runAsUser(this.userId, () => Meteor.call('saveNotificationSettings', roomId, notificationKey, notifications[notificationKey]));
});
};
const { roomId, notifications } = this.bodyParams;

if (!roomId) {
return RocketChat.API.v1.failure('The \'roomId\' param is required');
}

if (!notifications || Object.keys(notifications).length === 0) {
return RocketChat.API.v1.failure('The \'notifications\' param is required');
}

saveNotifications(notifications, roomId);
}
});
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
Meteor.methods({
saveNotificationSettings(rid, field, value) {
saveNotificationSettings(roomId, field, value) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'saveNotificationSettings' });
}

check(rid, String);
check(roomId, String);
check(field, String);
check(value, String);

if (['audioNotifications', 'desktopNotifications', 'mobilePushNotifications', 'emailNotifications', 'unreadAlert', 'disableNotifications', 'hideUnreadStatus'].indexOf(field) === -1) {
const notifications = {
'audioNotifications': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateAudioNotificationsById(subscription._id, value)
},
'desktopNotifications': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateDesktopNotificationsById(subscription._id, value)
},
'mobilePushNotifications': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateMobilePushNotificationsById(subscription._id, value)
},
'emailNotifications': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, value)
},
'unreadAlert': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateUnreadAlertById(subscription._id, value)
},
'disableNotifications': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateDisableNotificationsById(subscription._id, value === '1')
},
'hideUnreadStatus': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateHideUnreadStatusById(subscription._id, value === '1')
},
'desktopNotificationDuration': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateDesktopNotificationDurationById(subscription._id, value)
},
'audioNotificationValue': {
updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateAudioNotificationValueById(subscription._id, value)
}
};
const isInvalidNotification = !Object.keys(notifications).includes(field);
const basicValuesForNotifications = ['all', 'mentions', 'nothing', 'default'];
const fieldsMustHaveBasicValues = ['emailNotifications', 'audioNotifications', 'mobilePushNotifications', 'desktopNotifications'];

if (isInvalidNotification) {
throw new Meteor.Error('error-invalid-settings', 'Invalid settings field', { method: 'saveNotificationSettings' });
}

if (field !== 'hideUnreadStatus' && field !== 'disableNotifications' && ['all', 'mentions', 'nothing', 'default'].indexOf(value) === -1) {
if (fieldsMustHaveBasicValues.includes(field) && !basicValuesForNotifications.includes(value)) {
throw new Meteor.Error('error-invalid-settings', 'Invalid settings value', { method: 'saveNotificationSettings' });
}

const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId());
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, Meteor.userId());
if (!subscription) {
throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', { method: 'saveNotificationSettings' });
}

switch (field) {
case 'audioNotifications':
RocketChat.models.Subscriptions.updateAudioNotificationsById(subscription._id, value);
break;
case 'desktopNotifications':
RocketChat.models.Subscriptions.updateDesktopNotificationsById(subscription._id, value);
break;
case 'mobilePushNotifications':
RocketChat.models.Subscriptions.updateMobilePushNotificationsById(subscription._id, value);
break;
case 'emailNotifications':
RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, value);
break;
case 'unreadAlert':
RocketChat.models.Subscriptions.updateUnreadAlertById(subscription._id, value);
break;
case 'disableNotifications':
RocketChat.models.Subscriptions.updateDisableNotificationsById(subscription._id, value === '1' ? true : false);
break;
case 'hideUnreadStatus':
RocketChat.models.Subscriptions.updateHideUnreadStatusById(subscription._id, value === '1' ? true : false);
break;
}
notifications[field].updateMethod(subscription, value);

return true;
},
Expand Down
54 changes: 52 additions & 2 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
/* globals expect */
/* eslint no-unused-vars: 0 */

import {getCredentials, api, login, request, credentials, apiEmail, apiUsername, targetUser, log, apiPublicChannelName, channel } from '../../data/api-data.js';
import {adminEmail, password} from '../../data/user.js';
import {
getCredentials,
api,
login,
request,
credentials,
apiEmail,
apiUsername,
targetUser,
log,
apiPublicChannelName,
channel
} from '../../data/api-data.js';
import { adminEmail, password } from '../../data/user.js';
import supertest from 'supertest';

function getRoomInfo(roomId) {
Expand Down Expand Up @@ -477,6 +489,44 @@ describe('[Channels]', function() {
.end(done);
});

it('GET /channels.notifications', (done) => {
request.get(api('channels.notifications'))
.set(credentials)
.query({
roomId: channel._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('subscription').and.to.be.an('object');
})
.end(done);
});

it('POST /channels.notifications', (done) => {
request.post(api('channels.notifications'))
.set(credentials)
.send({
roomId: channel._id,
notifications: {
disableNotifications: '0',
emailNotifications: 'nothing',
audioNotificationValue: 'beep',
desktopNotifications: 'nothing',
desktopNotificationDuration: '2',
audioNotifications: 'all',
mobilePushNotifications: 'mentions'
}
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});

it('/channels.leave', async(done) => {
const roomInfo = await getRoomInfo(channel._id);

Expand Down