2023-03-12 18:00:57 +03: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 16:34:33 +03:00
|
|
|
|
2021-06-13 19:42:28 +03:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
2021-08-25 16:34:33 +03:00
|
|
|
"context"
|
2023-02-22 18:05:26 +03:00
|
|
|
"errors"
|
2021-06-13 19:42:28 +03:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-08-31 16:59:12 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-06-13 19:42:28 +03:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2023-02-22 18:05:26 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 19:42:28 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2024-02-29 17:20:57 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
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"
|
2023-02-22 18:05:26 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-12-20 17:19:53 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/uris"
|
2023-10-04 15:09:42 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-06-13 19:42:28 +03:00
|
|
|
)
|
|
|
|
|
2023-02-22 18:05:26 +03: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 11:25:54 +03:00
|
|
|
//
|
|
|
|
// Precondition: the form's fields should have already been validated and normalized by the caller.
|
2024-02-29 17:20:57 +03:00
|
|
|
func (p *Processor) Create(
|
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
|
|
|
application *gtsmodel.Application,
|
|
|
|
form *apimodel.AdvancedStatusCreateForm,
|
|
|
|
) (
|
|
|
|
*apimodel.Status,
|
|
|
|
gtserror.WithCode,
|
|
|
|
) {
|
2024-03-22 16:03:46 +03:00
|
|
|
// Ensure account populated; we'll need settings.
|
|
|
|
if err := p.state.DB.PopulateAccount(ctx, requester); err != nil {
|
|
|
|
log.Errorf(ctx, "error(s) populating account, will continue: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// Generate new ID for status.
|
|
|
|
statusID := id.NewULID()
|
|
|
|
|
|
|
|
// Generate necessary URIs for username, to build status URIs.
|
2024-02-29 17:20:57 +03:00
|
|
|
accountURIs := uris.GenerateURIsForAccount(requester.Username)
|
2023-10-04 15:09:42 +03:00
|
|
|
|
|
|
|
// 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),
|
2024-02-29 17:20:57 +03:00
|
|
|
Account: requester,
|
|
|
|
AccountID: requester.ID,
|
|
|
|
AccountURI: requester.URI,
|
2021-08-31 16:59:12 +03:00
|
|
|
ActivityStreamsType: ap.ObjectNote,
|
2023-10-04 15:09:42 +03:00
|
|
|
Sensitive: &form.Sensitive,
|
2021-06-13 19:42:28 +03:00
|
|
|
CreatedWithApplicationID: application.ID,
|
|
|
|
Text: form.Status,
|
|
|
|
}
|
|
|
|
|
2023-11-08 17:32:17 +03:00
|
|
|
if form.Poll != nil {
|
|
|
|
// Update the status AS type to "Question".
|
|
|
|
status.ActivityStreamsType = ap.ActivityQuestion
|
|
|
|
|
|
|
|
// Create new poll for status from form.
|
|
|
|
secs := time.Duration(form.Poll.ExpiresIn)
|
|
|
|
status.Poll = >smodel.Poll{
|
|
|
|
ID: id.NewULID(),
|
|
|
|
Multiple: &form.Poll.Multiple,
|
|
|
|
HideCounts: &form.Poll.HideTotals,
|
|
|
|
Options: form.Poll.Options,
|
|
|
|
StatusID: statusID,
|
|
|
|
Status: status,
|
|
|
|
ExpiresAt: now.Add(secs * time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set poll ID on the status.
|
|
|
|
status.PollID = status.Poll.ID
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
// Check + attach in-reply-to status.
|
|
|
|
if errWithCode := p.processInReplyTo(ctx,
|
|
|
|
requester,
|
|
|
|
status,
|
|
|
|
form.InReplyToID,
|
|
|
|
); errWithCode != nil {
|
2022-06-08 21:38:03 +03:00
|
|
|
return nil, errWithCode
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-10-25 17:04:53 +03:00
|
|
|
if errWithCode := p.processThreadID(ctx, status); errWithCode != nil {
|
|
|
|
return nil, errWithCode
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
if errWithCode := p.processMediaIDs(ctx, form, requester.ID, status); errWithCode != nil {
|
2022-11-05 15:33:38 +03:00
|
|
|
return nil, errWithCode
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2024-03-22 16:03:46 +03:00
|
|
|
if err := processVisibility(form, requester.Settings.Privacy, status); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:46:52 +03:00
|
|
|
// Process policy AFTER visibility as it
|
|
|
|
// relies on status.Visibility being set.
|
|
|
|
if err := processInteractionPolicy(form, requester.Settings, status); err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2024-03-22 16:03:46 +03:00
|
|
|
if err := processLanguage(form, requester.Settings.Language, status); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
if err := p.processContent(ctx, p.parseMention, form, status); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-11-08 17:32:17 +03:00
|
|
|
if status.Poll != nil {
|
|
|
|
// Try to insert the new status poll in the database.
|
|
|
|
if err := p.state.DB.PutPoll(ctx, status.Poll); err != nil {
|
|
|
|
err := gtserror.Newf("error inserting poll in db: %w", err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// Insert this new status in the database.
|
|
|
|
if err := p.state.DB.PutStatus(ctx, status); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// send it back to the client API worker for async side-effects.
|
2024-04-26 15:50:46 +03:00
|
|
|
p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
|
2021-08-31 16:59:12 +03:00
|
|
|
APObjectType: ap.ObjectNote,
|
|
|
|
APActivityType: ap.ActivityCreate,
|
2023-10-04 15:09:42 +03:00
|
|
|
GTSModel: status,
|
2024-04-26 15:50:46 +03:00
|
|
|
Origin: requester,
|
2022-04-28 15:23:11 +03:00
|
|
|
})
|
2021-06-13 19:42:28 +03:00
|
|
|
|
2023-11-08 17:32:17 +03:00
|
|
|
if status.Poll != nil {
|
|
|
|
// Now that the status is inserted, and side effects queued,
|
|
|
|
// attempt to schedule an expiry handler for the status poll.
|
|
|
|
if err := p.polls.ScheduleExpiry(ctx, status.Poll); err != nil {
|
2024-02-29 17:20:57 +03:00
|
|
|
log.Errorf(ctx, "error scheduling poll expiry: %v", err)
|
2023-11-08 17:32:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
return p.c.GetAPIStatus(ctx, requester, status)
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
2023-02-22 18:05:26 +03:00
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
func (p *Processor) processInReplyTo(ctx context.Context, requester *gtsmodel.Account, status *gtsmodel.Status, inReplyToID string) gtserror.WithCode {
|
|
|
|
if inReplyToID == "" {
|
2024-07-24 14:27:42 +03:00
|
|
|
// Not a reply.
|
|
|
|
// Nothing to do.
|
2023-02-22 18:05:26 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
// Fetch target in-reply-to status (checking visibility).
|
|
|
|
inReplyTo, errWithCode := p.c.GetVisibleTargetStatus(ctx,
|
|
|
|
requester,
|
|
|
|
inReplyToID,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if errWithCode != nil {
|
|
|
|
return errWithCode
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
2023-10-04 15:09:42 +03:00
|
|
|
|
2024-02-29 17:20:57 +03:00
|
|
|
// If this is a boost, unwrap it to get source status.
|
|
|
|
inReplyTo, errWithCode = p.c.UnwrapIfBoost(ctx,
|
|
|
|
requester,
|
|
|
|
inReplyTo,
|
|
|
|
)
|
|
|
|
if errWithCode != nil {
|
|
|
|
return errWithCode
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-24 14:27:42 +03:00
|
|
|
// Ensure valid reply target for requester.
|
|
|
|
policyResult, err := p.intFilter.StatusReplyable(ctx,
|
|
|
|
requester,
|
|
|
|
inReplyTo,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("error seeing if status %s is replyable: %w", status.ID, err)
|
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if policyResult.Forbidden() {
|
|
|
|
const errText = "you do not have permission to reply to this status"
|
|
|
|
err := gtserror.New(errText)
|
|
|
|
return gtserror.NewErrorForbidden(err, errText)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive pendingApproval status.
|
|
|
|
var pendingApproval bool
|
|
|
|
switch {
|
|
|
|
case policyResult.WithApproval():
|
|
|
|
// We're allowed to do
|
|
|
|
// this pending approval.
|
|
|
|
pendingApproval = true
|
|
|
|
|
|
|
|
case policyResult.MatchedOnCollection():
|
|
|
|
// We're permitted to do this, but since
|
|
|
|
// we matched due to presence in a followers
|
|
|
|
// or following collection, we should mark
|
|
|
|
// as pending approval and wait for an accept.
|
|
|
|
pendingApproval = true
|
|
|
|
|
|
|
|
case policyResult.Permitted():
|
|
|
|
// We're permitted to do this
|
|
|
|
// based on another kind of match.
|
|
|
|
pendingApproval = false
|
|
|
|
}
|
|
|
|
|
|
|
|
status.PendingApproval = &pendingApproval
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// Set status fields from inReplyTo.
|
|
|
|
status.InReplyToID = inReplyTo.ID
|
2023-10-25 17:04:53 +03:00
|
|
|
status.InReplyTo = inReplyTo
|
2023-10-04 15:09:42 +03:00
|
|
|
status.InReplyToURI = inReplyTo.URI
|
|
|
|
status.InReplyToAccountID = inReplyTo.AccountID
|
2023-02-22 18:05:26 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-25 17:04:53 +03:00
|
|
|
func (p *Processor) processThreadID(ctx context.Context, status *gtsmodel.Status) gtserror.WithCode {
|
2024-02-19 13:50:49 +03:00
|
|
|
// Status takes the thread ID of
|
|
|
|
// whatever it replies to, if set.
|
|
|
|
//
|
|
|
|
// Might not be set if status is local
|
|
|
|
// and replies to a remote status that
|
|
|
|
// doesn't have a thread ID yet.
|
|
|
|
//
|
|
|
|
// If so, we can just thread from this
|
|
|
|
// status onwards instead, since this
|
|
|
|
// is where the relevant part of the
|
|
|
|
// thread starts, from the perspective
|
|
|
|
// of our instance at least.
|
|
|
|
if status.InReplyTo != nil &&
|
|
|
|
status.InReplyTo.ThreadID != "" {
|
|
|
|
// Just inherit threadID from parent.
|
2023-10-25 17:04:53 +03:00
|
|
|
status.ThreadID = status.InReplyTo.ThreadID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:50:49 +03:00
|
|
|
// Mark new thread (or threaded
|
|
|
|
// subsection) starting from here.
|
2023-10-25 17:04:53 +03:00
|
|
|
threadID := id.NewULID()
|
|
|
|
if err := p.state.DB.PutThread(
|
|
|
|
ctx,
|
|
|
|
>smodel.Thread{
|
|
|
|
ID: threadID,
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
err := gtserror.Newf("error inserting new thread in db: %w", err)
|
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Future replies to this status
|
|
|
|
// (if any) will inherit this thread ID.
|
|
|
|
status.ThreadID = threadID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
func (p *Processor) processMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode {
|
2023-02-22 18:05:26 +03:00
|
|
|
if form.MediaIDs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// Get minimum allowed char descriptions.
|
|
|
|
minChars := config.GetMediaDescriptionMinChars()
|
|
|
|
|
2023-02-22 18:05:26 +03:00
|
|
|
attachments := []*gtsmodel.MediaAttachment{}
|
|
|
|
attachmentIDs := []string{}
|
2024-01-19 15:57:29 +03:00
|
|
|
|
2023-02-22 18:05:26 +03:00
|
|
|
for _, mediaID := range form.MediaIDs {
|
2023-10-04 15:09:42 +03: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 18:05:26 +03:00
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
if attachment == nil {
|
|
|
|
text := fmt.Sprintf("media %s not found", mediaID)
|
|
|
|
return gtserror.NewErrorBadRequest(errors.New(text), text)
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:05:26 +03:00
|
|
|
if attachment.AccountID != thisAccountID {
|
2023-10-04 15:09:42 +03:00
|
|
|
text := fmt.Sprintf("media %s does not belong to account", mediaID)
|
|
|
|
return gtserror.NewErrorBadRequest(errors.New(text), text)
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if attachment.StatusID != "" || attachment.ScheduledStatusID != "" {
|
2023-10-04 15:09:42 +03:00
|
|
|
text := fmt.Sprintf("media %s already attached to status", mediaID)
|
|
|
|
return gtserror.NewErrorBadRequest(errors.New(text), text)
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03: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 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
attachments = append(attachments, attachment)
|
|
|
|
attachmentIDs = append(attachmentIDs, attachment.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
status.Attachments = attachments
|
|
|
|
status.AttachmentIDs = attachmentIDs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:46:52 +03:00
|
|
|
func processVisibility(
|
|
|
|
form *apimodel.AdvancedStatusCreateForm,
|
|
|
|
accountDefaultVis gtsmodel.Visibility,
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
) error {
|
2023-02-22 18:05:26 +03:00
|
|
|
switch {
|
2024-07-17 17:46:52 +03:00
|
|
|
// Visibility set on form, use that.
|
2023-02-22 18:05:26 +03:00
|
|
|
case form.Visibility != "":
|
2024-07-17 17:46:52 +03:00
|
|
|
status.Visibility = typeutils.APIVisToVis(form.Visibility)
|
|
|
|
|
|
|
|
// Fall back to account default.
|
2023-02-22 18:05:26 +03:00
|
|
|
case accountDefaultVis != "":
|
2024-07-17 17:46:52 +03:00
|
|
|
status.Visibility = accountDefaultVis
|
|
|
|
|
|
|
|
// What? Fall back to global default.
|
2023-02-22 18:05:26 +03:00
|
|
|
default:
|
2024-07-17 17:46:52 +03:00
|
|
|
status.Visibility = gtsmodel.VisibilityDefault
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-17 17:46:52 +03:00
|
|
|
// Set federated flag to form value
|
|
|
|
// if provided, or default to true.
|
2024-07-17 23:04:52 +03:00
|
|
|
federated := util.PtrOrValue(form.Federated, true)
|
2023-02-22 18:05:26 +03:00
|
|
|
status.Federated = &federated
|
2024-07-17 17:46:52 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func processInteractionPolicy(
|
|
|
|
_ *apimodel.AdvancedStatusCreateForm,
|
|
|
|
settings *gtsmodel.AccountSettings,
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
) error {
|
|
|
|
// TODO: parse policy for this
|
|
|
|
// status from form and prefer this.
|
|
|
|
|
|
|
|
// TODO: prevent scope widening by
|
|
|
|
// limiting interaction policy if
|
|
|
|
// inReplyTo status has a stricter
|
|
|
|
// interaction policy than this one.
|
|
|
|
|
|
|
|
switch status.Visibility {
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityPublic:
|
|
|
|
// Take account's default "public" policy if set.
|
|
|
|
if p := settings.InteractionPolicyPublic; p != nil {
|
|
|
|
status.InteractionPolicy = p
|
|
|
|
}
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityUnlocked:
|
|
|
|
// Take account's default "unlisted" policy if set.
|
|
|
|
if p := settings.InteractionPolicyUnlocked; p != nil {
|
|
|
|
status.InteractionPolicy = p
|
|
|
|
}
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityFollowersOnly,
|
|
|
|
gtsmodel.VisibilityMutualsOnly:
|
|
|
|
// Take account's default followers-only policy if set.
|
|
|
|
// TODO: separate policy for mutuals-only vis.
|
|
|
|
if p := settings.InteractionPolicyFollowersOnly; p != nil {
|
|
|
|
status.InteractionPolicy = p
|
|
|
|
}
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityDirect:
|
|
|
|
// Take account's default direct policy if set.
|
|
|
|
if p := settings.InteractionPolicyDirect; p != nil {
|
|
|
|
status.InteractionPolicy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no policy set by now, status interaction
|
|
|
|
// policy will be stored as nil, which just means
|
|
|
|
// "fall back to global default policy". We avoid
|
|
|
|
// setting it explicitly to save space.
|
2023-02-22 18:05:26 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
func processLanguage(form *apimodel.AdvancedStatusCreateForm, accountDefaultLanguage string, status *gtsmodel.Status) error {
|
2023-02-22 18:05:26 +03: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 15:09:42 +03: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.
|
2024-03-22 16:03:46 +03:00
|
|
|
contentType := apimodel.StatusContentType(status.Account.Settings.StatusContentType)
|
2023-10-04 15:09:42 +03:00
|
|
|
form.ContentType = contentType
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// format is the currently set text formatting
|
|
|
|
// function, according to the provided content-type.
|
|
|
|
var format text.FormatFunc
|
2023-02-22 18:05:26 +03:00
|
|
|
|
2023-10-04 15:09:42 +03: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 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-02 14:06:40 +03:00
|
|
|
switch form.ContentType {
|
2023-10-04 15:09:42 +03:00
|
|
|
// None given / set,
|
|
|
|
// use default (plain).
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
// Format status according to text/plain.
|
2023-03-02 14:06:40 +03:00
|
|
|
case apimodel.StatusContentTypePlain:
|
2023-10-04 15:09:42 +03:00
|
|
|
format = p.formatter.FromPlain
|
|
|
|
|
|
|
|
// Format status according to text/markdown.
|
2023-03-02 14:06:40 +03:00
|
|
|
case apimodel.StatusContentTypeMarkdown:
|
2023-10-04 15:09:42 +03:00
|
|
|
format = p.formatter.FromMarkdown
|
|
|
|
|
|
|
|
// Unknown.
|
2023-02-22 18:05:26 +03:00
|
|
|
default:
|
2023-10-04 15:09:42 +03:00
|
|
|
return fmt.Errorf("invalid status format: %q", form.ContentType)
|
2023-02-22 18:05:26 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// Sanitize status text and format.
|
|
|
|
contentRes := formatInput(format, form.Status)
|
2023-02-22 18:05:26 +03:00
|
|
|
|
2023-10-04 15:09:42 +03: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 18:05:26 +03:00
|
|
|
|
2023-10-04 15:09:42 +03: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...)
|
|
|
|
|
2023-11-08 17:32:17 +03:00
|
|
|
if status.Poll != nil {
|
|
|
|
for i := range status.Poll.Options {
|
|
|
|
// Sanitize each option title name and format.
|
|
|
|
option := text.SanitizeToPlaintext(status.Poll.Options[i])
|
|
|
|
optionRes := formatInput(format, option)
|
|
|
|
|
|
|
|
// Collect each formatted result.
|
|
|
|
status.Poll.Options[i] = optionRes.HTML
|
|
|
|
status.Emojis = append(status.Emojis, optionRes.Emojis...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:09:42 +03:00
|
|
|
// 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 18:05:26 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-04 15:09:42 +03: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
|
|
|
|
}
|