Coverage for slidge/contact/contact.py: 90%
288 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-12-01 19:36 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-12-01 19:36 +0000
1from __future__ import annotations
3import datetime
4import logging
5from collections.abc import Iterator
6from typing import TYPE_CHECKING, Any, ClassVar, Literal, Self
8import sqlalchemy as sa
9from slixmpp import JID, Message, Presence
10from slixmpp.plugins.xep_0292.stanza import VCard4
11from slixmpp.types import MessageTypes, PresenceShows
12from typing_extensions import TypedDict
14from ..core.mixins import FullCarbonMixin, ImageMixin
15from ..core.mixins.disco import ContactAccountDiscoMixin
16from ..core.mixins.recipient import RecipientMixin
17from ..db.avatar import CachedAvatar
18from ..db.models import Contact, ContactSent
19from ..util.types import (
20 ClientType,
21 ContactMessage,
22 ContactSticker,
23 ExtraAttrMapping,
24 HoleBound,
25 MessageOrPresenceTypeVar,
26)
28if TYPE_CHECKING:
29 from ..command.base import ContactCommand
30 from ..core.session import BaseSession
31 from ..room.participant import LegacyParticipant
32 from .roster import LegacyRoster
35class _ContactProps(TypedDict, total=False, closed=True):
36 name: str | None
37 is_friend: bool
38 full_name: str | None
39 birthday: datetime.date | None
40 bio: str | None
41 url: str | None
42 email: str | None
43 country: str | None
44 locality: str | None
45 pronouns: str | None
46 client_type: ClientType
47 online: bool
48 pshow: PresenceShows | Literal["available"] | None
49 pstatus: str | None
50 last_seen: datetime.datetime | None
51 added_to_roster: bool
52 phones: list[str] | None
55class LegacyContact[T: ExtraAttrMapping = Any](
56 ImageMixin[T, _ContactProps],
57 ContactAccountDiscoMixin,
58 FullCarbonMixin,
59 RecipientMixin,
60):
61 """
62 This class centralizes actions in relation to a specific legacy contact.
64 You shouldn't create instances of contacts manually, but rather rely on
65 :meth:`.LegacyRoster.get_or_create` to ensure that contact instances are
66 singletons. The :class:`.LegacyRoster` instance of a session is accessible
67 through the :attr:`.BaseSession.contacts` attribute.
69 Typically, your plugin should have methods hook to the legacy events and
70 call appropriate methods here to transmit the "legacy action" to the xmpp
71 user. This should look like this:
73 .. code-block:python
75 class Session(BaseSession):
76 ...
78 async def on_cool_chat_network_new_text_message(self, legacy_msg_event):
79 contact = self.contacts.get_or_create(legacy_msg_event.from)
80 contact.send_text(legacy_msg_event.text)
82 async def on_cool_chat_network_new_typing_event(self, legacy_typing_event):
83 contact = self.contacts.get_or_create(legacy_msg_event.from)
84 contact.composing()
85 ...
87 Use ``carbon=True`` as a keyword arg for methods to represent an action FROM
88 the user TO the contact, typically when the user uses an official client to
89 do an action such as sending a message or marking as message as read.
90 This will use :xep:`0363` to impersonate the XMPP user in order.
91 """
93 RESOURCE: str = "slidge"
94 """
95 A full JID, including a resource part is required for chat states (and maybe other stuff)
96 to work properly. This is the name of the resource the contacts will use.
97 """
98 PROPAGATE_PRESENCE_TO_ROOMS = True
100 _mtype: MessageTypes = "chat"
101 _can_send_carbon = True
102 is_participant: Literal[False] = False
103 is_group: Literal[False] = False
105 _ONLY_SEND_PRESENCE_CHANGES = True
107 _STRIP_SHORT_DELAY = True
108 _NON_FRIEND_PRESENCES_FILTER: ClassVar[set[str]] = {"subscribe", "unsubscribed"}
110 _INVITATION_RECIPIENT = True
112 _commands: ClassVar[dict[str, type[ContactCommand[LegacyContact]]]] = {}
113 _commands_chat: ClassVar[dict[str, type[ContactCommand[LegacyContact]]]] = {}
115 _stored: Contact
116 _model: Contact
118 extra_attributes: T
119 _session: BaseSession[LegacyRoster[Self], Any]
121 def __init__(
122 self, session: BaseSession[LegacyRoster[Self], Any], stored: Contact
123 ) -> None:
124 self._session = session
125 self.xmpp = session.xmpp
126 self._stored = stored
127 self._set_logger()
128 self.__batch_set_presence = False
129 super().__init__()
131 @property
132 def is_user(self) -> bool:
133 return self.legacy_id == self._session.contacts.user_legacy_id
135 def _recipient_pk(self) -> int:
136 return self._stored.id
138 @property
139 def name(self) -> str:
140 """
141 Friendly name of the contact, as it should appear in the user's roster
142 """
143 return self._stored.nick or ""
145 @name.setter
146 def name(self, n: str | None) -> None:
147 if not self._update_stored_attribute(nick=n):
148 return
149 self._set_logger()
150 if self.is_friend:
151 self.xmpp.pubsub.broadcast_nick(
152 user_jid=self.user_jid, jid=self.jid.bare, nick=n
153 )
154 for p in self.participants:
155 p.nickname = n or str(self.legacy_id)
157 @property
158 def is_friend(self) -> bool:
159 """
160 Whether this contact is a friend or not.
161 """
162 return self._stored.is_friend
164 @is_friend.setter
165 def is_friend(self, value: bool) -> None:
166 if (
167 self._update_stored_attribute(is_friend=value)
168 and value
169 and not self._session.contacts.is_filling
170 ):
171 # TODO: should we broadcast avatar and nick at this stage?
172 # maybe it's actually triggered by receiving the user's presence?
173 self._session.create_task(
174 self._session.contacts._add_to_roster(self), f"add {self} to roster"
175 )
177 @property
178 def full_name(self) -> str | None:
179 return self._stored.full_name
181 @full_name.setter
182 def full_name(self, full_name: str | None) -> None:
183 self._update_stored_attribute(full_name=full_name)
185 @property
186 def birthday(self) -> datetime.date | None:
187 return self._stored.birthday
189 @birthday.setter
190 def birthday(self, birthday: datetime.date | None) -> None:
191 """
192 The birthday of this contact. Only appears in the VCard (:xep:`0292`).
193 """
194 self._update_stored_attribute(birthday=birthday)
196 @property
197 def bio(self) -> str | None:
198 return self._stored.bio
200 @bio.setter
201 def bio(self, bio: str | None) -> None:
202 self._update_stored_attribute(bio=bio)
204 @property
205 def url(self) -> str | None:
206 return self._stored.url
208 @url.setter
209 def url(self, url: str | None) -> None:
210 self._update_stored_attribute(url=url)
212 @property
213 def email(self) -> str | None:
214 return self._stored.email
216 @email.setter
217 def email(self, email: str | None) -> None:
218 self._update_stored_attribute(email=email)
220 @property
221 def country(self) -> str | None:
222 return self._stored.country
224 @country.setter
225 def country(self, country: str | None) -> None:
226 self._update_stored_attribute(country=country)
228 @property
229 def locality(self) -> str | None:
230 return self._stored.locality
232 @locality.setter
233 def locality(self, locality: str | None) -> None:
234 self._update_stored_attribute(locality=locality)
236 @property
237 def pronouns(self) -> str | None:
238 return self._stored.pronouns
240 @pronouns.setter
241 def pronouns(self, pronouns: str | None) -> None:
242 self._update_stored_attribute(pronouns=pronouns)
244 @property
245 def phones(self) -> list[str] | None:
246 return self._stored.phones
248 @phones.setter
249 def phones(self, phones: list[str] | None) -> None:
250 self._update_stored_attribute(phones=phones)
252 @property
253 def client_type(self) -> ClientType:
254 """
255 The client type of this contact, cf https://xmpp.org/registrar/disco-categories.html#client
257 Default is "pc".
258 """
259 return self._stored.client_type
261 @client_type.setter
262 def client_type(self, value: ClientType) -> None:
263 self._update_stored_attribute(client_type=value)
265 async def on_message(self, message: ContactMessage) -> str | None:
266 """
267 Triggered when the user sends a message to this :term:`Contact`.
269 :return: A message ID of that can be used later to further reference
270 this message (reactions, read marks, etc.).
271 """
272 raise NotImplementedError
274 async def on_sticker(self, sticker: ContactSticker) -> str | None:
275 """
276 Triggered when the user sends a sticker to this :term:`Contact`.
278 :param sticker: The sticker sent by the user.
280 :return: A message ID of that can be used later to further reference
281 this message (reactions, read marks, etc.).
282 """
283 raise NotImplementedError
285 @property
286 def jid(self) -> JID:
287 jid = self._stored.jid
288 jid.resource = self.RESOURCE
289 return jid
291 @property
292 def legacy_id(self) -> str:
293 return self._stored.legacy_id
295 async def get_vcard(self) -> VCard4:
296 if not self._stored.extra_info_filled:
297 await self.fill_extra_info()
298 self.extra_info_filled = True
299 with self.xmpp.store.session() as orm:
300 orm.add(self._stored)
301 return self._stored.vcard(self.xmpp.boundjid.bare)
303 @property
304 def extra_info_filled(self) -> bool:
305 return self._stored.extra_info_filled
307 @extra_info_filled.setter
308 def extra_info_filled(self, value: bool) -> None:
309 self._update_stored_attribute(extra_info_filled=value)
311 @property
312 def added_to_roster(self) -> bool:
313 return self._stored.added_to_roster
315 @added_to_roster.setter
316 def added_to_roster(self, value: bool) -> None:
317 self._update_stored_attribute(added_to_roster=value)
319 async def fill_extra_info(self) -> None:
320 pass
322 @property
323 def participants(self) -> Iterator[LegacyParticipant[Self]]:
324 self._stored.load_participants(self.xmpp.store.session)
325 for p in self._stored.participants:
326 room = self._session.rooms._from_store(p.room)
327 yield room.participants._from_store(p, contact=self)
329 @property
330 def DISCO_TYPE(self) -> str:
331 return self.client_type
333 def _set_logger(self) -> None:
334 self.log = logging.getLogger(f"{self.user_jid.bare}:contact:{self}")
336 def __repr__(self) -> str:
337 return f"<Contact #{self._stored.id} '{self.name}' ({self.legacy_id} - {self.jid.user})'>"
339 def __propagate_to_participants(self, stanza: Presence) -> None:
340 if not self.PROPAGATE_PRESENCE_TO_ROOMS:
341 return
342 self._stored.ensure_loaded("presence", self.xmpp.store.session)
343 for part in self.participants:
344 part._send(
345 part._make_presence(
346 pshow=self._stored.pshow,
347 pstatus=self._stored.pstatus,
348 last_seen=self._stored.last_seen,
349 )
350 )
352 def _send(
353 self,
354 stanza: MessageOrPresenceTypeVar,
355 carbon: bool = False,
356 nick: bool = False,
357 **send_kwargs: Any, # noqa:ANN401
358 ) -> MessageOrPresenceTypeVar:
359 carbon = carbon or self.is_user
361 if carbon and isinstance(stanza, Message):
362 stanza["to"] = self.jid.bare
363 stanza["from"] = self.user_jid
364 self._privileged_send(stanza)
365 return stanza
367 if isinstance(stanza, Presence):
368 self.__propagate_to_participants(stanza)
369 if (
370 not self.is_friend
371 and stanza["type"] not in self._NON_FRIEND_PRESENCES_FILTER
372 ):
373 return stanza
374 if self.name and (nick or not self.is_friend):
375 n = self.xmpp.plugin["xep_0172"].stanza.UserNick()
376 n["nick"] = self.name
377 stanza.append(n)
378 if self.xmpp.MARK_ALL_MESSAGES and is_markable(stanza):
379 with self.xmpp.store.session(expire_on_commit=False) as orm:
380 self._stored = orm.merge(self._stored)
381 exists = (
382 orm.query(ContactSent)
383 .filter_by(contact_id=self._stored.id, msg_id=stanza["id"])
384 .first()
385 )
386 if exists:
387 self.log.warning(
388 "Contact has already sent message %s", stanza["id"]
389 )
390 else:
391 new = ContactSent(contact=self._stored, msg_id=stanza["id"])
392 orm.add(new)
393 self._stored.sent_order.append(new)
394 orm.commit()
395 stanza["to"] = self.user_jid
396 stanza.send()
397 return stanza
399 def _store_last_sent_msg(
400 self, legacy_id: str, when: datetime.datetime | None
401 ) -> None:
402 with self.xmpp.store.session(expire_on_commit=False) as orm:
403 orm.execute(
404 sa.update(Contact)
405 .where(Contact.id == self._recipient_pk())
406 .values(
407 last_sent_msg_legacy_id=legacy_id,
408 last_sent_msg_date=when or datetime.datetime.now(tz=datetime.UTC),
409 )
410 )
411 orm.commit()
413 def pop_unread_xmpp_ids_up_to(self, horizon_xmpp_id: str) -> list[str]:
414 """
415 Return XMPP msg ids sent by this contact up to a given XMPP msg id.
417 Legacy modules have no reason to use this, but it is used by slidge core
418 for legacy networks that need to mark all messages as read (most XMPP
419 clients only send a read marker for the latest message).
421 This has side effects, if the horizon XMPP id is found, messages up to
422 this horizon are cleared, to avoid sending the same read mark twice.
424 :param horizon_xmpp_id: The latest message
425 :return: A list of XMPP ids up to horizon_xmpp_id, included
426 """
427 with self.xmpp.store.session() as orm:
428 assert self._stored.id is not None
429 ids = self.xmpp.store.contacts.pop_sent_up_to(
430 orm, self._stored.id, horizon_xmpp_id
431 )
432 orm.commit()
433 return ids
435 def _post_image_update(
436 self, cached_avatar: CachedAvatar | None, *, attr: str = "avatar"
437 ) -> None:
438 if self.is_friend:
439 self._session.create_task(
440 self._session.xmpp.pubsub.broadcast_avatar(
441 self.jid.bare, self._session.user_jid, cached_avatar
442 ),
443 name=f"Post avatar update of {self}",
444 )
445 self.log.debug("Propagating avatar to participants")
446 for p in self._stored.get_participants_for_avatar_propagation(
447 self.xmpp.store.session
448 ):
449 room = self._session.rooms._from_store(p.room)
450 part = room.participants._from_store(p, contact=self)
451 part.send_last_presence(force=True)
452 self.log.debug("Propagating avatar to participants: done")
454 async def _broadcast_pubsub_items(self) -> None:
455 if not self.is_friend:
456 return
458 await self._session.contacts.ready
460 cached_avatar = self.get_cached_avatar()
461 if cached_avatar is not None:
462 await self.xmpp.pubsub.broadcast_avatar(
463 self.jid.bare, self._session.user_jid, cached_avatar
464 )
465 nick = self.name
467 if nick is not None:
468 self.xmpp.pubsub.broadcast_nick(
469 self._session.user_jid,
470 self.jid.bare,
471 nick,
472 )
474 def send_friend_request(self, text: str | None = None) -> None:
475 presence = self._make_presence(ptype="subscribe", pstatus=text, bare=True)
476 self._send(presence, nick=True)
478 async def accept_friend_request(self, text: str | None = None) -> None:
479 """
480 Call this to signify that this Contact has accepted to be a friend
481 of the user.
483 :param text: Optional message from the friend to the user
484 """
485 self.is_friend = True
486 self.added_to_roster = True
487 self.log.debug("Accepting friend request")
488 presence = self._make_presence(ptype="subscribed", pstatus=text, bare=True)
489 self._send(presence, nick=True)
490 self.send_last_presence()
491 await self._broadcast_pubsub_items()
492 self.log.debug("Accepted friend request")
494 def reject_friend_request(self, text: str | None = None) -> None:
495 """
496 Call this to signify that this Contact has refused to be a contact
497 of the user (or that they don't want to be friends anymore)
499 :param text: Optional message from the non-friend to the user
500 """
501 presence = self._make_presence(ptype="unsubscribed", pstatus=text, bare=True)
503 self._send(presence, nick=True)
504 self.is_friend = False
506 async def on_friend_request(self, text: str = "") -> None:
507 """
508 Called when receiving a "subscribe" presence, ie, "I would like to add
509 you to my contacts/friends", from the user to this contact.
511 In XMPP terms: "I would like to receive your presence updates"
513 This is only called if self.is_friend = False. If self.is_friend = True,
514 slidge will automatically "accept the friend request", ie, reply with
515 a "subscribed" presence.
517 When called, a 'friend request event' should be sent to the legacy
518 service, and when the contact responds, you should either call
519 self.accept_subscription() or self.reject_subscription()
520 """
522 async def on_friend_delete(self, text: str = "") -> None:
523 """
524 Called when receiving an "unsubscribed" presence, ie, "I would like to
525 remove you to my contacts/friends" or "I refuse your friend request"
526 from the user to this contact.
528 In XMPP terms: "You won't receive my presence updates anymore (or you
529 never have)".
530 """
532 async def on_friend_accept(self) -> None:
533 """
534 Called when receiving a "subscribed" presence, ie, "I accept to be
535 your/confirm that you are my friend" from the user to this contact.
537 In XMPP terms: "You will receive my presence updates".
538 """
540 def unsubscribe(self) -> None:
541 """
542 (internal use by slidge)
544 Send an "unsubscribe", "unsubscribed", "unavailable" presence sequence
545 from this contact to the user, ie, "this contact has removed you from
546 their 'friends'".
547 """
548 for ptype in "unsubscribe", "unsubscribed", "unavailable":
549 self.xmpp.send_presence(pfrom=self.jid, pto=self.user_jid.bare, ptype=ptype)
551 def _on_delete(self) -> None:
552 self.unsubscribe()
554 def _make_presence(
555 self,
556 *,
557 last_seen: datetime.datetime | None = None,
558 **presence_kwargs: Any, # noqa:ANN401
559 ) -> Presence:
560 p = super()._make_presence(last_seen=last_seen, **presence_kwargs)
561 caps = self.xmpp.plugin["xep_0115"]
563 if p.get_from().resource and (ver := self.xmpp.get_caps_ver(self.client_type)):
564 p["caps"]["node"] = caps.caps_node
565 p["caps"]["hash"] = caps.hash
566 p["caps"]["ver"] = ver
567 return p
569 async def backfill(self, after: HoleBound | None) -> None:
570 """
571 This method can be overridden to implement history fetching for this
572 contact.
574 Since the message archive (:xep:`0313`) is managed by the XMPP server
575 of the user, there are several caveats.
577 - We cannot prevent the XMPP server from injecting a :xep:`0203`
578 timestamp at the time it receives the messages. Most XMPP clients
579 will use that timestamp instead of the one we inject and thus message
580 ordering may be messed up under certain circumstances.
581 - We cannot know if a message is already in this archive, so legacy
582 modules should ensure that this does not send message that have
583 already sent before or there will be duplicates.
584 - Related to the previous point, if a legacy network APIs returns a
585 message while iterating over a "fetch history" call and also pass
586 it as a live message, you may end up with duplicates.
588 For these reasons, we recommend sending only recent messages, mostly
589 messages that could have been missed when slidge was down.
591 NB: if the legacy client receives "message while it was down" as live
592 messages on startup, this is pretty much useless.
594 :param after: Last message to or from this contact that slidge saw
595 passing through. If ``None``, it means slidge never saw any message
596 from this contact.
597 """
600def is_markable(stanza: Message | Presence) -> bool:
601 if isinstance(stanza, Presence):
602 return False
603 return bool(stanza["body"])
606log = logging.getLogger(__name__)