Pull groups from WhatsApp
This commit is contained in:
@@ -7,7 +7,7 @@ from django.views import View
|
||||
from mixins.views import ObjectList, ObjectRead
|
||||
|
||||
from core.clients import transport
|
||||
from core.models import ChatSession, Message, PersonIdentifier
|
||||
from core.models import PersonIdentifier
|
||||
from core.util import logs
|
||||
from core.views.compose import _compose_urls, _service_icon_class
|
||||
from core.views.manage.permissions import SuperUserRequiredMixin
|
||||
@@ -263,84 +263,102 @@ class WhatsAppChatsList(WhatsAppContactsList):
|
||||
rows = []
|
||||
seen = set()
|
||||
state = transport.get_runtime_state("whatsapp")
|
||||
|
||||
runtime_contacts = state.get("contacts") or []
|
||||
runtime_name_map = {}
|
||||
for item in runtime_contacts:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
identifier = str(item.get("identifier") or "").strip()
|
||||
runtime_groups = state.get("groups") or []
|
||||
combined_contacts = []
|
||||
for item in runtime_contacts + runtime_groups:
|
||||
if isinstance(item, dict):
|
||||
combined_contacts.append(item)
|
||||
contact_index = {}
|
||||
for item in combined_contacts:
|
||||
raw_identifier = str(
|
||||
item.get("identifier") or item.get("jid") or item.get("chat") or ""
|
||||
).strip()
|
||||
jid = str(item.get("jid") or "").strip()
|
||||
name = str(item.get("name") or item.get("chat") or "").strip()
|
||||
base_id = raw_identifier.split("@", 1)[0].strip()
|
||||
jid_base = jid.split("@", 1)[0].strip()
|
||||
for key in {raw_identifier, base_id, jid, jid_base}:
|
||||
if key:
|
||||
contact_index[key] = {"name": name, "jid": jid}
|
||||
|
||||
history_anchors = state.get("history_anchors") or {}
|
||||
for key, anchor in (history_anchors.items() if isinstance(history_anchors, dict) else []):
|
||||
identifier = str(key or "").strip()
|
||||
if not identifier:
|
||||
continue
|
||||
runtime_name_map[identifier] = str(item.get("name") or "").strip()
|
||||
|
||||
sessions = (
|
||||
ChatSession.objects.filter(
|
||||
user=self.request.user,
|
||||
identifier__service="whatsapp",
|
||||
)
|
||||
.select_related("identifier", "identifier__person")
|
||||
.order_by("-last_interaction", "-id")
|
||||
)
|
||||
for session in sessions:
|
||||
identifier = str(session.identifier.identifier or "").strip()
|
||||
if not identifier or identifier in seen:
|
||||
identifier = identifier.split("@", 1)[0].strip() or identifier
|
||||
if identifier in seen:
|
||||
continue
|
||||
seen.add(identifier)
|
||||
latest = (
|
||||
Message.objects.filter(user=self.request.user, session=session)
|
||||
.order_by("-ts")
|
||||
.first()
|
||||
anchor_jid = str((anchor or {}).get("chat_jid") or "").strip()
|
||||
contact = contact_index.get(identifier) or contact_index.get(anchor_jid)
|
||||
jid = (contact or {}).get("jid") or anchor_jid or identifier
|
||||
linked = self._linked_identifier(identifier, jid)
|
||||
urls = _compose_urls(
|
||||
"whatsapp",
|
||||
identifier,
|
||||
linked.person_id if linked else None,
|
||||
)
|
||||
urls = _compose_urls("whatsapp", identifier, session.identifier.person_id)
|
||||
preview = str((latest.text if latest else "") or "").strip()
|
||||
if len(preview) > 80:
|
||||
preview = f"{preview[:77]}..."
|
||||
display_name = (
|
||||
preview
|
||||
or runtime_name_map.get(identifier)
|
||||
or session.identifier.person.name
|
||||
name = (
|
||||
(contact or {}).get("name")
|
||||
or (linked.person.name if linked else "")
|
||||
or jid
|
||||
or identifier
|
||||
or "WhatsApp Chat"
|
||||
)
|
||||
rows.append(
|
||||
{
|
||||
"identifier": identifier,
|
||||
"jid": identifier,
|
||||
"name": display_name,
|
||||
"jid": jid,
|
||||
"name": name,
|
||||
"service_icon_class": _service_icon_class("whatsapp"),
|
||||
"person_name": session.identifier.person.name,
|
||||
"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": int(latest.ts or 0) if latest else 0,
|
||||
"last_ts": int((anchor or {}).get("ts") or (anchor or {}).get("updated_at") or 0),
|
||||
}
|
||||
)
|
||||
# Fallback: show synced WhatsApp contacts as chat entries even when no
|
||||
# local message history exists yet.
|
||||
for item in runtime_contacts:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
identifier = str(item.get("identifier") or item.get("jid") or "").strip()
|
||||
|
||||
if rows:
|
||||
rows.sort(key=lambda row: row.get("last_ts", 0), reverse=True)
|
||||
return rows
|
||||
|
||||
# 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:
|
||||
continue
|
||||
identifier = identifier.split("@", 1)[0].strip()
|
||||
if not identifier or identifier in seen:
|
||||
continue
|
||||
seen.add(identifier)
|
||||
linked = self._linked_identifier(identifier, str(item.get("jid") or ""))
|
||||
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": str(item.get("jid") or identifier).strip(),
|
||||
"name": str(item.get("name") or "WhatsApp Chat").strip()
|
||||
or "WhatsApp Chat",
|
||||
"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"],
|
||||
@@ -352,10 +370,7 @@ class WhatsAppChatsList(WhatsAppContactsList):
|
||||
"last_ts": 0,
|
||||
}
|
||||
)
|
||||
if rows:
|
||||
rows.sort(key=lambda row: row.get("last_ts", 0), reverse=True)
|
||||
return rows
|
||||
return super().get_queryset(*args, **kwargs)
|
||||
return rows
|
||||
|
||||
|
||||
class WhatsAppAccountAdd(SuperUserRequiredMixin, ObjectRead):
|
||||
|
||||
Reference in New Issue
Block a user