replay failed AP tasks

This commit is contained in:
rimu 2023-12-26 12:36:20 +13:00
parent e3ec351f80
commit 4c58a04f8a
4 changed files with 79 additions and 2 deletions

View file

@ -709,6 +709,36 @@ def upvote_post(post, user):
db.session.add(vote)
def delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id):
if current_app.debug:
delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id)
else:
delete_post_or_comment_task.delay(user_ap_id, community_ap_id, to_be_deleted_ap_id)
@celery.task
def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id):
deletor = find_actor_or_create(user_ap_id)
community = find_actor_or_create(community_ap_id)
to_delete = find_liked_object(to_be_deleted_ap_id)
if deletor and community and to_delete:
if deletor.is_admin() or community.is_moderator(deletor) or to_delete.author.id == deletor.id:
if isinstance(to_delete, Post):
to_delete.delete_dependencies()
to_delete.flush_cache()
db.session.delete(to_delete)
db.session.commit()
elif isinstance(to_delete, PostReply):
if to_delete.has_replies():
to_delete.body = 'Deleted by author' if to_delete.author.id == deletor.id else 'Deleted by moderator'
to_delete.body_html = markdown_to_html(to_delete.body)
else:
to_delete.delete_dependencies()
db.session.delete(to_delete)
db.session.commit()
def lemmy_site_data():
site = g.site
data = {

View file

@ -1,11 +1,12 @@
from datetime import datetime, timedelta
from flask import request, flash, json, url_for
from flask import request, flash, json, url_for, current_app
from flask_login import login_required, current_user
from flask_babel import _
from sqlalchemy import text, desc
from app import db
from app.activitypub.routes import process_inbox_request
from app.admin.forms import FederationForm, SiteMiscForm, SiteProfileForm
from app.models import AllowedInstances, BannedInstances, ActivityPubLog, utcnow, Site
from app.utils import render_template, permission_required, set_setting, get_setting
@ -145,4 +146,13 @@ def admin_activities():
def activity_json(activity_id):
activity = ActivityPubLog.query.get_or_404(activity_id)
return render_template('admin/activity_json.html', title=_('Activity JSON'),
activity_json_data=json.loads(activity.activity_json))
activity_json_data=json.loads(activity.activity_json), activity=activity, current_app=current_app)
@bp.route('/activity_json/<int:activity_id>/replay')
@login_required
@permission_required('change instance settings')
def activity_replay(activity_id):
activity = ActivityPubLog.query.get_or_404(activity_id)
process_inbox_request(json.loads(activity.activity_json), activity.id)
return 'Ok'

View file

@ -11,6 +11,9 @@
<div class="row">
<div class="col">
<code><pre>{{ activity_json_data | tojson(indent=2) }}</pre></code>
{% if current_app.debug %}
<p><a class="btn btn-warning" href="{{ url_for('admin.activity_replay', activity_id=activity.id) }}">Re-submit this activity</a></p>
{% endif %}
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,34 @@
"""report comments
Revision ID: 52789c4b1d0f
Revises: c12823f18553
Create Date: 2023-12-26 11:13:13.623630
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '52789c4b1d0f'
down_revision = 'c12823f18553'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('report', schema=None) as batch_op:
batch_op.add_column(sa.Column('suspect_post_reply_id', sa.Integer(), nullable=True))
batch_op.create_foreign_key(None, 'post_reply', ['suspect_post_reply_id'], ['id'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('report', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='foreignkey')
batch_op.drop_column('suspect_post_reply_id')
# ### end Alembic commands ###