From 2dc9fc1626507bb54417fc4a1920b847cafb27a2 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 25 Aug 2021 15:34:33 +0200 Subject: Pg to bun (#148) * start moving to bun * changing more stuff * more * and yet more * tests passing * seems stable now * more big changes * small fix * little fixes --- internal/typeutils/converter.go | 71 +++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 35 deletions(-) (limited to 'internal/typeutils/converter.go') diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go index e477a6135..4af9767bc 100644 --- a/internal/typeutils/converter.go +++ b/internal/typeutils/converter.go @@ -19,6 +19,7 @@ package typeutils import ( + "context" "net/url" "github.com/go-fed/activity/streams/vocab" @@ -47,45 +48,45 @@ type TypeConverter interface { // 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. - AccountToMastoSensitive(account *gtsmodel.Account) (*model.Account, error) + AccountToMastoSensitive(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) // 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. - AccountToMastoPublic(account *gtsmodel.Account) (*model.Account, error) + AccountToMastoPublic(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) // 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. - AccountToMastoBlocked(account *gtsmodel.Account) (*model.Account, error) + AccountToMastoBlocked(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) // 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. - AppToMastoSensitive(application *gtsmodel.Application) (*model.Application, error) + AppToMastoSensitive(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) // 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. - AppToMastoPublic(application *gtsmodel.Application) (*model.Application, error) + AppToMastoPublic(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) // AttachmentToMasto converts a gts model media attacahment into its mastodon representation for serialization on the API. - AttachmentToMasto(attachment *gtsmodel.MediaAttachment) (model.Attachment, error) + AttachmentToMasto(ctx context.Context, attachment *gtsmodel.MediaAttachment) (model.Attachment, error) // MentionToMasto converts a gts model mention into its mastodon (frontend) representation for serialization on the API. - MentionToMasto(m *gtsmodel.Mention) (model.Mention, error) + MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) // EmojiToMasto converts a gts model emoji into its mastodon (frontend) representation for serialization on the API. - EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error) + EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) // TagToMasto converts a gts model tag into its mastodon (frontend) representation for serialization on the API. - TagToMasto(t *gtsmodel.Tag) (model.Tag, error) + TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) // StatusToMasto converts a gts model status into its mastodon (frontend) representation for serialization on the API. // // Requesting account can be nil. - StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) + StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) // VisToMasto converts a gts visibility into its mastodon equivalent - VisToMasto(m gtsmodel.Visibility) model.Visibility + VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility // InstanceToMasto converts a gts instance into its mastodon equivalent for serving at /api/v1/instance - InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, error) + InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) // RelationshipToMasto converts a gts relationship into its mastodon equivalent for serving in various places - RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relationship, error) + RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) // NotificationToMasto converts a gts notification into a mastodon notification - NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) + NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) // DomainBlockTomasto converts a gts model domin block into a mastodon domain block, for serving at /api/v1/admin/domain_blocks - DomainBlockToMasto(b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) + DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) /* FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL @@ -103,17 +104,17 @@ type TypeConverter interface { // 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. - ASRepresentationToAccount(accountable ap.Accountable, update bool) (*gtsmodel.Account, error) + ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error) // ASStatus converts a remote activitystreams 'status' representation into a gts model status. - ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status, error) + ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) // ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request. - ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel.FollowRequest, error) + ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) // ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow. - ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow, error) + ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) // ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave. - ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, error) + ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) // ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block. - ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, error) + ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) // ASAnnounceToStatus converts an activitystreams 'announce' into a status. // // The returned bool indicates whether this status is new (true) or not new (false). @@ -126,46 +127,46 @@ type TypeConverter interface { // 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. - ASAnnounceToStatus(announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error) + ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error) /* INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL */ // AccountToAS converts a gts model account into an activity streams person, suitable for federation - AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) + AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) // 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). - AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) + AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) // StatusToAS converts a gts model status into an activity streams note, suitable for federation - StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) + StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) // FollowToASFollow converts a gts model Follow into an activity streams Follow, suitable for federation - FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) + FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) // MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation - MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) + MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) // AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation - AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) + AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) // FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation. - FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) + FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) // BoostToAS converts a gts model boost into an activityStreams ANNOUNCE, suitable for federation - BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) + BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) // BlockToAS converts a gts model block into an activityStreams BLOCK, suitable for federation. - BlockToAS(block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) + BlockToAS(ctx context.Context, block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) // StatusToASRepliesCollection converts a gts model status into an activityStreams REPLIES collection. - StatusToASRepliesCollection(status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) + StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) // StatusURIsToASRepliesPage returns a collection page with appropriate next/part of pagination. - StatusURIsToASRepliesPage(status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) + StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) /* INTERNAL (gts) MODEL TO INTERNAL MODEL */ // FollowRequestToFollow just converts a follow request into a follow, that's it! No bells and whistles. - FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.Follow + FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow // StatusToBoost wraps the given status into a boosting status. - StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) + StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) /* WRAPPER CONVENIENCE FUNCTIONS -- cgit v1.2.3