Saving display name (without propagating it)

This commit is contained in:
Brendan Abolivier 2017-07-07 11:20:51 +01:00
parent 263ed38414
commit 2ca6b914bd
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
4 changed files with 57 additions and 5 deletions

View file

@ -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
}

View file

@ -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,

View file

@ -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"))

View file

@ -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"])
}),
)