2019-07-29 17:36:22 +00:00
|
|
|
from enum import Enum
|
2019-07-29 20:53:41 +00:00
|
|
|
from enum import unique
|
2019-07-29 17:36:22 +00:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
from little_boxes import activitypub as ap
|
|
|
|
|
|
|
|
_SubQuery = Dict[str, Any]
|
|
|
|
|
|
|
|
|
2019-07-29 20:46:53 +00:00
|
|
|
@unique
|
2019-07-29 17:36:22 +00:00
|
|
|
class Box(Enum):
|
|
|
|
INBOX = "inbox"
|
|
|
|
OUTBOX = "outbox"
|
|
|
|
REPLIES = "replies"
|
|
|
|
|
|
|
|
|
2019-07-29 20:46:53 +00:00
|
|
|
@unique
|
2019-07-29 17:36:22 +00:00
|
|
|
class MetaKey(Enum):
|
|
|
|
NOTIFICATION = "notification"
|
|
|
|
NOTIFICATION_UNREAD = "notification_unread"
|
|
|
|
NOTIFICATION_FOLLOWS_BACK = "notification_follows_back"
|
2019-07-30 20:12:20 +00:00
|
|
|
POLL_ANSWER = "poll_answer"
|
|
|
|
STREAM = "stream"
|
2019-07-29 17:36:22 +00:00
|
|
|
ACTOR_ID = "actor_id"
|
|
|
|
UNDO = "undo"
|
|
|
|
PUBLISHED = "published"
|
2019-07-29 20:20:13 +00:00
|
|
|
GC_KEEP = "gc_keep"
|
|
|
|
OBJECT = "object"
|
2019-08-04 18:08:47 +00:00
|
|
|
OBJECT_ID = "object_id"
|
2019-07-29 20:20:13 +00:00
|
|
|
OBJECT_ACTOR = "object_actor"
|
2019-08-01 20:24:18 +00:00
|
|
|
PUBLIC = "public"
|
2019-07-29 17:36:22 +00:00
|
|
|
|
2019-08-04 18:08:47 +00:00
|
|
|
DELETED = "deleted"
|
|
|
|
BOOSTED = "boosted"
|
|
|
|
LIKED = "liked"
|
|
|
|
|
|
|
|
COUNT_LIKE = "count_like"
|
|
|
|
COUNT_BOOST = "count_boost"
|
|
|
|
|
2019-07-29 17:36:22 +00:00
|
|
|
|
|
|
|
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}
|
2019-08-01 20:24:18 +00:00
|
|
|
|
|
|
|
|
2019-08-04 18:08:47 +00:00
|
|
|
def by_object_id(object_id: str) -> _SubQuery:
|
|
|
|
return {_meta(MetaKey.OBJECT_ID): object_id}
|
|
|
|
|
|
|
|
|
2019-08-01 20:24:18 +00:00
|
|
|
def is_public() -> _SubQuery:
|
|
|
|
return {_meta(MetaKey.PUBLIC): True}
|
2019-08-04 18:08:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def inc(mk: MetaKey, val: int) -> _SubQuery:
|
|
|
|
return {"$inc": {_meta(mk): val}}
|
|
|
|
|
|
|
|
|
|
|
|
def upsert(data: Dict[MetaKey, Any]) -> _SubQuery:
|
|
|
|
sq: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
for mk, val in data.items():
|
|
|
|
sq[_meta(mk)] = val
|
|
|
|
|
|
|
|
return {"$set": sq}
|