From 1ab8920df822d3312104da02a02db82a8e42a62b Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 1 Sep 2019 11:32:12 +0200 Subject: [PATCH] More following/follows back stuff --- app.py | 6 ++-- core/migrations.py | 71 ++++++++++++++++++++++++++++++---------- templates/followers.html | 8 ++++- templates/following.html | 7 +++- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index bf2f838..e3e17a3 100644 --- a/app.py +++ b/app.py @@ -818,9 +818,7 @@ def followers(): ) raw_followers, older_than, newer_than = paginated_query(DB.activities, q) - followers = [ - doc["meta"]["actor"] for doc in raw_followers if "actor" in doc.get("meta", {}) - ] + followers = [doc["meta"] for doc in raw_followers if "actor" in doc.get("meta", {})] return htmlify( render_template( "followers.html", @@ -857,7 +855,7 @@ def following(): following, older_than, newer_than = paginated_query(DB.activities, q) following = [ - (doc["remote_id"], doc["meta"]["object"]) + (doc["remote_id"], doc["meta"]) for doc in following if "remote_id" in doc and "object" in doc.get("meta", {}) ] diff --git a/core/migrations.py b/core/migrations.py index 49cf534..8d7bcd4 100644 --- a/core/migrations.py +++ b/core/migrations.py @@ -14,9 +14,11 @@ from core.meta import FollowStatus from core.meta import MetaKey from core.meta import _meta from core.meta import by_actor_id +from core.meta import by_object_id from core.meta import by_remote_id from core.meta import by_type from core.meta import in_inbox +from core.meta import in_outbox from core.meta import not_undo from core.meta import upsert from utils.migrations import Migration @@ -232,24 +234,6 @@ class _20190830_FollowFollowBackMigration(Migration): except Exception: logger.exception(f"failed to process activity {data!r}") - for data in find_activities({**by_type(ap.ActivityType.FOLLOW), **in_inbox()}): - try: - accept_query = { - **in_inbox(), - **by_type(ap.ActivityType.ACCEPT), - **by_actor_id(data["meta"]["actor_id"]), - **not_undo(), - } - raw_accept = DB.activities.find_one(accept_query) - if raw_accept: - DB.activities.update_many( - accept_query, - {"$set": {_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): True}}, - ) - - except Exception: - logger.exception(f"failed to process activity {data!r}") - DB.activities.update_many( { **by_type(ap.ActivityType.FOLLOW), @@ -258,3 +242,54 @@ class _20190830_FollowFollowBackMigration(Migration): }, {"$set": {"meta.follow_status": "waiting"}}, ) + + +class _20190901_FollowFollowBackMigrationFix(Migration): + """Add the new meta flags for tracking accepted/rejected status and following/follows back info.""" + + def migrate(self) -> None: + for data in find_activities({**by_type(ap.ActivityType.ACCEPT), **in_inbox()}): + try: + update_one_activity( + { + **by_type(ap.ActivityType.FOLLOW), + **by_remote_id(data["meta"]["object_id"]), + }, + upsert({MetaKey.FOLLOW_STATUS: FollowStatus.ACCEPTED.value}), + ) + # Check if we are following this actor + follow_query = { + **in_inbox(), + **by_type(ap.ActivityType.FOLLOW), + **by_object_id(data["meta"]["actor_id"]), + **not_undo(), + } + raw_follow = DB.activities.find_one(follow_query) + if raw_follow: + DB.activities.update_many( + follow_query, + {"$set": {_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): True}}, + ) + + except Exception: + logger.exception(f"failed to process activity {data!r}") + + for data in find_activities({**by_type(ap.ActivityType.FOLLOW), **in_outbox()}): + try: + print(data) + follow_query = { + **in_inbox(), + **by_type(ap.ActivityType.FOLLOW), + **by_actor_id(data["meta"]["object_id"]), + **not_undo(), + } + raw_accept = DB.activities.find_one(follow_query) + print(raw_accept) + if raw_accept: + DB.activities.update_many( + by_remote_id(data["remote_id"]), + {"$set": {_meta(MetaKey.NOTIFICATION_FOLLOWS_BACK): True}}, + ) + + except Exception: + logger.exception(f"failed to process activity {data!r}") diff --git a/templates/followers.html b/templates/followers.html index f1bba01..c01f880 100644 --- a/templates/followers.html +++ b/templates/followers.html @@ -8,10 +8,16 @@ {% include "header.html" %}
- {% for follower in followers_data %} + {% for meta in followers_data %} + {% set follower = meta.actor %} {% if session.logged_in %}
profile + +{% if meta.notification_follows_back %} +following +{% endif %} +
{% endif %}
diff --git a/templates/following.html b/templates/following.html index f084d32..bdb5361 100644 --- a/templates/following.html +++ b/templates/following.html @@ -8,7 +8,8 @@ {% include "header.html" %}
- {% for (follow_id, follow) in following_data %} + {% for (follow_id, meta) in following_data %} + {% set follow = meta.object %} {% if session.logged_in %}
profile @@ -18,6 +19,9 @@ +{% if meta.notification_follows_back %} +follows you back +{% endif %} {% if lists %}
@@ -48,6 +52,7 @@ {% endif %} {% endfor %} +