snow/person/relationships.go

126 lines
3.2 KiB
Go
Raw Normal View History

2024-01-29 23:46:22 +00:00
package person
import (
"fmt"
2024-01-31 21:40:47 +00:00
"time"
2024-01-29 23:46:22 +00:00
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/storage"
)
type RelationshipDirection string
2024-01-30 16:34:17 +00:00
const RelationshipInboundDirection RelationshipDirection = "INBOUND"
const RelationshipOutboundDirection RelationshipDirection = "OUTBOUND"
2024-01-29 23:46:22 +00:00
2024-01-30 16:34:17 +00:00
type RelationshipGenerateType string
const GenerateTypeFromPerson RelationshipGenerateType = "FROM_PERSON"
const GenerateTypeTowardsPerson RelationshipGenerateType = "TOWARDS_PERSON"
2024-01-29 23:46:22 +00:00
2024-01-30 16:34:17 +00:00
type Relationship struct {
From *Person
2024-01-29 23:46:22 +00:00
Towards *Person
Status string
2024-01-30 16:34:17 +00:00
Direction RelationshipDirection
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
func (r *Relationship) ToDatabase() *storage.DB_Relationship {
2024-01-29 23:46:22 +00:00
return &storage.DB_Relationship{
2024-01-30 16:34:17 +00:00
FromPersonID: r.From.ID,
TowardsPersonID: r.Towards.ID,
2024-01-29 23:46:22 +00:00
Status: r.Status,
}
}
2024-01-30 16:34:17 +00:00
func (r *Relationship) GenerateFortniteFriendEntry(t RelationshipGenerateType) aid.JSON {
result := aid.JSON{
2024-01-29 23:46:22 +00:00
"status": r.Status,
2024-01-31 21:40:47 +00:00
"created": time.Now().Add(-time.Hour * 24 * 3).Format(time.RFC3339),
2024-01-29 23:46:22 +00:00
"favorite": false,
}
2024-01-30 16:34:17 +00:00
switch t {
case GenerateTypeFromPerson:
result["direction"] = RelationshipOutboundDirection
2024-01-30 16:34:17 +00:00
result["accountId"] = r.Towards.ID
case GenerateTypeTowardsPerson:
result["direction"] = RelationshipInboundDirection
2024-01-30 16:34:17 +00:00
result["accountId"] = r.From.ID
}
return result
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
func (r *Relationship) Save() (*Relationship, error) {
2024-01-29 23:46:22 +00:00
storage.Repo.Storage.SaveRelationship(r.ToDatabase())
2024-01-30 16:34:17 +00:00
r.From.Relationships.Set(r.Towards.ID, r)
r.Towards.Relationships.Set(r.From.ID, r)
return r, nil
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
func (r *Relationship) Delete() error {
2024-01-29 23:46:22 +00:00
storage.Repo.Storage.DeleteRelationship(r.ToDatabase())
2024-01-30 16:34:17 +00:00
return nil
2024-01-29 23:46:22 +00:00
}
func (p *Person) LoadRelationships() {
incoming := storage.Repo.Storage.GetIncomingRelationships(p.ID)
for _, entry := range incoming {
2024-01-30 16:34:17 +00:00
relationship := &Relationship{
From: FindShallow(entry.FromPersonID),
2024-01-30 16:34:17 +00:00
Towards: p,
2024-01-29 23:46:22 +00:00
Status: entry.Status,
2024-01-30 16:34:17 +00:00
Direction: RelationshipInboundDirection,
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
p.Relationships.Set(entry.FromPersonID, relationship)
2024-01-29 23:46:22 +00:00
}
outgoing := storage.Repo.Storage.GetOutgoingRelationships(p.ID)
for _, entry := range outgoing {
relationship := &Relationship{
From: p,
Towards: FindShallow(entry.TowardsPersonID),
Status: entry.Status,
Direction: RelationshipOutboundDirection,
}
p.Relationships.Set(entry.FromPersonID, relationship)
}
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
func (p *Person) CreateRelationship(personId string) (*Relationship, error) {
exists, okay := p.Relationships.Get(personId)
if !okay {
return p.createOutboundRelationship(personId)
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
if exists.Status != "PENDING" {
return nil, fmt.Errorf("relationship already exists")
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
if exists.Towards.ID == p.ID {
return p.createAcceptInboundRelationship(personId)
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
return nil, fmt.Errorf("relationship already exists")
}
2024-01-29 23:46:22 +00:00
2024-01-30 16:34:17 +00:00
func (p *Person) createOutboundRelationship(towards string) (*Relationship, error) {
relationship := &Relationship{
From: p,
Towards: FindShallow(towards),
2024-01-29 23:46:22 +00:00
Status: "PENDING",
2024-01-30 16:34:17 +00:00
Direction: RelationshipOutboundDirection,
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
return relationship.Save()
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
func (p *Person) createAcceptInboundRelationship(towards string) (*Relationship, error) {
relationship := &Relationship{
From: FindShallow(towards),
2024-01-29 23:46:22 +00:00
Towards: p,
Status: "ACCEPTED",
2024-01-30 16:34:17 +00:00
Direction: RelationshipInboundDirection,
2024-01-29 23:46:22 +00:00
}
2024-01-30 16:34:17 +00:00
return relationship.Save()
2024-01-29 23:46:22 +00:00
}