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

90 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-12-01 19:36 +0000

1import logging 

2from collections.abc import Iterable 

3from datetime import datetime 

4 

5from slixmpp import Message 

6 

7from ...util.types import ( 

8 LinkPreview, 

9 Mention, 

10 MessageReference, 

11 ProcessingHint, 

12) 

13from ...util.util import ( 

14 add_quote_prefix, 

15 remove_emoji_variation_selector_16, 

16 uniquify_replace, 

17) 

18from .message_maker import MessageMaker 

19 

20 

21class TextMessageMixin(MessageMaker): 

22 def __default_hints( 

23 self, hints: Iterable[ProcessingHint] | None = None 

24 ) -> Iterable[ProcessingHint]: 

25 if hints is not None: 

26 return hints 

27 elif self._mtype == "chat": 

28 return {"markable", "store"} 

29 elif self._mtype == "groupchat": 

30 return {"markable"} 

31 else: 

32 raise RuntimeError("Never") 

33 

34 def _replace_id(self, legacy_msg_id: str) -> str: 

35 if ( 

36 self._mtype == "groupchat" 

37 and (recipient_pk := self._recipient_pk()) is not None 

38 ): 

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

40 ids = self.xmpp.store.id_map.get_origin( 

41 orm, recipient_pk, str(legacy_msg_id) 

42 ) 

43 if ids: 

44 if len(ids) > 1: 

45 log.warning( 

46 "More than 1 origin msg ID for '%s': '%s'", 

47 legacy_msg_id, 

48 ids, 

49 ) 

50 return ids[0] 

51 return legacy_msg_id 

52 else: 

53 return self._legacy_to_xmpp(legacy_msg_id)[0] 

54 

55 def send_text( 

56 self, 

57 body: str, 

58 legacy_msg_id: str | None = None, 

59 *, 

60 when: datetime | None = None, 

61 reply_to: MessageReference | None = None, 

62 thread: str | None = None, 

63 hints: Iterable[ProcessingHint] | None = None, 

64 carbon: bool = False, 

65 correction: bool = False, 

66 correction_event_id: str | None = None, 

67 link_previews: list[LinkPreview] | None = None, 

68 mentions: Iterable[Mention] = (), 

69 **send_kwargs: object, 

70 ) -> Message | None: 

71 """ 

72 Send a text message from this :term:`XMPP Entity`. 

73 

74 :param body: Content of the message 

75 :param legacy_msg_id: If you want to be able to transport read markers from the gateway 

76 user to the legacy network, specify this 

77 :param when: when the message was sent, for a "delay" tag (:xep:`0203`) 

78 :param reply_to: Quote another message (:xep:`0461`) 

79 :param hints: 

80 :param thread: 

81 :param carbon: (only used if called on a :class:`LegacyContact`) 

82 Set this to ``True`` if this is actually a message sent **to** the 

83 :class:`LegacyContact` by the :term:`User`. 

84 Use this to synchronize outgoing history for legacy official apps. 

85 :param correction: whether this message is a correction or not 

86 :param correction_event_id: in the case where an ID is associated with the legacy 

87 'correction event', specify it here to use it on the XMPP side. If not specified, 

88 a random ID will be used. 

89 :param link_previews: A little of sender (or server, or gateway)-generated 

90 previews of URLs linked in the body. 

91 """ 

92 if ( 

93 carbon 

94 and not self.is_participant 

95 and (recipient_pk := self._recipient_pk()) is not None 

96 ): 

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

98 if not correction and self.xmpp.store.id_map.was_sent_by_user( 

99 orm, recipient_pk, str(legacy_msg_id) 

100 ): 

101 log.warning( 

102 "Carbon message for a message an XMPP has sent? This is a bug! %s", 

103 legacy_msg_id, 

104 ) 

105 return None 

106 hints = self.__default_hints(hints) 

107 

108 if correction_event_id == legacy_msg_id: 

109 correction_event_id = None 

110 

111 if correction: 

112 assert legacy_msg_id 

113 replace_id = legacy_msg_id 

114 event_id: str | None = correction_event_id or uniquify_replace( 

115 legacy_msg_id 

116 ) 

117 else: 

118 if correction_event_id: 

119 raise TypeError( 

120 "'correction_event_id' was passed but 'correction=False'" 

121 ) 

122 event_id = legacy_msg_id 

123 

124 msg = self._make_message( 

125 mbody=body, 

126 legacy_msg_id=event_id, 

127 when=when, 

128 reply_to=reply_to, 

129 hints=hints or (), 

130 carbon=carbon, 

131 thread=thread, 

132 link_previews=link_previews, 

133 mentions=mentions, 

134 ) 

135 if correction: 

136 msg["replace"]["id"] = self._replace_id(replace_id) 

137 

138 if event_id is not None: 

139 self._store_sent_msg(event_id, when) 

140 

141 return self._send( 

142 msg, 

143 carbon=carbon, 

144 legacy_msg_id=event_id, 

145 **send_kwargs, 

146 ) 

147 

148 def _store_sent_msg(self, legacy_id: str, when: datetime | None) -> None: 

149 pass 

150 

