| 
									
										
										
										
											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-08-25 15:34:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | package status | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							| 
									
										
										
										
											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-12-20 15:19:53 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/uris" | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | // Create processes the given form to create a new status, returning the api model representation of that status if it's OK. | 
					
						
							| 
									
										
										
										
											2023-08-07 01:25:54 -07:00
										 |  |  | // | 
					
						
							|  |  |  | // Precondition: the form's fields should have already been validated and normalized by the caller. | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func (p *Processor) Create(ctx context.Context, requestingAccount *gtsmodel.Account, application *gtsmodel.Application, form *apimodel.AdvancedStatusCreateForm) (*apimodel.Status, gtserror.WithCode) { | 
					
						
							|  |  |  | 	// Generate new ID for status. | 
					
						
							|  |  |  | 	statusID := id.NewULID() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Generate necessary URIs for username, to build status URIs. | 
					
						
							|  |  |  | 	accountURIs := uris.GenerateURIsForAccount(requestingAccount.Username) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Get current time. | 
					
						
							|  |  |  | 	now := time.Now() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status := >smodel.Status{ | 
					
						
							|  |  |  | 		ID:                       statusID, | 
					
						
							|  |  |  | 		URI:                      accountURIs.StatusesURI + "/" + statusID, | 
					
						
							|  |  |  | 		URL:                      accountURIs.StatusesURL + "/" + statusID, | 
					
						
							|  |  |  | 		CreatedAt:                now, | 
					
						
							|  |  |  | 		UpdatedAt:                now, | 
					
						
							|  |  |  | 		Local:                    util.Ptr(true), | 
					
						
							|  |  |  | 		Account:                  requestingAccount, | 
					
						
							|  |  |  | 		AccountID:                requestingAccount.ID, | 
					
						
							|  |  |  | 		AccountURI:               requestingAccount.URI, | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 		ActivityStreamsType:      ap.ObjectNote, | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		Sensitive:                &form.Sensitive, | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		CreatedWithApplicationID: application.ID, | 
					
						
							|  |  |  | 		Text:                     form.Status, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if errWithCode := p.processReplyToID(ctx, form, requestingAccount.ID, status); errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if errWithCode := p.processMediaIDs(ctx, form, requestingAccount.ID, status); errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2022-11-05 13:33:38 +01:00
										 |  |  | 		return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if err := processVisibility(form, requestingAccount.Privacy, status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if err := processLanguage(form, requestingAccount.Language, status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if err := p.processContent(ctx, p.parseMention, form, status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// Insert this new status in the database. | 
					
						
							|  |  |  | 	if err := p.state.DB.PutStatus(ctx, status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// send it back to the client API worker for async side-effects. | 
					
						
							| 
									
										
										
										
											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.ObjectNote, | 
					
						
							|  |  |  | 		APActivityType: ap.ActivityCreate, | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		GTSModel:       status, | 
					
						
							|  |  |  | 		OriginAccount:  requestingAccount, | 
					
						
							| 
									
										
										
										
											2022-04-28 13:23:11 +01:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	return p.apiStatus(ctx, status, requestingAccount) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func (p *Processor) processReplyToID(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if form.InReplyToID == "" { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If this status is a reply to another status, we need to do a bit of work to establish whether or not this status can be posted: | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// 1. Does the replied status exist in the database? | 
					
						
							|  |  |  | 	// 2. Is the replied status marked as replyable? | 
					
						
							|  |  |  | 	// 3. Does a block exist between either the current account or the account that posted the status it's replying to? | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// If this is all OK, then we fetch the repliedStatus and the repliedAccount for later processing. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	inReplyTo, err := p.state.DB.GetStatusByID(ctx, form.InReplyToID) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error fetching status %s from db: %w", form.InReplyToID, err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if inReplyTo == nil { | 
					
						
							|  |  |  | 		const text = "cannot reply to status that does not exist" | 
					
						
							|  |  |  | 		return gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if !*inReplyTo.Replyable { | 
					
						
							|  |  |  | 		text := fmt.Sprintf("status %s is marked as not replyable", form.InReplyToID) | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	if blocked, err := p.state.DB.IsEitherBlocked(ctx, thisAccountID, inReplyTo.AccountID); err != nil { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error checking block in db: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} else if blocked { | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		text := fmt.Sprintf("status %s is not replyable", form.InReplyToID) | 
					
						
							|  |  |  | 		return gtserror.NewErrorNotFound(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// Set status fields from inReplyTo. | 
					
						
							|  |  |  | 	status.InReplyToID = inReplyTo.ID | 
					
						
							|  |  |  | 	status.InReplyToURI = inReplyTo.URI | 
					
						
							|  |  |  | 	status.InReplyToAccountID = inReplyTo.AccountID | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func (p *Processor) processMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if form.MediaIDs == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// Get minimum allowed char descriptions. | 
					
						
							|  |  |  | 	minChars := config.GetMediaDescriptionMinChars() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	attachments := []*gtsmodel.MediaAttachment{} | 
					
						
							|  |  |  | 	attachmentIDs := []string{} | 
					
						
							|  |  |  | 	for _, mediaID := range form.MediaIDs { | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		attachment, err := p.state.DB.GetAttachmentByID(ctx, mediaID) | 
					
						
							|  |  |  | 		if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 			err := gtserror.Newf("error fetching media from db: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			return gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		if attachment == nil { | 
					
						
							|  |  |  | 			text := fmt.Sprintf("media %s not found", mediaID) | 
					
						
							|  |  |  | 			return gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		if attachment.AccountID != thisAccountID { | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 			text := fmt.Sprintf("media %s does not belong to account", mediaID) | 
					
						
							|  |  |  | 			return gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if attachment.StatusID != "" || attachment.ScheduledStatusID != "" { | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 			text := fmt.Sprintf("media %s already attached to status", mediaID) | 
					
						
							|  |  |  | 			return gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		if length := len([]rune(attachment.Description)); length < minChars { | 
					
						
							|  |  |  | 			text := fmt.Sprintf("media %s description too short, at least %d required", mediaID, minChars) | 
					
						
							|  |  |  | 			return gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		attachments = append(attachments, attachment) | 
					
						
							|  |  |  | 		attachmentIDs = append(attachmentIDs, attachment.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status.Attachments = attachments | 
					
						
							|  |  |  | 	status.AttachmentIDs = attachmentIDs | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func processVisibility(form *apimodel.AdvancedStatusCreateForm, accountDefaultVis gtsmodel.Visibility, status *gtsmodel.Status) error { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	// by default all flags are set to true | 
					
						
							|  |  |  | 	federated := true | 
					
						
							|  |  |  | 	boostable := true | 
					
						
							|  |  |  | 	replyable := true | 
					
						
							|  |  |  | 	likeable := true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If visibility isn't set on the form, then just take the account default. | 
					
						
							|  |  |  | 	// If that's also not set, take the default for the whole instance. | 
					
						
							|  |  |  | 	var vis gtsmodel.Visibility | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case form.Visibility != "": | 
					
						
							|  |  |  | 		vis = typeutils.APIVisToVis(form.Visibility) | 
					
						
							|  |  |  | 	case accountDefaultVis != "": | 
					
						
							|  |  |  | 		vis = accountDefaultVis | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		vis = gtsmodel.VisibilityDefault | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch vis { | 
					
						
							|  |  |  | 	case gtsmodel.VisibilityPublic: | 
					
						
							|  |  |  | 		// for public, there's no need to change any of the advanced flags from true regardless of what the user filled out | 
					
						
							|  |  |  | 		break | 
					
						
							|  |  |  | 	case gtsmodel.VisibilityUnlocked: | 
					
						
							|  |  |  | 		// for unlocked the user can set any combination of flags they like so look at them all to see if they're set and then apply them | 
					
						
							|  |  |  | 		if form.Federated != nil { | 
					
						
							|  |  |  | 			federated = *form.Federated | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Boostable != nil { | 
					
						
							|  |  |  | 			boostable = *form.Boostable | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Replyable != nil { | 
					
						
							|  |  |  | 			replyable = *form.Replyable | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Likeable != nil { | 
					
						
							|  |  |  | 			likeable = *form.Likeable | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case gtsmodel.VisibilityFollowersOnly, gtsmodel.VisibilityMutualsOnly: | 
					
						
							|  |  |  | 		// for followers or mutuals only, boostable will *always* be false, but the other fields can be set so check and apply them | 
					
						
							|  |  |  | 		boostable = false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Federated != nil { | 
					
						
							|  |  |  | 			federated = *form.Federated | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Replyable != nil { | 
					
						
							|  |  |  | 			replyable = *form.Replyable | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if form.Likeable != nil { | 
					
						
							|  |  |  | 			likeable = *form.Likeable | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case gtsmodel.VisibilityDirect: | 
					
						
							|  |  |  | 		// direct is pretty easy: there's only one possible setting so return it | 
					
						
							|  |  |  | 		federated = true | 
					
						
							|  |  |  | 		boostable = false | 
					
						
							|  |  |  | 		replyable = true | 
					
						
							|  |  |  | 		likeable = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status.Visibility = vis | 
					
						
							|  |  |  | 	status.Federated = &federated | 
					
						
							|  |  |  | 	status.Boostable = &boostable | 
					
						
							|  |  |  | 	status.Replyable = &replyable | 
					
						
							|  |  |  | 	status.Likeable = &likeable | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func processLanguage(form *apimodel.AdvancedStatusCreateForm, accountDefaultLanguage string, status *gtsmodel.Status) error { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if form.Language != "" { | 
					
						
							|  |  |  | 		status.Language = form.Language | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		status.Language = accountDefaultLanguage | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if status.Language == "" { | 
					
						
							|  |  |  | 		return errors.New("no language given either in status create form or account default") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | func (p *Processor) processContent(ctx context.Context, parseMention gtsmodel.ParseMentionFunc, form *apimodel.AdvancedStatusCreateForm, status *gtsmodel.Status) error { | 
					
						
							|  |  |  | 	if form.ContentType == "" { | 
					
						
							|  |  |  | 		// If content type wasn't specified, use the author's preferred content-type. | 
					
						
							|  |  |  | 		contentType := apimodel.StatusContentType(status.Account.StatusContentType) | 
					
						
							|  |  |  | 		form.ContentType = contentType | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// format is the currently set text formatting | 
					
						
							|  |  |  | 	// function, according to the provided content-type. | 
					
						
							|  |  |  | 	var format text.FormatFunc | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// formatInput is a shorthand function to format the given input string with the | 
					
						
							|  |  |  | 	// currently set 'formatFunc', passing in all required args and returning result. | 
					
						
							|  |  |  | 	formatInput := func(formatFunc text.FormatFunc, input string) *text.FormatResult { | 
					
						
							|  |  |  | 		return formatFunc(ctx, parseMention, status.AccountID, status.ID, input) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 03:06:40 -08:00
										 |  |  | 	switch form.ContentType { | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// None given / set, | 
					
						
							|  |  |  | 	// use default (plain). | 
					
						
							|  |  |  | 	case "": | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Format status according to text/plain. | 
					
						
							| 
									
										
										
										
											2023-03-02 03:06:40 -08:00
										 |  |  | 	case apimodel.StatusContentTypePlain: | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		format = p.formatter.FromPlain | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Format status according to text/markdown. | 
					
						
							| 
									
										
										
										
											2023-03-02 03:06:40 -08:00
										 |  |  | 	case apimodel.StatusContentTypeMarkdown: | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		format = p.formatter.FromMarkdown | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Unknown. | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 		return fmt.Errorf("invalid status format: %q", form.ContentType) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// Sanitize status text and format. | 
					
						
							|  |  |  | 	contentRes := formatInput(format, form.Status) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// Collect formatted results. | 
					
						
							|  |  |  | 	status.Content = contentRes.HTML | 
					
						
							|  |  |  | 	status.Mentions = append(status.Mentions, contentRes.Mentions...) | 
					
						
							|  |  |  | 	status.Emojis = append(status.Emojis, contentRes.Emojis...) | 
					
						
							|  |  |  | 	status.Tags = append(status.Tags, contentRes.Tags...) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	// From here-on-out just use emoji-only | 
					
						
							|  |  |  | 	// plain-text formatting as the FormatFunc. | 
					
						
							|  |  |  | 	format = p.formatter.FromPlainEmojiOnly | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Sanitize content warning and format. | 
					
						
							|  |  |  | 	spoiler := text.SanitizeToPlaintext(form.SpoilerText) | 
					
						
							|  |  |  | 	warningRes := formatInput(format, spoiler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Collect formatted results. | 
					
						
							|  |  |  | 	status.ContentWarning = warningRes.HTML | 
					
						
							|  |  |  | 	status.Emojis = append(status.Emojis, warningRes.Emojis...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Gather all the database IDs from each of the gathered status mentions, tags, and emojis. | 
					
						
							|  |  |  | 	status.MentionIDs = gatherIDs(status.Mentions, func(mention *gtsmodel.Mention) string { return mention.ID }) | 
					
						
							|  |  |  | 	status.TagIDs = gatherIDs(status.Tags, func(tag *gtsmodel.Tag) string { return tag.ID }) | 
					
						
							|  |  |  | 	status.EmojiIDs = gatherIDs(status.Emojis, func(emoji *gtsmodel.Emoji) string { return emoji.ID }) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // gatherIDs is a small utility function to gather IDs from a slice of type T. | 
					
						
							|  |  |  | func gatherIDs[T any](in []T, getID func(T) string) []string { | 
					
						
							|  |  |  | 	if getID == nil { | 
					
						
							|  |  |  | 		// move nil check out loop. | 
					
						
							|  |  |  | 		panic("nil getID function") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ids := make([]string, len(in)) | 
					
						
							|  |  |  | 	for i, t := range in { | 
					
						
							|  |  |  | 		ids[i] = getID(t) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ids | 
					
						
							|  |  |  | } |