Merge pull request #10105 from RocketChat/feature/rest-api-mentions-of-channel

[NEW] Added endpoint to retrieve mentions of a channel
pull/10221/head^2
Rodrigo Nascimento 8 years ago committed by GitHub
commit eee685f962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      packages/rocketchat-api/server/v1/channels.js
  2. 1
      packages/rocketchat-mentions/package.js
  3. 19
      packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js
  4. 20
      tests/end-to-end/api/02-channels.js

@ -814,6 +814,39 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, {
}
});
RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequired: true }, {
get() {
const { roomId } = this.requestParams();
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
if (!roomId) {
return RocketChat.API.v1.failure('The request param "roomId" is required');
}
const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', {
roomId,
options: {
sort: sort ? sort : { ts: 1 },
skip: offset,
limit: count
}
}));
const allMentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', {
roomId,
options: {}
}));
return RocketChat.API.v1.success({
mentions,
count: mentions.length,
offset,
total: allMentions.length
});
}
});
RocketChat.API.v1.addRoute('channels.notifications', { authRequired: true }, {
get() {
const { roomId } = this.requestParams();

@ -12,5 +12,6 @@ Package.onUse(function(api) {
]);
api.addFiles('server/server.js', 'server');
api.addFiles('server/methods/getUserMentionsByChannel.js', 'server');
api.addFiles('client/client.js', 'client');
});

@ -0,0 +1,19 @@
Meteor.methods({
getUserMentionsByChannel({ roomId, options }) {
check(roomId, String);
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getUserMentionsByChannel' });
}
const room = RocketChat.models.Rooms.findOneById(roomId);
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'getUserMentionsByChannel' });
}
const user = RocketChat.models.Users.findOneById(Meteor.userId());
return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId, options).fetch();
}
});

@ -625,4 +625,24 @@ describe('[Channels]', function() {
.end(done);
});
});
describe('/channels.getAllUserMentionsByChannel', () => {
it('should return and array of mentions by channel', (done) => {
request.get(api('channels.getAllUserMentionsByChannel'))
.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('mentions').and.to.be.an('array');
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
})
.end(done);
});
});
});

Loading…
Cancel
Save