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
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
room_id = self._room_join(room)
if not room_id:
@ -677,39 +680,49 @@ class NotifyMatrix(NotifyBase):
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),
}
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'
# Post our content
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:
# 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 = {
'msgtype': 'm.{}'.format(self.msgtype),
@ -740,7 +753,6 @@ class NotifyMatrix(NotifyBase):
})
# Post our content
method = 'PUT' if self.version == MatrixVersion.V3 else 'POST'
postokay, response = self._fetch(
path, payload=payload, method=method)
@ -770,10 +782,6 @@ class NotifyMatrix(NotifyBase):
"""
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:
@ -795,38 +803,28 @@ class NotifyMatrix(NotifyBase):
# "content_uri": "mxc://example.com/a-unique-key"
# }
# FUTURE if self.version == MatrixVersion.V3:
# FUTURE # Prepare our payload
# FUTURE payloads.append({
# FUTURE "body": attachment.name,
# FUTURE "info": {
# FUTURE "mimetype": attachment.mimetype,
# FUTURE "size": len(attachment),
# FUTURE },
# FUTURE "msgtype": "m.image",
# FUTURE "url": response.get('content_uri'),
# FUTURE })
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'),
})
# FUTURE else:
# FUTURE # Prepare our payload
# FUTURE payloads.append({
# FUTURE "info": {
# FUTURE "mimetype": attachment.mimetype,
# FUTURE },
# FUTURE "msgtype": "m.image",
# FUTURE "body": "tta.webp",
# FUTURE "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'),
})
else:
# Prepare our payload
payloads.append({
"info": {
"mimetype": attachment.mimetype,
},
"msgtype": "m.image",
"body": "tta.webp",
"url": response.get('content_uri'),
})
return payloads
@ -1251,12 +1249,11 @@ class NotifyMatrix(NotifyBase):
status_code = requests.codes.internal_server_error
if path == '/upload':
# FUTURE if self.version == MatrixVersion.V3:
# FUTURE url += MATRIX_V3_MEDIA_PATH + path
if self.version == MatrixVersion.V3:
url += MATRIX_V3_MEDIA_PATH + path
# FUTURE else:
# FUTURE url += MATRIX_V2_MEDIA_PATH + path
url += MATRIX_V2_MEDIA_PATH + path
else:
url += MATRIX_V2_MEDIA_PATH + path
params.update({'filename': attachment.name})
with open(attachment.path, 'rb') as fp: