EmailBuilder.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* eslint-disable
  2. max-len,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const _ = require('underscore')
  14. const settings = require('settings-sharelatex')
  15. const marked = require('marked')
  16. const StringHelper = require('../Helpers/StringHelper')
  17. const PersonalEmailLayout = require('./Layouts/PersonalEmailLayout')
  18. const NotificationEmailLayout = require('./Layouts/NotificationEmailLayout')
  19. const BaseWithHeaderEmailLayout = require(`./Layouts/${
  20. settings.brandPrefix
  21. }BaseWithHeaderEmailLayout`)
  22. const SpamSafe = require('./SpamSafe')
  23. // Single CTA Email
  24. const SingleCTAEmailBody = require(`./Bodies/${
  25. settings.brandPrefix
  26. }SingleCTAEmailBody`)
  27. const CTAEmailTemplate = function(content) {
  28. if (content.greeting == null) {
  29. content.greeting = () => 'Hi,'
  30. }
  31. if (content.secondaryMessage == null) {
  32. content.secondaryMessage = () => ''
  33. }
  34. return {
  35. subject(opts) {
  36. return content.subject(opts)
  37. },
  38. layout: BaseWithHeaderEmailLayout,
  39. plainTextTemplate(opts) {
  40. return `\
  41. ${content.greeting(opts)}
  42. ${content.message(opts).trim()}
  43. ${content.ctaText(opts)}: ${content.ctaURL(opts)}
  44. ${(typeof content.secondaryMessage === 'function'
  45. ? content.secondaryMessage(opts).trim()
  46. : undefined) || ''}
  47. Regards,
  48. The ${settings.appName} Team - ${settings.siteUrl}\
  49. `
  50. },
  51. compiledTemplate(opts) {
  52. return SingleCTAEmailBody({
  53. title:
  54. typeof content.title === 'function' ? content.title(opts) : undefined,
  55. greeting: content.greeting(opts),
  56. message: marked(content.message(opts).trim()),
  57. secondaryMessage: marked(content.secondaryMessage(opts).trim()),
  58. ctaText: content.ctaText(opts),
  59. ctaURL: content.ctaURL(opts),
  60. gmailGoToAction:
  61. typeof content.gmailGoToAction === 'function'
  62. ? content.gmailGoToAction(opts)
  63. : undefined,
  64. StringHelper
  65. })
  66. }
  67. }
  68. }
  69. // No CTA Email
  70. const NoCTAEmailBody = require(`./Bodies/NoCTAEmailBody`)
  71. const NoCTAEmailTemplate = function(content) {
  72. if (content.greeting == null) {
  73. content.greeting = () => 'Hi,'
  74. }
  75. if (content.secondaryMessage == null) {
  76. content.secondaryMessage = () => ''
  77. }
  78. return {
  79. subject(opts) {
  80. return content.subject(opts)
  81. },
  82. layout: BaseWithHeaderEmailLayout,
  83. plainTextTemplate(opts) {
  84. return `\
  85. ${content.greeting(opts)}
  86. ${content.message(opts).trim()}
  87. ${(typeof content.secondaryMessage === 'function'
  88. ? content.secondaryMessage(opts).trim()
  89. : undefined) || ''}
  90. Regards,
  91. The ${settings.appName} Team - ${settings.siteUrl}\
  92. `
  93. },
  94. compiledTemplate(opts) {
  95. return NoCTAEmailBody({
  96. title:
  97. typeof content.title === 'function' ? content.title(opts) : undefined,
  98. greeting: content.greeting(opts),
  99. message: marked(content.message(opts).trim()),
  100. secondaryMessage: marked(content.secondaryMessage(opts).trim()),
  101. gmailGoToAction:
  102. typeof content.gmailGoToAction === 'function'
  103. ? content.gmailGoToAction(opts)
  104. : undefined,
  105. StringHelper
  106. })
  107. }
  108. }
  109. }
  110. const templates = {}
  111. templates.accountMergeToOverleafAddress = CTAEmailTemplate({
  112. subject() {
  113. return `Confirm Account Merge - ${settings.appName}`
  114. },
  115. title() {
  116. return 'Confirm Account Merge'
  117. },
  118. message() {
  119. return `\
  120. To merge your ShareLaTeX and Overleaf accounts, click the button below.
  121. If you think you have received this message in error,
  122. please contact us at https://www.overleaf.com/contact\
  123. `
  124. },
  125. ctaText() {
  126. return 'Confirm Account Merge'
  127. },
  128. ctaURL(opts) {
  129. return opts.tokenLinkUrl
  130. }
  131. })
  132. templates.accountMergeToSharelatexAddress =
  133. templates.accountMergeToOverleafAddress
  134. templates.registered = CTAEmailTemplate({
  135. subject() {
  136. return `Activate your ${settings.appName} Account`
  137. },
  138. message(opts) {
  139. return `\
  140. Congratulations, you've just had an account created for you on ${
  141. settings.appName
  142. } with the email address '${_.escape(opts.to)}'.
  143. Click here to set your password and log in:\
  144. `
  145. },
  146. secondaryMessage() {
  147. return `If you have any questions or problems, please contact ${
  148. settings.adminEmail
  149. }`
  150. },
  151. ctaText() {
  152. return 'Set password'
  153. },
  154. ctaURL(opts) {
  155. return opts.setNewPasswordUrl
  156. }
  157. })
  158. templates.canceledSubscription = CTAEmailTemplate({
  159. subject() {
  160. return `${settings.appName} thoughts`
  161. },
  162. message() {
  163. return `\
  164. I'm sorry to see you cancelled your ${
  165. settings.appName
  166. } premium account. Would you mind giving us some feedback on what the site is lacking at the moment via this quick survey?\
  167. `
  168. },
  169. secondaryMessage() {
  170. return 'Thank you in advance!'
  171. },
  172. ctaText() {
  173. return 'Leave Feedback'
  174. },
  175. ctaURL(opts) {
  176. return 'https://docs.google.com/forms/d/e/1FAIpQLScqU6Je1r4Afz6ul6oY0RAfN7RabdWv_oL1u7Rj1YBmXS4fiQ/viewform?usp=sf_link'
  177. }
  178. })
  179. templates.reactivatedSubscription = CTAEmailTemplate({
  180. subject() {
  181. return `Subscription Reactivated - ${settings.appName}`
  182. },
  183. message(opts) {
  184. return `\
  185. Your subscription was reactivated successfully.\
  186. `
  187. },
  188. ctaText() {
  189. return 'View Subscription Dashboard'
  190. },
  191. ctaURL(opts) {
  192. return `${settings.siteUrl}/user/subscription`
  193. }
  194. })
  195. templates.passwordResetRequested = CTAEmailTemplate({
  196. subject() {
  197. return `Password Reset - ${settings.appName}`
  198. },
  199. title() {
  200. return 'Password Reset'
  201. },
  202. message() {
  203. return `We got a request to reset your ${settings.appName} password.`
  204. },
  205. secondaryMessage() {
  206. return `\
  207. If you ignore this message, your password won't be changed.
  208. If you didn't request a password reset, let us know.\
  209. `
  210. },
  211. ctaText() {
  212. return 'Reset password'
  213. },
  214. ctaURL(opts) {
  215. return opts.setNewPasswordUrl
  216. }
  217. })
  218. templates.passwordChanged = NoCTAEmailTemplate({
  219. subject(opts) {
  220. return `Password Changed - ${settings.appName}`
  221. },
  222. title(opts) {
  223. return `Password Changed`
  224. },
  225. message(opts) {
  226. return `We're contacting you to notify you that your password has been set or changed.
  227. If you have recently set your password for the first time, or if you just changed your password, you don't need to take any further action. If you didn't set or change your password, please contact us.`
  228. }
  229. })
  230. templates.confirmEmail = CTAEmailTemplate({
  231. subject() {
  232. return `Confirm Email - ${settings.appName}`
  233. },
  234. title() {
  235. return 'Confirm Email'
  236. },
  237. message() {
  238. return `Please confirm your email on ${settings.appName}.`
  239. },
  240. ctaText() {
  241. return 'Confirm Email'
  242. },
  243. ctaURL(opts) {
  244. return opts.confirmEmailUrl
  245. }
  246. })
  247. templates.projectInvite = CTAEmailTemplate({
  248. subject(opts) {
  249. return `${_.escape(
  250. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  251. )} - shared by ${_.escape(
  252. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  253. )}`
  254. },
  255. title(opts) {
  256. return `${_.escape(
  257. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  258. )} - shared by ${_.escape(
  259. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  260. )}`
  261. },
  262. message(opts) {
  263. return `${_.escape(
  264. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  265. )} wants to share ${_.escape(
  266. SpamSafe.safeProjectName(opts.project.name, 'a new project')
  267. )} with you.`
  268. },
  269. ctaText() {
  270. return 'View project'
  271. },
  272. ctaURL(opts) {
  273. return opts.inviteUrl
  274. },
  275. gmailGoToAction(opts) {
  276. return {
  277. target: opts.inviteUrl,
  278. name: 'View project',
  279. description: `Join ${_.escape(
  280. SpamSafe.safeProjectName(opts.project.name, 'project')
  281. )} at ${settings.appName}`
  282. }
  283. }
  284. })
  285. templates.verifyEmailToJoinTeam = CTAEmailTemplate({
  286. subject(opts) {
  287. return `${_.escape(
  288. SpamSafe.safeUserName(opts.inviterName, 'A collaborator')
  289. )} has invited you to join a team on ${settings.appName}`
  290. },
  291. title(opts) {
  292. return `${_.escape(
  293. SpamSafe.safeUserName(opts.inviterName, 'A collaborator')
  294. )} has invited you to join a team on ${settings.appName}`
  295. },
  296. message(opts) {
  297. return `Please click the button below to join the team and enjoy the benefits of an upgraded ${
  298. settings.appName
  299. } account.`
  300. },
  301. ctaText(opts) {
  302. return 'Join now'
  303. },
  304. ctaURL(opts) {
  305. return opts.acceptInviteUrl
  306. }
  307. })
  308. templates.dropboxUnlinkedDuplicate = CTAEmailTemplate({
  309. subject() {
  310. return `Your Dropbox Account has been Unlinked - ${settings.appName}`
  311. },
  312. message(opts) {
  313. return `\
  314. Our automated systems have detected that your Dropbox account was linked to more than one Overleaf accounts. This should not have been allowed and might be causing issues with the Dropbox sync feature.
  315. We have now unlinked all your Dropbox and Overleaf Accounts. To ensure your project will keep syncing you can link your Dropbox account to the Overleaf account of your choice now.\
  316. `
  317. },
  318. ctaText() {
  319. return 'Link Dropbox Account'
  320. },
  321. ctaURL(opts) {
  322. return `${settings.siteUrl}/user/settings`
  323. }
  324. })
  325. templates.testEmail = CTAEmailTemplate({
  326. subject() {
  327. return `A Test Email from ${settings.appName}`
  328. },
  329. title() {
  330. return `A Test Email from ${settings.appName}`
  331. },
  332. greeting() {
  333. return 'Hi,'
  334. },
  335. message() {
  336. return `This is a test Email from ${settings.appName}`
  337. },
  338. ctaText() {
  339. return `Open ${settings.appName}`
  340. },
  341. ctaURL() {
  342. return settings.siteUrl
  343. }
  344. })
  345. templates.projectsTransferredFromSharelatex = CTAEmailTemplate({
  346. subject() {
  347. return 'ShareLaTeX projects transferred to your Overleaf account'
  348. },
  349. title() {
  350. return 'ShareLaTeX projects transferred to your Overleaf account'
  351. },
  352. message(opts) {
  353. return `\
  354. We are writing with important information about your Overleaf and ShareLaTeX accounts.
  355. As part of our ongoing work to [integrate Overleaf and ShareLaTeX](https://www.overleaf.com/blog/518-exciting-news-sharelatex-is-joining-overleaf),
  356. we found a ShareLaTeX account with the email address ${
  357. opts.to
  358. } that matches your Overleaf account.
  359. We have now transferred the projects from this ShareLaTeX account into your Overleaf account, so you may notice some new
  360. projects on your Overleaf projects page.
  361. When you next log in, you may be prompted to reconfirm your email address in order to regain access to your account.
  362. If you have any questions, please contact our support team by reply.\
  363. `
  364. },
  365. ctaText() {
  366. return `Log in to ${settings.appName}`
  367. },
  368. ctaURL() {
  369. return settings.siteUrl + '/login'
  370. }
  371. })
  372. templates.emailAddressPoachedEmail = CTAEmailTemplate({
  373. subject() {
  374. return `One of your email addresses has been moved to another ${
  375. settings.appName
  376. } account`
  377. },
  378. title() {
  379. return `One of your email addresses has been moved to another ${
  380. settings.appName
  381. } account`
  382. },
  383. message(opts) {
  384. let message = `\
  385. We are writing with important information about your Overleaf account.
  386. You added the email address ${opts.poached} to your ${
  387. opts.to
  388. } Overleaf account as a secondary (or affiliation)
  389. email address, but we have had to remove it.
  390. This is because your ${
  391. opts.poached
  392. } email address was also in use as the primary email address for an older Overleaf
  393. account from before our [integration with ShareLaTeX to create Overleaf v2](https://www.overleaf.com/blog/518-exciting-news-sharelatex-is-joining-overleaf).
  394. ### What do I need to do?
  395. You now have two Overleaf accounts, one under ${opts.poached} and one under ${
  396. opts.to
  397. }.
  398. You may wish to log in to Overleaf as ${
  399. opts.poached
  400. } to check whether you have projects there that you would like to
  401. keep. If you are not sure of the password, you can send yourself a password reset email to ${
  402. opts.poached
  403. }, via
  404. https://www.overleaf.com/user/password/reset
  405. Once you have downloaded your projects, you may wish to delete your
  406. ${
  407. opts.poached
  408. } Overleaf account, which you can do from your account settings. You will then be able to add
  409. ${opts.poached} as a secondary email address on your ${opts.to} account again.
  410. \
  411. `
  412. if (opts.proFeatures) {
  413. message += `\
  414. Because your ${
  415. opts.poached
  416. } email address was an institutional affiliation through which you had Pro features. Your Pro
  417. features have been transferred to your ${
  418. opts.poached
  419. } account. If you would like to transfer them back to your
  420. ${opts.to} account, you will need to delete the ${
  421. opts.poached
  422. } account and re-add it as a secondary email address,
  423. as described above.
  424. \
  425. `
  426. }
  427. message += `\
  428. If you have any questions, you can contact our support team by reply.\
  429. `
  430. return message
  431. },
  432. ctaText() {
  433. return `Log in to ${settings.appName}`
  434. },
  435. ctaURL() {
  436. return settings.siteUrl + '/login'
  437. }
  438. })
  439. templates.emailThirdPartyIdentifierLinked = NoCTAEmailTemplate({
  440. subject(opts) {
  441. return `Your ${settings.appName} account is now linked with ${
  442. opts.provider
  443. }`
  444. },
  445. title(opts) {
  446. return `Accounts Linked`
  447. },
  448. message(opts) {
  449. let message = `We're contacting you to notify you that your ${opts.provider}
  450. account is now linked to your ${settings.appName} account.`
  451. return message
  452. }
  453. })
  454. templates.emailThirdPartyIdentifierUnlinked = NoCTAEmailTemplate({
  455. subject(opts) {
  456. return `Your ${settings.appName} account is no longer linked with ${
  457. opts.provider
  458. }`
  459. },
  460. title(opts) {
  461. return `Accounts No Longer Linked`
  462. },
  463. message(opts) {
  464. let message = `We're contacting you to notify you that your ${opts.provider}
  465. account is no longer linked with your ${settings.appName} account.`
  466. return message
  467. }
  468. })
  469. module.exports = {
  470. templates,
  471. CTAEmailTemplate,
  472. NoCTAEmailTemplate,
  473. buildEmail(templateName, opts) {
  474. const template = templates[templateName]
  475. opts.siteUrl = settings.siteUrl
  476. opts.body = template.compiledTemplate(opts)
  477. if (
  478. settings.email &&
  479. settings.email.template &&
  480. settings.email.template.customFooter
  481. ) {
  482. opts.body += settings.email.template.customFooter
  483. }
  484. return {
  485. subject: template.subject(opts),
  486. html: template.layout(opts),
  487. text: __guardMethod__(template, 'plainTextTemplate', o =>
  488. o.plainTextTemplate(opts)
  489. )
  490. }
  491. }
  492. }
  493. function __guardMethod__(obj, methodName, transform) {
  494. if (
  495. typeof obj !== 'undefined' &&
  496. obj !== null &&
  497. typeof obj[methodName] === 'function'
  498. ) {
  499. return transform(obj, methodName)
  500. } else {
  501. return undefined
  502. }
  503. }