From 4690e8be101907ee9577db77cd63d2c706222698 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 12 Feb 2024 17:24:17 +0800 Subject: [PATCH] :sparkles: Notify to followers when their following account posted --- pkg/models/accounts.go | 2 ++ pkg/services/posts.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/models/accounts.go b/pkg/models/accounts.go index 0c32fa5..9e7a889 100644 --- a/pkg/models/accounts.go +++ b/pkg/models/accounts.go @@ -27,6 +27,8 @@ type AccountMembership struct { ID uint `json:"id" gorm:"primaryKey"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` + Follower Account `json:"follower"` + Following Account `json:"following"` FollowerID uint FollowingID uint } diff --git a/pkg/services/posts.go b/pkg/services/posts.go index d71c709..1591b9e 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -209,6 +209,27 @@ func NewPost( } } + go func() { + var subscribers []models.AccountMembership + if err := database.C.Where(&models.AccountMembership{ + FollowingID: user.ID, + }).Preload("Follower").Find(&subscribers).Error; err != nil { + return + } + + accounts := lo.Map(subscribers, func(item models.AccountMembership, index int) models.Account { + return item.Follower + }) + + for _, account := range accounts { + _ = NotifyAccount( + account, + fmt.Sprintf("%s just posted a post", user.Name), + "Account you followed post a brand new post. Check it out!", + ) + } + }() + return post, nil }