mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-02-03 00:31:25 -08:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
40ffefe9b6
4 changed files with 48 additions and 22 deletions
|
@ -46,7 +46,8 @@ from pyld import jsonld
|
|||
from email.utils import formatdate
|
||||
from app import db, celery
|
||||
from app.constants import DATETIME_MS_FORMAT
|
||||
from app.models import utcnow, ActivityPubLog
|
||||
from app.models import utcnow, ActivityPubLog, Community, Instance, CommunityMember, User
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
def http_date(epoch_seconds=None):
|
||||
|
@ -103,8 +104,14 @@ def post_request(uri: str, body: dict | None, private_key: str, key_id: str, con
|
|||
result = HttpSignature.signed_request(uri, body, private_key, key_id, content_type, method, timeout)
|
||||
if result.status_code != 200 and result.status_code != 202 and result.status_code != 204:
|
||||
log.result = 'failure'
|
||||
log.exception_message += f' Response status code was {result.status_code}'
|
||||
current_app.logger.error(f'Response code for post attempt to {uri} was ' +
|
||||
log.exception_message = f'{result.status_code}: {result.text:.100}' + ' - '
|
||||
if 'DOCTYPE html' in result.text:
|
||||
log.result = 'ignored'
|
||||
log.exception_message = f'{result.status_code}: HTML instead of JSON response - '
|
||||
elif 'community_has_no_followers' in result.text:
|
||||
fix_local_community_membership(uri, private_key)
|
||||
else:
|
||||
current_app.logger.error(f'Response code for post attempt to {uri} was ' +
|
||||
str(result.status_code) + ' ' + result.text)
|
||||
log.exception_message += uri
|
||||
if result.status_code == 202:
|
||||
|
@ -536,3 +543,20 @@ def default_context():
|
|||
"identifier": "sc:identifier"
|
||||
})
|
||||
return context
|
||||
|
||||
|
||||
def fix_local_community_membership(uri: str, private_key: str):
|
||||
community = Community.query.filter_by(private_key=private_key).first()
|
||||
parsed_url = urlparse(uri)
|
||||
instance_domain = parsed_url.netloc
|
||||
instance = Instance.query.filter_by(domain=instance_domain).first()
|
||||
|
||||
if community and instance:
|
||||
followers = CommunityMember.query.filter_by(community_id=community.id). \
|
||||
join(User, User.id == CommunityMember.user_id). \
|
||||
filter(User.instance_id == instance.id)
|
||||
for f in followers:
|
||||
db.session.execute(text('DELETE FROM "community_member" WHERE user_id = :user_id AND community_id = :community_id'),
|
||||
{'user_id': f.user_id, 'community_id': community.id})
|
||||
|
||||
|
||||
|
|
|
@ -230,7 +230,7 @@ def register(app):
|
|||
break
|
||||
else:
|
||||
instance.failures += 1
|
||||
elif node.status_code >= 400:
|
||||
elif nodeinfo.status_code >= 400:
|
||||
current_app.logger.info(f"{instance.domain} has no well-known/nodeinfo response")
|
||||
except requests.exceptions.ReadTimeout:
|
||||
instance.failures += 1
|
||||
|
|
|
@ -499,15 +499,16 @@ def join_then_add(actor):
|
|||
join_request = CommunityJoinRequest(user_id=current_user.id, community_id=community.id)
|
||||
db.session.add(join_request)
|
||||
db.session.commit()
|
||||
follow = {
|
||||
"actor": current_user.public_url(),
|
||||
"to": [community.public_url()],
|
||||
"object": community.public_url(),
|
||||
"type": "Follow",
|
||||
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}"
|
||||
}
|
||||
success = post_request(community.ap_inbox_url, follow, current_user.private_key,
|
||||
current_user.public_url() + '#main-key')
|
||||
if not community.instance.gone_forever:
|
||||
follow = {
|
||||
"actor": current_user.public_url(),
|
||||
"to": [community.public_url()],
|
||||
"object": community.public_url(),
|
||||
"type": "Follow",
|
||||
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}"
|
||||
}
|
||||
post_request(community.ap_inbox_url, follow, current_user.private_key,
|
||||
current_user.public_url() + '#main-key')
|
||||
member = CommunityMember(user_id=current_user.id, community_id=community.id)
|
||||
db.session.add(member)
|
||||
db.session.commit()
|
||||
|
|
|
@ -305,12 +305,13 @@ def send_community_follow(community_id, join_request_id, user_id):
|
|||
with current_app.app_context():
|
||||
user = User.query.get(user_id)
|
||||
community = Community.query.get(community_id)
|
||||
follow = {
|
||||
"actor": user.public_url(),
|
||||
"to": [community.public_url()],
|
||||
"object": community.public_url(),
|
||||
"type": "Follow",
|
||||
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request_id}"
|
||||
}
|
||||
success = post_request(community.ap_inbox_url, follow, user.private_key,
|
||||
user.public_url() + '#main-key')
|
||||
if not community.instance.gone_forever:
|
||||
follow = {
|
||||
"actor": user.public_url(),
|
||||
"to": [community.public_url()],
|
||||
"object": community.public_url(),
|
||||
"type": "Follow",
|
||||
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request_id}"
|
||||
}
|
||||
post_request(community.ap_inbox_url, follow, user.private_key,
|
||||
user.public_url() + '#main-key')
|
||||
|
|
Loading…
Add table
Reference in a new issue