diff --git a/database/wiiu/accept_friend_request_and_return_friend_info.go b/database/wiiu/accept_friend_request_and_return_friend_info.go index 89649bc..16fff76 100644 --- a/database/wiiu/accept_friend_request_and_return_friend_info.go +++ b/database/wiiu/accept_friend_request_and_return_friend_info.go @@ -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 diff --git a/database/wiiu/get_user_block_list.go b/database/wiiu/get_user_block_list.go index b81457c..6fb6471 100644 --- a/database/wiiu/get_user_block_list.go +++ b/database/wiiu/get_user_block_list.go @@ -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 diff --git a/database/wiiu/get_user_friend_list.go b/database/wiiu/get_user_friend_list.go index cd71a6d..c7cee56 100644 --- a/database/wiiu/get_user_friend_list.go +++ b/database/wiiu/get_user_friend_list.go @@ -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 diff --git a/database/wiiu/get_user_friend_requests_in.go b/database/wiiu/get_user_friend_requests_in.go index 7f3987a..c7f67e7 100644 --- a/database/wiiu/get_user_friend_requests_in.go +++ b/database/wiiu/get_user_friend_requests_in.go @@ -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 diff --git a/database/wiiu/get_user_friend_requests_out.go b/database/wiiu/get_user_friend_requests_out.go index b494054..ab81d32 100644 --- a/database/wiiu/get_user_friend_requests_out.go +++ b/database/wiiu/get_user_friend_requests_out.go @@ -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 diff --git a/nex/friends-wiiu/add_blacklist.go b/nex/friends-wiiu/add_blacklist.go index ff03017..d09e9fe 100644 --- a/nex/friends-wiiu/add_blacklist.go +++ b/nex/friends-wiiu/add_blacklist.go @@ -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 diff --git a/nex/friends-wiiu/add_friend_request.go b/nex/friends-wiiu/add_friend_request.go index 6a19742..8c30c44 100644 --- a/nex/friends-wiiu/add_friend_request.go +++ b/nex/friends-wiiu/add_friend_request.go @@ -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) diff --git a/nex/friends-wiiu/deny_friend_request.go b/nex/friends-wiiu/deny_friend_request.go index 8a0d5f7..974b491 100644 --- a/nex/friends-wiiu/deny_friend_request.go +++ b/nex/friends-wiiu/deny_friend_request.go @@ -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 diff --git a/nex/friends-wiiu/get_basic_info.go b/nex/friends-wiiu/get_basic_info.go index 021d690..98f0810 100644 --- a/nex/friends-wiiu/get_basic_info.go +++ b/nex/friends-wiiu/get_basic_info.go @@ -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 diff --git a/database/wiiu/get_user_info_by_pnid_data.go b/utility/get_user_info_by_pid.go similarity index 56% rename from database/wiiu/get_user_info_by_pnid_data.go rename to utility/get_user_info_by_pid.go index d6c26c4..2886a4c 100644 --- a/database/wiiu/get_user_info_by_pnid_data.go +++ b/utility/get_user_info_by_pid.go @@ -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