Bugfixes and notifications improvements
This commit is contained in:
@@ -29,4 +29,4 @@ def parse_datetime(s: str) -> datetime:
|
||||
|
||||
|
||||
def now() -> str:
|
||||
ap.format_datetime(datetime.now(timezone.utc))
|
||||
return ap.format_datetime(datetime.now(timezone.utc))
|
||||
|
@@ -20,6 +20,9 @@ class MetaKey(Enum):
|
||||
ACTOR_ID = "actor_id"
|
||||
UNDO = "undo"
|
||||
PUBLISHED = "published"
|
||||
GC_KEEP = "gc_keep"
|
||||
OBJECT = "object"
|
||||
OBJECT_ACTOR = "object_actor"
|
||||
|
||||
|
||||
def _meta(mk: MetaKey) -> str:
|
||||
|
@@ -5,9 +5,11 @@ from typing import Dict
|
||||
|
||||
from little_boxes import activitypub as ap
|
||||
|
||||
from config import BASE_URL
|
||||
from config import DB
|
||||
from config import MetaKey
|
||||
from config import _meta
|
||||
from tasks import Tasks
|
||||
from utils.meta import by_actor
|
||||
from utils.meta import by_type
|
||||
from utils.meta import in_inbox
|
||||
@@ -18,6 +20,17 @@ _logger = logging.getLogger(__name__)
|
||||
_NewMeta = Dict[str, Any]
|
||||
|
||||
|
||||
def _is_from_outbox(activity: ap.BaseActivity) -> bool:
|
||||
return activity.id.startswith(BASE_URL)
|
||||
|
||||
|
||||
def _flag_as_notification(activity: ap.BaseActivity, new_meta: _NewMeta) -> None:
|
||||
new_meta.update(
|
||||
**{_meta(MetaKey.NOTIFICATION): True, _meta(MetaKey.NOTIFICATION_UNREAD): True}
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@singledispatch
|
||||
def set_inbox_flags(activity: ap.BaseActivity, new_meta: _NewMeta) -> None:
|
||||
return None
|
||||
@@ -44,13 +57,8 @@ def _accept_set_inbox_flags(activity: ap.Accept, new_meta: _NewMeta) -> None:
|
||||
)
|
||||
|
||||
# This Accept will be a "You started following $actor" notification
|
||||
new_meta.update(
|
||||
**{
|
||||
_meta(MetaKey.NOTIFICATION): True,
|
||||
_meta(MetaKey.NOTIFICATION_UNREAD): True,
|
||||
_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): follows_back,
|
||||
}
|
||||
)
|
||||
_flag_as_notification(activity, new_meta)
|
||||
new_meta.update(**{_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): follows_back})
|
||||
return None
|
||||
|
||||
|
||||
@@ -74,11 +82,38 @@ def _follow_set_inbox_flags(activity: ap.Follow, new_meta: _NewMeta) -> None:
|
||||
)
|
||||
|
||||
# This Follow will be a "$actor started following you" notification
|
||||
new_meta.update(
|
||||
**{
|
||||
_meta(MetaKey.NOTIFICATION): True,
|
||||
_meta(MetaKey.NOTIFICATION_UNREAD): True,
|
||||
_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): follows_back,
|
||||
}
|
||||
)
|
||||
_flag_as_notification(activity, new_meta)
|
||||
new_meta.update(**{_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): follows_back})
|
||||
return None
|
||||
|
||||
|
||||
@set_inbox_flags.register
|
||||
def _like_set_inbox_flags(activity: ap.Like, new_meta: _NewMeta) -> None:
|
||||
# Is it a Like of local acitivty/from the outbox
|
||||
if _is_from_outbox(activity.get_object()):
|
||||
# Flag it as a notification
|
||||
_flag_as_notification(activity, new_meta)
|
||||
|
||||
# Cache the object (for display on the notifcation page)
|
||||
Tasks.cache_object(activity.id)
|
||||
|
||||
# Also set the "keep mark" for the GC (as we want to keep it forever)
|
||||
new_meta.update(**{_meta(MetaKey.GC_KEEP): True})
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@set_inbox_flags.register
|
||||
def _announce_set_inbox_flags(activity: ap.Announce, new_meta: _NewMeta) -> None:
|
||||
# Is it a Like of local acitivty/from the outbox
|
||||
if _is_from_outbox(activity.get_object()):
|
||||
# Flag it as a notification
|
||||
_flag_as_notification(activity, new_meta)
|
||||
|
||||
# Also set the "keep mark" for the GC (as we want to keep it forever)
|
||||
new_meta.update(**{_meta(MetaKey.GC_KEEP): True})
|
||||
|
||||
# Cache the object in all case (for display on the notifcation page)
|
||||
Tasks.cache_object(activity.id)
|
||||
|
||||
return None
|
||||
|
Reference in New Issue
Block a user