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

Fix: Renaming channels.notifications Get/Post endpoints #10257

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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 }, {
Copy link
Member

Choose a reason for hiding this comment

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

I know this is just for renaming endpoints, but since I was not able to comment on the old PR I'll do it now =)

what is the difference between subscriptions.getOne and subscriptions.get? IMO they should be the same accepting different queries.

Copy link
Contributor

Choose a reason for hiding this comment

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

One calls the method and one calls directly from the database. I haven't had a chance to look into what exactly that method does, so no clue if there is a difference.

Copy link
Contributor

Choose a reason for hiding this comment

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

@sampaiodiego sadly, I'm going to have to say that we should keep the calls separated. The main reason for this is because the subscriptions.get returns an object with two properties that are arrays. If we switch to allow support for getting only one, either we would have to change the response format for it to make sense or use the current format which doesn't make sense when returning/getting only one. So, keeping the subscriptions.getOne makes sense for the data being returned. What's your thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

yep.. I've tested this and saw the payload differences as well.. 😞 agreed keeping both separated

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