What it do flight crew

This commit is contained in:
Eccentric 2024-01-30 16:34:17 +00:00
parent d16d9dd3ec
commit 9275e8fcd4
5 changed files with 79 additions and 135 deletions

View File

@ -14,13 +14,13 @@ func GetFriendList(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person) person := c.Locals("person").(*p.Person)
result := []aid.JSON{} result := []aid.JSON{}
person.IncomingRelationships.Range(func(key string, value *p.Relationship[p.RelationshipInboundDirection]) bool { person.Relationships.Range(func(key string, value *p.Relationship) bool {
result = append(result, value.GenerateFortniteFriendEntry()) switch value.Direction {
return true case p.RelationshipInboundDirection:
}) result = append(result, value.GenerateFortniteFriendEntry(p.GenerateTypeTowardsPerson))
case p.RelationshipOutboundDirection:
person.OutgoingRelationships.Range(func(key string, value *p.Relationship[p.RelationshipOutboundDirection]) bool { result = append(result, value.GenerateFortniteFriendEntry(p.GenerateTypeFromPerson))
result = append(result, value.GenerateFortniteFriendEntry()) }
return true return true
}) })
@ -28,53 +28,27 @@ func GetFriendList(c *fiber.Ctx) error {
} }
func PostCreateFriend(c *fiber.Ctx) error { func PostCreateFriend(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person) relationship, err := c.Locals("person").(*p.Person).CreateRelationship(c.Params("wanted"))
wanted := c.Params("wanted")
direction, err := person.CreateRelationship(wanted)
if err != nil { if err != nil {
aid.Print(fmt.Sprintf("Error creating relationship: %s", err.Error())) aid.Print(fmt.Sprintf("Error creating relationship: %s", err.Error()))
return c.Status(400).JSON(aid.ErrorBadRequest(err.Error())) return c.Status(400).JSON(aid.ErrorBadRequest(err.Error()))
} }
socket.JabberSockets.Range(func(key string, value *socket.Socket[socket.JabberData]) bool { from, found := socket.GetJabberSocketByPersonID(relationship.From.ID)
aid.Print(fmt.Sprintf("Checking socket: %s", key, value))
return true
})
personSocket, found := socket.GetJabberSocketByPersonID(wanted)
aid.Print(fmt.Sprintf("Found socket: %t", found), fmt.Sprintf("Direction: %s", direction))
if found { if found {
payload := aid.JSON{} from.JabberSendMessageToPerson(aid.JSON{
switch direction {
case "INBOUND":
payload = personSocket.Person.IncomingRelationships.MustGet(wanted).GenerateFortniteFriendEntry()
case "OUTBOUND":
payload = personSocket.Person.OutgoingRelationships.MustGet(wanted).GenerateFortniteFriendEntry()
}
personSocket.JabberSendMessageToPerson(aid.JSON{
"type": "com.epicgames.friends.core.apiobjects.Friend", "type": "com.epicgames.friends.core.apiobjects.Friend",
"timestamp": time.Now().Format(time.RFC3339), "timestamp": time.Now().Format(time.RFC3339),
"payload": payload, "payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeFromPerson),
}) })
} }
friendSocket, found := socket.GetJabberSocketByPersonID(wanted) towards, found := socket.GetJabberSocketByPersonID(relationship.Towards.ID)
aid.Print(fmt.Sprintf("Found friend socket: %t", found), fmt.Sprintf("Direction: %s", direction))
if found { if found {
payload := aid.JSON{} towards.JabberSendMessageToPerson(aid.JSON{
switch direction {
case "INBOUND":
payload = friendSocket.Person.OutgoingRelationships.MustGet(person.ID).GenerateFortniteFriendEntry()
case "OUTBOUND":
payload = friendSocket.Person.IncomingRelationships.MustGet(person.ID).GenerateFortniteFriendEntry()
}
friendSocket.JabberSendMessageToPerson(aid.JSON{
"type": "com.epicgames.friends.core.apiobjects.Friend", "type": "com.epicgames.friends.core.apiobjects.Friend",
"timestamp": time.Now().Format(time.RFC3339), "timestamp": time.Now().Format(time.RFC3339),
"payload": payload, "payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeTowardsPerson),
}) })
} }

View File

@ -18,8 +18,7 @@ type Person struct {
CollectionsProfile *Profile CollectionsProfile *Profile
CreativeProfile *Profile CreativeProfile *Profile
Discord *storage.DB_DiscordPerson Discord *storage.DB_DiscordPerson
OutgoingRelationships aid.GenericSyncMap[Relationship[RelationshipOutboundDirection]] Relationships aid.GenericSyncMap[Relationship]
IncomingRelationships aid.GenericSyncMap[Relationship[RelationshipInboundDirection]]
} }
func NewPerson() *Person { func NewPerson() *Person {
@ -194,8 +193,7 @@ func findHelper(databasePerson *storage.DB_Person, shallow bool, save bool) *Per
CollectionsProfile: collectionsProfile, CollectionsProfile: collectionsProfile,
CreativeProfile: creativeProfile, CreativeProfile: creativeProfile,
Discord: &databasePerson.Discord, Discord: &databasePerson.Discord,
OutgoingRelationships: aid.GenericSyncMap[Relationship[RelationshipOutboundDirection]]{}, Relationships: aid.GenericSyncMap[Relationship]{},
IncomingRelationships: aid.GenericSyncMap[Relationship[RelationshipInboundDirection]]{},
} }
if !shallow { if !shallow {

View File

@ -8,134 +8,106 @@ import (
) )
type RelationshipDirection string type RelationshipDirection string
const RelationshipInboundDirection RelationshipDirection = "INBOUND"
const RelationshipOutboundDirection RelationshipDirection = "OUTBOUND"
type RelationshipInboundDirection RelationshipDirection type RelationshipGenerateType string
const RelationshipInboundDirectionValue RelationshipInboundDirection = "INBOUND" const GenerateTypeFromPerson RelationshipGenerateType = "FROM_PERSON"
const GenerateTypeTowardsPerson RelationshipGenerateType = "TOWARDS_PERSON"
type RelationshipOutboundDirection RelationshipDirection type Relationship struct {
const RelationshipOutboundDirectionValue RelationshipOutboundDirection = "OUTBOUND" From *Person
type Relationship[T RelationshipInboundDirection | RelationshipOutboundDirection] struct {
Me *Person
Towards *Person Towards *Person
Status string Status string
Direction T Direction RelationshipDirection
} }
func (r *Relationship[T]) ToDatabase() *storage.DB_Relationship { func (r *Relationship) ToDatabase() *storage.DB_Relationship {
return &storage.DB_Relationship{ return &storage.DB_Relationship{
IncomingPersonID: r.Me.ID, FromPersonID: r.From.ID,
OutgoingPersonID: r.Towards.ID, TowardsPersonID: r.Towards.ID,
Status: r.Status, Status: r.Status,
} }
} }
func (r *Relationship[T]) GenerateFortniteFriendEntry() aid.JSON { func (r *Relationship) GenerateFortniteFriendEntry(t RelationshipGenerateType) aid.JSON {
return aid.JSON{ result := aid.JSON{
"accountId": r.Towards.ID,
"status": r.Status, "status": r.Status,
"direction": string(r.Direction),
"created": "0000-00-00T00:00:00.000Z", "created": "0000-00-00T00:00:00.000Z",
"favorite": false, "favorite": false,
} }
switch t {
case GenerateTypeFromPerson:
result["direction"] = "OUTBOUND"
result["accountId"] = r.Towards.ID
case GenerateTypeTowardsPerson:
result["direction"] = "INBOUND"
result["accountId"] = r.From.ID
}
return result
} }
func (r *Relationship[T]) Save() { func (r *Relationship) Save() (*Relationship, error) {
storage.Repo.Storage.SaveRelationship(r.ToDatabase()) storage.Repo.Storage.SaveRelationship(r.ToDatabase())
r.From.Relationships.Set(r.Towards.ID, r)
r.Towards.Relationships.Set(r.From.ID, r)
return r, nil
} }
func (r *Relationship[T]) Delete() { func (r *Relationship) Delete() error {
storage.Repo.Storage.DeleteRelationship(r.ToDatabase()) storage.Repo.Storage.DeleteRelationship(r.ToDatabase())
return nil
} }
func (p *Person) LoadRelationships() { func (p *Person) LoadRelationships() {
incoming := storage.Repo.Storage.GetIncomingRelationships(p.ID) incoming := storage.Repo.Storage.GetIncomingRelationships(p.ID)
for _, entry := range incoming { for _, entry := range incoming {
relationship := &Relationship[RelationshipInboundDirection]{ relationship := &Relationship{
Status: entry.Status, From: Find(entry.FromPersonID),
Me: p,
Towards: FindShallow(entry.OutgoingPersonID),
Direction: RelationshipInboundDirectionValue,
}
p.IncomingRelationships.Set(entry.OutgoingPersonID, relationship)
}
outgoing := storage.Repo.Storage.GetOutgoingRelationships(p.ID)
for _, entry := range outgoing {
relationship := &Relationship[RelationshipOutboundDirection]{
Status: entry.Status,
Me: p,
Towards: FindShallow(entry.IncomingPersonID),
Direction: RelationshipOutboundDirectionValue,
}
p.OutgoingRelationships.Set(entry.IncomingPersonID, relationship)
}
}
func (p *Person) CreateRelationship(personId string) (string, error) {
if p.ID == personId {
return "", fmt.Errorf("cannot create relationship with yourself")
}
if p.IncomingRelationships.Has(personId) {
return "INBOUND", p.createAcceptInboundRelationship(personId)
}
return "OUTBOUND", p.createOutboundRelationship(personId)
}
func (p *Person) createOutboundRelationship(towards string) error {
towardsPerson := Find(towards)
if towardsPerson == nil {
return fmt.Errorf("person not found")
}
relationship := &Relationship[RelationshipOutboundDirection]{
Me: p,
Towards: towardsPerson,
Status: "PENDING",
Direction: RelationshipOutboundDirectionValue,
}
relationship.Save()
p.OutgoingRelationships.Set(towards, relationship)
tempRelationship := &Relationship[RelationshipInboundDirection]{
Me: towardsPerson,
Towards: p, Towards: p,
Status: "PENDING", Status: entry.Status,
Direction: RelationshipInboundDirectionValue, Direction: RelationshipInboundDirection,
} }
tempRelationship.Save()
towardsPerson.IncomingRelationships.Set(p.ID, tempRelationship)
return nil p.Relationships.Set(entry.FromPersonID, relationship)
}
} }
func (p *Person) createAcceptInboundRelationship(towards string) error { func (p *Person) CreateRelationship(personId string) (*Relationship, error) {
towardsPerson := Find(towards) exists, okay := p.Relationships.Get(personId)
if towardsPerson == nil { if !okay {
return fmt.Errorf("person not found") return p.createOutboundRelationship(personId)
} }
relationship := &Relationship[RelationshipInboundDirection]{ if exists.Status != "PENDING" {
Me: p, return nil, fmt.Errorf("relationship already exists")
Towards: towardsPerson,
Status: "ACCEPTED",
Direction: RelationshipInboundDirectionValue,
} }
relationship.Save()
p.IncomingRelationships.Set(towards, relationship)
tempRelationship := &Relationship[RelationshipOutboundDirection]{ if exists.Towards.ID == p.ID {
Me: towardsPerson, return p.createAcceptInboundRelationship(personId)
}
return nil, fmt.Errorf("relationship already exists")
}
func (p *Person) createOutboundRelationship(towards string) (*Relationship, error) {
relationship := &Relationship{
From: p,
Towards: Find(towards),
Status: "PENDING",
Direction: RelationshipOutboundDirection,
}
return relationship.Save()
}
func (p *Person) createAcceptInboundRelationship(towards string) (*Relationship, error) {
relationship := &Relationship{
From: Find(towards),
Towards: p, Towards: p,
Status: "ACCEPTED", Status: "ACCEPTED",
Direction: RelationshipOutboundDirectionValue, Direction: RelationshipInboundDirection,
} }
tempRelationship.Save() return relationship.Save()
towardsPerson.OutgoingRelationships.Set(p.ID, tempRelationship)
return nil
} }

View File

@ -184,13 +184,13 @@ func (s *PostgresStorage) DeletePerson(personId string) {
func (s *PostgresStorage) GetIncomingRelationships(personId string) []*DB_Relationship { func (s *PostgresStorage) GetIncomingRelationships(personId string) []*DB_Relationship {
var dbRelationships []*DB_Relationship var dbRelationships []*DB_Relationship
s.Postgres.Model(&DB_Relationship{}).Where("incoming_person_id = ?", personId).Find(&dbRelationships) s.Postgres.Model(&DB_Relationship{}).Where("towards_person_id = ?", personId).Find(&dbRelationships)
return dbRelationships return dbRelationships
} }
func (s *PostgresStorage) GetOutgoingRelationships(personId string) []*DB_Relationship { func (s *PostgresStorage) GetOutgoingRelationships(personId string) []*DB_Relationship {
var dbRelationships []*DB_Relationship var dbRelationships []*DB_Relationship
s.Postgres.Model(&DB_Relationship{}).Where("outgoing_person_id = ?", personId).Find(&dbRelationships) s.Postgres.Model(&DB_Relationship{}).Where("from_person_id = ?", personId).Find(&dbRelationships)
return dbRelationships return dbRelationships
} }
@ -199,7 +199,7 @@ func (s *PostgresStorage) SaveRelationship(relationship *DB_Relationship) {
} }
func (s *PostgresStorage) DeleteRelationship(relationship *DB_Relationship) { func (s *PostgresStorage) DeleteRelationship(relationship *DB_Relationship) {
s.Postgres.Delete(&DB_Relationship{}, "incoming_person_id = ? AND outgoing_person_id = ?", relationship.IncomingPersonID, relationship.OutgoingPersonID) s.Postgres.Delete(relationship)
} }
func (s *PostgresStorage) SaveProfile(profile *DB_Profile) { func (s *PostgresStorage) SaveProfile(profile *DB_Profile) {

View File

@ -21,8 +21,8 @@ func (DB_Person) TableName() string {
} }
type DB_Relationship struct { type DB_Relationship struct {
OutgoingPersonID string `gorm:"primary_key"` FromPersonID string `gorm:"primary_key"`
IncomingPersonID string `gorm:"primary_key"` TowardsPersonID string `gorm:"primary_key"`
Status string Status string
} }