diff --git a/backend/events.go b/backend/events.go index 5af6841..fb6c203 100644 --- a/backend/events.go +++ b/backend/events.go @@ -46,8 +46,6 @@ func ListenNewImageEvents(db *sql.DB, eventManager *EventManager) { ctx := context.Background() go func() { - time.Sleep(10 * time.Second) - image, err := imageModel.GetToProcessWithData(ctx, imageId) if err != nil { databaseEventLog.Error("Failed to GetToProcessWithData", "error", err) diff --git a/backend/main.go b/backend/main.go index 52552bd..dce1247 100644 --- a/backend/main.go +++ b/backend/main.go @@ -85,67 +85,7 @@ func main() { return } - type DataType struct { - Type string `json:"type"` - Data any `json:"data"` - } - - dataTypes := make([]DataType, 0) - - // lord - // forgive me - idMap := make(map[uuid.UUID]bool) - - for _, image := range images { - for _, location := range image.Locations { - _, exists := idMap[location.ID] - if exists { - continue - } - dataTypes = append(dataTypes, DataType{ - Type: "location", - Data: location, - }) - - idMap[location.ID] = true - } - - for _, event := range image.Events { - _, exists := idMap[event.ID] - if exists { - continue - } - dataTypes = append(dataTypes, DataType{ - Type: "event", - Data: event, - }) - - idMap[event.ID] = true - } - - for _, note := range image.Notes { - dataTypes = append(dataTypes, DataType{ - Type: "note", - Data: note, - }) - idMap[note.ID] = true - } - - for _, contact := range image.Contacts { - _, exists := idMap[contact.ID] - if exists { - continue - } - - dataTypes = append(dataTypes, DataType{ - Type: "contact", - Data: contact, - }) - idMap[contact.ID] = true - } - } - - jsonImages, err := json.Marshal(dataTypes) + jsonImages, err := json.Marshal(models.GetTypedImageProperties(images)) if err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) diff --git a/backend/models/user.go b/backend/models/user.go index ee195bf..38c74d9 100644 --- a/backend/models/user.go +++ b/backend/models/user.go @@ -22,19 +22,180 @@ type ImageWithProperties struct { Image model.Image - Tags []struct { - model.ImageTags - Tag model.UserTags - } - Links []model.ImageLinks - Text []model.ImageText - Locations []model.Locations Events []model.Events Notes []model.Notes Contacts []model.Contacts } +type PropertiesWithImage struct { + Locations []struct { + model.Locations + + Images uuid.UUIDs + } + Contacts []struct { + model.Contacts + + Images uuid.UUIDs + } + Events []struct { + model.Events + + Images uuid.UUIDs + } + Notes []struct { + model.Notes + + Images uuid.UUIDs + } +} + +func transpose(imageProperties []ImageWithProperties) PropertiesWithImage { + // EntityID -> []ImageIDs + dependencies := make(map[uuid.UUID]uuid.UUIDs) + + addDependency := func(entityId uuid.UUID, imageId uuid.UUID) { + deps, exists := dependencies[entityId] + if !exists { + dep := uuid.UUIDs{imageId} + dependencies[entityId] = dep + + return + } + + deps = append(deps, imageId) + } + + contactMap := make(map[uuid.UUID]model.Contacts) + locationMap := make(map[uuid.UUID]model.Locations) + noteMap := make(map[uuid.UUID]model.Notes) + eventMap := make(map[uuid.UUID]model.Events) + + for _, image := range imageProperties { + for _, contact := range image.Contacts { + contactMap[contact.ID] = contact + addDependency(contact.ID, image.Image.ID) + } + for _, location := range image.Locations { + locationMap[location.ID] = location + addDependency(location.ID, image.Image.ID) + } + for _, note := range image.Notes { + noteMap[note.ID] = note + addDependency(note.ID, image.Image.ID) + } + for _, event := range image.Events { + eventMap[event.ID] = event + addDependency(event.ID, image.Image.ID) + } + } + + properties := PropertiesWithImage{ + Contacts: make([]struct { + model.Contacts + Images uuid.UUIDs + }, 0), + Locations: make([]struct { + model.Locations + Images uuid.UUIDs + }, 0), + Notes: make([]struct { + model.Notes + Images uuid.UUIDs + }, 0), + Events: make([]struct { + model.Events + Images uuid.UUIDs + }, 0), + } + + for contactId, contact := range contactMap { + properties.Contacts = append(properties.Contacts, struct { + model.Contacts + Images uuid.UUIDs + }{ + Contacts: contact, + Images: dependencies[contactId], + }) + } + + for locationId, location := range locationMap { + properties.Locations = append(properties.Locations, struct { + model.Locations + Images uuid.UUIDs + }{ + Locations: location, + Images: dependencies[locationId], + }) + } + + for noteId, note := range noteMap { + properties.Notes = append(properties.Notes, struct { + model.Notes + Images uuid.UUIDs + }{ + Notes: note, + Images: dependencies[noteId], + }) + } + + for eventId, event := range eventMap { + properties.Events = append(properties.Events, struct { + model.Events + Images uuid.UUIDs + }{ + Events: event, + Images: dependencies[eventId], + }) + } + + return properties +} + +type TypedProperties struct { + Type string `json:"type"` + Data any `json:"data"` +} + +func propertiesToTypeArray(properties PropertiesWithImage) []TypedProperties { + typedProperties := make([]TypedProperties, 0) + + for _, location := range properties.Locations { + typedProperties = append(typedProperties, TypedProperties{ + Type: "location", + Data: location, + }) + } + + for _, contact := range properties.Contacts { + typedProperties = append(typedProperties, TypedProperties{ + Type: "contact", + Data: contact, + }) + } + + for _, note := range properties.Notes { + typedProperties = append(typedProperties, TypedProperties{ + Type: "note", + Data: note, + }) + } + + for _, event := range properties.Events { + typedProperties = append(typedProperties, TypedProperties{ + Type: "event", + Data: event, + }) + } + + return typedProperties +} + +func GetTypedImageProperties(imageProperties []ImageWithProperties) []TypedProperties { + return propertiesToTypeArray(transpose(imageProperties)) +} + func getUserIdFromImage(ctx context.Context, dbPool *sql.DB, imageId uuid.UUID) (uuid.UUID, error) { getUserIdStmt := UserImages.SELECT(UserImages.UserID).WHERE(UserImages.ImageID.EQ(UUID(imageId)))