Merge pull request #514 from RocketChat/setUsername
Set username as API method; Separate logic for joining default channelspull/515/head
commit
b780e90204
@ -0,0 +1,2 @@ |
||||
RocketChat.checkUsernameAvailability = (username) -> |
||||
return not Meteor.users.findOne({ username: { $regex : new RegExp("^" + _.trim(username) + "$", "i") } }) |
@ -0,0 +1,36 @@ |
||||
RocketChat.setUsername = (user, username) -> |
||||
username = _.trim username |
||||
if not user or not username |
||||
return false |
||||
|
||||
if not /^[0-9a-zA-Z-_.]+$/.test username |
||||
return false |
||||
|
||||
# User already has desired username, return |
||||
if user.username is username |
||||
return user |
||||
|
||||
# Check username availability |
||||
unless RocketChat.checkUsernameAvailability username |
||||
return false |
||||
|
||||
previousUsername = user.username |
||||
|
||||
# Username is available; if coming from old username, update all references |
||||
if previousUsername |
||||
ChatMessage.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true } |
||||
|
||||
ChatMessage.find({ "mentions.username": previousUsername }).forEach (msg) -> |
||||
updatedMsg = msg.msg.replace(new RegExp("@#{previousUsername}", "ig"), "@#{username}") |
||||
ChatMessage.update { _id: msg._id, "mentions.username": previousUsername }, { $set: { "mentions.$.username": username, "msg": updatedMsg } } |
||||
|
||||
ChatRoom.update { usernames: previousUsername }, { $set: { "usernames.$": username } }, { multi: true } |
||||
ChatRoom.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true } |
||||
|
||||
ChatSubscription.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true } |
||||
ChatSubscription.update { name: previousUsername, t: "d" }, { $set: { name: username } }, { multi: true } |
||||
|
||||
# Set new username |
||||
Meteor.users.update { _id: user._id }, { $set: { username: username } } |
||||
user.username = username |
||||
return user |
@ -0,0 +1,41 @@ |
||||
Meteor.methods |
||||
joinDefaultChannels: -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error('invalid-user', "[methods] setUsername -> Invalid user") |
||||
|
||||
console.log '[methods] joinDefaultChannels -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments |
||||
|
||||
user = Meteor.user() |
||||
|
||||
ChatRoom.find({default: true, t: {$in: ['c', 'p']}}).forEach (room) -> |
||||
|
||||
# put user in default rooms |
||||
ChatRoom.update room._id, |
||||
$addToSet: |
||||
usernames: user.username |
||||
|
||||
if not ChatSubscription.findOne(rid: room._id, 'u._id': user._id)? |
||||
|
||||
# Add a subscription to this user |
||||
ChatSubscription.insert |
||||
rid: room._id |
||||
name: room.name |
||||
ts: new Date() |
||||
t: room.t |
||||
f: false |
||||
open: true |
||||
alert: true |
||||
unread: 1 |
||||
u: |
||||
_id: user._id |
||||
username: user.username |
||||
|
||||
# Insert user joined message |
||||
ChatMessage.insert |
||||
rid: room._id |
||||
ts: new Date() |
||||
t: 'uj' |
||||
msg: '' |
||||
u: |
||||
_id: user._id |
||||
username: user.username |
@ -0,0 +1,22 @@ |
||||
Meteor.methods |
||||
setUsername: (username) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error('invalid-user', "[methods] setUsername -> Invalid user") |
||||
|
||||
console.log '[methods] setUsername -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments |
||||
|
||||
user = Meteor.user() |
||||
|
||||
if user.username is username |
||||
return username |
||||
|
||||
if not /^[0-9a-zA-Z-_.]+$/.test username |
||||
throw new Meteor.Error 'username-invalid' |
||||
|
||||
if not RocketChat.checkUsernameAvailability username |
||||
throw new Meteor.Error 'username-unavailable' |
||||
|
||||
unless RocketChat.setUsername user, username |
||||
throw new Meteor.Error 'could-not-change-username', t('Could_not_change_username') |
||||
|
||||
return username |
@ -1,60 +0,0 @@ |
||||
Meteor.methods |
||||
setUsername: (username) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error('invalid-user', "[methods] setUsername -> Invalid user") |
||||
|
||||
console.log '[methods] setUsername -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments |
||||
|
||||
user = Meteor.user() |
||||
|
||||
if user.username? |
||||
throw new Meteor.Error 'username-already-setted' |
||||
|
||||
if not usernameIsAvaliable username |
||||
throw new Meteor.Error 'username-unavaliable' |
||||
|
||||
if not /^[0-9a-zA-Z-_.]+$/.test username |
||||
throw new Meteor.Error 'username-invalid' |
||||
|
||||
if not user.username? |
||||
ChatRoom.find({default: true, t: {$in: ['c', 'p']}}).forEach (room) -> |
||||
# put user in default rooms |
||||
ChatRoom.update room._id, |
||||
$addToSet: |
||||
usernames: username |
||||
|
||||
if not ChatSubscription.findOne(rid: room._id, 'u._id': user._id)? |
||||
ChatSubscription.insert |
||||
rid: room._id |
||||
name: room.name |
||||
ts: new Date() |
||||
t: room.t |
||||
f: false |
||||
open: true |
||||
alert: true |
||||
unread: 1 |
||||
u: |
||||
_id: user._id |
||||
username: username |
||||
|
||||
ChatMessage.insert |
||||
rid: room._id |
||||
ts: new Date() |
||||
t: 'uj' |
||||
msg: '' |
||||
u: |
||||
_id: user._id |
||||
username: username |
||||
|
||||
Meteor.users.update({_id: user._id}, {$set: {username: username}}) |
||||
|
||||
return username |
||||
|
||||
slug = (text) -> |
||||
text = slugify text, '.' |
||||
return text.replace(/[^0-9a-z-_.]/g, '') |
||||
|
||||
usernameIsAvaliable = (username) -> |
||||
if username.length < 1 |
||||
return false |
||||
return not Meteor.users.findOne({username: {$regex : new RegExp(username, "i") }}) |
Loading…
Reference in new issue