Skip to content

Commit

Permalink
render html/markdown for new comment notification email (Chocobozzz#3255
Browse files Browse the repository at this point in the history
)

Co-authored-by: kimsible <[email protected]>
Co-authored-by: Rigel Kent <[email protected]>
  • Loading branch information
3 people authored and fflorent committed Dec 31, 2020
1 parent 5447821 commit a0bb002
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 34 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"karma-jasmine-html-reporter": "^1.5.0",
"linkifyjs": "^2.1.5",
"lodash-es": "^4.17.4",
"markdown-it": "^11.0.0",
"markdown-it": "11.x",
"node-sass": "^4.9.3",
"npm-font-source-sans-pro": "^1.0.2",
"p2p-media-loader-hlsjs": "^0.6.2",
Expand Down
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7218,10 +7218,10 @@ map-visit@^1.0.0:
dependencies:
object-visit "^1.0.0"

markdown-it@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-11.0.0.tgz#dbfc30363e43d756ebc52c38586b91b90046b876"
integrity sha512-+CvOnmbSubmQFSA9dKz1BRiaSMV7rhexl3sngKqFyXSagoA3fBdJQ8oZWtRy2knXdpDXaBw44euz37DeJQ9asg==
markdown-it@11.x:
version "11.0.1"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-11.0.1.tgz#b54f15ec2a2193efa66dda1eb4173baea08993d6"
integrity sha512-aU1TzmBKcWNNYvH9pjq6u92BML+Hz3h5S/QpfTFwiQF852pLT+9qHsrhM9JYipkOXZxGn+sGH8oyJE9FD9WezQ==
dependencies:
argparse "^1.0.7"
entities "~2.0.0"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"lodash": "^4.17.10",
"lru-cache": "^6.0.0",
"magnet-uri": "^5.1.4",
"markdown-it": "11.x",
"memoizee": "^0.4.14",
"morgan": "^1.5.3",
"multer": "^1.1.0",
Expand All @@ -133,6 +134,7 @@
"redis": "^3.0.2",
"reflect-metadata": "^0.1.12",
"request": "^2.81.0",
"sanitize-html": "2.x",
"scripty": "^2.0.0",
"sequelize": "5.21.13",
"sequelize-typescript": "^1.0.0-beta.4",
Expand Down
51 changes: 51 additions & 0 deletions server/lib/emailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,53 @@ import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorF
import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video'
import { JobQueue } from './job-queue'

const sanitizeHtml = require('sanitize-html')
const markdownItEmoji = require('markdown-it-emoji/light')
const MarkdownItClass = require('markdown-it')
const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })

markdownIt.enable([
'linkify',
'autolink',
'emphasis',
'link',
'newline',
'list'
])

markdownIt.use(markdownItEmoji)

const toSafeHtml = text => {
// Restore line feed
const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')

// Convert possible markdown (emojis, emphasis and lists) to html
const html = markdownIt.render(textWithLineFeed)

// Convert to safe Html
return sanitizeHtml(html, {
allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ],
allowedSchemes: [ 'http', 'https' ],
allowedAttributes: {
a: [ 'href', 'class', 'target', 'rel' ]
},
transformTags: {
a: (tagName, attribs) => {
let rel = 'noopener noreferrer'
if (attribs.rel === 'me') rel += ' me'

return {
tagName,
attribs: Object.assign(attribs, {
target: '_blank',
rel
})
}
}
}
})
}

const Email = require('email-templates')

class Emailer {
Expand Down Expand Up @@ -236,6 +283,7 @@ class Emailer {
const video = comment.Video
const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
const commentHtml = toSafeHtml(comment.text)

const emailPayload: EmailPayload = {
template: 'video-comment-new',
Expand All @@ -245,6 +293,7 @@ class Emailer {
accountName: comment.Account.getDisplayName(),
accountUrl: comment.Account.Actor.url,
comment,
commentHtml,
video,
videoUrl,
action: {
Expand All @@ -262,13 +311,15 @@ class Emailer {
const video = comment.Video
const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
const commentHtml = toSafeHtml(comment.text)

const emailPayload: EmailPayload = {
template: 'video-comment-mention',
to,
subject: 'Mention on video ' + video.name,
locals: {
comment,
commentHtml,
video,
videoUrl,
accountName,
Expand Down
6 changes: 3 additions & 3 deletions server/lib/emails/video-comment-mention/html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block title

block content
p.
#[a(href=accountUrl title=handle) #{accountName}] mentioned you in a comment on video
#[a(href=accountUrl title=handle) #{accountName}] mentioned you in a comment on video
"#[a(href=videoUrl) #{video.name}]":
blockquote #{comment.text}
br(style="display: none;")
blockquote !{commentHtml}
br(style="display: none;")
6 changes: 3 additions & 3 deletions server/lib/emails/video-comment-new/html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block title

block content
p.
#[a(href=accountUrl title=handle) #{accountName}] added a comment on your video
#[a(href=accountUrl title=handle) #{accountName}] added a comment on your video
"#[a(href=videoUrl) #{video.name}]":
blockquote #{comment.text}
br(style="display: none;")
blockquote !{commentHtml}
br(style="display: none;")
Loading

0 comments on commit a0bb002

Please sign in to comment.