Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RocketChat/Rocket.Chat#329. Check for duplicate name before creating Channel and Private Group. Also, check Private Group for illegal name. #330

Merged
merged 2 commits into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions server/methods/createChannel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Meteor.methods

members.push user.username

# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name'

# name = s.slugify name

room =
Expand Down
7 changes: 7 additions & 0 deletions 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 @@ -13,6 +16,10 @@ Meteor.methods

name = s.slugify name

# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name'

# create new room
rid = ChatRoom.insert
usernames: members
Expand Down