mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-10 16:33:11 -06:00
Saving avatar (without propagating it)
This commit is contained in:
parent
490d14ebe7
commit
263ed38414
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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{}{}}
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue