Skip to content

Commit

Permalink
Fix notifications for own edits (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored Jan 3, 2025
1 parent 7d5b864 commit a9163b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/manager/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func OnApplyEdit(fac models.Repo, edit *models.Edit) {
nqb := fac.Notification()
eqb := fac.Edit()
if edit.Status == models.VoteStatusEnumAccepted.String() || edit.Status == models.VoteStatusEnumImmediateAccepted.String() && edit.Operation == models.OperationEnumCreate.String() {
if (edit.Status == models.VoteStatusEnumAccepted.String() || edit.Status == models.VoteStatusEnumImmediateAccepted.String()) && edit.Operation == models.OperationEnumCreate.String() {
if edit.TargetType == models.TargetTypeEnumScene.String() && edit.Operation == models.OperationEnumCreate.String() {
sceneID, err := eqb.FindSceneID(edit.ID)
if err != nil || sceneID == nil {
Expand Down
30 changes: 20 additions & 10 deletions pkg/sqlx/querybuilder_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ func (qb *notificationsQueryBuilder) TriggerSceneCreationNotifications(sceneID u
query := `
INSERT INTO notifications
SELECT N.user_id, N.type, $1
FROM scenes S JOIN studio_favorites SF ON S.studio_id = SF.studio_id
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_SCENE'
FROM scenes S
JOIN scene_edits SE ON S.id = SE.scene_id
JOIN edits E ON SE.edit_id = E.id AND E.operation = 'CREATE'
JOIN studio_favorites SF ON S.studio_id = SF.studio_id
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_SCENE' AND E.user_id != N.user_id
WHERE S.id = $1
UNION
SELECT N.user_id, N.type, $1
FROM scene_performers SP JOIN performer_favorites PF ON SP.performer_id = PF.performer_id
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_SCENE'
FROM scene_performers SP
JOIN scene_edits SE ON SP.scene_id = SE.scene_id
JOIN edits E ON SE.edit_id = E.id AND E.operation = 'CREATE'
JOIN performer_favorites PF ON SP.performer_id = PF.performer_id
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_SCENE' AND E.user_id != N.user_id
WHERE SP.scene_id = $1
`
err := qb.dbi.RawExec(query, args)
Expand All @@ -50,8 +56,10 @@ func (qb *notificationsQueryBuilder) TriggerPerformerEditNotifications(editID uu
query := `
INSERT INTO notifications
SELECT N.user_id, N.type, $1
FROM performer_edits PE JOIN performer_favorites PF ON PE.performer_id = PF.performer_id
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_EDIT'
FROM performer_edits PE
JOIN edits E ON PE.edit_id = E.id
JOIN performer_favorites PF ON PE.performer_id = PF.performer_id
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_EDIT' AND N.user_id != E.user_id
WHERE PE.edit_id = $1
`
err := qb.dbi.RawExec(query, args)
Expand All @@ -65,8 +73,9 @@ func (qb *notificationsQueryBuilder) TriggerStudioEditNotifications(editID uuid.
INSERT INTO notifications
SELECT N.user_id, N.type, $1
FROM studio_edits SE
JOIN edits E ON SE.edit_id = E.id
JOIN studio_favorites SF ON SE.studio_id = SF.studio_id
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_EDIT'
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_EDIT' AND N.user_id != E.user_id
WHERE SE.edit_id = $1
`
err := qb.dbi.RawExec(query, args)
Expand All @@ -80,14 +89,15 @@ func (qb *notificationsQueryBuilder) TriggerSceneEditNotifications(editID uuid.U
INSERT INTO notifications
SELECT N.user_id, N.type, $1
FROM edits E JOIN studio_favorites SF ON (E.data->'new_data'->>'studio_id')::uuid = SF.studio_id
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_EDIT'
JOIN user_notifications N ON SF.user_id = N.user_id AND N.type = 'FAVORITE_STUDIO_EDIT' AND N.user_id != E.user_id
WHERE E.id = $1
UNION
SELECT N.user_id, N.type, $1
FROM (
SELECT id, (jsonb_array_elements(edits.data->'new_data'->'added_performers')->>'performer_id')::uuid AS performer_id FROM edits
SELECT id, (jsonb_array_elements(edits.data->'new_data'->'added_performers')->>'performer_id')::uuid AS performer_id, user_id
FROM edits
) E JOIN performer_favorites PF ON E.performer_id = PF.performer_id
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_EDIT'
JOIN user_notifications N ON PF.user_id = N.user_id AND N.type = 'FAVORITE_PERFORMER_EDIT' AND N.user_id != E.user_id
WHERE E.id = $1
`
err := qb.dbi.RawExec(query, args)
Expand Down

0 comments on commit a9163b5

Please sign in to comment.