Skip to content

Commit

Permalink
Nextcloud and Nextcloud Talk URL prefix support
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Oct 10, 2023
1 parent 8e54bba commit 8ae4418
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
25 changes: 22 additions & 3 deletions apprise/plugins/NotifyNextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class NotifyNextcloud(NotifyBase):
'min': 1,
'default': 21,
},
'url_prefix': {
'name': _('URL Prefix'),
'type': 'string',
},
'to': {
'alias_of': 'targets',
},
Expand All @@ -131,7 +135,8 @@ class NotifyNextcloud(NotifyBase):
},
}

def __init__(self, targets=None, version=None, headers=None, **kwargs):
def __init__(self, targets=None, version=None, headers=None,
url_prefix=None, **kwargs):
"""
Initialize Nextcloud Object
"""
Expand All @@ -157,6 +162,10 @@ def __init__(self, targets=None, version=None, headers=None, **kwargs):
self.logger.warning(msg)
raise TypeError(msg)

# Support URL Prefixes
self.url_prefix = '' if not url_prefix \
else '{}'.join(url_prefix.strip('/'))

self.headers = {}
if headers:
# Store our extra headers
Expand Down Expand Up @@ -200,18 +209,19 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
auth = (self.user, self.password)

# Nextcloud URL based on version used
notify_url = '{schema}://{host}/ocs/v2.php/'\
notify_url = '{schema}://{host}/{url_prefix}/ocs/v2.php/'\
'apps/admin_notifications/' \
'api/v1/notifications/{target}' \
if self.version < 21 else \
'{schema}://{host}/ocs/v2.php/'\
'{schema}://{host}/{url_prefix}/ocs/v2.php/'\
'apps/notifications/'\
'api/v2/admin_notifications/{target}'

notify_url = notify_url.format(
schema='https' if self.secure else 'http',
host=self.host if not isinstance(self.port, int)
else '{}:{}'.format(self.host, self.port),
url_prefix=self.url_prefix,
target=target,
)

Expand Down Expand Up @@ -281,6 +291,9 @@ def url(self, privacy=False, *args, **kwargs):
# Set our version
params['version'] = str(self.version)

if self.url_prefix:
params['url_prefix'] = self.url_prefix

# Extend our parameters
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))

Expand Down Expand Up @@ -347,6 +360,12 @@ def parse_url(url):
results['version'] = \
NotifyNextcloud.unquote(results['qsd']['version'])

# Support URL Prefixes
if 'url_prefix' in results['qsd'] \
and len(results['qsd']['url_prefix']):
results['url_prefix'] = \
NotifyNextcloud.unquote(results['qsd']['url_prefix'])

# Add our headers that the user can potentially over-ride if they wish
# to to our returned result set and tidy entries by unquoting them
results['headers'] = {
Expand Down
34 changes: 31 additions & 3 deletions apprise/plugins/NotifyNextcloudTalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class NotifyNextcloudTalk(NotifyBase):
},
})

# Define our template arguments
template_args = dict(NotifyBase.template_args, **{
'url_prefix': {
'name': _('URL Prefix'),
'type': 'string',
},
})

# Define any kwargs we're using
template_kwargs = {
'headers': {
Expand All @@ -116,7 +124,7 @@ class NotifyNextcloudTalk(NotifyBase):
},
}

def __init__(self, targets=None, headers=None, **kwargs):
def __init__(self, targets=None, headers=None, url_prefix=None, **kwargs):
"""
Initialize Nextcloud Talk Object
"""
Expand All @@ -133,6 +141,10 @@ def __init__(self, targets=None, headers=None, **kwargs):
self.logger.warning(msg)
raise TypeError(msg)

# Support URL Prefixes
self.url_prefix = '' if not url_prefix \
else '{}'.join(url_prefix.strip('/'))

self.headers = {}
if headers:
# Store our extra headers
Expand Down Expand Up @@ -176,13 +188,14 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
}

# Nextcloud Talk URL
notify_url = '{schema}://{host}'\
notify_url = '{schema}://{host}/{url_prefix}'\
'/ocs/v2.php/apps/spreed/api/v1/chat/{target}'

notify_url = notify_url.format(
schema='https' if self.secure else 'http',
host=self.host if not isinstance(self.port, int)
else '{}:{}'.format(self.host, self.port),
url_prefix=self.url_prefix,
target=target,
)

Expand Down Expand Up @@ -245,6 +258,14 @@ def url(self, privacy=False, *args, **kwargs):
Returns the URL built dynamically based on specified arguments.
"""

# Our default set of parameters
params = self.url_parameters(privacy=privacy, *args, **kwargs)

# Append our headers into our parameters
params.update({'+{}'.format(k): v for k, v in self.headers.items()})
if self.url_prefix:
params['url_prefix'] = self.url_prefix

# Determine Authentication
auth = '{user}:{password}@'.format(
user=NotifyNextcloudTalk.quote(self.user, safe=''),
Expand All @@ -254,7 +275,7 @@ def url(self, privacy=False, *args, **kwargs):

default_port = 443 if self.secure else 80

return '{schema}://{auth}{hostname}{port}/{targets}' \
return '{schema}://{auth}{hostname}{port}/{targets}?{params}' \
.format(
schema=self.secure_protocol
if self.secure else self.protocol,
Expand All @@ -266,6 +287,7 @@ def url(self, privacy=False, *args, **kwargs):
else ':{}'.format(self.port),
targets='/'.join([NotifyNextcloudTalk.quote(x)
for x in self.targets]),
params=NotifyNextcloudTalk.urlencode(params),
)

def __len__(self):
Expand All @@ -291,6 +313,12 @@ def parse_url(url):
results['targets'] = \
NotifyNextcloudTalk.split_path(results['fullpath'])

# Support URL Prefixes
if 'url_prefix' in results['qsd'] \
and len(results['qsd']['url_prefix']):
results['url_prefix'] = \
NotifyNextcloudTalk.unquote(results['qsd']['url_prefix'])

# Add our headers that the user can potentially over-ride if they wish
# to to our returned result set and tidy entries by unquoting them
results['headers'] = {
Expand Down
6 changes: 6 additions & 0 deletions test/test_plugin_nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
('ncloud://user@localhost?to=user1,user2&version=21', {
'instance': NotifyNextcloud,
}),
('ncloud://user@localhost?to=user1&version=20&url_prefix=/abcd', {
'instance': NotifyNextcloud,
}),
('ncloud://user@localhost?to=user1&version=21&url_prefix=/abcd', {
'instance': NotifyNextcloud,
}),
('ncloud://user:pass@localhost/user1/user2', {
'instance': NotifyNextcloud,

Expand Down
4 changes: 4 additions & 0 deletions test/test_plugin_nextcloudtalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
'instance': NotifyNextcloudTalk,
'requests_response_code': requests.codes.created,
}),
('nctalk://user:pass@localhost:8080/roomid?url_prefix=/prefix', {
'instance': NotifyNextcloudTalk,
'requests_response_code': requests.codes.created,
}),
('nctalks://user:pass@localhost/roomid', {
'instance': NotifyNextcloudTalk,
'requests_response_code': requests.codes.created,
Expand Down

0 comments on commit 8ae4418

Please sign in to comment.