send email when admin unflags post (#7580)

* send email when admin unflags post

* Improved email acquisition code, minor changes to sent email

* Refactor getGoupUrl into its own method

* added unit test for getGroupUrl

* improved test and getGroupUrl
This commit is contained in:
Shervin Sarain
2016-06-08 05:22:33 +02:00
committed by Blade Barringer
parent 00491670e0
commit 8490b481f6
3 changed files with 65 additions and 25 deletions

View File

@@ -5,6 +5,7 @@ import nodemailer from 'nodemailer';
import Bluebird from 'bluebird'; import Bluebird from 'bluebird';
import requireAgain from 'require-again'; import requireAgain from 'require-again';
import logger from '../../../../../website/server/libs/api-v3/logger'; import logger from '../../../../../website/server/libs/api-v3/logger';
import { TAVERN_ID } from '../../../../../website/server/models/group';
function defer () { function defer () {
let resolve; let resolve;
@@ -137,6 +138,23 @@ describe('emails', () => {
}); });
}); });
describe('getGroupUrl', () => {
it('returns correct url if group is the tavern', () => {
let getGroupUrl = require(pathToEmailLib).getGroupUrl;
expect(getGroupUrl({_id: TAVERN_ID, type: 'guild'})).to.eql('/#/options/groups/tavern');
});
it('returns correct url if group is a guild', () => {
let getGroupUrl = require(pathToEmailLib).getGroupUrl;
expect(getGroupUrl({_id: 'random _id', type: 'guild'})).to.eql('/#/options/groups/guilds/random _id');
});
it('returns correct url if group is a party', () => {
let getGroupUrl = require(pathToEmailLib).getGroupUrl;
expect(getGroupUrl({_id: 'random _id', type: 'party'})).to.eql('party');
});
});
describe('sendTxnEmail', () => { describe('sendTxnEmail', () => {
beforeEach(() => { beforeEach(() => {
sandbox.stub(request, 'post'); sandbox.stub(request, 'post');

View File

@@ -1,8 +1,5 @@
import { authWithHeaders } from '../../middlewares/api-v3/auth'; import { authWithHeaders } from '../../middlewares/api-v3/auth';
import { import { model as Group } from '../../models/group';
model as Group,
TAVERN_ID,
} from '../../models/group';
import { model as User } from '../../models/user'; import { model as User } from '../../models/user';
import { import {
NotFound, NotFound,
@@ -10,7 +7,7 @@ import {
} from '../../libs/api-v3/errors'; } from '../../libs/api-v3/errors';
import _ from 'lodash'; import _ from 'lodash';
import { removeFromArray } from '../../libs/api-v3/collectionManipulators'; import { removeFromArray } from '../../libs/api-v3/collectionManipulators';
import { sendTxn } from '../../libs/api-v3/email'; import { getUserInfo, getGroupUrl, sendTxn } from '../../libs/api-v3/email';
import nconf from 'nconf'; import nconf from 'nconf';
import Bluebird from 'bluebird'; import Bluebird from 'bluebird';
@@ -209,28 +206,11 @@ api.flagChat = {
update update
); );
let reporterEmailContent; let reporterEmailContent = getUserInfo(user, ['email']).email;
if (user.auth.local) {
reporterEmailContent = user.auth.local.email;
} else if (user.auth.facebook && user.auth.facebook.emails && user.auth.facebook.emails[0]) {
reporterEmailContent = user.auth.facebook.emails[0].value;
}
let authorEmailContent; let authorEmailContent = getUserInfo(author, ['email']).email;
if (author.auth.local) {
authorEmailContent = author.auth.local.email;
} else if (author.auth.facebook && author.auth.facebook.emails && author.auth.facebook.emails[0]) {
authorEmailContent = author.auth.facebook.emails[0].value;
}
let groupUrl; let groupUrl = getGroupUrl(group);
if (group._id === TAVERN_ID) {
groupUrl = '/#/options/groups/tavern';
} else if (group.type === 'guild') {
groupUrl = `/#/options/groups/guilds/${group._id}`;
} else {
groupUrl = 'party';
}
sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods', [ sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods', [
{name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()}, {name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()},
@@ -300,6 +280,34 @@ api.clearChatFlags = {
{$set: {'chat.$.flagCount': message.flagCount}} {$set: {'chat.$.flagCount': message.flagCount}}
); );
let adminEmailContent = getUserInfo(user, ['email']).email;
let author = await User.findOne({_id: message.uuid}, {auth: 1});
let authorEmailContent = getUserInfo(author, ['email']).email;
let groupUrl = getGroupUrl(group);
sendTxn(FLAG_REPORT_EMAILS, 'unflag-report-to-mods', [
{name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()},
{name: 'MESSAGE_TEXT', content: message.text},
{name: 'ADMIN_USERNAME', content: user.profile.name},
{name: 'ADMIN_UUID', content: user._id},
{name: 'ADMIN_EMAIL', content: adminEmailContent},
{name: 'ADMIN_MODAL_URL', content: `/static/front/#?memberId=${user._id}`},
{name: 'AUTHOR_USERNAME', content: message.user},
{name: 'AUTHOR_UUID', content: message.uuid},
{name: 'AUTHOR_EMAIL', content: authorEmailContent},
{name: 'AUTHOR_MODAL_URL', content: `/static/front/#?memberId=${message.uuid}`},
{name: 'GROUP_NAME', content: group.name},
{name: 'GROUP_TYPE', content: group.type},
{name: 'GROUP_ID', content: group._id},
{name: 'GROUP_URL', content: groupUrl},
]);
res.respond(200, {}); res.respond(200, {});
}, },
}; };

View File

@@ -1,5 +1,6 @@
import { createTransport } from 'nodemailer'; import { createTransport } from 'nodemailer';
import nconf from 'nconf'; import nconf from 'nconf';
import { TAVERN_ID } from '../../models/group';
import { encrypt } from './encryption'; import { encrypt } from './encryption';
import request from 'request'; import request from 'request';
import logger from './logger'; import logger from './logger';
@@ -64,6 +65,19 @@ export function getUserInfo (user, fields = []) {
return info; return info;
} }
export function getGroupUrl (group) {
let groupUrl;
if (group._id === TAVERN_ID) {
groupUrl = '/#/options/groups/tavern';
} else if (group.type === 'guild') {
groupUrl = `/#/options/groups/guilds/${group._id}`;
} else if (group.type === 'party') {
groupUrl = 'party';
}
return groupUrl;
}
// Send a transactional email using Mandrill through the external email server // Send a transactional email using Mandrill through the external email server
export function sendTxn (mailingInfoArray, emailType, variables, personalVariables) { export function sendTxn (mailingInfoArray, emailType, variables, personalVariables) {
mailingInfoArray = Array.isArray(mailingInfoArray) ? mailingInfoArray : [mailingInfoArray]; mailingInfoArray = Array.isArray(mailingInfoArray) ? mailingInfoArray : [mailingInfoArray];