Cleanup queries

This commit is contained in:
Thomas Sileo 2019-09-02 23:44:38 +02:00
parent 151ced0b41
commit bcec7146fd
2 changed files with 48 additions and 45 deletions

89
app.py
View file

@ -56,12 +56,15 @@ from core.meta import Box
from core.meta import MetaKey from core.meta import MetaKey
from core.meta import _meta from core.meta import _meta
from core.meta import by_hashtag from core.meta import by_hashtag
from core.meta import by_object_id
from core.meta import by_remote_id from core.meta import by_remote_id
from core.meta import by_type from core.meta import by_type
from core.meta import by_visibility from core.meta import by_visibility
from core.meta import in_inbox
from core.meta import in_outbox from core.meta import in_outbox
from core.meta import is_public from core.meta import is_public
from core.meta import not_deleted from core.meta import not_deleted
from core.meta import not_poll_answer
from core.meta import not_undo from core.meta import not_undo
from core.meta import pinned from core.meta import pinned
from core.shared import _build_thread from core.shared import _build_thread
@ -121,27 +124,29 @@ def inject_config():
notes_count = DB.activities.count(q) notes_count = DB.activities.count(q)
# FIXME(tsileo): rename to all_count, and remove poll answers from it # FIXME(tsileo): rename to all_count, and remove poll answers from it
all_q = { all_q = {
"box": Box.OUTBOX.value, **in_outbox(),
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]}, **by_type([ActivityType.CREATE, ActivityType.ANNOUNCE]),
"meta.undo": False, **not_deleted(),
"meta.deleted": False, **not_undo(),
"meta.poll_answer": False, **not_poll_answer(),
} }
liked_q = { liked_q = {
**in_outbox(), **in_outbox(),
"meta.deleted": False, **by_type(ActivityType.LIKE),
"meta.undo": False, **not_undo(),
"type": ActivityType.LIKE.value, **not_deleted(),
} }
followers_q = { followers_q = {
"box": Box.INBOX.value, **in_inbox(),
"type": ActivityType.FOLLOW.value, **by_type(ActivityType.FOLLOW),
"meta.undo": False, **not_undo(),
**not_deleted(),
} }
following_q = { following_q = {
"box": Box.OUTBOX.value, **in_outbox(),
"type": ActivityType.FOLLOW.value, **by_type(ActivityType.FOLLOW),
"meta.undo": False, **not_undo(),
**not_deleted(),
} }
unread_notifications_q = {_meta(MetaKey.NOTIFICATION_UNREAD): True} unread_notifications_q = {_meta(MetaKey.NOTIFICATION_UNREAD): True}
@ -395,11 +400,11 @@ def index():
@login_required @login_required
def all(): def all():
q = { q = {
"box": Box.OUTBOX.value, **in_outbox(),
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]}, **by_type([ActivityType.CREATE, ActivityType.ANNOUNCE]),
"meta.deleted": False, **not_deleted(),
"meta.undo": False, **not_undo(),
"meta.poll_answer": False, **not_poll_answer(),
} }
outbox_data, older_than, newer_than = paginated_query(DB.activities, q) outbox_data, older_than, newer_than = paginated_query(DB.activities, q)
@ -419,7 +424,7 @@ def note_by_id(note_id):
return redirect(url_for("outbox_activity", item_id=note_id)) return redirect(url_for("outbox_activity", item_id=note_id))
data = DB.activities.find_one( data = DB.activities.find_one(
{"box": Box.OUTBOX.value, "remote_id": activity_url(note_id)} {**in_outbox(), **by_remote_id(activity_url(note_id))}
) )
if not data: if not data:
abort(404) abort(404)
@ -432,14 +437,10 @@ def note_by_id(note_id):
raw_likes = list( raw_likes = list(
DB.activities.find( DB.activities.find(
{ {
"meta.undo": False, **not_undo(),
"meta.deleted": False, **not_deleted(),
"type": ActivityType.LIKE.value, **by_type(ActivityType.LIKE),
"$or": [ **by_object_id(data["activity"]["object"]["id"]),
# FIXME(tsileo): remove all the useless $or
{"activity.object.id": data["activity"]["object"]["id"]},
{"activity.object": data["activity"]["object"]["id"]},
],
} }
) )
) )
@ -454,13 +455,10 @@ def note_by_id(note_id):
raw_shares = list( raw_shares = list(
DB.activities.find( DB.activities.find(
{ {
"meta.undo": False, **not_undo(),
"meta.deleted": False, **not_deleted(),
"type": ActivityType.ANNOUNCE.value, **by_type(ActivityType.ANNOUNCE),
"$or": [ **by_object_id(data["activity"]["object"]["id"]),
{"activity.object.id": data["activity"]["object"]["id"]},
{"activity.object": data["activity"]["object"]["id"]},
],
} }
) )
) )
@ -531,9 +529,10 @@ def ap_emoji(name):
def outbox_detail(item_id): def outbox_detail(item_id):
doc = DB.activities.find_one( doc = DB.activities.find_one(
{ {
"box": Box.OUTBOX.value, **in_outbox(),
"remote_id": activity_url(item_id), **by_remote_id(activity_url(item_id)),
"meta.public": True, **not_deleted(),
**is_public(),
} }
) )
if not doc: if not doc:
@ -571,10 +570,10 @@ def outbox_activity_replies(item_id):
_log_sig() _log_sig()
data = DB.activities.find_one( data = DB.activities.find_one(
{ {
"box": Box.OUTBOX.value, **in_outbox(),
"remote_id": activity_url(item_id), **by_remote_id(activity_url(item_id)),
"meta.deleted": False, **not_deleted(),
"meta.public": True, **is_public(),
} }
) )
if not data: if not data:
@ -584,9 +583,9 @@ def outbox_activity_replies(item_id):
abort(404) abort(404)
q = { q = {
"meta.deleted": False, **is_public(),
"meta.public": True, **not_deleted(),
"type": ActivityType.CREATE.value, **by_type(ActivityType.CREATE),
"activity.object.inReplyTo": obj.get_object().id, "activity.object.inReplyTo": obj.get_object().id,
} }

View file

@ -101,6 +101,10 @@ def follow_request_accepted() -> _SubQuery:
return flag(MetaKey.FOLLOW_STATUS, FollowStatus.ACCEPTED.value) return flag(MetaKey.FOLLOW_STATUS, FollowStatus.ACCEPTED.value)
def not_poll_answer() -> _SubQuery:
return flag(MetaKey.POLL_ANSWER, False)
def not_in_reply_to() -> _SubQuery: def not_in_reply_to() -> _SubQuery:
return {"activity.object.inReplyTo": None} return {"activity.object.inReplyTo": None}