Increase security and reformat
This commit is contained in:
@@ -4,7 +4,7 @@ from typing import Iterable
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from core.models import Message, User
|
||||
from core.models import Message
|
||||
from core.presence import AvailabilitySignal, record_inferred_signal
|
||||
from core.presence.inference import now_ms
|
||||
|
||||
@@ -19,7 +19,9 @@ class Command(BaseCommand):
|
||||
parser.add_argument("--user-id", default="")
|
||||
parser.add_argument("--dry-run", action="store_true", default=False)
|
||||
|
||||
def _iter_messages(self, *, days: int, limit: int, service: str, user_id: str) -> Iterable[Message]:
|
||||
def _iter_messages(
|
||||
self, *, days: int, limit: int, service: str, user_id: str
|
||||
) -> Iterable[Message]:
|
||||
cutoff_ts = now_ms() - (max(1, int(days)) * 24 * 60 * 60 * 1000)
|
||||
qs = Message.objects.filter(ts__gte=cutoff_ts).select_related(
|
||||
"user", "session", "session__identifier", "session__identifier__person"
|
||||
@@ -40,7 +42,9 @@ class Command(BaseCommand):
|
||||
created = 0
|
||||
scanned = 0
|
||||
|
||||
for msg in self._iter_messages(days=days, limit=limit, service=service_filter, user_id=user_filter):
|
||||
for msg in self._iter_messages(
|
||||
days=days, limit=limit, service=service_filter, user_id=user_filter
|
||||
):
|
||||
scanned += 1
|
||||
identifier = getattr(getattr(msg, "session", None), "identifier", None)
|
||||
person = getattr(identifier, "person", None)
|
||||
@@ -48,12 +52,18 @@ class Command(BaseCommand):
|
||||
if not identifier or not person or not user:
|
||||
continue
|
||||
|
||||
service = str(getattr(msg, "source_service", "") or identifier.service or "").strip().lower()
|
||||
service = (
|
||||
str(getattr(msg, "source_service", "") or identifier.service or "")
|
||||
.strip()
|
||||
.lower()
|
||||
)
|
||||
if not service:
|
||||
continue
|
||||
|
||||
base_ts = int(getattr(msg, "ts", 0) or 0)
|
||||
message_author = str(getattr(msg, "custom_author", "") or "").strip().upper()
|
||||
message_author = (
|
||||
str(getattr(msg, "custom_author", "") or "").strip().upper()
|
||||
)
|
||||
outgoing = message_author in {"USER", "BOT"}
|
||||
|
||||
candidates = []
|
||||
@@ -84,7 +94,9 @@ class Command(BaseCommand):
|
||||
"origin": "backfill_contact_availability",
|
||||
"message_id": str(msg.id),
|
||||
"inferred_from": "read_receipt",
|
||||
"read_by": str(getattr(msg, "read_by_identifier", "") or ""),
|
||||
"read_by": str(
|
||||
getattr(msg, "read_by_identifier", "") or ""
|
||||
),
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user