Fix timezone handling

This commit is contained in:
Thomas Sileo 2022-07-11 12:48:38 +02:00
parent 8dd6890a7d
commit 296cb05ace
3 changed files with 14 additions and 6 deletions

View file

@ -3,7 +3,6 @@ from datetime import datetime
from typing import Any from typing import Any
import pydantic import pydantic
from dateutil.parser import isoparse
from markdown import markdown from markdown import markdown
from app import activitypub as ap 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 Actor
from app.actor import RemoteActor from app.actor import RemoteActor
from app.media import proxied_media_url from app.media import proxied_media_url
from app.utils.datetime import parse_isoformat
class Object: class Object:
@ -46,9 +46,9 @@ class Object:
def ap_published_at(self) -> datetime | None: def ap_published_at(self) -> datetime | None:
# TODO: default to None? or now()? # TODO: default to None? or now()?
if "published" in self.ap_object: 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: elif "created" in self.ap_object:
return isoparse(self.ap_object["created"]) return parse_isoformat(self.ap_object["created"])
return None return None
@property @property

View file

@ -6,7 +6,6 @@ from urllib.parse import urlparse
import fastapi import fastapi
import httpx import httpx
from dateutil.parser import isoparse
from loguru import logger from loguru import logger
from sqlalchemy import delete from sqlalchemy import delete
from sqlalchemy import func from sqlalchemy import func
@ -35,6 +34,7 @@ from app.source import markdownify
from app.uploads import upload_to_attachment from app.uploads import upload_to_attachment
from app.utils import opengraph from app.utils import opengraph
from app.utils import webmentions from app.utils import webmentions
from app.utils.datetime import parse_isoformat
AnyboxObject = models.InboxObject | models.OutboxObject AnyboxObject = models.InboxObject | models.OutboxObject
@ -724,7 +724,7 @@ async def _handle_create_activity(
ap_published_at = now() ap_published_at = now()
if "published" in ro.ap_object: 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_reply = bool(ro.in_reply_to)
is_local_reply = ro.in_reply_to and ro.in_reply_to.startswith(BASE_URL) 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() ap_published_at = now()
if "published" in raw_object: 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) activity_ro = RemoteObject(raw_object, actor=actor)

8
app/utils/datetime.py Normal file
View file

@ -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)