Start on friends being synced

This commit is contained in:
Eccentric 2024-01-21 19:54:20 +00:00
parent c9c39b2ce8
commit 8619cac5ca
6 changed files with 62 additions and 26 deletions

View File

@ -1,6 +1,8 @@
package handlers package handlers
import ( import (
"time"
"github.com/ectrc/snow/aid" "github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person" p "github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
@ -30,15 +32,28 @@ func GetFriendList(c *fiber.Ctx) error {
func PostCreateFriend(c *fiber.Ctx) error { func PostCreateFriend(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person) 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") { if existing != nil && (existing.Direction == "BOTH" || existing.Direction == "OUTGOING") {
return c.Status(400).JSON(aid.ErrorBadRequest("already active friend request")) return c.Status(400).JSON(aid.ErrorBadRequest("already active friend request"))
} }
person.AddFriend(wanted) person.AddFriend(friendId)
// send xmpp message to disply a popup
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) return c.SendStatus(204)
} }
@ -52,9 +67,8 @@ func DeleteFriend(c *fiber.Ctx) error {
return c.Status(400).JSON(aid.ErrorBadRequest("not friends")) return c.Status(400).JSON(aid.ErrorBadRequest("not friends"))
} }
existing.Person.RemoveFriend(wanted)
person.RemoveFriend(wanted) person.RemoveFriend(wanted)
// send xmpp message to disply a popup existing.Person.RemoveFriend(person.ID)
return c.SendStatus(204) return c.SendStatus(204)
} }
@ -83,13 +97,13 @@ func GetFriendListSummary(c *fiber.Ctx) error {
for _, friend := range all { for _, friend := range all {
switch friend.Status { switch friend.Status {
case "ACCEPTED": case p.FriendStatusAccepted:
result["friends"] = append(result["friends"].([]aid.JSON), friend.GenerateSummaryResponse()) result["friends"] = append(result["friends"].([]aid.JSON), friend.GenerateSummaryResponse())
case "PENDING": case p.FriendStatusPending:
switch friend.Direction { switch friend.Direction {
case "INCOMING": case p.FriendDirectionIncoming:
result["incoming"] = append(result["incoming"].([]aid.JSON), friend.GenerateSummaryResponse()) result["incoming"] = append(result["incoming"].([]aid.JSON), friend.GenerateSummaryResponse())
case "OUTGOING": case p.FriendDirectionOutgoing:
result["outgoing"] = append(result["outgoing"].([]aid.JSON), friend.GenerateSummaryResponse()) result["outgoing"] = append(result["outgoing"].([]aid.JSON), friend.GenerateSummaryResponse())
} }
} }

View File

@ -81,10 +81,21 @@ func WebsocketConnection(c *websocket.Conn) {
if handle, ok := socketHandlers[protocol]; ok { if handle, ok := socketHandlers[protocol]; ok {
handle(uuid) 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 var recieverSocket *Socket
sockets.Range(func(key string, value *Socket) bool { sockets.Range(func(key string, value *Socket) bool {
if value.Person == nil { if value.Person == nil {
@ -110,8 +121,6 @@ func init() {
} }
} }
aid.Print("(socket) write queue started")
for { for {
message := <-socketWriteQueue message := <-socketWriteQueue
aid.Print("(socket) writing message to", message.Socket.ID, string(message.Message)) aid.Print("(socket) writing message to", message.Socket.ID, string(message.Message))

View File

@ -84,7 +84,7 @@ func presenceSocketHandle(id string) {
continue continue
} }
friendSocket := GetSocketByPerson(friend.Person) friendSocket := FindSocketForPerson(friend.Person)
if friendSocket == nil { if friendSocket == nil {
continue continue
} }
@ -224,7 +224,7 @@ func presenceSocketMessageEvent(socket *Socket, tree *etree.Document) error {
return nil return nil
} }
recieverSocket := GetSocketByPerson(reciever) recieverSocket := FindSocketForPerson(reciever)
if recieverSocket == nil { if recieverSocket == nil {
return nil return nil
} }
@ -270,7 +270,7 @@ func (s *Socket) PresenceWriteStatus() {
continue continue
} }
friendSocket := GetSocketByPerson(friend.Person) friendSocket := FindSocketForPerson(friend.Person)
if friendSocket == nil { if friendSocket == nil {
continue continue
} }
@ -296,10 +296,12 @@ func (s *Socket) PresenceWriteStatus() {
} }
func (s *Socket) PresenceWriteJSON(body aid.JSON) { func (s *Socket) PresenceWriteJSON(body aid.JSON) {
aid.PrintJSON(body)
document := etree.NewDocument() document := etree.NewDocument()
message := document.CreateElement("message") 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: "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: "to", Value: s.PresenceState.JID})
message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"}) message.Attr = append(message.Attr, etree.Attr{Key: "xmlns", Value: "jabber:client"})
message.CreateElement("body").SetText(aid.JSONStringify(body)) message.CreateElement("body").SetText(aid.JSONStringify(body))

View File

@ -131,6 +131,7 @@ func main() {
snow := r.Group("/snow") snow := r.Group("/snow")
snow.Get("/cosmetics", handlers.GetPreloadedCosmetics) snow.Get("/cosmetics", handlers.GetPreloadedCosmetics)
snow.Get("/image/:playlist", handlers.GetPlaylistImage) snow.Get("/image/:playlist", handlers.GetPlaylistImage)
snow.Get("/sockets", handlers.GetAllSockets)
discord := snow.Group("/discord") discord := snow.Group("/discord")
discord.Get("/", handlers.GetDiscordOAuthURL) discord.Get("/", handlers.GetDiscordOAuthURL)

View File

@ -6,10 +6,20 @@ import (
"github.com/ectrc/snow/aid" "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 { type Friend struct {
Person *Person Person *Person
Status string Status FriendStatus
Direction string Direction FriendDirection
} }
func (f *Friend) GenerateSummaryResponse() aid.JSON { func (f *Friend) GenerateSummaryResponse() aid.JSON {

View File

@ -276,23 +276,23 @@ func (p *Person) GetFriend(friendId string) *Friend {
if friend.IsFriendInFriendList(p.ID) { if friend.IsFriendInFriendList(p.ID) {
return &Friend{ return &Friend{
Person: friend, Person: friend,
Status: "ACCEPTED", Status: FriendStatusAccepted,
Direction: "BOTH", Direction: FriendDirectionBoth,
} }
} }
return &Friend{ return &Friend{
Person: friend, Person: friend,
Status: "PENDING", Status: FriendStatusPending,
Direction: "OUTBOUND", Direction: FriendDirectionOutgoing,
} }
} }
if friend.IsFriendInFriendList(p.ID) { if friend.IsFriendInFriendList(p.ID) {
return &Friend{ return &Friend{
Person: friend, Person: friend,
Status: "PENDING", Status: FriendStatusPending,
Direction: "INBOUND", Direction: FriendDirectionIncoming,
} }
} }