more instance patch work
This commit is contained in:
parent
7974d5e2e6
commit
69000ed68e
|
@ -33,7 +33,7 @@ func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
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
|
||||
|
|
|
@ -76,7 +76,6 @@ 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"`
|
||||
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"`
|
||||
|
|
|
@ -24,6 +24,8 @@ type Instance struct {
|
|||
ShortDescription string
|
||||
// Longer description of this instance
|
||||
Description string
|
||||
// Terms and conditions of this instance
|
||||
Terms string
|
||||
// Contact email address for this instance
|
||||
ContactEmail string
|
||||
// Contact account ID in the database for this instance
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
func (p *processor) InstanceGet(domain string) (*apimodel.Instance, gtserror.WithCode) {
|
||||
|
@ -42,15 +43,116 @@ func (p *processor) InstanceGet(domain string) (*apimodel.Instance, gtserror.Wit
|
|||
}
|
||||
|
||||
func (p *processor) InstancePatch(form *apimodel.InstanceSettingsUpdateRequest) (*apimodel.Instance, gtserror.WithCode) {
|
||||
// fetch the instance entry from the db for processing
|
||||
i := >smodel.Instance{}
|
||||
if err := p.db.GetWhere([]db.Where{{Key: "domain", Value: p.config.Host}}, i); err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", p.config.Host, err))
|
||||
}
|
||||
|
||||
// fetch the instance account from the db for processing
|
||||
ia := >smodel.Account{}
|
||||
if err := p.db.GetLocalAccountByUsername(p.config.Host, ia); err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance account %s: %s", p.config.Host, err))
|
||||
}
|
||||
|
||||
// validate & update site title if it's set on the form
|
||||
if form.SiteTitle != nil {
|
||||
if err := util.ValidateSiteTitle(*form.SiteTitle); err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err))
|
||||
}
|
||||
i.Title = *form.SiteTitle
|
||||
}
|
||||
|
||||
// validate & update site contact account if it's set on the form
|
||||
if form.SiteContactUsername != nil {
|
||||
// make sure the account with the given username exists in the db
|
||||
contactAccount := >smodel.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))
|
||||
}
|
||||
// make sure it has a user associated with it
|
||||
contactUser := >smodel.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))
|
||||
}
|
||||
// suspended accounts cannot be contact accounts
|
||||
if !contactAccount.SuspendedAt.IsZero() {
|
||||
err := fmt.Errorf("selected contact account %s is suspended", contactAccount.Username)
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
// unconfirmed or unapproved users cannot be contacts
|
||||
if contactUser.ConfirmedAt.IsZero() {
|
||||
err := fmt.Errorf("user of selected contact account %s is not confirmed", contactAccount.Username)
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
if !contactUser.Approved {
|
||||
err := fmt.Errorf("user of selected contact account %s is not approved", contactAccount.Username)
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
// contact account user must be admin or moderator otherwise what's the point of contacting them
|
||||
if !contactUser.Admin || !contactUser.Moderator {
|
||||
err := fmt.Errorf("user of selected contact account %s is neither admin nor moderator", contactAccount.Username)
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
i.ContactAccountID = contactAccount.ID
|
||||
}
|
||||
|
||||
// validate & update site contact email if it's set on the form
|
||||
if form.SiteContactEmail != nil {
|
||||
if err := util.ValidateEmail(*form.SiteContactEmail); err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
i.ContactEmail = *form.SiteContactEmail
|
||||
}
|
||||
|
||||
// validate & update site short description if it's set on the form
|
||||
if form.SiteShortDescription != nil {
|
||||
if err := util.ValidateSiteShortDescription(*form.SiteShortDescription); err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
i.ShortDescription = *form.SiteShortDescription
|
||||
}
|
||||
|
||||
// validate & update site description if it's set on the form
|
||||
if form.SiteDescription != nil {
|
||||
if err := util.ValidateSiteDescription(*form.SiteDescription); err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
i.Description = *form.SiteDescription
|
||||
}
|
||||
|
||||
// validate & update site terms if it's set on the form
|
||||
if form.SiteTerms != nil {
|
||||
if err := util.ValidateSiteTerms(*form.SiteTerms); err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
||||
}
|
||||
i.Terms = *form.SiteTerms
|
||||
}
|
||||
|
||||
// process avatar if provided
|
||||
if form.Avatar != nil && form.Avatar.Size != 0 {
|
||||
_, err := p.updateAccountAvatar(form.Avatar, ia.ID)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, "error processing avatar")
|
||||
}
|
||||
}
|
||||
|
||||
// process header if provided
|
||||
if form.Header != nil && form.Header.Size != 0 {
|
||||
_, err := p.updateAccountHeader(form.Header, ia.ID)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorBadRequest(err, "error processing header")
|
||||
}
|
||||
}
|
||||
|
||||
if err := p.db.UpdateByID(i.ID, i); err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", p.config.Host, err))
|
||||
}
|
||||
|
||||
ai, err := p.tc.InstanceToMasto(i)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
||||
}
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
|
||||
return ai, nil
|
||||
}
|
||||
|
|
|
@ -542,7 +542,7 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro
|
|||
mi.URLS = &model.InstanceURLs{
|
||||
StreamingAPI: fmt.Sprintf("wss://%s", c.config.Host),
|
||||
}
|
||||
}
|
||||
}aaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
|
||||
// contact account is optional but let's try to get it
|
||||
if i.ContactAccountID != "" {
|
||||
|
|
Loading…
Reference in New Issue