Skip to content

Commit

Permalink
[FIX] Endpoints not working when using "Use Real Name" setting (#26530)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Aug 10, 2022
1 parent 6953c06 commit 9d818a7
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 35 deletions.
14 changes: 4 additions & 10 deletions apps/meteor/app/api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,10 @@ API.v1.addRoute(
projection: fields,
});

const [channels, total] = await Promise.all([
cursor.map((room) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [channels, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
channels,
channels: channels.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
count: channels.length,
offset,
total,
Expand Down Expand Up @@ -492,13 +489,10 @@ API.v1.addRoute(
projection: fields,
});

const [channels, total] = await Promise.all([
cursor.map((room) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [channels, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
channels,
channels: channels.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: channels.length,
total,
Expand Down
14 changes: 4 additions & 10 deletions apps/meteor/app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,10 @@ API.v1.addRoute(
projection: fields,
});

const [groups, total] = await Promise.all([
cursor.map((room) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [groups, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
groups,
groups: groups.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: groups.length,
total,
Expand Down Expand Up @@ -626,13 +623,10 @@ API.v1.addRoute(
projection: fields,
});

const [rooms, total] = await Promise.all([
cursor.map((room) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
groups: rooms,
groups: rooms.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: rooms.length,
total,
Expand Down
16 changes: 5 additions & 11 deletions apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ API.v1.addRoute(

// TODO: CACHE: Add Breaking notice since we removed the query param

const subscriptions = await Subscriptions.find({ 'u._id': this.userId, 't': 'd' })
const subscriptions = await Subscriptions.find({ 'u._id': this.userId, 't': 'd' }, { projection: { rid: 1 } })
.map((item) => item.rid)
.toArray();

Expand All @@ -443,13 +443,10 @@ API.v1.addRoute(
},
);

const [ims, total] = await Promise.all([
cursor.map((room: IRoom) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [ims, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
ims,
ims: ims.map((room: IRoom) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: ims.length,
total,
Expand Down Expand Up @@ -480,13 +477,10 @@ API.v1.addRoute(
},
);

const [rooms, total] = await Promise.all([
cursor.map((room: IRoom) => this.composeRoomWithLastMessage(room, this.userId)).toArray(),
totalCount,
]);
const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
ims: rooms,
ims: rooms.map((room: IRoom) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: rooms.length,
total,
Expand Down
4 changes: 3 additions & 1 deletion apps/meteor/server/startup/serverRunning.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const exitIfNotBypassed = (ignore, errorCode = 1) => {
process.exit(errorCode);
};

const skipMongoDbDeprecationCheck = ['yes', 'true'].includes(String(process.env.SKIP_MONGODEPRECATION_CHECK).toLowerCase());

Meteor.startup(function () {
const { oplogEnabled, mongoVersion, mongoStorageEngine } = getMongoInfo();

Expand Down Expand Up @@ -82,7 +84,7 @@ Meteor.startup(function () {
showSuccessBox('SERVER RUNNING', msg);

// Deprecation
if (!semver.satisfies(semver.coerce(mongoVersion), '>=4.4.0')) {
if (!skipMongoDbDeprecationCheck && !semver.satisfies(semver.coerce(mongoVersion), '>=4.4.0')) {
msg = [
`YOUR CURRENT MONGODB VERSION (${mongoVersion}) IS DEPRECATED.`,
'IT WILL NOT BE SUPPORTED ON ROCKET.CHAT VERSION 6.0.0 AND GREATER,',
Expand Down
88 changes: 88 additions & 0 deletions apps/meteor/tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2013,4 +2013,92 @@ describe('[Channels]', function () {
return expect(channel.usersCount).to.be.equals(3);
});
});

context("Setting: 'Use Real Name': true", () => {
before(async () => {
await updateSetting('UI_Use_Real_Name', true);

await request
.post(api('channels.join'))
.set(credentials)
.send({
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.nested.property('channel._id', channel._id);
});

await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Sample message',
rid: channel._id,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
});
after(async () => {
await updateSetting('UI_Use_Real_Name', false);

await request
.post(api('channels.leave'))
.set(credentials)
.send({
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.nested.property('channel._id', channel._id);
});
});

it('/channels.list', (done) => {
request
.get(api('channels.list'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('channels').and.to.be.an('array');

const retChannel = res.body.channels.find(({ _id }) => _id === channel._id);

expect(retChannel).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});

it('/channels.list.joined', (done) => {
request
.get(api('channels.list.joined'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('channels').and.to.be.an('array');

const retChannel = res.body.channels.find(({ _id }) => _id === channel._id);

expect(retChannel).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});
});
});
88 changes: 85 additions & 3 deletions apps/meteor/tests/end-to-end/api/03-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,13 @@ describe('[Groups]', function () {
request
.get(api('groups.list'))
.set(credentials)
.query({
roomId: group._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('groups').and.to.be.an('array');
})
.end(done);
});
Expand Down Expand Up @@ -1618,4 +1616,88 @@ describe('[Groups]', function () {
.end(done);
});
});

context("Setting: 'Use Real Name': true", () => {
let realNameGroup;

before(async () => {
await updateSetting('UI_Use_Real_Name', true);

await request
.post(api('groups.create'))
.set(credentials)
.send({ name: `group-${Date.now()}` })
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);

realNameGroup = res.body.group;
});

await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Sample message',
rid: realNameGroup._id,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
});
after(async () => {
await updateSetting('UI_Use_Real_Name', false);

await request
.post(api('groups.delete'))
.set(credentials)
.send({ roomId: realNameGroup._id })
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
});

it('/groups.list', (done) => {
request
.get(api('groups.list'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('groups').and.to.be.an('array');

const retGroup = res.body.groups.find(({ _id }) => _id === realNameGroup._id);

expect(retGroup).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});

it('/groups.listAll', (done) => {
request
.get(api('groups.listAll'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('groups').and.to.be.an('array');

const retGroup = res.body.groups.find(({ _id }) => _id === realNameGroup._id);

expect(retGroup).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});
});
});
64 changes: 64 additions & 0 deletions apps/meteor/tests/end-to-end/api/04-direct-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,70 @@ describe('[Direct Messages]', function () {
.end(done);
});

context("Setting: 'Use Real Name': true", () => {
before(async () => updateSetting('UI_Use_Real_Name', true));
after(async () => updateSetting('UI_Use_Real_Name', false));

it('/im.list', (done) => {
request
.get(api('im.list'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count', 1);
expect(res.body).to.have.property('total', 1);
expect(res.body).to.have.property('ims').and.to.be.an('array');

const im = res.body.ims[0];

expect(im).to.have.property('_id');
expect(im).to.have.property('t').and.to.be.eq('d');
expect(im).to.have.property('msgs').and.to.be.a('number');
expect(im).to.have.property('usernames').and.to.be.an('array');
expect(im).to.have.property('lm');
expect(im).to.have.property('_updatedAt');
expect(im).to.have.property('ts');
expect(im).to.have.property('lastMessage');

const { lastMessage } = im;

expect(lastMessage).to.have.nested.property('u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});

it('/im.list.everyone', (done) => {
request
.get(api('im.list.everyone'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count', 1);
expect(res.body).to.have.property('total', 1);
expect(res.body).to.have.property('ims').and.to.be.an('array');
const im = res.body.ims[0];
expect(im).to.have.property('_id');
expect(im).to.have.property('t').and.to.be.eq('d');
expect(im).to.have.property('msgs').and.to.be.a('number');
expect(im).to.have.property('usernames').and.to.be.an('array');
expect(im).to.have.property('ro');
expect(im).to.have.property('sysMes');
expect(im).to.have.property('_updatedAt');
expect(im).to.have.property('ts');
expect(im).to.have.property('lastMessage');

const { lastMessage } = im;

expect(lastMessage).to.have.nested.property('u.name', 'RocketChat Internal Admin Test');
})
.end(done);
});
});

it('/im.open', (done) => {
request
.post(api('im.open'))
Expand Down

0 comments on commit 9d818a7

Please sign in to comment.