Skip to content

Commit

Permalink
Maintenance: Refactor building of email addresses in CoffeeScript.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgruner committed Jan 27, 2023
1 parent e464997 commit 9b0b496
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
Expand Up @@ -116,12 +116,9 @@ class App.ObjectOrganizationAutocompletion extends App.Controller
if objectId && App[@objectSingle].exists(objectId)
object = App[@objectSingle].find(objectId)
name = object.displayName()
if object.email

# quote name for special character
if name.match(/\@|,|;|\^|\+|#|§|\$|%|&|\/|\(|\)|=|\?|!|\*|\[|\]/)
name = "\"#{name}\""
name += " <#{object.email}>"
if object.email
name = App.Utils.buildEmailAddress(object.displayName(), object.email)

@objectSelect.val(name)

Expand Down Expand Up @@ -342,6 +339,8 @@ class App.ObjectOrganizationAutocompletion extends App.Controller
object = App[@objectSingle].find(value)
name = object.displayName()
if object.email
# Do not use App.Utils.buildEmailAddress here because we don't build an email address and the
# quoting would confuse sorting in the GUI.
name += " <#{object.email}>"
else if @params && @params["#{@attribute.name}_completion"]
name = @params["#{@attribute.name}_completion"]
Expand Down
Expand Up @@ -72,7 +72,7 @@ class App.FullQuoteHeader
output = "#{user.displayName()}"

if !user.permission('ticket.agent') && user.email
output += " <#{user.email}>"
output = App.Utils.buildEmailAddress(user.displayName(), user.email)

output

Expand Down
Expand Up @@ -17,6 +17,8 @@ class App.UserOrganizationAutocompletion extends App.ObjectOrganizationAutocompl
buildObjectItem: (object) =>
realname = object.displayName()
if @Config.get('ui_user_organization_selector_with_email') && !_.isEmpty(object.email)
# Do not use App.Utils.buildEmailAddress here because we don't build an email address and the
# quoting would confuse sorting in the GUI.
realname += " <#{object.email}>"

icon = @objectIcon
Expand Down
11 changes: 11 additions & 0 deletions app/assets/javascripts/app/lib/app_post/utils.coffee
Expand Up @@ -1536,3 +1536,14 @@ class App.Utils
return 'image/jpeg'

return 'image/png'

# Constructs a recipient email address with display name in a safe way
@buildEmailAddress: (display_name, email) ->
if !display_name || display_name is undefined || display_name is null
return email

# Inspired by https://github.com/closeio/addresscompiler/blob/master/src/addresscompiler.js
if (!/^[\w ']*$/.test(display_name) && /^[\x00-\x7F\xA0-\xFF]*$/.test(display_name))
display_name = '"' + display_name.replace(/([\\"])/g, '\\$1') + '"'

return display_name + ' <' + email + '>'
2 changes: 2 additions & 0 deletions app/assets/javascripts/app/models/user.coffee
Expand Up @@ -417,6 +417,8 @@ class App.User extends App.Model

displayName: ->
if @realname
# Do not use App.Utils.buildEmailAddress here because we don't build an email address and the
# quoting would confuse sorting in the GUI.
return "#{@realname} <#{@email}>"
if !_.isEmpty(@firstname)
name = @firstname
Expand Down
23 changes: 23 additions & 0 deletions public/assets/tests/qunit/html_utils.js
Expand Up @@ -3575,3 +3575,26 @@ QUnit.test("#safeParseHtml", assert => {
result = App.Utils.safeParseHtml(html)
assert.equal(unwrap(result), should)
})

QUnit.test("#buildEmailAddress", assert => {
assert.equal(
App.Utils.buildEmailAddress(undefined, 'undef@example.com'),
'undef@example.com'
)
assert.equal(
App.Utils.buildEmailAddress(null, 'null@example.com'),
'null@example.com'
)
assert.equal(
App.Utils.buildEmailAddress('', 'nobody@example.com'),
'nobody@example.com'
)
assert.equal(
App.Utils.buildEmailAddress('John Doe', 'john.doe@example.com'),
'John Doe <john.doe@example.com>'
)
assert.equal(
App.Utils.buildEmailAddress('Somebody @ "Company"', 'some.body@example.com'),
'"Somebody @ \\"Company\\"" <some.body@example.com>'
)
})
2 changes: 1 addition & 1 deletion test/browser/agent_user_manage_test.rb
Expand Up @@ -8,7 +8,7 @@ def test_agent_customer_ticket_create
customer_user_email = "customer-test-#{random_number}@example.com"
firstname = "Customer Firstname #{random_number}"
lastname = 'Customer Lastname'
fullname = "#{firstname} #{lastname} <#{customer_user_email}>"
fullname = "\"#{firstname} #{lastname}\" <#{customer_user_email}>"

@browser = browser_instance
login(
Expand Down

0 comments on commit 9b0b496

Please sign in to comment.