Skip to content

Commit

Permalink
Fix RocketChat#329. Check for duplicate name when creating
Browse files Browse the repository at this point in the history
private group and channel.  Check for illegal name for private group.

- Use il8n for error messages
- Add il8n english error message for duplicate channel/private group
  name
- Add illegal name serverside check to createPrivateGroup method
- Add logic to handle different error types on privateGroupsFlex page
  • Loading branch information
rwakida committed Jul 24, 2015
1 parent 8ffddb1 commit 3fe20c6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions client/views/app/sideNav/createChannelFlex.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ Template.createChannelFlex.events
if err.error is 'name-invalid'
instance.error.set({ invalid: true })
return
if err.error is 'duplicate-name'
instance.error.set({ duplicate: true })
return
else
return toastr.error err.reason

Expand Down
6 changes: 6 additions & 0 deletions client/views/app/sideNav/createChannelFlex.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ <h4>{{_ "Create_new_public_channel" }}</h4>
{{{_ "Invalid_room_name" roomName}}}
</div>
{{/if}}
{{#if error.duplicate}}
<div class="input-error">
<strong>{{_ "Oops!"}}</strong>
{{{_ "Duplicate_channel_name" roomName}}}
</div>
{{/if}}
<div class="input-submit">
<button class="button clean primary save-channel">{{_ "Save" }}</button>
<button class="button clean cancel-channel">{{_ "Cancel" }}</button>
Expand Down
16 changes: 15 additions & 1 deletion client/views/app/sideNav/privateGroupsFlex.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Template.privateGroupsFlex.helpers
name: ->
return Template.instance().selectedUserNames[this.valueOf()]

groupName: ->
return Template.instance().groupName.get()

error: ->
return Template.instance().error.get()

Expand Down Expand Up @@ -68,24 +71,35 @@ Template.privateGroupsFlex.events

'click .save-pvt-group': (e, instance) ->
err = SideNav.validate()
instance.groupName.set instance.find('#pvt-group-name').value
console.log err
if not err
Meteor.call 'createPrivateGroup', instance.find('#pvt-group-name').value, instance.selectedUsers.get(), (err, result) ->
if err
console.log err
if err.error is 'name-invalid'
instance.error.set({ invalid: true })
return
if err.error is 'duplicate-name'
instance.error.set({ duplicate: true })
return
return toastr.error err.reason
SideNav.closeFlex()
instance.clearForm()
FlowRouter.go 'room', { _id: result.rid }
else
Template.instance().error.set(err)
Template.instance().error.set({fields: err})

Template.privateGroupsFlex.onCreated ->
instance = this
instance.selectedUsers = new ReactiveVar []
instance.selectedUserNames = {}
instance.error = new ReactiveVar []
instance.groupName = new ReactiveVar ''

instance.clearForm = ->
instance.error.set([])
instance.groupName.set('')
instance.selectedUsers.set([])
instance.find('#pvt-group-name').value = ''
instance.find('#pvt-group-members').value = ''
20 changes: 16 additions & 4 deletions client/views/app/sideNav/privateGroupsFlex.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@ <h4>{{_ "Create_new_private_group"}}</h4>
{{/each}}
</ul>
</div>
{{#if error}}
{{#if error.fields}}
<div class="input-error">
<strong>Ops!</strong>
{{#each error}}
<p>{{_ "The_field_is_required" error}}</p>
<strong>{{_ "Oops!"}}</strong>
{{#each error.fields}}
<p>{{_ "The_field_is_required" .}}</p>
{{/each}}
</div>
{{/if}}
{{#if error.invalid}}
<div class="input-error">
<strong>{{_ "Oops!"}}</strong>
{{{_ "Invalid_room_name" groupName}}}
</div>
{{/if}}
{{#if error.duplicate}}
<div class="input-error">
<strong>{{_ "Oops!"}}</strong>
{{{_ "Duplicate_private_group_name" groupName}}}
</div>
{{/if}}
<div class="input-submit">
<button class="button clean primary save-pvt-group">{{_ "Save" }}</button>
<button class="button clean cancel-pvt-group">{{_ "Cancel" }}</button>
Expand Down
2 changes: 2 additions & 0 deletions i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"Created_at" : "Created at",
"Direct_Messages" : "Direct Messages",
"Deleted" : "Deleted!",
"Duplicate_private_group_name" : "A Private Group with the name, '%s', exists",
"Duplicate_channel_name" : "A Channel with the name, '%s', exists",
"edited" : "edited",
"Email_or_username" : "Email or username",
"Email_verified" : "Email verified",
Expand Down
2 changes: 1 addition & 1 deletion server/methods/createChannel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Meteor.methods

# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name', "A Channel with the same name exists"
throw new Meteor.Error 'duplicate-name'

# name = s.slugify name

Expand Down
5 changes: 4 additions & 1 deletion server/methods/createPrivateGroup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Meteor.methods

console.log '[methods] createPrivateGroup -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments

if not /^[0-9a-z-_]+$/i.test name
throw new Meteor.Error 'name-invalid'

now = new Date()

me = Meteor.user()
Expand All @@ -15,7 +18,7 @@ Meteor.methods

# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name', "A private group with the same name exists"
throw new Meteor.Error 'duplicate-name'

# create new room
rid = ChatRoom.insert
Expand Down

0 comments on commit 3fe20c6

Please sign in to comment.