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

Some improvements from more-streams #4510

Merged
merged 1 commit into from
Oct 1, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-actionlink', 'Invalid action link', { method: 'actionLinkHandler' });
}

var room = RocketChat.models.Rooms.findOne({ _id: message.rid });
var room = RocketChat.models.Rooms.findOneById(message.rid);
if (Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) === -1) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'actionLinkHandler' });
}
Expand Down
7 changes: 3 additions & 4 deletions packages/rocketchat-api/server/routes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RocketChat.API.v1.addRoute 'channels.create', authRequired: true,
return RocketChat.API.v1.failure e.name + ': ' + e.message

return RocketChat.API.v1.success
channel: RocketChat.models.Rooms.findOne({_id: id.rid})
channel: RocketChat.models.Rooms.findOneById(id.rid)

# List Private Groups a user has access to
RocketChat.API.v1.addRoute 'groups.list', authRequired: true,
Expand All @@ -123,7 +123,7 @@ RocketChat.API.v1.addRoute 'channel.addall', authRequired: true,
return RocketChat.API.v1.failure e.name + ': ' + e.message

return RocketChat.API.v1.success
channel: RocketChat.models.Rooms.findOne({_id: @bodyParams.roomId})
channel: RocketChat.models.Rooms.findOneById(@bodyParams.roomId)

# List all users
RocketChat.API.v1.addRoute 'users.list', authRequired: true,
Expand Down Expand Up @@ -255,5 +255,4 @@ RocketChat.API.v1.addRoute 'groups.create', authRequired: true,
return RocketChat.API.v1.failure e.name + ': ' + e.message

return RocketChat.API.v1.success
group: RocketChat.models.Rooms.findOne({_id: id.rid})

group: RocketChat.models.Rooms.findOneById(id.rid)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Meteor.startup ->

return msg

RocketChat.callbacks.add 'streamMessage', roomSettingsChangedCallback, RocketChat.callbacks.priority.HIGH
RocketChat.callbacks.add 'streamMessage', roomSettingsChangedCallback, RocketChat.callbacks.priority.HIGH, 'room-settings-changed'

roomNameChangedCallback = (msg) ->
Tracker.nonreactive ->
Expand All @@ -24,4 +24,4 @@ Meteor.startup ->

return msg

RocketChat.callbacks.add 'streamMessage', roomNameChangedCallback, RocketChat.callbacks.priority.HIGH
RocketChat.callbacks.add 'streamMessage', roomNameChangedCallback, RocketChat.callbacks.priority.HIGH, 'room-name-changed'
2 changes: 1 addition & 1 deletion packages/rocketchat-highlight-words/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class HighlightWordsClient
message.html = msg
return message

RocketChat.callbacks.add 'renderMessage', HighlightWordsClient, RocketChat.callbacks.priority.MEDIUM + 1
RocketChat.callbacks.add 'renderMessage', HighlightWordsClient, RocketChat.callbacks.priority.MEDIUM, 'highlight-words'
2 changes: 1 addition & 1 deletion packages/rocketchat-highlight/highlight.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ class Highlight

return message

RocketChat.callbacks.add 'renderMessage', Highlight, RocketChat.callbacks.priority.HIGH
RocketChat.callbacks.add 'renderMessage', Highlight, RocketChat.callbacks.priority.HIGH, 'highlight'
RocketChat.Highlight = true
18 changes: 3 additions & 15 deletions packages/rocketchat-integrations/server/processWebhookMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) {

switch (channelType) {
case '#':
room = RocketChat.models.Rooms.findOne({
$or: [
{
_id: channel
}, {
name: channel
}
]
});
room = RocketChat.models.Rooms.findOneByIdOrName(channel);
if (!_.isObject(room)) {
throw new Meteor.Error('invalid-channel');
}
Expand All @@ -52,18 +44,14 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) {
]
}) || {};
rid = [user._id, roomUser._id].sort().join('');
room = RocketChat.models.Rooms.findOne({
_id: {
$in: [rid, channel]
}
});
room = RocketChat.models.Rooms.findOneById({$in: [rid, channel]});
if (!_.isObject(roomUser) && !_.isObject(room)) {
throw new Meteor.Error('invalid-channel');
}
if (!room) {
Meteor.runAsUser(user._id, function() {
Meteor.call('createDirectMessage', roomUser.username);
room = RocketChat.models.Rooms.findOne(rid);
room = RocketChat.models.Rooms.findOneById(rid);
});
}
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-katex/katex.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Katex
RocketChat.katex = new Katex

cb = RocketChat.katex.render_message.bind(RocketChat.katex)
RocketChat.callbacks.add 'renderMessage', cb, RocketChat.callbacks.priority.HIGH - 1
RocketChat.callbacks.add 'renderMessage', cb, RocketChat.callbacks.priority.HIGH - 1, 'katex'

if Meteor.isClient
Blaze.registerHelper 'RocketChatKatex', (text) ->
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ RocketChat.settings.addGroup('Settings_Group', function() {

this.section('Group_Section', function() {
this.add('Setting_Inside_Section', 'default_value', {
type: 'boolean',
public: true,
type: 'boolean',
public: true,
enableQuery: {
_id: 'SettingInGroup',
value: true
Expand Down
20 changes: 5 additions & 15 deletions packages/rocketchat-lib/client/MessageAction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ Meteor.startup ->
input.updateAutogrow()
, 200
validation: (message) ->
room = RocketChat.models.Rooms.findOne({ _id: message.rid })

if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1
if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })?
return false

hasPermission = RocketChat.authz.hasAtLeastOnePermission('edit-message', message.rid)
Expand Down Expand Up @@ -153,9 +151,7 @@ Meteor.startup ->
RocketChat.MessageAction.hideDropDown()
chatMessages[Session.get('openedRoom')].confirmDeleteMsg(message)
validation: (message) ->
room = RocketChat.models.Rooms.findOne({ _id: message.rid })

if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1
if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })?
return false

hasPermission = RocketChat.authz.hasAtLeastOnePermission('delete-message', message.rid)
Expand Down Expand Up @@ -188,9 +184,7 @@ Meteor.startup ->
$(event.currentTarget).attr('data-clipboard-text', RocketChat.MessageAction.getPermaLink(message._id));
toastr.success(TAPi18n.__('Copied'))
validation: (message) ->
room = RocketChat.models.Rooms.findOne({ _id: message.rid })

if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1
if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })?
return false

return true
Expand All @@ -211,9 +205,7 @@ Meteor.startup ->
$(event.currentTarget).attr('data-clipboard-text', message)
toastr.success(TAPi18n.__('Copied'))
validation: (message) ->
room = RocketChat.models.Rooms.findOne({ _id: message.rid })

if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1
if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })?
return false

return true
Expand All @@ -238,9 +230,7 @@ Meteor.startup ->
input.focus()
RocketChat.MessageAction.hideDropDown()
validation: (message) ->
room = RocketChat.models.Rooms.findOne({ _id: message.rid })

if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1
if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })?
return false

return true
Expand Down
32 changes: 27 additions & 5 deletions packages/rocketchat-lib/lib/settings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
###
RocketChat.settings =
callbacks: {}
regexCallbacks: {}
ts: new Date

get: (_id) ->
return Meteor.settings?[_id]

get: (_id, callback) ->
if callback?
RocketChat.settings.onload _id, callback
Expand All @@ -17,9 +15,22 @@ RocketChat.settings =
callback key, value
return

if _.isRegExp(_id)
for key, value of Meteor.settings when _id.test(key)
callback key, value
return

if Meteor.settings?[_id]?
callback _id, Meteor.settings?[_id]
else
if _.isRegExp(_id)
items = []
for key, value of Meteor.settings when _id.test(key)
items.push
key: key
value: value
return items

return Meteor.settings?[_id]

set: (_id, value, callback) ->
Expand All @@ -46,6 +57,10 @@ RocketChat.settings =
for callback in RocketChat.settings.callbacks['*']
callback key, value, initialLoad

for key, value of RocketChat.settings.regexCallbacks
if value.regex.test(key)
callback(key, value) for callback in value.callbacks


onload: (key, callback) ->
# if key is '*'
Expand All @@ -57,5 +72,12 @@ RocketChat.settings =
keys = [].concat key

for k in keys
RocketChat.settings.callbacks[k] ?= []
RocketChat.settings.callbacks[k].push callback
if _.isRegExp k
RocketChat.settings.regexCallbacks[k.source] ?= {
regex: k
callbacks: []
}
RocketChat.settings.regexCallbacks[k.source].callbacks.push callback
else
RocketChat.settings.callbacks[k] ?= []
RocketChat.settings.callbacks[k].push callback
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/methods/filterATAllTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ RocketChat.callbacks.add('beforeSaveMessage', function(message) {

return message;

}, 1);
}, 1, 'filterATAllTag');
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/methods/filterBadWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ RocketChat.callbacks.add('beforeSaveMessage', function(message) {

return message;

}, 1);
}, 1, 'filterBadWords');
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Meteor.methods
joinDefaultChannels: (silenced) ->

