2021-05-08 12:25:55 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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 (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-08-10 11:32:39 +00:00
|
|
|
"net/url"
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
2021-08-29 10:03:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-10 11:32:39 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-08-20 10:26:56 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/cache"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
2021-05-21 13:48:26 +00:00
|
|
|
const (
|
|
|
|
asPublicURI = "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
)
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
// TypeConverter is an interface for the common action of converting between apimodule (frontend, serializable) models,
|
|
|
|
// internal gts models used in the database, and activitypub models used in federation.
|
|
|
|
//
|
|
|
|
// It requires access to the database because many of the conversions require pulling out database entries and counting them etc.
|
|
|
|
// That said, it *absolutely should not* manipulate database entries in any way, only examine them.
|
|
|
|
type TypeConverter interface {
|
|
|
|
/*
|
|
|
|
INTERNAL (gts) MODEL TO FRONTEND (mastodon) MODEL
|
|
|
|
*/
|
|
|
|
|
|
|
|
// AccountToMastoSensitive takes a db model account as a param, and returns a populated mastotype account, or an error
|
|
|
|
// if something goes wrong. The returned account should be ready to serialize on an API level, and may have sensitive fields,
|
|
|
|
// so serve it only to an authorized user who should have permission to see it.
|
2021-08-25 13:34:33 +00:00
|
|
|
AccountToMastoSensitive(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// AccountToMastoPublic takes a db model account as a param, and returns a populated mastotype account, or an error
|
|
|
|
// if something goes wrong. The returned account should be ready to serialize on an API level, and may NOT have sensitive fields.
|
|
|
|
// In other words, this is the public record that the server has of an account.
|
2021-08-25 13:34:33 +00:00
|
|
|
AccountToMastoPublic(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
|
2021-07-11 14:22:21 +00:00
|
|
|
// AccountToMastoBlocked takes a db model account as a param, and returns a mastotype account, or an error if
|
|
|
|
// something goes wrong. The returned account will be a bare minimum representation of the account. This function should be used
|
|
|
|
// when someone wants to view an account they've blocked.
|
2021-08-25 13:34:33 +00:00
|
|
|
AccountToMastoBlocked(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// AppToMastoSensitive takes a db model application as a param, and returns a populated mastotype application, or an error
|
|
|
|
// if something goes wrong. The returned application should be ready to serialize on an API level, and may have sensitive fields
|
|
|
|
// (such as client id and client secret), so serve it only to an authorized user who should have permission to see it.
|
2021-08-25 13:34:33 +00:00
|
|
|
AppToMastoSensitive(ctx context.Context, application *gtsmodel.Application) (*model.Application, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// AppToMastoPublic takes a db model application as a param, and returns a populated mastotype application, or an error
|
|
|
|
// if something goes wrong. The returned application should be ready to serialize on an API level, and has sensitive
|
|
|
|
// fields sanitized so that it can be served to non-authorized accounts without revealing any private information.
|
2021-08-25 13:34:33 +00:00
|
|
|
AppToMastoPublic(ctx context.Context, application *gtsmodel.Application) (*model.Application, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// AttachmentToMasto converts a gts model media attacahment into its mastodon representation for serialization on the API.
|
2021-08-25 13:34:33 +00:00
|
|
|
AttachmentToMasto(ctx context.Context, attachment *gtsmodel.MediaAttachment) (model.Attachment, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// MentionToMasto converts a gts model mention into its mastodon (frontend) representation for serialization on the API.
|
2021-08-25 13:34:33 +00:00
|
|
|
MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// EmojiToMasto converts a gts model emoji into its mastodon (frontend) representation for serialization on the API.
|
2021-08-25 13:34:33 +00:00
|
|
|
EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// TagToMasto converts a gts model tag into its mastodon (frontend) representation for serialization on the API.
|
2021-08-25 13:34:33 +00:00
|
|
|
TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// StatusToMasto converts a gts model status into its mastodon (frontend) representation for serialization on the API.
|
2021-06-17 16:02:33 +00:00
|
|
|
//
|
|
|
|
// Requesting account can be nil.
|
2021-08-25 13:34:33 +00:00
|
|
|
StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// VisToMasto converts a gts visibility into its mastodon equivalent
|
2021-08-25 13:34:33 +00:00
|
|
|
VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility
|
2021-05-09 12:06:06 +00:00
|
|
|
// InstanceToMasto converts a gts instance into its mastodon equivalent for serving at /api/v1/instance
|
2021-08-25 13:34:33 +00:00
|
|
|
InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
// RelationshipToMasto converts a gts relationship into its mastodon equivalent for serving in various places
|
2021-08-25 13:34:33 +00:00
|
|
|
RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error)
|
2021-05-27 14:06:24 +00:00
|
|
|
// NotificationToMasto converts a gts notification into a mastodon notification
|
2021-08-25 13:34:33 +00:00
|
|
|
NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error)
|
2021-07-05 11:23:03 +00:00
|
|
|
// DomainBlockTomasto converts a gts model domin block into a mastodon domain block, for serving at /api/v1/admin/domain_blocks
|
2021-08-25 13:34:33 +00:00
|
|
|
DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
/*
|
|
|
|
FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL
|
|
|
|
*/
|
|
|
|
|
|
|
|
// MastoVisToVis converts a mastodon visibility into its gts equivalent.
|
|
|
|
MastoVisToVis(m model.Visibility) gtsmodel.Visibility
|
|
|
|
|
|
|
|
/*
|
|
|
|
ACTIVITYSTREAMS MODEL TO INTERNAL (gts) MODEL
|
|
|
|
*/
|
|
|
|
|
2021-05-23 16:07:04 +00:00
|
|
|
// ASPersonToAccount converts a remote account/person/application representation into a gts model account.
|
|
|
|
//
|
|
|
|
// If update is false, and the account is already known in the database, then the existing account entry will be returned.
|
|
|
|
// If update is true, then even if the account is already known, all fields in the accountable will be parsed and a new *gtsmodel.Account
|
|
|
|
// will be generated. This is useful when one needs to force refresh of an account, eg., during an Update of a Profile.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error)
|
2021-05-15 09:58:11 +00:00
|
|
|
// ASStatus converts a remote activitystreams 'status' representation into a gts model status.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error)
|
2021-05-15 09:58:11 +00:00
|
|
|
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error)
|
2021-05-27 14:06:24 +00:00
|
|
|
// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error)
|
2021-07-11 14:22:21 +00:00
|
|
|
// ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error)
|
2021-05-28 17:57:04 +00:00
|
|
|
// ASAnnounceToStatus converts an activitystreams 'announce' into a status.
|
|
|
|
//
|
|
|
|
// The returned bool indicates whether this status is new (true) or not new (false).
|
|
|
|
//
|
|
|
|
// In other words, if the status is already in the database with the ID set on the announceable, then that will be returned,
|
|
|
|
// the returned bool will be false, and no further processing is necessary. If the returned bool is true, indicating
|
|
|
|
// that this is a new announce, then further processing will be necessary, because the returned status will be bareboned and
|
|
|
|
// require further dereferencing.
|
|
|
|
//
|
|
|
|
// This is useful when multiple users on an instance might receive the same boost, and we only want to process the boost once.
|
|
|
|
//
|
|
|
|
// NOTE -- this is different from one status being boosted multiple times! In this case, new boosts should indeed be created.
|
2021-08-25 13:34:33 +00:00
|
|
|
ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error)
|
2021-05-08 12:25:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL
|
|
|
|
*/
|
|
|
|
|
|
|
|
// AccountToAS converts a gts model account into an activity streams person, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
|
2021-07-11 14:22:21 +00:00
|
|
|
// AccountToASMinimal converts a gts model account into an activity streams person, suitable for federation.
|
|
|
|
//
|
|
|
|
// The returned account will just have the Type, Username, PublicKey, and ID properties set. This is
|
|
|
|
// suitable for serving to requesters to whom we want to give as little information as possible because
|
|
|
|
// we don't trust them (yet).
|
2021-08-25 13:34:33 +00:00
|
|
|
AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
// StatusToAS converts a gts model status into an activity streams note, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
// FollowToASFollow converts a gts model Follow into an activity streams Follow, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
// MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error)
|
2021-05-21 13:48:26 +00:00
|
|
|
// AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
|
2021-05-24 16:49:48 +00:00
|
|
|
// FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation.
|
2021-08-25 13:34:33 +00:00
|
|
|
FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error)
|
2021-05-28 17:57:04 +00:00
|
|
|
// BoostToAS converts a gts model boost into an activityStreams ANNOUNCE, suitable for federation
|
2021-08-25 13:34:33 +00:00
|
|
|
BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error)
|
2021-07-11 14:22:21 +00:00
|
|
|
// BlockToAS converts a gts model block into an activityStreams BLOCK, suitable for federation.
|
2021-08-25 13:34:33 +00:00
|
|
|
BlockToAS(ctx context.Context, block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error)
|
2021-08-10 11:32:39 +00:00
|
|
|
// StatusToASRepliesCollection converts a gts model status into an activityStreams REPLIES collection.
|
2021-08-25 13:34:33 +00:00
|
|
|
StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error)
|
2021-08-10 11:32:39 +00:00
|
|
|
// StatusURIsToASRepliesPage returns a collection page with appropriate next/part of pagination.
|
2021-08-25 13:34:33 +00:00
|
|
|
StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error)
|
2021-05-27 14:06:24 +00:00
|
|
|
/*
|
|
|
|
INTERNAL (gts) MODEL TO INTERNAL MODEL
|
|
|
|
*/
|
|
|
|
|
|
|
|
// FollowRequestToFollow just converts a follow request into a follow, that's it! No bells and whistles.
|
2021-08-25 13:34:33 +00:00
|
|
|
FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow
|
2021-05-27 14:06:24 +00:00
|
|
|
// StatusToBoost wraps the given status into a boosting status.
|
2021-08-25 13:34:33 +00:00
|
|
|
StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error)
|
2021-05-28 20:47:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
WRAPPER CONVENIENCE FUNCTIONS
|
|
|
|
*/
|
2021-05-29 17:39:43 +00:00
|
|
|
|
2021-05-28 20:47:18 +00:00
|
|
|
// WrapPersonInUpdate
|
|
|
|
WrapPersonInUpdate(person vocab.ActivityStreamsPerson, originAccount *gtsmodel.Account) (vocab.ActivityStreamsUpdate, error)
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type converter struct {
|
2021-08-20 10:26:56 +00:00
|
|
|
config *config.Config
|
|
|
|
db db.DB
|
2021-08-29 10:03:08 +00:00
|
|
|
log *logrus.Logger
|
2021-08-20 10:26:56 +00:00
|
|
|
frontendCache cache.Cache
|
|
|
|
asCache cache.Cache
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConverter returns a new Converter
|
2021-08-29 10:03:08 +00:00
|
|
|
func NewConverter(config *config.Config, db db.DB, log *logrus.Logger) TypeConverter {
|
2021-05-08 12:25:55 +00:00
|
|
|
return &converter{
|
2021-08-20 10:26:56 +00:00
|
|
|
config: config,
|
|
|
|
db: db,
|
2021-08-29 10:03:08 +00:00
|
|
|
log: log,
|
2021-08-20 10:26:56 +00:00
|
|
|
frontendCache: cache.New(),
|
|
|
|
asCache: cache.New(),
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
}
|