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
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())
}
}

View File

@ -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))

View File

@ -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))

View File

@ -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)

View File

@ -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 {

View File

@ -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,
}
}