Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: split into multiple files

goenningb2fd819f6fa5
diff --git a/app/tasks/change_email.go b/app/tasks/change_email.go
new file mode 100644
index 000000000..d0a6789d4
--- /dev/null
+++ b/app/tasks/change_email.go
@@ -0,0 +1,39 @@
+package tasks
+
+import (
+ "github.com/getfider/fider/app/actions"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//SendChangeEmailConfirmation is used to send the change email confirmation email to requestor
+func SendChangeEmailConfirmation(action *actions.ChangeUserEmail) worker.Task {
+ return describe("Send change email confirmation", func(c *worker.Context) error {
+
+ previous := c.User().Email
+ if previous == "" {
+ previous = "(empty)"
+ }
+
+ to := dto.NewRecipient(action.Requestor.Name, action.Email, dto.Props{
+ "name": c.User().Name,
+ "oldEmail": previous,
+ "newEmail": action.Email,
+ "link": link(web.BaseURL(c), "/change-email/verify?k=%s", action.VerificationKey),
+ })
+
+ bus.Publish(c, &cmd.SendMail{
+ From: c.Tenant().Name,
+ To: []dto.Recipient{to},
+ TemplateName: "change_emailaddress_email",
+ Props: dto.Props{
+ "logo": web.LogoURL(c),
+ },
+ })
+
+ return nil
+ })
+}
diff --git a/app/tasks/change_email_test.go b/app/tasks/change_email_test.go
new file mode 100644
index 000000000..e10d3f52a
--- /dev/null
+++ b/app/tasks/change_email_test.go
@@ -0,0 +1,52 @@
+package tasks_test
+
+import (
+ "testing"
+
+ "github.com/getfider/fider/app/actions"
+
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestSendChangeEmailConfirmationTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ worker := mock.NewWorker()
+ task := tasks.SendChangeEmailConfirmation(&actions.ChangeUserEmail{
+ Email: "[email protected]",
+ VerificationKey: "13579",
+ Requestor: mock.JonSnow,
+ })
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_emailaddress_email")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals(mock.DemoTenant.Name)
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Jon Snow",
+ Address: "[email protected]",
+ Props: dto.Props{
+ "name": "Jon Snow",
+ "oldEmail": "[email protected]",
+ "newEmail": "[email protected]",
+ "link": "<a href='http://domain.com/change-email/verify?k=13579'>http://domain.com/change-email/verify?k=13579</a>",
+ },
+ })
+}
diff --git a/app/tasks/delete_post.go b/app/tasks/delete_post.go
new file mode 100644
index 000000000..4b0c6fc40
--- /dev/null
+++ b/app/tasks/delete_post.go
@@ -0,0 +1,89 @@
+package tasks
+
+import (
+ "fmt"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/i18n"
+ "github.com/getfider/fider/app/pkg/markdown"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/webhook"
+ "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 {
+ return describe("Notify about deleted post", func(c *worker.Context) error {
+
+ // 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{
+ User: user,
+ Title: title,
+ PostID: post.ID,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+ }
+ }
+
+ // Email notification
+ users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventChangeStatus)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ to := make([]dto.Recipient, 0)
+ for _, user := range users {
+ if user.ID != author.ID {
+ to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
+ }
+ }
+
+ tenant := c.Tenant()
+ baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
+
+ props := dto.Props{
+ "title": post.Title,
+ "siteName": tenant.Name,
+ "content": markdown.Full(post.Response.Text),
+ "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
+ "logo": logoURL,
+ }
+
+ bus.Publish(c, &cmd.SendMail{
+ From: c.User().Name,
+ To: to,
+ TemplateName: "delete_post",
+ 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
+ })
+}
diff --git a/app/tasks/delete_post_test.go b/app/tasks/delete_post_test.go
new file mode 100644
index 000000000..13e3bdef0
--- /dev/null
+++ b/app/tasks/delete_post_test.go
@@ -0,0 +1,127 @@
+package tasks_test
+
+import (
+ "context"
+ "html/template"
+ "testing"
+ "time"
+
+ "github.com/getfider/fider/app/pkg/webhook"
+
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestNotifyAboutDeletePostTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.AryaStark,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Description: "TypeScript is great, please add support for it",
+ User: mock.AryaStark,
+ Status: enum.PostDeleted,
+ Response: &entity.PostResponse{
+ RespondedAt: time.Now(),
+ Text: "Invalid post!",
+ User: mock.JonSnow,
+ },
+ }
+
+ task := tasks.NotifyAboutDeletedPost(post)
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("delete_post")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "title": "Add support for TypeScript",
+ "siteName": "Demonstration",
+ "content": template.HTML("<p>Invalid post!</p>"),
+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Arya Stark",
+ Address: "[email protected]",
+ Props: dto.Props{},
+ })
+
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("")
+ Expect(addNewNotification.Title).Equals("**Jon Snow** deleted **Add support for TypeScript**")
+ Expect(addNewNotification.User).Equals(mock.AryaStark)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookDeletePost)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "post_id": post.ID,
+ "post_number": post.Number,
+ "post_title": post.Title,
+ "post_slug": post.Slug,
+ "post_description": post.Description,
+ "post_status": post.Status.Name(),
+ "post_url": "http://domain.com/posts/1/add-support-for-typescript",
+ "post_author_id": mock.AryaStark.ID,
+ "post_author_name": mock.AryaStark.Name,
+ "post_author_email": mock.AryaStark.Email,
+ "post_author_role": mock.AryaStark.Role.String(),
+ "post_response": true,
+ "post_response_text": post.Response.Text,
+ "post_response_responded_at": post.Response.RespondedAt,
+ "post_response_author_id": mock.JonSnow.ID,
+ "post_response_author_name": mock.JonSnow.Name,
+ "post_response_author_email": mock.JonSnow.Email,
+ "post_response_author_role": mock.JonSnow.Role.String(),
+ "author_id": mock.JonSnow.ID,
+ "author_name": mock.JonSnow.Name,
+ "author_email": mock.JonSnow.Email,
+ "author_role": mock.JonSnow.Role.String(),
+ "tenant_id": mock.DemoTenant.ID,
+ "tenant_name": mock.DemoTenant.Name,
+ "tenant_subdomain": mock.DemoTenant.Subdomain,
+ "tenant_status": mock.DemoTenant.Status.String(),
+ "tenant_url": "http://domain.com",
+ })
+}
diff --git a/app/tasks/invites.go b/app/tasks/invites.go
new file mode 100644
index 000000000..c95b06ae2
--- /dev/null
+++ b/app/tasks/invites.go
@@ -0,0 +1,51 @@
+package tasks
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ "github.com/getfider/fider/app"
+ "github.com/getfider/fider/app/actions"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/markdown"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//SendInvites sends one email to each invited recipient
+func SendInvites(subject, message string, invitations []*actions.UserInvitation) worker.Task {
+ return describe("Send invites", func(c *worker.Context) error {
+ to := make([]dto.Recipient, len(invitations))
+ for i, invite := range invitations {
+ err := bus.Dispatch(c, &cmd.SaveVerificationKey{
+ Key: invite.VerificationKey,
+ Duration: 15 * 24 * time.Hour,
+ Request: invite,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ url := fmt.Sprintf("%s/invite/verify?k=%s", web.BaseURL(c), invite.VerificationKey)
+ toMessage := strings.Replace(message, app.InvitePlaceholder, url, -1)
+ to[i] = dto.NewRecipient("", invite.Email, dto.Props{
+ "message": markdown.Full(toMessage),
+ })
+ }
+
+ bus.Publish(c, &cmd.SendMail{
+ From: c.User().Name,
+ To: to,
+ TemplateName: "invite_email",
+ Props: dto.Props{
+ "subject": subject,
+ "logo": web.LogoURL(c),
+ },
+ })
+
+ return nil
+ })
+}
diff --git a/app/tasks/invites_test.go b/app/tasks/invites_test.go
new file mode 100644
index 000000000..cf2e83374
--- /dev/null
+++ b/app/tasks/invites_test.go
@@ -0,0 +1,69 @@
+package tasks_test
+
+import (
+ "context"
+ "html/template"
+ "testing"
+
+ "github.com/getfider/fider/app/actions"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestSendInvites(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ savedKeys := make([]*cmd.SaveVerificationKey, 0)
+ bus.AddHandler(func(ctx context.Context, c *cmd.SaveVerificationKey) error {
+ savedKeys = append(savedKeys, c)
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ task := tasks.SendInvites("My Subject", "Click here: %invite%", []*actions.UserInvitation{
+ {Email: "[email protected]", VerificationKey: "1234"},
+ {Email: "[email protected]", VerificationKey: "5678"},
+ })
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("invite_email")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "subject": "My Subject",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
+ Expect(emailmock.MessageHistory[0].To).HasLen(2)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Address: "[email protected]",
+ Props: dto.Props{
+ "message": template.HTML(`<p>Click here: <a href="http://domain.com/invite/verify?k=1234">http://domain.com/invite/verify?k=1234</a></p>`),
+ },
+ })
+ Expect(emailmock.MessageHistory[0].To[1]).Equals(dto.Recipient{
+ Address: "[email protected]",
+ Props: dto.Props{
+ "message": template.HTML(`<p>Click here: <a href="http://domain.com/invite/verify?k=5678">http://domain.com/invite/verify?k=5678</a></p>`),
+ },
+ })
+
+ Expect(savedKeys).HasLen(2)
+ Expect(savedKeys[0].Key).Equals("1234")
+ Expect(savedKeys[0].Request.GetEmail()).Equals("[email protected]")
+ Expect(savedKeys[1].Key).Equals("5678")
+ Expect(savedKeys[1].Request.GetEmail()).Equals("[email protected]")
+}
diff --git a/app/tasks/new_comment.go b/app/tasks/new_comment.go
new file mode 100644
index 000000000..76ec31507
--- /dev/null
+++ b/app/tasks/new_comment.go
@@ -0,0 +1,94 @@
+package tasks
+
+import (
+ "fmt"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/i18n"
+ "github.com/getfider/fider/app/pkg/markdown"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/webhook"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//NotifyAboutNewComment sends a notification (web and email) to subscribers
+func NotifyAboutNewComment(post *entity.Post, comment string) worker.Task {
+ return describe("Notify about new comment", func(c *worker.Context) error {
+ // Web notification
+ users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventNewComment)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ author := c.User()
+ title := fmt.Sprintf("**%s** left a comment on **%s**", author.Name, post.Title)
+ link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
+ for _, user := range users {
+ if user.ID != author.ID {
+ err = bus.Dispatch(c, &cmd.AddNewNotification{
+ User: user,
+ Title: title,
+ Link: link,
+ PostID: post.ID,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+ }
+ }
+
+ // Email notification
+ users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventNewComment)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ to := make([]dto.Recipient, 0)
+ for _, user := range users {
+ if user.ID != author.ID {
+ to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
+ }
+ }
+
+ tenant := c.Tenant()
+ baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
+
+ mailProps := dto.Props{
+ "title": post.Title,
+ "siteName": tenant.Name,
+ "userName": author.Name,
+ "content": markdown.Full(comment),
+ "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "unsubscribe": linkWithText(i18n.T(c, "email.subscription.unsubscribe"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
+ "logo": logoURL,
+ }
+
+ bus.Publish(c, &cmd.SendMail{
+ From: author.Name,
+ To: to,
+ TemplateName: "new_comment",
+ Props: mailProps,
+ })
+
+ webhookProps := webhook.Props{"comment": comment}
+ 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.WebhookNewComment,
+ Props: webhookProps,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ return nil
+ })
+}
diff --git a/app/tasks/new_comment_test.go b/app/tasks/new_comment_test.go
new file mode 100644
index 000000000..a61fb0b5b
--- /dev/null
+++ b/app/tasks/new_comment_test.go
@@ -0,0 +1,116 @@
+package tasks_test
+
+import (
+ "context"
+ "html/template"
+ "testing"
+
+ "github.com/getfider/fider/app/pkg/webhook"
+
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestNotifyAboutNewCommentTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.JonSnow,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Description: "TypeScript is great, please add support for it",
+ User: mock.JonSnow,
+ }
+ task := tasks.NotifyAboutNewComment(post, "I agree")
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.AryaStark).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("new_comment")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "title": "Add support for TypeScript",
+ "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
+ "siteName": "Demonstration",
+ "userName": "Arya Stark",
+ "content": template.HTML("<p>I agree</p>"),
+ "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
+ "unsubscribe": "<a href='http://domain.com/posts/1/add-support-for-typescript'>unsubscribe from it</a>",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Arya Stark")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Jon Snow",
+ Address: "[email protected]",
+ Props: dto.Props{},
+ })
+
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
+ Expect(addNewNotification.Title).Equals("**Arya Stark** left a comment on **Add support for TypeScript**")
+ Expect(addNewNotification.User).Equals(mock.JonSnow)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookNewComment)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "comment": "I agree",
+ "post_id": post.ID,
+ "post_number": post.Number,
+ "post_title": post.Title,
+ "post_slug": post.Slug,
+ "post_description": post.Description,
+ "post_url": "http://domain.com/posts/1/add-support-for-typescript",
+ "post_author_id": mock.JonSnow.ID,
+ "post_author_name": mock.JonSnow.Name,
+ "post_author_email": mock.JonSnow.Email,
+ "post_author_role": mock.JonSnow.Role.String(),
+ "author_id": mock.AryaStark.ID,
+ "author_name": mock.AryaStark.Name,
+ "author_email": mock.AryaStark.Email,
+ "author_role": mock.AryaStark.Role.String(),
+ "tenant_id": mock.DemoTenant.ID,
+ "tenant_name": mock.DemoTenant.Name,
+ "tenant_subdomain": mock.DemoTenant.Subdomain,
+ "tenant_status": mock.DemoTenant.Status.String(),
+ "tenant_url": "http://domain.com",
+ })
+}
diff --git a/app/tasks/new_post.go b/app/tasks/new_post.go
new file mode 100644
index 000000000..ba87d4a7d
--- /dev/null
+++ b/app/tasks/new_post.go
@@ -0,0 +1,93 @@
+package tasks
+
+import (
+ "fmt"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/i18n"
+ "github.com/getfider/fider/app/pkg/markdown"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/webhook"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//NotifyAboutNewPost sends a notification (web and email) to subscribers
+func NotifyAboutNewPost(post *entity.Post) worker.Task {
+ return describe("Notify about new post", func(c *worker.Context) error {
+ // Web notification
+ users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventNewPost)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ author := c.User()
+ title := fmt.Sprintf("New post: **%s**", post.Title)
+ link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
+ for _, user := range users {
+ if user.ID != author.ID {
+ err = bus.Dispatch(c, &cmd.AddNewNotification{
+ User: user,
+ Title: title,
+ Link: link,
+ PostID: post.ID,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+ }
+ }
+
+ // Email notification
+ users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventNewPost)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ to := make([]dto.Recipient, 0)
+ for _, user := range users {
+ if user.ID != author.ID {
+ to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
+ }
+ }
+
+ tenant := c.Tenant()
+ baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
+
+ mailProps := dto.Props{
+ "title": post.Title,
+ "siteName": tenant.Name,
+ "userName": author.Name,
+ "content": markdown.Full(post.Description),
+ "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
+ "logo": logoURL,
+ }
+
+ bus.Publish(c, &cmd.SendMail{
+ From: author.Name,
+ To: to,
+ TemplateName: "new_post",
+ Props: mailProps,
+ })
+
+ webhookProps := webhook.Props{}
+ webhookProps.SetPost(post, "post", baseURL, false, false)
+ webhookProps.SetUser(author, "author")
+ webhookProps.SetTenant(tenant, "tenant", baseURL, logoURL)
+
+ err = bus.Dispatch(c, &cmd.TriggerWebhooks{
+ Type: enum.WebhookNewPost,
+ Props: webhookProps,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ return nil
+ })
+}
diff --git a/app/tasks/new_post_test.go b/app/tasks/new_post_test.go
new file mode 100644
index 000000000..31df7948c
--- /dev/null
+++ b/app/tasks/new_post_test.go
@@ -0,0 +1,109 @@
+package tasks_test
+
+import (
+ "context"
+ "html/template"
+ "testing"
+
+ "github.com/getfider/fider/app/pkg/webhook"
+
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestNotifyAboutNewPostTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.AryaStark,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Description: "TypeScript is great, please add support for it",
+ }
+ task := tasks.NotifyAboutNewPost(post)
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("new_post")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "title": "Add support for TypeScript",
+ "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
+ "siteName": "Demonstration",
+ "userName": "Jon Snow",
+ "content": template.HTML("<p>TypeScript is great, please add support for it</p>"),
+ "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Arya Stark",
+ Address: "[email protected]",
+ Props: dto.Props{},
+ })
+
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
+ Expect(addNewNotification.Title).Equals("New post: **Add support for TypeScript**")
+ Expect(addNewNotification.User).Equals(mock.AryaStark)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookNewPost)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "post_id": post.ID,
+ "post_number": post.Number,
+ "post_title": post.Title,
+ "post_slug": post.Slug,
+ "post_description": post.Description,
+ "post_url": "http://domain.com/posts/1/add-support-for-typescript",
+ "author_id": mock.JonSnow.ID,
+ "author_name": mock.JonSnow.Name,
+ "author_email": mock.JonSnow.Email,
+ "author_role": mock.JonSnow.Role.String(),
+ "tenant_id": mock.DemoTenant.ID,
+ "tenant_name": mock.DemoTenant.Name,
+ "tenant_subdomain": mock.DemoTenant.Subdomain,
+ "tenant_status": mock.DemoTenant.Status.String(),
+ "tenant_url": "http://domain.com",
+ })
+}
diff --git a/app/tasks/signin.go b/app/tasks/signin.go
new file mode 100644
index 000000000..7b36c31dc
--- /dev/null
+++ b/app/tasks/signin.go
@@ -0,0 +1,30 @@
+package tasks
+
+import (
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//SendSignInEmail is used to send the sign in email to requestor
+func SendSignInEmail(email, verificationKey string) worker.Task {
+ return describe("Send sign in email", func(c *worker.Context) error {
+ to := dto.NewRecipient("", email, dto.Props{
+ "siteName": c.Tenant().Name,
+ "link": link(web.BaseURL(c), "/signin/verify?k=%s", verificationKey),
+ })
+
+ bus.Publish(c, &cmd.SendMail{
+ From: c.Tenant().Name,
+ To: []dto.Recipient{to},
+ TemplateName: "signin_email",
+ Props: dto.Props{
+ "logo": web.LogoURL(c),
+ },
+ })
+
+ return nil
+ })
+}
diff --git a/app/tasks/signin_test.go b/app/tasks/signin_test.go
new file mode 100644
index 000000000..f1c87520a
--- /dev/null
+++ b/app/tasks/signin_test.go
@@ -0,0 +1,43 @@
+package tasks_test
+
+import (
+ "testing"
+
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestSendSignInEmailTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ worker := mock.NewWorker()
+ task := tasks.SendSignInEmail("[email protected]", "9876")
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("signin_email")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals(mock.DemoTenant.Name)
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Address: "[email protected]",
+ Props: dto.Props{
+ "siteName": mock.DemoTenant.Name,
+ "link": "<a href='http://domain.com/signin/verify?k=9876'>http://domain.com/signin/verify?k=9876</a>",
+ },
+ })
+}
diff --git a/app/tasks/signup.go b/app/tasks/signup.go
new file mode 100644
index 000000000..3340abf38
--- /dev/null
+++ b/app/tasks/signup.go
@@ -0,0 +1,30 @@
+package tasks
+
+import (
+ "github.com/getfider/fider/app/actions"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//SendSignUpEmail is used to send the sign up email to requestor
+func SendSignUpEmail(action *actions.CreateTenant, baseURL string) worker.Task {
+ return describe("Send sign up email", func(c *worker.Context) error {
+ to := dto.NewRecipient(action.Name, action.Email, dto.Props{
+ "link": link(baseURL, "/signup/verify?k=%s", action.VerificationKey),
+ })
+
+ bus.Publish(c, &cmd.SendMail{
+ From: "Fider",
+ To: []dto.Recipient{to},
+ TemplateName: "signup_email",
+ Props: dto.Props{
+ "logo": web.LogoURL(c),
+ },
+ })
+
+ return nil
+ })
+}
diff --git a/app/tasks/signup_test.go b/app/tasks/signup_test.go
new file mode 100644
index 000000000..929dd0ba6
--- /dev/null
+++ b/app/tasks/signup_test.go
@@ -0,0 +1,42 @@
+package tasks_test
+
+import (
+ "testing"
+
+ "github.com/getfider/fider/app/actions"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestSendSignUpEmailTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ worker := mock.NewWorker()
+ task := tasks.SendSignUpEmail(&actions.CreateTenant{
+ VerificationKey: "1234",
+ }, "http://domain.com")
+
+ err := worker.
+ AsUser(mock.JonSnow).
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("signup_email")
+ Expect(emailmock.MessageHistory[0].Tenant).IsNil()
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Fider")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Props: dto.Props{
+ "link": "<a href='http://domain.com/signup/verify?k=1234'>http://domain.com/signup/verify?k=1234</a>",
+ },
+ })
+}
diff --git a/app/tasks/status_change.go b/app/tasks/status_change.go
new file mode 100644
index 000000000..b0e300c08
--- /dev/null
+++ b/app/tasks/status_change.go
@@ -0,0 +1,106 @@
+package tasks
+
+import (
+ "fmt"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/i18n"
+ "github.com/getfider/fider/app/pkg/markdown"
+ "github.com/getfider/fider/app/pkg/web"
+ "github.com/getfider/fider/app/pkg/webhook"
+ "github.com/getfider/fider/app/pkg/worker"
+)
+
+//NotifyAboutStatusChange sends a notification (web and email) to subscribers
+func NotifyAboutStatusChange(post *entity.Post, prevStatus enum.PostStatus) worker.Task {
+ return describe("Notify about post status change", func(c *worker.Context) error {
+ //Don't notify if previous status is the same
+ if prevStatus == post.Status {
+ 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** changed status of **%s** to **%s**", author.Name, post.Title, post.Status.Name())
+ link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
+ for _, user := range users {
+ if user.ID != author.ID {
+ err = bus.Dispatch(c, &cmd.AddNewNotification{
+ User: user,
+ Title: title,
+ Link: link,
+ PostID: post.ID,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+ }
+ }
+
+ // Email notification
+ users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventChangeStatus)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ baseURL := web.BaseURL(c)
+ var duplicate string
+ if post.Status == enum.PostDuplicate {
+ duplicate = linkWithText(post.Response.Original.Title, baseURL, "/posts/%d/%s", post.Response.Original.Number, post.Response.Original.Slug)
+ }
+
+ to := make([]dto.Recipient, 0)
+ for _, user := range users {
+ if user.ID != author.ID {
+ to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
+ }
+ }
+
+ tenant := c.Tenant()
+ logoURL := web.LogoURL(c)
+
+ props := dto.Props{
+ "title": post.Title,
+ "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "siteName": tenant.Name,
+ "content": markdown.Full(post.Response.Text),
+ "status": i18n.T(c, fmt.Sprintf("enum.poststatus.%s", post.Status.Name())),
+ "duplicate": duplicate,
+ "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "unsubscribe": linkWithText(i18n.T(c, "email.subscription.unsubscribe"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
+ "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
+ "logo": logoURL,
+ }
+
+ bus.Publish(c, &cmd.SendMail{
+ From: author.Name,
+ To: to,
+ TemplateName: "change_status",
+ Props: props,
+ })
+
+ webhookProps := webhook.Props{"post_old_status": prevStatus.Name()}
+ 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.WebhookChangeStatus,
+ Props: webhookProps,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ return nil
+ })
+}
diff --git a/app/tasks/status_change_test.go b/app/tasks/status_change_test.go
new file mode 100644
index 000000000..4a6194af3
--- /dev/null
+++ b/app/tasks/status_change_test.go
@@ -0,0 +1,253 @@
+package tasks_test
+
+import (
+ "context"
+ "html/template"
+ "testing"
+ "time"
+
+ "github.com/getfider/fider/app/pkg/webhook"
+
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/services/email/emailmock"
+ "github.com/getfider/fider/app/tasks"
+)
+
+func TestNotifyAboutStatusChangeTask(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.AryaStark,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Description: "TypeScript is great, please add support for it",
+ User: mock.AryaStark,
+ Status: enum.PostPlanned,
+ Response: &entity.PostResponse{
+ RespondedAt: time.Now(),
+ Text: "Planned for next release.",
+ User: mock.JonSnow,
+ },
+ }
+
+ task := tasks.NotifyAboutStatusChange(post, enum.PostOpen)
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_status")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "title": "Add support for TypeScript",
+ "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
+ "siteName": "Demonstration",
+ "content": template.HTML("<p>Planned for next release.</p>"),
+ "duplicate": "",
+ "status": "Planned",
+ "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
+ "unsubscribe": "<a href='http://domain.com/posts/1/add-support-for-typescript'>unsubscribe from it</a>",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Arya Stark",
+ Address: "[email protected]",
+ Props: dto.Props{},
+ })
+
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
+ Expect(addNewNotification.Title).Equals("**Jon Snow** changed status of **Add support for TypeScript** to **planned**")
+ Expect(addNewNotification.User).Equals(mock.AryaStark)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookChangeStatus)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "post_old_status": enum.PostOpen.Name(),
+ "post_id": post.ID,
+ "post_number": post.Number,
+ "post_title": post.Title,
+ "post_slug": post.Slug,
+ "post_description": post.Description,
+ "post_status": post.Status.Name(),
+ "post_url": "http://domain.com/posts/1/add-support-for-typescript",
+ "post_author_id": mock.AryaStark.ID,
+ "post_author_name": mock.AryaStark.Name,
+ "post_author_email": mock.AryaStark.Email,
+ "post_author_role": mock.AryaStark.Role.String(),
+ "post_response": true,
+ "post_response_text": post.Response.Text,
+ "post_response_responded_at": post.Response.RespondedAt,
+ "post_response_author_id": mock.JonSnow.ID,
+ "post_response_author_name": mock.JonSnow.Name,
+ "post_response_author_email": mock.JonSnow.Email,
+ "post_response_author_role": mock.JonSnow.Role.String(),
+ "author_id": mock.JonSnow.ID,
+ "author_name": mock.JonSnow.Name,
+ "author_email": mock.JonSnow.Email,
+ "author_role": mock.JonSnow.Role.String(),
+ "tenant_id": mock.DemoTenant.ID,
+ "tenant_name": mock.DemoTenant.Name,
+ "tenant_subdomain": mock.DemoTenant.Subdomain,
+ "tenant_status": mock.DemoTenant.Status.String(),
+ "tenant_url": "http://domain.com",
+ })
+}
+
+func TestNotifyAboutStatusChangeTask_Duplicate(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.AryaStark,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 2,
+ Number: 2,
+ Title: "I need TypeScript",
+ Slug: "i-need-typescript",
+ User: mock.AryaStark,
+ Status: enum.PostDuplicate,
+ Response: &entity.PostResponse{
+ RespondedAt: time.Now(),
+ User: mock.JonSnow,
+ Original: &entity.OriginalPost{
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Status: enum.PostOpen,
+ },
+ },
+ }
+
+ task := tasks.NotifyAboutStatusChange(post, enum.PostOpen)
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(emailmock.MessageHistory).HasLen(1)
+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_status")
+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
+ "title": "I need TypeScript",
+ "postLink": "<a href='http://domain.com/posts/2/i-need-typescript'>#2</a>",
+ "siteName": "Demonstration",
+ "content": template.HTML(""),
+ "duplicate": "<a href='http://domain.com/posts/1/add-support-for-typescript'>Add support for TypeScript</a>",
+ "status": "Duplicate",
+ "view": "<a href='http://domain.com/posts/2/i-need-typescript'>view it on your browser</a>",
+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
+ "unsubscribe": "<a href='http://domain.com/posts/2/i-need-typescript'>unsubscribe from it</a>",
+ "logo": "https://getfider.com/images/logo-100x100.png",
+ })
+ Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
+ Expect(emailmock.MessageHistory[0].To).HasLen(1)
+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
+ Name: "Arya Stark",
+ Address: "[email protected]",
+ Props: dto.Props{},
+ })
+
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("/posts/2/i-need-typescript")
+ Expect(addNewNotification.Title).Equals("**Jon Snow** changed status of **I need TypeScript** to **duplicate**")
+ Expect(addNewNotification.User).Equals(mock.AryaStark)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookChangeStatus)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "post_old_status": enum.PostOpen.Name(),
+ "post_id": post.ID,
+ "post_number": post.Number,
+ "post_title": post.Title,
+ "post_slug": post.Slug,
+ "post_description": post.Description,
+ "post_status": post.Status.Name(),
+ "post_url": "http://domain.com/posts/2/i-need-typescript",
+ "post_author_id": mock.AryaStark.ID,
+ "post_author_name": mock.AryaStark.Name,
+ "post_author_email": mock.AryaStark.Email,
+ "post_author_role": mock.AryaStark.Role.String(),
+ "post_response": true,
+ "post_response_text": post.Response.Text,
+ "post_response_responded_at": post.Response.RespondedAt,
+ "post_response_author_id": mock.JonSnow.ID,
+ "post_response_author_name": mock.JonSnow.Name,
+ "post_response_author_email": mock.JonSnow.Email,
+ "post_response_author_role": mock.JonSnow.Role.String(),
+ "post_response_original_number": post.Response.Original.Number,
+ "post_response_original_title": post.Response.Original.Title,
+ "post_response_original_slug": post.Response.Original.Slug,
+ "post_response_original_status": post.Response.Original.Status.Name(),
+ "post_response_original_url": "http://domain.com/posts/1/add-support-for-typescript",
+ "author_id": mock.JonSnow.ID,
+ "author_name": mock.JonSnow.Name,
+ "author_email": mock.JonSnow.Email,
+ "author_role": mock.JonSnow.Role.String(),
+ "tenant_id": mock.DemoTenant.ID,
+ "tenant_name": mock.DemoTenant.Name,
+ "tenant_subdomain": mock.DemoTenant.Subdomain,
+ "tenant_status": mock.DemoTenant.Status.String(),
+ "tenant_url": "http://domain.com",
+ })
+}
diff --git a/app/tasks/tasks.go b/app/tasks/tasks.go
index 0d28e0f31..4b67873f1 100644
--- a/app/tasks/tasks.go
+++ b/app/tasks/tasks.go
@@ -3,21 +3,11 @@ package tasks
import (
"context"
"fmt"
- "github.com/getfider/fider/app/pkg/webhook"
- "strings"
- "time"
- "github.com/getfider/fider/app"
- "github.com/getfider/fider/app/actions"
- "github.com/getfider/fider/app/models/cmd"
- "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
- "github.com/getfider/fider/app/pkg/i18n"
- "github.com/getfider/fider/app/pkg/markdown"
- "github.com/getfider/fider/app/pkg/web"
"github.com/getfider/fider/app/pkg/worker"
)
@@ -33,429 +23,6 @@ func linkWithText(text, baseURL, path string, args ...interface{}) string {
return fmt.Sprintf("<a href='%s%s'>%s</a>", baseURL, fmt.Sprintf(path, args...), text)
}
-//SendSignUpEmail is used to send the sign up email to requestor
-func SendSignUpEmail(action *actions.CreateTenant, baseURL string) worker.Task {
- return describe("Send sign up email", func(c *worker.Context) error {
- to := dto.NewRecipient(action.Name, action.Email, dto.Props{
- "link": link(baseURL, "/signup/verify?k=%s", action.VerificationKey),
- })
-
- bus.Publish(c, &cmd.SendMail{
- From: "Fider",
- To: []dto.Recipient{to},
- TemplateName: "signup_email",
- Props: dto.Props{
- "logo": web.LogoURL(c),
- },
- })
-
- return nil
- })
-}
-
-//SendSignInEmail is used to send the sign in email to requestor
-func SendSignInEmail(email, verificationKey string) worker.Task {
- return describe("Send sign in email", func(c *worker.Context) error {
- to := dto.NewRecipient("", email, dto.Props{
- "siteName": c.Tenant().Name,
- "link": link(web.BaseURL(c), "/signin/verify?k=%s", verificationKey),
- })
-
- bus.Publish(c, &cmd.SendMail{
- From: c.Tenant().Name,
- To: []dto.Recipient{to},
- TemplateName: "signin_email",
- Props: dto.Props{
- "logo": web.LogoURL(c),
- },
- })
-
- return nil
- })
-}
-
-//SendChangeEmailConfirmation is used to send the change email confirmation email to requestor
-func SendChangeEmailConfirmation(action *actions.ChangeUserEmail) worker.Task {
- return describe("Send change email confirmation", func(c *worker.Context) error {
-
- previous := c.User().Email
- if previous == "" {
- previous = "(empty)"
- }
-
- to := dto.NewRecipient(action.Requestor.Name, action.Email, dto.Props{
- "name": c.User().Name,
- "oldEmail": previous,
- "newEmail": action.Email,
- "link": link(web.BaseURL(c), "/change-email/verify?k=%s", action.VerificationKey),
- })
-
- bus.Publish(c, &cmd.SendMail{
- From: c.Tenant().Name,
- To: []dto.Recipient{to},
- TemplateName: "change_emailaddress_email",
- Props: dto.Props{
- "logo": web.LogoURL(c),
- },
- })
-
- return nil
- })
-}
-
-//NotifyAboutNewPost sends a notification (web and email) to subscribers
-func NotifyAboutNewPost(post *entity.Post) worker.Task {
- return describe("Notify about new post", func(c *worker.Context) error {
- // Web notification
- users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventNewPost)
- if err != nil {
- return c.Failure(err)
- }
-
- author := c.User()
- title := fmt.Sprintf("New post: **%s**", post.Title)
- link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
- for _, user := range users {
- if user.ID != author.ID {
- err = bus.Dispatch(c, &cmd.AddNewNotification{
- User: user,
- Title: title,
- Link: link,
- PostID: post.ID,
- })
- if err != nil {
- return c.Failure(err)
- }
- }
- }
-
- // Email notification
- users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventNewPost)
- if err != nil {
- return c.Failure(err)
- }
-
- to := make([]dto.Recipient, 0)
- for _, user := range users {
- if user.ID != author.ID {
- to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
- }
- }
-
- tenant := c.Tenant()
- baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
-
- mailProps := dto.Props{
- "title": post.Title,
- "siteName": tenant.Name,
- "userName": author.Name,
- "content": markdown.Full(post.Description),
- "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
- "logo": logoURL,
- }
-
- bus.Publish(c, &cmd.SendMail{
- From: author.Name,
- To: to,
- TemplateName: "new_post",
- Props: mailProps,
- })
-
- webhookProps := webhook.Props{}
- webhookProps.SetPost(post, "post", baseURL, false, false)
- webhookProps.SetUser(author, "author")
- webhookProps.SetTenant(tenant, "tenant", baseURL, logoURL)
-
- err = bus.Dispatch(c, &cmd.TriggerWebhooks{
- Type: enum.WebhookNewPost,
- Props: webhookProps,
- })
- if err != nil {
- return c.Failure(err)
- }
-
- return nil
- })
-}
-
-//NotifyAboutNewComment sends a notification (web and email) to subscribers
-func NotifyAboutNewComment(post *entity.Post, comment string) worker.Task {
- return describe("Notify about new comment", func(c *worker.Context) error {
- // Web notification
- users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventNewComment)
- if err != nil {
- return c.Failure(err)
- }
-
- author := c.User()
- title := fmt.Sprintf("**%s** left a comment on **%s**", author.Name, post.Title)
- link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
- for _, user := range users {
- if user.ID != author.ID {
- err = bus.Dispatch(c, &cmd.AddNewNotification{
- User: user,
- Title: title,
- Link: link,
- PostID: post.ID,
- })
- if err != nil {
- return c.Failure(err)
- }
- }
- }
-
- // Email notification
- users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventNewComment)
- if err != nil {
- return c.Failure(err)
- }
-
- to := make([]dto.Recipient, 0)
- for _, user := range users {
- if user.ID != author.ID {
- to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
- }
- }
-
- tenant := c.Tenant()
- baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
-
- mailProps := dto.Props{
- "title": post.Title,
- "siteName": tenant.Name,
- "userName": author.Name,
- "content": markdown.Full(comment),
- "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "unsubscribe": linkWithText(i18n.T(c, "email.subscription.unsubscribe"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
- "logo": logoURL,
- }
-
- bus.Publish(c, &cmd.SendMail{
- From: author.Name,
- To: to,
- TemplateName: "new_comment",
- Props: mailProps,
- })
-
- webhookProps := webhook.Props{"comment": comment}
- 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.WebhookNewComment,
- Props: webhookProps,
- })
- if err != nil {
- return c.Failure(err)
- }
-
- return nil
- })
-}
-
-//NotifyAboutStatusChange sends a notification (web and email) to subscribers
-func NotifyAboutStatusChange(post *entity.Post, prevStatus enum.PostStatus) worker.Task {
- return describe("Notify about post status change", func(c *worker.Context) error {
- //Don't notify if previous status is the same
- if prevStatus == post.Status {
- 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** changed status of **%s** to **%s**", author.Name, post.Title, post.Status.Name())
- link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
- for _, user := range users {
- if user.ID != author.ID {
- err = bus.Dispatch(c, &cmd.AddNewNotification{
- User: user,
- Title: title,
- Link: link,
- PostID: post.ID,
- })
- if err != nil {
- return c.Failure(err)
- }
- }
- }
-
- // Email notification
- users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventChangeStatus)
- if err != nil {
- return c.Failure(err)
- }
-
- baseURL := web.BaseURL(c)
- var duplicate string
- if post.Status == enum.PostDuplicate {
- duplicate = linkWithText(post.Response.Original.Title, baseURL, "/posts/%d/%s", post.Response.Original.Number, post.Response.Original.Slug)
- }
-
- to := make([]dto.Recipient, 0)
- for _, user := range users {
- if user.ID != author.ID {
- to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
- }
- }
-
- tenant := c.Tenant()
- logoURL := web.LogoURL(c)
-
- props := dto.Props{
- "title": post.Title,
- "postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "siteName": tenant.Name,
- "content": markdown.Full(post.Response.Text),
- "status": i18n.T(c, fmt.Sprintf("enum.poststatus.%s", post.Status.Name())),
- "duplicate": duplicate,
- "view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "unsubscribe": linkWithText(i18n.T(c, "email.subscription.unsubscribe"), baseURL, "/posts/%d/%s", post.Number, post.Slug),
- "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
- "logo": logoURL,
- }
-
- bus.Publish(c, &cmd.SendMail{
- From: author.Name,
- To: to,
- TemplateName: "change_status",
- Props: props,
- })
-
- webhookProps := webhook.Props{"post_old_status": prevStatus.Name()}
- 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.WebhookChangeStatus,
- Props: webhookProps,
- })
- if err != nil {
- return c.Failure(err)
- }
-
- return nil
- })
-}
-
-//NotifyAboutDeletedPost sends a notification (web and email) to subscribers of the post that has been deleted
-func NotifyAboutDeletedPost(post *entity.Post) worker.Task {
- return describe("Notify about deleted post", func(c *worker.Context) error {
-
- // 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{
- User: user,
- Title: title,
- PostID: post.ID,
- })
- if err != nil {
- return c.Failure(err)
- }
- }
- }
-
- // Email notification
- users, err = getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventChangeStatus)
- if err != nil {
- return c.Failure(err)
- }
-
- to := make([]dto.Recipient, 0)
- for _, user := range users {
- if user.ID != author.ID {
- to = append(to, dto.NewRecipient(user.Name, user.Email, dto.Props{}))
- }
- }
-
- tenant := c.Tenant()
- baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)
-
- props := dto.Props{
- "title": post.Title,
- "siteName": tenant.Name,
- "content": markdown.Full(post.Response.Text),
- "change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),
- "logo": logoURL,
- }
-
- bus.Publish(c, &cmd.SendMail{
- From: c.User().Name,
- To: to,
- TemplateName: "delete_post",
- 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
- })
-}
-
-//SendInvites sends one email to each invited recipient
-func SendInvites(subject, message string, invitations []*actions.UserInvitation) worker.Task {
- return describe("Send invites", func(c *worker.Context) error {
- to := make([]dto.Recipient, len(invitations))
- for i, invite := range invitations {
- err := bus.Dispatch(c, &cmd.SaveVerificationKey{
- Key: invite.VerificationKey,
- Duration: 15 * 24 * time.Hour,
- Request: invite,
- })
- if err != nil {
- return c.Failure(err)
- }
-
- url := fmt.Sprintf("%s/invite/verify?k=%s", web.BaseURL(c), invite.VerificationKey)
- toMessage := strings.Replace(message, app.InvitePlaceholder, url, -1)
- to[i] = dto.NewRecipient("", invite.Email, dto.Props{
- "message": markdown.Full(toMessage),
- })
- }
-
- bus.Publish(c, &cmd.SendMail{
- From: c.User().Name,
- To: to,
- TemplateName: "invite_email",
- Props: dto.Props{
- "subject": subject,
- "logo": web.LogoURL(c),
- },
- })
-
- return nil
- })
-}
-
func getActiveSubscribers(ctx context.Context, post *entity.Post, channel enum.NotificationChannel, event enum.NotificationEvent) ([]*entity.User, error) {
q := &query.GetActiveSubscribers{
Number: post.Number,
diff --git a/app/tasks/tasks_test.go b/app/tasks/tasks_test.go
deleted file mode 100644
index 61b7ac414..000000000
--- a/app/tasks/tasks_test.go
+++ /dev/null
@@ -1,691 +0,0 @@
-package tasks_test
-
-import (
- "context"
- "github.com/getfider/fider/app/pkg/webhook"
- "html/template"
- "testing"
- "time"
-
- "github.com/getfider/fider/app/actions"
- "github.com/getfider/fider/app/models/entity"
- "github.com/getfider/fider/app/models/enum"
- "github.com/getfider/fider/app/models/query"
-
- "github.com/getfider/fider/app/models/cmd"
- "github.com/getfider/fider/app/models/dto"
- . "github.com/getfider/fider/app/pkg/assert"
- "github.com/getfider/fider/app/pkg/bus"
- "github.com/getfider/fider/app/pkg/mock"
- "github.com/getfider/fider/app/services/email/emailmock"
- "github.com/getfider/fider/app/tasks"
-)
-
-func TestSendSignUpEmailTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- worker := mock.NewWorker()
- task := tasks.SendSignUpEmail(&actions.CreateTenant{
- VerificationKey: "1234",
- }, "http://domain.com")
-
- err := worker.
- AsUser(mock.JonSnow).
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("signup_email")
- Expect(emailmock.MessageHistory[0].Tenant).IsNil()
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Fider")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Props: dto.Props{
- "link": "<a href='http://domain.com/signup/verify?k=1234'>http://domain.com/signup/verify?k=1234</a>",
- },
- })
-}
-
-func TestSendSignInEmailTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- worker := mock.NewWorker()
- task := tasks.SendSignInEmail("[email protected]", "9876")
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("signin_email")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals(mock.DemoTenant.Name)
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Address: "[email protected]",
- Props: dto.Props{
- "siteName": mock.DemoTenant.Name,
- "link": "<a href='http://domain.com/signin/verify?k=9876'>http://domain.com/signin/verify?k=9876</a>",
- },
- })
-}
-
-func TestSendChangeEmailConfirmationTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- worker := mock.NewWorker()
- task := tasks.SendChangeEmailConfirmation(&actions.ChangeUserEmail{
- Email: "[email protected]",
- VerificationKey: "13579",
- Requestor: mock.JonSnow,
- })
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_emailaddress_email")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals(mock.DemoTenant.Name)
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Jon Snow",
- Address: "[email protected]",
- Props: dto.Props{
- "name": "Jon Snow",
- "oldEmail": "[email protected]",
- "newEmail": "[email protected]",
- "link": "<a href='http://domain.com/change-email/verify?k=13579'>http://domain.com/change-email/verify?k=13579</a>",
- },
- })
-}
-
-func TestNotifyAboutNewPostTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- var addNewNotification *cmd.AddNewNotification
- bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
- addNewNotification = c
- return nil
- })
-
- bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
- q.Result = []*entity.User{
- mock.AryaStark,
- }
- return nil
- })
-
- var triggerWebhooks *cmd.TriggerWebhooks
- bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
- triggerWebhooks = c
- return nil
- })
-
- worker := mock.NewWorker()
- post := &entity.Post{
- ID: 1,
- Number: 1,
- Title: "Add support for TypeScript",
- Slug: "add-support-for-typescript",
- Description: "TypeScript is great, please add support for it",
- }
- task := tasks.NotifyAboutNewPost(post)
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("new_post")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "title": "Add support for TypeScript",
- "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
- "siteName": "Demonstration",
- "userName": "Jon Snow",
- "content": template.HTML("<p>TypeScript is great, please add support for it</p>"),
- "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
- "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Arya Stark",
- Address: "[email protected]",
- Props: dto.Props{},
- })
-
- Expect(addNewNotification).IsNotNil()
- Expect(addNewNotification.PostID).Equals(post.ID)
- Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
- Expect(addNewNotification.Title).Equals("New post: **Add support for TypeScript**")
- Expect(addNewNotification.User).Equals(mock.AryaStark)
-
- Expect(triggerWebhooks).IsNotNil()
- Expect(triggerWebhooks.Type).Equals(enum.WebhookNewPost)
- Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
- "post_id": post.ID,
- "post_number": post.Number,
- "post_title": post.Title,
- "post_slug": post.Slug,
- "post_description": post.Description,
- "post_url": "http://domain.com/posts/1/add-support-for-typescript",
- "author_id": mock.JonSnow.ID,
- "author_name": mock.JonSnow.Name,
- "author_email": mock.JonSnow.Email,
- "author_role": mock.JonSnow.Role.String(),
- "tenant_id": mock.DemoTenant.ID,
- "tenant_name": mock.DemoTenant.Name,
- "tenant_subdomain": mock.DemoTenant.Subdomain,
- "tenant_status": mock.DemoTenant.Status.String(),
- "tenant_url": "http://domain.com",
- })
-}
-
-func TestNotifyAboutNewCommentTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- var addNewNotification *cmd.AddNewNotification
- bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
- addNewNotification = c
- return nil
- })
-
- bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
- q.Result = []*entity.User{
- mock.JonSnow,
- }
- return nil
- })
-
- var triggerWebhooks *cmd.TriggerWebhooks
- bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
- triggerWebhooks = c
- return nil
- })
-
- worker := mock.NewWorker()
- post := &entity.Post{
- ID: 1,
- Number: 1,
- Title: "Add support for TypeScript",
- Slug: "add-support-for-typescript",
- Description: "TypeScript is great, please add support for it",
- User: mock.JonSnow,
- }
- task := tasks.NotifyAboutNewComment(post, "I agree")
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.AryaStark).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("new_comment")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "title": "Add support for TypeScript",
- "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
- "siteName": "Demonstration",
- "userName": "Arya Stark",
- "content": template.HTML("<p>I agree</p>"),
- "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
- "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
- "unsubscribe": "<a href='http://domain.com/posts/1/add-support-for-typescript'>unsubscribe from it</a>",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Arya Stark")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Jon Snow",
- Address: "[email protected]",
- Props: dto.Props{},
- })
-
- Expect(addNewNotification).IsNotNil()
- Expect(addNewNotification.PostID).Equals(post.ID)
- Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
- Expect(addNewNotification.Title).Equals("**Arya Stark** left a comment on **Add support for TypeScript**")
- Expect(addNewNotification.User).Equals(mock.JonSnow)
-
- Expect(triggerWebhooks).IsNotNil()
- Expect(triggerWebhooks.Type).Equals(enum.WebhookNewComment)
- Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
- "comment": "I agree",
- "post_id": post.ID,
- "post_number": post.Number,
- "post_title": post.Title,
- "post_slug": post.Slug,
- "post_description": post.Description,
- "post_url": "http://domain.com/posts/1/add-support-for-typescript",
- "post_author_id": mock.JonSnow.ID,
- "post_author_name": mock.JonSnow.Name,
- "post_author_email": mock.JonSnow.Email,
- "post_author_role": mock.JonSnow.Role.String(),
- "author_id": mock.AryaStark.ID,
- "author_name": mock.AryaStark.Name,
- "author_email": mock.AryaStark.Email,
- "author_role": mock.AryaStark.Role.String(),
- "tenant_id": mock.DemoTenant.ID,
- "tenant_name": mock.DemoTenant.Name,
- "tenant_subdomain": mock.DemoTenant.Subdomain,
- "tenant_status": mock.DemoTenant.Status.String(),
- "tenant_url": "http://domain.com",
- })
-}
-
-func TestNotifyAboutStatusChangeTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- var addNewNotification *cmd.AddNewNotification
- bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
- addNewNotification = c
- return nil
- })
-
- bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
- q.Result = []*entity.User{
- mock.AryaStark,
- }
- return nil
- })
-
- var triggerWebhooks *cmd.TriggerWebhooks
- bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
- triggerWebhooks = c
- return nil
- })
-
- worker := mock.NewWorker()
- post := &entity.Post{
- ID: 1,
- Number: 1,
- Title: "Add support for TypeScript",
- Slug: "add-support-for-typescript",
- Description: "TypeScript is great, please add support for it",
- User: mock.AryaStark,
- Status: enum.PostPlanned,
- Response: &entity.PostResponse{
- RespondedAt: time.Now(),
- Text: "Planned for next release.",
- User: mock.JonSnow,
- },
- }
-
- task := tasks.NotifyAboutStatusChange(post, enum.PostOpen)
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_status")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "title": "Add support for TypeScript",
- "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",
- "siteName": "Demonstration",
- "content": template.HTML("<p>Planned for next release.</p>"),
- "duplicate": "",
- "status": "Planned",
- "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",
- "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
- "unsubscribe": "<a href='http://domain.com/posts/1/add-support-for-typescript'>unsubscribe from it</a>",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Arya Stark",
- Address: "[email protected]",
- Props: dto.Props{},
- })
-
- Expect(addNewNotification).IsNotNil()
- Expect(addNewNotification.PostID).Equals(post.ID)
- Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
- Expect(addNewNotification.Title).Equals("**Jon Snow** changed status of **Add support for TypeScript** to **planned**")
- Expect(addNewNotification.User).Equals(mock.AryaStark)
-
- Expect(triggerWebhooks).IsNotNil()
- Expect(triggerWebhooks.Type).Equals(enum.WebhookChangeStatus)
- Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
- "post_old_status": enum.PostOpen.Name(),
- "post_id": post.ID,
- "post_number": post.Number,
- "post_title": post.Title,
- "post_slug": post.Slug,
- "post_description": post.Description,
- "post_status": post.Status.Name(),
- "post_url": "http://domain.com/posts/1/add-support-for-typescript",
- "post_author_id": mock.AryaStark.ID,
- "post_author_name": mock.AryaStark.Name,
- "post_author_email": mock.AryaStark.Email,
- "post_author_role": mock.AryaStark.Role.String(),
- "post_response": true,
- "post_response_text": post.Response.Text,
- "post_response_responded_at": post.Response.RespondedAt,
- "post_response_author_id": mock.JonSnow.ID,
- "post_response_author_name": mock.JonSnow.Name,
- "post_response_author_email": mock.JonSnow.Email,
- "post_response_author_role": mock.JonSnow.Role.String(),
- "author_id": mock.JonSnow.ID,
- "author_name": mock.JonSnow.Name,
- "author_email": mock.JonSnow.Email,
- "author_role": mock.JonSnow.Role.String(),
- "tenant_id": mock.DemoTenant.ID,
- "tenant_name": mock.DemoTenant.Name,
- "tenant_subdomain": mock.DemoTenant.Subdomain,
- "tenant_status": mock.DemoTenant.Status.String(),
- "tenant_url": "http://domain.com",
- })
-}
-
-func TestNotifyAboutDeletePostTask(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- var addNewNotification *cmd.AddNewNotification
- bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
- addNewNotification = c
- return nil
- })
-
- bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
- q.Result = []*entity.User{
- mock.AryaStark,
- }
- return nil
- })
-
- var triggerWebhooks *cmd.TriggerWebhooks
- bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
- triggerWebhooks = c
- return nil
- })
-
- worker := mock.NewWorker()
- post := &entity.Post{
- ID: 1,
- Number: 1,
- Title: "Add support for TypeScript",
- Slug: "add-support-for-typescript",
- Description: "TypeScript is great, please add support for it",
- User: mock.AryaStark,
- Status: enum.PostDeleted,
- Response: &entity.PostResponse{
- RespondedAt: time.Now(),
- Text: "Invalid post!",
- User: mock.JonSnow,
- },
- }
-
- task := tasks.NotifyAboutDeletedPost(post)
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("delete_post")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "title": "Add support for TypeScript",
- "siteName": "Demonstration",
- "content": template.HTML("<p>Invalid post!</p>"),
- "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Arya Stark",
- Address: "[email protected]",
- Props: dto.Props{},
- })
-
- Expect(addNewNotification).IsNotNil()
- Expect(addNewNotification.PostID).Equals(post.ID)
- Expect(addNewNotification.Link).Equals("")
- Expect(addNewNotification.Title).Equals("**Jon Snow** deleted **Add support for TypeScript**")
- Expect(addNewNotification.User).Equals(mock.AryaStark)
-
- Expect(triggerWebhooks).IsNotNil()
- Expect(triggerWebhooks.Type).Equals(enum.WebhookDeletePost)
- Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
- "post_id": post.ID,
- "post_number": post.Number,
- "post_title": post.Title,
- "post_slug": post.Slug,
- "post_description": post.Description,
- "post_status": post.Status.Name(),
- "post_url": "http://domain.com/posts/1/add-support-for-typescript",
- "post_author_id": mock.AryaStark.ID,
- "post_author_name": mock.AryaStark.Name,
- "post_author_email": mock.AryaStark.Email,
- "post_author_role": mock.AryaStark.Role.String(),
- "post_response": true,
- "post_response_text": post.Response.Text,
- "post_response_responded_at": post.Response.RespondedAt,
- "post_response_author_id": mock.JonSnow.ID,
- "post_response_author_name": mock.JonSnow.Name,
- "post_response_author_email": mock.JonSnow.Email,
- "post_response_author_role": mock.JonSnow.Role.String(),
- "author_id": mock.JonSnow.ID,
- "author_name": mock.JonSnow.Name,
- "author_email": mock.JonSnow.Email,
- "author_role": mock.JonSnow.Role.String(),
- "tenant_id": mock.DemoTenant.ID,
- "tenant_name": mock.DemoTenant.Name,
- "tenant_subdomain": mock.DemoTenant.Subdomain,
- "tenant_status": mock.DemoTenant.Status.String(),
- "tenant_url": "http://domain.com",
- })
-}
-
-func TestNotifyAboutStatusChangeTask_Duplicate(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- var addNewNotification *cmd.AddNewNotification
- bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
- addNewNotification = c
- return nil
- })
-
- bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
- q.Result = []*entity.User{
- mock.AryaStark,
- }
- return nil
- })
-
- var triggerWebhooks *cmd.TriggerWebhooks
- bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
- triggerWebhooks = c
- return nil
- })
-
- worker := mock.NewWorker()
- post := &entity.Post{
- ID: 2,
- Number: 2,
- Title: "I need TypeScript",
- Slug: "i-need-typescript",
- User: mock.AryaStark,
- Status: enum.PostDuplicate,
- Response: &entity.PostResponse{
- RespondedAt: time.Now(),
- User: mock.JonSnow,
- Original: &entity.OriginalPost{
- Number: 1,
- Title: "Add support for TypeScript",
- Slug: "add-support-for-typescript",
- Status: enum.PostOpen,
- },
- },
- }
-
- task := tasks.NotifyAboutStatusChange(post, enum.PostOpen)
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("change_status")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "title": "I need TypeScript",
- "postLink": "<a href='http://domain.com/posts/2/i-need-typescript'>#2</a>",
- "siteName": "Demonstration",
- "content": template.HTML(""),
- "duplicate": "<a href='http://domain.com/posts/1/add-support-for-typescript'>Add support for TypeScript</a>",
- "status": "Duplicate",
- "view": "<a href='http://domain.com/posts/2/i-need-typescript'>view it on your browser</a>",
- "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",
- "unsubscribe": "<a href='http://domain.com/posts/2/i-need-typescript'>unsubscribe from it</a>",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
- Expect(emailmock.MessageHistory[0].To).HasLen(1)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Name: "Arya Stark",
- Address: "[email protected]",
- Props: dto.Props{},
- })
-
- Expect(addNewNotification).IsNotNil()
- Expect(addNewNotification.PostID).Equals(post.ID)
- Expect(addNewNotification.Link).Equals("/posts/2/i-need-typescript")
- Expect(addNewNotification.Title).Equals("**Jon Snow** changed status of **I need TypeScript** to **duplicate**")
- Expect(addNewNotification.User).Equals(mock.AryaStark)
-
- Expect(triggerWebhooks).IsNotNil()
- Expect(triggerWebhooks.Type).Equals(enum.WebhookChangeStatus)
- Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
- "post_old_status": enum.PostOpen.Name(),
- "post_id": post.ID,
- "post_number": post.Number,
- "post_title": post.Title,
- "post_slug": post.Slug,
- "post_description": post.Description,
- "post_status": post.Status.Name(),
- "post_url": "http://domain.com/posts/2/i-need-typescript",
- "post_author_id": mock.AryaStark.ID,
- "post_author_name": mock.AryaStark.Name,
- "post_author_email": mock.AryaStark.Email,
- "post_author_role": mock.AryaStark.Role.String(),
- "post_response": true,
- "post_response_text": post.Response.Text,
- "post_response_responded_at": post.Response.RespondedAt,
- "post_response_author_id": mock.JonSnow.ID,
- "post_response_author_name": mock.JonSnow.Name,
- "post_response_author_email": mock.JonSnow.Email,
- "post_response_author_role": mock.JonSnow.Role.String(),
- "post_response_original_number": post.Response.Original.Number,
- "post_response_original_title": post.Response.Original.Title,
- "post_response_original_slug": post.Response.Original.Slug,
- "post_response_original_status": post.Response.Original.Status.Name(),
- "post_response_original_url": "http://domain.com/posts/1/add-support-for-typescript",
- "author_id": mock.JonSnow.ID,
- "author_name": mock.JonSnow.Name,
- "author_email": mock.JonSnow.Email,
- "author_role": mock.JonSnow.Role.String(),
- "tenant_id": mock.DemoTenant.ID,
- "tenant_name": mock.DemoTenant.Name,
- "tenant_subdomain": mock.DemoTenant.Subdomain,
- "tenant_status": mock.DemoTenant.Status.String(),
- "tenant_url": "http://domain.com",
- })
-}
-
-func TestSendInvites(t *testing.T) {
- RegisterT(t)
- bus.Init(emailmock.Service{})
-
- savedKeys := make([]*cmd.SaveVerificationKey, 0)
- bus.AddHandler(func(ctx context.Context, c *cmd.SaveVerificationKey) error {
- savedKeys = append(savedKeys, c)
- return nil
- })
-
- worker := mock.NewWorker()
- task := tasks.SendInvites("My Subject", "Click here: %invite%", []*actions.UserInvitation{
- {Email: "[email protected]", VerificationKey: "1234"},
- {Email: "[email protected]", VerificationKey: "5678"},
- })
-
- err := worker.
- OnTenant(mock.DemoTenant).
- AsUser(mock.JonSnow).
- WithBaseURL("http://domain.com").
- Execute(task)
-
- Expect(err).IsNil()
- Expect(emailmock.MessageHistory).HasLen(1)
- Expect(emailmock.MessageHistory[0].TemplateName).Equals("invite_email")
- Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)
- Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{
- "subject": "My Subject",
- "logo": "https://getfider.com/images/logo-100x100.png",
- })
- Expect(emailmock.MessageHistory[0].From).Equals("Jon Snow")
- Expect(emailmock.MessageHistory[0].To).HasLen(2)
- Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{
- Address: "[email protected]",
- Props: dto.Props{
- "message": template.HTML(`<p>Click here: <a href="http://domain.com/invite/verify?k=1234">http://domain.com/invite/verify?k=1234</a></p>`),
- },
- })
- Expect(emailmock.MessageHistory[0].To[1]).Equals(dto.Recipient{
- Address: "[email protected]",
- Props: dto.Props{
- "message": template.HTML(`<p>Click here: <a href="http://domain.com/invite/verify?k=5678">http://domain.com/invite/verify?k=5678</a></p>`),
- },
- })
-
- Expect(savedKeys).HasLen(2)
- Expect(savedKeys[0].Key).Equals("1234")
- Expect(savedKeys[0].Request.GetEmail()).Equals("[email protected]")
- Expect(savedKeys[1].Key).Equals("5678")
- Expect(savedKeys[1].Request.GetEmail()).Equals("[email protected]")
-}