2021-07-05 14:23:03 +03:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 20:42:19 +03:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-07-05 14:23:03 +03: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 (
|
2021-08-25 16:34:33 +03:00
|
|
|
"context"
|
2021-07-05 14:23:03 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
|
2021-08-31 16:59:12 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-07-05 14:23:03 +03:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-12-07 15:31:39 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2022-09-26 12:56:01 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-06-08 21:38:03 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-07-05 14:23:03 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-01-09 20:41:22 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
2021-08-31 16:59:12 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2021-07-26 21:25:54 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-09-11 14:19:06 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-09-01 19:29:25 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
2021-07-05 14:23:03 +03:00
|
|
|
)
|
|
|
|
|
2022-06-08 21:38:03 +03:00
|
|
|
func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) {
|
2021-07-05 14:23:03 +03:00
|
|
|
if form.Discoverable != nil {
|
2022-08-15 13:35:05 +03:00
|
|
|
account.Discoverable = form.Discoverable
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Bot != nil {
|
2022-08-15 13:35:05 +03:00
|
|
|
account.Bot = form.Bot
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-26 12:56:01 +03:00
|
|
|
var updateEmojis bool
|
|
|
|
|
2021-07-05 14:23:03 +03:00
|
|
|
if form.DisplayName != nil {
|
2021-09-01 19:29:25 +03:00
|
|
|
if err := validate.DisplayName(*form.DisplayName); err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2022-05-26 12:37:13 +03:00
|
|
|
account.DisplayName = text.SanitizePlaintext(*form.DisplayName)
|
2022-09-26 12:56:01 +03:00
|
|
|
updateEmojis = true
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Note != nil {
|
2021-09-01 19:29:25 +03:00
|
|
|
if err := validate.Note(*form.Note); err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2022-05-07 18:55:27 +03:00
|
|
|
|
|
|
|
// Set the raw note before processing
|
|
|
|
account.NoteRaw = *form.Note
|
|
|
|
|
|
|
|
// Process note to generate a valid HTML representation
|
2022-11-19 12:07:51 +03:00
|
|
|
note, err := p.processNote(ctx, *form.Note, account)
|
2021-09-11 14:19:06 +03:00
|
|
|
if err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2022-05-07 18:55:27 +03:00
|
|
|
|
|
|
|
// Set updated HTML-ified note
|
2021-09-11 14:19:06 +03:00
|
|
|
account.Note = note
|
2022-09-26 12:56:01 +03:00
|
|
|
updateEmojis = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateEmojis {
|
|
|
|
// account emojis -- treat the sanitized display name and raw
|
|
|
|
// note like one long text for the purposes of deriving emojis
|
|
|
|
accountEmojiShortcodes := util.DeriveEmojisFromText(account.DisplayName + "\n\n" + account.NoteRaw)
|
|
|
|
account.Emojis = make([]*gtsmodel.Emoji, 0, len(accountEmojiShortcodes))
|
|
|
|
account.EmojiIDs = make([]string, 0, len(accountEmojiShortcodes))
|
|
|
|
|
|
|
|
for _, shortcode := range accountEmojiShortcodes {
|
|
|
|
emoji, err := p.db.GetEmojiByShortcodeDomain(ctx, shortcode, "")
|
|
|
|
if err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
|
|
|
log.Errorf("error getting local emoji with shortcode %s: %s", shortcode, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if *emoji.VisibleInPicker && !*emoji.Disabled {
|
|
|
|
account.Emojis = append(account.Emojis, emoji)
|
|
|
|
account.EmojiIDs = append(account.EmojiIDs, emoji.ID)
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Avatar != nil && form.Avatar.Size != 0 {
|
2022-11-08 20:11:06 +03:00
|
|
|
avatarInfo, err := p.UpdateAvatar(ctx, form.Avatar, nil, account.ID)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2021-09-11 14:19:06 +03:00
|
|
|
account.AvatarMediaAttachmentID = avatarInfo.ID
|
|
|
|
account.AvatarMediaAttachment = avatarInfo
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Tracef("new avatar info for account %s is %+v", account.ID, avatarInfo)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Header != nil && form.Header.Size != 0 {
|
2022-11-08 20:11:06 +03:00
|
|
|
headerInfo, err := p.UpdateHeader(ctx, form.Header, nil, account.ID)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2021-09-11 14:19:06 +03:00
|
|
|
account.HeaderMediaAttachmentID = headerInfo.ID
|
|
|
|
account.HeaderMediaAttachment = headerInfo
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Tracef("new header info for account %s is %+v", account.ID, headerInfo)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Locked != nil {
|
2022-08-15 13:35:05 +03:00
|
|
|
account.Locked = form.Locked
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source != nil {
|
|
|
|
if form.Source.Language != nil {
|
2021-09-01 19:29:25 +03:00
|
|
|
if err := validate.Language(*form.Source.Language); err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2021-09-11 14:19:06 +03:00
|
|
|
account.Language = *form.Source.Language
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source.Sensitive != nil {
|
2022-08-15 13:35:05 +03:00
|
|
|
account.Sensitive = form.Source.Sensitive
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.Source.Privacy != nil {
|
2021-09-01 19:29:25 +03:00
|
|
|
if err := validate.Privacy(*form.Source.Privacy); err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorBadRequest(err)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2021-10-04 16:24:19 +03:00
|
|
|
privacy := p.tc.APIVisToVis(apimodel.Visibility(*form.Source.Privacy))
|
2021-09-11 14:19:06 +03:00
|
|
|
account.Privacy = privacy
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2022-08-06 13:09:21 +03:00
|
|
|
|
|
|
|
if form.Source.StatusFormat != nil {
|
|
|
|
if err := validate.StatusFormat(*form.Source.StatusFormat); err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
account.StatusFormat = *form.Source.StatusFormat
|
|
|
|
}
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:14:29 +03:00
|
|
|
if form.CustomCSS != nil {
|
|
|
|
customCSS := *form.CustomCSS
|
|
|
|
if err := validate.CustomCSS(customCSS); err != nil {
|
|
|
|
return nil, gtserror.NewErrorBadRequest(err, err.Error())
|
|
|
|
}
|
|
|
|
account.CustomCSS = text.SanitizePlaintext(customCSS)
|
|
|
|
}
|
|
|
|
|
2022-10-08 15:00:39 +03:00
|
|
|
if form.EnableRSS != nil {
|
|
|
|
account.EnableRSS = form.EnableRSS
|
|
|
|
}
|
|
|
|
|
2022-11-15 21:45:15 +03:00
|
|
|
err := p.db.UpdateAccount(ctx, account)
|
2021-08-25 16:34:33 +03:00
|
|
|
if err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not update account %s: %s", account.ID, err))
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 15:23:11 +03:00
|
|
|
p.clientWorker.Queue(messages.FromClientAPI{
|
2021-08-31 16:59:12 +03:00
|
|
|
APObjectType: ap.ObjectProfile,
|
|
|
|
APActivityType: ap.ActivityUpdate,
|
2022-11-15 21:45:15 +03:00
|
|
|
GTSModel: account,
|
|
|
|
OriginAccount: account,
|
2022-04-28 15:23:11 +03:00
|
|
|
})
|
2021-07-05 14:23:03 +03:00
|
|
|
|
2022-11-15 21:45:15 +03:00
|
|
|
acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, account)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not convert account into apisensitive account: %s", err))
|
2021-07-05 14:23:03 +03: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.
|
2022-11-08 20:11:06 +03:00
|
|
|
func (p *processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) {
|
2022-05-30 15:41:24 +03:00
|
|
|
maxImageSize := config.GetMediaImageMaxSize()
|
2022-09-29 23:50:43 +03:00
|
|
|
if avatar.Size > int64(maxImageSize) {
|
2022-01-11 19:49:14 +03:00
|
|
|
return nil, fmt.Errorf("UpdateAvatar: avatar with size %d exceeded max image size of %d bytes", avatar.Size, maxImageSize)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-11-03 17:03:12 +03:00
|
|
|
dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) {
|
2022-01-23 16:41:58 +03:00
|
|
|
f, err := avatar.Open()
|
2022-09-29 23:50:43 +03:00
|
|
|
return f, avatar.Size, err
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-01-09 20:41:22 +03:00
|
|
|
isAvatar := true
|
2022-01-11 19:49:14 +03:00
|
|
|
ai := &media.AdditionalMediaInfo{
|
2022-11-08 20:11:06 +03:00
|
|
|
Avatar: &isAvatar,
|
|
|
|
Description: description,
|
2022-01-11 19:49:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:50:33 +03:00
|
|
|
processingMedia, err := p.mediaManager.ProcessMedia(ctx, dataFunc, nil, accountID, ai)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2022-01-08 19:17:01 +03:00
|
|
|
return nil, fmt.Errorf("UpdateAvatar: error processing avatar: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-01-11 19:49:14 +03:00
|
|
|
return processingMedia.LoadAttachment(ctx)
|
2021-07-05 14:23:03 +03: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.
|
2022-11-08 20:11:06 +03:00
|
|
|
func (p *processor) UpdateHeader(ctx context.Context, header *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) {
|
2022-05-30 15:41:24 +03:00
|
|
|
maxImageSize := config.GetMediaImageMaxSize()
|
2022-09-29 23:50:43 +03:00
|
|
|
if header.Size > int64(maxImageSize) {
|
2022-01-11 19:49:14 +03:00
|
|
|
return nil, fmt.Errorf("UpdateHeader: header with size %d exceeded max image size of %d bytes", header.Size, maxImageSize)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-11-03 17:03:12 +03:00
|
|
|
dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) {
|
2022-01-23 16:41:58 +03:00
|
|
|
f, err := header.Open()
|
2022-09-29 23:50:43 +03:00
|
|
|
return f, header.Size, err
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2022-01-09 20:41:22 +03:00
|
|
|
isHeader := true
|
2022-01-11 19:49:14 +03:00
|
|
|
ai := &media.AdditionalMediaInfo{
|
2022-01-09 20:41:22 +03:00
|
|
|
Header: &isHeader,
|
2022-01-11 19:49:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:50:33 +03:00
|
|
|
processingMedia, err := p.mediaManager.ProcessMedia(ctx, dataFunc, nil, accountID, ai)
|
2022-01-11 19:49:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("UpdateHeader: error processing header: %s", err)
|
|
|
|
}
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2022-01-08 19:17:01 +03:00
|
|
|
return nil, fmt.Errorf("UpdateHeader: error processing header: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-01-11 19:49:14 +03:00
|
|
|
return processingMedia.LoadAttachment(ctx)
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
2021-09-11 14:19:06 +03:00
|
|
|
|
2022-11-19 12:07:51 +03:00
|
|
|
func (p *processor) processNote(ctx context.Context, note string, account *gtsmodel.Account) (string, error) {
|
2021-09-11 14:19:06 +03:00
|
|
|
if note == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tagStrings := util.DeriveHashtagsFromText(note)
|
2022-11-19 12:07:51 +03:00
|
|
|
tags, err := p.db.TagStringsToTags(ctx, tagStrings, account.ID)
|
2021-09-11 14:19:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:54:56 +03:00
|
|
|
mentionStrings := util.DeriveMentionNamesFromText(note)
|
|
|
|
mentions := []*gtsmodel.Mention{}
|
|
|
|
for _, mentionString := range mentionStrings {
|
2022-11-19 12:07:51 +03:00
|
|
|
mention, err := p.parseMention(ctx, mentionString, account.ID, "")
|
2022-03-29 12:54:56 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mentions = append(mentions, mention)
|
2021-09-11 14:19:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support emojis in account notes
|
|
|
|
// emojiStrings := util.DeriveEmojisFromText(note)
|
|
|
|
// emojis, err := p.db.EmojiStringsToEmojis(ctx, emojiStrings)
|
|
|
|
|
2022-11-19 12:07:51 +03:00
|
|
|
if account.StatusFormat == "markdown" {
|
|
|
|
return p.formatter.FromMarkdown(ctx, note, mentions, tags, nil), nil
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:19:06 +03:00
|
|
|
return p.formatter.FromPlain(ctx, note, mentions, tags), nil
|
|
|
|
}
|