2026-03-06 23:45:12 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
2026-03-10 15:46:17 +00:00
|
|
|
"slices"
|
2026-03-06 23:45:12 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
|
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
2026-03-12 23:49:16 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
2026-03-09 15:03:01 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
|
2026-03-09 22:59:21 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-03-06 23:45:12 +00:00
|
|
|
"github.com/google/uuid"
|
2026-03-09 15:03:01 +00:00
|
|
|
//"github.com/rs/zerolog/log"
|
2026-03-06 23:45:12 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-10 04:58:24 +00:00
|
|
|
type historyEntry struct {
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
|
|
|
|
}
|
2026-03-06 23:45:12 +00:00
|
|
|
type communication struct {
|
2026-03-09 22:59:21 +00:00
|
|
|
Created time.Time `json:"created"`
|
2026-03-10 04:58:24 +00:00
|
|
|
History []historyEntry `json:"history"`
|
2026-03-09 22:59:21 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
PublicReport types.PublicReport `json:"public_report"`
|
|
|
|
|
Type string `json:"type"`
|
2026-03-06 23:45:12 +00:00
|
|
|
}
|
|
|
|
|
type contentListCommunication struct {
|
|
|
|
|
Communications []communication `json:"communications"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func listCommunication(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*contentListCommunication, *nhttp.ErrorWithStatus) {
|
2026-03-18 15:36:20 +00:00
|
|
|
reports, err := publicreport.ReportsForOrganization(ctx, user.Organization.ID())
|
2026-03-06 23:45:12 +00:00
|
|
|
if err != nil {
|
2026-03-09 22:59:21 +00:00
|
|
|
return nil, nhttp.NewError("nuisance report query: %w", err)
|
2026-03-06 23:45:12 +00:00
|
|
|
}
|
2026-03-18 15:36:20 +00:00
|
|
|
comms := make([]communication, len(reports))
|
|
|
|
|
for i, report := range reports {
|
2026-03-06 23:45:12 +00:00
|
|
|
comms[i] = communication{
|
2026-03-10 04:58:24 +00:00
|
|
|
Created: report.Created,
|
|
|
|
|
History: []historyEntry{
|
|
|
|
|
historyEntry{
|
|
|
|
|
Action: "created",
|
|
|
|
|
Timestamp: report.Created,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-09 15:03:01 +00:00
|
|
|
ID: report.PublicID,
|
|
|
|
|
PublicReport: report,
|
|
|
|
|
Type: "nuisance",
|
2026-03-06 23:45:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 15:46:17 +00:00
|
|
|
_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)
|
2026-03-06 23:45:12 +00:00
|
|
|
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))
|
2026-03-07 01:20:06 +00:00
|
|
|
for i, u := range uuids {
|
2026-03-07 02:02:10 +00:00
|
|
|
urls[i] = config.MakeURLNidus("/api/image/%s/content", u.String())
|
2026-03-06 23:45:12 +00:00
|
|
|
}
|
|
|
|
|
return urls
|
|
|
|
|
}
|