mirror of
https://github.com/PretendoNetwork/friends.git
synced 2026-03-21 18:04:11 -05:00
Move from Cassandra to Postgres
This commit is contained in:
parent
a3f9757608
commit
ca54e576d5
|
|
@ -2,7 +2,6 @@ package database
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
|
@ -16,31 +15,26 @@ func AcceptFriendshipAndReturnFriendInfo(friendRequestID uint64) *nexproto.Frien
|
|||
var senderPID uint32
|
||||
var recipientPID uint32
|
||||
|
||||
if err := cassandraClusterSession.Query(`SELECT sender_pid, recipient_pid FROM pretendo_friends.friend_requests WHERE id=?`, friendRequestID).Scan(&senderPID, &recipientPID); err != nil {
|
||||
err := postgres.QueryRow(`SELECT sender_pid, recipient_pid FROM wiiu.friend_requests WHERE id=$1`, friendRequestID).Scan(&senderPID, &recipientPID)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
nodeID := rand.Intn(len(globals.SnowflakeNodes))
|
||||
|
||||
snowflakeNode := globals.SnowflakeNodes[nodeID]
|
||||
|
||||
friendshipID1 := uint64(snowflakeNode.Generate().Int64())
|
||||
friendshipID2 := uint64(snowflakeNode.Generate().Int64())
|
||||
|
||||
acceptedTime := nex.NewDateTime(0)
|
||||
acceptedTime.FromTimestamp(time.Now())
|
||||
|
||||
// Friendships are two-way relationships, not just one link between 2 entities
|
||||
// "A" has friend "B" and "B" has friend "A", so store both relationships
|
||||
|
||||
if err := cassandraClusterSession.Query(`INSERT INTO pretendo_friends.friendships (id, user1_pid, user2_pid, date) VALUES (?, ?, ?, ?) IF NOT EXISTS`, friendshipID1, senderPID, recipientPID, acceptedTime.Value()).Exec(); err != nil {
|
||||
_, err = postgres.Exec(`INSERT INTO wiiu.friendships (user1_pid, user2_pid, date) VALUES ($1, $2, $3)`, senderPID, recipientPID, acceptedTime.Value())
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`INSERT INTO pretendo_friends.friendships (id, user1_pid, user2_pid, date) VALUES (?, ?, ?, ?) IF NOT EXISTS`, friendshipID2, recipientPID, senderPID, acceptedTime.Value()).Exec(); err != nil {
|
||||
_, err = postgres.Exec(`INSERT INTO wiiu.friendships (user1_pid, user2_pid, date) VALUES ($1, $2, $3)`, recipientPID, senderPID, acceptedTime.Value())
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
|
@ -98,12 +92,14 @@ func AcceptFriendshipAndReturnFriendInfo(friendRequestID uint64) *nexproto.Frien
|
|||
friendInfo.Presence.Unknown7 = 0
|
||||
|
||||
var lastOnlineTime uint64
|
||||
if err := cassandraClusterSession.Query(`SELECT time FROM pretendo_friends.last_online WHERE pid=?`, senderPID).Scan(&lastOnlineTime); err != nil {
|
||||
err := postgres.QueryRow(`SELECT last_online FROM wiiu.user_data WHERE pid=$1`, senderPID).Scan(&lastOnlineTime)
|
||||
if err != nil {
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
|
||||
if err == gocql.ErrNotFound {
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
globals.Logger.Error(err.Error())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,124 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/gocql/gocql"
|
||||
)
|
||||
|
||||
var cluster *gocql.ClusterConfig
|
||||
var cassandraClusterSession *gocql.Session
|
||||
|
||||
func connectCassandra() {
|
||||
// Connect to Cassandra
|
||||
|
||||
var err error
|
||||
|
||||
cluster = gocql.NewCluster("127.0.0.1")
|
||||
cluster.Timeout = 30 * time.Second
|
||||
|
||||
createKeyspace("pretendo_friends")
|
||||
|
||||
cluster.Keyspace = "pretendo_friends"
|
||||
|
||||
cassandraClusterSession, err = cluster.CreateSession()
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Create tables if missing
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.preferences (
|
||||
pid int PRIMARY KEY,
|
||||
show_online boolean,
|
||||
show_current_game boolean,
|
||||
block_friend_requests boolean
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.blocks (
|
||||
id text PRIMARY KEY,
|
||||
blocker_pid int,
|
||||
blocked_pid int,
|
||||
date bigint
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.friend_requests (
|
||||
id bigint PRIMARY KEY,
|
||||
sender_pid int,
|
||||
recipient_pid int,
|
||||
sent_on bigint,
|
||||
expires_on bigint,
|
||||
message text,
|
||||
received boolean,
|
||||
accepted boolean,
|
||||
denied boolean
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.friendships (
|
||||
id bigint PRIMARY KEY,
|
||||
user1_pid int,
|
||||
user2_pid int,
|
||||
date bigint
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.comments (
|
||||
pid int PRIMARY KEY,
|
||||
message text,
|
||||
changed bigint
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cassandraClusterSession.Query(`CREATE TABLE IF NOT EXISTS pretendo_friends.last_online (
|
||||
pid int PRIMARY KEY,
|
||||
time bigint
|
||||
)`).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
globals.Logger.Success("Connected to db")
|
||||
}
|
||||
|
||||
// Adapted from gocql common_test.go
|
||||
func createKeyspace(keyspace string) {
|
||||
flagRF := flag.Int("rf", 1, "replication factor for pretendo_friends keyspace")
|
||||
|
||||
c := *cluster
|
||||
c.Keyspace = "system"
|
||||
c.Timeout = 30 * time.Second
|
||||
|
||||
s, err := c.CreateSession()
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
defer s.Close()
|
||||
|
||||
if err := s.Query(fmt.Sprintf(`CREATE KEYSPACE IF NOT EXISTS %s
|
||||
WITH replication = {
|
||||
'class' : 'SimpleStrategy',
|
||||
'replication_factor' : %d
|
||||
}`, keyspace, *flagRF)).Exec(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
24
database/connect_postgres.go
Normal file
24
database/connect_postgres.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
)
|
||||
|
||||
var postgres *sql.DB
|
||||
|
||||
func connectPostgres() {
|
||||
var err error
|
||||
|
||||
postgres, err = sql.Open("postgres", os.Getenv("DATABASE_URI"))
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
initPostgresWiiU()
|
||||
// TODO: 3DS database
|
||||
}
|
||||
|
|
@ -2,5 +2,5 @@ package database
|
|||
|
||||
func Connect() {
|
||||
connectMongo()
|
||||
connectCassandra()
|
||||
connectPostgres()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
"github.com/gocql/gocql"
|
||||
)
|
||||
|
||||
// Get a users comment
|
||||
|
|
@ -14,12 +15,11 @@ func GetUserComment(pid uint32) *nexproto.Comment {
|
|||
|
||||
var changed uint64 = 0
|
||||
|
||||
if err := cassandraClusterSession.Query(`SELECT message, changed FROM pretendo_friends.comments WHERE pid=?`,
|
||||
pid).Consistency(gocql.One).Scan(&comment.Contents, &changed); err != nil {
|
||||
if err == gocql.ErrNotFound {
|
||||
comment.Contents = ""
|
||||
err := postgres.QueryRow(`SELECT comment, comment_changed FROM wiiu.user_data WHERE pid=$1`, pid).Scan(&comment.Contents, &changed)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
globals.Logger.Warning(err.Error())
|
||||
} else {
|
||||
comment.Contents = ""
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,19 +14,18 @@ import (
|
|||
|
||||
// Get a users friend list
|
||||
func GetUserFriendList(pid uint32) []*nexproto.FriendInfo {
|
||||
var sliceMap []map[string]interface{}
|
||||
var err error
|
||||
|
||||
if sliceMap, err = cassandraClusterSession.Query(`SELECT user2_pid, date FROM pretendo_friends.friendships WHERE user1_pid=? ALLOW FILTERING`, pid).Iter().SliceMap(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
|
||||
return make([]*nexproto.FriendInfo, 0)
|
||||
}
|
||||
|
||||
friendList := make([]*nexproto.FriendInfo, 0)
|
||||
|
||||
for i := 0; i < len(sliceMap); i++ {
|
||||
friendPID := uint32(sliceMap[i]["user2_pid"].(int))
|
||||
rows, err := postgres.Query(`SELECT user2_pid, date FROM wiiu.friendships WHERE user1_pid=$1`, pid)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return friendList
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var friendPID uint32
|
||||
var date uint64
|
||||
rows.Scan(&friendPID, &date)
|
||||
|
||||
friendInfo := nexproto.NewFriendInfo()
|
||||
connectedUser := globals.ConnectedUsers[friendPID]
|
||||
|
|
@ -91,12 +90,14 @@ func GetUserFriendList(pid uint32) []*nexproto.FriendInfo {
|
|||
friendInfo.Presence.Unknown7 = 0
|
||||
|
||||
var lastOnlineTime uint64
|
||||
if err := cassandraClusterSession.Query(`SELECT time FROM pretendo_friends.last_online WHERE pid=?`, friendPID).Scan(&lastOnlineTime); err != nil {
|
||||
err := postgres.QueryRow(`SELECT last_online FROM wiiu.user_data WHERE pid=$1`, friendPID).Scan(&lastOnlineTime)
|
||||
if err != nil {
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
|
||||
if err == gocql.ErrNotFound {
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
globals.Logger.Error(err.Error())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
lastOnlineTime = nex.NewDateTime(0).Now()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +105,7 @@ func GetUserFriendList(pid uint32) []*nexproto.FriendInfo {
|
|||
}
|
||||
|
||||
friendInfo.Status = GetUserComment(friendPID)
|
||||
friendInfo.BecameFriend = nex.NewDateTime(uint64(sliceMap[i]["date"].(int64)))
|
||||
friendInfo.BecameFriend = nex.NewDateTime(date)
|
||||
friendInfo.LastOnline = lastOnline
|
||||
friendInfo.Unknown = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -11,18 +11,22 @@ import (
|
|||
|
||||
// Get a users received friend requests
|
||||
func GetUserFriendRequestsIn(pid uint32) []*nexproto.FriendRequest {
|
||||
var sliceMap []map[string]interface{}
|
||||
var err error
|
||||
|
||||
if sliceMap, err = cassandraClusterSession.Query(`SELECT id, sender_pid, sent_on, expires_on, message, received FROM pretendo_friends.friend_requests WHERE recipient_pid=? AND accepted=false AND denied=false ALLOW FILTERING`, pid).Iter().SliceMap(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return make([]*nexproto.FriendRequest, 0)
|
||||
}
|
||||
|
||||
friendRequestsIn := make([]*nexproto.FriendRequest, 0)
|
||||
|
||||
for i := 0; i < len(sliceMap); i++ {
|
||||
senderPID := uint32(sliceMap[i]["sender_pid"].(int))
|
||||
rows, err := postgres.Query(`SELECT id, sender_pid, sent_on, expires_on, message, received FROM wiiu.friend_requests WHERE recipient_pid=$1 AND accepted=false AND denied=false`, pid)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return friendRequestsIn
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var id uint64
|
||||
var senderPID uint32
|
||||
var sentOn uint64
|
||||
var expiresOn uint64
|
||||
var message string
|
||||
var received bool
|
||||
rows.Scan(&id, &senderPID, &sentOn, &expiresOn, &message, &received)
|
||||
|
||||
senderUserInforation := GetUserInfoByPID(senderPID)
|
||||
encodedMiiData := senderUserInforation["mii"].(bson.M)["data"].(string)
|
||||
|
|
@ -42,18 +46,18 @@ func GetUserFriendRequestsIn(pid uint32) []*nexproto.FriendRequest {
|
|||
friendRequest.PrincipalInfo.Unknown = 2 // replaying from real server
|
||||
|
||||
friendRequest.Message = nexproto.NewFriendRequestMessage()
|
||||
friendRequest.Message.FriendRequestID = uint64(sliceMap[i]["id"].(int64))
|
||||
friendRequest.Message.Received = sliceMap[i]["received"].(bool)
|
||||
friendRequest.Message.FriendRequestID = id
|
||||
friendRequest.Message.Received = received
|
||||
friendRequest.Message.Unknown2 = 1
|
||||
friendRequest.Message.Message = sliceMap[i]["message"].(string)
|
||||
friendRequest.Message.Message = message
|
||||
friendRequest.Message.Unknown3 = 0
|
||||
friendRequest.Message.Unknown4 = ""
|
||||
friendRequest.Message.GameKey = nexproto.NewGameKey()
|
||||
friendRequest.Message.GameKey.TitleID = 0
|
||||
friendRequest.Message.GameKey.TitleVersion = 0
|
||||
friendRequest.Message.Unknown5 = nex.NewDateTime(134222053376) // idk what this value means but its always this
|
||||
friendRequest.Message.ExpiresOn = nex.NewDateTime(uint64(sliceMap[i]["expires_on"].(int64)))
|
||||
friendRequest.SentOn = nex.NewDateTime(uint64(sliceMap[i]["sent_on"].(int64)))
|
||||
friendRequest.Message.ExpiresOn = nex.NewDateTime(expiresOn)
|
||||
friendRequest.SentOn = nex.NewDateTime(sentOn)
|
||||
|
||||
friendRequestsIn = append(friendRequestsIn, friendRequest)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,19 +11,22 @@ import (
|
|||
|
||||
// Get a users sent friend requests
|
||||
func GetUserFriendRequestsOut(pid uint32) []*nexproto.FriendRequest {
|
||||
var sliceMap []map[string]interface{}
|
||||
var err error
|
||||
|
||||
if sliceMap, err = cassandraClusterSession.Query(`SELECT id, recipient_pid, sent_on, expires_on, message, received FROM pretendo_friends.friend_requests WHERE sender_pid=? AND accepted=false AND denied=false ALLOW FILTERING`, pid).Iter().SliceMap(); err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
|
||||
return make([]*nexproto.FriendRequest, 0)
|
||||
}
|
||||
|
||||
friendRequestsOut := make([]*nexproto.FriendRequest, 0)
|
||||
|
||||
for i := 0; i < len(sliceMap); i++ {
|
||||
recipientPID := uint32(sliceMap[i]["recipient_pid"].(int))
|
||||
rows, err := postgres.Query(`SELECT id, recipient_pid, sent_on, expires_on, message, received FROM wiiu.friend_requests WHERE sender_pid=$1 AND accepted=false AND denied=false`, pid)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return friendRequestsOut
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var id uint64
|
||||
var recipientPID uint32
|
||||
var sentOn uint64
|
||||
var expiresOn uint64
|
||||
var message string
|
||||
var received bool
|
||||
rows.Scan(&id, &recipientPID, &sentOn, &expiresOn, &message, &received)
|
||||
|
||||
recipientUserInforation := GetUserInfoByPID(recipientPID)
|
||||
encodedMiiData := recipientUserInforation["mii"].(bson.M)["data"].(string)
|
||||
|
|
@ -43,18 +46,18 @@ func GetUserFriendRequestsOut(pid uint32) []*nexproto.FriendRequest {
|
|||
friendRequest.PrincipalInfo.Unknown = 2 // replaying from real server
|
||||
|
||||
friendRequest.Message = nexproto.NewFriendRequestMessage()
|
||||
friendRequest.Message.FriendRequestID = uint64(sliceMap[i]["id"].(int64))
|
||||
friendRequest.Message.Received = sliceMap[i]["received"].(bool)
|
||||
friendRequest.Message.FriendRequestID = id
|
||||
friendRequest.Message.Received = received
|
||||
friendRequest.Message.Unknown2 = 1
|
||||
friendRequest.Message.Message = sliceMap[i]["message"].(string)
|
||||
friendRequest.Message.Message = message
|
||||
friendRequest.Message.Unknown3 = 0
|
||||
friendRequest.Message.Unknown4 = ""
|
||||
friendRequest.Message.GameKey = nexproto.NewGameKey()
|
||||
friendRequest.Message.GameKey.TitleID = 0
|
||||
friendRequest.Message.GameKey.TitleVersion = 0
|
||||
friendRequest.Message.Unknown5 = nex.NewDateTime(134222053376) // idk what this value means but its always this
|
||||
friendRequest.Message.ExpiresOn = nex.NewDateTime(uint64(sliceMap[i]["expires_on"].(int64)))
|
||||
friendRequest.SentOn = nex.NewDateTime(uint64(sliceMap[i]["sent_on"].(int64)))
|
||||
friendRequest.Message.ExpiresOn = nex.NewDateTime(expiresOn)
|
||||
friendRequest.SentOn = nex.NewDateTime(sentOn)
|
||||
|
||||
friendRequestsOut = append(friendRequestsOut, friendRequest)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
package database
|
||||
|
||||
import nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
func GetUserPrincipalPreference(pid uint32) *nexproto.PrincipalPreference {
|
||||
preference := nexproto.NewPrincipalPreference()
|
||||
|
||||
_ = cassandraClusterSession.Query(`SELECT show_online, show_current_game, block_friend_requests FROM pretendo_friends.preferences WHERE pid=?`, pid).Scan(&preference.ShowOnlinePresence, &preference.ShowCurrentTitle, &preference.BlockFriendRequests)
|
||||
err := postgres.QueryRow(`SELECT show_online, show_current_game, block_friend_requests FROM wiiu.user_data WHERE pid=$1`, pid).Scan(&preference.ShowOnlinePresence, &preference.ShowCurrentTitle, &preference.BlockFriendRequests)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
globals.Logger.Warning(err.Error())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return preference
|
||||
}
|
||||
|
|
|
|||
65
database/init_postgres_wiiu.go
Normal file
65
database/init_postgres_wiiu.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package database
|
||||
|
||||
import "github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
||||
func initPostgresWiiU() {
|
||||
var err error
|
||||
|
||||
_, err = postgres.Exec(`CREATE SCHEMA IF NOT EXISTS wiiu`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = postgres.Exec(`CREATE TABLE IF NOT EXISTS wiiu.user_data (
|
||||
pid integer PRIMARY KEY,
|
||||
show_online boolean,
|
||||
show_current_game boolean,
|
||||
block_friend_requests boolean,
|
||||
comment text,
|
||||
comment_changed bigint,
|
||||
last_online bigint
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = postgres.Exec(`CREATE TABLE IF NOT EXISTS wiiu.friendships (
|
||||
id bigserial PRIMARY KEY,
|
||||
user1_pid integer,
|
||||
user2_pid integer,
|
||||
date bigint
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = postgres.Exec(`CREATE TABLE IF NOT EXISTS wiiu.blocks (
|
||||
id bigserial PRIMARY KEY,
|
||||
blocker_pid integer,
|
||||
blocked_pid integer,
|
||||
date bigint
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = postgres.Exec(`CREATE TABLE IF NOT EXISTS wiiu.friend_requests (
|
||||
id bigserial PRIMARY KEY,
|
||||
sender_pid integer,
|
||||
recipient_pid integer,
|
||||
sent_on bigint,
|
||||
expires_on bigint,
|
||||
message text,
|
||||
received boolean,
|
||||
accepted boolean,
|
||||
denied boolean
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,15 @@
|
|||
package database
|
||||
|
||||
import "github.com/gocql/gocql"
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
)
|
||||
|
||||
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
|
||||
var found bool
|
||||
err := postgres.QueryRow(`SELECT COUNT(*) FROM wiiu.blocks WHERE blocker_pid=$1 AND blocked_pid=$2 LIMIT 1`, requesterPID, requestedPID).Scan(&found)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
// Assume a block record was found
|
||||
return true
|
||||
return found
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,16 @@ package database
|
|||
|
||||
import "github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
||||
func SaveFriendRequest(friendRequestID uint64, senderPID uint32, recipientPID uint32, sentTime uint64, expireTime uint64, message string) {
|
||||
if err := cassandraClusterSession.Query(`INSERT INTO pretendo_friends.friend_requests (id, sender_pid, recipient_pid, sent_on, expires_on, message, received, accepted, denied) VALUES (?, ?, ?, ?, ?, ?, false, false, false) IF NOT EXISTS`, friendRequestID, senderPID, recipientPID, sentTime, expireTime, message).Exec(); err != nil {
|
||||
func SaveFriendRequest(senderPID uint32, recipientPID uint32, sentTime uint64, expireTime uint64, message string) uint64 {
|
||||
var id uint64
|
||||
err := postgres.QueryRow(`
|
||||
INSERT INTO wiiu.friend_requests (sender_pid, recipient_pid, sent_on, expires_on, message, received, accepted, denied)
|
||||
VALUES ($1, $2, $3, $4, $5, false, false, false) RETURNING id`, senderPID, recipientPID, sentTime, expireTime, message).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return 0
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package database
|
|||
import "github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
||||
func SetFriendRequestAccepted(friendRequestID uint64) {
|
||||
if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.friend_requests SET accepted=true WHERE id=?`, friendRequestID).Exec(); err != nil {
|
||||
_, err := postgres.Exec(`UPDATE wiiu.friend_requests SET accepted=true WHERE id=$1`, friendRequestID)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package database
|
|||
import "github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
||||
func SetFriendRequestReceived(friendRequestID uint64) {
|
||||
if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.friend_requests SET received=true WHERE id=?`, friendRequestID).Exec(); err != nil {
|
||||
_, err := postgres.Exec(`UPDATE wiiu.friend_requests SET received=true WHERE id=$1`, friendRequestID)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,15 @@ import (
|
|||
func UpdateUserComment(pid uint32, message string) uint64 {
|
||||
changed := nex.NewDateTime(0).Now()
|
||||
|
||||
if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.comments SET message=?, changed=? WHERE pid=?`, message, changed, pid).Exec(); err != nil {
|
||||
_, err := postgres.Exec(`
|
||||
INSERT INTO wiiu.user_data (pid, comment, comment_changed)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
comment = $2,
|
||||
comment_changed = $3`, pid, message, changed)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,14 @@ import (
|
|||
)
|
||||
|
||||
func UpdateUserLastOnlineTime(pid uint32, lastOnline *nex.DateTime) {
|
||||
if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.last_online SET time=? WHERE pid=?`, lastOnline.Value(), pid).Exec(); err != nil {
|
||||
_, err := postgres.Exec(`
|
||||
INSERT INTO wiiu.user_data (pid, last_online)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
last_online = $2`, pid, lastOnline.Value())
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,16 @@ import (
|
|||
)
|
||||
|
||||
func UpdateUserPrincipalPreference(pid uint32, principalPreference *nexproto.PrincipalPreference) {
|
||||
if err := cassandraClusterSession.Query(`UPDATE pretendo_friends.preferences SET
|
||||
show_online=?,
|
||||
show_current_game=?,
|
||||
block_friend_requests=?
|
||||
WHERE pid=?`, principalPreference.ShowOnlinePresence, principalPreference.ShowCurrentTitle, principalPreference.BlockFriendRequests, pid).Exec(); err != nil {
|
||||
_, err := postgres.Exec(`
|
||||
INSERT INTO wiiu.user_data (pid, show_online, show_current_game, block_friend_requests)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
show_online = $2,
|
||||
show_current_game = $3,
|
||||
block_friend_requests = $4`, pid, principalPreference.ShowOnlinePresence, principalPreference.ShowCurrentTitle, principalPreference.BlockFriendRequests)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
package globals
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/bwmarrin/snowflake"
|
||||
)
|
||||
|
||||
func CreateSnowflakeNodes() {
|
||||
SnowflakeNodes = make([]*snowflake.Node, 0)
|
||||
|
||||
for corenum := 0; corenum < runtime.NumCPU(); corenum++ {
|
||||
node, err := snowflake.NewNode(int64(corenum))
|
||||
if err != nil {
|
||||
// TODO: Handle error
|
||||
Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
SnowflakeNodes = append(SnowflakeNodes, node)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,10 +3,8 @@ package globals
|
|||
import (
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
"github.com/PretendoNetwork/plogger-go"
|
||||
"github.com/bwmarrin/snowflake"
|
||||
)
|
||||
|
||||
var Logger = plogger.NewLogger()
|
||||
var NEXServer *nex.Server
|
||||
var ConnectedUsers map[uint32]*ConnectedUser
|
||||
var SnowflakeNodes []*snowflake.Node
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -7,10 +7,10 @@ require (
|
|||
github.com/PretendoNetwork/nex-go v1.0.9
|
||||
github.com/PretendoNetwork/nex-protocols-go v1.0.11
|
||||
github.com/PretendoNetwork/plogger-go v1.0.2
|
||||
github.com/bwmarrin/snowflake v0.3.0
|
||||
github.com/gocql/gocql v1.2.1
|
||||
github.com/golang/protobuf v1.5.2
|
||||
github.com/joho/godotenv v1.4.0
|
||||
github.com/lib/pq v1.10.7
|
||||
go.mongodb.org/mongo-driver v1.10.2
|
||||
google.golang.org/grpc v1.49.0
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -10,8 +10,6 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYE
|
|||
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
|
||||
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
||||
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
|
@ -43,6 +41,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
|||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
|
|
|
|||
1
init.go
1
init.go
|
|
@ -61,5 +61,4 @@ func init() {
|
|||
}
|
||||
|
||||
database.Connect()
|
||||
globals.CreateSnowflakeNodes()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package friends_wiiu
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
|
|
@ -13,24 +12,20 @@ import (
|
|||
)
|
||||
|
||||
func AddFriendRequest(err error, client *nex.Client, callID uint32, pid uint32, unknown2 uint8, message string, unknown4 uint8, unknown5 string, gameKey *nexproto.GameKey, unknown6 *nex.DateTime) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
nodeID := rand.Intn(len(globals.SnowflakeNodes))
|
||||
|
||||
snowflakeNode := globals.SnowflakeNodes[nodeID]
|
||||
|
||||
friendRequestID := uint64(snowflakeNode.Generate().Int64())
|
||||
|
||||
currentTimestamp := time.Now()
|
||||
expireTimestamp := currentTimestamp.Add(time.Hour * 24 * 29)
|
||||
|
||||
sentTime := nex.NewDateTime(0)
|
||||
expireTime := nex.NewDateTime(0)
|
||||
|
||||
sentTime.FromTimestamp(currentTimestamp)
|
||||
expireTime.FromTimestamp(expireTimestamp)
|
||||
|
||||
senderPID := client.PID()
|
||||
recipientPID := pid
|
||||
|
||||
friendRequestID := database.SaveFriendRequest(senderPID, recipientPID, sentTime.Value(), expireTime.Value(), message)
|
||||
|
||||
recipientUserInforation := database.GetUserInfoByPID(recipientPID)
|
||||
|
||||
friendRequest := nexproto.NewFriendRequest()
|
||||
|
|
@ -105,8 +100,6 @@ func AddFriendRequest(err error, client *nex.Client, callID uint32, pid uint32,
|
|||
friendInfo.LastOnline = nex.NewDateTime(0)
|
||||
friendInfo.Unknown = 0
|
||||
|
||||
database.SaveFriendRequest(friendRequestID, senderPID, recipientPID, sentTime.Value(), expireTime.Value(), message)
|
||||
|
||||
recipientClient := client.Server().FindClientFromPID(recipientPID)
|
||||
|
||||
if recipientClient != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user