Coverage for slidge / core / mixins / message_maker.py: 89%

137 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-20 19:56 +0000

1import base64 

2import io 

3import logging 

4import uuid 

5import warnings 

6from collections.abc import Iterable 

7from datetime import UTC, datetime 

8from pathlib import Path 

9from typing import TYPE_CHECKING, cast 

10 

11from PIL import Image 

12from slixmpp import Message 

13from slixmpp.plugins.xep_0511.stanza import LinkMetadata 

14from slixmpp.types import MessageTypes 

15 

16from slidge.util import strip_illegal_chars 

17 

18from ...db.models import GatewayUser 

19from ...util.types import ( 

20 AnyMessageReference, 

21 AnyMUC, 

22 ChatState, 

23 LegacyMessageType, 

24 LinkPreview, 

25 ProcessingHint, 

26) 

27from .. import config 

28from .base import BaseSender 

29 

30if TYPE_CHECKING: 

31 from ...group import LegacyParticipant 

32 

33 

34class MessageMaker(BaseSender): 

35 mtype: MessageTypes = NotImplemented 

36 _can_send_carbon: bool = NotImplemented 

37 STRIP_SHORT_DELAY = False 

38 USE_STANZA_ID = False 

39 

40 muc: AnyMUC 

41 

42 def _recipient_pk(self) -> int: 

43 return ( 

44 self.muc.stored.id if self.is_participant else self.stored.id # type:ignore 

45 ) 

46 

47 def _make_message( 

48 self, 

49 state: ChatState | None = None, 

50 hints: Iterable[ProcessingHint] = (), 

51 legacy_msg_id: LegacyMessageType | None = None, 

52 when: datetime | None = None, 

53 reply_to: AnyMessageReference | None = None, 

54 carbon: bool = False, 

55 link_previews: Iterable[LinkPreview] | None = None, 

56 **kwargs: object, 

57 ) -> Message: 

58 body = kwargs.pop("mbody", None) 

59 mfrom = kwargs.pop("mfrom", self.jid) 

60 mto = kwargs.pop("mto", None) 

61 thread = kwargs.pop("thread", None) 

62 # the msg needs to have jabber:client as xmlns, so 

63 # we don't want to associate with the XML stream 

64 msg_cls = Message if carbon and self._can_send_carbon else self.xmpp.Message 

65 msg = msg_cls( 

66 sfrom=mfrom, 

67 stype=kwargs.pop("mtype", None) or self.mtype, 

68 sto=mto, 

69 **kwargs, 

70 ) 

71 if body: 

72 assert isinstance(body, str) 

73 msg["body"] = strip_illegal_chars(body, "�") 

74 state = "active" 

75 if thread: 

76 with self.xmpp.store.session() as orm: 

77 thread_str = str(thread) 

78 msg["thread"] = ( 

79 self.xmpp.store.id_map.get_thread( 

80 orm, 

81 self._recipient_pk(), 

82 thread_str, 

83 self.is_participant, 

84 ) 

85 or thread_str 

86 ) 

87 if state: 

88 msg["chat_state"] = state 

89 for hint in hints: 

90 msg.enable(hint) 

91 self._set_msg_id(msg, legacy_msg_id) 

92 self._add_delay(msg, when) 

93 if link_previews: 

94 self._add_link_previews(msg, link_previews) 

95 if reply_to: 

96 self._add_reply_to(msg, reply_to) 

97 return msg 

98 

99 def _set_msg_id( 

100 self, msg: Message, legacy_msg_id: LegacyMessageType | None = None 

101 ) -> None: 

102 if legacy_msg_id is not None: 

103 i = self.session.legacy_to_xmpp_msg_id(legacy_msg_id) 

104 msg.set_id(i) 

105 if self.USE_STANZA_ID: 

106 msg["stanza_id"]["id"] = i 

107 msg["stanza_id"]["by"] = self.muc.jid 

108 elif self.USE_STANZA_ID: 

109 msg["stanza_id"]["id"] = str(uuid.uuid4()) 

110 msg["stanza_id"]["by"] = self.muc.jid 

111 

112 def _legacy_to_xmpp(self, legacy_id: LegacyMessageType) -> list[str]: 

113 with self.xmpp.store.session() as orm: 

114 ids = self.xmpp.store.id_map.get_xmpp( 

115 orm, 

116 self._recipient_pk(), 

117 str(legacy_id), 

118 self.is_participant, 

119 ) 

120 if ids: 

121 return ids 

122 return [self.session.legacy_to_xmpp_msg_id(legacy_id)] 

123 

124 def _add_delay(self, msg: Message, when: datetime | None) -> None: 

125 if when: 

126 if when.tzinfo is None: 

127 when = when.astimezone(UTC) 

128 if self.STRIP_SHORT_DELAY: 

