diff --git a/pkg/grpc/client.go b/pkg/grpc/client.go index ca3f014..c882dc8 100644 --- a/pkg/grpc/client.go +++ b/pkg/grpc/client.go @@ -8,6 +8,7 @@ import ( "google.golang.org/grpc" ) +var Friendships idpb.FriendshipsClient var Notify idpb.NotifyClient var Auth idpb.AuthClient @@ -16,6 +17,7 @@ func ConnectPassport() error { if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil { return err } else { + Friendships = idpb.NewFriendshipsClient(conn) Notify = idpb.NewNotifyClient(conn) Auth = idpb.NewAuthClient(conn) } diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index c59277f..583a847 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -34,6 +34,17 @@ func GetAccountFollowed(user models.Account, target models.Account) (models.Acco return relationship, err == nil } +func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResponse, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + return grpc.Friendships.GetFriendship(ctx, &proto.FriendshipTwoSideLookupRequest{ + AccountId: uint64(userId), + RelatedId: uint64(relatedId), + Status: uint32(status), + }) +} + 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() diff --git a/pkg/services/realms.go b/pkg/services/realms.go index 2588f52..9fb4b0a 100644 --- a/pkg/services/realms.go +++ b/pkg/services/realms.go @@ -1,6 +1,7 @@ package services import ( + "fmt" "git.solsynth.dev/hydrogen/interactive/pkg/database" "git.solsynth.dev/hydrogen/interactive/pkg/models" "github.com/samber/lo" @@ -76,13 +77,16 @@ func ListRealmMember(realmId uint) ([]models.RealmMember, error) { } func InviteRealmMember(user models.Account, target models.Realm) error { + if _, err := GetAccountFriend(user.ID, target.AccountID, 1); err != nil { + return fmt.Errorf("you only can invite your friends to your realm") + } + member := models.RealmMember{ RealmID: target.ID, AccountID: user.ID, } err := database.C.Save(&member).Error - return err }