| 
									
										
										
										
											2023-03-12 16:00:57 +01: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-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"mime/multipart" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/media" | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/messages" | 
					
						
							| 
									
										
										
										
											2021-07-26 20:25:54 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/text" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/typeutils" | 
					
						
							| 
									
										
										
										
											2021-09-01 18:29:25 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/validate" | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | func (p *Processor) selectNoteFormatter(contentType string) text.FormatFunc { | 
					
						
							|  |  |  | 	if contentType == "text/markdown" { | 
					
						
							|  |  |  | 		return p.formatter.FromMarkdown | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return p.formatter.FromPlain | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | // Update processes the update of an account with the given form. | 
					
						
							|  |  |  | func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	if form.Discoverable != nil { | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 		account.Discoverable = form.Discoverable | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Bot != nil { | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 		account.Bot = form.Bot | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 	// Via the process of updating the account, | 
					
						
							|  |  |  | 	// it is possible that the emojis used by | 
					
						
							|  |  |  | 	// that account in note/display name/fields | 
					
						
							|  |  |  | 	// may change; we need to keep track of this. | 
					
						
							|  |  |  | 	var emojisChanged bool | 
					
						
							| 
									
										
										
										
											2022-09-26 11:56:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	if form.DisplayName != nil { | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		displayName := *form.DisplayName | 
					
						
							|  |  |  | 		if err := validate.DisplayName(displayName); err != nil { | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parse new display name (always from plaintext). | 
					
						
							|  |  |  | 		account.DisplayName = text.SanitizePlaintext(displayName) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If display name has changed, account emojis may have also changed. | 
					
						
							|  |  |  | 		emojisChanged = true | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Note != nil { | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		note := *form.Note | 
					
						
							|  |  |  | 		if err := validate.Note(note); err != nil { | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Store raw version of the note for now, | 
					
						
							|  |  |  | 		// we'll process the proper version later. | 
					
						
							|  |  |  | 		account.NoteRaw = note | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If note has changed, account emojis may have also changed. | 
					
						
							|  |  |  | 		emojisChanged = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.FieldsAttributes != nil { | 
					
						
							|  |  |  | 		var ( | 
					
						
							|  |  |  | 			fieldsAttributes = *form.FieldsAttributes | 
					
						
							|  |  |  | 			fieldsLen        = len(fieldsAttributes) | 
					
						
							|  |  |  | 			fieldsRaw        = make([]*gtsmodel.Field, 0, fieldsLen) | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, updateField := range fieldsAttributes { | 
					
						
							|  |  |  | 			if updateField.Name == nil || updateField.Value == nil { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var ( | 
					
						
							|  |  |  | 				name  string = *updateField.Name | 
					
						
							|  |  |  | 				value string = *updateField.Value | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if name == "" || value == "" { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Sanitize raw field values. | 
					
						
							|  |  |  | 			fieldRaw := >smodel.Field{ | 
					
						
							|  |  |  | 				Name:  text.SanitizePlaintext(name), | 
					
						
							|  |  |  | 				Value: text.SanitizePlaintext(value), | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			fieldsRaw = append(fieldsRaw, fieldRaw) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Check length of parsed raw fields. | 
					
						
							|  |  |  | 		if err := validate.ProfileFields(fieldsRaw); err != nil { | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-05-07 16:55:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		// OK, new raw fields are valid. | 
					
						
							|  |  |  | 		account.FieldsRaw = fieldsRaw | 
					
						
							|  |  |  | 		account.Fields = make([]*gtsmodel.Field, 0, fieldsLen) // process these in a sec | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If fields have changed, account emojis may also have changed. | 
					
						
							|  |  |  | 		emojisChanged = true | 
					
						
							| 
									
										
										
										
											2023-02-07 08:58:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-07 16:55:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 	if emojisChanged { | 
					
						
							|  |  |  | 		// Use map to deduplicate emojis by their ID. | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 		emojis := make(map[string]*gtsmodel.Emoji) | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Retrieve display name emojis. | 
					
						
							|  |  |  | 		for _, emoji := range p.formatter.FromPlainEmojiOnly( | 
					
						
							|  |  |  | 			ctx, | 
					
						
							|  |  |  | 			p.parseMention, | 
					
						
							|  |  |  | 			account.ID, | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			account.DisplayName, | 
					
						
							|  |  |  | 		).Emojis { | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 			emojis[emoji.ID] = emoji | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-05-07 16:55:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		// Format + set note according to user prefs. | 
					
						
							|  |  |  | 		f := p.selectNoteFormatter(account.StatusContentType) | 
					
						
							|  |  |  | 		formatNoteResult := f(ctx, p.parseMention, account.ID, "", account.NoteRaw) | 
					
						
							|  |  |  | 		account.Note = formatNoteResult.HTML | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		// Retrieve note emojis. | 
					
						
							|  |  |  | 		for _, emoji := range formatNoteResult.Emojis { | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 			emojis[emoji.ID] = emoji | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		// Process the raw fields we stored earlier. | 
					
						
							| 
									
										
										
										
											2023-05-15 12:52:40 +02:00
										 |  |  | 		account.Fields = make([]*gtsmodel.Field, 0, len(account.FieldsRaw)) | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 		for _, fieldRaw := range account.FieldsRaw { | 
					
						
							|  |  |  | 			field := >smodel.Field{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Name stays plain, but we still need to | 
					
						
							|  |  |  | 			// see if there are any emojis set in it. | 
					
						
							|  |  |  | 			field.Name = fieldRaw.Name | 
					
						
							|  |  |  | 			for _, emoji := range p.formatter.FromPlainEmojiOnly( | 
					
						
							|  |  |  | 				ctx, | 
					
						
							|  |  |  | 				p.parseMention, | 
					
						
							|  |  |  | 				account.ID, | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				fieldRaw.Name, | 
					
						
							|  |  |  | 			).Emojis { | 
					
						
							|  |  |  | 				emojis[emoji.ID] = emoji | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Value can be HTML, but we don't want | 
					
						
							|  |  |  | 			// to wrap the result in <p> tags. | 
					
						
							|  |  |  | 			fieldFormatValueResult := p.formatter.FromPlainNoParagraph(ctx, p.parseMention, account.ID, "", fieldRaw.Value) | 
					
						
							|  |  |  | 			field.Value = fieldFormatValueResult.HTML | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Retrieve field emojis. | 
					
						
							|  |  |  | 			for _, emoji := range fieldFormatValueResult.Emojis { | 
					
						
							|  |  |  | 				emojis[emoji.ID] = emoji | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// We're done, append the shiny new field. | 
					
						
							|  |  |  | 			account.Fields = append(account.Fields, field) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		emojisCount := len(emojis) | 
					
						
							|  |  |  | 		account.Emojis = make([]*gtsmodel.Emoji, 0, emojisCount) | 
					
						
							|  |  |  | 		account.EmojiIDs = make([]string, 0, emojisCount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for id, emoji := range emojis { | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 			account.Emojis = append(account.Emojis, emoji) | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 			account.EmojiIDs = append(account.EmojiIDs, id) | 
					
						
							| 
									
										
										
										
											2023-02-20 15:27:41 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Avatar != nil && form.Avatar.Size != 0 { | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		avatarInfo, err := p.UpdateAvatar(ctx, form.Avatar, nil, account.ID) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-11 13:19:06 +02:00
										 |  |  | 		account.AvatarMediaAttachmentID = avatarInfo.ID | 
					
						
							|  |  |  | 		account.AvatarMediaAttachment = avatarInfo | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		log.Tracef(ctx, "new avatar info for account %s is %+v", account.ID, avatarInfo) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Header != nil && form.Header.Size != 0 { | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		headerInfo, err := p.UpdateHeader(ctx, form.Header, nil, account.ID) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-11 13:19:06 +02:00
										 |  |  | 		account.HeaderMediaAttachmentID = headerInfo.ID | 
					
						
							|  |  |  | 		account.HeaderMediaAttachment = headerInfo | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		log.Tracef(ctx, "new header info for account %s is %+v", account.ID, headerInfo) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Locked != nil { | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 		account.Locked = form.Locked | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Source != nil { | 
					
						
							|  |  |  | 		if form.Source.Language != nil { | 
					
						
							| 
									
										
										
										
											2021-09-01 18:29:25 +02:00
										 |  |  | 			if err := validate.Language(*form.Source.Language); err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 				return nil, gtserror.NewErrorBadRequest(err) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-11 13:19:06 +02:00
										 |  |  | 			account.Language = *form.Source.Language | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Source.Sensitive != nil { | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 			account.Sensitive = form.Source.Sensitive | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Source.Privacy != nil { | 
					
						
							| 
									
										
										
										
											2021-09-01 18:29:25 +02:00
										 |  |  | 			if err := validate.Privacy(*form.Source.Privacy); err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 				return nil, gtserror.NewErrorBadRequest(err) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			privacy := typeutils.APIVisToVis(apimodel.Visibility(*form.Source.Privacy)) | 
					
						
							| 
									
										
										
										
											2021-09-11 13:19:06 +02:00
										 |  |  | 			account.Privacy = privacy | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-06 12:09:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 03:06:40 -08:00
										 |  |  | 		if form.Source.StatusContentType != nil { | 
					
						
							|  |  |  | 			if err := validate.StatusContentType(*form.Source.StatusContentType); err != nil { | 
					
						
							| 
									
										
										
										
											2022-08-06 12:09:21 +02:00
										 |  |  | 				return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 03:06:40 -08:00
										 |  |  | 			account.StatusContentType = *form.Source.StatusContentType | 
					
						
							| 
									
										
										
										
											2022-08-06 12:09:21 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 13:14:29 +02: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 14:00:39 +02:00
										 |  |  | 	if form.EnableRSS != nil { | 
					
						
							|  |  |  | 		account.EnableRSS = form.EnableRSS | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	err := p.state.DB.UpdateAccount(ctx, account) | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not update account %s: %s", account.ID, err)) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{ | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 		APObjectType:   ap.ObjectProfile, | 
					
						
							|  |  |  | 		APActivityType: ap.ActivityUpdate, | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | 		GTSModel:       account, | 
					
						
							|  |  |  | 		OriginAccount:  account, | 
					
						
							| 
									
										
										
										
											2022-04-28 13:23:11 +01:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | 	acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, account) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not convert account into apisensitive account: %s", err)) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02: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. | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { | 
					
						
							| 
									
										
										
										
											2022-05-30 13:41:24 +01:00
										 |  |  | 	maxImageSize := config.GetMediaImageMaxSize() | 
					
						
							| 
									
										
										
										
											2022-09-29 21:50:43 +01:00
										 |  |  | 	if avatar.Size > int64(maxImageSize) { | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 		return nil, fmt.Errorf("UpdateAvatar: avatar with size %d exceeded max image size of %d bytes", avatar.Size, maxImageSize) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 	dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) { | 
					
						
							| 
									
										
										
										
											2022-01-23 14:41:58 +01:00
										 |  |  | 		f, err := avatar.Open() | 
					
						
							| 
									
										
										
										
											2022-09-29 21:50:43 +01:00
										 |  |  | 		return f, avatar.Size, err | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 	isAvatar := true | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	ai := &media.AdditionalMediaInfo{ | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		Avatar:      &isAvatar, | 
					
						
							|  |  |  | 		Description: description, | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-28 13:08:35 +01:00
										 |  |  | 	processingMedia, err := p.mediaManager.PreProcessMedia(ctx, dataFunc, accountID, ai) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		return nil, fmt.Errorf("UpdateAvatar: error processing avatar: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	return processingMedia.LoadAttachment(ctx) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02: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. | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) UpdateHeader(ctx context.Context, header *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { | 
					
						
							| 
									
										
										
										
											2022-05-30 13:41:24 +01:00
										 |  |  | 	maxImageSize := config.GetMediaImageMaxSize() | 
					
						
							| 
									
										
										
										
											2022-09-29 21:50:43 +01:00
										 |  |  | 	if header.Size > int64(maxImageSize) { | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 		return nil, fmt.Errorf("UpdateHeader: header with size %d exceeded max image size of %d bytes", header.Size, maxImageSize) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 	dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) { | 
					
						
							| 
									
										
										
										
											2022-01-23 14:41:58 +01:00
										 |  |  | 		f, err := header.Open() | 
					
						
							| 
									
										
										
										
											2022-09-29 21:50:43 +01:00
										 |  |  | 		return f, header.Size, err | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 	isHeader := true | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	ai := &media.AdditionalMediaInfo{ | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 		Header: &isHeader, | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-28 13:08:35 +01:00
										 |  |  | 	processingMedia, err := p.mediaManager.PreProcessMedia(ctx, dataFunc, accountID, ai) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		return nil, fmt.Errorf("UpdateHeader: error processing header: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-11 17:49:14 +01:00
										 |  |  | 	return processingMedia.LoadAttachment(ctx) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | } |