Improve notifications
- Keep track of unread count - "follow back" action
This commit is contained in:
@@ -3,6 +3,7 @@ from datetime import datetime
|
||||
from datetime import timezone
|
||||
|
||||
from dateutil import parser
|
||||
from little_boxes import activitypub as ap
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,3 +26,7 @@ def parse_datetime(s: str) -> datetime:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
return dt
|
||||
|
||||
|
||||
def now() -> str:
|
||||
ap.format_datetime(datetime.now(timezone.utc))
|
||||
|
50
utils/meta.py
Normal file
50
utils/meta.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
from little_boxes import activitypub as ap
|
||||
|
||||
_SubQuery = Dict[str, Any]
|
||||
|
||||
|
||||
class Box(Enum):
|
||||
INBOX = "inbox"
|
||||
OUTBOX = "outbox"
|
||||
REPLIES = "replies"
|
||||
|
||||
|
||||
class MetaKey(Enum):
|
||||
NOTIFICATION = "notification"
|
||||
NOTIFICATION_UNREAD = "notification_unread"
|
||||
NOTIFICATION_FOLLOWS_BACK = "notification_follows_back"
|
||||
ACTOR_ID = "actor_id"
|
||||
UNDO = "undo"
|
||||
PUBLISHED = "published"
|
||||
|
||||
|
||||
def _meta(mk: MetaKey) -> str:
|
||||
return f"meta.{mk.value}"
|
||||
|
||||
|
||||
def by_remote_id(remote_id: str) -> _SubQuery:
|
||||
return {"remote_id": remote_id}
|
||||
|
||||
|
||||
def in_inbox() -> _SubQuery:
|
||||
return {"box": Box.INBOX.value}
|
||||
|
||||
|
||||
def in_outbox() -> _SubQuery:
|
||||
return {"box": Box.OUTBOX.value}
|
||||
|
||||
|
||||
def by_type(type_: ap.ActivityType) -> _SubQuery:
|
||||
return {"type": type_.value}
|
||||
|
||||
|
||||
def not_undo() -> _SubQuery:
|
||||
return {_meta(MetaKey.UNDO): False}
|
||||
|
||||
|
||||
def by_actor(actor: ap.BaseActivity) -> _SubQuery:
|
||||
return {_meta(MetaKey.ACTOR_ID): actor.id}
|
84
utils/notifications.py
Normal file
84
utils/notifications.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import logging
|
||||
from functools import singledispatch
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
from little_boxes import activitypub as ap
|
||||
|
||||
from config import DB
|
||||
from config import MetaKey
|
||||
from config import _meta
|
||||
from utils.meta import by_actor
|
||||
from utils.meta import by_type
|
||||
from utils.meta import in_inbox
|
||||
from utils.meta import not_undo
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
_NewMeta = Dict[str, Any]
|
||||
|
||||
|
||||
@singledispatch
|
||||
def set_inbox_flags(activity: ap.BaseActivity, new_meta: _NewMeta) -> None:
|
||||
return None
|
||||
|
||||
|
||||
@set_inbox_flags.register
|
||||
def _accept_set_inbox_flags(activity: ap.Accept, new_meta: _NewMeta) -> None:
|
||||
"""Handle notifications for "accepted" following requests."""
|
||||
_logger.info(f"set_inbox_flags activity={activity!r}")
|
||||
# Check if this actor already follow us back
|
||||
follows_back = False
|
||||
follow_query = {
|
||||
**in_inbox(),
|
||||
**by_type(ap.ActivityType.FOLLOW),
|
||||
**by_actor(activity.get_actor()),
|
||||
**not_undo(),
|
||||
}
|
||||
raw_follow = DB.activities.find_one(follow_query)
|
||||
if raw_follow:
|
||||
follows_back = True
|
||||
|
||||
DB.activities.update_many(
|
||||
follow_query, {"$set": {_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): True}}
|
||||
)
|
||||
|
||||
# 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,
|
||||
}
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@set_inbox_flags.register
|
||||
def _follow_set_inbox_flags(activity: ap.Follow, new_meta: _NewMeta) -> None:
|
||||
"""Handle notification for new followers."""
|
||||
# Check if we're already following this actor
|
||||
follows_back = False
|
||||
accept_query = {
|
||||
**in_inbox(),
|
||||
**by_type(ap.ActivityType.ACCEPT),
|
||||
**by_actor(activity.get_actor()),
|
||||
**not_undo(),
|
||||
}
|
||||
raw_accept = DB.activities.find_one(accept_query)
|
||||
if raw_accept:
|
||||
follows_back = True
|
||||
|
||||
DB.activities.update_many(
|
||||
accept_query, {"$set": {_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): True}}
|
||||
)
|
||||
|
||||
# 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,
|
||||
}
|
||||
)
|
||||
return None
|
Reference in New Issue
Block a user