Skip to content

Commit

Permalink
Protocol.receive: post age bug fix, handle missing timezone
Browse files Browse the repository at this point in the history
snarfed committed Jan 29, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7d6b30f commit ece5592
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions protocol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Base protocol class and common code."""
import copy
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
import logging
import os
import re
@@ -965,7 +965,10 @@ def receive(from_cls, obj, authed_as=None, internal=False, received_at=None):
if obj.type == 'post':
if published := inner_obj_as1.get('published'):
try:
age = util.now() - util.parse_iso8601(published)
published_dt = util.parse_iso8601(published)
if not published_dt.tzinfo:
published_dt = published_dt.replace(tzinfo=timezone.utc)
age = util.now() - published_dt
if age > CREATE_MAX_AGE:
error(f'Ignoring, too old, {age} is over {CREATE_MAX_AGE}',
status=204)
15 changes: 15 additions & 0 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
@@ -3050,6 +3050,21 @@ def test_too_old(self):
self.assertEqual([], Fake.sent)
self.assertEqual([], OtherFake.sent)

def test_too_old_published_without_timezone(self):
Follower.get_or_create(to=self.user, from_=self.alice)

with self.assertRaises(NoContent):
Fake.receive_as1({
'id': 'fake:post',
'objectType': 'note',
'author': 'fake:user',
'published': '2021-12-14T03:04:05', # NOW - 2w
})
self.assertIsNone(Object.get_by_id('fake:post'))

self.assertEqual([], Fake.sent)
self.assertEqual([], OtherFake.sent)

def test_receive_activity_lease(self):
Follower.get_or_create(to=self.user, from_=self.alice)

0 comments on commit ece5592

Please sign in to comment.