2021-05-17 17:06:58 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-05-17 17:06:58 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-30 11:12:00 +00:00
|
|
|
package processing
|
2021-05-09 12:06:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-05-09 12:06:06 +00:00
|
|
|
"fmt"
|
2022-06-23 14:54:54 +00:00
|
|
|
"sort"
|
2021-05-09 12:06:06 +00:00
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-12-07 12:31:39 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-05-21 13:48:26 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 16:42:28 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-09 12:06:06 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-06-23 14:54:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-07-26 18:25:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2022-06-23 14:54:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-09-01 16:29:25 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
2021-05-09 12:06:06 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) InstanceGet(ctx context.Context, domain string) (*apimodel.Instance, gtserror.WithCode) {
|
2021-05-09 12:06:06 +00:00
|
|
|
i := >smodel.Instance{}
|
2021-08-25 13:34:33 +00:00
|
|
|
if err := p.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: domain}}, i); err != nil {
|
2021-12-07 12:31:39 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", domain, err))
|
2021-05-09 12:06:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:24:19 +00:00
|
|
|
ai, err := p.tc.InstanceToAPIInstance(ctx, i)
|
2021-05-09 12:06:06 +00:00
|
|
|
if err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
2021-05-09 12:06:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:34:27 +00:00
|
|
|
return ai, nil
|
2021-05-09 12:06:06 +00:00
|
|
|
}
|
2021-06-23 14:35:57 +00:00
|
|
|
|
2022-06-23 14:54:54 +00:00
|
|
|
func (p *processor) InstancePeersGet(ctx context.Context, authed *oauth.Auth, includeSuspended bool, includeOpen bool, flat bool) (interface{}, gtserror.WithCode) {
|
|
|
|
domains := []*apimodel.Domain{}
|
|
|
|
|
|
|
|
if includeOpen {
|
|
|
|
if !config.GetInstanceExposePeers() && (authed.Account == nil || authed.User == nil) {
|
|
|
|
err := fmt.Errorf("peers open query requires an authenticated account/user")
|
|
|
|
return nil, gtserror.NewErrorUnauthorized(err, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
instances, err := p.db.GetInstancePeers(ctx, false)
|
|
|
|
if err != nil && err != db.ErrNoEntries {
|
|
|
|
err = fmt.Errorf("error selecting instance peers: %s", err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range instances {
|
|
|
|
domain := &apimodel.Domain{Domain: i.Domain}
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeSuspended {
|
|
|
|
if !config.GetInstanceExposeSuspended() && (authed.Account == nil || authed.User == nil) {
|
|
|
|
err := fmt.Errorf("peers suspended query requires an authenticated account/user")
|
|
|
|
return nil, gtserror.NewErrorUnauthorized(err, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
domainBlocks := []*gtsmodel.DomainBlock{}
|
|
|
|
if err := p.db.GetAll(ctx, &domainBlocks); err != nil && err != db.ErrNoEntries {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range domainBlocks {
|
2022-08-15 10:35:05 +00:00
|
|
|
if *d.Obfuscate {
|
2022-06-23 14:54:54 +00:00
|
|
|
d.Domain = obfuscate(d.Domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := &apimodel.Domain{
|
|
|
|
Domain: d.Domain,
|
|
|
|
SuspendedAt: util.FormatISO8601(d.CreatedAt),
|
|
|
|
PublicComment: d.PublicComment,
|
|
|
|
}
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(domains, func(i, j int) bool {
|
|
|
|
return domains[i].Domain < domains[j].Domain
|
|
|
|
})
|
|
|
|
|
|
|
|
if flat {
|
|
|
|
flattened := []string{}
|
|
|
|
for _, d := range domains {
|
|
|
|
flattened = append(flattened, d.Domain)
|
|
|
|
}
|
|
|
|
return flattened, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return domains, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSettingsUpdateRequest) (*apimodel.Instance, gtserror.WithCode) {
|
2021-06-23 14:35:57 +00:00
|
|
|
// fetch the instance entry from the db for processing
|
|
|
|
i := >smodel.Instance{}
|
2022-05-30 12:41:24 +00:00
|
|
|
host := config.GetHost()
|
2021-12-07 12:31:39 +00:00
|
|
|
if err := p.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: host}}, i); err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", host, err))
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the instance account from the db for processing
|
2021-08-25 13:34:33 +00:00
|
|
|
ia, err := p.db.GetInstanceAccount(ctx, "")
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-12-07 12:31:39 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance account %s: %s", host, err))
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns := []string{}
|
|
|
|
|
2021-06-23 14:35:57 +00:00
|
|
|
// validate & update site title if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.Title != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.SiteTitle(*form.Title); err != nil {
|
2021-06-23 14:35:57 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err))
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "title")
|
2022-05-26 09:37:13 +00:00
|
|
|
i.Title = text.SanitizePlaintext(*form.Title) // don't allow html in site title
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site contact account if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.ContactUsername != nil {
|
2021-06-23 14:35:57 +00:00
|
|
|
// make sure the account with the given username exists in the db
|
2022-09-02 09:56:33 +00:00
|
|
|
contactAccount, err := p.db.GetAccountByUsernameDomain(ctx, *form.ContactUsername, "")
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-07-08 13:05:19 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
// make sure it has a user associated with it
|
2022-10-03 08:46:11 +00:00
|
|
|
contactUser, err := p.db.GetUserByAccountID(ctx, contactAccount.ID)
|
|
|
|
if err != nil {
|
2021-07-08 13:05:19 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("user for account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
// 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())
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
if !*contactUser.Approved {
|
2021-06-23 14:35:57 +00:00
|
|
|
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
|
2022-08-15 10:35:05 +00:00
|
|
|
if !*contactUser.Admin && !*contactUser.Moderator {
|
2021-06-23 14:35:57 +00:00
|
|
|
err := fmt.Errorf("user of selected contact account %s is neither admin nor moderator", contactAccount.Username)
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "contact_account_id")
|
2021-06-23 14:35:57 +00:00
|
|
|
i.ContactAccountID = contactAccount.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site contact email if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.ContactEmail != nil {
|
2022-06-24 08:43:21 +00:00
|
|
|
contactEmail := *form.ContactEmail
|
|
|
|
if contactEmail != "" {
|
|
|
|
if err := validate.Email(contactEmail); err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "contact_email")
|
2022-06-24 08:43:21 +00:00
|
|
|
i.ContactEmail = contactEmail
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site short description if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.ShortDescription != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.SiteShortDescription(*form.ShortDescription); err != nil {
|
2021-06-23 14:35:57 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "short_description")
|
2021-07-26 18:25:54 +00:00
|
|
|
i.ShortDescription = text.SanitizeHTML(*form.ShortDescription) // html is OK in site description, but we should sanitize it
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site description if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.Description != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.SiteDescription(*form.Description); err != nil {
|
2021-06-23 14:35:57 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "description")
|
2021-07-26 18:25:54 +00:00
|
|
|
i.Description = text.SanitizeHTML(*form.Description) // html is OK in site description, but we should sanitize it
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site terms if it's set on the form
|
2021-07-08 13:05:19 +00:00
|
|
|
if form.Terms != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.SiteTerms(*form.Terms); err != nil {
|
2021-06-23 14:35:57 +00:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
updatingColumns = append(updatingColumns, "terms")
|
2021-07-26 18:25:54 +00:00
|
|
|
i.Terms = text.SanitizeHTML(*form.Terms) // html is OK in site terms, but we should sanitize it
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// process avatar if provided
|
|
|
|
if form.Avatar != nil && form.Avatar.Size != 0 {
|
2021-08-25 13:34:33 +00:00
|
|
|
_, err := p.accountProcessor.UpdateAvatar(ctx, form.Avatar, ia.ID)
|
2021-06-23 14:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, "error processing avatar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process header if provided
|
|
|
|
if form.Header != nil && form.Header.Size != 0 {
|
2021-08-25 13:34:33 +00:00
|
|
|
_, err := p.accountProcessor.UpdateHeader(ctx, form.Header, ia.ID)
|
2021-06-23 14:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, "error processing header")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:48 +00:00
|
|
|
if err := p.db.UpdateByID(ctx, i, i.ID, updatingColumns...); err != nil {
|
2021-12-07 12:31:39 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", host, err))
|
2021-06-23 14:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:24:19 +00:00
|
|
|
ai, err := p.tc.InstanceToAPIInstance(ctx, i)
|
2021-06-23 14:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai, nil
|
|
|
|
}
|
2022-06-23 14:54:54 +00:00
|
|
|
|
|
|
|
func obfuscate(domain string) string {
|
|
|
|
obfuscated := make([]rune, len(domain))
|
|
|
|
for i, r := range domain {
|
|
|
|
if i%3 == 1 || i%5 == 1 {
|
|
|
|
obfuscated[i] = '*'
|
|
|
|
} else {
|
|
|
|
obfuscated[i] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(obfuscated)
|
|
|
|
}
|