Interactive/pkg/internal/services/accounts.go

65 lines
1.9 KiB
Go
Raw Normal View History

2024-02-03 07:20:32 +00:00
package services
import (
2024-03-31 13:46:59 +00:00
"context"
"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/hydrogen/passport/pkg/proto"
2024-04-26 16:13:47 +00:00
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"time"
2024-02-03 07:20:32 +00:00
)
2024-04-06 04:05:54 +00:00
func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResponse, error) {
2024-04-06 15:23:29 +00:00
var user models.Account
if err := database.C.Where("id = ?", userId).First(&user).Error; err != nil {
return nil, err
}
var related models.Account
if err := database.C.Where("id = ?", relatedId).First(&related).Error; err != nil {
return nil, err
}
2024-04-06 04:05:54 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
if err != nil {
return nil, err
}
return proto.NewFriendshipsClient(pc).GetFriendship(ctx, &proto.FriendshipTwoSideLookupRequest{
2024-04-06 15:23:29 +00:00
AccountId: uint64(user.ExternalID),
RelatedId: uint64(related.ExternalID),
2024-04-06 04:05:54 +00:00
Status: uint32(status),
})
}
func NotifyPosterAccount(user models.Account, subject, content string, links ...*proto.NotifyLink) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
if err != nil {
return err
}
_, err = proto.NewNotifyClient(pc).NotifyUser(ctx, &proto.NotifyRequest{
2024-04-13 11:26:44 +00:00
ClientId: viper.GetString("passport.client_id"),
ClientSecret: viper.GetString("passport.client_secret"),
2024-06-08 08:28:34 +00:00
Type: "interactive.feedback",
Subject: subject,
Content: content,
Links: links,
2024-03-31 14:49:32 +00:00
RecipientId: uint64(user.ExternalID),
IsRealtime: false,
IsForcePush: true,
})
2024-04-26 16:13:47 +00:00
if err != nil {
log.Warn().Err(err).Msg("An error occurred when notify account...")
} else {
log.Debug().Uint("eid", user.ExternalID).Msg("Notified account.")
2024-04-26 16:13:47 +00:00
}
return err
}