129 delay = (datetime.now().astimezone(UTC) - when).seconds 

130 if delay < config.IGNORE_DELAY_THRESHOLD: 

131 return 

132 msg["delay"].set_stamp(when) 

133 msg["delay"].set_from(self.xmpp.boundjid.bare) 

134 

135 def _add_reply_to(self, msg: Message, reply_to: AnyMessageReference) -> None: 

136 xmpp_id = self._legacy_to_xmpp(reply_to.legacy_id)[0] 

137 msg["reply"]["id"] = xmpp_id 

138 

139 muc = getattr(self, "muc", None) 

140 

141 if entity := reply_to.author: 

142 if entity == "user" or isinstance(entity, GatewayUser): 

143 if isinstance(entity, GatewayUser): 

144 warnings.warn( 

145 "Using a GatewayUser as the author of a " 

146 "MessageReference is deprecated. Use the string 'user' " 

147 "instead.", 

148 DeprecationWarning, 

149 ) 

150 if muc: 

151 msg["reply"]["to"] = muc.user_muc_jid 

152 fallback_nick = muc.user_nick 

153 else: 

154 msg["reply"]["to"] = self.session.user_jid 

155 # TODO: here we should use preferably use the PEP nick of the user 

156 # (but it doesn't matter much) 

157 fallback_nick = self.session.user_jid.user 

158 else: 

159 if muc: 

160 if hasattr(entity, "muc"): 

161 # TODO: accept a Contact here and use muc.get_participant_by_legacy_id() 

162 # a bit of work because right now this is a sync function 

163 entity = cast("LegacyParticipant", entity) 

164 fallback_nick = entity.nickname 

165 else: 

166 warnings.warn( 

167 "The author of a message reference in a MUC must be a" 

168 " Participant instance, not a Contact" 

169 ) 

170 fallback_nick = entity.name 

171 else: 

172 fallback_nick = entity.name 

173 msg["reply"]["to"] = entity.jid 

174 else: 

175 fallback_nick = None 

176 

177 if fallback := reply_to.body: 

178 msg["reply"].add_quoted_fallback(fallback, fallback_nick) 

179 

180 def _add_link_previews( 

181 self, msg: Message, link_previews: Iterable[LinkPreview] 

182 ) -> None: 

183 for preview in link_previews: 

184 if preview.is_empty: 

185 continue 

186 element = LinkMetadata() 

187 for i, name in enumerate(preview._fields): 

188 val = preview[i] 

189 if isinstance(val, Path): 

190 val = val.read_bytes() 

191 if isinstance(val, bytes): 

192 val = self._process_link_preview_image(val) 

193 if not val: 

194 continue 

195 element[name] = val 

196 msg.append(element) 

197 

198 @staticmethod 

199 def _process_link_preview_image(data: bytes) -> str | None: 

200 # this will block the main thread. if this proves to be an issue in practice, 

201 # this could be rewritten to use the thread pool we use to resize avatars. 

202 try: 

203 image = Image.open(io.BytesIO(data)) 

204 except Exception: 

205 log.exception("Skipping link preview image") 

206 return None 

207 

208 rewrite = False 

209 if image.format != "JPEG": 

210 rewrite = True 

211 

212 if any(x > MAX_LINK_PREVIEW_IMAGE_SIZE for x in image.size): 

213 image.thumbnail((MAX_LINK_PREVIEW_IMAGE_SIZE, MAX_LINK_PREVIEW_IMAGE_SIZE)) 

214 rewrite = True 

215 

216 if rewrite: 

217 with io.BytesIO() as f: 

218 image.save(f, format="JPEG") 

219 data = f.getvalue() 

220 

221 return "data:image/jpeg;base64," + base64.b64encode(data).decode("utf-8") 

222 

223 

224# Instead of having a hardcoded value for this, we would ideally use 

225# XEP-0478: Stream Limits Advertisement to know which size is authorized. 

226# However, this isn't possible until XEP-0225: Component Connections is a thing. 

227# Prosody defaults to 512kb for s2s connection and 10Mb for c2s connections. 

228# Some quick tests about JPEG image: 

229# median size of 50 base64-encoded JPEG random RGB image 

230# 128x128 pixels: 14kb ± 0.04 

231# 256x256 pixels: 53kb ± 0.06 # sounds like a good tradeoff 

232# 384x384 pixels: 119kb ± 0.11 

233# 512x512 pixels: 211kb ± 0.13 

234# 640x640 pixels: 329kb ± 0.15 

235# 768x768 pixels: 473kb ± 0.21 

236# 896x896 pixels: 644kb ± 0.27 

237# 1024x1024 pixels: 841kb ± 0.22 

238MAX_LINK_PREVIEW_IMAGE_SIZE = 256 

239 

240log = logging.getLogger(__name__)