nidus-sync/api/communication.go
Eli Ribble 44c4f17f32
Massive rework of platform layer user/organization
The goal of this rework is to make it so I can pass around platform.User
instead of a pair of models.Organization and models.User. This is useful
for reason I kind of forget now, but it started with working on
notifications and ballooned massively from there into refactoring a
number of things that were bugging me.

This also includes a tiny amount of work on server-side events (SSE).

 * background stuff lives inside the platform now, which I need for
   having it push updates through SSE
 * userfile now lives in the platform, under file, so other platform
   functions can safely use it
 * oauth is broken into pieces and inside platform because other stuff
   was calling it already, but badly.
 * notifications go into the platform as well
2026-03-12 23:49:16 +00:00

101 lines
2.7 KiB
Go

package api
import (
"context"
"net/http"
"slices"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform"
"github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/google/uuid"
//"github.com/rs/zerolog/log"
)
type historyEntry struct {
Action string `json:"action"`
Timestamp time.Time `json:"timestamp"`
}
type reporter struct {
HasEmail bool `json:"has_email"`
HasPhone bool `json:"has_phone"`
Name string `json:"name"`
}
type communication struct {
Created time.Time `json:"created"`
History []historyEntry `json:"history"`
ID string `json:"id"`
PublicReport types.PublicReport `json:"public_report"`
Type string `json:"type"`
}
type contentListCommunication struct {
Communications []communication `json:"communications"`
}
func listCommunication(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*contentListCommunication, *nhttp.ErrorWithStatus) {
nreports, err := publicreport.NuisanceReportForOrganization(ctx, user.Organization.ID())
if err != nil {
return nil, nhttp.NewError("nuisance report query: %w", err)
}
wreports, err := publicreport.WaterReportForOrganization(ctx, user.Organization.ID())
if err != nil {
return nil, nhttp.NewError("water report query: %w", err)
}
comms := make([]communication, len(nreports)+len(wreports))
for i, report := range nreports {
comms[i] = communication{
Created: report.Created,
History: []historyEntry{
historyEntry{
Action: "created",
Timestamp: report.Created,
},
},
ID: report.PublicID,
PublicReport: report,
Type: "nuisance",
}
}
for i, report := range wreports {
comms[i+len(nreports)] = communication{
Created: report.Created,
History: []historyEntry{
historyEntry{
Action: "created",
Timestamp: report.Created,
},
},
ID: report.PublicID,
PublicReport: report,
Type: "water",
}
}
_by_created := func(a, b communication) int {
if a.Created == b.Created {
return 0
} else if a.Created.Before(b.Created) {
return 1
} else {
return -1
}
}
slices.SortFunc(comms, _by_created)
return &contentListCommunication{
Communications: comms,
}, nil
}
func toImageURLs(m map[string][]uuid.UUID, id string) []string {
uuids, ok := m[id]
if !ok {
return []string{}
}
urls := make([]string, len(uuids))
for i, u := range uuids {
urls[i] = config.MakeURLNidus("/api/image/%s/content", u.String())
}
return urls
}