in phones/models.py [0:0]
def get_last_text_sender(relay_number: RelayNumber) -> InboundContact | None:
"""
Get the last text sender.
MPP-2581 introduces a last_text_date column for determining the last sender.
Before MPP-2581, the last_inbound_date with last_inbound_type=text was used.
During the transition, look at both methods.
"""
try:
latest = InboundContact.objects.filter(
relay_number=relay_number, last_text_date__isnull=False
).latest("last_text_date")
except InboundContact.DoesNotExist:
latest = None
try:
latest_by_old_method = InboundContact.objects.filter(
relay_number=relay_number, last_inbound_type="text"
).latest("last_inbound_date")
except InboundContact.DoesNotExist:
latest_by_old_method = None
if (latest is None and latest_by_old_method is not None) or (
latest
and latest_by_old_method
and latest != latest_by_old_method
and latest.last_text_date
and latest_by_old_method.last_inbound_date > latest.last_text_date
):
# Pre-MPP-2581 server handled the latest text message
return latest_by_old_method
return latest