Improved twitter length calculation. If streaming api is used, fetch parent tweet via REST api. Added initials support as article feature.

This commit is contained in:
Martin Edenhofer 2017-06-23 09:37:54 +02:00
parent 62e5be1ed2
commit 691b63d011
4 changed files with 161 additions and 57 deletions

View file

@ -85,7 +85,7 @@ class App.TicketZoomArticleNew extends App.Controller
icon: 'twitter' icon: 'twitter'
attributes: [] attributes: []
internal: false, internal: false,
features: ['body:limit'] features: ['body:limit', 'body:initials']
maxTextLength: 140 maxTextLength: 140
warningTextLength: 30 warningTextLength: 30
} }
@ -95,7 +95,7 @@ class App.TicketZoomArticleNew extends App.Controller
icon: 'twitter' icon: 'twitter'
attributes: ['to'] attributes: ['to']
internal: false, internal: false,
features: ['body:limit'] features: ['body:limit', 'body:initials']
maxTextLength: 10000 maxTextLength: 10000
warningTextLength: 500 warningTextLength: 500
} }
@ -229,7 +229,7 @@ class App.TicketZoomArticleNew extends App.Controller
) )
configure_attributes = [ configure_attributes = [
{ name: 'customer_id', display: 'Recipients', tag: 'user_autocompletion', null: false, placeholder: 'Enter Person or Organization/Company', minLengt: 2, disableCreateUser: false }, { name: 'customer_id', display: 'Recipients', tag: 'user_autocompletion', null: false, placeholder: 'Enter Person or Organization/Company', minLengt: 2, disableCreateObject: false },
] ]
controller = new App.ControllerForm( controller = new App.ControllerForm(
@ -335,12 +335,12 @@ class App.TicketZoomArticleNew extends App.Controller
if params.type is 'twitter status' if params.type is 'twitter status'
App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false) App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false)
params.content_type = 'text/plain' params.content_type = 'text/plain'
params.body = "#{App.Utils.html2text(params.body, true)}\n#{@signature.text()}" params.body = App.Utils.html2text(params.body, true)
if params.type is 'twitter direct-message' if params.type is 'twitter direct-message'
App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false) App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false)
params.content_type = 'text/plain' params.content_type = 'text/plain'
params.body = "#{App.Utils.html2text(params.body, true)}\n#{@signature.text()}" params.body = App.Utils.html2text(params.body, true)
if params.type is 'facebook feed comment' if params.type is 'facebook feed comment'
App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false) App.Utils.htmlRemoveRichtext(@$('[data-name=body]'), false)
@ -352,6 +352,16 @@ class App.TicketZoomArticleNew extends App.Controller
params.content_type = 'text/plain' params.content_type = 'text/plain'
params.body = App.Utils.html2text(params.body, true) params.body = App.Utils.html2text(params.body, true)
# add initals?
for articleType in @articleTypes
if articleType.name is @type
if _.contains(articleType.features, 'body:initials')
if params.content_type is 'text/html'
params.body = "#{params.body}</br>#{@signature.text()}"
else
params.body = "#{params.body}\n#{@signature.text()}"
break
params params
validate: => validate: =>
@ -411,11 +421,11 @@ class App.TicketZoomArticleNew extends App.Controller
return false return false
if params.type is 'twitter status' if params.type is 'twitter status'
textLength = @maxTextLength - params.body.length textLength = @maxTextLength - App.Utils.textLengthWithUrl(params.body)
return false if textLength < 0 return false if textLength < 0
if params.type is 'twitter direct-message' if params.type is 'twitter direct-message'
textLength = @maxTextLength - params.body.length textLength = @maxTextLength - App.Utils.textLengthWithUrl(params.body)
return false if textLength < 0 return false if textLength < 0
true true
@ -534,11 +544,12 @@ class App.TicketZoomArticleNew extends App.Controller
for name in articleType.features for name in articleType.features
if name is 'attachment' if name is 'attachment'
@$('.article-attachment, .attachments').removeClass('hide') @$('.article-attachment, .attachments').removeClass('hide')
if name is 'body:initials'
@updateInitials()
if name is 'body:limit' if name is 'body:limit'
@maxTextLength = articleType.maxTextLength @maxTextLength = articleType.maxTextLength
@warningTextLength = articleType.warningTextLength @warningTextLength = articleType.warningTextLength
@delay(@updateLetterCount, 600) @delay(@updateLetterCount, 600)
@updateInitials()
@$('.js-textSizeLimit').removeClass('hide') @$('.js-textSizeLimit').removeClass('hide')
@scrollToBottom() if wasScrolledToBottom @scrollToBottom() if wasScrolledToBottom
@ -557,7 +568,8 @@ class App.TicketZoomArticleNew extends App.Controller
return if !@maxTextLength return if !@maxTextLength
return if !@warningTextLength return if !@warningTextLength
params = @params() params = @params()
textLength = @maxTextLength - params.body.length textLength = App.Utils.textLengthWithUrl(params.body)
textLength = @maxTextLength - textLength
className = switch className = switch
when textLength < 0 then 'label-danger' when textLength < 0 then 'label-danger'
when textLength < @warningTextLength then 'label-warning' when textLength < @warningTextLength then 'label-warning'

