2023-03-12 15:00:57 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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-17 18:06:58 +01:00
|
|
|
|
2021-05-30 12:12:00 +01:00
|
|
|
package processing
|
2021-05-09 13:06:06 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2021-05-09 13:06:06 +01:00
|
|
|
"fmt"
|
2022-06-23 15:54:54 +01:00
|
|
|
"sort"
|
2021-05-09 13:06:06 +01: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 14:48:26 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 17:42:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-09 13:06:06 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2023-05-07 18:53:21 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-07-26 19:25:54 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2022-06-23 15:54:54 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-09-01 17:29:25 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
2021-05-09 13:06:06 +01:00
|
|
|
)
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) getThisInstance(ctx context.Context) (*gtsmodel.Instance, error) {
|
2023-07-07 10:34:12 +01:00
|
|
|
instance, err := p.state.DB.GetInstance(ctx, config.GetHost())
|
|
|
|
if err != nil {
|
2023-02-02 13:08:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-07 10:34:12 +01:00
|
|
|
|
|
|
|
return instance, nil
|
2023-02-02 13:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) InstanceGetV1(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode) {
|
2023-02-02 13:08:13 +00:00
|
|
|
i, err := p.getThisInstance(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err))
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:44:11 +01:00
|
|
|
ai, err := p.converter.InstanceToAPIV1Instance(ctx, i)
|
2023-02-02 13:08:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai, nil
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) InstanceGetV2(ctx context.Context) (*apimodel.InstanceV2, gtserror.WithCode) {
|
2023-02-02 13:08:13 +00:00
|
|
|
i, err := p.getThisInstance(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err))
|
2021-05-09 13:06:06 +01:00
|
|
|
}
|
|
|
|
|
2023-09-23 17:44:11 +01:00
|
|
|
ai, err := p.converter.InstanceToAPIV2Instance(ctx, i)
|
2021-05-09 13:06:06 +01:00
|
|
|
if err != nil {
|
2021-06-13 17:42:28 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
2021-05-09 13:06:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 19:34:27 +01:00
|
|
|
return ai, nil
|
2021-05-09 13:06:06 +01:00
|
|
|
}
|
2021-06-23 15:35:57 +01:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) InstancePeersGet(ctx context.Context, includeSuspended bool, includeOpen bool, flat bool) (interface{}, gtserror.WithCode) {
|
2022-06-23 15:54:54 +01:00
|
|
|
domains := []*apimodel.Domain{}
|
|
|
|
|
|
|
|
if includeOpen {
|
2023-03-01 18:26:53 +00:00
|
|
|
instances, err := p.state.DB.GetInstancePeers(ctx, false)
|
2022-06-23 15:54:54 +01:00
|
|
|
if err != nil && err != db.ErrNoEntries {
|
|
|
|
err = fmt.Errorf("error selecting instance peers: %s", err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range instances {
|
2023-05-07 18:53:21 +01:00
|
|
|
// Domain may be in Punycode,
|
|
|
|
// de-punify it just in case.
|
|
|
|
d, err := util.DePunify(i.Domain)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "couldn't depunify domain %s: %s", i.Domain, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = append(domains, &apimodel.Domain{Domain: d})
|
2022-06-23 15:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeSuspended {
|
|
|
|
domainBlocks := []*gtsmodel.DomainBlock{}
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.GetAll(ctx, &domainBlocks); err != nil && err != db.ErrNoEntries {
|
2022-06-23 15:54:54 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-05-07 18:53:21 +01:00
|
|
|
for _, domainBlock := range domainBlocks {
|
|
|
|
// Domain may be in Punycode,
|
|
|
|
// de-punify it just in case.
|
|
|
|
d, err := util.DePunify(domainBlock.Domain)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "couldn't depunify domain %s: %s", domainBlock.Domain, err)
|
|
|
|
continue
|
2022-06-23 15:54:54 +01:00
|
|
|
}
|
|
|
|
|
2023-05-07 18:53:21 +01:00
|
|
|
if *domainBlock.Obfuscate {
|
|
|
|
// Obfuscate the de-punified version.
|
|
|
|
d = obfuscate(d)
|
2022-06-23 15:54:54 +01:00
|
|
|
}
|
2023-05-07 18:53:21 +01:00
|
|
|
|
|
|
|
domains = append(domains, &apimodel.Domain{
|
|
|
|
Domain: d,
|
|
|
|
SuspendedAt: util.FormatISO8601(domainBlock.CreatedAt),
|
|
|
|
PublicComment: domainBlock.PublicComment,
|
|
|
|
})
|
2022-06-23 15:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:33:15 +01:00
|
|
|
func (p *Processor) InstanceGetRules(ctx context.Context) ([]apimodel.InstanceRule, gtserror.WithCode) {
|
|
|
|
i, err := p.getThisInstance(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err))
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:44:11 +01:00
|
|
|
return p.converter.InstanceRulesToAPIRules(i.Rules), nil
|
2023-08-19 13:33:15 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSettingsUpdateRequest) (*apimodel.InstanceV1, gtserror.WithCode) {
|
2021-06-23 15:35:57 +01:00
|
|
|
// fetch the instance entry from the db for processing
|
2022-05-30 13:41:24 +01:00
|
|
|
host := config.GetHost()
|
2023-07-07 10:34:12 +01:00
|
|
|
|
|
|
|
instance, err := p.state.DB.GetInstance(ctx, host)
|
|
|
|
if err != nil {
|
2021-12-07 12:31:39 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", host, err))
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the instance account from the db for processing
|
2023-03-01 18:26:53 +00:00
|
|
|
ia, err := p.state.DB.GetInstanceAccount(ctx, "")
|
2021-08-20 11:26:56 +01: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 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns := []string{}
|
|
|
|
|
2021-06-23 15:35:57 +01:00
|
|
|
// validate & update site title if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.Title != nil {
|
2021-09-01 17:29:25 +01:00
|
|
|
if err := validate.SiteTitle(*form.Title); err != nil {
|
2021-06-23 15:35:57 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err))
|
|
|
|
}
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "title")
|
2023-08-11 13:40:11 +01:00
|
|
|
instance.Title = text.SanitizeToPlaintext(*form.Title) // don't allow html in site title
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site contact account if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.ContactUsername != nil {
|
2021-06-23 15:35:57 +01:00
|
|
|
// make sure the account with the given username exists in the db
|
2023-03-01 18:26:53 +00:00
|
|
|
contactAccount, err := p.state.DB.GetAccountByUsernameDomain(ctx, *form.ContactUsername, "")
|
2021-08-20 11:26:56 +01:00
|
|
|
if err != nil {
|
2021-07-08 14:05:19 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
// make sure it has a user associated with it
|
2023-03-01 18:26:53 +00:00
|
|
|
contactUser, err := p.state.DB.GetUserByAccountID(ctx, contactAccount.ID)
|
2022-10-03 09:46:11 +01:00
|
|
|
if err != nil {
|
2021-07-08 14:05:19 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("user for account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 15:35:57 +01: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 11:35:05 +01:00
|
|
|
if !*contactUser.Approved {
|
2021-06-23 15:35:57 +01: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 11:35:05 +01:00
|
|
|
if !*contactUser.Admin && !*contactUser.Moderator {
|
2021-06-23 15:35:57 +01: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 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "contact_account_id")
|
2023-07-07 10:34:12 +01:00
|
|
|
instance.ContactAccountID = contactAccount.ID
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site contact email if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.ContactEmail != nil {
|
2022-06-24 09:43:21 +01:00
|
|
|
contactEmail := *form.ContactEmail
|
|
|
|
if contactEmail != "" {
|
|
|
|
if err := validate.Email(contactEmail); err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "contact_email")
|
2023-07-07 10:34:12 +01:00
|
|
|
instance.ContactEmail = contactEmail
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site short description if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.ShortDescription != nil {
|
2021-09-01 17:29:25 +01:00
|
|
|
if err := validate.SiteShortDescription(*form.ShortDescription); err != nil {
|
2021-06-23 15:35:57 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "short_description")
|
2023-08-11 13:40:11 +01:00
|
|
|
instance.ShortDescription = text.SanitizeToHTML(*form.ShortDescription) // html is OK in site description, but we should sanitize it
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site description if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.Description != nil {
|
2021-09-01 17:29:25 +01:00
|
|
|
if err := validate.SiteDescription(*form.Description); err != nil {
|
2021-06-23 15:35:57 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "description")
|
2023-08-11 13:40:11 +01:00
|
|
|
instance.Description = text.SanitizeToHTML(*form.Description) // html is OK in site description, but we should sanitize it
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site terms if it's set on the form
|
2021-07-08 14:05:19 +01:00
|
|
|
if form.Terms != nil {
|
2021-09-01 17:29:25 +01:00
|
|
|
if err := validate.SiteTerms(*form.Terms); err != nil {
|
2021-06-23 15:35:57 +01:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns = append(updatingColumns, "terms")
|
2023-08-11 13:40:11 +01:00
|
|
|
instance.Terms = text.SanitizeToHTML(*form.Terms) // html is OK in site terms, but we should sanitize it
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 17:11:06 +00:00
|
|
|
var updateInstanceAccount bool
|
|
|
|
|
2021-06-23 15:35:57 +01:00
|
|
|
if form.Avatar != nil && form.Avatar.Size != 0 {
|
2023-02-04 14:53:11 +00:00
|
|
|
// process instance avatar image + description
|
2023-02-22 15:05:26 +00:00
|
|
|
avatarInfo, err := p.account.UpdateAvatar(ctx, form.Avatar, form.AvatarDescription, ia.ID)
|
2021-06-23 15:35:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, "error processing avatar")
|
|
|
|
}
|
2022-11-08 17:11:06 +00:00
|
|
|
ia.AvatarMediaAttachmentID = avatarInfo.ID
|
|
|
|
ia.AvatarMediaAttachment = avatarInfo
|
|
|
|
updateInstanceAccount = true
|
2023-02-04 14:53:11 +00:00
|
|
|
} else if form.AvatarDescription != nil && ia.AvatarMediaAttachment != nil {
|
|
|
|
// process just the description for the existing avatar
|
|
|
|
ia.AvatarMediaAttachment.Description = *form.AvatarDescription
|
2023-03-03 23:02:23 +00:00
|
|
|
if err := p.state.DB.UpdateAttachment(ctx, ia.AvatarMediaAttachment, "description"); err != nil {
|
2023-02-04 14:53:11 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance avatar description: %s", err))
|
|
|
|
}
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Header != nil && form.Header.Size != 0 {
|
2023-02-04 14:53:11 +00:00
|
|
|
// process instance header image
|
2023-02-22 15:05:26 +00:00
|
|
|
headerInfo, err := p.account.UpdateHeader(ctx, form.Header, nil, ia.ID)
|
2021-06-23 15:35:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, "error processing header")
|
|
|
|
}
|
2022-11-08 17:11:06 +00:00
|
|
|
ia.HeaderMediaAttachmentID = headerInfo.ID
|
|
|
|
ia.HeaderMediaAttachment = headerInfo
|
|
|
|
updateInstanceAccount = true
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 17:11:06 +00:00
|
|
|
if updateInstanceAccount {
|
|
|
|
// if either avatar or header is updated, we need
|
|
|
|
// to update the instance account that stores them
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.UpdateAccount(ctx, ia); err != nil {
|
2022-11-08 17:11:06 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance account: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(updatingColumns) != 0 {
|
2023-07-07 10:34:12 +01:00
|
|
|
if err := p.state.DB.UpdateInstance(ctx, instance, updatingColumns...); err != nil {
|
2022-11-08 17:11:06 +00:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", host, err))
|
|
|
|
}
|
2021-06-23 15:35:57 +01:00
|
|
|
}
|
|
|
|
|
2023-09-23 17:44:11 +01:00
|
|
|
ai, err := p.converter.InstanceToAPIV1Instance(ctx, instance)
|
2021-06-23 15:35:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai, nil
|
|
|
|
}
|
2022-06-23 15:54:54 +01: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)
|
|
|
|
}
|