Skip to content

Commit

Permalink
Channel settings: change name, topic, type
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloschmidt committed Nov 16, 2015
1 parent 6c253c3 commit 1a514fb
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 103 deletions.
26 changes: 0 additions & 26 deletions client/methods/saveRoomName.coffee

This file was deleted.

22 changes: 5 additions & 17 deletions client/views/app/room.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Template.room.helpers
else
return roomData.name

roomTopic: ->
roomData = Session.get('roomData' + this._id)
return '' unless roomData
return roomData.topic

roomIcon: ->
roomData = Session.get('roomData' + this._id)
return '' unless roomData?.t
Expand Down Expand Up @@ -114,26 +119,9 @@ Template.room.helpers
return '' unless roomData
return roomData.t is 'c'

canEditName: ->
roomData = Session.get('roomData' + this._id)
return '' unless roomData
if roomData.t in ['c', 'p']
return RocketChat.authz.hasAtLeastOnePermission('edit-room', this._id)
else
return ''

canDirectMessage: ->
return Meteor.user()?.username isnt this.username

roomNameEdit: ->
return Session.get('roomData' + this._id)?.name

editingTitle: ->
return 'hidden' if Session.get('editRoomTitle')

showEditingTitle: ->
return 'hidden' if not Session.get('editRoomTitle')

flexOpened: ->
return 'opened' if RocketChat.TabBar.isFlexOpen()

Expand Down
7 changes: 2 additions & 5 deletions client/views/app/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ <h2>
<a href="#favorite" class="toggle-favorite"><i class="{{favorite}}"></i></a>
{{/if}}
<i class="{{roomIcon}} status-{{userStatus}}"></i>
<span class="room-title {{editingTitle}}">{{roomName}}</span>
{{#if canEditName}}
<input type="text" id="room-title-field" class="{{showEditingTitle}}" value="{{roomNameEdit}}" dir="auto">
<a href="#edit" class="edit-room-title"><i class="icon-pencil" aria-label="{{_ "Edit"}}"></i></a>
{{/if}}
<span class="room-title">{{roomName}}</span>
<span class="room-topic">{{roomTopic}}</span>
</h2>
</header>
<div class="container-bars">
Expand Down
1 change: 1 addition & 0 deletions i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"Invalid_name" : "The name must not be empty",
"Invalid_pass" : "The password must not be empty",
"Invalid_room_name" : "<strong>%s</strong> is not a valid room name,<br/> use only letters, numbers and dashes",
"Invalid_room_type" : "<strong>%s</strong> is not a valid room type.",
"invisible" : "invisible",
"Invisible" : "Invisible",
"Invitation_HTML" : "Invitation HTML",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,55 @@ Template.channelSettings.helpers
return ChatRoom.findOne(@rid)?.t isnt 'd'
roomType: ->
return ChatRoom.findOne(@rid)?.t
roomName: ->
return ChatRoom.findOne(@rid)?.name
roomTopic: ->
return ChatRoom.findOne(@rid)?.topic

Template.channelSettings.events
'click .save': (e, t) ->
e.preventDefault()

settings =
roomType: t.$('input[name=roomType]:checked').val()
roomName: t.$('input[name=roomName]').val()
roomTopic: t.$('input[name=roomTopic]').val()

Meteor.call 'saveRoomSettings', t.data.rid, settings, (err, results) ->
return toastr.error err.reason if err
toastr.success TAPi18n.__ 'Settings_updated'
if t.validate()
Meteor.call 'saveRoomSettings', t.data.rid, settings, (err, results) ->
if err
if err.error in [ 'duplicate-name', 'name-invalid' ]
return toastr.error TAPi18n.__(err.reason, err.details.channelName)
if err.error is 'invalid-room-type'
return toastr.error TAPi18n.__(err.reason, err.details.roomType)
return toastr.error TAPi18n.__(err.reason)

toastr.success TAPi18n.__ 'Settings_updated'

# switch room.t
# when 'c'
# FlowRouter.go 'channel', name: name
# when 'p'
# FlowRouter.go 'group', name: name
Template.channelSettings.onCreated ->
@validateRoomType = =>
type = @$('input[name=roomType]:checked').val()
if type not in ['c', 'p']
toastr.error t('Invalid_room_type', type)
return true

@validateRoomName = =>
rid = Template.currentData()?.rid
room = ChatRoom.findOne rid

if room.u._id isnt Meteor.userId() or room.t not in ['c', 'p']
toastr.error t('Not_allowed')
return false

name = $('input[name=roomName]').val()
if not /^[0-9a-z-_]+$/.test name
toastr.error t('Invalid_room_name', name)
return false

return true

@validateRoomTopic = =>
return true

@validate = =>
return @validateRoomType() and @validateRoomName() and @validateRoomTopic()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ <h2>{{_ "Room_Settings"}}</h2>
<div class="channel-settings scrollable">
<form>
<fieldset>
{{#if notDirect}}
<div class="input-line double-col">
<label>{{_ "Room_Name"}}</label>
<div>
<input type="text" name="roomName" value="{{roomName}}" />
</div>
</div>
{{/if}}
<div class="input-line double-col">
<label>{{_ "Room_Topic"}}</label>
<div>
<input type="text" name="roomTopic" value="{{roomTopic}}" />
</div>
</div>
{{#if notDirect}}
<div class="input-line double-col">
<label>{{_ "Room_Type"}}</label>
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-channel-settings/i18n/en.i18n.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Channel": "Channel",
"Private_Group": "Private Group",
"Room_Name": "Room Name",
"Room_Topic": "Room Topic",
"Room_Type": "Room Type",
"Room_Settings": "Room Settings",
"room_changed_privacy": "Room type changed to: <em>__room_type__</em> by <em>__user_by__</em>",
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-channel-settings/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Package.onUse(function(api) {

api.addFiles([
'server/functions/changeRoomType.coffee',
'server/functions/changeRoomTopic.coffee',
'server/methods/saveRoomName.coffee',
'server/methods/saveRoomSettings.coffee',
'server/models/Messages.coffee'
], 'server');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RocketChat.changeRoomTopic = (rid, roomTopic) ->
unless Match.test rid, String
throw new Meteor.Error 'invalid-rid'

console.log '[function] RocketChat.changeRoomTopic'.green, rid, roomTopic
return RocketChat.models.Rooms.setTopicById(rid, roomTopic)
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,6 @@ RocketChat.changeRoomType = (rid, roomType) ->
throw new Meteor.Error 'invalid-rid'

if roomType not in ['c', 'p']
throw new Meteor.Error 'invalid-room-type'
throw new Meteor.Error 'invalid-room-type', 'Invalid_room_type', { roomType: roomType }

return RocketChat.models.Rooms.setTypeById(rid, roomType) and RocketChat.models.Subscriptions.updateTypeByRoomId(rid, roomType)


# username = s.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
# RocketChat.models.Messages.updateAllUsernamesByUserId user._id, username

# RocketChat.models.Messages.findByMention(previousUsername).forEach (msg) ->
# updatedMsg = msg.msg.replace(new RegExp("@#{previousUsername}", "ig"), "@#{username}")
# RocketChat.models.Messages.updateUsernameAndMessageOfMentionByIdAndOldUsername msg._id, previousUsername, username, updatedMsg

# RocketChat.models.Rooms.replaceUsername previousUsername, username
# RocketChat.models.Rooms.replaceUsernameOfUserByUserId user._id, username

# RocketChat.models.Subscriptions.setUserUsernameByUserId user._id, username
# RocketChat.models.Subscriptions.setNameForDirectRoomsWithOldName previousUsername, username

# # Set new username
# Meteor.users.update { _id: user._id }, { $set: { username: username } }
# user.username = username
# return user
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Meteor.methods
throw new Meteor.Error 403, 'Not allowed'

if not /^[0-9a-z-_]+$/.test name
throw new Meteor.Error 'name-invalid'
throw new Meteor.Error 'name-invalid', 'Invalid_room_name', { channelName: name }

name = _.slugify name

Expand All @@ -22,7 +22,7 @@ Meteor.methods

# avoid duplicate names
if RocketChat.models.Rooms.findOneByName name
throw new Meteor.Error 'duplicate-name'
throw new Meteor.Error 'duplicate-name', 'Duplicate_channel_name', { channelName: name }

RocketChat.models.Rooms.setNameById rid, name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Meteor.methods
console.log '[method] saveRoomSettings'.green, rid, settings

unless Match.test rid, String
throw new Meteor.Error 'invalid-rid'
throw new Meteor.Error 'invalid-rid', 'Invalid room'

unless Match.test settings, Match.ObjectIncluding { roomType: String }
throw new Meteor.Error 'invalid-settings'
unless Match.test settings, Match.ObjectIncluding { roomName: String, roomTopic: String, roomType: String }
throw new Meteor.Error 'invalid-settings', 'Invalid settings provided'

unless RocketChat.authz.hasPermission(Meteor.userId(), 'edit-room', rid)
throw new Meteor.Error 503, 'Not authorized'
Expand All @@ -23,4 +23,12 @@ Meteor.methods

RocketChat.models.Messages.createRoomSettingsChangedWithTypeRoomIdMessageAndUser 'room_changed_privacy', rid, message, Meteor.user()

if settings.roomName isnt room.name
name = Meteor.call 'saveRoomName', rid, settings.roomName

if settings.roomTopic isnt room.topic
RocketChat.changeRoomTopic(rid, settings.roomTopic)
message = settings.roomTopic
RocketChat.models.Messages.createRoomSettingsChangedWithTypeRoomIdMessageAndUser 'room_changed_topic', rid, message, Meteor.user()

return true
16 changes: 13 additions & 3 deletions packages/rocketchat-lib/server/models/Rooms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,25 @@ RocketChat.models.Rooms = new class extends RocketChat.models._Base
return @update query, update

setTypeById: (_id, type) ->
query =
query =
_id: _id

update =
$set:
update =
$set:
t: type

return @update query, update

setTopicById: (_id, topic) ->
query =
_id: _id

update =
$set:
topic: topic

return @update query, update


# INSERT
createWithTypeNameUserAndUsernames: (type, name, user, usernames, extraData) ->
Expand Down
6 changes: 5 additions & 1 deletion packages/rocketchat-theme/assets/stylesheets/base.less
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,11 @@ a.github-fork {
height: 100%;
top: 0;
left: 0;

.room-topic {
font-size: 14px;
opacity: 0.4;
margin-left: 10px;
}
.edit-room-title {
margin-left: 4px;
font-size: 16px;
Expand Down
2 changes: 2 additions & 0 deletions server/startup/roomPublishes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Meteor.startup ->
cl: 1
u: 1
usernames: 1
topic: 1
return RocketChat.models.Rooms.findByTypeAndName 'c', identifier, options

RocketChat.roomTypes.addPublish 'p', (identifier) ->
Expand All @@ -17,6 +18,7 @@ Meteor.startup ->
cl: 1
u: 1
usernames: 1
topic: 1
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
return RocketChat.models.Rooms.findByTypeAndNameContainigUsername 'p', identifier, user.username, options

Expand Down

0 comments on commit 1a514fb

Please sign in to comment.