2019-08-15 10:30:40 -05:00
|
|
|
from datetime import datetime
|
2019-07-29 12:36:22 -05:00
|
|
|
from enum import Enum
|
2019-07-29 15:53:41 -05:00
|
|
|
from enum import unique
|
2019-07-29 12:36:22 -05:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2019-08-17 02:56:46 -05:00
|
|
|
from typing import List
|
|
|
|
from typing import Union
|
2019-07-29 12:36:22 -05:00
|
|
|
|
|
|
|
from little_boxes import activitypub as ap
|
|
|
|
|
|
|
|
_SubQuery = Dict[str, Any]
|
|
|
|
|
|
|
|
|
2019-07-29 15:46:53 -05:00
|
|
|
@unique
|
2019-07-29 12:36:22 -05:00
|
|
|
class Box(Enum):
|
|
|
|
INBOX = "inbox"
|
|
|
|
OUTBOX = "outbox"
|
|
|
|
REPLIES = "replies"
|
|
|
|
|
|
|
|
|
2019-09-01 03:26:25 -05:00
|
|
|
@unique
|
|
|
|
class FollowStatus(Enum):
|
|
|
|
WAITING = "waiting"
|
|
|
|
ACCEPTED = "accepted"
|
|
|
|
REJECTED = "rejected"
|
|
|
|
|
|
|
|
|
2019-07-29 15:46:53 -05:00
|
|
|
@unique
|
2019-07-29 12:36:22 -05:00
|
|
|
class MetaKey(Enum):
|
|
|
|
NOTIFICATION = "notification"
|
|
|
|
NOTIFICATION_UNREAD = "notification_unread"
|
|
|
|
NOTIFICATION_FOLLOWS_BACK = "notification_follows_back"
|
2019-07-30 15:12:20 -05:00
|
|
|
POLL_ANSWER = "poll_answer"
|
2019-08-25 04:09:34 -05:00
|
|
|
POLL_ANSWER_TO = "poll_answer_to"
|
2019-07-30 15:12:20 -05:00
|
|
|
STREAM = "stream"
|
2019-07-29 12:36:22 -05:00
|
|
|
ACTOR_ID = "actor_id"
|
2019-08-10 04:47:50 -05:00
|
|
|
ACTOR = "actor"
|
|
|
|
ACTOR_HASH = "actor_hash"
|
2019-07-29 12:36:22 -05:00
|
|
|
UNDO = "undo"
|
|
|
|
PUBLISHED = "published"
|
2019-07-29 15:20:13 -05:00
|
|
|
GC_KEEP = "gc_keep"
|
|
|
|
OBJECT = "object"
|
2019-08-04 13:08:47 -05:00
|
|
|
OBJECT_ID = "object_id"
|
2019-07-29 15:20:13 -05:00
|
|
|
OBJECT_ACTOR = "object_actor"
|
2019-08-10 04:47:50 -05:00
|
|
|
OBJECT_ACTOR_ID = "object_actor_id"
|
|
|
|
OBJECT_ACTOR_HASH = "object_actor_hash"
|
2019-08-01 15:24:18 -05:00
|
|
|
PUBLIC = "public"
|
2019-09-01 03:26:25 -05:00
|
|
|
|
2019-09-01 14:38:38 -05:00
|
|
|
PINNED = "pinned"
|
2019-09-01 13:58:51 -05:00
|
|
|
HASHTAGS = "hashtags"
|
|
|
|
MENTIONS = "mentions"
|
|
|
|
|
2019-09-01 03:26:25 -05:00
|
|
|
FOLLOW_STATUS = "follow_status"
|
|
|
|
|
2019-08-16 14:16:25 -05:00
|
|
|
THREAD_ROOT_PARENT = "thread_root_parent"
|
2019-07-29 12:36:22 -05:00
|
|
|
|
2019-09-03 15:24:30 -05:00
|
|
|
IN_REPLY_TO = "in_reply_to"
|
2019-08-25 05:48:04 -05:00
|
|
|
IN_REPLY_TO_SELF = "in_reply_to_self"
|
|
|
|
IN_REPLY_TO_ACTOR = "in_reply_to_actor"
|
|
|
|
|
2019-08-21 17:45:49 -05:00
|
|
|
SERVER = "server"
|
|
|
|
VISIBILITY = "visibility"
|
|
|
|
OBJECT_VISIBILITY = "object_visibility"
|
|
|
|
|
2019-08-04 13:08:47 -05:00
|
|
|
DELETED = "deleted"
|
|
|
|
BOOSTED = "boosted"
|
|
|
|
LIKED = "liked"
|
|
|
|
|
|
|
|
COUNT_LIKE = "count_like"
|
|
|
|
COUNT_BOOST = "count_boost"
|
2019-08-16 07:42:15 -05:00
|
|
|
COUNT_REPLY = "count_reply"
|
2019-08-04 13:08:47 -05:00
|
|
|
|
2019-07-29 12:36:22 -05:00
|
|
|
|
|
|
|
def _meta(mk: MetaKey) -> str:
|
|
|
|
return f"meta.{mk.value}"
|
|
|
|
|
|
|
|
|
2019-08-15 10:30:40 -05:00
|
|
|
def flag(mk: MetaKey, val: Any) -> _SubQuery:
|
|
|
|
return {_meta(mk): val}
|
|
|
|
|
|
|
|
|
2019-07-29 12:36:22 -05:00
|
|
|
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}
|
|
|
|
|
|
|
|
|
2019-08-17 02:56:46 -05:00
|
|
|
def by_type(type_: Union[ap.ActivityType, List[ap.ActivityType]]) -> _SubQuery:
|
|
|
|
if isinstance(type_, list):
|
|
|
|
return {"type": {"$in": [t.value for t in type_]}}
|
|
|
|
|
2019-07-29 12:36:22 -05:00
|
|
|
return {"type": type_.value}
|
|
|
|
|
|
|
|
|
2019-09-01 03:26:25 -05:00
|
|
|
def follow_request_accepted() -> _SubQuery:
|
|
|
|
return flag(MetaKey.FOLLOW_STATUS, FollowStatus.ACCEPTED.value)
|
|
|
|
|
|
|
|
|
2019-09-02 16:44:38 -05:00
|
|
|
def not_poll_answer() -> _SubQuery:
|
|
|
|
return flag(MetaKey.POLL_ANSWER, False)
|
|
|
|
|
|
|
|
|
2019-09-01 14:38:38 -05:00
|
|
|
def not_in_reply_to() -> _SubQuery:
|
|
|
|
return {"activity.object.inReplyTo": None}
|
|
|
|
|
|
|
|
|
2019-07-29 12:36:22 -05:00
|
|
|
def not_undo() -> _SubQuery:
|
2019-08-15 10:30:40 -05:00
|
|
|
return flag(MetaKey.UNDO, False)
|
2019-07-29 12:36:22 -05:00
|
|
|
|
|
|
|
|
2019-08-16 15:27:59 -05:00
|
|
|
def not_deleted() -> _SubQuery:
|
|
|
|
return flag(MetaKey.DELETED, False)
|
|
|
|
|
|
|
|
|
2019-09-01 14:38:38 -05:00
|
|
|
def pinned() -> _SubQuery:
|
|
|
|
return flag(MetaKey.PINNED, True)
|
|
|
|
|
|
|
|
|
2019-07-29 12:36:22 -05:00
|
|
|
def by_actor(actor: ap.BaseActivity) -> _SubQuery:
|
2019-08-15 10:30:40 -05:00
|
|
|
return flag(MetaKey.ACTOR_ID, actor.id)
|
2019-08-01 15:24:18 -05:00
|
|
|
|
|
|
|
|
2019-09-01 03:26:25 -05:00
|
|
|
def by_actor_id(actor_id: str) -> _SubQuery:
|
|
|
|
return flag(MetaKey.ACTOR_ID, actor_id)
|
|
|
|
|
|
|
|
|
2019-08-04 13:08:47 -05:00
|
|
|
def by_object_id(object_id: str) -> _SubQuery:
|
2019-08-15 10:30:40 -05:00
|
|
|
return flag(MetaKey.OBJECT_ID, object_id)
|
2019-08-04 13:08:47 -05:00
|
|
|
|
|
|
|
|
2019-08-01 15:24:18 -05:00
|
|
|
def is_public() -> _SubQuery:
|
2019-08-15 10:30:40 -05:00
|
|
|
return flag(MetaKey.PUBLIC, True)
|
2019-08-04 13:08:47 -05:00
|
|
|
|
|
|
|
|
2019-09-01 13:58:51 -05:00
|
|
|
def by_visibility(vis: ap.Visibility) -> _SubQuery:
|
|
|
|
return flag(MetaKey.VISIBILITY, vis.name)
|
|
|
|
|
|
|
|
|
2019-10-27 08:53:00 -05:00
|
|
|
def by_object_visibility(vis: ap.Visibility) -> _SubQuery:
|
|
|
|
return flag(MetaKey.OBJECT_VISIBILITY, vis.name)
|
|
|
|
|
|
|
|
|
2019-09-01 13:58:51 -05:00
|
|
|
def by_hashtag(ht: str) -> _SubQuery:
|
|
|
|
return flag(MetaKey.HASHTAGS, ht)
|
|
|
|
|
|
|
|
|
2019-08-04 13:08:47 -05:00
|
|
|
def inc(mk: MetaKey, val: int) -> _SubQuery:
|
2019-08-15 10:30:40 -05:00
|
|
|
return {"$inc": flag(mk, val)}
|
2019-08-04 13:08:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
def upsert(data: Dict[MetaKey, Any]) -> _SubQuery:
|
|
|
|
sq: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
for mk, val in data.items():
|
|
|
|
sq[_meta(mk)] = val
|
|
|
|
|
|
|
|
return {"$set": sq}
|
2019-08-10 04:47:50 -05:00
|
|
|
|
|
|
|
|
2019-08-15 10:30:40 -05:00
|
|
|
def published_after(dt: datetime) -> _SubQuery:
|
2019-08-17 02:56:46 -05:00
|
|
|
return flag(MetaKey.PUBLISHED, {"$gt": ap.format_datetime(dt)})
|