From 9275e8fcd4818012d838f8e2905b985e9b5fcea9 Mon Sep 17 00:00:00 2001 From: Eccentric Date: Tue, 30 Jan 2024 16:34:17 +0000 Subject: [PATCH] What it do flight crew --- handlers/friends.go | 56 +++++----------- person/person.go | 6 +- person/relationships.go | 142 ++++++++++++++++------------------------ storage/postgres.go | 6 +- storage/tables.go | 4 +- 5 files changed, 79 insertions(+), 135 deletions(-) diff --git a/handlers/friends.go b/handlers/friends.go index afacbde..82def37 100644 --- a/handlers/friends.go +++ b/handlers/friends.go @@ -14,13 +14,13 @@ func GetFriendList(c *fiber.Ctx) error { person := c.Locals("person").(*p.Person) result := []aid.JSON{} - person.IncomingRelationships.Range(func(key string, value *p.Relationship[p.RelationshipInboundDirection]) bool { - result = append(result, value.GenerateFortniteFriendEntry()) - return true - }) - - person.OutgoingRelationships.Range(func(key string, value *p.Relationship[p.RelationshipOutboundDirection]) bool { - result = append(result, value.GenerateFortniteFriendEntry()) + 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)) + } return true }) @@ -28,53 +28,27 @@ func GetFriendList(c *fiber.Ctx) error { } func PostCreateFriend(c *fiber.Ctx) error { - person := c.Locals("person").(*p.Person) - wanted := c.Params("wanted") - - direction, err := person.CreateRelationship(wanted) + relationship, err := c.Locals("person").(*p.Person).CreateRelationship(c.Params("wanted")) if err != nil { aid.Print(fmt.Sprintf("Error creating relationship: %s", err.Error())) return c.Status(400).JSON(aid.ErrorBadRequest(err.Error())) } - - socket.JabberSockets.Range(func(key string, value *socket.Socket[socket.JabberData]) bool { - 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)) + + from, found := socket.GetJabberSocketByPersonID(relationship.From.ID) if found { - payload := 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{ + from.JabberSendMessageToPerson(aid.JSON{ "type": "com.epicgames.friends.core.apiobjects.Friend", "timestamp": time.Now().Format(time.RFC3339), - "payload": payload, + "payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeFromPerson), }) } - friendSocket, found := socket.GetJabberSocketByPersonID(wanted) - aid.Print(fmt.Sprintf("Found friend socket: %t", found), fmt.Sprintf("Direction: %s", direction)) + towards, found := socket.GetJabberSocketByPersonID(relationship.Towards.ID) if found { - payload := 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{ + towards.JabberSendMessageToPerson(aid.JSON{ "type": "com.epicgames.friends.core.apiobjects.Friend", "timestamp": time.Now().Format(time.RFC3339), - "payload": payload, + "payload": relationship.GenerateFortniteFriendEntry(p.GenerateTypeTowardsPerson), }) } diff --git a/person/person.go b/person/person.go index e41b06b..b4e4534 100644 --- a/person/person.go +++ b/person/person.go @@ -18,8 +18,7 @@ type Person struct { CollectionsProfile *Profile CreativeProfile *Profile Discord *storage.DB_DiscordPerson - OutgoingRelationships aid.GenericSyncMap[Relationship[RelationshipOutboundDirection]] - IncomingRelationships aid.GenericSyncMap[Relationship[RelationshipInboundDirection]] + Relationships aid.GenericSyncMap[Relationship] } func NewPerson() *Person { @@ -194,8 +193,7 @@ func findHelper(databasePerson *storage.DB_Person, shallow bool, save bool) *Per CollectionsProfile: collectionsProfile, CreativeProfile: creativeProfile, Discord: &databasePerson.Discord, - OutgoingRelationships: aid.GenericSyncMap[Relationship[RelationshipOutboundDirection]]{}, - IncomingRelationships: aid.GenericSyncMap[Relationship[RelationshipInboundDirection]]{}, + Relationships: aid.GenericSyncMap[Relationship]{}, } if !shallow { diff --git a/person/relationships.go b/person/relationships.go index fccaba3..84fb3d4 100644 --- a/person/relationships.go +++ b/person/relationships.go @@ -8,134 +8,106 @@ import ( ) type RelationshipDirection string +const RelationshipInboundDirection RelationshipDirection = "INBOUND" +const RelationshipOutboundDirection RelationshipDirection = "OUTBOUND" -type RelationshipInboundDirection RelationshipDirection -const RelationshipInboundDirectionValue RelationshipInboundDirection = "INBOUND" +type RelationshipGenerateType string +const GenerateTypeFromPerson RelationshipGenerateType = "FROM_PERSON" +const GenerateTypeTowardsPerson RelationshipGenerateType = "TOWARDS_PERSON" -type RelationshipOutboundDirection RelationshipDirection -const RelationshipOutboundDirectionValue RelationshipOutboundDirection = "OUTBOUND" - -type Relationship[T RelationshipInboundDirection | RelationshipOutboundDirection] struct { - Me *Person +type Relationship struct { + From *Person Towards *Person 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{ - IncomingPersonID: r.Me.ID, - OutgoingPersonID: r.Towards.ID, + FromPersonID: r.From.ID, + TowardsPersonID: r.Towards.ID, Status: r.Status, } } -func (r *Relationship[T]) GenerateFortniteFriendEntry() aid.JSON { - return aid.JSON{ - "accountId": r.Towards.ID, +func (r *Relationship) GenerateFortniteFriendEntry(t RelationshipGenerateType) aid.JSON { + result := aid.JSON{ "status": r.Status, - "direction": string(r.Direction), "created": "0000-00-00T00:00:00.000Z", "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()) + 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()) + return nil } func (p *Person) LoadRelationships() { incoming := storage.Repo.Storage.GetIncomingRelationships(p.ID) for _, entry := range incoming { - relationship := &Relationship[RelationshipInboundDirection]{ + relationship := &Relationship{ + From: Find(entry.FromPersonID), + Towards: p, Status: entry.Status, - Me: p, - Towards: FindShallow(entry.OutgoingPersonID), - Direction: RelationshipInboundDirectionValue, + Direction: RelationshipInboundDirection, } - 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) + p.Relationships.Set(entry.FromPersonID, relationship) } } -func (p *Person) CreateRelationship(personId string) (string, error) { - if p.ID == personId { - return "", fmt.Errorf("cannot create relationship with yourself") +func (p *Person) CreateRelationship(personId string) (*Relationship, error) { + exists, okay := p.Relationships.Get(personId) + if !okay { + return p.createOutboundRelationship(personId) } - if p.IncomingRelationships.Has(personId) { - return "INBOUND", p.createAcceptInboundRelationship(personId) + if exists.Status != "PENDING" { + return nil, fmt.Errorf("relationship already exists") } - return "OUTBOUND", p.createOutboundRelationship(personId) + if exists.Towards.ID == p.ID { + return p.createAcceptInboundRelationship(personId) + } + + return nil, fmt.Errorf("relationship already exists") } -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, +func (p *Person) createOutboundRelationship(towards string) (*Relationship, error) { + relationship := &Relationship{ + From: p, + Towards: Find(towards), Status: "PENDING", - Direction: RelationshipOutboundDirectionValue, + Direction: RelationshipOutboundDirection, } - relationship.Save() - p.OutgoingRelationships.Set(towards, relationship) - - tempRelationship := &Relationship[RelationshipInboundDirection]{ - Me: towardsPerson, - Towards: p, - Status: "PENDING", - Direction: RelationshipInboundDirectionValue, - } - tempRelationship.Save() - towardsPerson.IncomingRelationships.Set(p.ID, tempRelationship) - - return nil + return relationship.Save() } -func (p *Person) createAcceptInboundRelationship(towards string) error { - towardsPerson := Find(towards) - if towardsPerson == nil { - return fmt.Errorf("person not found") - } - - relationship := &Relationship[RelationshipInboundDirection]{ - Me: p, - Towards: towardsPerson, - Status: "ACCEPTED", - Direction: RelationshipInboundDirectionValue, - } - relationship.Save() - p.IncomingRelationships.Set(towards, relationship) - - tempRelationship := &Relationship[RelationshipOutboundDirection]{ - Me: towardsPerson, +func (p *Person) createAcceptInboundRelationship(towards string) (*Relationship, error) { + relationship := &Relationship{ + From: Find(towards), Towards: p, Status: "ACCEPTED", - Direction: RelationshipOutboundDirectionValue, + Direction: RelationshipInboundDirection, } - tempRelationship.Save() - towardsPerson.OutgoingRelationships.Set(p.ID, tempRelationship) - - return nil + return relationship.Save() } \ No newline at end of file diff --git a/storage/postgres.go b/storage/postgres.go index 0846f1a..583dca1 100644 --- a/storage/postgres.go +++ b/storage/postgres.go @@ -184,13 +184,13 @@ func (s *PostgresStorage) DeletePerson(personId string) { func (s *PostgresStorage) GetIncomingRelationships(personId string) []*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 } func (s *PostgresStorage) GetOutgoingRelationships(personId string) []*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 } @@ -199,7 +199,7 @@ func (s *PostgresStorage) SaveRelationship(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) { diff --git a/storage/tables.go b/storage/tables.go index b048df3..d48711a 100644 --- a/storage/tables.go +++ b/storage/tables.go @@ -21,8 +21,8 @@ func (DB_Person) TableName() string { } type DB_Relationship struct { - OutgoingPersonID string `gorm:"primary_key"` - IncomingPersonID string `gorm:"primary_key"` + FromPersonID string `gorm:"primary_key"` + TowardsPersonID string `gorm:"primary_key"` Status string }