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

Convert Mailer Package to Js #6780

Merged
merged 2 commits into from
Apr 27, 2017
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
10 changes: 0 additions & 10 deletions packages/rocketchat-mailer/client/router.coffee

This file was deleted.

16 changes: 16 additions & 0 deletions packages/rocketchat-mailer/client/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FlowRouter.route('/mailer', {
name: 'mailer',
action() {
return BlazeLayout.render('main', {
center: 'mailer'
});
}
});

FlowRouter.route('/mailer/unsubscribe/:_id/:createdAt', {
name: 'mailer-unsubscribe',
action(params) {
Meteor.call('Mailer:unsubscribe', params._id, params.createdAt);
return BlazeLayout.render('mailerUnsubscribe');
}
});
5 changes: 0 additions & 5 deletions packages/rocketchat-mailer/client/startup.coffee

This file was deleted.

7 changes: 7 additions & 0 deletions packages/rocketchat-mailer/client/startup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RocketChat.AdminBox.addOption({
href: 'mailer',
i18nLabel: 'Mailer',
permissionGranted() {
return RocketChat.authz.hasAllPermission('access-mailer');
}
});
25 changes: 0 additions & 25 deletions packages/rocketchat-mailer/client/views/mailer.coffee

This file was deleted.

31 changes: 31 additions & 0 deletions packages/rocketchat-mailer/client/views/mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import toastr from 'toastr';
Template.mailer.helpers({
fromEmail() {
return RocketChat.settings.get('From_Email');
}
});

Template.mailer.events({
'click .send'(e, t) {
e.preventDefault();
const from = $(t.find('[name=from]')).val();
const subject = $(t.find('[name=subject]')).val();
const body = $(t.find('[name=body]')).val();
const dryrun = $(t.find('[name=dryrun]:checked')).val();
const query = $(t.find('[name=query]')).val();
if (!from) {
toastr.error(TAPi18n.__('error-invalid-from-address'));
return;
}
if (body.indexOf('[unsubscribe]') === -1) {
toastr.error(TAPi18n.__('error-missing-unsubscribe-link'));
return;
}
return Meteor.call('Mailer.sendMail', from, subject, body, dryrun, query, function(err) {
if (err) {
return handleError(err);
}
return toastr.success(TAPi18n.__('The_emails_are_being_sent'));
});
}
});

This file was deleted.

3 changes: 3 additions & 0 deletions packages/rocketchat-mailer/client/views/mailerUnsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Template.mailerUnsubscribe.onRendered(function() {
return $('#initial-page-loading').remove();
});
1 change: 0 additions & 1 deletion packages/rocketchat-mailer/lib/Mailer.coffee

This file was deleted.

