title handling and bulletproofing

This commit is contained in:
Chris Caron 2019-03-10 12:10:01 -04:00
parent 643dc3f561
commit 268d11f181
6 changed files with 43 additions and 6 deletions

View File

@ -74,6 +74,13 @@ class NotifyBase(URLBase):
# Default Overflow Mode
overflow_mode = OverflowMode.UPSTREAM
# Default Title HTML Tagging
# When a title is specified for a notification service that doesn't accept
# titles, by default apprise tries to give a plesant view and convert the
# title so that it can be placed into the body. The default is to just
# use a <b> tag. The below causes the <b>title</b> to get generated:
default_html_tag_id = 'b'
def __init__(self, **kwargs):
"""
Initialize some general configuration that will keep things consistent
@ -224,9 +231,23 @@ class NotifyBase(URLBase):
# default
overflow = self.overflow_mode
if self.title_maxlen <= 0:
# Content is appended to body
body = '{}\r\n{}'.format(title, body)
if self.title_maxlen <= 0 and len(title) > 0:
if self.notify_format == NotifyFormat.MARKDOWN:
# Content is appended to body as markdown
body = '**{}**\r\n{}'.format(title, body)
elif self.notify_format == NotifyFormat.HTML:
# Content is appended to body as html
body = '<{open_tag}>{title}</{close_tag}>' \
'<br />\r\n{body}'.format(
open_tag=self.default_html_tag_id,
title=self.escape_html(title),
close_tag=self.default_html_tag_id,
body=body)
else:
# Content is appended to body as text
body = '{}\r\n{}'.format(title, body)
title = ''
# Enforce the line count first always

View File

@ -96,7 +96,7 @@ class NotifyGnome(NotifyBase):
# content to display
body_max_line_count = 10
# A title can not be used for SMS Messages. Setting this to zero will
# A title can not be used for Gnome Messages. Setting this to zero will
# cause any title (if defined) to get placed into the message body.
title_maxlen = 0

View File

@ -64,6 +64,10 @@ class NotifyPushed(NotifyBase):
# Pushed uses the http protocol with JSON requests
notify_url = 'https://api.pushed.co/1/push'
# A title can not be used for Pushed Messages. Setting this to zero will
# cause any title (if defined) to get placed into the message body.
title_maxlen = 0
# The maximum allowable characters allowed in the body per message
body_maxlen = 140

View File

@ -167,7 +167,7 @@ class NotifyRyver(NotifyBase):
# prepare JSON Object
payload = {
"body": body if not title else '**%s**\r\n%s' % (title, body),
"body": body if not title else '**{}**\r\n{}'.format(title, body),
'createSource': {
"displayName": self.user,
"avatar": self.image_url(notify_type),

View File

@ -367,8 +367,10 @@ class NotifyTelegram(NotifyBase):
# Tabs become 3 spaces
title = re.sub('&emsp;?', ' ', title, re.I)
# HTML
title = NotifyBase.escape_html(title, whitespace=False)
# HTML
title = NotifyBase.escape_html(title, whitespace=False)
body = NotifyBase.escape_html(body, whitespace=False)
# Assign the body

View File

@ -3128,6 +3128,16 @@ def test_notify_overflow_truncate():
# Verify that we break the title to a max length of our title_max
# and that the body remains untouched
obj.notify_format = NotifyFormat.HTML
chunks = obj._apply_overflow(body=body, title=title)
assert len(chunks) == 1
obj.notify_format = NotifyFormat.MARKDOWN
chunks = obj._apply_overflow(body=body, title=title)
assert len(chunks) == 1
obj.notify_format = NotifyFormat.TEXT
chunks = obj._apply_overflow(body=body, title=title)
assert len(chunks) == 1