gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

converter.go (16111B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package typeutils
     19 
     20 import (
     21 	"context"
     22 	"net/url"
     23 	"sync"
     24 
     25 	"github.com/gorilla/feeds"
     26 	"github.com/superseriousbusiness/activity/streams/vocab"
     27 	"github.com/superseriousbusiness/gotosocial/internal/ap"
     28 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     29 	"github.com/superseriousbusiness/gotosocial/internal/db"
     30 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     31 )
     32 
     33 // TypeConverter is an interface for the common action of converting between apimodule (frontend, serializable) models,
     34 // internal gts models used in the database, and activitypub models used in federation.
     35 //
     36 // It requires access to the database because many of the conversions require pulling out database entries and counting them etc.
     37 // That said, it *absolutely should not* manipulate database entries in any way, only examine them.
     38 type TypeConverter interface {
     39 	/*
     40 		INTERNAL (gts) MODEL TO FRONTEND (api) MODEL
     41 	*/
     42 
     43 	// AccountToAPIAccountSensitive takes a db model account as a param, and returns a populated apitype account, or an error
     44 	// if something goes wrong. The returned account should be ready to serialize on an API level, and may have sensitive fields,
     45 	// so serve it only to an authorized user who should have permission to see it.
     46 	AccountToAPIAccountSensitive(ctx context.Context, account *gtsmodel.Account) (*apimodel.Account, error)
     47 	// AccountToAPIAccountPublic takes a db model account as a param, and returns a populated apitype account, or an error
     48 	// if something goes wrong. The returned account should be ready to serialize on an API level, and may NOT have sensitive fields.
     49 	// In other words, this is the public record that the server has of an account.
     50 	AccountToAPIAccountPublic(ctx context.Context, account *gtsmodel.Account) (*apimodel.Account, error)
     51 	// AccountToAPIAccountBlocked takes a db model account as a param, and returns a apitype account, or an error if
     52 	// something goes wrong. The returned account will be a bare minimum representation of the account. This function should be used
     53 	// when someone wants to view an account they've blocked.
     54 	AccountToAPIAccountBlocked(ctx context.Context, account *gtsmodel.Account) (*apimodel.Account, error)
     55 	// AppToAPIAppSensitive takes a db model application as a param, and returns a populated apitype application, or an error
     56 	// if something goes wrong. The returned application should be ready to serialize on an API level, and may have sensitive fields
     57 	// (such as client id and client secret), so serve it only to an authorized user who should have permission to see it.
     58 	AppToAPIAppSensitive(ctx context.Context, application *gtsmodel.Application) (*apimodel.Application, error)
     59 	// AppToAPIAppPublic takes a db model application as a param, and returns a populated apitype application, or an error
     60 	// if something goes wrong. The returned application should be ready to serialize on an API level, and has sensitive
     61 	// fields sanitized so that it can be served to non-authorized accounts without revealing any private information.
     62 	AppToAPIAppPublic(ctx context.Context, application *gtsmodel.Application) (*apimodel.Application, error)
     63 	// AttachmentToAPIAttachment converts a gts model media attacahment into its api representation for serialization on the API.
     64 	AttachmentToAPIAttachment(ctx context.Context, attachment *gtsmodel.MediaAttachment) (apimodel.Attachment, error)
     65 	// MentionToAPIMention converts a gts model mention into its api (frontend) representation for serialization on the API.
     66 	MentionToAPIMention(ctx context.Context, m *gtsmodel.Mention) (apimodel.Mention, error)
     67 	// EmojiToAPIEmoji converts a gts model emoji into its api (frontend) representation for serialization on the API.
     68 	EmojiToAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (apimodel.Emoji, error)
     69 	// EmojiToAdminAPIEmoji converts a gts model emoji into an API representation with extra admin information.
     70 	EmojiToAdminAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (*apimodel.AdminEmoji, error)
     71 	// EmojiCategoryToAPIEmojiCategory converts a gts model emoji category into its api (frontend) representation.
     72 	EmojiCategoryToAPIEmojiCategory(ctx context.Context, category *gtsmodel.EmojiCategory) (*apimodel.EmojiCategory, error)
     73 	// TagToAPITag converts a gts model tag into its api (frontend) representation for serialization on the API.
     74 	TagToAPITag(ctx context.Context, t *gtsmodel.Tag) (apimodel.Tag, error)
     75 	// StatusToAPIStatus converts a gts model status into its api (frontend) representation for serialization on the API.
     76 	//
     77 	// Requesting account can be nil.
     78 	StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*apimodel.Status, error)
     79 	// VisToAPIVis converts a gts visibility into its api equivalent
     80 	VisToAPIVis(ctx context.Context, m gtsmodel.Visibility) apimodel.Visibility
     81 	// InstanceToAPIV1Instance converts a gts instance into its api equivalent for serving at /api/v1/instance
     82 	InstanceToAPIV1Instance(ctx context.Context, i *gtsmodel.Instance) (*apimodel.InstanceV1, error)
     83 	// InstanceToAPIV2Instance converts a gts instance into its api equivalent for serving at /api/v2/instance
     84 	InstanceToAPIV2Instance(ctx context.Context, i *gtsmodel.Instance) (*apimodel.InstanceV2, error)
     85 	// RelationshipToAPIRelationship converts a gts relationship into its api equivalent for serving in various places
     86 	RelationshipToAPIRelationship(ctx context.Context, r *gtsmodel.Relationship) (*apimodel.Relationship, error)
     87 	// NotificationToAPINotification converts a gts notification into a api notification
     88 	NotificationToAPINotification(ctx context.Context, n *gtsmodel.Notification) (*apimodel.Notification, error)
     89 	// DomainBlockToAPIDomainBlock converts a gts model domin block into a api domain block, for serving at /api/v1/admin/domain_blocks
     90 	DomainBlockToAPIDomainBlock(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*apimodel.DomainBlock, error)
     91 	// ReportToAPIReport converts a gts model report into an api model report, for serving at /api/v1/reports
     92 	ReportToAPIReport(ctx context.Context, r *gtsmodel.Report) (*apimodel.Report, error)
     93 	// ReportToAdminAPIReport converts a gts model report into an admin view report, for serving at /api/v1/admin/reports
     94 	ReportToAdminAPIReport(ctx context.Context, r *gtsmodel.Report, requestingAccount *gtsmodel.Account) (*apimodel.AdminReport, error)
     95 	// ListToAPIList converts one gts model list into an api model list, for serving at /api/v1/lists/{id}
     96 	ListToAPIList(ctx context.Context, l *gtsmodel.List) (*apimodel.List, error)
     97 
     98 	/*
     99 		INTERNAL (gts) MODEL TO FRONTEND (rss) MODEL
    100 	*/
    101 
    102 	StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*feeds.Item, error)
    103 
    104 	/*
    105 		ACTIVITYSTREAMS MODEL TO INTERNAL (gts) MODEL
    106 	*/
    107 
    108 	// ASRepresentationToAccount converts a remote account/person/application representation into a gts model account.
    109 	//
    110 	// If accountDomain is provided then this value will be used as the account's Domain, else the AP ID host.
    111 	ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, accountDomain string) (*gtsmodel.Account, error)
    112 	// ASStatus converts a remote activitystreams 'status' representation into a gts model status.
    113 	ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error)
    114 	// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request.
    115 	ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error)
    116 	// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
    117 	ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error)
    118 	// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
    119 	ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error)
    120 	// ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block.
    121 	ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error)
    122 	// ASAnnounceToStatus converts an activitystreams 'announce' into a status.
    123 	//
    124 	// The returned bool indicates whether this status is new (true) or not new (false).
    125 	//
    126 	// In other words, if the status is already in the database with the ID set on the announceable, then that will be returned,
    127 	// the returned bool will be false, and no further processing is necessary. If the returned bool is true, indicating
    128 	// that this is a new announce, then further processing will be necessary, because the returned status will be bareboned and
    129 	// require further dereferencing.
    130 	//
    131 	// This is useful when multiple users on an instance might receive the same boost, and we only want to process the boost once.
    132 	//
    133 	// NOTE -- this is different from one status being boosted multiple times! In this case, new boosts should indeed be created.
    134 	ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error)
    135 	// ASFlagToReport converts a remote activitystreams 'flag' representation into a gts model report.
    136 	ASFlagToReport(ctx context.Context, flaggable ap.Flaggable) (report *gtsmodel.Report, err error)
    137 
    138 	/*
    139 		INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL
    140 	*/
    141 
    142 	// AccountToAS converts a gts model account into an activity streams person, suitable for federation
    143 	AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
    144 	// AccountToASMinimal converts a gts model account into an activity streams person, suitable for federation.
    145 	//
    146 	// The returned account will just have the Type, Username, PublicKey, and ID properties set. This is
    147 	// suitable for serving to requesters to whom we want to give as little information as possible because
    148 	// we don't trust them (yet).
    149 	AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
    150 	// StatusToAS converts a gts model status into an activity streams note, suitable for federation
    151 	StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error)
    152 	// StatusToASDelete converts a gts model status into a Delete of that status, using just the
    153 	// URI of the status as object, and addressing the Delete appropriately.
    154 	StatusToASDelete(ctx context.Context, status *gtsmodel.Status) (vocab.ActivityStreamsDelete, error)
    155 	// FollowToASFollow converts a gts model Follow into an activity streams Follow, suitable for federation
    156 	FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error)
    157 	// MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation
    158 	MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error)
    159 	// EmojiToAS converts a gts emoji into a mastodon ns Emoji, suitable for federation
    160 	EmojiToAS(ctx context.Context, e *gtsmodel.Emoji) (vocab.TootEmoji, error)
    161 	// AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation
    162 	AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
    163 	// FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation.
    164 	FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error)
    165 	// BoostToAS converts a gts model boost into an activityStreams ANNOUNCE, suitable for federation
    166 	BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error)
    167 	// BlockToAS converts a gts model block into an activityStreams BLOCK, suitable for federation.
    168 	BlockToAS(ctx context.Context, block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error)
    169 	// StatusToASRepliesCollection converts a gts model status into an activityStreams REPLIES collection.
    170 	StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error)
    171 	// StatusURIsToASRepliesPage returns a collection page with appropriate next/part of pagination.
    172 	StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error)
    173 	// OutboxToASCollection returns an ordered collection with appropriate id, next, and last fields.
    174 	// The returned collection won't have any actual entries; just links to where entries can be obtained.
    175 	OutboxToASCollection(ctx context.Context, outboxID string) (vocab.ActivityStreamsOrderedCollection, error)
    176 	// StatusesToASOutboxPage returns an ordered collection page using the given statuses and parameters as contents.
    177 	//
    178 	// The maxID and minID should be the parameters that were passed to the database to obtain the given statuses.
    179 	// These will be used to create the 'id' field of the collection.
    180 	//
    181 	// OutboxID is used to create the 'partOf' field in the collection.
    182 	//
    183 	// Appropriate 'next' and 'prev' fields will be created based on the highest and lowest IDs present in the statuses slice.
    184 	StatusesToASOutboxPage(ctx context.Context, outboxID string, maxID string, minID string, statuses []*gtsmodel.Status) (vocab.ActivityStreamsOrderedCollectionPage, error)
    185 	// StatusesToASFeaturedCollection converts a slice of statuses into an ordered collection
    186 	// of URIs, suitable for serializing and serving via the activitypub API.
    187 	StatusesToASFeaturedCollection(ctx context.Context, featuredCollectionID string, statuses []*gtsmodel.Status) (vocab.ActivityStreamsOrderedCollection, error)
    188 	// ReportToASFlag converts a gts model report into an activitystreams FLAG, suitable for federation.
    189 	ReportToASFlag(ctx context.Context, r *gtsmodel.Report) (vocab.ActivityStreamsFlag, error)
    190 
    191 	/*
    192 		INTERNAL (gts) MODEL TO INTERNAL MODEL
    193 	*/
    194 
    195 	// FollowRequestToFollow just converts a follow request into a follow, that's it! No bells and whistles.
    196 	FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow
    197 	// StatusToBoost wraps the given status into a boosting status.
    198 	StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error)
    199 
    200 	/*
    201 		WRAPPER CONVENIENCE FUNCTIONS
    202 	*/
    203 
    204 	// WrapPersonInUpdate
    205 	WrapPersonInUpdate(person vocab.ActivityStreamsPerson, originAccount *gtsmodel.Account) (vocab.ActivityStreamsUpdate, error)
    206 	// WrapNoteInCreate wraps a Note with a Create activity.
    207 	//
    208 	// If objectIRIOnly is set to true, then the function won't put the *entire* note in the Object field of the Create,
    209 	// but just the AP URI of the note. This is useful in cases where you want to give a remote server something to dereference,
    210 	// and still have control over whether or not they're allowed to actually see the contents.
    211 	WrapNoteInCreate(note vocab.ActivityStreamsNote, objectIRIOnly bool) (vocab.ActivityStreamsCreate, error)
    212 }
    213 
    214 type converter struct {
    215 	db             db.DB
    216 	defaultAvatars []string
    217 	randAvatars    sync.Map
    218 }
    219 
    220 // NewConverter returns a new Converter
    221 func NewConverter(db db.DB) TypeConverter {
    222 	return &converter{
    223 		db:             db,
    224 		defaultAvatars: populateDefaultAvatars(),
    225 	}
    226 }