Cleanup, more typing for queries

This commit is contained in:
Thomas Sileo 2019-08-01 22:24:18 +02:00
parent d4590996e0
commit 5b9a0cb286
5 changed files with 43 additions and 28 deletions

39
app.py
View file

@ -24,7 +24,6 @@ from little_boxes.activitypub import clean_activity
from little_boxes.activitypub import get_backend from little_boxes.activitypub import get_backend
from little_boxes.errors import ActivityGoneError from little_boxes.errors import ActivityGoneError
from little_boxes.errors import Error from little_boxes.errors import Error
from little_boxes.httpsig import HTTPSigAuth
from little_boxes.httpsig import verify_request from little_boxes.httpsig import verify_request
from little_boxes.webfinger import get_actor_url from little_boxes.webfinger import get_actor_url
from little_boxes.webfinger import get_remote_follow_template from little_boxes.webfinger import get_remote_follow_template
@ -38,18 +37,19 @@ import config
from core.meta import Box from core.meta import Box
from core.activitypub import embed_collection from core.activitypub import embed_collection
from blueprints.api import _api_required from blueprints.api import _api_required
from config import ADMIN_API_KEY
from config import BLACKLIST
from config import DB from config import DB
from config import HEADERS from config import HEADERS
from config import ID from config import ID
from config import KEY
from config import ME from config import ME
from config import MEDIA_CACHE from config import MEDIA_CACHE
from config import VERSION from config import VERSION
from core import activitypub from core import activitypub
from core.db import find_one_activity
from core.meta import MetaKey from core.meta import MetaKey
from core.meta import _meta from core.meta import _meta
from core.meta import is_public
from core.meta import by_remote_id
from core.meta import in_outbox
from core.shared import MY_PERSON from core.shared import MY_PERSON
from core.shared import _add_answers_to_question from core.shared import _add_answers_to_question
from core.shared import _build_thread from core.shared import _build_thread
@ -61,11 +61,11 @@ from core.shared import noindex
from core.shared import paginated_query from core.shared import paginated_query
from core.shared import post_to_outbox from core.shared import post_to_outbox
from core.tasks import Tasks from core.tasks import Tasks
from blueprints.tasks import TaskError
from utils import now from utils import now
from utils.key import get_secret_key from utils.key import get_secret_key
from utils.template_filters import filters from utils.template_filters import filters
# p = PousseTaches("http://localhost:7991", "http://localhost:5000")
app = Flask(__name__) app = Flask(__name__)
app.secret_key = get_secret_key("flask") app.secret_key = get_secret_key("flask")
@ -91,12 +91,10 @@ else:
root_logger.handlers = gunicorn_logger.handlers root_logger.handlers = gunicorn_logger.handlers
root_logger.setLevel(gunicorn_logger.level) root_logger.setLevel(gunicorn_logger.level)
SIG_AUTH = HTTPSigAuth(KEY)
def is_blacklisted(url: str) -> bool: def is_blacklisted(url: str) -> bool:
try: try:
return urlparse(url).netloc in BLACKLIST return urlparse(url).netloc in config.BLACKLIST
except Exception: except Exception:
logger.exception(f"failed to blacklist for {url}") logger.exception(f"failed to blacklist for {url}")
return False return False
@ -209,13 +207,6 @@ def handle_activitypub_error(error):
return response return response
class TaskError(Exception):
"""Raised to log the error for poussetaches."""
def __init__(self):
self.message = traceback.format_exc()
@app.errorhandler(TaskError) @app.errorhandler(TaskError)
def handle_task_error(error): def handle_task_error(error):
logger.error( logger.error(
@ -575,13 +566,11 @@ def outbox_detail(item_id):
@app.route("/outbox/<item_id>/activity") @app.route("/outbox/<item_id>/activity")
def outbox_activity(item_id): def outbox_activity(item_id):
data = DB.activities.find_one( data = find_one_activity({
{ **in_outbox(),
"box": Box.OUTBOX.value, **by_remote_id(back.activity_url(item_id)),
"remote_id": back.activity_url(item_id), **is_public(),
"meta.public": True, })
}
)
if not data: if not data:
abort(404) abort(404)
@ -711,12 +700,6 @@ def outbox_activity_shares(item_id):
) )
@app.route("/api/key")
@login_required
def api_user_key():
return flask_jsonify(api_key=ADMIN_API_KEY)
@app.route("/inbox", methods=["GET", "POST"]) # noqa: C901 @app.route("/inbox", methods=["GET", "POST"]) # noqa: C901
def inbox(): def inbox():
# GET /inbox # GET /inbox

View file

@ -31,6 +31,7 @@ from config import DEBUG_MODE
from config import ID from config import ID
from config import JWT from config import JWT
from config import MEDIA_CACHE from config import MEDIA_CACHE
from config import ADMIN_API_KEY
from config import _drop_db from config import _drop_db
from core import activitypub from core import activitypub
from core.meta import MetaKey from core.meta import MetaKey
@ -40,6 +41,7 @@ from core.shared import _Response
from core.shared import back from core.shared import back
from core.shared import csrf from core.shared import csrf
from core.shared import post_to_outbox from core.shared import post_to_outbox
from core.shared import login_required
from core.tasks import Tasks from core.tasks import Tasks
from utils import now from utils import now
@ -126,6 +128,12 @@ def _user_api_response(**kwargs) -> _Response:
return resp return resp
@blueprint.route("/api/key")
@login_required
def api_user_key() -> _Response:
return flask.jsonify(api_key=ADMIN_API_KEY)
@blueprint.route("/note/delete", methods=["POST"]) @blueprint.route("/note/delete", methods=["POST"])
@api_required @api_required
def api_delete() -> _Response: def api_delete() -> _Response:

0
core/__init__.py Normal file
View file

19
core/db.py Normal file
View file

@ -0,0 +1,19 @@
from enum import Enum
from enum import unique
from typing import Dict
from typing import Any
from typing import Optional
from config import DB
_Q = Dict[str, Any]
_Doc = Optional[Dict[str, Any]]
@unique
class CollectionName(Enum):
ACTIVITIES = "activities"
def find_one_activity(q: _Q) -> _Doc:
return DB[CollectionName.ACTIVITIES.value].find_one(q)

View file

@ -28,6 +28,7 @@ class MetaKey(Enum):
GC_KEEP = "gc_keep" GC_KEEP = "gc_keep"
OBJECT = "object" OBJECT = "object"
OBJECT_ACTOR = "object_actor" OBJECT_ACTOR = "object_actor"
PUBLIC = "public"
def _meta(mk: MetaKey) -> str: def _meta(mk: MetaKey) -> str:
@ -56,3 +57,7 @@ def not_undo() -> _SubQuery:
def by_actor(actor: ap.BaseActivity) -> _SubQuery: def by_actor(actor: ap.BaseActivity) -> _SubQuery:
return {_meta(MetaKey.ACTOR_ID): actor.id} return {_meta(MetaKey.ACTOR_ID): actor.id}
def is_public() -> _SubQuery:
return {_meta(MetaKey.PUBLIC): True}