More following/follows back stuff

This commit is contained in:
Thomas Sileo 2019-09-01 11:32:12 +02:00
parent efc8a41624
commit 1ab8920df8
4 changed files with 68 additions and 24 deletions

6
app.py
View file

@ -818,9 +818,7 @@ def followers():
) )
raw_followers, older_than, newer_than = paginated_query(DB.activities, q) raw_followers, older_than, newer_than = paginated_query(DB.activities, q)
followers = [ followers = [doc["meta"] for doc in raw_followers if "actor" in doc.get("meta", {})]
doc["meta"]["actor"] for doc in raw_followers if "actor" in doc.get("meta", {})
]
return htmlify( return htmlify(
render_template( render_template(
"followers.html", "followers.html",
@ -857,7 +855,7 @@ def following():
following, older_than, newer_than = paginated_query(DB.activities, q) following, older_than, newer_than = paginated_query(DB.activities, q)
following = [ following = [
(doc["remote_id"], doc["meta"]["object"]) (doc["remote_id"], doc["meta"])
for doc in following for doc in following
if "remote_id" in doc and "object" in doc.get("meta", {}) if "remote_id" in doc and "object" in doc.get("meta", {})
] ]

View file

@ -14,9 +14,11 @@ from core.meta import FollowStatus
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_actor_id 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_remote_id
from core.meta import by_type from core.meta import by_type
from core.meta import in_inbox from core.meta import in_inbox
from core.meta import in_outbox
from core.meta import not_undo from core.meta import not_undo
from core.meta import upsert from core.meta import upsert
from utils.migrations import Migration from utils.migrations import Migration
@ -232,24 +234,6 @@ class _20190830_FollowFollowBackMigration(Migration):
except Exception: except Exception:
logger.exception(f"failed to process activity {data!r}") 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( DB.activities.update_many(
{ {
**by_type(ap.ActivityType.FOLLOW), **by_type(ap.ActivityType.FOLLOW),
@ -258,3 +242,54 @@ class _20190830_FollowFollowBackMigration(Migration):
}, },
{"$set": {"meta.follow_status": "waiting"}}, {"$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}")

View file

@ -8,10 +8,16 @@
{% include "header.html" %} {% include "header.html" %}
<div id="followers"> <div id="followers">
{% for follower in followers_data %} {% for meta in followers_data %}
{% set follower = meta.actor %}
{% if session.logged_in %} {% if session.logged_in %}
<div style="margin-left:90px;padding-bottom:5px;margin-bottom:15px;display:inline-block;"> <div style="margin-left:90px;padding-bottom:5px;margin-bottom:15px;display:inline-block;">
<a class="bar-item" href="/admin/profile?actor_id={{follower.id}}">profile</a> <a class="bar-item" href="/admin/profile?actor_id={{follower.id}}">profile</a>
{% if meta.notification_follows_back %}
<span class="bar-item-no-hover">following</span>
{% endif %}
</div> </div>
{% endif %} {% endif %}
<div style="height: 100px;"> <div style="height: 100px;">

View file

@ -8,7 +8,8 @@
{% include "header.html" %} {% include "header.html" %}
<div id="following"> <div id="following">
{% for (follow_id, follow) in following_data %} {% for (follow_id, meta) in following_data %}
{% set follow = meta.object %}
{% if session.logged_in %} {% if session.logged_in %}
<div style="margin-left:90px;padding-bottom:5px;margin-bottom:15px;display:inline-block;"> <div style="margin-left:90px;padding-bottom:5px;margin-bottom:15px;display:inline-block;">
<a class="bar-item" href="/admin/profile?actor_id={{follow.id}}">profile</a> <a class="bar-item" href="/admin/profile?actor_id={{follow.id}}">profile</a>
@ -18,6 +19,9 @@
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<button type="submit" class="bar-item">unfollow</button> <button type="submit" class="bar-item">unfollow</button>
</form> </form>
{% if meta.notification_follows_back %}
<span class="bar-item-no-hover">follows you back</span>
{% endif %}
{% if lists %} {% if lists %}
<form action="/api/add_to_list" class="action-form" method="post"> <form action="/api/add_to_list" class="action-form" method="post">
@ -48,6 +52,7 @@
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>