From 263ed384147b2e529f02b2985398c542409eb34a Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 6 Jul 2017 18:11:32 +0100 Subject: [PATCH] Saving avatar (without propagating it) --- .../auth/storage/accounts/storage.go | 4 ++ .../dendrite/clientapi/readers/profile.go | 41 ++++++++++++++++++- .../dendrite/clientapi/routing/routing.go | 18 +++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index 6c6fde9e8..f4bac2a02 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -66,6 +66,10 @@ func (d *Database) GetProfileByLocalpart(localpart string) (*authtypes.Profile, return d.profiles.selectProfileByLocalpart(localpart) } +func (d *Database) SetAvatarURL(localpart string, avatarUrl string) error { + return d.profiles.setAvatarURL(localpart, avatarUrl) +} + // CreateAccount makes a new account with the given login name and password. If no password is supplied, // the account will be a passwordless account. func (d *Database) CreateAccount(localpart, plaintextPassword string) (*authtypes.Account, error) { diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go b/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go index 693bf860b..eaf3e5913 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go @@ -20,7 +20,7 @@ import ( "strings" "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/gomatrixserverlib" "github.com/matrix-org/util" @@ -31,7 +31,11 @@ type profileResponse struct { DisplayName string `json:"displayname"` } -func Profile( +type avatarURLRequest struct { + AvatarURL string `json:"avatar_url"` +} + +func GetProfile( req *http.Request, accountDB *accounts.Database, userID string, ) util.JSONResponse { if req.Method == "GET" { @@ -58,6 +62,39 @@ func Profile( } } +func AvatarURL( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + if req.Method == "PUT" { + var r avatarURLRequest + if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { + return *resErr + } + if r.AvatarURL == "" { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("'avatar_url' must be supplied."), + } + } + + localpart := getLocalPart(userID) + if err := accountDB.SetAvatarURL(localpart, r.AvatarURL); err != nil { + return util.JSONResponse{ + Code: 500, + JSON: jsonerror.Unknown("Failed to set avatar URL"), + } + } + return util.JSONResponse{ + Code: 200, + JSON: struct{}{}, + } + } + return util.JSONResponse{ + Code: 405, + JSON: jsonerror.NotFound("Bad method"), + } +} + func getLocalPart(userID string) string { if !strings.HasPrefix(userID, "@") { panic(fmt.Errorf("Invalid user ID")) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index f1146840a..3a2e8a5c8 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -163,9 +163,23 @@ func Setup( r0mux.Handle("/profile/{userID}", common.MakeAPI("profile", func(req *http.Request) util.JSONResponse { - // TODO: Get profile data for user ID vars := mux.Vars(req) - return readers.Profile(req, accountDB, vars["userID"]) + return readers.GetProfile(req, accountDB, vars["userID"]) + }), + ) + + r0mux.Handle("/profile/{userID}/avatar_url", + common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse { + // TODO: Set the avatar URL + vars := mux.Vars(req) + return readers.AvatarURL(req, accountDB, vars["userID"]) + }), + ) + + r0mux.Handle("/profile/{userID}/displayname", + common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse { + // TODO: Set and get the displayname + return util.JSONResponse{Code: 200, JSON: struct{}{}} }), )