continue with instance patch stuff

This commit is contained in:
tsmethurst
2021-06-22 18:49:07 +02:00
parent c08dc0937b
commit 7974d5e2e6
7 changed files with 131 additions and 36 deletions

View File

@ -0,0 +1,50 @@
package instance
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
)
func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {
l := m.log.WithField("func", "InstanceUpdatePATCHHandler")
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
l.Debugf("couldn't auth: %s", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}
// only admins can update instance settings
if !authed.User.Admin {
l.Debug("user is not an admin so cannot update instance settings")
c.JSON(http.StatusUnauthorized, gin.H{"error": "not an admin"})
return
}
l.Debugf("parsing request form %s", c.Request.Form)
form := &model.InstanceSettingsUpdateRequest{}
if err := c.ShouldBind(&form); err != nil || form == nil {
l.Debugf("could not parse form from request: %s", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// if everything on the form is nil, then nothing has been set and we shouldn't continue
if form.SiteTitle == nil && form.RegistrationsMode == nil && form.SiteContactUsername == nil && form.SiteContactEmail == nil && form.SiteShortDescription == nil && form.SiteDescription == nil && form.SiteTerms == 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
}
i, errWithCode := m.processor.InstancePatch(form)
if errWithCode != nil {
l.Debugf("error with instance patch request: %s", errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}
c.JSON(http.StatusOK, i)
}

View File

@ -18,8 +18,6 @@
package model
import "mime/multipart"
// AdminAccountInfo represents the *admin* view of an account's details. See here: https://docs.joinmastodon.org/entities/admin-account/
type AdminAccountInfo struct {
// The ID of the account in the database.
@ -81,24 +79,3 @@ type AdminReportInfo struct {
// Statuses attached to the report, for context.
Statuses []Status `json:"statuses"`
}
// AdminSiteSettings is the form to be parsed on a POST or PATCH to /admin/settings
type AdminSiteSettings struct {
FormAdminSettings FormAdminSettings `form:"form_admin_settings" json:"form_admin_settings" xml:"form_admin_settings"`
}
// FormAdminSettings wraps a whole bunch of instance settings
type FormAdminSettings struct {
SiteTitle *string `form:"site_title" json:"site_title" xml:"site_title"`
RegistrationsMode *string `form:"registrations_mode" json:"registrations_mode" xml:"registrations_mode"`
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"`
SiteExtendedDescription *string `form:"site_extended_description" json:"site_extended_description" xml:"site_extended_description"`
SiteTerms *string `form:"site_terms" json:"site_terms" xml:"site_terms"`
Thumbnail *multipart.FileHeader `form:"thumbnail" json:"thumbnail" xml:"thumbnail"`
Hero *multipart.FileHeader `form:"hero" json:"hero" xml:"hero"`
Mascot *multipart.FileHeader `form:"mascot" json:"mascot" xml:"mascot"`
RequireInviteText *bool `form:"require_invite_text" json:"require_invite_text" xml:"require_invite_text"`
}

View File

@ -18,6 +18,8 @@
package model
import "mime/multipart"
// Instance represents the software instance of Mastodon running on this domain. See https://docs.joinmastodon.org/entities/instance/
type Instance struct {
// REQUIRED
@ -70,3 +72,16 @@ type InstanceStats struct {
// Domains federated with this instance.
DomainCount int `json:"domain_count"`
}
// 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"`
RegistrationsMode *string `form:"registrations_mode" json:"registrations_mode" xml:"registrations_mode"`
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"`
Avatar *multipart.FileHeader `form:"avatar" json:"avatar" xml:"avatar"`
Header *multipart.FileHeader `form:"header" json:"header" xml:"header"`
}