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

Matrix login URL updated to accomodate newer API #970

Merged
merged 3 commits into from
Oct 15, 2023
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
135 changes: 92 additions & 43 deletions apprise/plugins/NotifyMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def _send_server_notification(self, body, title='',
attachments = None
if attach and self.attachment_support:
attachments = self._send_attachments(attach)
if not attachments:
if attachments is False:
# take an early exit
return False

Expand All @@ -611,31 +611,46 @@ def _send_server_notification(self, body, title='',
self.image_url(notify_type)

# Build our path
path = '/rooms/{}/send/m.room.message'.format(
NotifyMatrix.quote(room_id))

if image_url:
# Define our payload
image_payload = {
'msgtype': 'm.image',
'url': image_url,
'body': '{}'.format(notify_type if not title else title),
}

# Post our content
postokay, response = self._fetch(path, payload=image_payload)
if not postokay:
# Mark our failure
has_error = True
continue
if self.version == MatrixVersion.V3:
path = '/rooms/{}/send/m.room.message/0'.format(
NotifyMatrix.quote(room_id))

if attachments:
for attachment in attachments:
postokay, response = self._fetch(path, payload=attachment)
if not postokay:
# Mark our failure
has_error = True
continue
else:
path = '/rooms/{}/send/m.room.message'.format(
NotifyMatrix.quote(room_id))

if self.version == MatrixVersion.V2:
#
# Attachments don't work beyond V2 at this time
#
if image_url:
# Define our payload
image_payload = {
'msgtype': 'm.image',
'url': image_url,
'body': '{}'.format(
notify_type if not title else title),
}

# Post our content
postokay, response = self._fetch(
path, payload=image_payload)
if not postokay:
# Mark our failure
has_error = True
continue

if attachments:
for attachment in attachments:
attachment['room_id'] = room_id
attachment['type'] = 'm.room.message'

postokay, response = self._fetch(
path, payload=attachment)
if not postokay:
# Mark our failure
has_error = True
continue

# Define our payload
payload = {
Expand Down Expand Up @@ -667,7 +682,9 @@ def _send_server_notification(self, body, title='',
})

# Post our content
postokay, response = self._fetch(path, payload=payload)
method = 'PUT' if self.version == MatrixVersion.V3 else 'POST'
postokay, response = self._fetch(
path, payload=payload, method=method)
if not postokay:
# Notify our user
self.logger.warning(
Expand All @@ -685,6 +702,11 @@ def _send_attachments(self, attach):
"""

payloads = []
if self.version != MatrixVersion.V2:
self.logger.warning(
'Add ?v=2 to Apprise URL to support Attachments')
return next((False for a in attach if not a), [])

for attachment in attach:
if not attachment:
# invalid attachment (bad file)
Expand All @@ -705,15 +727,28 @@ def _send_attachments(self, attach):
# "content_uri": "mxc://example.com/a-unique-key"
# }

# Prepare our payload
payloads.append({
"info": {
"mimetype": attachment.mimetype,
},
"msgtype": "m.image",
"body": "tta.webp",
"url": response.get('content_uri'),
})
if self.version == MatrixVersion.V3:
# Prepare our payload
payloads.append({
"body": attachment.name,
"info": {
"mimetype": attachment.mimetype,
"size": len(attachment),
},
"msgtype": "m.image",
"url": response.get('content_uri'),
})

else:
# Prepare our payload
payloads.append({
"info": {
"mimetype": attachment.mimetype,
},
"msgtype": "m.image",
"body": "tta.webp",
"url": response.get('content_uri'),
})

return payloads

Expand Down Expand Up @@ -780,12 +815,23 @@ def _login(self):
'user/pass combo is missing.')
return False

# Prepare our Registration Payload
payload = {
'type': 'm.login.password',
'user': self.user,
'password': self.password,
}
# Prepare our Authentication Payload
if self.version == MatrixVersion.V3:
payload = {
'type': 'm.login.password',
'identifier': {
'type': 'm.id.user',
'user': self.user,
},
'password': self.password,
}

else:
payload = {
'type': 'm.login.password',
'user': self.user,
'password': self.password,
}

# Build our URL
postokay, response = self._fetch('/login', payload=payload)
Expand Down Expand Up @@ -1109,7 +1155,8 @@ def _fetch(self, path, payload=None, params=None, attachment=None,
response = {}

# fetch function
fn = requests.post if method == 'POST' else requests.get
fn = requests.post if method == 'POST' else (
requests.put if method == 'PUT' else requests.get)

# Define how many attempts we'll make if we get caught in a throttle
# event
Expand Down Expand Up @@ -1137,7 +1184,9 @@ def _fetch(self, path, payload=None, params=None, attachment=None,
timeout=self.request_timeout,
)

self.logger.debug('Matrix Response: %s' % str(r.content))
self.logger.debug(
'Matrix Response: code=%d, %s' % (
r.status_code, str(r.content)))
response = loads(r.content)

if r.status_code == 429:
Expand Down
Loading