mirror of
https://github.com/PretendoNetwork/friends.git
synced 2026-04-26 07:46:54 -05:00
Update database
This commit is contained in:
parent
1d82e5f182
commit
b4ce1e275b
48
database/3ds/get_friend_miis.go
Normal file
48
database/3ds/get_friend_miis.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Get a friend's mii
|
||||
func GetFriendMiis(pids []uint32) []*nexproto.FriendMii {
|
||||
friendMiis := make([]*nexproto.FriendMii, 0)
|
||||
|
||||
rows, err := database.Postgres.Query(`
|
||||
SELECT pid, mii_name, mii_data FROM "3ds".user_data WHERE pid IN ($1)`, database.PIDArrayToString(pids))
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
globals.Logger.Warning(err.Error())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
changedTime := nex.NewDateTime(0)
|
||||
changedTime.FromTimestamp(time.Now())
|
||||
|
||||
for rows.Next() {
|
||||
var pid uint32
|
||||
|
||||
mii := nexproto.NewMii()
|
||||
mii.Unknown2 = false
|
||||
mii.Unknown3 = 0
|
||||
|
||||
rows.Scan(&pid, &mii.Name, &mii.MiiData)
|
||||
|
||||
friendMii := nexproto.NewFriendMii()
|
||||
friendMii.PID = pid
|
||||
friendMii.Mii = mii
|
||||
friendMii.ModifiedAt = changedTime
|
||||
|
||||
friendMiis = append(friendMiis, friendMii)
|
||||
}
|
||||
|
||||
return friendMiis
|
||||
}
|
||||
60
database/3ds/get_friend_persistent_infos.go
Normal file
60
database/3ds/get_friend_persistent_infos.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Get a friend's persistent information
|
||||
func GetFriendPersistentInfos(user1_pid uint32, pids []uint32) []*nexproto.FriendPersistentInfo {
|
||||
persistentInfos := make([]*nexproto.FriendPersistentInfo, 0)
|
||||
|
||||
rows, err := database.Postgres.Query(`
|
||||
SELECT pid, region, area, language, favorite_title, favorite_title_version, comment, comment_changed, last_online FROM "3ds".user_data WHERE pid IN ($1)`, database.PIDArrayToString(pids))
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
globals.Logger.Warning(err.Error())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
persistentInfo := nexproto.NewFriendPersistentInfo()
|
||||
|
||||
gameKey := nexproto.NewGameKey()
|
||||
|
||||
var lastOnlineTime uint64
|
||||
var msgUpdateTime uint64
|
||||
var friendedAtTime uint64
|
||||
|
||||
rows.Scan(
|
||||
&persistentInfo.PID, &persistentInfo.Region, &persistentInfo.Area, &persistentInfo.Language,
|
||||
&gameKey.TitleID, &gameKey.TitleVersion, &persistentInfo.Message, &msgUpdateTime, &lastOnlineTime)
|
||||
|
||||
err = database.Postgres.QueryRow(`
|
||||
SELECT date FROM "3ds".friendships WHERE user1_pid=$1 AND user2_pid=$2 AND type=0 LIMIT 1`, user1_pid, persistentInfo.PID).Scan(&friendedAtTime)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
friendedAtTime = uint64(time.Now().Unix())
|
||||
} else {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
persistentInfo.MessageUpdatedAt = nex.NewDateTime(msgUpdateTime)
|
||||
persistentInfo.FriendedAt = nex.NewDateTime(friendedAtTime)
|
||||
persistentInfo.LastOnline = nex.NewDateTime(lastOnlineTime)
|
||||
persistentInfo.GameKey = gameKey
|
||||
persistentInfo.Platform = 2 // Always 3DS
|
||||
|
||||
persistentInfos = append(persistentInfos, persistentInfo)
|
||||
}
|
||||
|
||||
return persistentInfos
|
||||
}
|
||||
30
database/3ds/get_user_friends.go
Normal file
30
database/3ds/get_user_friends.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Get all of a user's friend relationships
|
||||
func GetUserFriends(pid uint32) []*nexproto.FriendRelationship {
|
||||
friendRelationships := make([]*nexproto.FriendRelationship, 0)
|
||||
|
||||
rows, err := database.Postgres.Query(`
|
||||
SELECT user2_pid, type FROM "3ds".friendships WHERE user1_pid=$1 AND type=1`, pid)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
relationship := nexproto.NewFriendRelationship()
|
||||
relationship.LFC = 0
|
||||
rows.Scan(&relationship.PID, &relationship.RelationshipType)
|
||||
|
||||
friendRelationships = append(friendRelationships, relationship)
|
||||
}
|
||||
|
||||
return friendRelationships
|
||||
}
|
||||
21
database/3ds/remove_friendship.go
Normal file
21
database/3ds/remove_friendship.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
)
|
||||
|
||||
// Remove a user's friend relationship
|
||||
func RemoveFriendship(user1_pid uint32, user2_pid uint32) {
|
||||
_, err := database.Postgres.Exec(`
|
||||
DELETE FROM "3ds".friendships WHERE user1_pid=$1 AND user2_pid=$2`, user1_pid, user2_pid)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
||||
_, err = database.Postgres.Exec(`
|
||||
UPDATE "3ds".friendships SET type=0 WHERE user1_pid=$1 AND user2_pid=$2`, user2_pid, user1_pid)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
80
database/3ds/save_friendship.go
Normal file
80
database/3ds/save_friendship.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Save a friend relationship for a user
|
||||
func SaveFriendship(senderPID uint32, recipientPID uint32) *nexproto.FriendRelationship {
|
||||
friendRelationship := nexproto.NewFriendRelationship()
|
||||
friendRelationship.PID = recipientPID
|
||||
friendRelationship.LFC = 0
|
||||
friendRelationship.RelationshipType = 0 // Incomplete
|
||||
|
||||
nowTime := nex.NewDateTime(0)
|
||||
nowTime.FromTimestamp(time.Now())
|
||||
|
||||
// Ensure that we inputted a valid user.
|
||||
var found bool
|
||||
err := database.Postgres.QueryRow(`SELECT COUNT(*) FROM "3ds".user_data WHERE pid=$1 LIMIT 1`, recipientPID).Scan(&found)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
if !found {
|
||||
friendRelationship.RelationshipType = 2 // Non-existent
|
||||
return friendRelationship
|
||||
}
|
||||
|
||||
// Get the other side's relationship, we need to know if we've already got one sent to us.
|
||||
err = database.Postgres.QueryRow(`
|
||||
SELECT COUNT(*) FROM "3ds".friendships WHERE user1_pid=$1 AND user2_pid=$2 AND type=0 LIMIT 1`, recipientPID, senderPID).Scan(&found)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
if !found {
|
||||
_, err = database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".friendships (user1_pid, user2_pid, type)
|
||||
VALUES ($1, $2, 0)
|
||||
ON CONFLICT (user1_pid, user2_pid)
|
||||
DO NOTHING`, senderPID, recipientPID)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
return friendRelationship
|
||||
}
|
||||
|
||||
acceptedTime := nex.NewDateTime(0).Now()
|
||||
|
||||
// We need to have two relationships for both sides as friend relationships are not one single object.
|
||||
_, err = database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".friendships (user1_pid, user2_pid, date, type)
|
||||
VALUES ($1, $2, $3, 1)
|
||||
ON CONFLICT (user1_pid, user2_pid)
|
||||
DO UPDATE SET
|
||||
date = $3,
|
||||
type = 1`, senderPID, recipientPID, acceptedTime)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".friendships (user1_pid, user2_pid, date, type)
|
||||
VALUES ($1, $2, $3, 1)
|
||||
ON CONFLICT (user1_pid, user2_pid)
|
||||
DO UPDATE SET
|
||||
date = $3,
|
||||
type = 1`, recipientPID, senderPID, acceptedTime)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
friendRelationship.RelationshipType = 1 // Complete
|
||||
return friendRelationship
|
||||
}
|
||||
24
database/3ds/update_user_comment.go
Normal file
24
database/3ds/update_user_comment.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
)
|
||||
|
||||
// Update a user's comment
|
||||
func UpdateUserComment(pid uint32, message string) {
|
||||
changed := nex.NewDateTime(0).Now()
|
||||
|
||||
_, err := database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".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())
|
||||
}
|
||||
}
|
||||
22
database/3ds/update_user_favorite_game.go
Normal file
22
database/3ds/update_user_favorite_game.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Update a user's favorite game
|
||||
func UpdateUserFavoriteGame(pid uint32, gameKey *nexproto.GameKey) {
|
||||
_, err := database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".user_data (pid, favorite_title, favorite_title_version)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
favorite_title = $2,
|
||||
favorite_title_version = $3`, pid, gameKey.TitleID, gameKey.TitleVersion)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
32
database/3ds/update_user_last_online_time.go
Normal file
32
database/3ds/update_user_last_online_time.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
)
|
||||
|
||||
// Update a user's last online time
|
||||
func UpdateUserLastOnlineTime(pid uint32, lastOnline *nex.DateTime) {
|
||||
var showOnline sql.NullBool
|
||||
|
||||
err := database.Postgres.QueryRow(`SELECT show_online FROM "3ds".user_data WHERE pid=$1`, pid).Scan(&showOnline)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
if showOnline.Valid && !showOnline.Bool {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".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())
|
||||
}
|
||||
}
|
||||
24
database/3ds/update_user_mii.go
Normal file
24
database/3ds/update_user_mii.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Update a user's mii
|
||||
func UpdateUserMii(pid uint32, mii *nexproto.Mii) {
|
||||
_, err := database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".user_data (pid, mii_name, mii_data, mii_changed)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
mii_name = $2,
|
||||
mii_data = $3,
|
||||
mii_changed = $4`, pid, mii.Name, mii.MiiData, nex.NewDateTime(0).Now())
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
21
database/3ds/update_user_preferences.go
Normal file
21
database/3ds/update_user_preferences.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
)
|
||||
|
||||
// Update a user's preferences
|
||||
func UpdateUserPreferences(pid uint32, show_online bool, show_current_game bool) {
|
||||
_, err := database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".user_data (pid, show_online, show_current_game)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
show_online = $2,
|
||||
show_current_game = $3`, pid, show_online, show_current_game)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
23
database/3ds/update_user_profile.go
Normal file
23
database/3ds/update_user_profile.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package database_3ds
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/database"
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
nexproto "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
// Update a user's profile
|
||||
func UpdateUserProfile(pid uint32, profileData *nexproto.MyProfile) {
|
||||
_, err := database.Postgres.Exec(`
|
||||
INSERT INTO "3ds".user_data (pid, region, area, language)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (pid)
|
||||
DO UPDATE SET
|
||||
region = $2,
|
||||
area = $3,
|
||||
language = $4`, pid, profileData.Region, profileData.Area, profileData.Language)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
@ -22,5 +22,5 @@ func connectPostgres() {
|
|||
globals.Logger.Success("Connected to Postgres!")
|
||||
|
||||
initPostgresWiiU()
|
||||
// TODO: 3DS database
|
||||
initPostgres3DS()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,15 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Connect() {
|
||||
connectMongo()
|
||||
connectPostgres()
|
||||
}
|
||||
|
||||
func PIDArrayToString(array []uint32) string {
|
||||
return strings.Trim(strings.Replace(fmt.Sprint(array), " ", ",", -1), "[]")
|
||||
}
|
||||
|
|
|
|||
51
database/init_postgres_3ds.go
Normal file
51
database/init_postgres_3ds.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package database
|
||||
|
||||
import "github.com/PretendoNetwork/friends-secure/globals"
|
||||
|
||||
func initPostgres3DS() {
|
||||
var err error
|
||||
|
||||
_, err = Postgres.Exec(`CREATE SCHEMA IF NOT EXISTS "3ds"`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
globals.Logger.Success("[3DS] Postgres schema created")
|
||||
|
||||
_, err = Postgres.Exec(`CREATE TABLE IF NOT EXISTS "3ds".user_data (
|
||||
pid integer PRIMARY KEY,
|
||||
show_online boolean,
|
||||
show_current_game boolean,
|
||||
comment text,
|
||||
comment_changed bigint,
|
||||
last_online bigint,
|
||||
favorite_title bigint,
|
||||
favorite_title_version integer,
|
||||
mii_name text,
|
||||
mii_data bytea,
|
||||
mii_changed bigint,
|
||||
region integer,
|
||||
area integer,
|
||||
language integer
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Postgres.Exec(`CREATE TABLE IF NOT EXISTS "3ds".friendships (
|
||||
id bigserial PRIMARY KEY,
|
||||
user1_pid integer,
|
||||
user2_pid integer,
|
||||
type integer,
|
||||
date bigint,
|
||||
UNIQUE (user1_pid, user2_pid)
|
||||
)`)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
globals.Logger.Success("[3DS] Postgres tables created")
|
||||
}
|
||||
|
|
@ -63,7 +63,7 @@ func AcceptFriendshipAndReturnFriendInfo(friendRequestID uint64) *nexproto.Frien
|
|||
if connectedUser != nil {
|
||||
// Online
|
||||
friendInfo.NNAInfo = connectedUser.NNAInfo
|
||||
friendInfo.Presence = connectedUser.Presence
|
||||
friendInfo.Presence = connectedUser.PresenceV2
|
||||
|
||||
lastOnline = nex.NewDateTime(0)
|
||||
lastOnline.FromTimestamp(time.Now())
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func GetUserFriendList(pid uint32) []*nexproto.FriendInfo {
|
|||
if connectedUser != nil {
|
||||
// Online
|
||||
friendInfo.NNAInfo = connectedUser.NNAInfo
|
||||
friendInfo.Presence = connectedUser.Presence
|
||||
friendInfo.Presence = connectedUser.PresenceV2
|
||||
|
||||
if friendInfo.NNAInfo == nil || friendInfo.NNAInfo.PrincipalBasicInfo == nil {
|
||||
// TODO: Fix this
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user