151 def correct( 

152 self, 

153 legacy_msg_id: str, 

154 new_text: str, 

155 *, 

156 when: datetime | None = None, 

157 reply_to: MessageReference | None = None, 

158 thread: str | None = None, 

159 hints: Iterable[ProcessingHint] | None = None, 

160 carbon: bool = False, 

161 correction_event_id: str | None = None, 

162 link_previews: list[LinkPreview] | None = None, 

163 mentions: Iterable[Mention] = (), 

164 **send_kwargs: object, 

165 ) -> None: 

166 """ 

167 Modify a message that was previously sent by this :term:`XMPP Entity`. 

168 

169 Uses last message correction (:xep:`0308`) 

170 

171 :param new_text: New content of the message 

172 :param legacy_msg_id: The legacy message ID of the message to correct 

173 :param when: when the message was sent, for a "delay" tag (:xep:`0203`) 

174 :param reply_to: Quote another message (:xep:`0461`) 

175 :param hints: 

176 :param thread: 

177 :param carbon: (only in 1:1) Reflect a message sent to this ``Contact`` by the user. 

178 Use this to synchronize outgoing history for legacy official apps. 

179 :param correction_event_id: in the case where an ID is associated with the legacy 

180 'correction event', specify it here to use it on the XMPP side. If not specified, 

181 a random ID will be used. 

182 :param link_previews: A little of sender (or server, or gateway)-generated 

183 previews of URLs linked in the body. 

184 """ 

185 self.send_text( 

186 new_text, 

187 legacy_msg_id, 

188 when=when, 

189 reply_to=reply_to, 

190 hints=hints, 

191 carbon=carbon, 

192 thread=thread, 

193 correction=True, 

194 correction_event_id=correction_event_id, 

195 link_previews=link_previews, 

196 mentions=mentions, 

197 **send_kwargs, 

198 ) 

199 

200 def react( 

201 self, 

202 legacy_msg_id: str, 

203 emojis: Iterable[str] = (), 

204 thread: str | None = None, 

205 **kwargs: object, 

206 ) -> None: 

207 """ 

208 Send a reaction (:xep:`0444`) from this :term:`XMPP Entity`. 

209 

210 :param legacy_msg_id: The message which the reaction refers to. 

211 :param emojis: An iterable of emojis used as reactions 

212 :param thread: 

213 """ 

214 xmpp_id = kwargs.pop("xmpp_id", None) 

215 emojis = {remove_emoji_variation_selector_16(e) for e in emojis} 

216 if xmpp_id: 

217 assert isinstance(xmpp_id, str) 

218 xmpp_ids = [xmpp_id] 

219 else: 

220 xmpp_ids = self._legacy_to_xmpp(legacy_msg_id) 

221 for xmpp_id in xmpp_ids: 

222 msg = self._make_message( 

223 hints={"store"}, carbon=bool(kwargs.get("carbon")), thread=thread 

224 ) 

225 self.xmpp.plugin["xep_0444"].set_reactions( 

226 msg, to_id=xmpp_id, reactions=emojis 

227 ) 

228 self.__add_reaction_fallback(msg, legacy_msg_id, emojis) 

229 self._send(msg, **kwargs) 

230 

231 def __add_reaction_fallback( 

232 self, 

233 msg: Message, 

234 legacy_msg_id: str, 

235 emojis: Iterable[str] = (), 

236 ) -> None: 

237 session = self._session 

238 if session is None or not session.preferences.get("reaction_fallback", False): 

239 return 

240 msg["fallback"]["for"] = self.xmpp.plugin["xep_0444"].namespace 

241 msg["fallback"].enable("body") 

242 msg["body"] = " ".join(emojis) 

243 if not self.is_participant: 

244 return 

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

246 body = self.xmpp.store.mam.get_body( 

247 orm, self._room._stored.id, str(legacy_msg_id) 

248 ) 

249 if body is None: 

250 return 

251 msg["body"] = add_quote_prefix(body) + "\n" + msg["body"] 

252 

253 def retract( 

254 self, 

255 legacy_msg_id: str, 

256 thread: str | None = None, 

257 **kwargs: object, 

258 ) -> None: 

259 """ 

260 Send a message retraction (:XEP:`0424`) from this :term:`XMPP Entity`. 

261 

262 :param legacy_msg_id: Legacy ID of the message to delete 

263 :param thread: 

264 """ 

265 xmpp_ids = self._legacy_to_xmpp(legacy_msg_id) 

266 replace_id = self._replace_id(legacy_msg_id) 

267 if replace_id not in xmpp_ids: 

268 xmpp_ids.append(replace_id) 

269 for xmpp_id in xmpp_ids: 

270 msg = self._make_message( 

271 state=None, 

272 hints={"store"}, 

273 mbody=f"/me retracted the message {legacy_msg_id}", 

274 carbon=bool(kwargs.get("carbon")), 

275 thread=thread, 

276 ) 

277 msg.enable("fallback") 

278 # namespace version mismatch between slidge and slixmpp, update me later 

279 msg["fallback"]["for"] = self.xmpp.plugin["xep_0424"].namespace[:-1] + "1" 

280 msg["retract"]["id"] = msg["replace"]["id"] = xmpp_id 

281 self._send(msg, **kwargs) 

282 

283 

284log = logging.getLogger(__name__)