From d9648a873bd40670c2bfa92d70fea3ee2f6584fb Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Sun, 24 Oct 2021 20:34:11 -0400 Subject: [PATCH] Added GetRequestBlockSettings --- database.go | 46 +++++++++++++++++++----------- get_request_block_settings.go | 47 +++++++++++++++++++++++++++++++ main.go | 1 + update_and_get_all_information.go | 4 --- 4 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 get_request_block_settings.go diff --git a/database.go b/database.go index f6119f7..b3acc3d 100644 --- a/database.go +++ b/database.go @@ -22,9 +22,9 @@ func connectCassandra() { cluster = gocql.NewCluster("127.0.0.1") cluster.Timeout = 30 * time.Second - createKeyspace("pretendo_friends_wiiu") + createKeyspace("pretendo_friends") - cluster.Keyspace = "pretendo_friends_wiiu" + cluster.Keyspace = "pretendo_friends" cassandraClusterSession, err = cluster.CreateSession() @@ -34,7 +34,7 @@ func connectCassandra() { // Create tables if missing - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.users ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.users ( pid int PRIMARY KEY, nnid text, changed_flags int, @@ -52,7 +52,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.preferences ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.preferences ( pid int PRIMARY KEY, show_online boolean, show_current_game boolean, @@ -61,7 +61,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.friendships ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.friendships ( id bigint PRIMARY KEY, user1_pid int, user2_pid int, @@ -70,7 +70,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.blocks ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.blocks ( id text PRIMARY KEY, blocker_pid int, blocked_pid int, @@ -79,7 +79,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.miis ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.miis ( pid int PRIMARY KEY, name text, unknown1 tinyint, @@ -90,7 +90,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.friend_requests ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.friend_requests ( id bigint PRIMARY KEY, sender_pid int, recipient_pid int, @@ -100,7 +100,7 @@ func connectCassandra() { log.Fatal(err) } - if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends_wiiu.notifications ( + if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.notifications ( id bigint PRIMARY KEY, sender_pid int, recipient_pid int, @@ -115,7 +115,7 @@ func connectCassandra() { // Adapted from gocql common_test.go func createKeyspace(keyspace string) { - flagRF := flag.Int("rf", 1, "replication factor for pretendo_friends_wiiu keyspace") + flagRF := flag.Int("rf", 1, "replication factor for pretendo_friends keyspace") c := *cluster c.Keyspace = "system" @@ -148,13 +148,13 @@ func updateNNAInfo(nnaInfo *nexproto.NNAInfo) { // Insert users NNID into users table incase missing - if err := cassandraClusterSession.Query(`UPDATE pretendo_friends_wiiu.users SET nnid = ? WHERE pid = ?`, userNNID, userPID).Exec(); err != nil { + if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.users SET nnid = ? WHERE pid = ?`, userNNID, userPID).Exec(); err != nil { log.Fatal(err) } // Update user Mii data - if err := cassandraClusterSession.Query(`UPDATE pretendo_friends_wiiu.miis SET + if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.miis SET data = ?, name = ?, date = ? @@ -174,7 +174,7 @@ func getUserComment(pid uint32) *nexproto.Comment { var content string var changed uint64 - if err := cassandraClusterSession.Query(`SELECT comment_message, comment_changed FROM pretendo_friends_wiiu.users WHERE pid = ? LIMIT 1`, + if err := cassandraClusterSession.Query(`SELECT comment_message, comment_changed FROM pretendo_friends.users WHERE pid = ? LIMIT 1`, pid).Consistency(gocql.One).Scan(&content, &changed); err != nil { comment := nexproto.NewComment() comment.Unknown = 0 @@ -212,7 +212,7 @@ func getUserBlockList(pid uint32) {} func getUserNotifications(pid uint32) {} func updateUserPrincipalPreference(pid uint32, principalPreference *nexproto.PrincipalPreference) { - if err := cassandraClusterSession.Query(`UPDATE pretendo_friends_wiiu.preferences SET show_online=?, show_current_game=?, block_friend_requests=? WHERE pid=?`, principalPreference.Unknown1, principalPreference.Unknown2, principalPreference.Unknown3, pid).Exec(); err != nil { + if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.preferences SET show_online=?, show_current_game=?, block_friend_requests=? WHERE pid=?`, principalPreference.Unknown1, principalPreference.Unknown2, principalPreference.Unknown3, pid).Exec(); err != nil { log.Fatal(err) } } @@ -222,14 +222,26 @@ func getUserPrincipalPreference(pid uint32) *nexproto.PrincipalPreference { var showCurrentGame bool var blockFriendRequests bool - _ = cassandraClusterSession.Query(`SELECT show_online, show_current_game, block_friend_requests FROM pretendo_friends_wiiu.preferences WHERE pid=?`, pid).Scan(&showOnline, &showCurrentGame, &blockFriendRequests) + _ = cassandraClusterSession.Query(`SELECT show_online, show_current_game, block_friend_requests FROM pretendo_friends.preferences WHERE pid=?`, pid).Scan(&showOnline, &showCurrentGame, &blockFriendRequests) preference := nexproto.NewPrincipalPreference() preference.Unknown1 = showOnline preference.Unknown2 = showCurrentGame preference.Unknown3 = blockFriendRequests - fmt.Println(preference) - return preference } + +func isFriendRequestBlocked(requesterPID uint32, requestedPID uint32) bool { + if err := cassandraClusterSession.Query(`SELECT id FROM pretendo_friends.blocks WHERE blocker_pid=? AND blocked_pid=? LIMIT 1 ALLOW FILTERING`, requestedPID, requesterPID).Scan(); err != nil { + if err == gocql.ErrNotFound { + // Assume no block record was found + return false + } + + // TODO: Error handling + } + + // Assume a block record was found + return true +} diff --git a/get_request_block_settings.go b/get_request_block_settings.go new file mode 100644 index 0000000..7f37dac --- /dev/null +++ b/get_request_block_settings.go @@ -0,0 +1,47 @@ +package main + +import ( + nex "github.com/PretendoNetwork/nex-go" + nexproto "github.com/PretendoNetwork/nex-protocols-go" +) + +func getRequestBlockSettings(err error, client *nex.Client, callID uint32, pids []uint32) { + settings := make([]*nexproto.PrincipalRequestBlockSetting, 0) + + // TODO: + // Improve this. Use less database reads + for i := 0; i < len(pids); i++ { + requestedPID := pids[i] + + setting := nexproto.NewPrincipalRequestBlockSetting() + setting.PID = requestedPID + setting.IsBlocked = isFriendRequestBlocked(client.PID(), requestedPID) + + settings = append(settings, setting) + } + + rmcResponseStream := nex.NewStreamOut(nexServer) + + rmcResponseStream.WriteListStructure(settings) + + rmcResponseBody := rmcResponseStream.Bytes() + + // Build response packet + rmcResponse := nex.NewRMCResponse(nexproto.FriendsProtocolID, callID) + rmcResponse.SetSuccess(nexproto.FriendsMethodGetRequestBlockSettings, rmcResponseBody) + + rmcResponseBytes := rmcResponse.Bytes() + + responsePacket, _ := nex.NewPacketV0(client, nil) + + responsePacket.SetVersion(0) + responsePacket.SetSource(0xA1) + responsePacket.SetDestination(0xAF) + responsePacket.SetType(nex.DataPacket) + responsePacket.SetPayload(rmcResponseBytes) + + responsePacket.AddFlag(nex.FlagNeedsAck) + responsePacket.AddFlag(nex.FlagReliable) + + nexServer.Send(responsePacket) +} diff --git a/main.go b/main.go index 62f193c..fb4d72f 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,7 @@ func main() { friendsServer.UpdateAndGetAllInformation(updateAndGetAllInformation) friendsServer.CheckSettingStatus(checkSettingStatus) friendsServer.UpdatePreference(updatePreferenceWiiU) + friendsServer.GetRequestBlockSettings(getRequestBlockSettings) // Friends (3DS) protocol handles friends3DSServer.UpdateProfile(updateProfile) diff --git a/update_and_get_all_information.go b/update_and_get_all_information.go index c97fea9..d3c054a 100644 --- a/update_and_get_all_information.go +++ b/update_and_get_all_information.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - nex "github.com/PretendoNetwork/nex-go" nexproto "github.com/PretendoNetwork/nex-protocols-go" ) @@ -30,8 +28,6 @@ func updateAndGetAllInformation(err error, client *nex.Client, callID uint32, nn //blockList := getUserBlockList(pid) //notifications := getUserNotifications(pid) - fmt.Printf("%+v\n", principalPreference) - rmcResponseStream := nex.NewStreamOut(nexServer) rmcResponseStream.WriteStructure(principalPreference)