Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor overflow=truncate when sending attachments #1097

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions apprise/plugins/NotifyBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@ def _build_send_calls(self, body=None, title=None,
# Handle situations where the title is None
title = '' if not title else title

# Truncate flag set with attachments ensures that only 1
# attachment passes through. In the event there could be many
# services specified, we only want to do this logic once.
# The logic is only applicable if ther was more then 1 attachment
# specified
overflow = self.overflow_mode if overflow is None else overflow
if attach and len(attach) > 1 and overflow == OverflowMode.TRUNCATE:
# Save first attachment
_attach = AppriseAttachment(attach[0], asset=self.asset)
else:
# reference same attachment
_attach = attach

# Apply our overflow (if defined)
for chunk in self._apply_overflow(
body=body, title=title, overflow=overflow,
Expand All @@ -465,7 +478,7 @@ def _build_send_calls(self, body=None, title=None,
# Send notification
yield dict(
body=chunk['body'], title=chunk['title'],
notify_type=notify_type, attach=attach,
notify_type=notify_type, attach=_attach,
body_format=body_format
)

Expand All @@ -485,7 +498,7 @@ def _apply_overflow(self, body, title=None, overflow=None,
},
{
title: 'the title goes here',
body: 'the message body goes here',
body: 'the continued message body goes here',
},

]
Expand Down
57 changes: 57 additions & 0 deletions test/test_apprise_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
# POSSIBILITY OF SUCH DAMAGE.

import pytest
import json
import requests
from unittest import mock
from os.path import getsize
from os.path import join
from os.path import dirname
from inspect import cleandoc
from apprise.Apprise import Apprise
from apprise.AttachmentManager import AttachmentManager
from apprise.AppriseAttachment import AppriseAttachment
from apprise.AppriseAsset import AppriseAsset
Expand Down Expand Up @@ -261,6 +265,59 @@ def test_apprise_attachment():
assert aa.size() == 0


@mock.patch('requests.get')
def test_apprise_attachment_truncate(mock_get):
"""
API: AppriseAttachment when truncation in place

"""

# Prepare our response
response = requests.Request()
response.status_code = requests.codes.ok

# Prepare Mock
mock_get.return_value = response

# our Apprise Object
ap_obj = Apprise()

# Add ourselves an object set to truncate
ap_obj.add('json://localhost/?method=GET&overflow=truncate')

# Add ourselves a second object without truncate
ap_obj.add('json://localhost/?method=GET&overflow=upstream')

# Create ourselves an attachment object
aa = AppriseAttachment()

# There are no attachents loaded
assert len(aa) == 0

# Object can be directly checked as a boolean; response is False
# when there are no entries loaded
assert not aa

# Add 2 attachments
assert aa.add(join(TEST_VAR_DIR, 'apprise-test.gif'))
assert aa.add(join(TEST_VAR_DIR, 'apprise-test.png'))

assert mock_get.call_count == 0
assert ap_obj.notify(body='body', title='title', attach=aa)

assert mock_get.call_count == 2

# Our first item was truncated, so only 1 attachment
details = mock_get.call_args_list[0]
dataset = json.loads(details[1]['data'])
assert len(dataset['attachments']) == 1

# Our second item was not truncated, so all attachments
details = mock_get.call_args_list[1]
dataset = json.loads(details[1]['data'])
assert len(dataset['attachments']) == 2


def test_apprise_attachment_instantiate():
"""
API: AppriseAttachment.instantiate()
Expand Down
Loading