♻️ Moved account-based post to publisher-based post
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetSubscriptionOnUser(user models.Account, target models.Account) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnUser(user authm.Account, target models.Publisher) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -25,7 +23,7 @@ func GetSubscriptionOnUser(user models.Account, target models.Account) (*models.
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnTag(user models.Account, target models.Tag) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnTag(user authm.Account, target models.Tag) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -36,7 +34,7 @@ func GetSubscriptionOnTag(user models.Account, target models.Tag) (*models.Subsc
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnCategory(user models.Account, target models.Category) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnCategory(user authm.Account, target models.Category) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -47,18 +45,7 @@ func GetSubscriptionOnCategory(user models.Account, target models.Category) (*mo
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnRealm(user models.Account, target models.Realm) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unable to get subscription: %v", err)
|
||||
}
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func SubscribeToUser(user models.Account, target models.Account) (models.Subscription, error) {
|
||||
func SubscribeToUser(user authm.Account, target models.Publisher) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -75,7 +62,7 @@ func SubscribeToUser(user models.Account, target models.Account) (models.Subscri
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToTag(user models.Account, target models.Tag) (models.Subscription, error) {
|
||||
func SubscribeToTag(user authm.Account, target models.Tag) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -92,7 +79,7 @@ func SubscribeToTag(user models.Account, target models.Tag) (models.Subscription
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToCategory(user models.Account, target models.Category) (models.Subscription, error) {
|
||||
func SubscribeToCategory(user authm.Account, target models.Category) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -109,24 +96,7 @@ func SubscribeToCategory(user models.Account, target models.Category) (models.Su
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToRealm(user models.Account, target models.Realm) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return subscription, fmt.Errorf("subscription already exists")
|
||||
}
|
||||
}
|
||||
|
||||
subscription = models.Subscription{
|
||||
FollowerID: user.ID,
|
||||
RealmID: &target.ID,
|
||||
}
|
||||
|
||||
err := database.C.Save(&subscription).Error
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func UnsubscribeFromUser(user models.Account, target models.Account) error {
|
||||
func UnsubscribeFromUser(user authm.Account, target models.Publisher) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -139,7 +109,7 @@ func UnsubscribeFromUser(user models.Account, target models.Account) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromTag(user models.Account, target models.Tag) error {
|
||||
func UnsubscribeFromTag(user authm.Account, target models.Tag) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -152,7 +122,7 @@ func UnsubscribeFromTag(user models.Account, target models.Tag) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromCategory(user models.Account, target models.Category) error {
|
||||
func UnsubscribeFromCategory(user authm.Account, target models.Category) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -165,20 +135,7 @@ func UnsubscribeFromCategory(user models.Account, target models.Category) error
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromRealm(user models.Account, target models.Realm) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("subscription does not exist")
|
||||
}
|
||||
return fmt.Errorf("unable to check subscription is exists or not: %v", err)
|
||||
}
|
||||
|
||||
err := database.C.Delete(&subscription).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyUserSubscription(poster models.Account, content string, title *string) error {
|
||||
func NotifyUserSubscription(poster models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("account_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@@ -197,30 +154,18 @@ func NotifyUserSubscription(poster models.Account, content string, title *string
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyTagSubscription(poster models.Tag, og models.Account, content string, title *string) error {
|
||||
func NotifyTagSubscription(poster models.Tag, og models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("tag_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@@ -239,30 +184,18 @@ func NotifyTagSubscription(poster models.Tag, og models.Account, content string,
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyCategorySubscription(poster models.Category, og models.Account, content string, title *string) error {
|
||||
func NotifyCategorySubscription(poster models.Category, og models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("category_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@@ -281,66 +214,12 @@ func NotifyCategorySubscription(poster models.Category, og models.Account, conte
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyRealmSubscription(poster models.Realm, og models.Account, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("realm_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
}
|
||||
|
||||
nTitle := fmt.Sprintf("New post in %s by %s (%s)", poster.Name, og.Nick, og.Name)
|
||||
nSubtitle := "From your subscription"
|
||||
|
||||
body := TruncatePostContentShort(content)
|
||||
if title != nil {
|
||||
body = fmt.Sprintf("%s\n%s", *title, body)
|
||||
}
|
||||
|
||||
userIDs := make([]uint64, 0, len(subscriptions))
|
||||
for _, subscription := range subscriptions {
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
|
Reference in New Issue
Block a user