summaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-06-23 16:35:57 +0200
committerLibravatar GitHub <noreply@github.com>2021-06-23 16:35:57 +0200
commit8c9a8533434ec6e159541896d5f43e39303d42e4 (patch)
tree3da3cbf9aa33a160cd076f2b9f8d56396ae3bdfa /internal/api
parentOpengraph meta tags (#55) (diff)
downloadgotosocial-8c9a8533434ec6e159541896d5f43e39303d42e4.tar.xz
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
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/client/instance/instance.go1
-rw-r--r--internal/api/client/instance/instancepatch.go50
-rw-r--r--internal/api/model/instance.go16
3 files changed, 66 insertions, 1 deletions
diff --git a/internal/api/client/instance/instance.go b/internal/api/client/instance/instance.go
index 7fb08f29c..a5becf97d 100644
--- a/internal/api/client/instance/instance.go
+++ b/internal/api/client/instance/instance.go
@@ -34,5 +34,6 @@ func New(config *config.Config, processor processing.Processor, log *logrus.Logg
// Route satisfies the ClientModule interface
func (m *Module) Route(s router.Router) error {
s.AttachHandler(http.MethodGet, InstanceInformationPath, m.InstanceInformationGETHandler)
+ s.AttachHandler(http.MethodPatch, InstanceInformationPath, m.InstanceUpdatePATCHHandler)
return nil
}
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)
+}
diff --git a/internal/api/model/instance.go b/internal/api/model/instance.go
index e4dad3559..834c6fb55 100644
--- a/internal/api/model/instance.go
+++ b/internal/api/model/instance.go
@@ -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
@@ -45,7 +47,7 @@ type Instance struct {
// URLs of interest for clients apps.
URLS *InstanceURLs `json:"urls,omitempty"`
// Statistics about how much information the instance contains.
- Stats *InstanceStats `json:"stats,omitempty"`
+ Stats map[string]int `json:"stats,omitempty"`
// Banner image for the website.
Thumbnail string `json:"thumbnail"`
// A user that can be contacted, as an alternative to email.
@@ -70,3 +72,15 @@ 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"`
+ 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"`
+}