From e51a78716201765ba93dd97d04918e5ce8055770 Mon Sep 17 00:00:00 2001 From: ewin Date: Mon, 3 Mar 2025 13:46:37 -0500 Subject: [PATCH] Write status content type on create/edit --- internal/processing/status/common.go | 12 +++------ internal/processing/status/create.go | 16 +++++++++++- internal/processing/status/edit.go | 39 +++++++++++++++++++++++++++- internal/processing/status/get.go | 1 + 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/internal/processing/status/common.go b/internal/processing/status/common.go index 3f2b7b6cb..aaf310c2c 100644 --- a/internal/processing/status/common.go +++ b/internal/processing/status/common.go @@ -110,7 +110,7 @@ func (p *Processor) processContent( ctx context.Context, author *gtsmodel.Account, statusID string, - contentType string, + contentType apimodel.StatusContentType, content string, contentWarning string, language string, @@ -146,20 +146,14 @@ func (p *Processor) processContent( // function, according to the provided content-type. var format text.FormatFunc - if contentType == "" { - // If content type wasn't specified, use - // the author's preferred content-type. - contentType = author.Settings.StatusContentType - } - switch contentType { // Format status according to text/plain. - case "", string(apimodel.StatusContentTypePlain): + case apimodel.StatusContentTypePlain: format = p.formatter.FromPlain // Format status according to text/markdown. - case string(apimodel.StatusContentTypeMarkdown): + case apimodel.StatusContentTypeMarkdown: format = p.formatter.FromMarkdown // Unknown. diff --git a/internal/processing/status/create.go b/internal/processing/status/create.go index 727c12084..c1a93d594 100644 --- a/internal/processing/status/create.go +++ b/internal/processing/status/create.go @@ -66,11 +66,22 @@ func (p *Processor) Create( // Generate new ID for status. statusID := id.NewULID() + // Process incoming content type + contentType := form.ContentType + if contentType == "" { + // If not set in the form, use the user's default + contentType = apimodel.StatusContentType(requester.Settings.StatusContentType) + if contentType == "" { + // ??? use the global default value + contentType = apimodel.StatusContentTypeDefault + } + } + // Process incoming status content fields. content, errWithCode := p.processContent(ctx, requester, statusID, - string(form.ContentType), + contentType, form.Status, form.SpoilerText, form.Language, @@ -159,6 +170,9 @@ func (p *Processor) Create( // Set validated language. Language: content.Language, + // Set resolved content type. + ContentType: typeutils.APIContentTypeToContentType(contentType), + // Set formatted status content. Content: content.Content, ContentWarning: content.ContentWarning, diff --git a/internal/processing/status/edit.go b/internal/processing/status/edit.go index 95665074e..9d7821588 100644 --- a/internal/processing/status/edit.go +++ b/internal/processing/status/edit.go @@ -33,6 +33,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" "github.com/superseriousbusiness/gotosocial/internal/util/xslices" ) @@ -84,11 +85,14 @@ func (p *Processor) Edit( return nil, errWithCode } + // Process incoming content type and update as needed + p.processContentType(ctx, form, status, requester.Settings.StatusContentType) + // Process incoming status edit content fields. content, errWithCode := p.processContent(ctx, requester, statusID, - string(form.ContentType), + form.ContentType, form.Status, form.SpoilerText, form.Language, @@ -256,6 +260,7 @@ func (p *Processor) Edit( edit.Content = status.Content edit.ContentWarning = status.ContentWarning edit.Text = status.Text + edit.ContentType = status.ContentType edit.Language = status.Language edit.Sensitive = status.Sensitive edit.StatusID = status.ID @@ -342,6 +347,38 @@ func (p *Processor) Edit( return p.c.GetAPIStatus(ctx, requester, status) } +// Updates the content type of the status +func (p *Processor) processContentType( + ctx context.Context, + form *apimodel.StatusEditRequest, + status *gtsmodel.Status, + accountDefaultContentType string, +) { + switch { + // Content type set on form, update the status with the new value. + case form.ContentType != "": + status.ContentType = typeutils.APIContentTypeToContentType(form.ContentType) + + // No content type on the form, get the status's current content type and + // set it back on the form for later use. + case status.ContentType != 0: + form.ContentType = p.converter.ContentTypeToAPIContentType(ctx, status.ContentType) + + // Old statuses may not have a saved content type; update the status to the + // user's preference and set this back on the form for later use. + case accountDefaultContentType != "": + // TODO: is this conversion from string to StatusContentType safe + status.ContentType = typeutils.APIContentTypeToContentType(apimodel.StatusContentType(accountDefaultContentType)) + form.ContentType = apimodel.StatusContentType(accountDefaultContentType) + + // uhh.. Fall back to global default, set + // this back on the form for later use. + default: + status.ContentType = gtsmodel.StatusContentTypeDefault + form.ContentType = apimodel.StatusContentTypeDefault + } +} + // HistoryGet gets edit history for the target status, taking account of privacy settings and blocks etc. func (p *Processor) HistoryGet(ctx context.Context, requester *gtsmodel.Account, targetStatusID string) ([]*apimodel.StatusEdit, gtserror.WithCode) { target, errWithCode := p.c.GetVisibleTargetStatus(ctx, diff --git a/internal/processing/status/get.go b/internal/processing/status/get.go index 812f01683..9d6b15b1b 100644 --- a/internal/processing/status/get.go +++ b/internal/processing/status/get.go @@ -56,5 +56,6 @@ func (p *Processor) SourceGet(ctx context.Context, requester *gtsmodel.Account, ID: status.ID, Text: status.Text, SpoilerText: status.ContentWarning, + ContentType: p.converter.ContentTypeToAPIContentType(ctx, status.ContentType), }, nil }