Return 404 when profile is not found

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
Anant Prakash 2018-06-27 17:30:13 +05:30
parent af08eea46d
commit 7028bfa320
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 38 additions and 18 deletions

View file

@ -98,7 +98,13 @@ func (d *Database) GetAccountByPassword(
func (d *Database) GetProfileByLocalpart(
ctx context.Context, localpart string,
) (*authtypes.Profile, error) {
return d.profiles.selectProfileByLocalpart(ctx, localpart)
profile, err := d.profiles.selectProfileByLocalpart(ctx, localpart)
// err == sql.ErrNoRows signifies that this profile is not in the db
if err != nil && err != sql.ErrNoRows {
return nil, err
}
return profile, nil
}
// SetAvatarURL updates the avatar URL of the profile associated with the given

View file

@ -41,15 +41,12 @@ func GetProfile(
JSON: jsonerror.NotFound("Bad method"),
}
}
localpart, _, err := gomatrixserverlib.SplitID('@', userID)
profile, err := getProfileByUserID(req, accountDB, userID)
if err != nil {
return httputil.LogThenError(req, err)
return *err
}
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
if err != nil {
return httputil.LogThenError(req, err)
}
res := common.ProfileResponse{
AvatarURL: profile.AvatarURL,
DisplayName: profile.DisplayName,
@ -60,19 +57,40 @@ func GetProfile(
}
}
// GetAvatarURL implements GET /profile/{userID}/avatar_url
func GetAvatarURL(
func getProfileByUserID(
req *http.Request, accountDB *accounts.Database, userID string,
) util.JSONResponse {
) (*authtypes.Profile, *util.JSONResponse) {
localpart, _, err := gomatrixserverlib.SplitID('@', userID)
if err != nil {
return httputil.LogThenError(req, err)
resErr := httputil.LogThenError(req, err)
return nil, &resErr
}
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
if err != nil {
return httputil.LogThenError(req, err)
resErr := httputil.LogThenError(req, err)
return nil, &resErr
}
if profile == nil {
return nil, &util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"),
}
}
return profile, nil
}
// GetAvatarURL implements GET /profile/{userID}/avatar_url
func GetAvatarURL(
req *http.Request, accountDB *accounts.Database, userID string,
) util.JSONResponse {
profile, err := getProfileByUserID(req, accountDB, userID)
if err != nil {
return *err
}
res := common.AvatarURL{
AvatarURL: profile.AvatarURL,
}
@ -156,15 +174,11 @@ func SetAvatarURL(
func GetDisplayName(
req *http.Request, accountDB *accounts.Database, userID string,
) util.JSONResponse {
localpart, _, err := gomatrixserverlib.SplitID('@', userID)
profile, err := getProfileByUserID(req, accountDB, userID)
if err != nil {
return httputil.LogThenError(req, err)
return *err
}
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
if err != nil {
return httputil.LogThenError(req, err)
}
res := common.DisplayName{
DisplayName: profile.DisplayName,
}