Allow linking chats

This commit is contained in:
2026-02-19 17:13:34 +00:00
parent bac2841298
commit 0816687c71
15 changed files with 369 additions and 461 deletions

View File

@@ -7,7 +7,7 @@ from django.views import View
from mixins.views import ObjectList, ObjectRead
from core.clients import transport
from core.models import PersonIdentifier
from core.models import PersonIdentifier, PlatformChatLink
from core.util import logs
from core.views.compose import _compose_urls, _service_icon_class
from core.views.manage.permissions import SuperUserRequiredMixin
@@ -288,7 +288,10 @@ class WhatsAppChatsList(WhatsAppContactsList):
identifier = str(key or "").strip()
if not identifier:
continue
if "@newsletter" in identifier:
continue
identifier = identifier.split("@", 1)[0].strip() or identifier
identifier = identifier.lstrip("+")
if identifier in seen:
continue
seen.add(identifier)
@@ -327,50 +330,88 @@ class WhatsAppChatsList(WhatsAppContactsList):
if rows:
rows.sort(key=lambda row: row.get("last_ts", 0), reverse=True)
return rows
else:
# Fallback: if no anchors yet, surface the runtime contacts (best effort live state)
for item in combined_contacts:
raw_item_id = str(
item.get("identifier") or item.get("jid") or item.get("chat") or ""
).strip()
if "@newsletter" in raw_item_id:
continue
identifier = raw_item_id
if not identifier:
continue
identifier = identifier.split("@", 1)[0].strip()
if not identifier or identifier in seen:
continue
seen.add(identifier)
jid = str(item.get("jid") or "").strip()
linked = self._linked_identifier(identifier, jid)
urls = _compose_urls(
"whatsapp",
identifier,
linked.person_id if linked else None,
)
name = (
str(item.get("name") or item.get("chat") or "").strip()
or (linked.person.name if linked else "")
or jid
or identifier
or "WhatsApp Chat"
)
rows.append(
{
"identifier": identifier,
"jid": jid or identifier,
"name": name,
"is_group": False,
"service_icon_class": _service_icon_class("whatsapp"),
"person_name": linked.person.name if linked else "",
"compose_page_url": urls["page_url"],
"compose_widget_url": urls["widget_url"],
"match_url": (
f"{reverse('compose_contact_match')}?"
f"{urlencode({'service': 'whatsapp', 'identifier': identifier})}"
),
"last_ts": 0,
}
)
# Fallback: if no anchors yet, surface the runtime contacts (best effort live state)
for item in combined_contacts:
identifier = str(
item.get("identifier") or item.get("jid") or item.get("chat") or ""
).strip()
if not identifier:
db_group_ids = set()
db_groups = PlatformChatLink.objects.filter(
user=self.request.user,
service="whatsapp",
is_group=True,
)
for link in db_groups:
bare_id = str(link.chat_identifier or "").strip()
if not bare_id:
continue
identifier = identifier.split("@", 1)[0].strip()
if not identifier or identifier in seen:
continue
seen.add(identifier)
jid = str(item.get("jid") or "").strip()
linked = self._linked_identifier(identifier, jid)
urls = _compose_urls(
"whatsapp",
identifier,
linked.person_id if linked else None,
)
name = (
str(item.get("name") or item.get("chat") or "").strip()
or (linked.person.name if linked else "")
or jid
or identifier
or "WhatsApp Chat"
)
rows.append(
{
"identifier": identifier,
"jid": jid or identifier,
"name": name,
"service_icon_class": _service_icon_class("whatsapp"),
"person_name": linked.person.name if linked else "",
"compose_page_url": urls["page_url"],
"compose_widget_url": urls["widget_url"],
"match_url": (
f"{reverse('compose_contact_match')}?"
f"{urlencode({'service': 'whatsapp', 'identifier': identifier})}"
),
"last_ts": 0,
}
)
return rows
db_group_ids.add(bare_id)
if bare_id not in seen:
seen.add(bare_id)
jid = link.chat_jid or f"{bare_id}@g.us"
urls = _compose_urls("whatsapp", bare_id, None)
rows.append(
{
"identifier": bare_id,
"jid": jid,
"name": link.chat_name or bare_id,
"is_group": True,
"service_icon_class": _service_icon_class("whatsapp"),
"person_name": "",
"compose_page_url": urls["page_url"],
"compose_widget_url": urls["widget_url"],
"match_url": "",
"last_ts": int(link.updated_at.timestamp()),
}
)
for row in rows:
if not row.get("is_group") and row.get("identifier") in db_group_ids:
row["is_group"] = True
return [row for row in rows if row.get("is_group")]
class WhatsAppAccountAdd(SuperUserRequiredMixin, ObjectRead):