-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow admins to set instance settings through a PATCH to /api/v1/instance Update templates to reflect some of the new fields
- Loading branch information
1 parent
fd57cca
commit 8c9a853
Showing
13 changed files
with
340 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pg | ||
|
||
import ( | ||
"github.com/go-pg/pg/v10" | ||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | ||
) | ||
|
||
func (ps *postgresService) GetUserCountForInstance(domain string) (int, error) { | ||
q := ps.conn.Model(&[]*gtsmodel.Account{}) | ||
|
||
if domain == ps.config.Host { | ||
// if the domain is *this* domain, just count where the domain field is null | ||
q = q.Where("? IS NULL", pg.Ident("domain")) | ||
} else { | ||
q = q.Where("domain = ?", domain) | ||
} | ||
|
||
// don't count the instance account or suspended users | ||
q = q.Where("username != ?", domain).Where("? IS NULL", pg.Ident("suspended_at")) | ||
|
||
return q.Count() | ||
} | ||
|
||
func (ps *postgresService) GetStatusCountForInstance(domain string) (int, error) { | ||
q := ps.conn.Model(&[]*gtsmodel.Status{}) | ||
|
||
if domain == ps.config.Host { | ||
// if the domain is *this* domain, just count where local is true | ||
q = q.Where("local = ?", true) | ||
} else { | ||
// join on the domain of the account | ||
q = q.Join("JOIN accounts AS account ON account.id = status.account_id"). | ||
Where("account.domain = ?", domain) | ||
} | ||
|
||
return q.Count() | ||
} | ||
|
||
func (ps *postgresService) GetDomainCountForInstance(domain string) (int, error) { | ||
q := ps.conn.Model(&[]*gtsmodel.Instance{}) | ||
|
||
if domain == ps.config.Host { | ||
// if the domain is *this* domain, just count other instances it knows about | ||
// TODO: exclude domains that are blocked or silenced | ||
q = q.Where("domain != ?", domain) | ||
} else { | ||
// TODO: implement federated domain counting properly for remote domains | ||
return 0, nil | ||
} | ||
|
||
return q.Count() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.