summaryrefslogtreecommitdiff
path: root/internal/processing/workers/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/workers/util.go')
-rw-r--r--internal/processing/workers/util.go200
1 files changed, 107 insertions, 93 deletions
diff --git a/internal/processing/workers/util.go b/internal/processing/workers/util.go
index 49c6183a4..bb7faffbf 100644
--- a/internal/processing/workers/util.go
+++ b/internal/processing/workers/util.go
@@ -26,12 +26,11 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
- "github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/processing/account"
"github.com/superseriousbusiness/gotosocial/internal/processing/media"
"github.com/superseriousbusiness/gotosocial/internal/state"
- "github.com/superseriousbusiness/gotosocial/internal/uris"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
@@ -488,128 +487,143 @@ func (u *utils) decrementFollowRequestsCount(
return nil
}
-// approveFave stores + returns an
-// interactionApproval for a fave.
-func (u *utils) approveFave(
+// requestFave stores an interaction request
+// for the given fave, and notifies the interactee.
+func (u *utils) requestFave(
ctx context.Context,
fave *gtsmodel.StatusFave,
-) (*gtsmodel.InteractionApproval, error) {
- id := id.NewULID()
+) error {
+ // Only create interaction request
+ // if fave targets a local status.
+ if fave.Status == nil ||
+ !fave.Status.IsLocal() {
+ return nil
+ }
- approval := &gtsmodel.InteractionApproval{
- ID: id,
- AccountID: fave.TargetAccountID,
- Account: fave.TargetAccount,
- InteractingAccountID: fave.AccountID,
- InteractingAccount: fave.Account,
- InteractionURI: fave.URI,
- InteractionType: gtsmodel.InteractionLike,
- URI: uris.GenerateURIForAccept(fave.TargetAccount.Username, id),
+ // Lock on the interaction URI.
+ unlock := u.state.ProcessingLocks.Lock(fave.URI)
+ defer unlock()
+
+ // Ensure no req with this URI exists already.
+ req, err := u.state.DB.GetInteractionRequestByInteractionURI(ctx, fave.URI)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return gtserror.Newf("db error checking for existing interaction request: %w", err)
}
- if err := u.state.DB.PutInteractionApproval(ctx, approval); err != nil {
- err := gtserror.Newf("db error inserting interaction approval: %w", err)
- return nil, err
+ if req != nil {
+ // Interaction req already exists,
+ // no need to do anything else.
+ return nil
}
- // Mark the fave itself as now approved.
- fave.PendingApproval = util.Ptr(false)
- fave.PreApproved = false
- fave.ApprovedByURI = approval.URI
+ // Create + store new interaction request.
+ req, err = typeutils.StatusFaveToInteractionRequest(ctx, fave)
+ if err != nil {
+ return gtserror.Newf("error creating interaction request: %w", err)
+ }
- if err := u.state.DB.UpdateStatusFave(
- ctx,
- fave,
- "pending_approval",
- "approved_by_uri",
- ); err != nil {
- err := gtserror.Newf("db error updating status fave: %w", err)
- return nil, err
+ if err := u.state.DB.PutInteractionRequest(ctx, req); err != nil {
+ return gtserror.Newf("db error storing interaction request: %w", err)
}
- return approval, nil
+ // Notify *local* account of pending announce.
+ if err := u.surface.notifyPendingFave(ctx, fave); err != nil {
+ return gtserror.Newf("error notifying pending fave: %w", err)
+ }
+
+ return nil
}
-// approveReply stores + returns an
-// interactionApproval for a reply.
-func (u *utils) approveReply(
+// requestReply stores an interaction request
+// for the given reply, and notifies the interactee.
+func (u *utils) requestReply(
ctx context.Context,
- status *gtsmodel.Status,
-) (*gtsmodel.InteractionApproval, error) {
- id := id.NewULID()
+ reply *gtsmodel.Status,
+) error {
+ // Only create interaction request if
+ // status replies to a local status.
+ if reply.InReplyTo == nil ||
+ !reply.InReplyTo.IsLocal() {
+ return nil
+ }
- approval := &gtsmodel.InteractionApproval{
- ID: id,
- AccountID: status.InReplyToAccountID,
- Account: status.InReplyToAccount,
- InteractingAccountID: status.AccountID,
- InteractingAccount: status.Account,
- InteractionURI: status.URI,
- InteractionType: gtsmodel.InteractionReply,
- URI: uris.GenerateURIForAccept(status.InReplyToAccount.Username, id),
+ // Lock on the interaction URI.
+ unlock := u.state.ProcessingLocks.Lock(reply.URI)
+ defer unlock()
+
+ // Ensure no req with this URI exists already.
+ req, err := u.state.DB.GetInteractionRequestByInteractionURI(ctx, reply.URI)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return gtserror.Newf("db error checking for existing interaction request: %w", err)
}
- if err := u.state.DB.PutInteractionApproval(ctx, approval); err != nil {
- err := gtserror.Newf("db error inserting interaction approval: %w", err)
- return nil, err
+ if req != nil {
+ // Interaction req already exists,
+ // no need to do anything else.
+ return nil
}
- // Mark the status itself as now approved.
- status.PendingApproval = util.Ptr(false)
- status.PreApproved = false
- status.ApprovedByURI = approval.URI
+ // Create + store interaction request.
+ req, err = typeutils.StatusToInteractionRequest(ctx, reply)
+ if err != nil {
+ return gtserror.Newf("error creating interaction request: %w", err)
+ }
- if err := u.state.DB.UpdateStatus(
- ctx,
- status,
- "pending_approval",
- "approved_by_uri",
- ); err != nil {
- err := gtserror.Newf("db error updating status: %w", err)
- return nil, err
+ if err := u.state.DB.PutInteractionRequest(ctx, req); err != nil {
+ return gtserror.Newf("db error storing interaction request: %w", err)
+ }
+
+ // Notify *local* account of pending reply.
+ if err := u.surface.notifyPendingReply(ctx, reply); err != nil {
+ return gtserror.Newf("error notifying pending reply: %w", err)
}
- return approval, nil
+ return nil
}
-// approveAnnounce stores + returns an
-// interactionApproval for an announce.
-func (u *utils) approveAnnounce(
+// requestAnnounce stores an interaction request
+// for the given announce, and notifies the interactee.
+func (u *utils) requestAnnounce(
ctx context.Context,
boost *gtsmodel.Status,
-) (*gtsmodel.InteractionApproval, error) {
- id := id.NewULID()
+) error {
+ // Only create interaction request if
+ // status announces a local status.
+ if boost.BoostOf == nil ||
+ !boost.BoostOf.IsLocal() {
+ return nil
+ }
+
+ // Lock on the interaction URI.
+ unlock := u.state.ProcessingLocks.Lock(boost.URI)
+ defer unlock()
- approval := &gtsmodel.InteractionApproval{
- ID: id,
- AccountID: boost.BoostOfAccountID,
- Account: boost.BoostOfAccount,
- InteractingAccountID: boost.AccountID,
- InteractingAccount: boost.Account,
- InteractionURI: boost.URI,
- InteractionType: gtsmodel.InteractionReply,
- URI: uris.GenerateURIForAccept(boost.BoostOfAccount.Username, id),
+ // Ensure no req with this URI exists already.
+ req, err := u.state.DB.GetInteractionRequestByInteractionURI(ctx, boost.URI)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return gtserror.Newf("db error checking for existing interaction request: %w", err)
}
- if err := u.state.DB.PutInteractionApproval(ctx, approval); err != nil {
- err := gtserror.Newf("db error inserting interaction approval: %w", err)
- return nil, err
+ if req != nil {
+ // Interaction req already exists,
+ // no need to do anything else.
+ return nil
}
- // Mark the status itself as now approved.
- boost.PendingApproval = util.Ptr(false)
- boost.PreApproved = false
- boost.ApprovedByURI = approval.URI
+ // Create + store interaction request.
+ req, err = typeutils.StatusToInteractionRequest(ctx, boost)
+ if err != nil {
+ return gtserror.Newf("error creating interaction request: %w", err)
+ }
- if err := u.state.DB.UpdateStatus(
- ctx,
- boost,
- "pending_approval",
- "approved_by_uri",
- ); err != nil {
- err := gtserror.Newf("db error updating boost wrapper status: %w", err)
- return nil, err
+ if err := u.state.DB.PutInteractionRequest(ctx, req); err != nil {
+ return gtserror.Newf("db error storing interaction request: %w", err)
+ }
+
+ // Notify *local* account of pending announce.
+ if err := u.surface.notifyPendingAnnounce(ctx, boost); err != nil {
+ return gtserror.Newf("error notifying pending announce: %w", err)
}
- return approval, nil
+ return nil
}