View file

@ -1,6 +1,6 @@
# coffeelint: disable=no_unnecessary_double_quotes # coffeelint: disable=no_unnecessary_double_quotes
class App.Utils class App.Utils
@mapAttributes: @mapTagAttributes:
'TABLE': ['align', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'frame', 'rules', 'sortable', 'summary', 'width', 'style'] 'TABLE': ['align', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'frame', 'rules', 'sortable', 'summary', 'width', 'style']
'TD': ['abbr', 'align', 'axis', 'colspan', 'headers', 'rowspan', 'valign', 'width', 'style'] 'TD': ['abbr', 'align', 'axis', 'colspan', 'headers', 'rowspan', 'valign', 'width', 'style']
'TH': ['abbr', 'align', 'axis', 'colspan', 'headers', 'rowspan', 'scope', 'sorted', 'valign', 'width', 'style'] 'TH': ['abbr', 'align', 'axis', 'colspan', 'headers', 'rowspan', 'scope', 'sorted', 'valign', 'width', 'style']
@ -232,12 +232,12 @@ class App.Utils
@_removeWordMarkup(html) @_removeWordMarkup(html)
# remove tags, keep content # remove tags, keep content
html.find('div, span, p, li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, td, h1, h2, h3, h4, h5, h6').replaceWith( -> html.find('div, span, p, li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, th, td, h1, h2, h3, h4, h5, h6').replaceWith( ->
$(@).contents() $(@).contents()
) )
# remove tags & content # remove tags & content
html.find('div, span, p, li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, table, thead, tbody, tr, td, h1, h2, h3, h4, h5, h6, br, hr, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head').remove() html.find('div, span, p, li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, table, thead, tbody, tr, th, td, h1, h2, h3, h4, h5, h6, br, hr, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head').remove()
html html
@ -249,20 +249,19 @@ class App.Utils
# remove comments # remove comments
@_removeComments(html) @_removeComments(html)
# remove style and class
if parent
@_removeAttributes(html)
# remove work markup # remove work markup
@_removeWordMarkup(html) @_removeWordMarkup(html)
# remove tags, keep content # remove tags, keep content
html.find('li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, td, h1, h2, h3, h4, h5, h6').replaceWith( -> html.find('li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, th, td, h1, h2, h3, h4, h5, h6').replaceWith( ->
$(@).contents() $(@).contents()
) )
# remove tags & content # remove tags & content
html.find('li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, td, h1, h2, h3, h4, h5, h6, hr, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head').remove() html.find('li, ul, ol, a, b, u, i, label, small, strong, strike, pre, code, center, blockquote, form, fieldset, textarea, font, address, table, thead, tbody, tr, th, td, h1, h2, h3, h4, h5, h6, hr, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head').remove()
# remove style and class
@_removeAttributes(html, parent)
html html
@ -274,9 +273,6 @@ class App.Utils
# remove comments # remove comments
@_removeComments(html) @_removeComments(html)
# remove style and class
@_removeAttributes(html)
# remove work markup # remove work markup
@_removeWordMarkup(html) @_removeWordMarkup(html)
@ -307,6 +303,9 @@ class App.Utils
# remove tags & content # remove tags & content
html.find('font, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head, fieldset').remove() html.find('font, img, svg, input, select, button, style, applet, embed, noframes, canvas, script, frame, iframe, meta, link, title, head, fieldset').remove()
# remove style and class
@_cleanAttributes(html)
html html
@_checkTypeOf: (item) -> @_checkTypeOf: (item) ->
@ -327,40 +326,59 @@ class App.Utils
catch err catch err
return $("<div>#{item}</div>") return $("<div>#{item}</div>")
@_removeAttribute: (element) -> @_cleanAttribute: (element) ->
return if !element return if !element
if @mapAttributes[element.nodeName] if @mapTagAttributes[element.nodeName]
atts = element.attributes atts = element.attributes
for att in atts for att in atts
if att && att.name && !_.contains(@mapAttributes[element.nodeName], att.name) if att && att.name && !_.contains(@mapTagAttributes[element.nodeName], att.name)
element.removeAttributeNode(att) element.removeAttribute(att.name)
else else
$element = $(element) @_removeAttribute(element)
$element.removeAttr('style')
$element.removeAttr('class')
$element.removeAttr('title')
$element.removeAttr('lang')
$element.removeAttr('type')
$element.removeAttr('id')
$element.removeAttr('wrap')
$element.removeAttrs(/data-/)
if @mapCss[element.nodeName] if @mapCss[element.nodeName]
style = element.getAttributeNode('style') elementStyle = element.style
if style && style.nodeValue && style.nodeValue.split styleOld = ''
for prop in elementStyle
styleOld += "#{prop}:#{elementStyle[prop]};"
if styleOld && styleOld.split
styleNew = '' styleNew = ''
for local_pear in style.nodeValue.split(';') for local_pear in styleOld.split(';')
prop = local_pear.split(':') prop = local_pear.split(':')
if prop[0] && prop[0].trim if prop[0] && prop[0].trim
key = prop[0].trim() key = prop[0].trim()
if _.contains(@mapCss[element.nodeName], key) if _.contains(@mapCss[element.nodeName], key)
styleNew += "#{local_pear};" styleNew += "#{local_pear};"
if styleNew isnt '' if styleNew isnt ''
style.nodeValue = styleNew element.setAttribute('style', styleNew)
element.setAttributeNode(style)
else else
element.removeAttributeNode(style) element.removeAttribute('style')
@_cleanAttributes: (html, parent = true) ->
if parent
html.each((index, element) => @_cleanAttribute(element) )
html.find('*').each((index, element) => @_cleanAttribute(element) )
html
@_removeAttribute: (element) ->
return if !element
$element = $(element)
for att in element.attributes
if att && att.name
element.removeAttribute(att.name)
#$element.removeAttr(att.name)
$element.removeAttr('style')
.removeAttr('class')
.removeAttr('lang')
.removeAttr('type')
.removeAttr('align')
.removeAttr('id')
.removeAttr('wrap')
.removeAttr('title')
.removeAttrs(/data-/)
@_removeAttributes: (html, parent = true) -> @_removeAttributes: (html, parent = true) ->
if parent if parent
@ -877,3 +895,10 @@ class App.Utils
result = newOrderMethod(a, b, applyOrder) result = newOrderMethod(a, b, applyOrder)
return false if !result return false if !result
applyOrder applyOrder
@textLengthWithUrl: (text, url_max_length = 23) ->
length = 0
return length if !text
placeholder = Array(url_max_length + 1).join('X')
text = text.replace(/http(s|):\/\/[-A-Za-z0-9+&@#\/%?=~_\|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]/img, placeholder)
text.length

View file

@ -257,10 +257,12 @@ class TweetBase
begin begin
# in case of streaming mode, get parent tweet via REST client # in case of streaming mode, get parent tweet via REST client
if !@client && @auth if @connection_type == 'stream'
@client = TweetRest.new(@auth) client = TweetRest.new(@auth)
end parent_tweet = client.status(tweet.in_reply_to_status_id)
else
parent_tweet = @client.status(tweet.in_reply_to_status_id) parent_tweet = @client.status(tweet.in_reply_to_status_id)
end
ticket = to_group(parent_tweet, group_id, channel) ticket = to_group(parent_tweet, group_id, channel)
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
# just ignore if tweet has already gone # just ignore if tweet has already gone

File diff suppressed because one or more lines are too long