snow/handlers/friends.go

103 lines
2.9 KiB
Go
Raw Normal View History

2024-01-03 23:25:17 +00:00
package handlers
import (
2024-01-29 23:46:22 +00:00
"fmt"
"time"
2024-01-03 23:25:17 +00:00
"github.com/ectrc/snow/aid"
2024-01-29 23:46:22 +00:00
p "github.com/ectrc/snow/person"
"github.com/ectrc/snow/socket"
2024-01-03 23:25:17 +00:00
"github.com/gofiber/fiber/v2"
)
func GetFriendList(c *fiber.Ctx) error {
2024-01-29 23:46:22 +00:00
person := c.Locals("person").(*p.Person)
result := []aid.JSON{}
2024-01-30 16:34:17 +00:00
person.Relationships.Range(func(key string, value *p.Relationship) bool {
switch value.Direction {
case p.RelationshipInboundDirection:
result = append(result, value.GenerateFortniteFriendEntry(p.GenerateTypeTowardsPerson))
case p.RelationshipOutboundDirection:
result = append(result, value.GenerateFortniteFriendEntry(p.GenerateTypeFromPerson))
}
2024-01-29 23:46:22 +00:00
return true
})
return c.Status(200).JSON(result)
2024-01-03 23:25:17 +00:00
}
func PostCreateFriend(c *fiber.Ctx) error {
2024-01-30 16:34:17 +00:00
relationship, err := c.Locals("person").(*p.Person).CreateRelationship(c.Params("wanted"))
2024-01-29 23:46:22 +00:00
if err != nil {
aid.Print(fmt.Sprintf("Error creating relationship: %s", err.Error()))
return c.Status(400).JSON(aid.ErrorBadRequest(err.Error()))
}
2024-01-30 16:34:17 +00:00
from, found := socket.JabberSockets.Get(relationship.From.ID)
2024-01-29 23:46:22 +00:00
if found {
2024-01-30 16:34:17 +00:00
from.JabberSendMessageToPerson(aid.JSON{
2024-01-29 23:46:22 +00:00
"type": "com.epicgames.friends.core.apiobjects.Friend",
"timestamp": time.Now().Format(time.RFC3339),
2024-01-30 16:34:17 +00:00
"payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeFromPerson),
2024-01-29 23:46:22 +00:00
})
from.JabberNotifyFriends()
2024-01-29 23:46:22 +00:00
}
towards, found := socket.JabberSockets.Get(relationship.Towards.ID)
2024-01-29 23:46:22 +00:00
if found {
2024-01-30 16:34:17 +00:00
towards.JabberSendMessageToPerson(aid.JSON{
2024-01-29 23:46:22 +00:00
"type": "com.epicgames.friends.core.apiobjects.Friend",
"timestamp": time.Now().Format(time.RFC3339),
2024-01-30 16:34:17 +00:00
"payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeTowardsPerson),
2024-01-29 23:46:22 +00:00
})
towards.JabberNotifyFriends()
2024-01-29 23:46:22 +00:00
}
2024-01-03 23:25:17 +00:00
return c.SendStatus(204)
}
func DeleteFriend(c *fiber.Ctx) error {
return c.SendStatus(204)
}
func GetFriendListSummary(c *fiber.Ctx) error {
2024-02-04 16:01:24 +00:00
person := c.Locals("person").(*p.Person)
summary := aid.JSON{
"friends": []aid.JSON{},
"blocklist": []aid.JSON{},
"incoming": []aid.JSON{},
"outgoing": []aid.JSON{},
"suggested": []aid.JSON{},
}
person.Relationships.Range(func(key string, value *p.Relationship) bool {
switch value.Direction {
case p.RelationshipInboundDirection:
res := value.GenerateFortniteSummaryEntry(p.GenerateTypeTowardsPerson)
if value.Status == "ACCEPTED" {
summary["friends"] = append(summary["friends"].([]aid.JSON), res)
break
}
summary["incoming"] = append(summary["incoming"].([]aid.JSON), res)
case p.RelationshipOutboundDirection:
res := value.GenerateFortniteSummaryEntry(p.GenerateTypeFromPerson)
if value.Status == "ACCEPTED" {
summary["friends"] = append(summary["friends"].([]aid.JSON), res)
break
}
summary["outgoing"] = append(summary["outgoing"].([]aid.JSON), res)
}
return true
})
return c.Status(200).JSON(summary)
2024-01-03 23:25:17 +00:00
}
func GetPersonSearch(c *fiber.Ctx) error {
return c.Status(200).JSON([]aid.JSON{})
2024-01-03 23:25:17 +00:00
}