2021-07-05 11:23:03 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-07-05 11:23:03 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-07-05 11:23:03 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
|
2021-12-07 12:31:39 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2021-08-31 13:59:12 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-07-05 11:23:03 +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-07-05 11:23:03 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-01-09 17:41:22 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
2021-08-31 13:59:12 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2021-07-26 18:25:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-09-11 11:19:06 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-09-01 16:29:25 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
2021-07-05 11:23:03 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, error) {
|
2021-10-11 12:37:33 +00:00
|
|
|
l := logrus.WithField("func", "AccountUpdate")
|
2021-07-05 11:23:03 +00:00
|
|
|
|
|
|
|
if form.Discoverable != nil {
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Discoverable = *form.Discoverable
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Bot != nil {
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Bot = *form.Bot
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.DisplayName != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.DisplayName(*form.DisplayName); err != nil {
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
account.DisplayName = text.RemoveHTML(*form.DisplayName)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Note != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.Note(*form.Note); err != nil {
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
note, err := p.processNote(ctx, *form.Note, account.ID)
|
|
|
|
if err != nil {
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Note = note
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Avatar != nil && form.Avatar.Size != 0 {
|
2021-08-25 13:34:33 +00:00
|
|
|
avatarInfo, err := p.UpdateAvatar(ctx, form.Avatar, account.ID)
|
2021-07-05 11:23:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
account.AvatarMediaAttachmentID = avatarInfo.ID
|
|
|
|
account.AvatarMediaAttachment = avatarInfo
|
2021-07-05 11:23:03 +00:00
|
|
|
l.Tracef("new avatar info for account %s is %+v", account.ID, avatarInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Header != nil && form.Header.Size != 0 {
|
2021-08-25 13:34:33 +00:00
|
|
|
headerInfo, err := p.UpdateHeader(ctx, form.Header, account.ID)
|
2021-07-05 11:23:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
account.HeaderMediaAttachmentID = headerInfo.ID
|
|
|
|
account.HeaderMediaAttachment = headerInfo
|
2021-07-05 11:23:03 +00:00
|
|
|
l.Tracef("new header info for account %s is %+v", account.ID, headerInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Locked != nil {
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Locked = *form.Locked
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source != nil {
|
|
|
|
if form.Source.Language != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.Language(*form.Source.Language); err != nil {
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Language = *form.Source.Language
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source.Sensitive != nil {
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Sensitive = *form.Source.Sensitive
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source.Privacy != nil {
|
2021-09-01 16:29:25 +00:00
|
|
|
if err := validate.Privacy(*form.Source.Privacy); err != nil {
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-04 13:24:19 +00:00
|
|
|
privacy := p.tc.APIVisToVis(apimodel.Visibility(*form.Source.Privacy))
|
2021-09-11 11:19:06 +00:00
|
|
|
account.Privacy = privacy
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 11:19:06 +00:00
|
|
|
updatedAccount, err := p.db.UpdateAccount(ctx, account)
|
2021-08-25 13:34:33 +00:00
|
|
|
if err != nil {
|
2021-09-11 11:19:06 +00:00
|
|
|
return nil, fmt.Errorf("could not update account %s: %s", account.ID, err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 13:59:12 +00:00
|
|
|
p.fromClientAPI <- messages.FromClientAPI{
|
|
|
|
APObjectType: ap.ObjectProfile,
|
|
|
|
APActivityType: ap.ActivityUpdate,
|
2021-07-05 11:23:03 +00:00
|
|
|
GTSModel: updatedAccount,
|
|
|
|
OriginAccount: updatedAccount,
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:24:19 +00:00
|
|
|
acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, updatedAccount)
|
2021-07-05 11:23:03 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return nil, fmt.Errorf("could not convert account into apisensitive account: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
return acctSensitive, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAvatar does the dirty work of checking the avatar part of an account update form,
|
|
|
|
// parsing and checking the image, and doing the necessary updates in the database for this to become
|
|
|
|
// the account's new avatar image.
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, accountID string) (*gtsmodel.MediaAttachment, error) {
|
2021-07-05 11:23:03 +00:00
|
|
|
var err error
|
2021-12-07 12:31:39 +00:00
|
|
|
maxImageSize := viper.GetInt(config.Keys.MediaImageMaxSize)
|
|
|
|
if int(avatar.Size) > maxImageSize {
|
2022-01-08 16:17:01 +00:00
|
|
|
err = fmt.Errorf("UpdateAvatar: avatar with size %d exceeded max image size of %d bytes", avatar.Size, maxImageSize)
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := avatar.Open()
|
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateAvatar: could not read provided avatar: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract the bytes
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
size, err := io.Copy(buf, f)
|
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateAvatar: could not read provided avatar: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
if size == 0 {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, errors.New("UpdateAvatar: could not read provided avatar: size 0 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're done with the FileHeader now
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, fmt.Errorf("UpdateAvatar: error closing multipart fileheader: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// do the setting
|
2022-01-09 17:41:22 +00:00
|
|
|
isAvatar := true
|
|
|
|
processingMedia, err := p.mediaManager.ProcessMedia(ctx, buf.Bytes(), accountID, &media.AdditionalInfo{
|
|
|
|
Avatar: &isAvatar,
|
|
|
|
})
|
2021-07-05 11:23:03 +00:00
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateAvatar: error processing avatar: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:36:09 +00:00
|
|
|
return processingMedia.Load(ctx)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateHeader does the dirty work of checking the header part of an account update form,
|
|
|
|
// parsing and checking the image, and doing the necessary updates in the database for this to become
|
|
|
|
// the account's new header image.
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) UpdateHeader(ctx context.Context, header *multipart.FileHeader, accountID string) (*gtsmodel.MediaAttachment, error) {
|
2021-07-05 11:23:03 +00:00
|
|
|
var err error
|
2021-12-07 12:31:39 +00:00
|
|
|
maxImageSize := viper.GetInt(config.Keys.MediaImageMaxSize)
|
|
|
|
if int(header.Size) > maxImageSize {
|
2022-01-08 16:17:01 +00:00
|
|
|
err = fmt.Errorf("UpdateHeader: header with size %d exceeded max image size of %d bytes", header.Size, maxImageSize)
|
2021-07-05 11:23:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := header.Open()
|
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateHeader: could not read provided header: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract the bytes
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
size, err := io.Copy(buf, f)
|
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateHeader: could not read provided header: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
if size == 0 {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, errors.New("UpdateHeader: could not read provided header: size 0 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're done with the FileHeader now
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, fmt.Errorf("UpdateHeader: error closing multipart fileheader: %s", err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// do the setting
|
2022-01-09 17:41:22 +00:00
|
|
|
isHeader := true
|
|
|
|
processingMedia, err := p.mediaManager.ProcessMedia(ctx, buf.Bytes(), accountID, &media.AdditionalInfo{
|
|
|
|
Header: &isHeader,
|
|
|
|
})
|
2021-07-05 11:23:03 +00:00
|
|
|
if err != nil {
|
2022-01-08 16:17:01 +00:00
|
|
|
return nil, fmt.Errorf("UpdateHeader: error processing header: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:36:09 +00:00
|
|
|
return processingMedia.Load(ctx)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
2021-09-11 11:19:06 +00:00
|
|
|
|
|
|
|
func (p *processor) processNote(ctx context.Context, note string, accountID string) (string, error) {
|
|
|
|
if note == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tagStrings := util.DeriveHashtagsFromText(note)
|
|
|
|
tags, err := p.db.TagStringsToTags(ctx, tagStrings, accountID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
mentionStrings := util.DeriveMentionsFromText(note)
|
|
|
|
mentions, err := p.db.MentionStringsToMentions(ctx, mentionStrings, accountID, "")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support emojis in account notes
|
|
|
|
// emojiStrings := util.DeriveEmojisFromText(note)
|
|
|
|
// emojis, err := p.db.EmojiStringsToEmojis(ctx, emojiStrings)
|
|
|
|
|
|
|
|
return p.formatter.FromPlain(ctx, note, mentions, tags), nil
|
|
|
|
}
|