fix attachement with matrix v3

pull/1373/head
privacyfr3ak 2025-07-27 14:35:31 -04:00
parent 91faed0c6d
commit b673837f4e
No known key found for this signature in database
GPG Key ID: A8D8FC83B217A56A
1 changed files with 64 additions and 67 deletions

View File

@ -651,6 +651,9 @@ class NotifyMatrix(NotifyBase):
# Get our room # Get our room
room = rooms.pop(0) room = rooms.pop(0)
# Set method according to MatrixVersion
method = 'PUT' if self.version == MatrixVersion.V3 else 'POST'
# Get our room_id from our response # Get our room_id from our response
room_id = self._room_join(room) room_id = self._room_join(room)
if not room_id: if not room_id:
@ -677,39 +680,49 @@ class NotifyMatrix(NotifyBase):
path = '/rooms/{}/send/m.room.message'.format( path = '/rooms/{}/send/m.room.message'.format(
NotifyMatrix.quote(room_id)) NotifyMatrix.quote(room_id))
if self.version == MatrixVersion.V2: if image_url:
# # Define our payload
# Attachments don't work beyond V2 at this time image_payload = {
# 'msgtype': 'm.image',
if image_url: 'url': image_url,
# Define our payload 'body': '{}'.format(
image_payload = { notify_type if not title else title),
'msgtype': 'm.image', }
'url': image_url,
'body': '{}'.format( # Post our content
notify_type if not title else title), 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'
# Post our content
postokay, response = self._fetch( postokay, response = self._fetch(
path, payload=image_payload) path, payload=attachment, method=method)
# Increment the transaction ID to avoid future messages
# being recognized as retransmissions and ignored
if self.version == MatrixVersion.V3 \
and self.access_token != self.password:
self.transaction_id += 1
self.store.set(
'transaction_id', self.transaction_id,
expires=self.default_cache_expiry_sec)
path = '/rooms/{}/send/m.room.message/{}'.format(
NotifyMatrix.quote(room_id),
self.transaction_id,
)
if not postokay: if not postokay:
# Mark our failure # Mark our failure
has_error = True has_error = True
continue 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 # Define our payload
payload = { payload = {
'msgtype': 'm.{}'.format(self.msgtype), 'msgtype': 'm.{}'.format(self.msgtype),
@ -740,7 +753,6 @@ class NotifyMatrix(NotifyBase):
}) })
# Post our content # Post our content
method = 'PUT' if self.version == MatrixVersion.V3 else 'POST'
postokay, response = self._fetch( postokay, response = self._fetch(
path, payload=payload, method=method) path, payload=payload, method=method)
@ -770,10 +782,6 @@ class NotifyMatrix(NotifyBase):
""" """
payloads = [] 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: for attachment in attach:
if not attachment: if not attachment:
@ -795,38 +803,28 @@ class NotifyMatrix(NotifyBase):
# "content_uri": "mxc://example.com/a-unique-key" # "content_uri": "mxc://example.com/a-unique-key"
# } # }
# FUTURE if self.version == MatrixVersion.V3: if self.version == MatrixVersion.V3:
# FUTURE # Prepare our payload # Prepare our payload
# FUTURE payloads.append({ payloads.append({
# FUTURE "body": attachment.name, "body": attachment.name,
# FUTURE "info": { "info": {
# FUTURE "mimetype": attachment.mimetype, "mimetype": attachment.mimetype,
# FUTURE "size": len(attachment), "size": len(attachment),
# FUTURE }, },
# FUTURE "msgtype": "m.image", "msgtype": "m.image",
# FUTURE "url": response.get('content_uri'), "url": response.get('content_uri'),
# FUTURE }) })
# FUTURE else: else:
# FUTURE # Prepare our payload # Prepare our payload
# FUTURE payloads.append({ payloads.append({
# FUTURE "info": { "info": {
# FUTURE "mimetype": attachment.mimetype, "mimetype": attachment.mimetype,
# FUTURE }, },
# FUTURE "msgtype": "m.image", "msgtype": "m.image",
# FUTURE "body": "tta.webp", "body": "tta.webp",
# FUTURE "url": response.get('content_uri'), "url": response.get('content_uri'),
# FUTURE }) })
# Prepare our payload
payloads.append({
"info": {
"mimetype": attachment.mimetype,
},
"msgtype": "m.image",
"body": "tta.webp",
"url": response.get('content_uri'),
})
return payloads return payloads
@ -1251,12 +1249,11 @@ class NotifyMatrix(NotifyBase):
status_code = requests.codes.internal_server_error status_code = requests.codes.internal_server_error
if path == '/upload': if path == '/upload':
# FUTURE if self.version == MatrixVersion.V3: if self.version == MatrixVersion.V3:
# FUTURE url += MATRIX_V3_MEDIA_PATH + path url += MATRIX_V3_MEDIA_PATH + path
# FUTURE else: else:
# FUTURE url += MATRIX_V2_MEDIA_PATH + path url += MATRIX_V2_MEDIA_PATH + path
url += MATRIX_V2_MEDIA_PATH + path
params.update({'filename': attachment.name}) params.update({'filename': attachment.name})
with open(attachment.path, 'rb') as fp: with open(attachment.path, 'rb') as fp: