2024-02-03 07:20:32 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-03-31 13:46:59 +00:00
|
|
|
"context"
|
2024-08-17 17:06:52 +00:00
|
|
|
"fmt"
|
2024-09-16 16:12:09 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-16 02:53:02 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
2024-07-23 08:12:19 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
2024-04-26 16:13:47 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-08-17 17:06:52 +00:00
|
|
|
"github.com/samber/lo"
|
2024-02-03 07:20:32 +00:00
|
|
|
)
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
func GetAccountWithID(id uint) (models.Account, error) {
|
|
|
|
var account models.Account
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-17 17:06:52 +00:00
|
|
|
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)
|
2024-09-03 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-08-17 17:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2024-07-23 08:12:19 +00:00
|
|
|
func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error {
|
|
|
|
if isUpvote {
|
|
|
|
user.TotalUpvote += delta
|
|
|
|
} else {
|
|
|
|
user.TotalDownvote += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
return database.C.Save(&user).Error
|
|
|
|
}
|
|
|
|
|
2024-10-15 17:04:35 +00:00
|
|
|
func NotifyPosterAccount(user models.Account, post models.Post, title, body string, subtitle *string) error {
|
2024-02-21 14:58:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-07-16 02:53:02 +00:00
|
|
|
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
2024-06-22 09:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-16 02:53:02 +00:00
|
|
|
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
2024-09-11 15:42:46 +00:00
|
|
|
UserId: uint64(user.ID),
|
2024-07-16 02:53:02 +00:00
|
|
|
Notify: &proto.NotifyRequest{
|
2024-10-15 17:04:35 +00:00
|
|
|
Topic: "interactive.feedback",
|
|
|
|
Title: title,
|
|
|
|
Subtitle: subtitle,
|
|
|
|
Body: body,
|
|
|
|
Metadata: hyper.EncodeMap(map[string]any{
|
2024-10-17 12:05:05 +00:00
|
|
|
"related_post": TruncatePostContent(post),
|
2024-10-15 17:04:35 +00:00
|
|
|
}),
|
2024-07-16 02:53:02 +00:00
|
|
|
IsRealtime: false,
|
|
|
|
IsForcePush: true,
|
|
|
|
},
|
2024-02-21 14:58:51 +00:00
|
|
|
})
|
2024-04-26 16:13:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when notify account...")
|
|
|
|
} else {
|
2024-09-11 15:42:46 +00:00
|
|
|
log.Debug().Uint("uid", user.ID).Msg("Notified account.")
|
2024-04-26 16:13:47 +00:00
|
|
|
}
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
return err
|
2024-02-08 10:47:29 +00:00
|
|
|
}
|