Return M_NOT_FOUND when a profile could not be found. (#3596)

This commit is contained in:
Will Hunt 2018-08-03 19:08:05 +01:00 committed by Richard van der Hoff
parent df7f9871c1
commit 16d9701892
2 changed files with 59 additions and 31 deletions

1
changelog.d/3585.bugfix Normal file
View file

@ -0,0 +1 @@
Respond with M_NOT_FOUND when profiles are not found locally or over federation. Fixes #3585

View file

@ -17,7 +17,13 @@ import logging
from twisted.internet import defer from twisted.internet import defer
from synapse.api.errors import AuthError, CodeMessageException, SynapseError from synapse.api.errors import (
AuthError,
CodeMessageException,
Codes,
StoreError,
SynapseError,
)
from synapse.metrics.background_process_metrics import run_as_background_process from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.types import UserID, get_domain_from_id from synapse.types import UserID, get_domain_from_id
@ -49,12 +55,17 @@ class ProfileHandler(BaseHandler):
def get_profile(self, user_id): def get_profile(self, user_id):
target_user = UserID.from_string(user_id) target_user = UserID.from_string(user_id)
if self.hs.is_mine(target_user): if self.hs.is_mine(target_user):
try:
displayname = yield self.store.get_profile_displayname( displayname = yield self.store.get_profile_displayname(
target_user.localpart target_user.localpart
) )
avatar_url = yield self.store.get_profile_avatar_url( avatar_url = yield self.store.get_profile_avatar_url(
target_user.localpart target_user.localpart
) )
except StoreError as e:
if e.code == 404:
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
raise
defer.returnValue({ defer.returnValue({
"displayname": displayname, "displayname": displayname,
@ -74,7 +85,6 @@ class ProfileHandler(BaseHandler):
except CodeMessageException as e: except CodeMessageException as e:
if e.code != 404: if e.code != 404:
logger.exception("Failed to get displayname") logger.exception("Failed to get displayname")
raise raise
@defer.inlineCallbacks @defer.inlineCallbacks
@ -85,12 +95,17 @@ class ProfileHandler(BaseHandler):
""" """
target_user = UserID.from_string(user_id) target_user = UserID.from_string(user_id)
if self.hs.is_mine(target_user): if self.hs.is_mine(target_user):
try:
displayname = yield self.store.get_profile_displayname( displayname = yield self.store.get_profile_displayname(
target_user.localpart target_user.localpart
) )
avatar_url = yield self.store.get_profile_avatar_url( avatar_url = yield self.store.get_profile_avatar_url(
target_user.localpart target_user.localpart
) )
except StoreError as e:
if e.code == 404:
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
raise
defer.returnValue({ defer.returnValue({
"displayname": displayname, "displayname": displayname,
@ -103,9 +118,14 @@ class ProfileHandler(BaseHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def get_displayname(self, target_user): def get_displayname(self, target_user):
if self.hs.is_mine(target_user): if self.hs.is_mine(target_user):
try:
displayname = yield self.store.get_profile_displayname( displayname = yield self.store.get_profile_displayname(
target_user.localpart target_user.localpart
) )
except StoreError as e:
if e.code == 404:
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
raise
defer.returnValue(displayname) defer.returnValue(displayname)
else: else:
@ -122,7 +142,6 @@ class ProfileHandler(BaseHandler):
except CodeMessageException as e: except CodeMessageException as e:
if e.code != 404: if e.code != 404:
logger.exception("Failed to get displayname") logger.exception("Failed to get displayname")
raise raise
except Exception: except Exception:
logger.exception("Failed to get displayname") logger.exception("Failed to get displayname")
@ -157,10 +176,14 @@ class ProfileHandler(BaseHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def get_avatar_url(self, target_user): def get_avatar_url(self, target_user):
if self.hs.is_mine(target_user): if self.hs.is_mine(target_user):
try:
avatar_url = yield self.store.get_profile_avatar_url( avatar_url = yield self.store.get_profile_avatar_url(
target_user.localpart target_user.localpart
) )
except StoreError as e:
if e.code == 404:
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
raise
defer.returnValue(avatar_url) defer.returnValue(avatar_url)
else: else:
try: try:
@ -213,7 +236,7 @@ class ProfileHandler(BaseHandler):
just_field = args.get("field", None) just_field = args.get("field", None)
response = {} response = {}
try:
if just_field is None or just_field == "displayname": if just_field is None or just_field == "displayname":
response["displayname"] = yield self.store.get_profile_displayname( response["displayname"] = yield self.store.get_profile_displayname(
user.localpart user.localpart
@ -223,6 +246,10 @@ class ProfileHandler(BaseHandler):
response["avatar_url"] = yield self.store.get_profile_avatar_url( response["avatar_url"] = yield self.store.get_profile_avatar_url(
user.localpart user.localpart
) )
except StoreError as e:
if e.code == 404:
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
raise
defer.returnValue(response) defer.returnValue(response)