1 change: 1 addition & 0 deletions packages/rocketchat-mailer/lib/Mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mailer = {};//eslint-disable-line
23 changes: 11 additions & 12 deletions packages/rocketchat-mailer/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'ecmascript',
'coffeescript',
'ddp-rate-limiter',
'kadira:flow-router',
'rocketchat:lib',
Expand All @@ -16,24 +15,24 @@ Package.onUse(function(api) {

api.use('templating', 'client');

api.addFiles('lib/Mailer.coffee');
api.addFiles('lib/Mailer.js');

api.addFiles([
'client/startup.coffee',
'client/router.coffee',
'client/startup.js',
'client/router.js',
'client/views/mailer.html',
'client/views/mailer.coffee',
'client/views/mailer.js',
'client/views/mailerUnsubscribe.html',
'client/views/mailerUnsubscribe.coffee'
'client/views/mailerUnsubscribe.js'
], 'client');

api.addFiles([
'server/startup.coffee',
'server/models/Users.coffee',
'server/functions/sendMail.coffee',
'server/functions/unsubscribe.coffee',
'server/methods/sendMail.coffee',
'server/methods/unsubscribe.coffee'
'server/startup.js',
'server/models/Users.js',
'server/functions/sendMail.js',
'server/functions/unsubscribe.js',
'server/methods/sendMail.js',
'server/methods/unsubscribe.js'
], 'server');

api.export('Mailer');
Expand Down
63 changes: 0 additions & 63 deletions packages/rocketchat-mailer/server/functions/sendMail.coffee

This file was deleted.

80 changes: 80 additions & 0 deletions packages/rocketchat-mailer/server/functions/sendMail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*globals Mailer */
Mailer.sendMail = function(from, subject, body, dryrun, query) {

const rfcMailPatternWithName = /^(?:.*<)?([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)(?:>?)$/;
if (!rfcMailPatternWithName.test(from)) {
throw new Meteor.Error('error-invalid-from-address', 'Invalid from address', {
'function': 'Mailer.sendMail'
});
}
if (body.indexOf('[unsubscribe]') === -1) {
throw new Meteor.Error('error-missing-unsubscribe-link', 'You must provide the [unsubscribe] link.', {
'function': 'Mailer.sendMail'
});
}
const header = RocketChat.placeholders.replace(RocketChat.settings.get('Email_Header') || '');
const footer = RocketChat.placeholders.replace(RocketChat.settings.get('Email_Footer') || '');

let userQuery = { 'mailer.unsubscribed': { $exists: 0 } };
if (query) {
userQuery = { $and: [ userQuery, EJSON.parse(query) ] };
}

if (dryrun) {
return Meteor.users.find({
'emails.address': from
}).forEach((user) => {
let email = undefined;
if (user.emails && user.emails[0] && user.emails[0].address) {
email = user.emails[0].address;
}
const html = RocketChat.placeholders.replace(body, {
unsubscribe: Meteor.absoluteUrl(FlowRouter.path('mailer/unsubscribe/:_id/:createdAt', {
_id: user._id,
createdAt: user.createdAt.getTime()
})),
name: user.name,
email
});
email = `${ user.name } <${ email }>`;
if (rfcMailPatternWithName.test(email)) {
Meteor.defer(function() {
return Email.send({
to: email,
from,
subject,
html: header + html + footer
});
});
return console.log(`Sending email to ${ email }`);
}
});
} else {
return Meteor.users.find(userQuery).forEach(function(user) {
let email = undefined;
if (user.emails && user.emails[0] && user.emails[0].address) {
email = user.emails[0].address;
}
const html = RocketChat.placeholders.replace(body, {
unsubscribe: Meteor.absoluteUrl(FlowRouter.path('mailer/unsubscribe/:_id/:createdAt', {
_id: user._id,
createdAt: user.createdAt.getTime()
})),
name: user.name,
email
});
email = `${ user.name } <${ email }>`;
if (rfcMailPatternWithName.test(email)) {
Meteor.defer(function() {
return Email.send({
to: email,
from,
subject,
html: header + html + footer
});
});
return console.log(`Sending email to ${ email }`);
}
});
}
};

This file was deleted.

7 changes: 7 additions & 0 deletions packages/rocketchat-mailer/server/functions/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* globals Mailer */
Mailer.unsubscribe = function(_id, createdAt) {
if (_id && createdAt) {
return RocketChat.models.Users.rocketMailUnsubscribe(_id, createdAt) === 1;
}
return false;
};
16 changes: 0 additions & 16 deletions packages/rocketchat-mailer/server/methods/sendMail.coffee

This file was deleted.

25 changes: 25 additions & 0 deletions packages/rocketchat-mailer/server/methods/sendMail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*globals Mailer */
Meteor.methods({
'Mailer.sendMail'(from, subject, body, dryrun, query) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'Mailer.sendMail'
});
}
if (RocketChat.authz.hasRole(userId, 'admin') !== true) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'Mailer.sendMail'
});
}
return Mailer.sendMail(from, subject, body, dryrun, query);
}
});


//Limit setting username once per minute
//DDPRateLimiter.addRule
// type: 'method'
// name: 'Mailer.sendMail'
// connectionId: -> return true
// , 1, 60000
10 changes: 0 additions & 10 deletions packages/rocketchat-mailer/server/methods/unsubscribe.coffee

This file was deleted.

14 changes: 14 additions & 0 deletions packages/rocketchat-mailer/server/methods/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*globals Mailer */
Meteor.methods({
'Mailer:unsubscribe'(_id, createdAt) {
return Mailer.unsubscribe(_id, createdAt);
}
});

DDPRateLimiter.addRule({
type: 'method',
name: 'Mailer:unsubscribe',
connectionId() {
return true;
}
}, 1, 60000);
17 changes: 0 additions & 17 deletions packages/rocketchat-mailer/server/models/Users.coffee

This file was deleted.

Loading