EmailBuilder.js 15 KB

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