Properly export profile-related structs and fix wording

Signed-off-by: Andrew (anoa) <anoa@openmailbox.org>
This commit is contained in:
Andrew (anoa) 2017-11-03 11:14:30 -07:00
parent d39bcb005e
commit 3f4cf27b5c
No known key found for this signature in database
GPG key ID: 174BEAB009FD176D
4 changed files with 31 additions and 40 deletions

View file

@ -73,10 +73,10 @@ func MissingArgument(msg string) *MatrixError {
return &MatrixError{"M_MISSING_ARGUMENT", msg} return &MatrixError{"M_MISSING_ARGUMENT", msg}
} }
// InvalidArgumentBody is an error when the client tries to provide an // InvalidArgumentValue is an error when the client tries to provide an
// invalid body under a valid argument // invalid value for a valid argument
func InvalidArgumentBody(msg string) *MatrixError { func InvalidArgumentValue(msg string) *MatrixError {
return &MatrixError{"M_INVALID_ARGUMENT_BODY", msg} return &MatrixError{"M_INVALID_ARGUMENT_VALUE", msg}
} }
// MissingToken is an error when the client tries to access a resource which // MissingToken is an error when the client tries to access a resource which

View file

@ -31,19 +31,6 @@ import (
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
type profileResponse struct {
AvatarURL string `json:"avatar_url"`
DisplayName string `json:"displayname"`
}
type avatarURL struct {
AvatarURL string `json:"avatar_url"`
}
type displayName struct {
DisplayName string `json:"displayname"`
}
// GetProfile implements GET /profile/{userID} // GetProfile implements GET /profile/{userID}
func GetProfile( func GetProfile(
req *http.Request, accountDB *accounts.Database, userID string, req *http.Request, accountDB *accounts.Database, userID string,
@ -63,7 +50,7 @@ func GetProfile(
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
res := profileResponse{ res := common.ProfileResponse{
AvatarURL: profile.AvatarURL, AvatarURL: profile.AvatarURL,
DisplayName: profile.DisplayName, DisplayName: profile.DisplayName,
} }
@ -86,7 +73,7 @@ func GetAvatarURL(
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
res := avatarURL{ res := common.AvatarURL{
AvatarURL: profile.AvatarURL, AvatarURL: profile.AvatarURL,
} }
return util.JSONResponse{ return util.JSONResponse{
@ -110,7 +97,7 @@ func SetAvatarURL(
changedKey := "avatar_url" changedKey := "avatar_url"
var r avatarURL var r common.AvatarURL
if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
return *resErr return *resErr
} }
@ -178,7 +165,7 @@ func GetDisplayName(
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
res := displayName{ res := common.DisplayName{
DisplayName: profile.DisplayName, DisplayName: profile.DisplayName,
} }
return util.JSONResponse{ return util.JSONResponse{
@ -202,7 +189,7 @@ func SetDisplayName(
changedKey := "displayname" changedKey := "displayname"
var r displayName var r common.DisplayName
if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
return *resErr return *resErr
} }

View file

@ -20,3 +20,19 @@ type AccountData struct {
RoomID string `json:"room_id"` RoomID string `json:"room_id"`
Type string `json:"type"` Type string `json:"type"`
} }
// ProfileResponse is a struct containing all known user profile data
type ProfileResponse struct {
AvatarURL string `json:"avatar_url"`
DisplayName string `json:"displayname"`
}
// AvatarURL is a struct containing only the URL to a user's avatar
type AvatarURL struct {
AvatarURL string `json:"avatar_url"`
}
// DisplayName is a struct containing only a user's display name
type DisplayName struct {
DisplayName string `json:"displayname"`
}

View file

@ -20,24 +20,12 @@ import (
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
type profileResponse struct { // GetProfile implements GET /_matrix/federation/v1/query/profile
AvatarURL string `json:"avatar_url"`
DisplayName string `json:"displayname"`
}
type avatarURL struct {
AvatarURL string `json:"avatar_url"`
}
type displayName struct {
DisplayName string `json:"displayname"`
}
// GetProfile implements /_matrix/federation/v1/query/profile
func GetProfile( func GetProfile(
httpReq *http.Request, httpReq *http.Request,
accountDB *accounts.Database, accountDB *accounts.Database,
@ -68,19 +56,19 @@ func GetProfile(
if field != "" { if field != "" {
switch field { switch field {
case "displayname": case "displayname":
res = displayName{ res = common.DisplayName{
profile.DisplayName, profile.DisplayName,
} }
case "avatar_url": case "avatar_url":
res = avatarURL{ res = common.AvatarURL{
profile.AvatarURL, profile.AvatarURL,
} }
default: default:
code = 400 code = 400
res = jsonerror.InvalidArgumentBody("The request body did not contain allowed values of argument 'field'. Allowed: 'avatar_url', 'displayname'.") res = jsonerror.InvalidArgumentValue("The request body did not contain an allowed value of argument 'field'. Allowed values are either: 'avatar_url', 'displayname'.")
} }
} else { } else {
res = profileResponse{ res = common.ProfileResponse{
profile.AvatarURL, profile.AvatarURL,
profile.DisplayName, profile.DisplayName,
} }