parent
3c8794e107
commit
53e93aa86c
@ -0,0 +1,11 @@ |
||||
RocketChat.slashCommands.add 'kick', (command, params, item) -> |
||||
username = params.trim() |
||||
if username is '' |
||||
return |
||||
username = username.replace('@', '') |
||||
|
||||
if Session.get('showUserInfo') is username |
||||
Session.set('showUserInfo', null) |
||||
, |
||||
description: TAPi18n.__ 'Remove_someone_from_room' |
||||
params: '@username' |
||||
@ -0,0 +1,5 @@ |
||||
{ |
||||
"Username_doesnt_exist" : "The username `#%s` doesn't exist.", |
||||
"Username_is_not_in_this_room" : "The user `#%s` is not in this room.", |
||||
"Remove_someone_from_room" : "Remove someone from the room" |
||||
} |
||||
@ -0,0 +1,37 @@ |
||||
Package.describe({ |
||||
name: 'rocketchat:slashcommands-kick', |
||||
version: '0.0.1', |
||||
summary: 'Command handler for the /kick command', |
||||
git: '' |
||||
}); |
||||
|
||||
Package.onUse(function(api) { |
||||
|
||||
api.versionsFrom('1.0'); |
||||
|
||||
api.use([ |
||||
'coffeescript', |
||||
'check', |
||||
'rocketchat:lib@0.0.1' |
||||
]); |
||||
|
||||
api.addFiles('client.coffee', 'client'); |
||||
api.addFiles('server.coffee', 'server'); |
||||
|
||||
// TAPi18n
|
||||
api.use('templating', 'client'); |
||||
var _ = Npm.require('underscore'); |
||||
var fs = Npm.require('fs'); |
||||
tapi18nFiles = _.compact(_.map(fs.readdirSync('packages/rocketchat-slashcommands-kick/i18n'), function(filename) { |
||||
if (fs.statSync('packages/rocketchat-slashcommands-kick/i18n/' + filename).size > 16) { |
||||
return 'i18n/' + filename; |
||||
} |
||||
})); |
||||
api.use('tap:i18n@1.6.1', ['client', 'server']); |
||||
api.imply('tap:i18n'); |
||||
api.addFiles(tapi18nFiles, ['client', 'server']); |
||||
}); |
||||
|
||||
Package.onTest(function(api) { |
||||
|
||||
}); |
||||
@ -0,0 +1,40 @@ |
||||
### |
||||
# Kick is a named function that will replace /kick commands |
||||
### |
||||
|
||||
class Kick |
||||
constructor: (command, params, item) -> |
||||
if command isnt 'kick' or not Match.test params, String |
||||
return |
||||
|
||||
username = params.trim() |
||||
if username is '' |
||||
return |
||||
|
||||
username = username.replace('@', '') |
||||
|
||||
user = Meteor.users.findOne Meteor.userId() |
||||
kickedUser = RocketChat.models.Users.findOneByUsername username |
||||
room = RocketChat.models.Rooms.findOneById item.rid |
||||
|
||||
if not kickedUser? |
||||
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', { |
||||
_id: Random.id() |
||||
rid: item.rid |
||||
ts: new Date |
||||
msg: TAPi18n.__('Username_doesnt_exist', { postProcess: 'sprintf', sprintf: [ username ] }, user.language); |
||||
} |
||||
return |
||||
|
||||
if username not in (room.usernames or []) |
||||
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', { |
||||
_id: Random.id() |
||||
rid: item.rid |
||||
ts: new Date |
||||
msg: TAPi18n.__('Username_is_not_in_this_room', { postProcess: 'sprintf', sprintf: [ username ] }, user.language); |
||||
} |
||||
return |
||||
|
||||
Meteor.call 'removeUserFromRoom', { rid: item.rid, username: username } |
||||
|
||||
RocketChat.slashCommands.add 'kick', Kick |
||||
@ -1,25 +1,31 @@ |
||||
Meteor.methods |
||||
removeUserFromRoom: (data) -> |
||||
fromId = Meteor.userId() |
||||
# console.log '[methods] removeUserFromRoom -> '.green, 'fromId:', fromId, 'data:', data |
||||
console.log '[methods] removeUserFromRoom -> '.green, 'fromId:', fromId, 'data:', data |
||||
|
||||
check(data, Match.ObjectIncluding({ rid: String, username: String })) |
||||
|
||||
unless RocketChat.authz.hasPermission(fromId, 'remove-user', data.rid) |
||||
throw new Meteor.Error 'not-allowed', 'Not allowed' |
||||
|
||||
room = RocketChat.models.Rooms.findOneById data.rid |
||||
|
||||
if room.u?._id isnt Meteor.userId() and room.t is 'c' |
||||
throw new Meteor.Error 403, 'Not allowed' |
||||
if data.username not in (room?.usernames or []) |
||||
throw new Meteor.Error 'not-in-room', 'User is not in this room' |
||||
|
||||
removedUser = RocketChat.models.Users.findOneByUsername data.username |
||||
|
||||
RocketChat.models.Rooms.removeUsernameById data.rid, data.username |
||||
|
||||
RocketChat.models.Subscriptions.removeByRoomIdAndUserId data.rid, data.username |
||||
RocketChat.models.Subscriptions.removeByRoomIdAndUserId data.rid, removedUser._id |
||||
|
||||
switch room.t |
||||
when 'c' |
||||
RocketChat.authz.removeUsersFromRole(removedUser._id; 'channel-moderator', data.rid) |
||||
when 'p' |
||||
RocketChat.authz.removeUsersFromRole(removedUser._id; 'group-moderator', data.rid) |
||||
if room.t in [ 'c', 'p' ] |
||||
RocketChat.authz.removeUsersFromRoles(removedUser._id; 'moderator', data.rid) |
||||
|
||||
RocketChat.models.Messages.createUserRemovedWithRoomIdAndUser data.rid, removedUser |
||||
fromUser = RocketChat.models.Users.findOneById fromId |
||||
RocketChat.models.Messages.createUserRemovedWithRoomIdAndUser data.rid, removedUser, |
||||
u: |
||||
_id: fromUser._id |
||||
username: fromUser.username |
||||
|
||||
return true |
||||
|
||||
Loading…
Reference in new issue