Skip to content
Snippets Groups Projects
Commit a8150c79 authored by Marcelo Schmidt's avatar Marcelo Schmidt
Browse files

Add/Remove Room Moderators

parent 8da72776
No related branches found
No related tags found
No related merge requests found
Showing
with 235 additions and 18 deletions
...@@ -314,6 +314,7 @@ ...@@ -314,6 +314,7 @@
"More_unreads" : "More unreads", "More_unreads" : "More unreads",
"Msgs" : "Msgs", "Msgs" : "Msgs",
"multi" : "multi", "multi" : "multi",
"Muted" : "Muted",
"Mute_user" : "Mute user", "Mute_user" : "Mute user",
"My_Account" : "My Account", "My_Account" : "My Account",
"n_messages" : "%s messages", "n_messages" : "%s messages",
...@@ -398,7 +399,9 @@ ...@@ -398,7 +399,9 @@
"Registration_Succeeded" : "Registration Succeeded", "Registration_Succeeded" : "Registration Succeeded",
"Remember_me" : "Remember me", "Remember_me" : "Remember me",
"Remove" : "Remove", "Remove" : "Remove",
"Removed" : "Removed",
"Remove_Admin" : "Remove Admin", "Remove_Admin" : "Remove Admin",
"Remove_as_moderator" : "Remove as moderator",
"Remove_custom_oauth" : "Remove custom oauth", "Remove_custom_oauth" : "Remove custom oauth",
"Remove_from_room" : "Remove from room", "Remove_from_room" : "Remove from room",
"Reset" : "Reset", "Reset" : "Reset",
...@@ -450,6 +453,7 @@ ...@@ -450,6 +453,7 @@
"Send_your_JSON_payloads_to_this_URL" : "Send your JSON payloads to this URL.", "Send_your_JSON_payloads_to_this_URL" : "Send your JSON payloads to this URL.",
"Settings" : "Settings", "Settings" : "Settings",
"Settings_updated" : "Settings updated", "Settings_updated" : "Settings updated",
"Set_as_moderator" : "Set as moderator",
"Should_be_a_URL_of_an_image" : "Should be a URL of an image.", "Should_be_a_URL_of_an_image" : "Should be a URL of an image.",
"Should_exists_a_user_with_this_username" : "The user must already exist.", "Should_exists_a_user_with_this_username" : "The user must already exist.",
"Showing_archived_results" : "<p>Showing <b>%s</b> archived results</p>", "Showing_archived_results" : "<p>Showing <b>%s</b> archived results</p>",
...@@ -504,6 +508,8 @@ ...@@ -504,6 +508,8 @@
"The_redirectUri_is_required" : "The redirectUri is required", "The_redirectUri_is_required" : "The redirectUri is required",
"The_server_will_restart_in_s_seconds" : "The server will restart in %s seconds", "The_server_will_restart_in_s_seconds" : "The server will restart in %s seconds",
"The_setting_s_is_configured_to_s_and_you_are_accessing_from_s" : "The setting <strong>%s</strong> is configured to <strong>%s</strong> and you are accessing from <strong>%s</strong>!", "The_setting_s_is_configured_to_s_and_you_are_accessing_from_s" : "The setting <strong>%s</strong> is configured to <strong>%s</strong> and you are accessing from <strong>%s</strong>!",
"The_user_wont_be_able_to_type_in_s" : "The user won't be able to type in %s",
"The_user_will_be_removed_from_s" : "The user will be removed from %s",
"There_are_no_integrations" : "There are no integrations", "There_are_no_integrations" : "There are no integrations",
"This_is_a_push_test_messsage" : "This is a push test messsage", "This_is_a_push_test_messsage" : "This is a push test messsage",
"True" : "True", "True" : "True",
...@@ -527,6 +533,8 @@ ...@@ -527,6 +533,8 @@
"User_has_been_activated" : "User has been activated", "User_has_been_activated" : "User has been activated",
"User_has_been_deactivated" : "User has been deactivated", "User_has_been_deactivated" : "User has been deactivated",
"User_has_been_deleted" : "User has been deleted", "User_has_been_deleted" : "User has been deleted",
"User_has_been_muted_in_s" : "User has been muted in %s",
"User_has_been_removed_from_s" : "User has been removed from %s",
"User_Info" : "User Info", "User_Info" : "User Info",
"User_is_no_longer_an_admin" : "User is no longer an admin", "User_is_no_longer_an_admin" : "User is no longer an admin",
"User_is_not_activated" : "User is not activated", "User_is_not_activated" : "User is not activated",
...@@ -544,10 +552,14 @@ ...@@ -544,10 +552,14 @@
"User_or_channel_name" : "User or channel name", "User_or_channel_name" : "User or channel name",
"User_removed_by" : "User <em>__user_removed__</em> removed by <em>__user_by__</em>.", "User_removed_by" : "User <em>__user_removed__</em> removed by <em>__user_by__</em>.",
"User_removed_from_room" : "The user has been removed from the room", "User_removed_from_room" : "The user has been removed from the room",
"User__username__removed_from__room_name__moderators" : "User __username__ removed from __room_name__ moderators",
"User_Settings" : "User Settings", "User_Settings" : "User Settings",
"User_unmuted_by" : "User <em>__user_unmuted__</em> unmuted by <em>__user_by__</em>.", "User_unmuted_by" : "User <em>__user_unmuted__</em> unmuted by <em>__user_by__</em>.",
"User_unmuted_in_room" : "User unmuted in room", "User_unmuted_in_room" : "User unmuted in room",
"User_updated_successfully" : "User updated successfully", "User_updated_successfully" : "User updated successfully",
"User__username__is_now_a_moderator_of__room_name_" : "User __username__ is now a moderator of __room_name__",
"User__username__was_added_as_a_moderator_by__user_by_" : "User <em>__username__</em> was added as a moderator by <em>__user_by__</em>",
"User__username__was_removed_as_a_moderator_by__user_by_" : "User <em>__username__</em> was removed as a moderator by <em>__user_by__</em>",
"Username" : "Username", "Username" : "Username",
"Username_cant_be_empty" : "The username cannot be empty", "Username_cant_be_empty" : "The username cannot be empty",
"Username_Change_Disabled" : "Your Rocket.Chat administrator has disabled the changing of usernames", "Username_Change_Disabled" : "Your Rocket.Chat administrator has disabled the changing of usernames",
...@@ -569,6 +581,8 @@ ...@@ -569,6 +581,8 @@
"Yes" : "Yes", "Yes" : "Yes",
"Yes_clear_all" : "Yes, clear all!", "Yes_clear_all" : "Yes, clear all!",
"Yes_delete_it" : "Yes, delete it!", "Yes_delete_it" : "Yes, delete it!",
"Yes_mute_user" : "Yes, mute user!",
"Yes_remove_user" : "Yes, remove user!",
"you_are_in_preview_mode_of" : "You are in preview mode of channel #<strong>__room_name__</strong>", "you_are_in_preview_mode_of" : "You are in preview mode of channel #<strong>__room_name__</strong>",
"You_can_change_a_different_avatar_too" : "You can override the avatar used to post from this integration.", "You_can_change_a_different_avatar_too" : "You can override the avatar used to post from this integration.",
"You_can_use_an_emoji_as_avatar" : "You can also use an emoji as an avatar.", "You_can_use_an_emoji_as_avatar" : "You can also use an emoji as an avatar.",
......
...@@ -72,6 +72,8 @@ Meteor.startup -> ...@@ -72,6 +72,8 @@ Meteor.startup ->
{ _id: 'ban-user', { _id: 'ban-user',
roles : ['admin', 'moderator']} roles : ['admin', 'moderator']}
{ _id: 'set-moderator',
roles : ['admin', 'moderator']}
{ _id: 'create-p', { _id: 'create-p',
roles : ['admin', 'user']} roles : ['admin', 'user']}
......
...@@ -82,3 +82,17 @@ Meteor.startup -> ...@@ -82,3 +82,17 @@ Meteor.startup ->
message: 'User_unmuted_by' message: 'User_unmuted_by'
data: (message) -> data: (message) ->
return { user_unmuted: message.msg, user_by: message.u.username } return { user_unmuted: message.msg, user_by: message.u.username }
RocketChat.MessageTypes.registerType
id: 'new-moderator'
system: true
message: 'User__username__was_added_as_a_moderator_by__user_by_'
data: (message) ->
return { username: message.msg, user_by: message.u.username }
RocketChat.MessageTypes.registerType
id: 'moderator-removed'
system: true
message: 'User__username__was_removed_as_a_moderator_by__user_by_'
data: (message) ->
return { username: message.msg, user_by: message.u.username }
...@@ -294,6 +294,14 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base ...@@ -294,6 +294,14 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
message = user.username message = user.username
return @createWithTypeRoomIdMessageAndUser 'user-unmuted', roomId, message, user, extraData return @createWithTypeRoomIdMessageAndUser 'user-unmuted', roomId, message, user, extraData
createNewModeratorWithRoomIdAndUser: (roomId, user, extraData) ->
message = user.username
return @createWithTypeRoomIdMessageAndUser 'new-moderator', roomId, message, user, extraData
createModeratorRemovedWithRoomIdAndUser: (roomId, user, extraData) ->
message = user.username
return @createWithTypeRoomIdMessageAndUser 'moderator-removed', roomId, message, user, extraData
# REMOVE # REMOVE
removeById: (_id) -> removeById: (_id) ->
query = query =
......
...@@ -210,6 +210,26 @@ RocketChat.models.Subscriptions = new class extends RocketChat.models._Base ...@@ -210,6 +210,26 @@ RocketChat.models.Subscriptions = new class extends RocketChat.models._Base
return @update query, update, { multi: true } return @update query, update, { multi: true }
addRoleById: (_id, role) ->
query =
_id: _id
update =
$addToSet:
roles: role
return @update query, update
removeRoleById: (_id, role) ->
query =
_id: _id
update =
$pull:
roles: role
return @update query, update
# INSERT # INSERT
createWithRoomAndUser: (room, user, extraData) -> createWithRoomAndUser: (room, user, extraData) ->
subscription = subscription =
......
...@@ -45,6 +45,12 @@ Template.userInfo.helpers ...@@ -45,6 +45,12 @@ Template.userInfo.helpers
return true return true
return false return false
canSetModerator: ->
return RocketChat.authz.hasAllPermission('set-moderator', Session.get('openedRoom'))
isModerator: ->
return !!RoomModerators.findOne({ "u._id": @user?._id })
Template.userInfo.events Template.userInfo.events
'click .pvt-msg': (e) -> 'click .pvt-msg': (e) ->
Meteor.call 'createDirectMessage', Session.get('showUserInfo'), (error, result) -> Meteor.call 'createDirectMessage', Session.get('showUserInfo'), (error, result) ->
...@@ -84,30 +90,60 @@ Template.userInfo.events ...@@ -84,30 +90,60 @@ Template.userInfo.events
'click .back': (e) -> 'click .back': (e) ->
Session.set('showUserInfo', null) Session.set('showUserInfo', null)
'click .remove-user': (e, t) -> 'click .remove-user': (e) ->
e.preventDefault() e.preventDefault()
rid = Session.get('openedRoom') rid = Session.get('openedRoom')
room = ChatRoom.findOne rid room = ChatRoom.findOne rid
if RocketChat.authz.hasAllPermission('remove-user', rid) if RocketChat.authz.hasAllPermission('remove-user', rid)
Meteor.call 'removeUserFromRoom', { rid: rid, username: @user.username }, (err, result) -> swal {
if err title: t('Are_you_sure')
return toastr.error(err.reason or err.message) text: t('The_user_will_be_removed_from_s', room.name)
toastr.success TAPi18n.__ 'User_removed_from_room' type: 'warning'
Session.set('showUserInfo', null) showCancelButton: true
confirmButtonColor: '#DD6B55'
confirmButtonText: t('Yes_remove_user')
cancelButtonText: t('Cancel')
closeOnConfirm: false
html: false
}, =>
Meteor.call 'removeUserFromRoom', { rid: rid, username: @user.username }, (err, result) ->
if err
return toastr.error(err.reason or err.message)
swal
title: t('Removed')
text: t('User_has_been_removed_from_s', room.name)
type: 'success'
timer: 2000
showConfirmButton: false
Session.set('showUserInfo', null)
else else
toastr.error(TAPi18n.__ 'Not_allowed') toastr.error(TAPi18n.__ 'Not_allowed')
'click .mute-user': (e, t) -> 'click .mute-user': (e) ->
e.preventDefault() e.preventDefault()
rid = Session.get('openedRoom') rid = Session.get('openedRoom')
room = ChatRoom.findOne rid room = ChatRoom.findOne rid
if RocketChat.authz.hasAllPermission('mute-user', rid) if RocketChat.authz.hasAllPermission('mute-user', rid)
Meteor.call 'muteUserInRoom', { rid: rid, username: @user.username }, (err, result) -> swal {
if err title: t('Are_you_sure')
return toastr.error(err.reason or err.message) text: t('The_user_wont_be_able_to_type_in_s', room.name)
toastr.success TAPi18n.__ 'User_muted_in_room' type: 'warning'
else showCancelButton: true
toastr.error(TAPi18n.__ 'Not_allowed') confirmButtonColor: '#DD6B55'
confirmButtonText: t('Yes_mute_user')
cancelButtonText: t('Cancel')
closeOnConfirm: false
html: false
}, =>
Meteor.call 'muteUserInRoom', { rid: rid, username: @user.username }, (err, result) ->
if err
return toastr.error(err.reason or err.message)
swal
title: t('Muted')
text: t('User_has_been_muted_in_s', room.name)
type: 'success'
timer: 2000
showConfirmButton: false
'click .unmute-user': (e, t) -> 'click .unmute-user': (e, t) ->
e.preventDefault() e.preventDefault()
...@@ -121,6 +157,26 @@ Template.userInfo.events ...@@ -121,6 +157,26 @@ Template.userInfo.events
else else
toastr.error(TAPi18n.__ 'Not_allowed') toastr.error(TAPi18n.__ 'Not_allowed')
'click .set-moderator': (e, t) ->
e.preventDefault()
room = ChatRoom.findOne(Session.get('openedRoom'))
moderators = _.map RoomModerators.find().fetch(), (moderator) -> return moderator.u?._id
if @user._id not in moderators
Meteor.call 'addRoomModerator', Session.get('openedRoom'), @user._id, (err, results) =>
if err
return toastr.error(err.reason or err.message)
toastr.success TAPi18n.__ 'User__username__is_now_a_moderator_of__room_name_', { username: @user.username, room_name: room.name }
'click .unset-moderator': (e, t) ->
e.preventDefault()
room = ChatRoom.findOne(Session.get('openedRoom'))
moderators = _.map RoomModerators.find().fetch(), (moderator) -> return moderator.u?._id
if @user._id in moderators
Meteor.call 'removeRoomModerator', Session.get('openedRoom'), @user._id, (err, results) =>
if err
return toastr.error(err.reason or err.message)
toastr.success TAPi18n.__ 'User__username__removed_from__room_name__moderators', { username: @user.username, room_name: room.name }
Template.userInfo.onCreated -> Template.userInfo.onCreated ->
@now = new ReactiveVar moment() @now = new ReactiveVar moment()
self = @ self = @
......
...@@ -30,20 +30,27 @@ ...@@ -30,20 +30,27 @@
{{> videoButtons}} {{> videoButtons}}
{{#if showAll}} {{#if showAll}}
<button class='button secondary back'><span>{{_ "View_All"}} <i class='icon-angle-right'></i></span></button>
{{#if canDirectMessage user.username}} {{#if canDirectMessage user.username}}
<button class='button pvt-msg'><span><i class='icon-chat'></i> {{_ "Conversation"}}</span></button> <button class='button button-block pvt-msg'><span><i class='icon-chat'></i> {{_ "Conversation"}}</span></button>
{{/if}}
{{#if canSetModerator}}
{{#if isModerator}}
<button class="button button-block unset-moderator lightblue"><span>{{_ "Remove_as_moderator"}}</span></button>
{{else}}
<button class="button button-block set-moderator lightblue"><span>{{_ "Set_as_moderator"}}</span></button>
{{/if}}
{{/if}} {{/if}}
{{#if canRemoveUser}} {{#if canRemoveUser}}
<button class="button remove-user red"><span>{{_ "Remove_from_room"}}</span></button> <button class="button button-block remove-user red"><span>{{_ "Remove_from_room"}}</span></button>
{{/if}} {{/if}}
{{#if canMuteUser}} {{#if canMuteUser}}
{{#if userMuted}} {{#if userMuted}}
<button class="button unmute-user primary"><span>{{_ "Unmute_user"}}</span></button> <button class="button button-block unmute-user primary"><span>{{_ "Unmute_user"}}</span></button>
{{else}} {{else}}
<button class="button mute-user red"><span>{{_ "Mute_user"}}</span></button> <button class="button button-block mute-user red"><span>{{_ "Mute_user"}}</span></button>
{{/if}} {{/if}}
{{/if}} {{/if}}
<button class='button secondary back'><span>{{_ "View_All"}} <i class='icon-angle-right'></i></span></button>
{{/if}} {{/if}}
</nav> </nav>
{{/if}} {{/if}}
......
@RoomModerators = new Mongo.Collection 'room_moderators'
isSubscribed = (_id) -> isSubscribed = (_id) ->
return ChatSubscription.find({ rid: _id }).count() > 0 return ChatSubscription.find({ rid: _id }).count() > 0
...@@ -493,6 +495,9 @@ Template.room.onCreated -> ...@@ -493,6 +495,9 @@ Template.room.onCreated ->
@autorun => @autorun =>
@subscribe 'fullUserData', Session.get('showUserInfo'), 1 @subscribe 'fullUserData', Session.get('showUserInfo'), 1
@autorun =>
@subscribe 'roomModerators', Session.get('openedRoom')
Template.room.onDestroyed -> Template.room.onDestroyed ->
window.removeEventListener 'resize', this.onWindowResize window.removeEventListener 'resize', this.onWindowResize
......
Meteor.methods
addRoomModerator: (rid, userId) ->
unless Meteor.userId()
throw new Meteor.Error 'invalid-user', '[methods] addRoomModerator -> Invalid user'
check rid, String
check userId, String
subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId rid, userId
unless subscription?
throw new Meteor.Error 'invalid-subscription', '[methods] addRoomModerator -> Invalid Subscription'
RocketChat.models.Subscriptions.addRoleById(subscription._id, 'moderator')
user = RocketChat.models.Users.findOneById userId
fromUser = RocketChat.models.Users.findOneById Meteor.userId()
RocketChat.models.Messages.createNewModeratorWithRoomIdAndUser rid, user,
u:
_id: fromUser._id
username: fromUser.username
return true
Meteor.methods
getRoomModerators: (rid) ->
unless Meteor.userId()
throw new Meteor.Error(403, "[methods] getRoomModerators -> Invalid user")
query =
rid: rid
roles: 'moderator'
options =
sort:
"u.username": 1
fields:
u: 1
return _.map(RocketChat.models.Subscriptions.find(query, options).fetch(), (subscription) -> return subscription.u)
Meteor.methods
removeRoomModerator: (rid, userId) ->
unless Meteor.userId()
throw new Meteor.Error 'invalid-user', '[methods] removeRoomModerator -> Invalid user'
check rid, String
check userId, String
subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId rid, userId
unless subscription?
throw new Meteor.Error 'invalid-subscription', '[methods] removeRoomModerator -> Invalid Subscription'
RocketChat.models.Subscriptions.removeRoleById(subscription._id, 'moderator')
user = RocketChat.models.Users.findOneById userId
fromUser = RocketChat.models.Users.findOneById Meteor.userId()
RocketChat.models.Messages.createModeratorRemovedWithRoomIdAndUser rid, user,
u:
_id: fromUser._id
username: fromUser.username
return true
Meteor.publish 'roomModerators', (rid, limit = 50) ->
unless this.userId
return this.ready()
pub = this
query =
rid: rid
roles: 'moderator'
options =
limit: limit
sort:
"u.username": 1
fields:
u: 1
cursor = RocketChat.models.Subscriptions.find(query, options).observeChanges
added: (_id, record) ->
pub.added('room_moderators', _id, record)
changed: (_id, record) ->
pub.changed('room_moderators', _id, record)
removed: (_id, record) ->
pub.removed('room_moderators', _id, record)
this.ready()
this.onStop ->
cursor.stop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment