Skip to content

Commit

Permalink
[FIX] Email notifications settings not being honored on new DMs (#23574)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Sampaio <chinello@gmail.com>
  • Loading branch information
ostjen and sampaiodiego authored Dec 20, 2021
1 parent a2c9396 commit 05951c1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 17 deletions.
43 changes: 26 additions & 17 deletions app/lib/server/functions/createDirectRoom.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';

import { Apps } from '../../../apps/server';
import { callbacks } from '../../../callbacks/server';
import { Rooms, Subscriptions } from '../../../models/server';
import { Rooms } from '../../../models/server';
import { settings } from '../../../settings/server';
import { getDefaultSubscriptionPref } from '../../../utils/server';
import { Users, Subscriptions } from '../../../models/server/raw';

const generateSubscription = (fname, name, user, extra) => ({
_id: Random.id(),
alert: false,
unread: 0,
userMentions: 0,
Expand Down Expand Up @@ -83,26 +86,32 @@ export const createDirectRoom = function(members, roomExtraData = {}, options =
const rid = room?._id || Rooms.insert(roomInfo);

if (members.length === 1) { // dm to yourself
Subscriptions.upsert({ rid, 'u._id': members[0]._id }, {
Subscriptions.updateOne({ rid, 'u._id': members[0]._id }, {
$set: { open: true },
$setOnInsert: generateSubscription(members[0].name || members[0].username, members[0].username, members[0], { ...options.subscriptionExtra }),
});
}, { upsert: true });
} else {
members.forEach((member) => {
const otherMembers = sortedMembers.filter(({ _id }) => _id !== member._id);
const memberIds = members.map((member) => member._id);
const membersWithPreferences = Users.find({ _id: { $in: memberIds } }, { username: 1, 'settings.preferences': 1 });

Subscriptions.upsert({ rid, 'u._id': member._id }, {
...options.creator === member._id && { $set: { open: true } },
$setOnInsert: generateSubscription(
getFname(otherMembers),
getName(otherMembers),
member,
{
...options.subscriptionExtra,
...options.creator !== member._id && { open: members.length > 2 },
},
),
});
membersWithPreferences.forEach((member) => {
const otherMembers = sortedMembers.filter(({ _id }) => _id !== member._id);
Subscriptions.updateOne(
{ rid, 'u._id': member._id },
{
...options.creator === member._id && { $set: { open: true } },
$setOnInsert: generateSubscription(
getFname(otherMembers),
getName(otherMembers),
member,
{
...options.subscriptionExtra,
...options.creator !== member._id && { open: members.length > 2 },
},
),
},
{ upsert: true },
);
});
}

Expand Down
65 changes: 65 additions & 0 deletions tests/end-to-end/api/04-direct-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
directMessage,
apiUsername,
apiEmail,
methodCall,
} from '../../data/api-data.js';
import { password, adminUsername } from '../../data/user.js';
import { deleteRoom } from '../../data/rooms.helper';
Expand Down Expand Up @@ -573,6 +574,70 @@ describe('[Direct Messages]', function() {
.end(done);
});

describe('should create dm with correct notification preferences', () => {
let user;
let userCredentials;
let userPrefRoomId;

before(async () => {
user = await createUser();
userCredentials = await login(user.username, password);
});

after(async () => {
if (userPrefRoomId) {
await deleteRoom({ type: 'd', roomId: userPrefRoomId });
}
await deleteUser(user);
user = undefined;
});

it('should save user preferences', async () => {
await request.post(methodCall('saveUserPreferences'))
.set(userCredentials)
.send({
message: JSON.stringify({
method: 'saveUserPreferences',
params: [{ emailNotificationMode: 'nothing' }],
}),
})
.expect(200);
});

it('should create a DM', (done) => {
request.post(api('im.create'))
.set(userCredentials)
.send({
usernames: [user.username, otherUser.username].join(','),
})
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('room').and.to.be.an('object');
expect(res.body.room).to.have.property('usernames').and.to.have.members([user.username, otherUser.username]);
userPrefRoomId = res.body.room._id;
})
.end(done);
});

it('should return the right user notification preferences in the dm', (done) => {
request.get(api('subscriptions.getOne'))
.set(userCredentials)
.query({
roomId: userPrefRoomId,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('subscription').and.to.be.an('object');
expect(res.body).to.have.nested.property('subscription.emailNotifications').and.to.be.equal('nothing');
})
.end(done);
});
});

async function testRoomFNameForUser(testCredentials, roomId, fullName) {
return request.get(api('subscriptions.getOne'))
.set(testCredentials)
Expand Down

0 comments on commit 05951c1

Please sign in to comment.