From 296cb05acefe145d7c1fe310fc01532c6832f0e6 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 11 Jul 2022 12:48:38 +0200 Subject: [PATCH] Fix timezone handling --- app/ap_object.py | 6 +++--- app/boxes.py | 6 +++--- app/utils/datetime.py | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 app/utils/datetime.py diff --git a/app/ap_object.py b/app/ap_object.py index 86738cf..df3f698 100644 --- a/app/ap_object.py +++ b/app/ap_object.py @@ -3,7 +3,6 @@ from datetime import datetime from typing import Any import pydantic -from dateutil.parser import isoparse from markdown import markdown from app import activitypub as ap @@ -11,6 +10,7 @@ from app.actor import LOCAL_ACTOR from app.actor import Actor from app.actor import RemoteActor from app.media import proxied_media_url +from app.utils.datetime import parse_isoformat class Object: @@ -46,9 +46,9 @@ class Object: def ap_published_at(self) -> datetime | None: # TODO: default to None? or now()? if "published" in self.ap_object: - return isoparse(self.ap_object["published"]) + return parse_isoformat(self.ap_object["published"]) elif "created" in self.ap_object: - return isoparse(self.ap_object["created"]) + return parse_isoformat(self.ap_object["created"]) return None @property diff --git a/app/boxes.py b/app/boxes.py index dde094d..e1ee325 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -6,7 +6,6 @@ from urllib.parse import urlparse import fastapi import httpx -from dateutil.parser import isoparse from loguru import logger from sqlalchemy import delete from sqlalchemy import func @@ -35,6 +34,7 @@ from app.source import markdownify from app.uploads import upload_to_attachment from app.utils import opengraph from app.utils import webmentions +from app.utils.datetime import parse_isoformat AnyboxObject = models.InboxObject | models.OutboxObject @@ -724,7 +724,7 @@ async def _handle_create_activity( ap_published_at = now() if "published" in ro.ap_object: - ap_published_at = isoparse(ro.ap_object["published"]) + ap_published_at = parse_isoformat(ro.ap_object["published"]) is_reply = bool(ro.in_reply_to) is_local_reply = ro.in_reply_to and ro.in_reply_to.startswith(BASE_URL) @@ -847,7 +847,7 @@ async def save_to_inbox( ap_published_at = now() if "published" in raw_object: - ap_published_at = isoparse(raw_object["published"]) + ap_published_at = parse_isoformat(raw_object["published"]) activity_ro = RemoteObject(raw_object, actor=actor) diff --git a/app/utils/datetime.py b/app/utils/datetime.py new file mode 100644 index 0000000..800b00d --- /dev/null +++ b/app/utils/datetime.py @@ -0,0 +1,8 @@ +from datetime import datetime +from datetime import timezone + +from dateutil.parser import isoparse + + +def parse_isoformat(isodate: str) -> datetime: + return isoparse(isodate).astimezone(timezone.utc)