-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
Copy pathsetUsername.coffee
62 lines (49 loc) · 1.38 KB
/
setUsername.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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-z-_.]+$/.test username
throw new Meteor.Error 'username-invalid'
if not user.username?
# put user in general channel
ChatRoom.update 'GENERAL',
$push:
usernames:
$each: [username]
$sort: 1
if not ChatSubscription.findOne(rid: 'GENERAL', 'u._id': user._id)?
ChatSubscription.insert
rid: 'GENERAL'
name: 'general'
ts: new Date()
t: 'c'
f: true
open: true
alert: true
unread: 1
u:
_id: user._id
username: username
ChatMessage.insert
rid: 'GENERAL'
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: username})?