Show more fediverse errors to the end user

This commit is contained in:
jesopo 2019-11-14 10:53:34 +00:00
parent 76212faed4
commit 8ccbeb54d7
2 changed files with 28 additions and 13 deletions

View file

@ -112,14 +112,15 @@ class Module(ModuleManager.BaseModule):
def _get_from_outbox(self, username, instance): def _get_from_outbox(self, username, instance):
try:
actor_url = ap_utils.find_actor(username, instance) actor_url = ap_utils.find_actor(username, instance)
except ap_utils.FindActorException as e:
raise utils.EventError(str(e))
if not actor_url:
raise utils.EventError("Failed to find user")
actor = ap_actor.Actor(actor_url) actor = ap_actor.Actor(actor_url)
if not actor.load(): if not actor.load():
raise utils.EventError("Failed to find user") raise utils.EventError("Failed to load user")
items = actor.outbox.load() items = actor.outbox.load()
nonreply = [actor.followers] nonreply = [actor.followers]

View file

@ -34,13 +34,20 @@ def activity_request(url, data=None, method="GET", type=ACTIVITY_TYPE,
HOSTMETA_TEMPLATE = "https://%s/.well-known/host-meta" HOSTMETA_TEMPLATE = "https://%s/.well-known/host-meta"
WEBFINGER_TEMPLATE = "https://%s/.well-known/webfinger?resource={uri}" WEBFINGER_TEMPLATE = "https://%s/.well-known/webfinger?resource={uri}"
class FindActorException(Exception):
pass
def find_actor(username, instance): def find_actor(username, instance):
hostmeta = HOSTMETA_TEMPLATE % instance hostmeta = HOSTMETA_TEMPLATE % instance
hostmeta_request = utils.http.Request(HOSTMETA_TEMPLATE % instance, hostmeta_request = utils.http.Request(HOSTMETA_TEMPLATE % instance,
useragent=USERAGENT, parse=True, check_content_type=False) useragent=USERAGENT, parse=True, check_content_type=False)
try:
hostmeta = utils.http.request(hostmeta_request) hostmeta = utils.http.request(hostmeta_request)
except:
raise FindActorException("Failed to get host-meta for %s" % instance)
webfinger_url = None webfinger_url = None
if hostmeta.code == 200:
for item in hostmeta.data.find_all("link"): for item in hostmeta.data.find_all("link"):
if item["rel"] and item["rel"][0] == "lrdd": if item["rel"] and item["rel"][0] == "lrdd":
webfinger_url = item["template"] webfinger_url = item["template"]
@ -51,12 +58,19 @@ def find_actor(username, instance):
webfinger_url = webfinger_url.replace("{uri}", webfinger_url = webfinger_url.replace("{uri}",
"acct:%s@%s" % (username, instance), 1) "acct:%s@%s" % (username, instance), 1)
try:
webfinger = activity_request(webfinger_url, type=JRD_TYPE) webfinger = activity_request(webfinger_url, type=JRD_TYPE)
except:
raise FindActorException("Failed to get webfinger for %s" % instance)
actor_url = None actor_url = None
if webfinger.code == 200:
for link in webfinger.data["links"]: for link in webfinger.data["links"]:
if link["type"] == ACTIVITY_TYPE: if link["type"] == ACTIVITY_TYPE:
return link["href"] return link["href"]
else:
raise FindActorException("Could not find user @%s@%s" %
(username, instance))
KNOWN_TAGS = ["p", "br"] KNOWN_TAGS = ["p", "br"]