Interactive/pkg/services/accounts.go

74 lines
2.2 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"
2024-03-20 12:57:21 +00:00
"git.solsynth.dev/hydrogen/identity/pkg/grpc/proto"
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/grpc"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
"github.com/spf13/viper"
"time"
2024-02-03 07:20:32 +00:00
)
func FollowAccount(followerId, followingId uint) error {
relationship := models.AccountMembership{
FollowerID: followerId,
FollowingID: followingId,
}
return database.C.Create(&relationship).Error
}
func UnfollowAccount(followerId, followingId uint) error {
return database.C.Where(models.AccountMembership{
FollowerID: followerId,
FollowingID: followingId,
}).Delete(&models.AccountMembership{}).Error
}
func GetAccountFollowed(user models.Account, target models.Account) (models.AccountMembership, bool) {
var relationship models.AccountMembership
err := database.C.Model(&models.AccountMembership{}).
Where(&models.AccountMembership{FollowerID: user.ID, FollowingID: target.ID}).
2024-02-03 12:44:16 +00:00
First(&relationship).
2024-02-03 07:20:32 +00:00
Error
2024-02-03 12:44:16 +00:00
return relationship, err == nil
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()
return grpc.Friendships.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),
})
}
2024-03-31 13:46:59 +00:00
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
_, err := grpc.Notify.NotifyUser(ctx, &proto.NotifyRequest{
ClientId: viper.GetString("identity.client_id"),
ClientSecret: viper.GetString("identity.client_secret"),
Subject: subject,
Content: content,
Links: links,
2024-03-31 14:49:32 +00:00
RecipientId: uint64(user.ExternalID),
2024-03-31 13:46:59 +00:00
IsRealtime: realtime,
IsImportant: false,
})
return err
}