comparison sat/core/xmpp.py @ 3175:b9a2dd4d750a

core (xmpp): add `name` and `media_type` in attachements if they are missing.
author Goffi <goffi@goffi.org>
date Tue, 18 Feb 2020 18:17:18 +0100
parents 343b8076e967
children 84a94b385760
comparison
equal deleted inserted replaced
3174:c90f27ce52b0 3175:b9a2dd4d750a
19 import sys 19 import sys
20 import time 20 import time
21 import calendar 21 import calendar
22 import uuid 22 import uuid
23 import mimetypes 23 import mimetypes
24 from urllib.parse import urlparse, unquote
24 from functools import partial 25 from functools import partial
25 import shortuuid 26 import shortuuid
26 from sat.core.i18n import _ 27 from sat.core.i18n import _
27 from sat.core.constants import Const as C 28 from sat.core.constants import Const as C
28 from sat.memory import cache 29 from sat.memory import cache
1100 @param post_treat(defer.Deferred): post parsing treatments 1101 @param post_treat(defer.Deferred): post parsing treatments
1101 """ 1102 """
1102 if not cont: 1103 if not cont:
1103 return 1104 return
1104 data = self.parseMessage(message_elt) 1105 data = self.parseMessage(message_elt)
1106 post_treat.addCallback(self.completeAttachments)
1105 post_treat.addCallback(self.skipEmptyMessage) 1107 post_treat.addCallback(self.skipEmptyMessage)
1106 post_treat.addCallback(self.addToHistory) 1108 post_treat.addCallback(self.addToHistory)
1107 post_treat.addCallback(self.bridgeSignal, data) 1109 post_treat.addCallback(self.bridgeSignal, data)
1108 post_treat.addErrback(self.cancelErrorTrap) 1110 post_treat.addErrback(self.cancelErrorTrap)
1109 post_treat.callback(data) 1111 post_treat.callback(data)
1122 d = self.host.trigger.asyncPoint( 1124 d = self.host.trigger.asyncPoint(
1123 "messageReceived", client, message_elt, post_treat 1125 "messageReceived", client, message_elt, post_treat
1124 ) 1126 )
1125 1127
1126 d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat) 1128 d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat)
1129
1130 def completeAttachments(self, data):
1131 """Complete missing metadata of attachments"""
1132 for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []):
1133 if "name" not in attachment:
1134 name = unquote(urlparse(attachment['url']).path.rsplit('/', 1)[-1])
1135 attachment["name"] = name
1136 if C.MESS_KEY_MEDIA_TYPE not in attachment:
1137 media_type = mimetypes.guess_type(attachment['url'], strict=False)[0]
1138 if media_type:
1139 attachment[C.MESS_KEY_MEDIA_TYPE] = media_type
1140 return data
1127 1141
1128 def skipEmptyMessage(self, data): 1142 def skipEmptyMessage(self, data):
1129 if not data["message"] and not data["extra"] and not data["subject"]: 1143 if not data["message"] and not data["extra"] and not data["subject"]:
1130 raise failure.Failure(exceptions.CancelError("Cancelled empty message")) 1144 raise failure.Failure(exceptions.CancelError("Cancelled empty message"))
1131 return data 1145 return data