Move GetUserDataByPID to utility

This commit is contained in:
Daniel López Guimaraes 2023-08-14 13:17:48 +01:00
parent 36b0b7f3fb
commit 84566dd6a8
No known key found for this signature in database
GPG key ID: 6AC74DE3DEF050E0
10 changed files with 40 additions and 81 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
)
@ -72,16 +73,12 @@ func AcceptFriendRequestAndReturnFriendInfo(friendRequestID uint64) (*friends_wi
lastOnline.FromTimestamp(time.Now())
} else {
// Offline
userData, err := globals.GetUserData(senderPID)
userInfo, err := utility.GetUserInfoByPID(senderPID)
if err != nil {
return nil, err
}
friendInfo.NNAInfo = friends_wiiu_types.NewNNAInfo()
userInfo, err := GetUserInfoByPNIDData(userData)
if err != nil {
return nil, err
}
friendInfo.NNAInfo.PrincipalBasicInfo = userInfo
friendInfo.NNAInfo.Unknown1 = 0

View file

@ -5,6 +5,7 @@ import (
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
)
@ -29,13 +30,7 @@ func GetUserBlockList(pid uint32) ([]*friends_wiiu_types.BlacklistedPrincipal, e
var date *nex.DateTime
rows.Scan(&pid, &titleId, &titleVersion, &date)
userData, err := globals.GetUserData(pid)
if err != nil {
globals.Logger.Critical(err.Error())
continue
}
userInfo, err := GetUserInfoByPNIDData(userData)
userInfo, err := utility.GetUserInfoByPID(pid)
if err != nil {
globals.Logger.Critical(err.Error())
continue

View file

@ -7,6 +7,7 @@ import (
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
)
@ -55,13 +56,7 @@ func GetUserFriendList(pid uint32) ([]*friends_wiiu_types.FriendInfo, error) {
} else {
// Offline
userData, err := globals.GetUserData(friendPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue
}
userInfo, err := GetUserInfoByPNIDData(userData)
userInfo, err := utility.GetUserInfoByPID(friendPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue

View file

@ -6,6 +6,7 @@ import (
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
)
@ -32,13 +33,7 @@ func GetUserFriendRequestsIn(pid uint32) ([]*friends_wiiu_types.FriendRequest, e
var received bool
rows.Scan(&id, &senderPID, &sentOn, &expiresOn, &message, &received)
userData, err := globals.GetUserData(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue
}
userInfo, err := GetUserInfoByPNIDData(userData)
userInfo, err := utility.GetUserInfoByPID(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue

View file

@ -5,6 +5,7 @@ import (
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
)
@ -31,13 +32,7 @@ func GetUserFriendRequestsOut(pid uint32) ([]*friends_wiiu_types.FriendRequest,
var received bool
rows.Scan(&id, &recipientPID, &sentOn, &expiresOn, &message, &received)
userData, err := globals.GetUserData(recipientPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue
}
userInfo, err := GetUserInfoByPNIDData(userData)
userInfo, err := utility.GetUserInfoByPID(recipientPID)
if err != nil {
globals.Logger.Critical(err.Error())
continue

View file

@ -3,11 +3,10 @@ package nex_friends_wiiu
import (
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/PretendoNetwork/friends/database"
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
@ -28,9 +27,9 @@ func AddBlacklist(err error, client *nex.Client, callID uint32, blacklistPrincip
date := nex.NewDateTime(0)
date.FromTimestamp(time.Now())
userData, err := globals.GetUserData(currentBlacklistPrincipal.PrincipalBasicInfo.PID)
userInfo, err := utility.GetUserInfoByPID(currentBlacklistPrincipal.PrincipalBasicInfo.PID)
if err != nil {
if status.Code(err) == codes.InvalidArgument {
if err == database.ErrPIDNotFound {
return nex.Errors.FPD.InvalidPrincipalID // TODO: Not sure if this is the correct error.
} else {
globals.Logger.Critical(err.Error())
@ -38,12 +37,6 @@ func AddBlacklist(err error, client *nex.Client, callID uint32, blacklistPrincip
}
}
userInfo, err := database_wiiu.GetUserInfoByPNIDData(userData)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
currentBlacklistPrincipal.PrincipalBasicInfo = userInfo
currentBlacklistPrincipal.BlackListedSince = date

View file

@ -3,12 +3,11 @@ package nex_friends_wiiu
import (
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/PretendoNetwork/friends/database"
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
"github.com/PretendoNetwork/friends/globals"
notifications_wiiu "github.com/PretendoNetwork/friends/notifications/wiiu"
"github.com/PretendoNetwork/friends/utility"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
@ -23,21 +22,15 @@ func AddFriendRequest(err error, client *nex.Client, callID uint32, pid uint32,
senderPID := client.PID()
recipientPID := pid
senderUserData, err := globals.GetUserData(senderPID)
senderPrincipalInfo, err := utility.GetUserInfoByPID(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
senderPrincipalInfo, err := database_wiiu.GetUserInfoByPNIDData(senderUserData)
recipientPrincipalInfo, err := utility.GetUserInfoByPID(recipientPID)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
recipientUserData, err := globals.GetUserData(recipientPID)
if err != nil {
if status.Code(err) == codes.InvalidArgument {
if err == database.ErrPIDNotFound {
globals.Logger.Errorf("User %d has sent friend request to invalid PID %d", senderPID, pid)
return nex.Errors.FPD.InvalidPrincipalID // TODO: Not sure if this is the correct error.
} else {
@ -46,12 +39,6 @@ func AddFriendRequest(err error, client *nex.Client, callID uint32, pid uint32,
}
}
recipientPrincipalInfo, err := database_wiiu.GetUserInfoByPNIDData(recipientUserData)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
currentTimestamp := time.Now()
expireTimestamp := currentTimestamp.Add(time.Hour * 24 * 29)

View file

@ -6,6 +6,7 @@ import (
"github.com/PretendoNetwork/friends/database"
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
@ -39,13 +40,7 @@ func DenyFriendRequest(err error, client *nex.Client, callID uint32, id uint64)
return nex.Errors.FPD.Unknown
}
userData, err := globals.GetUserData(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown
}
info, err := database_wiiu.GetUserInfoByPNIDData(userData)
info, err := utility.GetUserInfoByPID(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
return nex.Errors.FPD.Unknown

View file

@ -1,8 +1,8 @@
package nex_friends_wiiu
import (
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/utility"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
@ -19,13 +19,7 @@ func GetBasicInfo(err error, client *nex.Client, callID uint32, pids []uint32) u
for i := 0; i < len(pids); i++ {
pid := pids[i]
userData, err := globals.GetUserData(pid)
if err != nil {
globals.Logger.Critical(err.Error())
continue
}
info, err := database_wiiu.GetUserInfoByPNIDData(userData)
info, err := utility.GetUserInfoByPID(pid)
if err != nil {
globals.Logger.Critical(err.Error())
continue

View file

@ -1,15 +1,28 @@
package database_wiiu
package utility
import (
"encoding/base64"
pb "github.com/PretendoNetwork/grpc-go/account"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
)
// GetUserInfoByPNIDData converts the account's PNID data into user info for friends
func GetUserInfoByPNIDData(userData *pb.GetUserDataResponse) (*friends_wiiu_types.PrincipalBasicInfo, error) {
// GetUserInfoByPID returns the user information for a given PID
func GetUserInfoByPID(pid uint32) (*friends_wiiu_types.PrincipalBasicInfo, error) {
userData, err := globals.GetUserData(pid)
if err != nil {
if status.Code(err) == codes.InvalidArgument {
return nil, database.ErrPIDNotFound
} else {
return nil, err
}
}
info := friends_wiiu_types.NewPrincipalBasicInfo()
info.PID = userData.Pid