mirror of
1
Fork 0

fix up some of the instance patching stuff (#85)

This commit is contained in:
Tobi Smethurst 2021-07-08 15:05:19 +02:00 committed by GitHub
parent fd0714cfde
commit fe269cd641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 29 deletions

View File

@ -25,7 +25,7 @@ func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {
return return
} }
l.Debugf("parsing request form %s", c.Request.Form) l.Debug("parsing request form")
form := &model.InstanceSettingsUpdateRequest{} form := &model.InstanceSettingsUpdateRequest{}
if err := c.ShouldBind(&form); err != nil || form == nil { if err := c.ShouldBind(&form); err != nil || form == nil {
l.Debugf("could not parse form from request: %s", err) l.Debugf("could not parse form from request: %s", err)
@ -33,8 +33,10 @@ func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {
return return
} }
l.Debugf("parsed form: %+v", form)
// if everything on the form is nil, then nothing has been set and we shouldn't continue // if everything on the form is nil, then nothing has been set and we shouldn't continue
if form.SiteTitle == nil && form.SiteContactUsername == nil && form.SiteContactEmail == nil && form.SiteShortDescription == nil && form.SiteDescription == nil && form.SiteTerms == nil && form.Avatar == nil && form.Header == nil { if form.Title == nil && form.ContactUsername == nil && form.ContactEmail == nil && form.ShortDescription == nil && form.Description == nil && form.Terms == nil && form.Avatar == nil && form.Header == nil {
l.Debugf("could not parse form from request") l.Debugf("could not parse form from request")
c.JSON(http.StatusBadRequest, gin.H{"error": "empty form submitted"}) c.JSON(http.StatusBadRequest, gin.H{"error": "empty form submitted"})
return return

View File

@ -75,12 +75,20 @@ type InstanceStats struct {
// InstanceSettingsUpdateRequest is the form to be parsed on a PATCH to /api/v1/instance // InstanceSettingsUpdateRequest is the form to be parsed on a PATCH to /api/v1/instance
type InstanceSettingsUpdateRequest struct { type InstanceSettingsUpdateRequest struct {
SiteTitle *string `form:"site_title" json:"site_title" xml:"site_title"` // Title to use for the instance. Max 40 characters.
SiteContactUsername *string `form:"site_contact_username" json:"site_contact_username" xml:"site_contact_username"` Title *string `form:"title" json:"title" xml:"title"`
SiteContactEmail *string `form:"site_contact_email" json:"site_contact_email" xml:"site_contact_email"` // Username for the instance contact account. Must be the username of an existing admin.
SiteShortDescription *string `form:"site_short_description" json:"site_short_description" xml:"site_short_description"` ContactUsername *string `form:"contact_username" json:"contact_username" xml:"contact_username"`
SiteDescription *string `form:"site_description" json:"site_description" xml:"site_description"` // Email for reaching the instance administrator(s).
SiteTerms *string `form:"site_terms" json:"site_terms" xml:"site_terms"` ContactEmail *string `form:"contact_email" json:"contact_email" xml:"contact_email"`
Avatar *multipart.FileHeader `form:"avatar" json:"avatar" xml:"avatar"` // Short description of the instance, max 500 chars. HTML formatting accepted.
Header *multipart.FileHeader `form:"header" json:"header" xml:"header"` ShortDescription *string `form:"short_description" json:"short_description" xml:"short_description"`
// Longer description of the instance, max 5,000 chars. HTML formatting accepted.
Description *string `form:"description" json:"description" xml:"description"`
// Terms and conditions of the instance, max 5,000 chars. HTML formatting accepted.
Terms *string `form:"terms" json:"terms" xml:"terms"`
// Image to use as the instance thumbnail.
Avatar *multipart.FileHeader `form:"avatar" json:"avatar" xml:"avatar"`
// Image to use as the instance header.
Header *multipart.FileHeader `form:"header" json:"header" xml:"header"`
} }

View File

@ -56,24 +56,24 @@ func (p *processor) InstancePatch(form *apimodel.InstanceSettingsUpdateRequest)
} }
// validate & update site title if it's set on the form // validate & update site title if it's set on the form
if form.SiteTitle != nil { if form.Title != nil {
if err := util.ValidateSiteTitle(*form.SiteTitle); err != nil { if err := util.ValidateSiteTitle(*form.Title); err != nil {
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err)) return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err))
} }
i.Title = *form.SiteTitle i.Title = *form.Title
} }
// validate & update site contact account if it's set on the form // validate & update site contact account if it's set on the form
if form.SiteContactUsername != nil { if form.ContactUsername != nil {
// make sure the account with the given username exists in the db // make sure the account with the given username exists in the db
contactAccount := &gtsmodel.Account{} contactAccount := &gtsmodel.Account{}
if err := p.db.GetLocalAccountByUsername(*form.SiteContactUsername, contactAccount); err != nil { if err := p.db.GetLocalAccountByUsername(*form.ContactUsername, contactAccount); err != nil {
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.SiteContactUsername)) return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.ContactUsername))
} }
// make sure it has a user associated with it // make sure it has a user associated with it
contactUser := &gtsmodel.User{} contactUser := &gtsmodel.User{}
if err := p.db.GetWhere([]db.Where{{Key: "account_id", Value: contactAccount.ID}}, contactUser); err != nil { if err := p.db.GetWhere([]db.Where{{Key: "account_id", Value: contactAccount.ID}}, contactUser); err != nil {
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("user for account with username %s not retrievable", *form.SiteContactUsername)) return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("user for account with username %s not retrievable", *form.ContactUsername))
} }
// suspended accounts cannot be contact accounts // suspended accounts cannot be contact accounts
if !contactAccount.SuspendedAt.IsZero() { if !contactAccount.SuspendedAt.IsZero() {
@ -98,35 +98,35 @@ func (p *processor) InstancePatch(form *apimodel.InstanceSettingsUpdateRequest)
} }
// validate & update site contact email if it's set on the form // validate & update site contact email if it's set on the form
if form.SiteContactEmail != nil { if form.ContactEmail != nil {
if err := util.ValidateEmail(*form.SiteContactEmail); err != nil { if err := util.ValidateEmail(*form.ContactEmail); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error()) return nil, gtserror.NewErrorBadRequest(err, err.Error())
} }
i.ContactEmail = *form.SiteContactEmail i.ContactEmail = *form.ContactEmail
} }
// validate & update site short description if it's set on the form // validate & update site short description if it's set on the form
if form.SiteShortDescription != nil { if form.ShortDescription != nil {
if err := util.ValidateSiteShortDescription(*form.SiteShortDescription); err != nil { if err := util.ValidateSiteShortDescription(*form.ShortDescription); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error()) return nil, gtserror.NewErrorBadRequest(err, err.Error())
} }
i.ShortDescription = *form.SiteShortDescription i.ShortDescription = *form.ShortDescription
} }
// validate & update site description if it's set on the form // validate & update site description if it's set on the form
if form.SiteDescription != nil { if form.Description != nil {
if err := util.ValidateSiteDescription(*form.SiteDescription); err != nil { if err := util.ValidateSiteDescription(*form.Description); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error()) return nil, gtserror.NewErrorBadRequest(err, err.Error())
} }
i.Description = *form.SiteDescription i.Description = *form.Description
} }
// validate & update site terms if it's set on the form // validate & update site terms if it's set on the form
if form.SiteTerms != nil { if form.Terms != nil {
if err := util.ValidateSiteTerms(*form.SiteTerms); err != nil { if err := util.ValidateSiteTerms(*form.Terms); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error()) return nil, gtserror.NewErrorBadRequest(err, err.Error())
} }
i.Terms = *form.SiteTerms i.Terms = *form.Terms
} }
// process avatar if provided // process avatar if provided