2021-05-17 19:06:58 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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 13:12:00 +02:00
|
|
|
package processing
|
2021-05-09 14:06:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-05-21 15:48:26 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 18:42:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-09 14:06:06 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-07-26 20:25:54 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-06-23 16:35:57 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-05-09 14:06:06 +02:00
|
|
|
)
|
|
|
|
|
2021-06-13 18:42:28 +02:00
|
|
|
func (p *processor) InstanceGet(domain string) (*apimodel.Instance, gtserror.WithCode) {
|
2021-05-09 14:06:06 +02:00
|
|
|
i := >smodel.Instance{}
|
2021-05-21 15:48:26 +02:00
|
|
|
if err := p.db.GetWhere([]db.Where{{Key: "domain", Value: domain}}, i); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", p.config.Host, err))
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ai, err := p.tc.InstanceToMasto(i)
|
|
|
|
if err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err))
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:34:27 +02:00
|
|
|
return ai, nil
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
2021-06-23 16:35:57 +02:00
|
|
|
|
|
|
|
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
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.Title != nil {
|
|
|
|
if err := util.ValidateSiteTitle(*form.Title); err != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("site title invalid: %s", err))
|
|
|
|
}
|
2021-07-26 20:25:54 +02:00
|
|
|
i.Title = text.RemoveHTML(*form.Title) // don't allow html in site title
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site contact account if it's set on the form
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.ContactUsername != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
// make sure the account with the given username exists in the db
|
|
|
|
contactAccount := >smodel.Account{}
|
2021-07-08 15:05:19 +02:00
|
|
|
if err := p.db.GetLocalAccountByUsername(*form.ContactUsername, contactAccount); err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
// 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 {
|
2021-07-08 15:05:19 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, fmt.Sprintf("user for account with username %s not retrievable", *form.ContactUsername))
|
2021-06-23 16:35:57 +02: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())
|
|
|
|
}
|
|
|
|
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
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.ContactEmail != nil {
|
|
|
|
if err := util.ValidateEmail(*form.ContactEmail); err != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-07-08 15:05:19 +02:00
|
|
|
i.ContactEmail = *form.ContactEmail
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site short description if it's set on the form
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.ShortDescription != nil {
|
|
|
|
if err := util.ValidateSiteShortDescription(*form.ShortDescription); err != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-07-26 20:25:54 +02:00
|
|
|
i.ShortDescription = text.SanitizeHTML(*form.ShortDescription) // html is OK in site description, but we should sanitize it
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site description if it's set on the form
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.Description != nil {
|
|
|
|
if err := util.ValidateSiteDescription(*form.Description); err != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-07-26 20:25:54 +02:00
|
|
|
i.Description = text.SanitizeHTML(*form.Description) // html is OK in site description, but we should sanitize it
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate & update site terms if it's set on the form
|
2021-07-08 15:05:19 +02:00
|
|
|
if form.Terms != nil {
|
|
|
|
if err := util.ValidateSiteTerms(*form.Terms); err != nil {
|
2021-06-23 16:35:57 +02:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
2021-07-26 20:25:54 +02:00
|
|
|
i.Terms = text.SanitizeHTML(*form.Terms) // html is OK in site terms, but we should sanitize it
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// process avatar if provided
|
|
|
|
if form.Avatar != nil && form.Avatar.Size != 0 {
|
2021-07-05 13:23:03 +02:00
|
|
|
_, err := p.accountProcessor.UpdateAvatar(form.Avatar, ia.ID)
|
2021-06-23 16:35:57 +02: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-07-05 13:23:03 +02:00
|
|
|
_, err := p.accountProcessor.UpdateHeader(form.Header, ia.ID)
|
2021-06-23 16:35:57 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai, nil
|
|
|
|
}
|