♻️ Moved account-based post to publisher-based post
This commit is contained in:
@ -1,84 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func GetAccountWithID(id uint) (models.Account, error) {
|
||||
var account models.Account
|
||||
func GetAccountWithID(id uint) (models.Publisher, error) {
|
||||
var account models.Publisher
|
||||
if err := database.C.Where("id = ?", id).First(&account).Error; err != nil {
|
||||
return account, fmt.Errorf("unable to get account by id: %v", err)
|
||||
}
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func ListAccountFriends(user models.Account) ([]models.Account, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account friends: %v", err)
|
||||
}
|
||||
result, err := proto.NewAuthClient(pc).ListUserFriends(ctx, &proto.ListUserRelativeRequest{
|
||||
UserId: uint64(user.ID),
|
||||
IsRelated: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account friends: %v", err)
|
||||
}
|
||||
|
||||
out := lo.Map(result.Data, func(item *proto.SimpleUserInfo, index int) uint {
|
||||
return uint(item.Id)
|
||||
})
|
||||
|
||||
var accounts []models.Account
|
||||
if err = database.C.Where("id IN ?", out).Find(&accounts).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to linking listed account friends: %v", err)
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func ListAccountBlockedUsers(user models.Account) ([]models.Account, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account blocked users: %v", err)
|
||||
}
|
||||
result, err := proto.NewAuthClient(pc).ListUserBlocklist(ctx, &proto.ListUserRelativeRequest{
|
||||
UserId: uint64(user.ID),
|
||||
IsRelated: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account blocked users: %v", err)
|
||||
}
|
||||
|
||||
out := lo.Map(result.Data, func(item *proto.SimpleUserInfo, index int) uint {
|
||||
return uint(item.Id)
|
||||
})
|
||||
|
||||
var accounts []models.Account
|
||||
if err = database.C.Where("id IN ?", out).Find(&accounts).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to linking listed blocked users: %v", err)
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error {
|
||||
func ModifyPosterVoteCount(user models.Publisher, isUpvote bool, delta int) error {
|
||||
if isUpvote {
|
||||
user.TotalUpvote += delta
|
||||
} else {
|
||||
@ -88,32 +28,29 @@ func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error
|
||||
return database.C.Save(&user).Error
|
||||
}
|
||||
|
||||
func NotifyPosterAccount(user models.Account, post models.Post, title, body string, subtitle *string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body string, subtitle ...string) error {
|
||||
if pub.AccountID == nil {
|
||||
return nil
|
||||
}
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
||||
UserId: uint64(user.ID),
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.feedback",
|
||||
Title: title,
|
||||
Subtitle: subtitle,
|
||||
Body: body,
|
||||
Metadata: hyper.EncodeMap(map[string]any{
|
||||
"related_post": TruncatePostContent(post),
|
||||
}),
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
|
||||
if len(subtitle) == 0 {
|
||||
subtitle = append(subtitle, "")
|
||||
}
|
||||
|
||||
err := authkit.NotifyUser(gap.Nx, uint64(*pub.AccountID), pushkit.Notification{
|
||||
Topic: "interactive.feedback",
|
||||
Title: title,
|
||||
Subtitle: subtitle[0],
|
||||
Body: body,
|
||||
Priority: 4,
|
||||
Metadata: map[string]any{
|
||||
"related_post": TruncatePostContent(post),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("An error occurred when notify account...")
|
||||
} else {
|
||||
log.Debug().Uint("uid", user.ID).Msg("Notified account.")
|
||||
log.Debug().Uint("uid", pub.ID).Msg("Notified account.")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -2,9 +2,9 @@ package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"gorm.io/gorm"
|
||||
@ -28,7 +28,7 @@ func GetCategory(alias string) (models.Category, error) {
|
||||
func GetCategoryWithID(id uint) (models.Category, error) {
|
||||
var category models.Category
|
||||
if err := database.C.Where(models.Category{
|
||||
BaseModel: hyper.BaseModel{ID: id},
|
||||
BaseModel: cruda.BaseModel{ID: id},
|
||||
}).First(&category).Error; err != nil {
|
||||
return category, err
|
||||
}
|
||||
@ -64,7 +64,7 @@ func DeleteCategory(category models.Category) error {
|
||||
func GetTagWithID(id uint) (models.Tag, error) {
|
||||
var tag models.Tag
|
||||
if err := database.C.Where(models.Tag{
|
||||
BaseModel: hyper.BaseModel{ID: id},
|
||||
BaseModel: cruda.BaseModel{ID: id},
|
||||
}).First(&tag).Error; err != nil {
|
||||
return tag, err
|
||||
}
|
||||
|
@ -3,6 +3,10 @@ package services
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -11,12 +15,11 @@ import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
if user == nil {
|
||||
return tx.Where("visibility = ?", models.PostVisibilityAll)
|
||||
}
|
||||
@ -28,13 +31,13 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
NoneVisibility = models.PostVisibilityNone
|
||||
)
|
||||
|
||||
friends, _ := ListAccountFriends(*user)
|
||||
allowlist := lo.Map(friends, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
friends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true)
|
||||
allowlist := lo.Map(friends, func(item *proto.UserInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
blocked, _ := ListAccountBlockedUsers(*user)
|
||||
blocklist := lo.Map(blocked, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
blocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), true)
|
||||
blocklist := lo.Map(blocked, func(item *proto.UserInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
|
||||
tx = tx.Where(
|
||||
@ -54,17 +57,15 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
}
|
||||
|
||||
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
|
||||
prefix := viper.GetString("database.prefix")
|
||||
return tx.Joins(fmt.Sprintf("JOIN %spost_categories ON %sposts.id = %spost_categories.post_id", prefix, prefix, prefix)).
|
||||
Joins(fmt.Sprintf("JOIN %scategories ON %scategories.id = %spost_categories.category_id", prefix, prefix, prefix)).
|
||||
Where(fmt.Sprintf("%scategories.alias = ?", prefix), alias)
|
||||
return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").
|
||||
Joins("JOIN categories ON categories.id = post_categories.category_id").
|
||||
Where("categories.alias = ?", alias)
|
||||
}
|
||||
|
||||
func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {
|
||||
prefix := viper.GetString("database.prefix")
|
||||
return tx.Joins(fmt.Sprintf("JOIN %spost_tags ON %sposts.id = %spost_tags.post_id", prefix, prefix, prefix)).
|
||||
Joins(fmt.Sprintf("JOIN %stags ON %stags.id = %spost_tags.tag_id", prefix, prefix, prefix)).
|
||||
Where(fmt.Sprintf("%stags.alias = ?", prefix), alias)
|
||||
return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id").
|
||||
Joins("JOIN tags ON tags.id = post_tags.tag_id").
|
||||
Where("tags.alias = ?", alias)
|
||||
}
|
||||
|
||||
func FilterPostWithRealm(tx *gorm.DB, id uint) *gorm.DB {
|
||||
@ -289,7 +290,7 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
func NewPost(user models.Publisher, item models.Post) (models.Post, error) {
|
||||
if item.Alias != nil && len(*item.Alias) == 0 {
|
||||
item.Alias = nil
|
||||
}
|
||||
@ -302,9 +303,9 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
}
|
||||
|
||||
if item.Realm != nil {
|
||||
item.AreaAlias = &item.Realm.Alias
|
||||
item.AliasPrefix = &item.Realm.Alias
|
||||
} else {
|
||||
item.AreaAlias = &user.Name
|
||||
item.AliasPrefix = &user.Name
|
||||
}
|
||||
|
||||
log.Debug().Any("body", item.Body).Msg("Posting a post...")
|
||||
@ -316,16 +317,6 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
return item, err
|
||||
}
|
||||
|
||||
if item.RealmID != nil {
|
||||
log.Debug().Uint("id", *item.RealmID).Msg("Looking for post author realm...")
|
||||
member, err := GetRealmMember(*item.RealmID, user.ID)
|
||||
if err != nil {
|
||||
return item, fmt.Errorf("you aren't a part of that realm: %v", err)
|
||||
} else if !item.Realm.IsCommunity && member.PowerLevel < 25 {
|
||||
return item, fmt.Errorf("you need has power level above 25 of a realm or in a community realm to post")
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("Saving post record into database...")
|
||||
if err := database.C.Save(&item).Error; err != nil {
|
||||
return item, err
|
||||
@ -338,14 +329,14 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
Where("id = ?", item.ReplyID).
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
log.Debug().Uint("user", op.AuthorID).Msg("Notifying the original poster their post got replied...")
|
||||
if op.Publisher.AccountID != nil && op.Publisher.ID != user.ID {
|
||||
log.Debug().Uint("user", *op.Publisher.AccountID).Msg("Notifying the original poster their post got replied...")
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
op.Publisher,
|
||||
op,
|
||||
"Post got replied",
|
||||
fmt.Sprintf("%s (%s) replied your post (#%d).", user.Nick, user.Name, op.ID),
|
||||
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
|
||||
fmt.Sprintf("%s replied you", user.Nick),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
@ -392,9 +383,9 @@ func EditPost(item models.Post) (models.Post, error) {
|
||||
}
|
||||
|
||||
if item.Realm != nil {
|
||||
item.AreaAlias = &item.Realm.Alias
|
||||
item.AliasPrefix = &item.Realm.Alias
|
||||
} else {
|
||||
item.AreaAlias = &item.Author.Name
|
||||
item.AliasPrefix = &item.Publisher.Name
|
||||
}
|
||||
|
||||
item, err := EnsurePostCategoriesAndTags(item)
|
||||
@ -411,7 +402,7 @@ func DeletePost(item models.Post) error {
|
||||
return database.C.Delete(&item).Error
|
||||
}
|
||||
|
||||
func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
var op models.Post
|
||||
if err := database.C.
|
||||
Where("id = ?", reaction.PostID).
|
||||
@ -422,13 +413,13 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
|
||||
if err := database.C.Where(reaction).First(&reaction).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if op.Author.ID != user.ID {
|
||||
if op.Publisher.AccountID != nil && *op.Publisher.AccountID != user.ID {
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
op.Publisher,
|
||||
op,
|
||||
"Post got reacted",
|
||||
fmt.Sprintf("%s (%s) reacted your post a %s.", user.Nick, user.Name, reaction.Symbol),
|
||||
lo.ToPtr(fmt.Sprintf("%s reacted you", user.Nick)),
|
||||
fmt.Sprintf("%s reacted you", user.Nick),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
@ -437,7 +428,7 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
|
||||
err = database.C.Save(&reaction).Error
|
||||
if err == nil && reaction.Attitude != models.AttitudeNeutral {
|
||||
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, 1)
|
||||
_ = ModifyPosterVoteCount(op.Publisher, reaction.Attitude == models.AttitudePositive, 1)
|
||||
|
||||
if reaction.Attitude == models.AttitudePositive {
|
||||
op.TotalUpvote++
|
||||
@ -454,7 +445,7 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
} else {
|
||||
err = database.C.Delete(&reaction).Error
|
||||
if err == nil && reaction.Attitude != models.AttitudeNeutral {
|
||||
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, -1)
|
||||
_ = ModifyPosterVoteCount(op.Publisher, reaction.Attitude == models.AttitudePositive, -1)
|
||||
|
||||
if reaction.Attitude == models.AttitudePositive {
|
||||
op.TotalUpvote--
|
||||
|
@ -1,26 +1,15 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func GetPublisher(alias string) (any, error) {
|
||||
realm, err := GetRealmWithAlias(alias)
|
||||
if err == nil {
|
||||
return fiber.Map{
|
||||
"type": "realm",
|
||||
"data": realm,
|
||||
}, nil
|
||||
func GetPublisher(id uint, userID uint) (models.Publisher, error) {
|
||||
var publisher models.Publisher
|
||||
if err := database.C.Where("id = ? AND account_id = ?", id, userID).First(&publisher).Error; err != nil {
|
||||
return publisher, fmt.Errorf("unable to get publisher: %v", err)
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
if err = database.C.Where("name = ?", alias).First(&account).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fiber.Map{
|
||||
"type": "account",
|
||||
"data": account,
|
||||
}, nil
|
||||
return publisher, nil
|
||||
}
|
||||
|
@ -1,67 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func GetRealmWithID(id uint) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Id: lo.ToPtr(uint64(id)),
|
||||
})
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
prefix := viper.GetString("database.prefix")
|
||||
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
||||
return models.Realm{BaseRealm: rm}, err
|
||||
}
|
||||
|
||||
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Alias: &alias,
|
||||
})
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
prefix := viper.GetString("database.prefix")
|
||||
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
||||
return models.Realm{BaseRealm: rm}, err
|
||||
}
|
||||
|
||||
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
|
||||
var realm models.Realm
|
||||
if err := database.C.Where("id = ?", realmId).First(&realm).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
||||
RealmId: lo.ToPtr(uint64(realm.ID)),
|
||||
UserId: lo.ToPtr(uint64(userId)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return response, nil
|
||||
}
|
||||
}
|
@ -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