mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-03-13 14:40:03 +01:00
* Add ContentType to internal models * Add ContentType to API models StatusSource and StatusEdit * Add helpers to convert between API/internal StatusContentType * Write status content type on create/edit * Add migration * Update API docs go run github.com/go-swagger/go-swagger/cmd/swagger generate spec --scan-models --exclude-deps --output docs/api/swagger.yaml * ensure ContentType is updated anywhere Text is * Update docs, take care of TODOs * Set ContentType in more places where Text is set * We don't actually use ContentType on the API status model * Update StatusSource test * Remove unused helper function I copied * Revert change to StatusContentType swagger annotation I'm going to include this in a follow-on PR instead. * Add test for updating content type in edits * Return a value from processContentType instead of modifying the existing status Fixes an issue that was caught by the test I just added - the recorded edit would be marked with the *new* content type instead of the old one, which is obviously bad * Add test for handling of statuses with no stored content type * repurpose an existing test status instead of adding a new one to avoid breaking other tests * Add test to ensure newly created statuses always have content type saved * Do include content type on status API model actually This is mostly important when deleting and redrafting. The comment on `apimodel.Status.Text` implies that it's not sent except in response to status deletion, but actually this doesn't seem to be the case; it also appears to be present in responses to creations and normal fetches and stuff. So I'm treating `ContentType` the same here. * Update new tests to check content type on API statuses * Check content type of API statuses in all tests where text is checked * update other api tests with status content type field * Add test ensuring text and content type are returned when deleting a status * Convert processContentType to free function and remove unused parameter * check for the correct value in the deletion test * Be explicit about this test status having an empty content type * Use omitempty consistently on API models * clean up the final diff a bit * one more swagger regen for the road * Handle nil statuses in processContentType * Don't pass processContentType the entire edit form, it doesn't need it * Move processContentType to common.go and use for creation as well * Remove unused parameters to ContentTypeToAPIContentType
258 lines
7.4 KiB
Go
258 lines
7.4 KiB
Go
// 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/>.
|
|
|
|
package typeutils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"slices"
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
)
|
|
|
|
func APIVisToVis(m apimodel.Visibility) gtsmodel.Visibility {
|
|
switch m {
|
|
case apimodel.VisibilityPublic:
|
|
return gtsmodel.VisibilityPublic
|
|
case apimodel.VisibilityUnlisted:
|
|
return gtsmodel.VisibilityUnlocked
|
|
case apimodel.VisibilityPrivate:
|
|
return gtsmodel.VisibilityFollowersOnly
|
|
case apimodel.VisibilityMutualsOnly:
|
|
return gtsmodel.VisibilityMutualsOnly
|
|
case apimodel.VisibilityDirect:
|
|
return gtsmodel.VisibilityDirect
|
|
case apimodel.VisibilityNone:
|
|
return gtsmodel.VisibilityNone
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func APIContentTypeToContentType(m apimodel.StatusContentType) gtsmodel.StatusContentType {
|
|
switch m {
|
|
case apimodel.StatusContentTypePlain:
|
|
return gtsmodel.StatusContentTypePlain
|
|
case apimodel.StatusContentTypeMarkdown:
|
|
return gtsmodel.StatusContentTypeMarkdown
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func APIMarkerNameToMarkerName(m apimodel.MarkerName) gtsmodel.MarkerName {
|
|
switch m {
|
|
case apimodel.MarkerNameHome:
|
|
return gtsmodel.MarkerNameHome
|
|
case apimodel.MarkerNameNotifications:
|
|
return gtsmodel.MarkerNameNotifications
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func APIFilterActionToFilterAction(m apimodel.FilterAction) gtsmodel.FilterAction {
|
|
switch m {
|
|
case apimodel.FilterActionWarn:
|
|
return gtsmodel.FilterActionWarn
|
|
case apimodel.FilterActionHide:
|
|
return gtsmodel.FilterActionHide
|
|
}
|
|
return gtsmodel.FilterActionNone
|
|
}
|
|
|
|
func APIPolicyValueToPolicyValue(u apimodel.PolicyValue) (gtsmodel.PolicyValue, error) {
|
|
switch u {
|
|
case apimodel.PolicyValuePublic:
|
|
return gtsmodel.PolicyValuePublic, nil
|
|
|
|
case apimodel.PolicyValueFollowers:
|
|
return gtsmodel.PolicyValueFollowers, nil
|
|
|
|
case apimodel.PolicyValueFollowing:
|
|
return gtsmodel.PolicyValueFollowing, nil
|
|
|
|
case apimodel.PolicyValueMutuals:
|
|
return gtsmodel.PolicyValueMutuals, nil
|
|
|
|
case apimodel.PolicyValueMentioned:
|
|
return gtsmodel.PolicyValueMentioned, nil
|
|
|
|
case apimodel.PolicyValueAuthor:
|
|
return gtsmodel.PolicyValueAuthor, nil
|
|
|
|
case apimodel.PolicyValueMe:
|
|
err := fmt.Errorf("policyURI %s has no corresponding internal model", apimodel.PolicyValueMe)
|
|
return "", err
|
|
|
|
default:
|
|
// Parse URI to ensure it's a
|
|
// url with a valid protocol.
|
|
url, err := url.Parse(string(u))
|
|
if err != nil {
|
|
err := fmt.Errorf("could not parse non-predefined policy value as uri: %w", err)
|
|
return "", err
|
|
}
|
|
|
|
if url.Host != "http" && url.Host != "https" {
|
|
err := fmt.Errorf("non-predefined policy values must have protocol 'http' or 'https' (%s)", u)
|
|
return "", err
|
|
}
|
|
|
|
return gtsmodel.PolicyValue(u), nil
|
|
}
|
|
}
|
|
|
|
func APIInteractionPolicyToInteractionPolicy(
|
|
p *apimodel.InteractionPolicy,
|
|
v apimodel.Visibility,
|
|
) (*gtsmodel.InteractionPolicy, error) {
|
|
visibility := APIVisToVis(v)
|
|
|
|
convertURIs := func(apiURIs []apimodel.PolicyValue) (gtsmodel.PolicyValues, error) {
|
|
policyURIs := gtsmodel.PolicyValues{}
|
|
for _, apiURI := range apiURIs {
|
|
uri, err := APIPolicyValueToPolicyValue(apiURI)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !uri.FeasibleForVisibility(visibility) {
|
|
err := fmt.Errorf("policyURI %s is not feasible for visibility %s", apiURI, v)
|
|
return nil, err
|
|
}
|
|
|
|
policyURIs = append(policyURIs, uri)
|
|
}
|
|
return policyURIs, nil
|
|
}
|
|
|
|
canLikeAlways, err := convertURIs(p.CanFavourite.Always)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_favourite.always: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
canLikeWithApproval, err := convertURIs(p.CanFavourite.WithApproval)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_favourite.with_approval: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
canReplyAlways, err := convertURIs(p.CanReply.Always)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_reply.always: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
canReplyWithApproval, err := convertURIs(p.CanReply.WithApproval)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_reply.with_approval: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
canAnnounceAlways, err := convertURIs(p.CanReblog.Always)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_reblog.always: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
canAnnounceWithApproval, err := convertURIs(p.CanReblog.WithApproval)
|
|
if err != nil {
|
|
err := fmt.Errorf("error converting %s.can_reblog.with_approval: %w", v, err)
|
|
return nil, err
|
|
}
|
|
|
|
// Normalize URIs.
|
|
//
|
|
// 1. Ensure canLikeAlways, canReplyAlways,
|
|
// and canAnnounceAlways include self
|
|
// (either explicitly or within public).
|
|
|
|
// ensureIncludesSelf adds the "author" PolicyValue
|
|
// to given slice of PolicyValues, if not already
|
|
// explicitly or implicitly included.
|
|
ensureIncludesSelf := func(vals gtsmodel.PolicyValues) gtsmodel.PolicyValues {
|
|
includesSelf := slices.ContainsFunc(
|
|
vals,
|
|
func(uri gtsmodel.PolicyValue) bool {
|
|
return uri == gtsmodel.PolicyValuePublic ||
|
|
uri == gtsmodel.PolicyValueAuthor
|
|
},
|
|
)
|
|
|
|
if includesSelf {
|
|
// This slice of policy values
|
|
// already includes self explicitly
|
|
// or implicitly, nothing to change.
|
|
return vals
|
|
}
|
|
|
|
// Need to add self/author to
|
|
// this slice of policy values.
|
|
vals = append(vals, gtsmodel.PolicyValueAuthor)
|
|
return vals
|
|
}
|
|
|
|
canLikeAlways = ensureIncludesSelf(canLikeAlways)
|
|
canReplyAlways = ensureIncludesSelf(canReplyAlways)
|
|
canAnnounceAlways = ensureIncludesSelf(canAnnounceAlways)
|
|
|
|
// 2. Ensure canReplyAlways includes mentioned
|
|
// accounts (either explicitly or within public).
|
|
if !slices.ContainsFunc(
|
|
canReplyAlways,
|
|
func(uri gtsmodel.PolicyValue) bool {
|
|
return uri == gtsmodel.PolicyValuePublic ||
|
|
uri == gtsmodel.PolicyValueMentioned
|
|
},
|
|
) {
|
|
canReplyAlways = append(
|
|
canReplyAlways,
|
|
gtsmodel.PolicyValueMentioned,
|
|
)
|
|
}
|
|
|
|
return >smodel.InteractionPolicy{
|
|
CanLike: gtsmodel.PolicyRules{
|
|
Always: canLikeAlways,
|
|
WithApproval: canLikeWithApproval,
|
|
},
|
|
CanReply: gtsmodel.PolicyRules{
|
|
Always: canReplyAlways,
|
|
WithApproval: canReplyWithApproval,
|
|
},
|
|
CanAnnounce: gtsmodel.PolicyRules{
|
|
Always: canAnnounceAlways,
|
|
WithApproval: canAnnounceWithApproval,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func APIWebPushNotificationPolicyToWebPushNotificationPolicy(policy apimodel.WebPushNotificationPolicy) gtsmodel.WebPushNotificationPolicy {
|
|
switch policy {
|
|
case apimodel.WebPushNotificationPolicyAll:
|
|
return gtsmodel.WebPushNotificationPolicyAll
|
|
case apimodel.WebPushNotificationPolicyFollowed:
|
|
return gtsmodel.WebPushNotificationPolicyFollowed
|
|
case apimodel.WebPushNotificationPolicyFollower:
|
|
return gtsmodel.WebPushNotificationPolicyFollower
|
|
case apimodel.WebPushNotificationPolicyNone:
|
|
return gtsmodel.WebPushNotificationPolicyNone
|
|
}
|
|
return 0
|
|
}
|