From 2ca6b914bdd76333e9e30c8979ae8c5e6f5d67aa Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Fri, 7 Jul 2017 11:20:51 +0100 Subject: [PATCH] Saving display name (without propagating it) --- .../auth/storage/accounts/profile_table.go | 12 ++++++ .../auth/storage/accounts/storage.go | 8 +++- .../dendrite/clientapi/readers/profile.go | 37 +++++++++++++++++++ .../dendrite/clientapi/routing/routing.go | 5 +-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go index 30097b433..36416e077 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go @@ -41,10 +41,14 @@ const selectProfileByLocalpartSQL = "" + const setAvatarURLSQL = "" + "UPDATE profiles SET avatar_url = $1 WHERE localpart = $2" +const setDisplayNameSQL = "" + + "UPDATE profiles SET display_name = $1 WHERE localpart = $2" + type profilesStatements struct { insertProfileStmt *sql.Stmt selectProfileByLocalpartStmt *sql.Stmt setAvatarURLStmt *sql.Stmt + setDisplayNameStmt *sql.Stmt } func (s *profilesStatements) prepare(db *sql.DB) (err error) { @@ -61,6 +65,9 @@ func (s *profilesStatements) prepare(db *sql.DB) (err error) { if s.setAvatarURLStmt, err = db.Prepare(setAvatarURLSQL); err != nil { return } + if s.setDisplayNameStmt, err = db.Prepare(setDisplayNameSQL); err != nil { + return + } return } @@ -79,3 +86,8 @@ func (s *profilesStatements) setAvatarURL(localpart string, avatarURL string) (e _, err = s.setAvatarURLStmt.Exec(avatarURL, localpart) return } + +func (s *profilesStatements) setDisplayName(localpart string, displayName string) (err error) { + _, err = s.setDisplayNameStmt.Exec(displayName, localpart) + return +} 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 f4bac2a02..7317721d0 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,8 +66,12 @@ 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) +func (d *Database) SetAvatarURL(localpart string, avatarURL string) error { + return d.profiles.setAvatarURL(localpart, avatarURL) +} + +func (d *Database) SetDisplayName(localpart string, displayName string) error { + return d.profiles.setDisplayName(localpart, displayName) } // CreateAccount makes a new account with the given login name and password. If no password is supplied, 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 eaf3e5913..498e773b7 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go @@ -35,6 +35,10 @@ type avatarURLRequest struct { AvatarURL string `json:"avatar_url"` } +type displayNameRequest struct { + DisplayName string `json:"displayname"` +} + func GetProfile( req *http.Request, accountDB *accounts.Database, userID string, ) util.JSONResponse { @@ -95,6 +99,39 @@ func AvatarURL( } } +func DisplayName( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + if req.Method == "PUT" { + var r displayNameRequest + if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { + return *resErr + } + if r.DisplayName == "" { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("'displayname' must be supplied."), + } + } + + localpart := getLocalPart(userID) + if err := accountDB.SetDisplayName(localpart, r.DisplayName); err != nil { + return util.JSONResponse{ + Code: 500, + JSON: jsonerror.Unknown("Failed to set display name"), + } + } + 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 3a2e8a5c8..ad41d798d 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -170,7 +170,6 @@ func Setup( 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"]) }), @@ -178,8 +177,8 @@ func Setup( 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{}{}} + vars := mux.Vars(req) + return readers.DisplayName(req, accountDB, vars["userID"]) }), )