mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-10 16:33:11 -06:00
Saving display name (without propagating it)
This commit is contained in:
parent
263ed38414
commit
2ca6b914bd
|
|
@ -41,10 +41,14 @@ const selectProfileByLocalpartSQL = "" +
|
||||||
const setAvatarURLSQL = "" +
|
const setAvatarURLSQL = "" +
|
||||||
"UPDATE profiles SET avatar_url = $1 WHERE localpart = $2"
|
"UPDATE profiles SET avatar_url = $1 WHERE localpart = $2"
|
||||||
|
|
||||||
|
const setDisplayNameSQL = "" +
|
||||||
|
"UPDATE profiles SET display_name = $1 WHERE localpart = $2"
|
||||||
|
|
||||||
type profilesStatements struct {
|
type profilesStatements struct {
|
||||||
insertProfileStmt *sql.Stmt
|
insertProfileStmt *sql.Stmt
|
||||||
selectProfileByLocalpartStmt *sql.Stmt
|
selectProfileByLocalpartStmt *sql.Stmt
|
||||||
setAvatarURLStmt *sql.Stmt
|
setAvatarURLStmt *sql.Stmt
|
||||||
|
setDisplayNameStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *profilesStatements) prepare(db *sql.DB) (err error) {
|
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 {
|
if s.setAvatarURLStmt, err = db.Prepare(setAvatarURLSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.setDisplayNameStmt, err = db.Prepare(setDisplayNameSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,3 +86,8 @@ func (s *profilesStatements) setAvatarURL(localpart string, avatarURL string) (e
|
||||||
_, err = s.setAvatarURLStmt.Exec(avatarURL, localpart)
|
_, err = s.setAvatarURLStmt.Exec(avatarURL, localpart)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *profilesStatements) setDisplayName(localpart string, displayName string) (err error) {
|
||||||
|
_, err = s.setDisplayNameStmt.Exec(displayName, localpart)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,8 +66,12 @@ func (d *Database) GetProfileByLocalpart(localpart string) (*authtypes.Profile,
|
||||||
return d.profiles.selectProfileByLocalpart(localpart)
|
return d.profiles.selectProfileByLocalpart(localpart)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) SetAvatarURL(localpart string, avatarUrl string) error {
|
func (d *Database) SetAvatarURL(localpart string, avatarURL string) error {
|
||||||
return d.profiles.setAvatarURL(localpart, avatarUrl)
|
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,
|
// CreateAccount makes a new account with the given login name and password. If no password is supplied,
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,10 @@ type avatarURLRequest struct {
|
||||||
AvatarURL string `json:"avatar_url"`
|
AvatarURL string `json:"avatar_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type displayNameRequest struct {
|
||||||
|
DisplayName string `json:"displayname"`
|
||||||
|
}
|
||||||
|
|
||||||
func GetProfile(
|
func GetProfile(
|
||||||
req *http.Request, accountDB *accounts.Database, userID string,
|
req *http.Request, accountDB *accounts.Database, userID string,
|
||||||
) util.JSONResponse {
|
) 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 {
|
func getLocalPart(userID string) string {
|
||||||
if !strings.HasPrefix(userID, "@") {
|
if !strings.HasPrefix(userID, "@") {
|
||||||
panic(fmt.Errorf("Invalid user ID"))
|
panic(fmt.Errorf("Invalid user ID"))
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,6 @@ func Setup(
|
||||||
|
|
||||||
r0mux.Handle("/profile/{userID}/avatar_url",
|
r0mux.Handle("/profile/{userID}/avatar_url",
|
||||||
common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse {
|
common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse {
|
||||||
// TODO: Set the avatar URL
|
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
return readers.AvatarURL(req, accountDB, vars["userID"])
|
return readers.AvatarURL(req, accountDB, vars["userID"])
|
||||||
}),
|
}),
|
||||||
|
|
@ -178,8 +177,8 @@ func Setup(
|
||||||
|
|
||||||
r0mux.Handle("/profile/{userID}/displayname",
|
r0mux.Handle("/profile/{userID}/displayname",
|
||||||
common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse {
|
common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse {
|
||||||
// TODO: Set and get the displayname
|
vars := mux.Vars(req)
|
||||||
return util.JSONResponse{Code: 200, JSON: struct{}{}}
|
return readers.DisplayName(req, accountDB, vars["userID"])
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue