parent
6d3f3e51a1
commit
748eee62f7
@ -0,0 +1,25 @@ |
||||
RocketChat.addUserToDefaultChannels = function(user, silenced) { |
||||
RocketChat.callbacks.run('beforeJoinDefaultChannels', user); |
||||
let defaultRooms = RocketChat.models.Rooms.findByDefaultAndTypes(true, ['c', 'p'], {fields: {usernames: 0}}).fetch(); |
||||
defaultRooms.forEach((room) => { |
||||
|
||||
// put user in default rooms
|
||||
RocketChat.models.Rooms.addUsernameById(room._id, user.username); |
||||
|
||||
if (!RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user._id)) { |
||||
|
||||
// Add a subscription to this user
|
||||
RocketChat.models.Subscriptions.createWithRoomAndUser(room, user, { |
||||
ts: new Date(), |
||||
open: true, |
||||
alert: true, |
||||
unread: 1 |
||||
}); |
||||
|
||||
// Insert user joined message
|
||||
if (!silenced) { |
||||
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(room._id, user); |
||||
} |
||||
} |
||||
}); |
||||
}; |
||||
@ -0,0 +1,42 @@ |
||||
RocketChat.addUserToRoom = function(rid, user, inviter) { |
||||
let now = new Date(); |
||||
let room = RocketChat.models.Rooms.findOneById(rid); |
||||
|
||||
// Check if user is already in room
|
||||
let subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, user._id); |
||||
if (subscription) { |
||||
return; |
||||
} |
||||
|
||||
if (room.t === 'c') { |
||||
RocketChat.callbacks.run('beforeJoinRoom', user, room); |
||||
} |
||||
|
||||
RocketChat.models.Rooms.addUsernameById(rid, user.username); |
||||
RocketChat.models.Subscriptions.createWithRoomAndUser(room, user, { |
||||
ts: now, |
||||
open: true, |
||||
alert: true, |
||||
unread: 1 |
||||
}); |
||||
|
||||
if (inviter) { |
||||
RocketChat.models.Messages.createUserAddedWithRoomIdAndUser(rid, user, { |
||||
ts: now, |
||||
u: { |
||||
_id: inviter._id, |
||||
username: inviter.username |
||||
} |
||||
}); |
||||
} else { |
||||
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(rid, user, { ts: now }); |
||||
} |
||||
|
||||
if (room.t === 'c') { |
||||
Meteor.defer(function() { |
||||
RocketChat.callbacks.run('afterJoinRoom', user, room); |
||||
}); |
||||
} |
||||
|
||||
return true; |
||||
}; |
||||
@ -0,0 +1,4 @@ |
||||
RocketChat.archiveRoom = function(rid) { |
||||
RocketChat.models.Rooms.archiveById(rid); |
||||
RocketChat.models.Subscriptions.archiveByRoomId(rid); |
||||
}; |
||||
@ -0,0 +1,83 @@ |
||||
/* globals RocketChat */ |
||||
RocketChat.createRoom = function(type, name, owner, members) { |
||||
name = s.trim(name); |
||||
owner = s.trim(owner); |
||||
members = [].concat(members); |
||||
|
||||
if (!name) { |
||||
throw new Meteor.Error('error-invalid-name', 'Invalid name', { function: 'RocketChat.createRoom' }); |
||||
} |
||||
|
||||
owner = RocketChat.models.Users.findOneByUsername(owner, { fields: { username: 1 }}); |
||||
if (!owner) { |
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'RocketChat.createRoom' }); |
||||
} |
||||
|
||||
let nameValidation; |
||||
try { |
||||
nameValidation = new RegExp('^' + RocketChat.settings.get('UTF8_Names_Validation') + '$'); |
||||
} catch (error) { |
||||
nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$'); |
||||
} |
||||
|
||||
if (!nameValidation.test(name)) { |
||||
throw new Meteor.Error('error-invalid-name', 'Invalid name', { function: 'RocketChat.createRoom' }); |
||||
} |
||||
|
||||
let now = new Date(); |
||||
if (!_.contains(members, owner.username)) { |
||||
members.push(owner.username); |
||||
} |
||||
|
||||
// avoid duplicate names
|
||||
let room = RocketChat.models.Rooms.findOneByName(name); |
||||
if (room) { |
||||
if (room.archived) { |
||||
throw new Meteor.Error('error-archived-duplicate-name', 'There\'s an archived channel with name ' + name, { function: 'RocketChat.createRoom', room_name: name }); |
||||
} else { |
||||
throw new Meteor.Error('error-duplicate-channel-name', 'A channel with name \'' + name + '\' exists', { function: 'RocketChat.createRoom', room_name: name }); |
||||
} |
||||
} |
||||
|
||||
if (type === 'c') { |
||||
RocketChat.callbacks.run('beforeCreateChannel', owner, { |
||||
t: 'c', |
||||
name: name, |
||||
ts: now, |
||||
usernames: members, |
||||
u: { |
||||
_id: owner._id, |
||||
username: owner.username |
||||
} |
||||
}); |
||||
} |
||||
|
||||
room = RocketChat.models.Rooms.createWithTypeNameUserAndUsernames(type, name, owner.username, members, { ts: now }); |
||||
|
||||
for (let username of members) { |
||||
let member = RocketChat.models.Users.findOneByUsername(username, { fields: { username: 1 }}); |
||||
if (!member) { |
||||
continue; |
||||
} |
||||
|
||||
let extra = { open: true }; |
||||
|
||||
if (username === owner.username) { |
||||
extra.ls = now; |
||||
} |
||||
|
||||
RocketChat.models.Subscriptions.createWithRoomAndUser(room, member, extra); |
||||
} |
||||
|
||||
RocketChat.authz.addUserRoles(owner._id, ['owner'], room._id); |
||||
|
||||
if (type === 'c') { |
||||
Meteor.defer(() => { |
||||
RocketChat.callbacks.run('afterCreateChannel', owner, room); |
||||
}); |
||||
} |
||||
|
||||
return { |
||||
rid: room._id |
||||
}; |
||||
}; |
||||
@ -0,0 +1,31 @@ |
||||
/* globals FileUpload */ |
||||
RocketChat.deleteMessage = function(message, user) { |
||||
let keepHistory = RocketChat.settings.get('Message_KeepHistory'); |
||||
let showDeletedStatus = RocketChat.settings.get('Message_ShowDeletedStatus'); |
||||
|
||||
if (keepHistory) { |
||||
if (showDeletedStatus) { |
||||
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id); |
||||
} else { |
||||
RocketChat.models.Messages.setHiddenById(message._id, true); |
||||
} |
||||
|
||||
if (message.file && message.file._id) { |
||||
RocketChat.models.Uploads.update(message.file._id, { $set: { _hidden: true } }); |
||||
} |
||||
} else { |
||||
if (!showDeletedStatus) { |
||||
RocketChat.models.Messages.removeById(message._id); |
||||
} |
||||
|
||||
if (message.file && message.file._id) { |
||||
FileUpload.delete(message.file._id); |
||||
} |
||||
} |
||||
|
||||
if (showDeletedStatus) { |
||||
RocketChat.models.Messages.setAsDeletedByIdAndUser(message._id, user); |
||||
} else { |
||||
RocketChat.Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id }); |
||||
} |
||||
}; |
||||
@ -0,0 +1,23 @@ |
||||
RocketChat.removeUserFromRoom = function(rid, user) { |
||||
let room = RocketChat.models.Rooms.findOneById(rid); |
||||
|
||||
if (room) { |
||||
RocketChat.callbacks.run('beforeLeaveRoom', user, room); |
||||
RocketChat.models.Rooms.removeUsernameById(rid, user.username); |
||||
|
||||
if (room.usernames.indexOf(user.username) !== -1) { |
||||
let removedUser = user; |
||||
RocketChat.models.Messages.createUserLeaveWithRoomIdAndUser(rid, removedUser); |
||||
} |
||||
|
||||
if (room.t === 'l') { |
||||
RocketChat.models.Messages.createCommandWithRoomIdAndUser('survey', rid, user); |
||||
} |
||||
|
||||
RocketChat.models.Subscriptions.removeByRoomIdAndUserId(rid, user._id); |
||||
|
||||
Meteor.defer(function() { |
||||
RocketChat.callbacks.run('afterLeaveRoom', user, room); |
||||
}); |
||||
} |
||||
}; |
||||
@ -0,0 +1,55 @@ |
||||
RocketChat.setUserAvatar = function(user, dataURI, contentType, service) { |
||||
if (service === 'initials') { |
||||
return RocketChat.models.Users.setAvatarOrigin(user._id, service); |
||||
} |
||||
|
||||
if (service === 'url') { |
||||
let result = null; |
||||
|
||||
try { |
||||
result = HTTP.get(dataURI, { npmRequestOptions: {encoding: 'binary'} }); |
||||
} catch (error) { |
||||
console.log(`Error while handling the setting of the avatar from a url (${dataURI}) for ${user.username}:`, error); |
||||
throw new Meteor.Error('error-avatar-url-handling', `Error while handling avatar setting from a URL (${dataURI}) for ${user.username}`, { function: 'RocketChat.setUserAvatar', url: dataURI, username: user.username }); |
||||
} |
||||
|
||||
if (result.statusCode !== 200) { |
||||
console.log(`Not a valid response, ${result.statusCode}, from the avatar url: ${dataURI}`); |
||||
throw new Meteor.Error('error-avatar-invalid-url', `Invalid avatar URL: ${dataURI}`, { function: 'RocketChat.setUserAvatar', url: dataURI }); |
||||
} |
||||
|
||||
if (!/image\/.+/.test(result.headers['content-type'])) { |
||||
console.log(`Not a valid content-type from the provided url, ${result.headers['content-type']}, from the avatar url: ${dataURI}`); |
||||
throw new Meteor.Error('error-avatar-invalid-url', `Invalid avatar URL: ${dataURI}`, { function: 'RocketChat.setUserAvatar', url: dataURI }); |
||||
} |
||||
|
||||
let ars = RocketChatFile.bufferToStream(new Buffer(result.content, 'binary')); |
||||
RocketChatFileAvatarInstance.deleteFile(encodeURIComponent(`${user.username}.jpg`)); |
||||
let aws = RocketChatFileAvatarInstance.createWriteStream(encodeURIComponent(`${user.username}.jpg`), result.headers['content-type']); |
||||
aws.on('end', Meteor.bindEnvironment(function() { |
||||
Meteor.setTimeout(function() { |
||||
console.log(`Set ${user.username}'s avatar from the url: ${dataURI}`); |
||||
RocketChat.models.Users.setAvatarOrigin(user._id, service); |
||||
RocketChat.Notifications.notifyAll('updateAvatar', { username: user.username }); |
||||
}, 500); |
||||
})); |
||||
|
||||
ars.pipe(aws); |
||||
return; |
||||
} |
||||
|
||||
let fileData = RocketChatFile.dataURIParse(dataURI); |
||||
let image = fileData.image; |
||||
contentType = fileData.contentType; |
||||
|
||||
let rs = RocketChatFile.bufferToStream(new Buffer(image, 'base64')); |
||||
RocketChatFileAvatarInstance.deleteFile(encodeURIComponent(`${user.username}.jpg`)); |
||||
let ws = RocketChatFileAvatarInstance.createWriteStream(encodeURIComponent(`${user.username}.jpg`), contentType); |
||||
ws.on('end', Meteor.bindEnvironment(function() { |
||||
Meteor.setTimeout(function() { |
||||
RocketChat.models.Users.setAvatarOrigin(user._id, service); |
||||
RocketChat.Notifications.notifyAll('updateAvatar', {username: user.username}); |
||||
}, 500); |
||||
})); |
||||
rs.pipe(ws); |
||||
}; |
||||
@ -0,0 +1,4 @@ |
||||
RocketChat.unarchiveRoom = function(rid) { |
||||
RocketChat.models.Rooms.unarchiveById(rid); |
||||
RocketChat.models.Subscriptions.unarchiveByRoomId(rid); |
||||
}; |
||||
@ -0,0 +1,30 @@ |
||||
RocketChat.updateMessage = function(message, user) { |
||||
// If we keep history of edits, insert a new message to store history information
|
||||
if (RocketChat.settings.get('Message_KeepHistory')) { |
||||
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id); |
||||
} |
||||
|
||||
message.editedAt = new Date(); |
||||
message.editedBy = { |
||||
_id: user._id, |
||||
username: user.username |
||||
}; |
||||
|
||||
let urls = message.msg.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g); |
||||
if (urls) { |
||||
message.urls = urls.map((url) => { return { url: url } }); |
||||
} |
||||
|
||||
message = RocketChat.callbacks.run('beforeSaveMessage', message); |
||||
|
||||
let tempid = message._id; |
||||
delete message._id; |
||||
|
||||
RocketChat.models.Messages.update({ _id: tempid }, { $set: message }); |
||||
|
||||
let room = RocketChat.models.Rooms.findOneById(message.rid); |
||||
|
||||
Meteor.defer(function() { |
||||
RocketChat.callbacks.run('afterSaveMessage', RocketChat.models.Messages.findOneById(tempid), room); |
||||
}); |
||||
}; |
||||
@ -0,0 +1,9 @@ |
||||
Meteor.methods |
||||
createChannel: (name, members) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error 'error-invalid-user', "Invalid user", { method: 'createChannel' } |
||||
|
||||
if RocketChat.authz.hasPermission(Meteor.userId(), 'create-c') isnt true |
||||
throw new Meteor.Error 'error-not-allowed', "Not allowed", { method: 'createChannel' } |
||||
|
||||
return RocketChat.createRoom('c', name, Meteor.user()?.username, members); |
||||
@ -0,0 +1,14 @@ |
||||
Meteor.methods |
||||
joinRoom: (rid) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error 'error-invalid-user', 'Invalid user', { method: 'joinRoom' } |
||||
|
||||
room = RocketChat.models.Rooms.findOneById rid |
||||
|
||||
if not room? |
||||
throw new Meteor.Error 'error-invalid-room', 'Invalid room', { method: 'joinRoom' } |
||||
|
||||
if room.t isnt 'c' or RocketChat.authz.hasPermission(Meteor.userId(), 'view-c-room') isnt true |
||||
throw new Meteor.Error 'error-not-allowed', 'Not allowed', { method: 'joinRoom' } |
||||
|
||||
RocketChat.addUserToRoom(rid, Meteor.user()) |
||||
@ -1,65 +0,0 @@ |
||||
Meteor.methods |
||||
createChannel: (name, members) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error 'error-invalid-user', "Invalid user", { method: 'createChannel' } |
||||
|
||||
try |
||||
nameValidation = new RegExp '^' + RocketChat.settings.get('UTF8_Names_Validation') + '$' |
||||
catch |
||||
nameValidation = new RegExp '^[0-9a-zA-Z-_.]+$' |
||||
|
||||
if not nameValidation.test name |
||||
throw new Meteor.Error 'error-invalid-name', "Invalid name", { method: 'createChannel' } |
||||
|
||||
if RocketChat.authz.hasPermission(Meteor.userId(), 'create-c') isnt true |
||||
throw new Meteor.Error 'error-not-allowed', "Not allowed", { method: 'createChannel' } |
||||
|
||||
now = new Date() |
||||
user = Meteor.user() |
||||
|
||||
members.push user.username if user.username not in members |
||||
|
||||
# avoid duplicate names |
||||
if RocketChat.models.Rooms.findOneByName name |
||||
if RocketChat.models.Rooms.findOneByName(name).archived |
||||
throw new Meteor.Error 'error-archived-duplicate-name', "There's an archived channel with name " + name, { method: 'createChannel', room_name: name } |
||||
else |
||||
throw new Meteor.Error 'error-duplicate-channel-name', "A channel with name '" + name + "' exists", { method: 'createChannel', room_name: name } |
||||
|
||||
# name = s.slugify name |
||||
|
||||
RocketChat.callbacks.run 'beforeCreateChannel', user, |
||||
t: 'c' |
||||
name: name |
||||
ts: now |
||||
usernames: members |
||||
u: |
||||
_id: user._id |
||||
username: user.username |
||||
|
||||
# create new room |
||||
room = RocketChat.models.Rooms.createWithTypeNameUserAndUsernames 'c', name, user, members, |
||||
ts: now |
||||
|
||||
for username in members |
||||
member = RocketChat.models.Users.findOneByUsername username |
||||
if not member? |
||||
continue |
||||
|
||||
extra = |
||||
open: true |
||||
|
||||
if username is user.username |
||||
extra.ls = now |
||||
|
||||
RocketChat.models.Subscriptions.createWithRoomAndUser room, member, extra |
||||
|
||||
# set creator as channel moderator. permission limited to channel by scoping to rid |
||||
RocketChat.authz.addUserRoles(Meteor.userId(), ['owner'], room._id) |
||||
|
||||
Meteor.defer -> |
||||
RocketChat.callbacks.run 'afterCreateChannel', user, room |
||||
|
||||
return { |
||||
rid: room._id |
||||
} |
||||
@ -1,39 +0,0 @@ |
||||
Meteor.methods |
||||
joinRoom: (rid) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error 'error-invalid-user', 'Invalid user', { method: 'joinRoom' } |
||||
|
||||
room = RocketChat.models.Rooms.findOneById rid |
||||
|
||||
if not room? |
||||
throw new Meteor.Error 'error-invalid-room', 'Invalid room', { method: 'joinRoom' } |
||||
|
||||
if room.t isnt 'c' or RocketChat.authz.hasPermission(Meteor.userId(), 'view-c-room') isnt true |
||||
throw new Meteor.Error 'error-not-allowed', 'Not allowed', { method: 'joinRoom' } |
||||
|
||||
now = new Date() |
||||
|
||||
# Check if user is already in room |
||||
subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId rid, Meteor.userId() |
||||
if subscription? |
||||
return |
||||
|
||||
user = RocketChat.models.Users.findOneById Meteor.userId() |
||||
|
||||
RocketChat.callbacks.run 'beforeJoinRoom', user, room |
||||
|
||||
RocketChat.models.Rooms.addUsernameById rid, user.username |
||||
|
||||
RocketChat.models.Subscriptions.createWithRoomAndUser room, user, |
||||
ts: now |
||||
open: true |
||||
alert: true |
||||
unread: 1 |
||||
|
||||
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser rid, user, |
||||
ts: now |
||||
|
||||
Meteor.defer -> |
||||
RocketChat.callbacks.run 'afterJoinRoom', user, room |
||||
|
||||
return true |
||||
Loading…
Reference in new issue