Skip to content

Commit

Permalink
Fix: No webhook message when post deleted with no comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwoberts committed Sep 17, 2024
1 parent acc0fc7 commit c1328f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
5 changes: 1 addition & 4 deletions app/handlers/apiv1/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ func DeletePost() web.HandlerFunc {
return c.Failure(err)
}

if action.Text != "" {
// Only send notification if user wrote a comment.
c.Enqueue(tasks.NotifyAboutDeletedPost(action.Post))
}
c.Enqueue(tasks.NotifyAboutDeletedPost(action.Post, action.Text != ""))

return c.Ok(web.Map{})
}
Expand Down
47 changes: 27 additions & 20 deletions app/tasks/delete_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,41 @@ import (
"github.com/getfider/fider/app/pkg/worker"
)

//NotifyAboutDeletedPost sends a notification (web and email) to subscribers of the post that has been deleted
func NotifyAboutDeletedPost(post *entity.Post) worker.Task {
// NotifyAboutDeletedPost sends a notification (web and email) to subscribers of the post that has been deleted
func NotifyAboutDeletedPost(post *entity.Post, deleteCommentAdded bool) worker.Task {
return describe("Notify about deleted post", func(c *worker.Context) error {

tenant := c.Tenant()
baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
author := c.User()
title := fmt.Sprintf("**%s** deleted **%s**", author.Name, post.Title)

// Webhook
webhookProps := webhook.Props{}
webhookProps.SetPost(post, "post", baseURL, true, true)
webhookProps.SetUser(author, "author")
webhookProps.SetTenant(tenant, "tenant", baseURL, logoURL)

err := bus.Dispatch(c, &cmd.TriggerWebhooks{
Type: enum.WebhookDeletePost,
Props: webhookProps,
})
if err != nil {
return c.Failure(err)
}

// If no comment was added, we can stop here
// (I'm not sure about the rational of this business rule, but this is how it currently works)
if !deleteCommentAdded {
return nil
}

// Web notification
users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventChangeStatus)
if err != nil {
return c.Failure(err)
}

author := c.User()
title := fmt.Sprintf("**%s** deleted **%s**", author.Name, post.Title)
for _, user := range users {
if user.ID != author.ID {
err = bus.Dispatch(c, &cmd.AddNewNotification{
Expand All @@ -53,9 +76,6 @@ func NotifyAboutDeletedPost(post *entity.Post) worker.Task {
}
}

tenant := c.Tenant()
baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)

props := dto.Props{
"title": post.Title,
"siteName": tenant.Name,
Expand All @@ -71,19 +91,6 @@ func NotifyAboutDeletedPost(post *entity.Post) worker.Task {
Props: props,
})

webhookProps := webhook.Props{}
webhookProps.SetPost(post, "post", baseURL, true, true)
webhookProps.SetUser(author, "author")
webhookProps.SetTenant(tenant, "tenant", baseURL, logoURL)

err = bus.Dispatch(c, &cmd.TriggerWebhooks{
Type: enum.WebhookDeletePost,
Props: webhookProps,
})
if err != nil {
return c.Failure(err)
}

return nil
})
}

0 comments on commit c1328f9

Please sign in to comment.