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/server/methods/createChannel.coffee

69 lines
1.5 KiB

Meteor.methods
createChannel: (name, members) ->
if not Meteor.userId()
throw new Meteor.Error 'invalid-user', "[methods] createChannel -> Invalid user"
if not /^[0-9a-z-_]+$/.test name
throw new Meteor.Error 'name-invalid'
if RocketChat.authz.hasPermission(Meteor.userId(), 'create-c') isnt true
throw new Meteor.Error 'not-authorized', '[methods] createChannel -> Not authorized'
console.log '[methods] createChannel -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
now = new Date()
user = Meteor.user()
members.push user.username
# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name'
# name = s.slugify name
room =
usernames: members
ts: now
t: 'c'
name: name
msgs: 0
u:
_id: Meteor.userId()
username: user.username
RocketChat.callbacks.run 'beforeCreateChannel', user, room
# create new room
rid = ChatRoom.insert room
# set creator as channel moderator. permission limited to channel by scoping to rid
RocketChat.authz.addUsersToRoles(Meteor.userId(), 'moderator', rid)
for username in members
member = Meteor.users.findOne({username: username})
if not member?
continue
sub =
rid: rid
ts: now
name: name
t: 'c'
unread: 0
u:
_id: member._id
username: username
if username is user.username
sub.ls = now
sub.open = true
ChatSubscription.insert sub
Meteor.defer ->
RocketChat.callbacks.run 'afterCreateChannel', user, room
return {
rid: rid
}