♻️ Moved to dealer
This commit is contained in:
@ -2,62 +2,37 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResponse, error) {
|
||||
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
|
||||
}
|
||||
|
||||
func NotifyPosterAccount(user models.Account, title, body string, subtitle *string) error {
|
||||
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{
|
||||
AccountId: uint64(user.ExternalID),
|
||||
RelatedId: uint64(related.ExternalID),
|
||||
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")
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = proto.NewNotifyClient(pc).NotifyUser(ctx, &proto.NotifyRequest{
|
||||
ClientId: viper.GetString("passport.client_id"),
|
||||
ClientSecret: viper.GetString("passport.client_secret"),
|
||||
Type: "interactive.feedback",
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Links: links,
|
||||
RecipientId: uint64(user.ExternalID),
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
||||
UserId: uint64(user.ExternalID),
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.feedback",
|
||||
Title: title,
|
||||
Subtitle: subtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("An error occurred when notify account...")
|
||||
} else {
|
||||
log.Debug().Uint("eid", user.ExternalID).Msg("Notified account.")
|
||||
log.Debug().Uint("uid", user.ExternalID).Msg("Notified account.")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
@ -211,12 +210,11 @@ func ReactArticle(user models.Account, reaction models.Reaction) (bool, models.R
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
articleUrl := fmt.Sprintf("https://%s/articles/%s", viper.GetString("domain"), op.Alias)
|
||||
err := NotifyPosterAccount(
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s reacted your article", user.Nick),
|
||||
"Article got reacted",
|
||||
fmt.Sprintf("%s (%s) reacted your article a %s", user.Nick, user.Name, reaction.Symbol),
|
||||
&proto.NotifyLink{Label: "Related article", Url: articleUrl},
|
||||
lo.ToPtr(fmt.Sprintf("%s reacted your article", user.Nick)),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
|
@ -2,13 +2,14 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func CheckAttachmentByIDExists(id uint, usage string) bool {
|
||||
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Paperclip")
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeFileProvider)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
@ -3,14 +3,14 @@ package services
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func LinkAccount(userinfo *proto.Userinfo) (models.Account, error) {
|
||||
func LinkAccount(userinfo *proto.UserInfo) (models.Account, error) {
|
||||
var account models.Account
|
||||
if userinfo == nil {
|
||||
return account, fmt.Errorf("remote userinfo was not found")
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
@ -266,12 +265,11 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), item.Alias)
|
||||
err := NotifyPosterAccount(
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s replied you", user.Nick),
|
||||
"Post got replied",
|
||||
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
@ -308,12 +306,11 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), op.Alias)
|
||||
err := NotifyPosterAccount(
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s reacted your post", user.Nick),
|
||||
fmt.Sprintf("%s (%s) reacted your post a %s", user.Nick, user.Name, reaction.Symbol),
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
"Post got replied",
|
||||
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
|
||||
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
|
@ -4,10 +4,11 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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/hydrogen/passport/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
@ -15,11 +16,11 @@ import (
|
||||
|
||||
func GetRealmWithExtID(id uint) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmsClient(pc).GetRealm(context.Background(), &proto.RealmLookupRequest{
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Id: lo.ToPtr(uint64(id)),
|
||||
})
|
||||
if err != nil {
|
||||
@ -30,11 +31,11 @@ func GetRealmWithExtID(id uint) (models.Realm, error) {
|
||||
|
||||
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmsClient(pc).GetRealm(context.Background(), &proto.RealmLookupRequest{
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Alias: &alias,
|
||||
})
|
||||
if err != nil {
|
||||
@ -43,16 +44,16 @@ func GetRealmWithAlias(alias string) (models.Realm, error) {
|
||||
return LinkRealm(response)
|
||||
}
|
||||
|
||||
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberResponse, error) {
|
||||
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.DiscoverServiceGRPC("Hydrogen.Passport")
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := proto.NewRealmsClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
||||
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
||||
RealmId: uint64(realm.ExternalID),
|
||||
UserId: lo.ToPtr(uint64(userId)),
|
||||
})
|
||||
@ -63,22 +64,7 @@ func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberResponse, erro
|
||||
}
|
||||
}
|
||||
|
||||
func ListRealmMember(realmId uint) ([]*proto.RealmMemberResponse, error) {
|
||||
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := proto.NewRealmsClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
||||
RealmId: uint64(realmId),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return response.Data, nil
|
||||
}
|
||||
}
|
||||
|
||||
func LinkRealm(info *proto.RealmResponse) (models.Realm, error) {
|
||||
func LinkRealm(info *proto.RealmInfo) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
if info == nil {
|
||||
return realm, fmt.Errorf("remote realm info was not found")
|
||||
|
Reference in New Issue
Block a user