From f83cb3489dd316f25cd23c3a168625419200b685 Mon Sep 17 00:00:00 2001 From: Marcos Spessatto Defendi Date: Fri, 18 May 2018 16:54:24 -0300 Subject: [PATCH] Add REST endpoint to mark messages as unread (#10778) [NEW] Add REST endpoint `subscriptions.unread` to mark messages as unread --- .../rocketchat-api/server/v1/subscriptions.js | 16 +++++ tests/end-to-end/api/10-subscriptions.js | 70 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/packages/rocketchat-api/server/v1/subscriptions.js b/packages/rocketchat-api/server/v1/subscriptions.js index 6b17cf66e820..e58fe4094a26 100644 --- a/packages/rocketchat-api/server/v1/subscriptions.js +++ b/packages/rocketchat-api/server/v1/subscriptions.js @@ -69,3 +69,19 @@ RocketChat.API.v1.addRoute('subscriptions.read', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('subscriptions.unread', { authRequired: true }, { + post() { + const { roomId, firstUnreadMessage } = this.bodyParams; + if (!roomId && (firstUnreadMessage && !firstUnreadMessage._id)) { + return RocketChat.API.v1.failure('At least one of "roomId" or "firstUnreadMessage._id" params is required'); + } + + Meteor.runAsUser(this.userId, () => + Meteor.call('unreadMessages', firstUnreadMessage, roomId) + ); + + return RocketChat.API.v1.success(); + } +}); + + diff --git a/tests/end-to-end/api/10-subscriptions.js b/tests/end-to-end/api/10-subscriptions.js index 24a67ca551b4..3e0d71af6efd 100644 --- a/tests/end-to-end/api/10-subscriptions.js +++ b/tests/end-to-end/api/10-subscriptions.js @@ -131,4 +131,74 @@ describe('[Subscriptions]', function() { .end(done); }); }); + + describe('[/subscriptions.unread]', () => { + 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('sending message', (done) => { + request.post(api('chat.sendMessage')) + .set(credentials) + .send({ + message: { + rid: testChannel._id, + msg: 'Sample message' + } + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('message').and.to.be.an('object'); + }) + .end(done); + }); + it('should return success: true when make as unread successfully', (done) => { + request.post(api('subscriptions.unread')) + .set(credentials) + .send({ + roomId: testChannel._id + }) + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('should fail on invalid params', (done) => { + request.post(api('subscriptions.unread')) + .set(credentials) + .send({ + roomId: 12345 + }) + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error'); + }) + .end(done); + }); + + it('should fail on empty params', (done) => { + request.post(api('subscriptions.unread')) + .set(credentials) + .send({}) + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error'); + }) + .end(done); + }); + }); });