snow/handlers/friends.go

138 lines
3.4 KiB
Go
Raw Normal View History

2024-01-03 23:25:17 +00:00
package handlers
import (
2024-01-21 19:54:20 +00:00
"time"
2024-01-03 23:25:17 +00:00
"github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage"
"github.com/gofiber/fiber/v2"
)
func GetFriendList(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
result := map[string]aid.JSON{}
for _, partial := range storage.Repo.GetFriendsForPerson(person.ID) {
friend := person.GetFriend(partial.ID)
if friend == nil {
continue
}
result[partial.ID] = friend.GenerateFriendResponse()
}
response := []aid.JSON{}
for _, friend := range result {
response = append(response, friend)
}
return c.Status(200).JSON(response)
}
func PostCreateFriend(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
2024-01-21 19:54:20 +00:00
friendId := c.Params("wanted")
2024-01-03 23:25:17 +00:00
2024-01-21 19:54:20 +00:00
existing := person.GetFriend(friendId)
2024-01-03 23:25:17 +00:00
if existing != nil && (existing.Direction == "BOTH" || existing.Direction == "OUTGOING") {
return c.Status(400).JSON(aid.ErrorBadRequest("already active friend request"))
}
2024-01-21 19:54:20 +00:00
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),
})
2024-01-03 23:25:17 +00:00
return c.SendStatus(204)
}
func DeleteFriend(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
wanted := c.Params("wanted")
existing := person.GetFriend(wanted)
if existing == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("not friends"))
}
person.RemoveFriend(wanted)
2024-01-21 19:54:20 +00:00
existing.Person.RemoveFriend(person.ID)
2024-01-03 23:25:17 +00:00
return c.SendStatus(204)
}
func GetFriendListSummary(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
all := map[string]*p.Friend{}
for _, partial := range storage.Repo.GetFriendsForPerson(person.ID) {
friend := person.GetFriend(partial.ID)
if friend == nil {
continue
}
all[partial.ID] = friend
}
result := aid.JSON{
"friends": []aid.JSON{},
"incoming": []aid.JSON{},
"outgoing": []aid.JSON{},
"settings": aid.JSON{
"acceptInvites": "public",
},
}
for _, friend := range all {
switch friend.Status {
2024-01-21 19:54:20 +00:00
case p.FriendStatusAccepted:
2024-01-03 23:25:17 +00:00
result["friends"] = append(result["friends"].([]aid.JSON), friend.GenerateSummaryResponse())
2024-01-21 19:54:20 +00:00
case p.FriendStatusPending:
2024-01-03 23:25:17 +00:00
switch friend.Direction {
2024-01-21 19:54:20 +00:00
case p.FriendDirectionIncoming:
2024-01-03 23:25:17 +00:00
result["incoming"] = append(result["incoming"].([]aid.JSON), friend.GenerateSummaryResponse())
2024-01-21 19:54:20 +00:00
case p.FriendDirectionOutgoing:
2024-01-03 23:25:17 +00:00
result["outgoing"] = append(result["outgoing"].([]aid.JSON), friend.GenerateSummaryResponse())
}
}
}
return c.Status(200).JSON(result)
}
func GetPersonSearch(c *fiber.Ctx) error {
query := c.Query("prefix")
matches := storage.Repo.GetPersonsByPartialDisplayFromDB(query)
if matches == nil {
return c.Status(200).JSON([]aid.JSON{})
}
result := []aid.JSON{}
for i, match := range matches {
result = append(result, aid.JSON{
"accountId": match.ID,
"epicMutuals": 0,
"sortPosition": i,
"matchType": "prefix",
"matches": []aid.JSON{{
"value": match.DisplayName,
"matchType": "prefix",
}},
})
}
return c.Status(200).JSON(result)
}