From c8e7714a124e18678a68fb7e9d44fa8e1f0994d0 Mon Sep 17 00:00:00 2001 From: InfiniteStash <117855276+InfiniteStash@users.noreply.github.com> Date: Wed, 8 Jan 2025 21:38:36 +0100 Subject: [PATCH] Fix notification triggering (#898) --- main.go | 2 ++ pkg/api/resolver_mutation_edit.go | 20 +++++++------- .../databasetest/database_test_utils.go | 2 ++ pkg/manager/edit/edit.go | 4 +-- pkg/manager/notifications/notifications.go | 27 ++++++++++++++----- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index dcb98d5..c1ec414 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/stashapp/stash-box/pkg/manager" "github.com/stashapp/stash-box/pkg/manager/config" "github.com/stashapp/stash-box/pkg/manager/cron" + "github.com/stashapp/stash-box/pkg/manager/notifications" "github.com/stashapp/stash-box/pkg/sqlx" "github.com/stashapp/stash-box/pkg/user" ) @@ -34,6 +35,7 @@ func main() { user.CreateSystemUsers(txnMgr.Repo(context.Background())) api.Start(txnMgr, ui) cron.Init(txnMgr) + notifications.Init(txnMgr) image.InitResizer() diff --git a/pkg/api/resolver_mutation_edit.go b/pkg/api/resolver_mutation_edit.go index dd7df09..618637d 100644 --- a/pkg/api/resolver_mutation_edit.go +++ b/pkg/api/resolver_mutation_edit.go @@ -72,7 +72,7 @@ func (r *mutationResolver) SceneEdit(ctx context.Context, input models.SceneEdit }) if err == nil { - go notifications.OnCreateEdit(fac, newEdit) + go notifications.OnCreateEdit(newEdit) } return newEdit, err @@ -108,7 +108,7 @@ func (r *mutationResolver) SceneEditUpdate(ctx context.Context, id uuid.UUID, in }) if err == nil { - go notifications.OnUpdateEdit(fac, existingEdit) + go notifications.OnUpdateEdit(existingEdit) } return existingEdit, err @@ -149,7 +149,7 @@ func (r *mutationResolver) StudioEdit(ctx context.Context, input models.StudioEd }) if err == nil { - go notifications.OnCreateEdit(fac, newEdit) + go notifications.OnCreateEdit(newEdit) } return newEdit, err @@ -185,7 +185,7 @@ func (r *mutationResolver) StudioEditUpdate(ctx context.Context, id uuid.UUID, i }) if err == nil { - go notifications.OnUpdateEdit(fac, existingEdit) + go notifications.OnUpdateEdit(existingEdit) } return existingEdit, err @@ -226,7 +226,7 @@ func (r *mutationResolver) TagEdit(ctx context.Context, input models.TagEditInpu }) if err == nil { - go notifications.OnCreateEdit(fac, newEdit) + go notifications.OnCreateEdit(newEdit) } return newEdit, err @@ -262,7 +262,7 @@ func (r *mutationResolver) TagEditUpdate(ctx context.Context, id uuid.UUID, inpu }) if err == nil { - go notifications.OnUpdateEdit(fac, existingEdit) + go notifications.OnUpdateEdit(existingEdit) } return existingEdit, err @@ -309,7 +309,7 @@ func (r *mutationResolver) PerformerEdit(ctx context.Context, input models.Perfo }) if err == nil { - go notifications.OnCreateEdit(fac, newEdit) + go notifications.OnCreateEdit(newEdit) } return newEdit, err @@ -345,7 +345,7 @@ func (r *mutationResolver) PerformerEditUpdate(ctx context.Context, id uuid.UUID }) if err == nil { - go notifications.OnUpdateEdit(fac, existingEdit) + go notifications.OnUpdateEdit(existingEdit) } return existingEdit, err @@ -395,7 +395,7 @@ func (r *mutationResolver) EditVote(ctx context.Context, input models.EditVoteIn }) if err == nil && input.Vote == models.VoteTypeEnumReject { - go notifications.OnEditDownvote(fac, voteEdit) + go notifications.OnEditDownvote(voteEdit) } return voteEdit, err @@ -421,7 +421,7 @@ func (r *mutationResolver) EditComment(ctx context.Context, input models.EditCom }) if err == nil { - go notifications.OnEditComment(fac, comment) + go notifications.OnEditComment(comment) } return edit, err diff --git a/pkg/database/databasetest/database_test_utils.go b/pkg/database/databasetest/database_test_utils.go index 5d7230e..d63b81e 100644 --- a/pkg/database/databasetest/database_test_utils.go +++ b/pkg/database/databasetest/database_test_utils.go @@ -11,6 +11,7 @@ import ( "github.com/jmoiron/sqlx" "github.com/stashapp/stash-box/pkg/database" + "github.com/stashapp/stash-box/pkg/manager/notifications" "github.com/stashapp/stash-box/pkg/models" sqlxx "github.com/stashapp/stash-box/pkg/sqlx" ) @@ -60,6 +61,7 @@ func initPostgres(connString string) func() { db = database.Initialize(databaseType, connString) txnMgr := sqlxx.NewTxnMgr(db) + notifications.Init(txnMgr) repo = txnMgr.Repo(context.TODO()) return teardownPostgres diff --git a/pkg/manager/edit/edit.go b/pkg/manager/edit/edit.go index 82357d6..1f66ed0 100644 --- a/pkg/manager/edit/edit.go +++ b/pkg/manager/edit/edit.go @@ -157,7 +157,7 @@ func ApplyEdit(fac models.Repo, editID uuid.UUID, immediate bool) (*models.Edit, }) if err == nil { - go notifications.OnApplyEdit(fac, updatedEdit) + go notifications.OnApplyEdit(updatedEdit) } return updatedEdit, err @@ -197,7 +197,7 @@ func CloseEdit(fac models.Repo, editID uuid.UUID, status models.VoteStatusEnum) }) if err == nil && status != models.VoteStatusEnumCanceled { - go notifications.OnCancelEdit(fac, updatedEdit) + go notifications.OnCancelEdit(updatedEdit) } return updatedEdit, err diff --git a/pkg/manager/notifications/notifications.go b/pkg/manager/notifications/notifications.go index 79f0fee..aea93d9 100644 --- a/pkg/manager/notifications/notifications.go +++ b/pkg/manager/notifications/notifications.go @@ -2,10 +2,20 @@ package notifications import ( + "context" + "github.com/stashapp/stash-box/pkg/models" + "github.com/stashapp/stash-box/pkg/sqlx" ) -func OnApplyEdit(fac models.Repo, edit *models.Edit) { +var rfp *sqlx.TxnMgr + +func Init(txnMgr *sqlx.TxnMgr) { + rfp = txnMgr +} + +func OnApplyEdit(edit *models.Edit) { + fac := rfp.Repo(context.Background()) nqb := fac.Notification() eqb := fac.Edit() if (edit.Status == models.VoteStatusEnumAccepted.String() || edit.Status == models.VoteStatusEnumImmediateAccepted.String()) && edit.Operation == models.OperationEnumCreate.String() { @@ -22,11 +32,13 @@ func OnApplyEdit(fac models.Repo, edit *models.Edit) { } } -func OnCancelEdit(fac models.Repo, edit *models.Edit) { +func OnCancelEdit(edit *models.Edit) { + fac := rfp.Repo(context.Background()) fac.Notification().TriggerFailedEditNotifications(edit.ID) } -func OnCreateEdit(fac models.Repo, edit *models.Edit) { +func OnCreateEdit(edit *models.Edit) { + fac := rfp.Repo(context.Background()) switch edit.TargetType { case models.TargetTypeEnumPerformer.String(): fac.Notification().TriggerPerformerEditNotifications(edit.ID) @@ -37,15 +49,18 @@ func OnCreateEdit(fac models.Repo, edit *models.Edit) { } } -func OnUpdateEdit(fac models.Repo, edit *models.Edit) { +func OnUpdateEdit(edit *models.Edit) { + fac := rfp.Repo(context.Background()) fac.Notification().TriggerUpdatedEditNotifications(edit.ID) } -func OnEditDownvote(fac models.Repo, edit *models.Edit) { +func OnEditDownvote(edit *models.Edit) { + fac := rfp.Repo(context.Background()) fac.Notification().TriggerDownvoteEditNotifications(edit.ID) } -func OnEditComment(fac models.Repo, comment *models.EditComment) { +func OnEditComment(comment *models.EditComment) { + fac := rfp.Repo(context.Background()) fac.Notification().TriggerEditCommentNotifications(comment.ID) }