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] Rename method to clean history of messages #10498

Merged
merged 2 commits into from
Apr 20, 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
5 changes: 4 additions & 1 deletion packages/rocketchat-api/server/helpers/deprecationWarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ RocketChat.API.helperMethods.set('deprecationWarning', function _deprecationWarn
const warningMessage = `The endpoint "${ endpoint }" is deprecated and will be removed after version ${ versionWillBeRemove }`;
console.warn(warningMessage);
if (process.env.NODE_ENV === 'development') {
response.warning = warningMessage;
return {
warning: warningMessage,
...response
};
}

return response;
Expand Down
15 changes: 13 additions & 2 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, {
}
});

/**
DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.67 this should be gone.
**/
RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ params: this.requestParams() });
Expand All @@ -107,7 +111,10 @@ RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, {
Meteor.call('cleanChannelHistory', { roomId: findResult._id, latest, oldest, inclusive });
});

return RocketChat.API.v1.success();
return RocketChat.API.v1.success(this.deprecationWarning({
endpoint: 'channels.cleanHistory',
versionWillBeRemove: 'v0.67'
}));
}
});

Expand Down Expand Up @@ -519,7 +526,11 @@ RocketChat.API.v1.addRoute('channels.members', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.messages', { authRequired: true }, {
get() {
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false, returnUsernames: true });
const findResult = findChannelByIdOrName({
params: this.requestParams(),
checkedArchived: false,
returnUsernames: true
});
const { offset, count } = this.getPaginationItems();
const { sort, fields, query } = this.parseJsonQuery();

Expand Down
28 changes: 28 additions & 0 deletions packages/rocketchat-api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,31 @@ RocketChat.API.v1.addRoute('rooms.favorite', { authRequired: true }, {
}
});

RocketChat.API.v1.addRoute('rooms.cleanHistory', { authRequired: true }, {
post() {
const findResult = findRoomByIdOrName({ params: this.bodyParams });

if (!this.bodyParams.latest) {
return RocketChat.API.v1.failure('Body parameter "latest" is required.');
}

if (!this.bodyParams.oldest) {
return RocketChat.API.v1.failure('Body parameter "oldest" is required.');
}

const latest = new Date(this.bodyParams.latest);
const oldest = new Date(this.bodyParams.oldest);

let inclusive = false;
if (typeof this.bodyParams.inclusive !== 'undefined') {
inclusive = this.bodyParams.inclusive;
}

Meteor.runAsUser(this.userId, () => {
Meteor.call('cleanRoomHistory', { roomId: findResult._id, latest, oldest, inclusive });
});

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

1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Package.onUse(function(api) {
api.addFiles('server/methods/checkRegistrationSecretURL.js', 'server');
api.addFiles('server/methods/checkUsernameAvailability.js', 'server');
api.addFiles('server/methods/cleanChannelHistory.js', 'server');
api.addFiles('server/methods/cleanRoomHistory.js', 'server');
api.addFiles('server/methods/createChannel.js', 'server');
api.addFiles('server/methods/createToken.js', 'server');
api.addFiles('server/methods/createPrivateGroup.js', 'server');
Expand Down
38 changes: 7 additions & 31 deletions packages/rocketchat-lib/server/methods/cleanChannelHistory.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
Meteor.methods({
cleanChannelHistory({roomId, latest, oldest, inclusive}) {
check(roomId, String);
check(latest, Date);
check(oldest, Date);
check(inclusive, Boolean);

if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'cleanChannelHistory' });
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'clean-channel-history')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'cleanChannelHistory' });
}

if (inclusive) {
RocketChat.models.Messages.remove({
rid: roomId,
ts: {
$gte: oldest,
$lte: latest
}
});
} else {
RocketChat.models.Messages.remove({
rid: roomId,
ts: {
$gt: oldest,
$lt: latest
}
});
}
/**
DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.67 this should be gone.
*/
cleanChannelHistory({ roomId, latest, oldest, inclusive }) {
console.warn('The method "cleanChannelHistory" is deprecated and will be removed after version 0.67, please use "cleanRoomHistory" instead');
Meteor.call('cleanRoomHistory', { roomId, latest, oldest, inclusive });
}
});
34 changes: 34 additions & 0 deletions packages/rocketchat-lib/server/methods/cleanRoomHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Meteor.methods({
cleanRoomHistory({ roomId, latest, oldest, inclusive }) {
check(roomId, String);
check(latest, Date);
check(oldest, Date);
check(inclusive, Boolean);

if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'cleanRoomHistory' });
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'clean-channel-history')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'cleanRoomHistory' });
}

if (inclusive) {
RocketChat.models.Messages.remove({
rid: roomId,
ts: {
$gte: oldest,
$lte: latest
}
});
} else {
RocketChat.models.Messages.remove({
rid: roomId,
ts: {
$gt: oldest,
$lt: latest
}
});
}
}
});
2 changes: 2 additions & 0 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ describe('[Channels]', function() {
.end(done);
});

//DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.67 this should be gone.
it('/channels.cleanHistory', (done) => {
request.post(api('channels.cleanHistory'))
.set(credentials)
Expand Down
142 changes: 141 additions & 1 deletion tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* 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';
import { password } from '../../data/user';

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

describe('[/rooms.cleanHistory]', () => {
let publicChannel;
let privateChannel;
let directMessageChannel;
let user;
beforeEach((done) => {
const username = `user.test.${ Date.now() }`;
const email = `${ username }@rocket.chat`;
request.post(api('users.create'))
.set(credentials)
.send({ email, name: username, username, password })
.end((err, res) => {
user = res.body.user;
done();
});
});

let userCredentials;
beforeEach((done) => {
request.post(api('login'))
.send({
user: user.username,
password
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
userCredentials = {};
userCredentials['X-Auth-Token'] = res.body.data.authToken;
userCredentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
afterEach(done => {
request.post(api('users.delete')).set(credentials).send({
userId: user._id
}).end(done);
user = undefined;
});
it('create a public channel', (done) => {
request.post(api('channels.create'))
.set(credentials)
.send({
name: `testeChannel${ +new Date() }`
})
.end((err, res) => {
publicChannel = res.body.channel;
done();
});
});
it('create a private channel', (done) => {
request.post(api('groups.create'))
.set(credentials)
.send({
name: `testPrivateChannel${ +new Date() }`
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
privateChannel = res.body.group;
})
.end(done);
});
it('create a direct message', (done) => {
request.post(api('im.create'))
.set(credentials)
.send({
username: 'rocket.cat'
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
directMessageChannel = res.body.room;
})
.end(done);
});
it('should return success when send a valid public channel', (done) => {
request.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: publicChannel._id,
latest: '2016-12-09T13:42:25.304Z',
oldest: '2016-08-30T13:42:25.304Z'
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return success when send a valid private channel', (done) => {
request.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: privateChannel._id,
latest: '2016-12-09T13:42:25.304Z',
oldest: '2016-08-30T13:42:25.304Z'
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return success when send a valid Direct Message channel', (done) => {
request.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: directMessageChannel._id,
latest: '2016-12-09T13:42:25.304Z',
oldest: '2016-08-30T13:42:25.304Z'
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return not allowed error when try deleting messages with user without permission', (done) => {
request.post(api('rooms.cleanHistory'))
.set(userCredentials)
.send({
roomId: directMessageChannel._id,
latest: '2016-12-09T13:42:25.304Z',
oldest: '2016-08-30T13:42:25.304Z'
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'error-not-allowed');
})
.end(done);
});
});
});