Skip to content

Commit

Permalink
Merge pull request #10257 from RocketChat/fix-rest-channels-notificat…
Browse files Browse the repository at this point in the history
…ions

Fix: Renaming channels.notifications Get/Post endpoints
  • Loading branch information
rodrigok committed Mar 29, 2018
2 parents 4de5294 + 4021c1d commit be482c5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 79 deletions.
39 changes: 0 additions & 39 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,42 +847,3 @@ RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequire
}
});

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.findOneByRoomIdAndUserId(roomId, this.userId, {
fields: {
_room: 0,
_user: 0,
$loki: 0
}
});

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);
}
});
23 changes: 23 additions & 0 deletions packages/rocketchat-api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@ RocketChat.API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, {
return RocketChat.API.v1.success();
}
});

RocketChat.API.v1.addRoute('rooms.saveNotification', { authRequired: true }, {
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);

return RocketChat.API.v1.success();
}
});
22 changes: 22 additions & 0 deletions packages/rocketchat-api/server/v1/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ RocketChat.API.v1.addRoute('subscriptions.get', { authRequired: true }, {
}
});

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

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

const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId, {
fields: {
_room: 0,
_user: 0,
$loki: 0
}
});

return RocketChat.API.v1.success({
subscription
});
}
});

/**
This API is suppose to mark any room as read.
Expand Down
38 changes: 0 additions & 38 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,44 +505,6 @@ 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
39 changes: 38 additions & 1 deletion tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
/* globals expect */

import {getCredentials, api, request, credentials } from '../../data/api-data.js';
import { getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('[Rooms]', function() {
this.retries(0);
Expand Down Expand Up @@ -34,4 +34,41 @@ describe('[Rooms]', function() {
})
.end(done);
});

describe('/rooms.saveNotification:', () => {
let testChannel;
it('create an channel', (done) => {
request.post(api('channels.create'))
.set(credentials)
.send({
name: `channel.test.${ Date.now() }`
})
.end((err, res) => {
testChannel = res.body.channel;
done();
});
});
it('/rooms.saveNotification:', (done) => {
request.post(api('rooms.saveNotification'))
.set(credentials)
.send({
roomId: testChannel._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);
});
});
});
31 changes: 30 additions & 1 deletion tests/end-to-end/api/10-subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
/* globals expect */

import {getCredentials, api, request, credentials } from '../../data/api-data.js';
import { getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('[Subscriptions]', function() {
this.retries(0);
Expand Down Expand Up @@ -36,6 +36,35 @@ describe('[Subscriptions]', function() {
.end(done);
});

it('/subscriptions.getOne:', () => {
let testChannel;
it('create an channel', (done) => {
request.post(api('channels.create'))
.set(credentials)
.send({
name: `channel.test.${ Date.now() }`
})
.end((err, res) => {
testChannel = res.body.channel;
done();
});
});
it('subscriptions.getOne', (done) => {
request.get(api('subscriptions.getOne'))
.set(credentials)
.query({
roomId: testChannel._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);
});
});

describe('[/subscriptions.read]', () => {
it('should mark public channels as read', (done) => {
request.post(api('subscriptions.read'))
Expand Down

0 comments on commit be482c5

Please sign in to comment.