The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/lib/server/functions/addUserToRoom.js

65 lines
1.6 KiB

import { Meteor } from 'meteor/meteor';
import { Rooms, Subscriptions, Messages } from '../../../models';
import { hasPermission } from '../../../authorization';
import { callbacks } from '../../../callbacks';
export const addUserToRoom = function(rid, user, inviter, silenced) {
const now = new Date();
const room = Rooms.findOneById(rid);
// Check if user is already in room
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
if (subscription) {
return;
}
if (room.t === 'c' || room.t === 'p') {
// Add a new event, with an optional inviter
callbacks.run('beforeAddedToRoom', { user, inviter }, room);
// Keep the current event
callbacks.run('beforeJoinRoom', user, room);
}
const muted = room.ro && !hasPermission(user._id, 'post-readonly');
if (muted) {
Rooms.muteUsernameByRoomId(rid, user.username);
}
Subscriptions.createWithRoomAndUser(room, user, {
ts: now,
open: true,
alert: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
});
if (!silenced) {
if (inviter) {
Messages.createUserAddedWithRoomIdAndUser(rid, user, {
ts: now,
u: {
_id: inviter._id,
username: inviter.username,
},
});
} else if (room.prid) {
Messages.createUserJoinWithRoomIdAndUserDiscussion(rid, user, { ts: now });
} else {
Messages.createUserJoinWithRoomIdAndUser(rid, user, { ts: now });
}
}
if (room.t === 'c' || room.t === 'p') {
Meteor.defer(function() {
// Add a new event, with an optional inviter
callbacks.run('afterAddedToRoom', { user, inviter }, room);
// Keep the current event
callbacks.run('afterJoinRoom', user, room);
});
}
return true;
};