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
}
l.Debugf("parsing request form %s", c.Request.Form)
l.Debug("parsing request form")
form := &model.InstanceSettingsUpdateRequest{}
if err := c.ShouldBind(&form); err != nil || form == nil {
l.Debugf("could not parse form from request: %s", err)
@ -33,8 +33,10 @@ func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {
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 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")
c.JSON(http.StatusBadRequest, gin.H{"error": "empty form submitted"})
return

View File

@ -75,12 +75,20 @@ type InstanceStats struct {
// InstanceSettingsUpdateRequest is the form to be parsed on a PATCH to /api/v1/instance
type InstanceSettingsUpdateRequest struct {
SiteTitle *string `form:"site_title" json:"site_title" xml:"site_title"`
SiteContactUsername *string `form:"site_contact_username" json:"site_contact_username" xml:"site_contact_username"`
SiteContactEmail *string `form:"site_contact_email" json:"site_contact_email" xml:"site_contact_email"`
SiteShortDescription *string `form:"site_short_description" json:"site_short_description" xml:"site_short_description"`
SiteDescription *string `form:"site_description" json:"site_description" xml:"site_description"`
SiteTerms *string `form:"site_terms" json:"site_terms" xml:"site_terms"`
// Title to use for the instance. Max 40 characters.
Title *string `form:"title" json:"title" xml:"title"`
// Username for the instance contact account. Must be the username of an existing admin.
ContactUsername *string `form:"contact_username" json:"contact_username" xml:"contact_username"`
// Email for reaching the instance administrator(s).
ContactEmail *string `form:"contact_email" json:"contact_email" xml:"contact_email"`
// Short description of the instance, max 500 chars. HTML formatting accepted.
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
if form.SiteTitle != nil {
if err := util.ValidateSiteTitle(*form.SiteTitle); err != nil {
if form.Title != nil {
if err := util.ValidateSiteTitle(*form.Title); err != nil {
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
if form.SiteContactUsername != nil {
if form.ContactUsername != nil {
// make sure the account with the given username exists in the db
contactAccount := &gtsmodel.Account{}
if err := p.db.GetLocalAccountByUsername(*form.SiteContactUsername, contactAccount); err != nil {
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.SiteContactUsername))
if err := p.db.GetLocalAccountByUsername(*form.ContactUsername, contactAccount); err != nil {
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.ContactUsername))
}
// make sure it has a user associated with it
contactUser := &gtsmodel.User{}
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
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
if form.SiteContactEmail != nil {
if err := util.ValidateEmail(*form.SiteContactEmail); err != nil {
if form.ContactEmail != nil {
if err := util.ValidateEmail(*form.ContactEmail); err != nil {
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
if form.SiteShortDescription != nil {
if err := util.ValidateSiteShortDescription(*form.SiteShortDescription); err != nil {
if form.ShortDescription != nil {
if err := util.ValidateSiteShortDescription(*form.ShortDescription); err != nil {
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
if form.SiteDescription != nil {
if err := util.ValidateSiteDescription(*form.SiteDescription); err != nil {
if form.Description != nil {
if err := util.ValidateSiteDescription(*form.Description); err != nil {
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
if form.SiteTerms != nil {
if err := util.ValidateSiteTerms(*form.SiteTerms); err != nil {
if form.Terms != nil {
if err := util.ValidateSiteTerms(*form.Terms); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
i.Terms = *form.SiteTerms
i.Terms = *form.Terms
}
// process avatar if provided