diff options
Diffstat (limited to 'internal/processing/workers')
-rw-r--r-- | internal/processing/workers/federate.go | 59 | ||||
-rw-r--r-- | internal/processing/workers/fromclientapi.go | 123 | ||||
-rw-r--r-- | internal/processing/workers/fromfediapi.go | 120 | ||||
-rw-r--r-- | internal/processing/workers/fromfediapi_test.go | 11 | ||||
-rw-r--r-- | internal/processing/workers/surfacenotify.go | 234 | ||||
-rw-r--r-- | internal/processing/workers/surfacetimeline.go | 11 | ||||
-rw-r--r-- | internal/processing/workers/wipestatus.go | 15 |
7 files changed, 416 insertions, 157 deletions
diff --git a/internal/processing/workers/federate.go b/internal/processing/workers/federate.go index 80b01ca40..44432998d 100644 --- a/internal/processing/workers/federate.go +++ b/internal/processing/workers/federate.go @@ -158,26 +158,52 @@ func (f *federate) CreateStatus(ctx context.Context, status *gtsmodel.Status) er return err } - // Convert status to ActivityStreams Statusable implementing type. + // Convert status to AS Statusable implementing type. statusable, err := f.converter.StatusToAS(ctx, status) if err != nil { return gtserror.Newf("error converting status to Statusable: %w", err) } - // Use ActivityStreams Statusable type as Object of Create. - create, err := f.converter.WrapStatusableInCreate(statusable, false) + // Send a Create activity with Statusable via the Actor's outbox. + create := typeutils.WrapStatusableInCreate(statusable, false) + if _, err := f.FederatingActor().Send(ctx, outboxIRI, create); err != nil { + return gtserror.Newf("error sending Create activity via outbox %s: %w", outboxIRI, err) + } + return nil +} + +func (f *federate) CreatePollVote(ctx context.Context, poll *gtsmodel.Poll, vote *gtsmodel.PollVote) error { + // Extract status from poll. + status := poll.Status + + // Do nothing if the status + // shouldn't be federated. + if !*status.Federated { + return nil + } + + // Do nothing if this is + // a vote in our status. + if *status.Local { + return nil + } + + // Parse the outbox URI of the poll vote author. + outboxIRI, err := parseURI(vote.Account.OutboxURI) if err != nil { - return gtserror.Newf("error wrapping Statusable in Create: %w", err) + return err } - // Send the Create via the Actor's outbox. - if _, err := f.FederatingActor().Send( - ctx, outboxIRI, create, - ); err != nil { - return gtserror.Newf( - "error sending activity %T via outbox %s: %w", - create, outboxIRI, err, - ) + // Convert votes to AS PollOptionable implementing type. + notes, err := f.converter.PollVoteToASOptions(ctx, vote) + if err != nil { + return gtserror.Newf("error converting to notes: %w", err) + } + + // Send a Create activity with PollOptionables via the Actor's outbox. + create := typeutils.WrapPollOptionablesInCreate(notes...) + if _, err := f.FederatingActor().Send(ctx, outboxIRI, create); err != nil { + return gtserror.Newf("error sending Create activity via outbox %s: %w", outboxIRI, err) } return nil @@ -256,13 +282,8 @@ func (f *federate) UpdateStatus(ctx context.Context, status *gtsmodel.Status) er return gtserror.Newf("error converting status to Statusable: %w", err) } - // Use ActivityStreams Statusable type as Object of Update. - update, err := f.converter.WrapStatusableInUpdate(statusable, false) - if err != nil { - return gtserror.Newf("error wrapping Statusable in Update: %w", err) - } - - // Send the Update activity with Statusable via the Actor's outbox. + // Send an Update activity with Statusable via the Actor's outbox. + update := typeutils.WrapStatusableInUpdate(statusable, false) if _, err := f.FederatingActor().Send(ctx, outboxIRI, update); err != nil { return gtserror.Newf("error sending Update activity via outbox %s: %w", outboxIRI, err) } diff --git a/internal/processing/workers/fromclientapi.go b/internal/processing/workers/fromclientapi.go index 789145226..e3f1e2d76 100644 --- a/internal/processing/workers/fromclientapi.go +++ b/internal/processing/workers/fromclientapi.go @@ -93,6 +93,13 @@ func (p *Processor) ProcessFromClientAPI(ctx context.Context, cMsg messages.From case ap.ObjectNote: return p.clientAPI.CreateStatus(ctx, cMsg) + // CREATE QUESTION + // (note we don't handle poll *votes* as AS + // question type when federating (just notes), + // but it makes for a nicer type switch here. + case ap.ActivityQuestion: + return p.clientAPI.CreatePollVote(ctx, cMsg) + // CREATE FOLLOW (request) case ap.ActivityFollow: return p.clientAPI.CreateFollowReq(ctx, cMsg) @@ -189,7 +196,7 @@ func (p *Processor) ProcessFromClientAPI(ctx context.Context, cMsg messages.From } } - return nil + return gtserror.Newf("unhandled: %s %s", cMsg.APActivityType, cMsg.APObjectType) } func (p *clientAPI) CreateAccount(ctx context.Context, cMsg messages.FromClientAPI) error { @@ -205,7 +212,7 @@ func (p *clientAPI) CreateAccount(ctx context.Context, cMsg messages.FromClientA } if err := p.surface.emailPleaseConfirm(ctx, user, account.Username); err != nil { - return gtserror.Newf("error emailing %s: %w", account.Username, err) + log.Errorf(ctx, "error emailing confirm: %v", err) } return nil @@ -218,7 +225,7 @@ func (p *clientAPI) CreateStatus(ctx context.Context, cMsg messages.FromClientAP } if err := p.surface.timelineAndNotifyStatus(ctx, status); err != nil { - return gtserror.Newf("error timelining status: %w", err) + log.Errorf(ctx, "error timelining and notifying status: %v", err) } if status.InReplyToID != "" { @@ -228,7 +235,48 @@ func (p *clientAPI) CreateStatus(ctx context.Context, cMsg messages.FromClientAP } if err := p.federate.CreateStatus(ctx, status); err != nil { - return gtserror.Newf("error federating status: %w", err) + log.Errorf(ctx, "error federating status: %v", err) + } + + return nil +} + +func (p *clientAPI) CreatePollVote(ctx context.Context, cMsg messages.FromClientAPI) error { + // Cast the create poll vote attached to message. + vote, ok := cMsg.GTSModel.(*gtsmodel.PollVote) + if !ok { + return gtserror.Newf("cannot cast %T -> *gtsmodel.Pollvote", cMsg.GTSModel) + } + + // Ensure the vote is fully populated in order to get original poll. + if err := p.state.DB.PopulatePollVote(ctx, vote); err != nil { + return gtserror.Newf("error populating poll vote from db: %w", err) + } + + // Ensure the poll on the vote is fully populated to get origin status. + if err := p.state.DB.PopulatePoll(ctx, vote.Poll); err != nil { + return gtserror.Newf("error populating poll from db: %w", err) + } + + // Get the origin status, + // (also set the poll on it). + status := vote.Poll.Status + status.Poll = vote.Poll + + // Interaction counts changed on the source status, uncache from timelines. + p.surface.invalidateStatusFromTimelines(ctx, vote.Poll.StatusID) + + if *status.Local { + // These are poll votes in a local status, we only need to + // federate the updated status model with latest vote counts. + if err := p.federate.UpdateStatus(ctx, status); err != nil { + log.Errorf(ctx, "error federating status update: %v", err) + } + } else { + // These are votes in a remote poll, federate to origin the new poll vote(s). + if err := p.federate.CreatePollVote(ctx, vote.Poll, vote); err != nil { + log.Errorf(ctx, "error federating poll vote: %v", err) + } } return nil @@ -241,14 +289,17 @@ func (p *clientAPI) CreateFollowReq(ctx context.Context, cMsg messages.FromClien } if err := p.surface.notifyFollowRequest(ctx, followRequest); err != nil { - return gtserror.Newf("error notifying follow request: %w", err) + log.Errorf(ctx, "error notifying follow request: %v", err) } + // Convert the follow request to follow model (requests are sent as follows). + follow := p.converter.FollowRequestToFollow(ctx, followRequest) + if err := p.federate.Follow( ctx, - p.converter.FollowRequestToFollow(ctx, followRequest), + follow, ); err != nil { - return gtserror.Newf("error federating follow: %w", err) + log.Errorf(ctx, "error federating follow request: %v", err) } return nil @@ -266,7 +317,7 @@ func (p *clientAPI) CreateLike(ctx context.Context, cMsg messages.FromClientAPI) } if err := p.surface.notifyFave(ctx, fave); err != nil { - return gtserror.Newf("error notifying fave: %w", err) + log.Errorf(ctx, "error notifying fave: %v", err) } // Interaction counts changed on the faved status; @@ -274,7 +325,7 @@ func (p *clientAPI) CreateLike(ctx context.Context, cMsg messages.FromClientAPI) p.surface.invalidateStatusFromTimelines(ctx, fave.StatusID) if err := p.federate.Like(ctx, fave); err != nil { - return gtserror.Newf("error federating like: %w", err) + log.Errorf(ctx, "error federating like: %v", err) } return nil @@ -288,12 +339,12 @@ func (p *clientAPI) CreateAnnounce(ctx context.Context, cMsg messages.FromClient // Timeline and notify the boost wrapper status. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { - return gtserror.Newf("error timelining boost: %w", err) + log.Errorf(ctx, "error timelining and notifying status: %v", err) } // Notify the boost target account. if err := p.surface.notifyAnnounce(ctx, boost); err != nil { - return gtserror.Newf("error notifying boost: %w", err) + log.Errorf(ctx, "error notifying boost: %v", err) } // Interaction counts changed on the boosted status; @@ -301,7 +352,7 @@ func (p *clientAPI) CreateAnnounce(ctx context.Context, cMsg messages.FromClient p.surface.invalidateStatusFromTimelines(ctx, boost.BoostOfID) if err := p.federate.Announce(ctx, boost); err != nil { - return gtserror.Newf("error federating announce: %w", err) + log.Errorf(ctx, "error federating announce: %v", err) } return nil @@ -335,7 +386,7 @@ func (p *clientAPI) CreateBlock(ctx context.Context, cMsg messages.FromClientAPI // TODO: same with bookmarks? if err := p.federate.Block(ctx, block); err != nil { - return gtserror.Newf("error federating block: %w", err) + log.Errorf(ctx, "error federating block: %v", err) } return nil @@ -350,7 +401,19 @@ func (p *clientAPI) UpdateStatus(ctx context.Context, cMsg messages.FromClientAP // Federate the updated status changes out remotely. if err := p.federate.UpdateStatus(ctx, status); err != nil { - return gtserror.Newf("error federating status update: %w", err) + log.Errorf(ctx, "error federating status update: %v", err) + } + + // Status representation has changed, invalidate from timelines. + p.surface.invalidateStatusFromTimelines(ctx, status.ID) + + if status.Poll != nil && status.Poll.Closing { + + // If the latest status has a newly closed poll, at least compared + // to the existing version, then notify poll close to all voters. + if err := p.surface.notifyPollClose(ctx, status); err != nil { + log.Errorf(ctx, "error notifying poll close: %v", err) + } } return nil @@ -363,7 +426,7 @@ func (p *clientAPI) UpdateAccount(ctx context.Context, cMsg messages.FromClientA } if err := p.federate.UpdateAccount(ctx, account); err != nil { - return gtserror.Newf("error federating account update: %w", err) + log.Errorf(ctx, "error federating account update: %v", err) } return nil @@ -382,7 +445,7 @@ func (p *clientAPI) UpdateReport(ctx context.Context, cMsg messages.FromClientAP } if err := p.surface.emailReportClosed(ctx, report); err != nil { - return gtserror.Newf("error sending report closed email: %w", err) + log.Errorf(ctx, "error emailing report closed: %v", err) } return nil @@ -395,11 +458,11 @@ func (p *clientAPI) AcceptFollow(ctx context.Context, cMsg messages.FromClientAP } if err := p.surface.notifyFollow(ctx, follow); err != nil { - return gtserror.Newf("error notifying follow: %w", err) + log.Errorf(ctx, "error notifying follow: %v", err) } if err := p.federate.AcceptFollow(ctx, follow); err != nil { - return gtserror.Newf("error federating follow request accept: %w", err) + log.Errorf(ctx, "error federating follow accept: %v", err) } return nil @@ -415,7 +478,7 @@ func (p *clientAPI) RejectFollowRequest(ctx context.Context, cMsg messages.FromC ctx, p.converter.FollowRequestToFollow(ctx, followReq), ); err != nil { - return gtserror.Newf("error federating reject follow: %w", err) + log.Errorf(ctx, "error federating follow reject: %v", err) } return nil @@ -428,7 +491,7 @@ func (p *clientAPI) UndoFollow(ctx context.Context, cMsg messages.FromClientAPI) } if err := p.federate.UndoFollow(ctx, follow); err != nil { - return gtserror.Newf("error federating undo follow: %w", err) + log.Errorf(ctx, "error federating follow undo: %v", err) } return nil @@ -441,7 +504,7 @@ func (p *clientAPI) UndoBlock(ctx context.Context, cMsg messages.FromClientAPI) } if err := p.federate.UndoBlock(ctx, block); err != nil { - return gtserror.Newf("error federating undo block: %w", err) + log.Errorf(ctx, "error federating block undo: %v", err) } return nil @@ -458,7 +521,7 @@ func (p *clientAPI) UndoFave(ctx context.Context, cMsg messages.FromClientAPI) e p.surface.invalidateStatusFromTimelines(ctx, statusFave.StatusID) if err := p.federate.UndoLike(ctx, statusFave); err != nil { - return gtserror.Newf("error federating undo like: %w", err) + log.Errorf(ctx, "error federating like undo: %v", err) } return nil @@ -475,7 +538,7 @@ func (p *clientAPI) UndoAnnounce(ctx context.Context, cMsg messages.FromClientAP } if err := p.surface.deleteStatusFromTimelines(ctx, status.ID); err != nil { - return gtserror.Newf("error removing status from timelines: %w", err) + log.Errorf(ctx, "error removing timelined status: %v", err) } // Interaction counts changed on the boosted status; @@ -483,7 +546,7 @@ func (p *clientAPI) UndoAnnounce(ctx context.Context, cMsg messages.FromClientAP p.surface.invalidateStatusFromTimelines(ctx, status.BoostOfID) if err := p.federate.UndoAnnounce(ctx, status); err != nil { - return gtserror.Newf("error federating undo announce: %w", err) + log.Errorf(ctx, "error federating announce undo: %v", err) } return nil @@ -509,7 +572,7 @@ func (p *clientAPI) DeleteStatus(ctx context.Context, cMsg messages.FromClientAP } if err := p.wipeStatus(ctx, status, deleteAttachments); err != nil { - return gtserror.Newf("error wiping status: %w", err) + log.Errorf(ctx, "error wiping status: %v", err) } if status.InReplyToID != "" { @@ -519,7 +582,7 @@ func (p *clientAPI) DeleteStatus(ctx context.Context, cMsg messages.FromClientAP } if err := p.federate.DeleteStatus(ctx, status); err != nil { - return gtserror.Newf("error federating status delete: %w", err) + log.Errorf(ctx, "error federating status delete: %v", err) } return nil @@ -543,11 +606,11 @@ func (p *clientAPI) DeleteAccount(ctx context.Context, cMsg messages.FromClientA } if err := p.federate.DeleteAccount(ctx, cMsg.TargetAccount); err != nil { - return gtserror.Newf("error federating account delete: %w", err) + log.Errorf(ctx, "error federating account delete: %v", err) } if err := p.account.Delete(ctx, cMsg.TargetAccount, originID); err != nil { - return gtserror.Newf("error deleting account: %w", err) + log.Errorf(ctx, "error deleting account: %v", err) } return nil @@ -563,12 +626,12 @@ func (p *clientAPI) ReportAccount(ctx context.Context, cMsg messages.FromClientA // remote instance if desired. if *report.Forwarded { if err := p.federate.Flag(ctx, report); err != nil { - return gtserror.Newf("error federating report: %w", err) + log.Errorf(ctx, "error federating flag: %v", err) } } if err := p.surface.emailReportOpened(ctx, report); err != nil { - return gtserror.Newf("error sending report opened email: %w", err) + log.Errorf(ctx, "error emailing report opened: %v", err) } return nil diff --git a/internal/processing/workers/fromfediapi.go b/internal/processing/workers/fromfediapi.go index 1ce3b6076..2b0bfa9fa 100644 --- a/internal/processing/workers/fromfediapi.go +++ b/internal/processing/workers/fromfediapi.go @@ -114,6 +114,10 @@ func (p *Processor) ProcessFromFediAPI(ctx context.Context, fMsg messages.FromFe // CREATE FLAG/REPORT case ap.ActivityFlag: return p.fediAPI.CreateFlag(ctx, fMsg) + + // CREATE QUESTION + case ap.ActivityQuestion: + return p.fediAPI.CreatePollVote(ctx, fMsg) } // UPDATE SOMETHING @@ -170,7 +174,7 @@ func (p *fediAPI) CreateStatus(ctx context.Context, fMsg messages.FromFediAPI) e // Both situations we need to parse account URI to fetch it. accountURI, err := url.Parse(status.AccountURI) if err != nil { - return err + return gtserror.Newf("error parsing account uri: %w", err) } // Ensure that account for this status has been deref'd. @@ -180,7 +184,7 @@ func (p *fediAPI) CreateStatus(ctx context.Context, fMsg messages.FromFediAPI) e accountURI, ) if err != nil { - return err + return gtserror.Newf("error getting account by uri: %w", err) } } @@ -192,7 +196,48 @@ func (p *fediAPI) CreateStatus(ctx context.Context, fMsg messages.FromFediAPI) e } if err := p.surface.timelineAndNotifyStatus(ctx, status); err != nil { - return gtserror.Newf("error timelining status: %w", err) + log.Errorf(ctx, "error timelining and notifying status: %v", err) + } + + return nil +} + +func (p *fediAPI) CreatePollVote(ctx context.Context, fMsg messages.FromFediAPI) error { + // Cast poll vote type from the worker message. + vote, ok := fMsg.GTSModel.(*gtsmodel.PollVote) + if !ok { + return gtserror.Newf("cannot cast %T -> *gtsmodel.PollVote", fMsg.GTSModel) + } + + // Insert the new poll vote in the database. + if err := p.state.DB.PutPollVote(ctx, vote); err != nil { + return gtserror.Newf("error inserting poll vote in db: %w", err) + } + + // Ensure the poll vote is fully populated at this point. + if err := p.state.DB.PopulatePollVote(ctx, vote); err != nil { + return gtserror.Newf("error populating poll vote from db: %w", err) + } + + // Ensure the poll on the vote is fully populated to get origin status. + if err := p.state.DB.PopulatePoll(ctx, vote.Poll); err != nil { + return gtserror.Newf("error populating poll from db: %w", err) + } + + // Get the origin status, + // (also set the poll on it). + status := vote.Poll.Status + status.Poll = vote.Poll + + // Interaction counts changed on the source status, uncache from timelines. + p.surface.invalidateStatusFromTimelines(ctx, vote.Poll.StatusID) + + if *status.Local { + // These were poll votes in a local status, we need to + // federate the updated status model with latest vote counts. + if err := p.federate.UpdateStatus(ctx, status); err != nil { + log.Errorf(ctx, "error federating status update: %v", err) + } } return nil @@ -269,12 +314,10 @@ func (p *fediAPI) CreateFollowReq(ctx context.Context, fMsg messages.FromFediAPI } if *followRequest.TargetAccount.Locked { - // Account on our instance is locked: - // just notify the follow request. + // Account on our instance is locked: just notify the follow request. if err := p.surface.notifyFollowRequest(ctx, followRequest); err != nil { - return gtserror.Newf("error notifying follow request: %w", err) + log.Errorf(ctx, "error notifying follow request: %v", err) } - return nil } @@ -291,11 +334,11 @@ func (p *fediAPI) CreateFollowReq(ctx context.Context, fMsg messages.FromFediAPI } if err := p.federate.AcceptFollow(ctx, follow); err != nil { - return gtserror.Newf("error federating accept follow request: %w", err) + log.Errorf(ctx, "error federating follow request accept: %v", err) } if err := p.surface.notifyFollow(ctx, follow); err != nil { - return gtserror.Newf("error notifying follow: %w", err) + log.Errorf(ctx, "error notifying follow: %v", err) } return nil @@ -313,7 +356,7 @@ func (p *fediAPI) CreateLike(ctx context.Context, fMsg messages.FromFediAPI) err } if err := p.surface.notifyFave(ctx, fave); err != nil { - return gtserror.Newf("error notifying fave: %w", err) + log.Errorf(ctx, "error notifying fave: %v", err) } // Interaction counts changed on the faved status; @@ -354,11 +397,11 @@ func (p *fediAPI) CreateAnnounce(ctx context.Context, fMsg messages.FromFediAPI) // Timeline and notify the announce. if err := p.surface.timelineAndNotifyStatus(ctx, status); err != nil { - return gtserror.Newf("error timelining status: %w", err) + log.Errorf(ctx, "error timelining and notifying status: %v", err) } if err := p.surface.notifyAnnounce(ctx, status); err != nil { - return gtserror.Newf("error notifying status: %w", err) + log.Errorf(ctx, "error notifying announce: %v", err) } // Interaction counts changed on the boosted status; @@ -382,7 +425,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.AccountID, block.TargetAccountID, ); err != nil { - return gtserror.Newf("%w", err) + log.Errorf(ctx, "error wiping items from block -> target's home timeline: %v", err) } if err := p.state.Timelines.Home.WipeItemsFromAccountID( @@ -390,7 +433,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.TargetAccountID, block.AccountID, ); err != nil { - return gtserror.Newf("%w", err) + log.Errorf(ctx, "error wiping items from target -> block's home timeline: %v", err) } // Now list timelines. @@ -399,7 +442,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.AccountID, block.TargetAccountID, ); err != nil { - return gtserror.Newf("%w", err) + log.Errorf(ctx, "error wiping items from block -> target's list timeline(s): %v", err) } if err := p.state.Timelines.List.WipeItemsFromAccountID( @@ -407,7 +450,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.TargetAccountID, block.AccountID, ); err != nil { - return gtserror.Newf("%w", err) + log.Errorf(ctx, "error wiping items from target -> block's list timeline(s): %v", err) } // Remove any follows that existed between blocker + blockee. @@ -416,10 +459,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.AccountID, block.TargetAccountID, ); err != nil { - return gtserror.Newf( - "db error deleting follow from %s targeting %s: %w", - block.AccountID, block.TargetAccountID, err, - ) + log.Errorf(ctx, "error deleting follow from block -> target: %v", err) } if err := p.state.DB.DeleteFollow( @@ -427,10 +467,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.TargetAccountID, block.AccountID, ); err != nil { - return gtserror.Newf( - "db error deleting follow from %s targeting %s: %w", - block.TargetAccountID, block.AccountID, err, - ) + log.Errorf(ctx, "error deleting follow from target -> block: %v", err) } // Remove any follow requests that existed between blocker + blockee. @@ -439,10 +476,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.AccountID, block.TargetAccountID, ); err != nil { - return gtserror.Newf( - "db error deleting follow request from %s targeting %s: %w", - block.AccountID, block.TargetAccountID, err, - ) + log.Errorf(ctx, "error deleting follow request from block -> target: %v", err) } if err := p.state.DB.DeleteFollowRequest( @@ -450,10 +484,7 @@ func (p *fediAPI) CreateBlock(ctx context.Context, fMsg messages.FromFediAPI) er block.TargetAccountID, block.AccountID, ); err != nil { - return gtserror.Newf( - "db error deleting follow request from %s targeting %s: %w", - block.TargetAccountID, block.AccountID, err, - ) + log.Errorf(ctx, "error deleting follow request from target -> block: %v", err) } return nil @@ -469,7 +500,7 @@ func (p *fediAPI) CreateFlag(ctx context.Context, fMsg messages.FromFediAPI) err // - notify admins by dm / notification if err := p.surface.emailReportOpened(ctx, incomingReport); err != nil { - return gtserror.Newf("error sending report opened email: %w", err) + log.Errorf(ctx, "error emailing report opened: %v", err) } return nil @@ -497,7 +528,7 @@ func (p *fediAPI) UpdateAccount(ctx context.Context, fMsg messages.FromFediAPI) true, // Force refresh. ) if err != nil { - return gtserror.Newf("error refreshing updated account: %w", err) + log.Errorf(ctx, "error refreshing account: %v", err) } return nil @@ -514,7 +545,7 @@ func (p *fediAPI) UpdateStatus(ctx context.Context, fMsg messages.FromFediAPI) e apStatus, _ := fMsg.APObjectModel.(ap.Statusable) // Fetch up-to-date attach status attachments, etc. - _, statusable, err := p.federate.RefreshStatus( + status, _, err := p.federate.RefreshStatus( ctx, fMsg.ReceivingAccount.Username, existing, @@ -522,12 +553,19 @@ func (p *fediAPI) UpdateStatus(ctx context.Context, fMsg messages.FromFediAPI) e true, ) if err != nil { - return gtserror.Newf("error refreshing updated status: %w", err) + log.Errorf(ctx, "error refreshing status: %v", err) } - if statusable != nil { - // Status representation was refetched, uncache from timelines. - p.surface.invalidateStatusFromTimelines(ctx, existing.ID) + // Status representation was refetched, uncache from timelines. + p.surface.invalidateStatusFromTimelines(ctx, status.ID) + + if status.Poll != nil && status.Poll.Closing { + + // If the latest status has a newly closed poll, at least compared + // to the existing version, then notify poll close to all voters. + if err := p.surface.notifyPollClose(ctx, status); err != nil { + log.Errorf(ctx, "error sending poll notification: %v", err) + } } return nil @@ -545,7 +583,7 @@ func (p *fediAPI) DeleteStatus(ctx context.Context, fMsg messages.FromFediAPI) e } if err := p.wipeStatus(ctx, status, deleteAttachments); err != nil { - return gtserror.Newf("error wiping status: %w", err) + log.Errorf(ctx, "error wiping status: %v", err) } if status.InReplyToID != "" { @@ -564,7 +602,7 @@ func (p *fediAPI) DeleteAccount(ctx context.Context, fMsg messages.FromFediAPI) } if err := p.account.Delete(ctx, account, account.ID); err != nil { - return gtserror.Newf("error deleting account: %w", err) + log.Errorf(ctx, "error deleting account: %v", err) } return nil diff --git a/internal/processing/workers/fromfediapi_test.go b/internal/processing/workers/fromfediapi_test.go index b8d86ac45..952c008cc 100644 --- a/internal/processing/workers/fromfediapi_test.go +++ b/internal/processing/workers/fromfediapi_test.go @@ -347,8 +347,15 @@ func (suite *FromFediAPITestSuite) TestProcessAccountDelete() { suite.FailNow("timeout waiting for statuses to be deleted") } - dbAccount, err := suite.db.GetAccountByID(ctx, deletedAccount.ID) - suite.NoError(err) + var dbAccount *gtsmodel.Account + + // account data should be zeroed. + if !testrig.WaitFor(func() bool { + dbAccount, err = suite.db.GetAccountByID(ctx, deletedAccount.ID) + return err == nil && dbAccount.DisplayName == "" + }) { + suite.FailNow("timeout waiting for statuses to be deleted") + } suite.Empty(dbAccount.Note) suite.Empty(dbAccount.DisplayName) diff --git a/internal/processing/workers/surfacenotify.go b/internal/processing/workers/surfacenotify.go index b99fa3ad3..2dc60023c 100644 --- a/internal/processing/workers/surfacenotify.go +++ b/internal/processing/workers/surfacenotify.go @@ -35,12 +35,25 @@ func (s *surface) notifyMentions( ctx context.Context, status *gtsmodel.Status, ) error { - var ( - mentions = status.Mentions - errs = gtserror.NewMultiError(len(mentions)) - ) + var errs gtserror.MultiError + + for _, mention := range status.Mentions { + // Set status on the mention (stops + // the below function populating it). + mention.Status = status + + // Beforehand, ensure the passed mention is fully populated. + if err := s.state.DB.PopulateMention(ctx, mention); err != nil { + errs.Appendf("error populating mention %s: %w", mention.ID, err) + continue + } + + if mention.TargetAccount.IsRemote() { + // no need to notify + // remote accounts. + continue + } - for _, mention := range mentions { // Ensure thread not muted // by mentioned account. muted, err := s.state.DB.IsThreadMutedByAccount( @@ -48,9 +61,8 @@ func (s *surface) notifyMentions( status.ThreadID, mention.TargetAccountID, ) - if err != nil { - errs.Append(err) + errs.Appendf("error checking status thread mute %s: %w", status.ThreadID, err) continue } @@ -61,14 +73,16 @@ func (s *surface) notifyMentions( continue } - if err := s.notify( - ctx, + // notify mentioned + // by status author. + if err := s.notify(ctx, gtsmodel.NotificationMention, - mention.TargetAccountID, - mention.OriginAccountID, + mention.TargetAccount, + mention.OriginAccount, mention.StatusID, ); err != nil { - errs.Append(err) + errs.Appendf("error notifying mention target %s: %w", mention.TargetAccountID, err) + continue } } @@ -79,15 +93,30 @@ func (s *surface) notifyMentions( // follow request that they have a new follow request. func (s *surface) notifyFollowRequest( ctx context.Context, - followRequest *gtsmodel.FollowRequest, + followReq *gtsmodel.FollowRequest, ) error { - return s.notify( - ctx, + // Beforehand, ensure the passed follow request is fully populated. + if err := s.state.DB.PopulateFollowRequest(ctx, followReq); err != nil { + return gtserror.Newf("error populating follow request %s: %w", followReq.ID, err) + } + + if followReq.TargetAccount.IsRemote() { + // no need to notify + // remote accounts. + return nil + } + + // Now notify the follow request itself. + if err := s.notify(ctx, gtsmodel.NotificationFollowRequest, - followRequest.TargetAccountID, - followRequest.AccountID, + followReq.TargetAccount, + followReq.Account, "", - ) + ); err != nil { + return gtserror.Newf("error notifying follow target %s: %w", followReq.TargetAccountID, err) + } + + return nil } // notifyFollow notifies the target of the given follow that @@ -98,6 +127,17 @@ func (s *surface) notifyFollow( ctx context.Context, follow *gtsmodel.Follow, ) error { + // Beforehand, ensure the passed follow is fully populated. + if err := s.state.DB.PopulateFollow(ctx, follow); err != nil { + return gtserror.Newf("error populating follow %s: %w", follow.ID, err) + } + + if follow.TargetAccount.IsRemote() { + // no need to notify + // remote accounts. + return nil + } + // Check if previous follow req notif exists. prevNotif, err := s.state.DB.GetNotification( gtscontext.SetBarebones(ctx), @@ -107,24 +147,28 @@ func (s *surface) notifyFollow( "", ) if err != nil && !errors.Is(err, db.ErrNoEntries) { - return gtserror.Newf("db error checking for previous follow request notification: %w", err) + return gtserror.Newf("error getting notification: %w", err) } if prevNotif != nil { - // Previous notif existed, delete it. - if err := s.state.DB.DeleteNotificationByID(ctx, prevNotif.ID); err != nil { - return gtserror.Newf("db error removing previous follow request notification %s: %w", prevNotif.ID, err) + // Previous follow request notif existed, delete it before creating new. + if err := s.state.DB.DeleteNotificationByID(ctx, prevNotif.ID); // nocollapse + err != nil && !errors.Is(err, db.ErrNoEntries) { + return gtserror.Newf("error deleting notification %s: %w", prevNotif.ID, err) } } // Now notify the follow itself. - return s.notify( - ctx, + if err := s.notify(ctx, gtsmodel.NotificationFollow, - follow.TargetAccountID, - follow.AccountID, + follow.TargetAccount, + follow.Account, "", - ) + ); err != nil { + return gtserror.Newf("error notifying follow target %s: %w", follow.TargetAccountID, err) + } + + return nil } // notifyFave notifies the target of the given @@ -138,6 +182,17 @@ func (s *surface) notifyFave( return nil } + // Beforehand, ensure the passed status fave is fully populated. + if err := s.state.DB.PopulateStatusFave(ctx, fave); err != nil { + return gtserror.Newf("error populating fave %s: %w", fave.ID, err) + } + + if fave.TargetAccount.IsRemote() { + // no need to notify + // remote accounts. + return nil + } + // Ensure favee hasn't // muted the thread. muted, err := s.state.DB.IsThreadMutedByAccount( @@ -145,24 +200,28 @@ func (s *surface) notifyFave( fave.Status.ThreadID, fave.TargetAccountID, ) - if err != nil { - return err + return gtserror.Newf("error checking status thread mute %s: %w", fave.StatusID, err) } if muted { - // Boostee doesn't want + // Favee doesn't want // notifs for this thread. return nil } - return s.notify( - ctx, + // notify status author + // of fave by account. + if err := s.notify(ctx, gtsmodel.NotificationFave, - fave.TargetAccountID, - fave.AccountID, + fave.TargetAccount, + fave.Account, fave.StatusID, - ) + ); err != nil { + return gtserror.Newf("error notifying status author %s: %w", fave.TargetAccountID, err) + } + + return nil } // notifyAnnounce notifies the status boost target @@ -176,14 +235,19 @@ func (s *surface) notifyAnnounce( return nil } - if status.BoostOf == nil { - // No boosted status - // set, nothing to do. + if status.BoostOfAccountID == status.AccountID { + // Self-boost, nothing to do. return nil } - if status.BoostOfAccountID == status.AccountID { - // Self-boost, nothing to do. + // Beforehand, ensure the passed status is fully populated. + if err := s.state.DB.PopulateStatus(ctx, status); err != nil { + return gtserror.Newf("error populating status %s: %w", status.ID, err) + } + + if status.BoostOfAccount.IsRemote() { + // no need to notify + // remote accounts. return nil } @@ -196,7 +260,7 @@ func (s *surface) notifyAnnounce( ) if err != nil { - return err + return gtserror.Newf("error checking status thread mute %s: %w", status.BoostOfID, err) } if muted { @@ -205,13 +269,68 @@ func (s *surface) notifyAnnounce( return nil } - return s.notify( - ctx, + // notify status author + // of boost by account. + if err := s.notify(ctx, gtsmodel.NotificationReblog, - status.BoostOfAccountID, - status.AccountID, + status.BoostOfAccount, + status.Account, status.ID, - ) + ); err != nil { + return gtserror.Newf("error notifying status author %s: %w", status.BoostOfAccountID, err) + } + + return nil +} + +func (s *surface) notifyPollClose(ctx context.Context, status *gtsmodel.Status) error { + // Beforehand, ensure the passed status is fully populated. + if err := s.state.DB.PopulateStatus(ctx, status); err != nil { + return gtserror.Newf("error populating status %s: %w", status.ID, err) + } + + // Fetch all votes in the attached status poll. + votes, err := s.state.DB.GetPollVotes(ctx, status.PollID) + if err != nil { + return gtserror.Newf("error getting poll %s votes: %w", status.PollID, err) + } + + var errs gtserror.MultiError + + if status.Account.IsLocal() { + // Send a notification to the status + // author that their poll has closed! + if err := s.notify(ctx, + gtsmodel.NotificationPoll, + status.Account, + status.Account, + status.ID, + ); err != nil { + errs.Appendf("error notifying poll author: %w", err) + } + } + + for _, vote := range votes { + if vote.Account.IsRemote() { + // no need to notify + // remote accounts. + continue + } + + // notify voter that + // poll has been closed. + if err := s.notify(ctx, + gtsmodel.NotificationMention, + vote.Account, + status.Account, + status.ID, + ); err != nil { + errs.Appendf("error notifying poll voter %s: %w", vote.AccountID, err) + continue + } + } + + return errs.Combine() } // notify creates, inserts, and streams a new @@ -228,17 +347,12 @@ func (s *surface) notifyAnnounce( func (s *surface) notify( ctx context.Context, notificationType gtsmodel.NotificationType, - targetAccountID string, - originAccountID string, + targetAccount *gtsmodel.Account, + originAccount *gtsmodel.Account, statusID string, ) error { - targetAccount, err := s.state.DB.GetAccountByID(ctx, targetAccountID) - if err != nil { - return gtserror.Newf("error getting target account %s: %w", targetAccountID, err) - } - - if !targetAccount.IsLocal() { - // Nothing to do. + if targetAccount.IsRemote() { + // nothing to do. return nil } @@ -247,8 +361,8 @@ func (s *surface) notify( if _, err := s.state.DB.GetNotification( gtscontext.SetBarebones(ctx), notificationType, - targetAccountID, - originAccountID, + targetAccount.ID, + originAccount.ID, statusID, ); err == nil { // Notification exists; @@ -264,8 +378,10 @@ func (s *surface) notify( notif := >smodel.Notification{ ID: id.NewULID(), NotificationType: notificationType, - TargetAccountID: targetAccountID, - OriginAccountID: originAccountID, + TargetAccountID: targetAccount.ID, + TargetAccount: targetAccount, + OriginAccountID: originAccount.ID, + OriginAccount: originAccount, StatusID: statusID, } diff --git a/internal/processing/workers/surfacetimeline.go b/internal/processing/workers/surfacetimeline.go index 15263cf78..baebdbc66 100644 --- a/internal/processing/workers/surfacetimeline.go +++ b/internal/processing/workers/surfacetimeline.go @@ -85,7 +85,7 @@ func (s *surface) timelineAndNotifyStatusForFollowers( follows []*gtsmodel.Follow, ) error { var ( - errs = new(gtserror.MultiError) + errs gtserror.MultiError boost = status.BoostOfID != "" reply = status.InReplyToURI != "" ) @@ -117,7 +117,7 @@ func (s *surface) timelineAndNotifyStatusForFollowers( ctx, status, follow, - errs, + &errs, ) // Add status to home timeline for owner @@ -160,11 +160,10 @@ func (s *surface) timelineAndNotifyStatusForFollowers( // - This is a top-level post (not a reply or boost). // // That means we can officially notify this one. - if err := s.notify( - ctx, + if err := s.notify(ctx, gtsmodel.NotificationStatus, - follow.AccountID, - status.AccountID, + follow.Account, + status.Account, status.ID, ); err != nil { errs.Appendf("error notifying account %s about new status: %w", follow.AccountID, err) diff --git a/internal/processing/workers/wipestatus.go b/internal/processing/workers/wipestatus.go index ab59f14be..90a037928 100644 --- a/internal/processing/workers/wipestatus.go +++ b/internal/processing/workers/wipestatus.go @@ -85,6 +85,21 @@ func wipeStatusF(state *state.State, media *media.Processor, surface *surface) w errs.Appendf("error deleting status faves: %w", err) } + if pollID := statusToDelete.PollID; pollID != "" { + // Delete this poll by ID from the database. + if err := state.DB.DeletePollByID(ctx, pollID); err != nil { + errs.Appendf("error deleting status poll: %w", err) + } + + // Delete any poll votes pointing to this poll ID. + if err := state.DB.DeletePollVotes(ctx, pollID); err != nil { + errs.Appendf("error deleting status poll votes: %w", err) + } + + // Cancel any scheduled expiry task for poll. + _ = state.Workers.Scheduler.Cancel(pollID) + } + // delete all boosts for this status + remove them from timelines boosts, err := state.DB.GetStatusBoosts( // we MUST set a barebones context here, |