mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-09 22:42:58 +00:00
Return 404 when profile is not found (#524)
* Return 404 when profile is not found * Handler sql.ErrNoRows and avoid returning nil update federation GetProfile as well Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
parent
1cc21d6bcd
commit
142fbcde21
@ -16,6 +16,7 @@ package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||
@ -41,15 +42,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 +58,39 @@ func GetProfile(
|
||||
}
|
||||
}
|
||||
|
||||
// getProfileByUserID returns the profile for userID, otherwise returns an error response
|
||||
func getProfileByUserID(
|
||||
req *http.Request, accountDB *accounts.Database, userID string,
|
||||
) (*authtypes.Profile, *util.JSONResponse) {
|
||||
localpart, _, err := gomatrixserverlib.SplitID('@', userID)
|
||||
if err != nil {
|
||||
resErr := httputil.LogThenError(req, err)
|
||||
return nil, &resErr
|
||||
}
|
||||
|
||||
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, &util.JSONResponse{
|
||||
Code: http.StatusNotFound,
|
||||
JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"),
|
||||
}
|
||||
} else if err != nil {
|
||||
resErr := httputil.LogThenError(req, err)
|
||||
return nil, &resErr
|
||||
}
|
||||
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
// GetAvatarURL implements GET /profile/{userID}/avatar_url
|
||||
func GetAvatarURL(
|
||||
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.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,
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||
@ -52,7 +53,12 @@ func GetProfile(
|
||||
}
|
||||
|
||||
profile, err := accountDB.GetProfileByLocalpart(httpReq.Context(), localpart)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusNotFound,
|
||||
JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"),
|
||||
}
|
||||
} else if err != nil {
|
||||
return httputil.LogThenError(httpReq, err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user