minor bugfixes via sentry

This commit is contained in:
rimu 2024-10-16 21:55:41 +13:00
parent ba2a97c4ad
commit 77172f1d9c
3 changed files with 39 additions and 28 deletions

View file

@ -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: if actor_data.status_code == 200:
try: try:
actor_json = actor_data.json() actor_json = actor_data.json()
except JSONDecodeError as e: except Exception as e:
actor_data.close() actor_data.close()
return None return None
actor_data.close() actor_data.close()
@ -480,7 +480,7 @@ def refresh_user_profile_task(user_id):
user.user_name = activity_json['preferredUsername'].strip() user.user_name = activity_json['preferredUsername'].strip()
if 'name' in activity_json: 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: if 'summary' in activity_json:
about_html = activity_json['summary'] about_html = activity_json['summary']
if about_html is not None and not about_html.startswith('<'): # PeerTube 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': if activity_json['type'] == 'Person' or activity_json['type'] == 'Service':
try: try:
user = User(user_name=activity_json['preferredUsername'].strip(), 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}", email=f"{address}@{server}",
matrix_user_id=activity_json['matrixUserId'] if 'matrixUserId' in activity_json else '', matrix_user_id=activity_json['matrixUserId'] if 'matrixUserId' in activity_json else '',
indexable=activity_json['indexable'] if 'indexable' in activity_json else True, 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 domain.post_count += 1
post.domain = domain 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 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: if 'image' in post_json and post.image is None:
image = File(source_url=post_json['image']['url']) image = File(source_url=post_json['image']['url'])
db.session.add(image) db.session.add(image)

View file

@ -10,6 +10,7 @@ import flask
import httpx import httpx
from flask import json, current_app from flask import json, current_app
from flask_babel import _ from flask_babel import _
from requests import JSONDecodeError
from sqlalchemy import or_, desc, text from sqlalchemy import or_, desc, text
from sqlalchemy.orm import configure_mappers 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) nodeinfo = get_request(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS)
if nodeinfo.status_code == 200: 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']: for links in nodeinfo_json['links']:
if isinstance(links, dict) and 'rel' in links and ( if isinstance(links, dict) and 'rel' in links and (
links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or
@ -240,7 +246,12 @@ def register(app):
try: try:
node = get_request(instance.nodeinfo_href, headers=HEADERS) node = get_request(instance.nodeinfo_href, headers=HEADERS)
if node.status_code == 200: 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: if 'software' in node_json:
instance.software = node_json['software']['name'].lower() instance.software = node_json['software']['name'].lower()
instance.version = node_json['software']['version'] instance.version = node_json['software']['version']

View file

@ -79,7 +79,7 @@ def instance_people(instance_domain):
if instance is None: if instance is None:
abort(404) 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)) people = User.query.filter_by(instance_id=instance.id, deleted=False, banned=False).order_by(desc(User.last_seen))
else: else:
people = User.query.filter_by(instance_id=instance.id, deleted=False, banned=False, searchable=True).order_by(desc(User.last_seen)) people = User.query.filter_by(instance_id=instance.id, deleted=False, banned=False, searchable=True).order_by(desc(User.last_seen))