Saving avatar (without propagating it)

This commit is contained in:
Brendan Abolivier 2017-07-06 18:11:32 +01:00
parent 490d14ebe7
commit 263ed38414
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
3 changed files with 59 additions and 4 deletions

View file

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

View file

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

View file

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