check silenced, Match.Optional(Boolean)

if not Meteor.userId()
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/models/Messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
update =
$set:
attachments: attachments
console.log(query, update);

return @update query, update


Expand Down
11 changes: 11 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ RocketChat.models.Rooms = new class extends RocketChat.models._Base

return @findOne query, options

findOneByIdOrName: (_idOrName, options) ->
query = {
$or: [{
_id: _idOrName
}, {
name: _idOrName
}]
}

return this.findOne(query, options)

findOneByImportId: (_id, options) ->
query =
importIds: _id
Expand Down
6 changes: 6 additions & 0 deletions packages/rocketchat-lib/server/models/Subscriptions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ RocketChat.models.Subscriptions = new class extends RocketChat.models._Base

return @find query, options

findByRoomId: (roomId, options) ->
query =
"rid": roomId

return @find query, options

getLastSeen: (options = {}) ->
query = { ls: { $exists: 1 } }
options.sort = { ls: -1 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ Template.visitorInfo.helpers({

const data = Template.currentData();
if (data && data.rid) {
const room = RocketChat.models.Rooms.findOne(data.rid);
const user = Meteor.user();
return room.usernames.indexOf(user && user.username) !== -1;
const subscription = RocketChat.models.Subscriptions.findOne({ rid: data.rid });
return subscription !== undefined;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ RocketChat.callbacks.add('livechat.offlineMessage', (data) => {
};

RocketChat.Livechat.sendRequest(postData);
});
}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-email-offline-message');
4 changes: 2 additions & 2 deletions packages/rocketchat-livechat/server/hooks/sendToCRM.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function sendToCRM(hook, room) {

RocketChat.callbacks.add('livechat.closeRoom', (room) => {
return sendToCRM('closeRoom', room);
});
}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-close-room');

RocketChat.callbacks.add('livechat.saveInfo', (room) => {
return sendToCRM('saveLivechatInfo', room);
});
}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-save-info');
2 changes: 1 addition & 1 deletion packages/rocketchat-livechat/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Meteor.startup(() => {
throw new Meteor.Error(TAPi18n.__('You_cant_leave_a_livechat_room_Please_use_the_close_button', {
lng: user.language || RocketChat.settings.get('language') || 'en'
}));
}, RocketChat.callbacks.priority.LOW);
}, RocketChat.callbacks.priority.LOW, 'cant-leave-room');
});
2 changes: 1 addition & 1 deletion packages/rocketchat-markdown/markdown.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Markdown


RocketChat.Markdown = Markdown
RocketChat.callbacks.add 'renderMessage', Markdown , RocketChat.callbacks.priority.HIGH
RocketChat.callbacks.add 'renderMessage', Markdown, RocketChat.callbacks.priority.HIGH, 'markdown'

if Meteor.isClient
Blaze.registerHelper 'RocketChatMarkdown', (text) ->
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-markdown/markdowncode.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ class MarkdownCode
RocketChat.MarkdownCode = MarkdownCode

# MarkdownCode gets higher priority over Markdown so it's possible place a callback in between (katex for exmaple)
RocketChat.callbacks.add 'renderMessage', MarkdownCode, RocketChat.callbacks.priority.HIGH - 2
RocketChat.callbacks.add 'renderMessage', MarkdownCode, RocketChat.callbacks.priority.HIGH - 2, 'markdowncode'
4 changes: 2 additions & 2 deletions packages/rocketchat-mentions/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ class MentionsClient
message.html = msg
return message

RocketChat.callbacks.add 'renderMessage', MentionsClient
RocketChat.callbacks.add 'renderMentions', MentionsClient
RocketChat.callbacks.add 'renderMessage', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-message'
RocketChat.callbacks.add 'renderMentions', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-mentions'
2 changes: 1 addition & 1 deletion packages/rocketchat-mentions/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class MentionsServer
message.channels = verifiedChannels
return message

RocketChat.callbacks.add 'beforeSaveMessage', MentionsServer, RocketChat.callbacks.priority.HIGH
RocketChat.callbacks.add 'beforeSaveMessage', MentionsServer, RocketChat.callbacks.priority.HIGH, 'mentions'
Loading