Skip to content
Snippets Groups Projects
Commit 2f8c637f authored by Gabriel Engel's avatar Gabriel Engel Committed by GitHub
Browse files

Merge pull request #4623 from claranet/snippet-message

Snippet message
parents 395ef957 be4b5f7a
No related branches found
No related tags found
No related merge requests found
/* globals Package */
Package.describe({
name: 'rocketchat:message-snippet',
version: '0.0.1',
summary: 'Transform your multilines messages into snippet files.',
git: ''
});
Package.onUse(function(api) {
api.use([
'ecmascript',
'rocketchat:lib',
'rocketchat:file',
'rocketchat:markdown',
'rocketchat:theme',
'less',
'random',
'underscore',
'tracker',
'webapp'
]);
api.use([
'templating',
'kadira:flow-router'
], 'client');
// Server
api.addFiles([
'server/startup/settings.js',
'server/methods/snippetMessage.js',
'server/requests.js',
'server/publications/snippetedMessagesByRoom.js',
'server/publications/snippetedMessage.js'
], 'server');
// Client
api.addFiles([
'client/lib/collections.js',
'client/actionButton.js',
'client/messageType.js',
'client/snippetMessage.js',
'client/router.js',
'client/page/snippetPage.html',
'client/page/snippetPage.js',
'client/tabBar/tabBar.js',
'client/tabBar/views/snippetedMessages.html',
'client/tabBar/views/snippetMessage.html',
'client/tabBar/views/snippetedMessages.js',
'client/tabBar/views/snippetMessage.js',
'client/page/stylesheets/snippetPage.less'
], 'client');
});
Meteor.methods({
snippetMessage: function(message, filename) {
if ((typeof Meteor.userId() === 'undefined') || (Meteor.userId() === null)) {
//noinspection JSUnresolvedFunction
throw new Meteor.Error('error-invalid-user', 'Invalid user',
{method: 'snippetMessage'});
}
let room = RocketChat.models.Rooms.findOne({ _id: message.rid });
if ((typeof room === 'undefined') || (room === null)) {
return false;
}
if (Array.isArray(room.usernames) && (room.usernames.indexOf(Meteor.user().username) === -1)) {
return false;
}
// 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);
}
let me = RocketChat.models.Users.findOneById(Meteor.userId());
message.snippeted = true;
message.snippetedAt = Date.now;
message.snippetedBy = {
_id: Meteor.userId(),
username: me.username
};
message = RocketChat.callbacks.run('beforeSaveMessage', message);
// Create the SnippetMessage
RocketChat.models.Messages.setSnippetedByIdAndUserId(message, filename, message.snippetedBy,
message.snippeted, Date.now, filename);
RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser(
'message_snippeted', message.rid, '', me, { 'snippetId': message._id, 'snippetName': filename });
}
});
Meteor.publish('snippetedMessage', function(_id) {
if (typeof this.userId === 'undefined' || this.userId === null) {
return this.ready();
}
let snippet = RocketChat.models.Messages.findOne({'_id': _id, snippeted: true});
let user = RocketChat.models.Users.findOneById(this.userId);
let roomSnippetQuery = {
'_id': snippet.rid,
'usernames': {
'$in': [
user.username
]
}
};
if (RocketChat.models.Rooms.findOne(roomSnippetQuery) === undefined) {
return this.ready();
}
let publication = this;
if (typeof user === 'undefined' || user === null) {
return this.ready();
}
let cursor = RocketChat.models.Messages.find(
{ '_id': _id }
).observeChanges({
added: function(_id, record) {
publication.added('rocketchat_snippeted_message', _id, record);
},
changed: function(_id, record) {
publication.changed('rocketchat_snippeted_message', _id, record);
},
removed: function(_id) {
publication.removed('rocketchat_snippeted_message', _id);
}
});
this.ready();
this.onStop = function() {
cursor.stop();
};
});
Meteor.publish('snippetedMessages', function(rid, limit=50) {
if (typeof this.userId === 'undefined' || this.userId === null) {
return this.ready();
}
let publication = this;
let user = RocketChat.models.Users.findOneById(this.userId);
if (typeof user === 'undefined' || user === null) {
return this.ready();
}
let cursorHandle = RocketChat.models.Messages.findSnippetedByRoom(
rid,
{
sort: {ts: -1},
limit: limit
}
).observeChanges({
added: function(_id, record) {
publication.added('rocketchat_snippeted_message', _id, record);
},
changed: function(_id, record) {
publication.changed('rocketchat_snippeted_message', _id, record);
},
removed: function(_id) {
publication.removed('rocketchat_snippeted_message', _id);
}
});
this.ready();
this.onStop = function() {
cursorHandle.stop();
};
});
/* global Cookies */
WebApp.connectHandlers.use('/snippet/download', function(req, res) {
var cookie, rawCookies, ref, token, uid;
cookie = new Cookies();
if ((typeof req !== 'undefined' && req !== null ? (ref = req.headers) !== null ? ref.cookie : void 0 : void 0) !== null) {
rawCookies = req.headers.cookie;
}
if (rawCookies !== null) {
uid = cookie.get('rc_uid', rawCookies);
}
if (rawCookies !== null) {
token = cookie.get('rc_token', rawCookies);
}
if (uid === null) {
uid = req.query.rc_uid;
token = req.query.rc_token;
}
let user = RocketChat.models.Users.findOneByIdAndLoginToken(uid, token);
if (!(uid && token && user)) {
res.writeHead(403);
res.end();
return false;
}
var match = /^\/([^\/]+)\/(.*)/.exec(req.url);
if (match[1]) {
let snippet = RocketChat.models.Messages.findOne(
{
'_id': match[1],
'snippeted': true
}
);
let room = RocketChat.models.Rooms.findOne({ '_id': snippet.rid, 'usernames': { '$in': [user.username] }});
if (room === undefined) {
res.writeHead(403);
res.end();
return false;
}
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(snippet.snippetName)}`);
res.setHeader('Content-Type', 'application/octet-stream');
// Removing the ``` contained in the msg.
let snippetContent = snippet.msg.substr(3, snippet.msg.length - 6);
res.setHeader('Content-Length', snippetContent.length);
res.write(snippetContent);
res.end();
return;
}
res.writeHead(404);
res.end();
return;
});
Meteor.startup(function() {
RocketChat.settings.add('Message_AllowSnippeting', false, {
type: 'boolean',
public: true,
group: 'Message'
});
RocketChat.models.Permissions.upsert('snippet-message', {
$setOnInsert: {
roles: ['owner', 'moderator', 'admin']
}
});
});
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