From 8619cac5caf6a3676892d7f307ccfb0e3ff4484c Mon Sep 17 00:00:00 2001 From: Eccentric Date: Sun, 21 Jan 2024 19:54:20 +0000 Subject: [PATCH] Start on friends being synced --- handlers/friends.go | 34 ++++++++++++++++++++++++---------- handlers/socket.go | 17 +++++++++++++---- handlers/xmpp.go | 10 ++++++---- main.go | 1 + person/friend.go | 14 ++++++++++++-- person/person.go | 12 ++++++------ 6 files changed, 62 insertions(+), 26 deletions(-) diff --git a/handlers/friends.go b/handlers/friends.go index 2f5c91a..edeb19f 100644 --- a/handlers/friends.go +++ b/handlers/friends.go @@ -1,6 +1,8 @@ package handlers import ( + "time" + "github.com/ectrc/snow/aid" p "github.com/ectrc/snow/person" "github.com/ectrc/snow/storage" @@ -30,15 +32,28 @@ func GetFriendList(c *fiber.Ctx) error { func PostCreateFriend(c *fiber.Ctx) error { person := c.Locals("person").(*p.Person) - wanted := c.Params("wanted") + friendId := c.Params("wanted") - existing := person.GetFriend(wanted) + existing := person.GetFriend(friendId) if existing != nil && (existing.Direction == "BOTH" || existing.Direction == "OUTGOING") { return c.Status(400).JSON(aid.ErrorBadRequest("already active friend request")) } - person.AddFriend(wanted) - // send xmpp message to disply a popup + person.AddFriend(friendId) + + socket := FindSocketForPerson(person) + socket.PresenceWriteJSON(aid.JSON{ + "payload": person.GetFriend(friendId).GenerateFriendResponse(), + "type": "com.epicgames.friends.core.apiobjects.Friend", + "timestamp": time.Now().Format(time.RFC3339), + }) + + friendSocket := FindSocketForPerson(p.Find(friendId)) + friendSocket.PresenceWriteJSON(aid.JSON{ + "payload": friendSocket.Person.GetFriend(person.ID).GenerateFriendResponse(), + "type": "com.epicgames.friends.core.apiobjects.Friend", + "timestamp": time.Now().Format(time.RFC3339), + }) return c.SendStatus(204) } @@ -52,9 +67,8 @@ func DeleteFriend(c *fiber.Ctx) error { return c.Status(400).JSON(aid.ErrorBadRequest("not friends")) } - existing.Person.RemoveFriend(wanted) person.RemoveFriend(wanted) - // send xmpp message to disply a popup + existing.Person.RemoveFriend(person.ID) return c.SendStatus(204) } @@ -83,13 +97,13 @@ func GetFriendListSummary(c *fiber.Ctx) error { for _, friend := range all { switch friend.Status { - case "ACCEPTED": + case p.FriendStatusAccepted: result["friends"] = append(result["friends"].([]aid.JSON), friend.GenerateSummaryResponse()) - case "PENDING": + case p.FriendStatusPending: switch friend.Direction { - case "INCOMING": + case p.FriendDirectionIncoming: result["incoming"] = append(result["incoming"].([]aid.JSON), friend.GenerateSummaryResponse()) - case "OUTGOING": + case p.FriendDirectionOutgoing: result["outgoing"] = append(result["outgoing"].([]aid.JSON), friend.GenerateSummaryResponse()) } } diff --git a/handlers/socket.go b/handlers/socket.go index 8d89555..a9affd8 100644 --- a/handlers/socket.go +++ b/handlers/socket.go @@ -81,10 +81,21 @@ func WebsocketConnection(c *websocket.Conn) { if handle, ok := socketHandlers[protocol]; ok { handle(uuid) - } + } } -func GetSocketByPerson(person *person.Person) *Socket { +func GetAllSockets(c *fiber.Ctx) error { + result := []any{} + + sockets.Range(func(key string, socket *Socket) bool { + result = append(result, *socket) + return true + }) + + return c.Status(200).JSON(result) +} + +func FindSocketForPerson(person *person.Person) *Socket { var recieverSocket *Socket sockets.Range(func(key string, value *Socket) bool { if value.Person == nil { @@ -110,8 +121,6 @@ func init() { } } - aid.Print("(socket) write queue started") - for { message := <-socketWriteQueue aid.Print("(socket) writing message to", message.Socket.ID, string(message.Message)) diff --git a/handlers/xmpp.go b/handlers/xmpp.go index 921f5a4..5e2a29c 100644 --- a/handlers/xmpp.go +++ b/handlers/xmpp.go @@ -84,7 +84,7 @@ func presenceSocketHandle(id string) { continue } - friendSocket := GetSocketByPerson(friend.Person) + friendSocket := FindSocketForPerson(friend.Person) if friendSocket == nil { continue } @@ -224,7 +224,7 @@ func presenceSocketMessageEvent(socket *Socket, tree *etree.Document) error { return nil } - recieverSocket := GetSocketByPerson(reciever) + recieverSocket := FindSocketForPerson(reciever) if recieverSocket == nil { return nil } @@ -270,7 +270,7 @@ func (s *Socket) PresenceWriteStatus() { continue } - friendSocket := GetSocketByPerson(friend.Person) + friendSocket := FindSocketForPerson(friend.Person) if friendSocket == nil { continue } @@ -296,10 +296,12 @@ func (s *Socket) PresenceWriteStatus() { } func (s *Socket) PresenceWriteJSON(body aid.JSON) { + aid.PrintJSON(body) + document := etree.NewDocument() message := document.CreateElement("message") message.Attr = append(message.Attr, etree.Attr{Key: "id", Value: uuid.New().String()}) - message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: "prod.ol.epicgames.com"}) + message.Attr = append(message.Attr, etree.Attr{Key: "from", Value: "xmpp-admin@prod.ol.epicgames.com"}) message.Attr = append(message.Attr, etree.Attr{Key: "to", Value: s.PresenceState.JID}) message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"}) message.CreateElement("body").SetText(aid.JSONStringify(body)) diff --git a/main.go b/main.go index 4563416..124c525 100644 --- a/main.go +++ b/main.go @@ -131,6 +131,7 @@ func main() { snow := r.Group("/snow") snow.Get("/cosmetics", handlers.GetPreloadedCosmetics) snow.Get("/image/:playlist", handlers.GetPlaylistImage) + snow.Get("/sockets", handlers.GetAllSockets) discord := snow.Group("/discord") discord.Get("/", handlers.GetDiscordOAuthURL) diff --git a/person/friend.go b/person/friend.go index c6d9ac1..c053cd6 100644 --- a/person/friend.go +++ b/person/friend.go @@ -6,10 +6,20 @@ import ( "github.com/ectrc/snow/aid" ) +type FriendDirection string +var FriendDirectionBoth FriendDirection = "BOTH" +var FriendDirectionIncoming FriendDirection = "INCOMING" +var FriendDirectionOutgoing FriendDirection = "OUTGOING" + +type FriendStatus string +var FriendStatusPending FriendStatus = "PENDING" +var FriendStatusAccepted FriendStatus = "ACCEPTED" +var FriendStatusDeleted FriendStatus = "DELETED" + type Friend struct { Person *Person - Status string - Direction string + Status FriendStatus + Direction FriendDirection } func (f *Friend) GenerateSummaryResponse() aid.JSON { diff --git a/person/person.go b/person/person.go index 6120192..03a822c 100644 --- a/person/person.go +++ b/person/person.go @@ -276,23 +276,23 @@ func (p *Person) GetFriend(friendId string) *Friend { if friend.IsFriendInFriendList(p.ID) { return &Friend{ Person: friend, - Status: "ACCEPTED", - Direction: "BOTH", + Status: FriendStatusAccepted, + Direction: FriendDirectionBoth, } } return &Friend{ Person: friend, - Status: "PENDING", - Direction: "OUTBOUND", + Status: FriendStatusPending, + Direction: FriendDirectionOutgoing, } } if friend.IsFriendInFriendList(p.ID) { return &Friend{ Person: friend, - Status: "PENDING", - Direction: "INBOUND", + Status: FriendStatusPending, + Direction: FriendDirectionIncoming, } }