Skip to content

Commit

Permalink
Add REST endpoint to mark messages as unread (#10778)
Browse files Browse the repository at this point in the history
[NEW] Add REST endpoint `subscriptions.unread` to mark messages as unread
  • Loading branch information
MarcosSpessatto authored and rodrigok committed May 18, 2018
1 parent 8160a5f commit f83cb34
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/rocketchat-api/server/v1/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});


70 changes: 70 additions & 0 deletions tests/end-to-end/api/10-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit f83cb34

Please sign in to comment.