From 8c9a8533434ec6e159541896d5f43e39303d42e4 Mon Sep 17 00:00:00 2001 From: Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 23 Jun 2021 16:35:57 +0200 Subject: Instance settings updates (#59) Allow admins to set instance settings through a PATCH to /api/v1/instance Update templates to reflect some of the new fields --- internal/api/client/instance/instancepatch.go | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 internal/api/client/instance/instancepatch.go (limited to 'internal/api/client/instance/instancepatch.go') diff --git a/internal/api/client/instance/instancepatch.go b/internal/api/client/instance/instancepatch.go new file mode 100644 index 000000000..ace7674c0 --- /dev/null +++ b/internal/api/client/instance/instancepatch.go @@ -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.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) +} -- cgit v1.2.3