diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 33e71395..4def65ef 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -319,7 +319,7 @@ def find_actor_or_create(actor: str, create_if_not_found=True, community_only=Fa if actor_data.status_code == 200: try: actor_json = actor_data.json() - except JSONDecodeError as e: + except Exception as e: actor_data.close() return None actor_data.close() @@ -480,7 +480,7 @@ def refresh_user_profile_task(user_id): user.user_name = activity_json['preferredUsername'].strip() if 'name' in activity_json: - user.title = activity_json['name'].strip() + user.title = activity_json['name'].strip() if activity_json['name'] else '' if 'summary' in activity_json: about_html = activity_json['summary'] if about_html is not None and not about_html.startswith('<'): # PeerTube @@ -682,7 +682,7 @@ def actor_json_to_model(activity_json, address, server): if activity_json['type'] == 'Person' or activity_json['type'] == 'Service': try: user = User(user_name=activity_json['preferredUsername'].strip(), - title=activity_json['name'].strip() if 'name' in activity_json else None, + title=activity_json['name'].strip() if 'name' in activity_json and activity_json['name'] else None, email=f"{address}@{server}", matrix_user_id=activity_json['matrixUserId'] if 'matrixUserId' in activity_json else '', indexable=activity_json['indexable'] if 'indexable' in activity_json else True, @@ -918,29 +918,29 @@ def post_json_to_model(activity_log, post_json, user, community) -> Post: domain.post_count += 1 post.domain = domain - if post_json['type'] == 'Video': - post.type = POST_TYPE_VIDEO - post.url = post_json['id'] - if 'icon' in post_json and isinstance(post_json['icon'], list): - icon = File(source_url=post_json['icon'][-1]['url']) - db.session.add(icon) - post.image = icon - - if 'language' in post_json: - language = find_language_or_create(post_json['language']['identifier'], post_json['language']['name']) - if language: - post.language_id = language.id - - if 'tag' in post_json: - for json_tag in post_json['tag']: - if json_tag['type'] == 'Hashtag': - # Lemmy adds the community slug as a hashtag on every post in the community, which we want to ignore - if json_tag['name'][1:].lower() != community.name.lower(): - hashtag = find_hashtag_or_create(json_tag['name']) - if hashtag: - post.tags.append(hashtag) - if post is not None: + if post_json['type'] == 'Video': + post.type = POST_TYPE_VIDEO + post.url = post_json['id'] + if 'icon' in post_json and isinstance(post_json['icon'], list): + icon = File(source_url=post_json['icon'][-1]['url']) + db.session.add(icon) + post.image = icon + + if 'language' in post_json: + language = find_language_or_create(post_json['language']['identifier'], post_json['language']['name']) + if language: + post.language_id = language.id + + if 'tag' in post_json: + for json_tag in post_json['tag']: + if json_tag['type'] == 'Hashtag': + # Lemmy adds the community slug as a hashtag on every post in the community, which we want to ignore + if json_tag['name'][1:].lower() != community.name.lower(): + hashtag = find_hashtag_or_create(json_tag['name']) + if hashtag: + post.tags.append(hashtag) + if 'image' in post_json and post.image is None: image = File(source_url=post_json['image']['url']) db.session.add(image) diff --git a/app/cli.py b/app/cli.py index dff7df06..a42783b9 100644 --- a/app/cli.py +++ b/app/cli.py @@ -10,6 +10,7 @@ import flask import httpx from flask import json, current_app from flask_babel import _ +from requests import JSONDecodeError from sqlalchemy import or_, desc, text from sqlalchemy.orm import configure_mappers @@ -217,7 +218,12 @@ def register(app): nodeinfo = get_request(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS) if nodeinfo.status_code == 200: - nodeinfo_json = nodeinfo.json() + try: + nodeinfo_json = nodeinfo.json() + except Exception as e: + nodeinfo_json = {} + finally: + nodeinfo.close() for links in nodeinfo_json['links']: if isinstance(links, dict) and 'rel' in links and ( links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or @@ -240,7 +246,12 @@ def register(app): try: node = get_request(instance.nodeinfo_href, headers=HEADERS) if node.status_code == 200: - node_json = node.json() + try: + node_json = node.json() + except Exception as e: + node_json = {} + finally: + node.close() if 'software' in node_json: instance.software = node_json['software']['name'].lower() instance.version = node_json['software']['version'] diff --git a/app/instance/routes.py b/app/instance/routes.py index b1c6830c..8d8a64a9 100644 --- a/app/instance/routes.py +++ b/app/instance/routes.py @@ -79,7 +79,7 @@ def instance_people(instance_domain): if instance is None: abort(404) - if current_user.is_admin(): + if current_user.is_authenticated and current_user.is_admin(): people = User.query.filter_by(instance_id=instance.id, deleted=False, banned=False).order_by(desc(User.last_seen)) else: people = User.query.filter_by(instance_id=instance.id, deleted=False, banned=False, searchable=True).order_by(desc(User.last_seen))