2026-03-13 21:22:34 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-03-22 09:54:02 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2026-03-22 01:22:44 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
2026-03-13 21:22:34 +00:00
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-22 01:33:14 +00:00
|
|
|
type contentURLAPI struct {
|
2026-03-22 07:06:50 +00:00
|
|
|
Communication string `json:"communication"`
|
|
|
|
|
PublicreportMessage string `json:"publicreport_message"`
|
2026-03-28 09:14:09 -07:00
|
|
|
ReviewTask string `json:"review_task"`
|
2026-03-22 09:54:02 +00:00
|
|
|
Signal string `json:"signal"`
|
2026-03-25 21:44:06 -07:00
|
|
|
Upload string `json:"upload"`
|
2026-03-28 14:45:49 -07:00
|
|
|
Users string `json:"users"`
|
2026-03-22 01:33:14 +00:00
|
|
|
}
|
2026-03-22 01:22:44 +00:00
|
|
|
type contentURLs struct {
|
2026-03-22 01:33:14 +00:00
|
|
|
API contentURLAPI `json:"api"`
|
|
|
|
|
Tegola string `json:"tegola"`
|
2026-03-22 09:54:02 +00:00
|
|
|
Tile string `json:"tile"`
|
2026-03-22 01:22:44 +00:00
|
|
|
}
|
|
|
|
|
type contentUserSelf struct {
|
|
|
|
|
Self platform.User `json:"self"`
|
|
|
|
|
URLs contentURLs `json:"urls"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getUserSelf(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*contentUserSelf, *nhttp.ErrorWithStatus) {
|
2026-03-13 21:22:34 +00:00
|
|
|
counts, err := platform.NotificationCountsForUser(ctx, user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("get notifications: %w", err)
|
|
|
|
|
}
|
2026-03-22 09:54:02 +00:00
|
|
|
org, err := platform.OrganizationByID(ctx, int(user.Organization.ID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("get org: %w", err)
|
|
|
|
|
}
|
|
|
|
|
user.Organization = *org
|
2026-03-13 21:22:34 +00:00
|
|
|
user.NotificationCounts = *counts
|
2026-03-22 01:22:44 +00:00
|
|
|
urls := html.NewContentURL()
|
|
|
|
|
return &contentUserSelf{
|
|
|
|
|
Self: user,
|
|
|
|
|
URLs: contentURLs{
|
2026-03-22 01:33:14 +00:00
|
|
|
API: contentURLAPI{
|
2026-03-22 07:06:50 +00:00
|
|
|
Communication: urls.API.Communication,
|
|
|
|
|
PublicreportMessage: urls.API.Publicreport.Message,
|
2026-03-28 09:14:09 -07:00
|
|
|
ReviewTask: config.MakeURLNidus("/api/review-task"),
|
2026-03-22 09:54:02 +00:00
|
|
|
Signal: config.MakeURLNidus("/api/signal"),
|
2026-03-25 21:44:06 -07:00
|
|
|
Upload: config.MakeURLNidus("/api/upload"),
|
2026-03-28 14:45:49 -07:00
|
|
|
Users: config.MakeURLNidus("/api/user"),
|
2026-03-22 01:33:14 +00:00
|
|
|
},
|
2026-03-22 01:22:44 +00:00
|
|
|
Tegola: urls.Tegola,
|
2026-03-22 09:54:02 +00:00
|
|
|
Tile: config.MakeURLNidus("/api/tile/{z}/{y}/{x}"),
|
2026-03-22 01:22:44 +00:00
|
|
|
},
|
|
|
|
|
}, nil
|
2026-03-13 21:22:34 +00:00
|
|
|
}
|
2026-03-20 05:20:37 +00:00
|
|
|
|
|
|
|
|
type responseListUser struct {
|
2026-03-28 14:45:49 -07:00
|
|
|
Users []*platform.User `json:"users"`
|
2026-03-20 05:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listUser(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*responseListUser, *nhttp.ErrorWithStatus) {
|
2026-03-28 14:45:49 -07:00
|
|
|
users, err := platform.UsersByOrg(ctx, user.Organization)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("list users: %w", err)
|
|
|
|
|
}
|
|
|
|
|
results := make([]*platform.User, len(users))
|
|
|
|
|
i := 0
|
|
|
|
|
for _, v := range users {
|
|
|
|
|
results[i] = v
|
|
|
|
|
i++
|
|
|
|
|
}
|
2026-03-20 05:20:37 +00:00
|
|
|
return &responseListUser{
|
2026-03-28 14:45:49 -07:00
|
|
|
Users: results,
|
2026-03-20 05:20:37 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type responseListUserSuggestion struct {
|
2026-03-28 14:45:49 -07:00
|
|
|
Users []*platform.User `json:"users"`
|
2026-03-20 05:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 14:45:49 -07:00
|
|
|
func listUserSuggestion(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*responseListUserSuggestion, *nhttp.ErrorWithStatus) {
|
2026-03-20 05:20:37 +00:00
|
|
|
if query.Query == nil {
|
|
|
|
|
return nil, nhttp.NewErrorStatus(http.StatusBadRequest, "you need to include a query")
|
|
|
|
|
}
|
|
|
|
|
users, err := platform.UserSuggestion(ctx, user, *query.Query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("query suggestions: %w", err)
|
|
|
|
|
}
|
2026-03-28 14:45:49 -07:00
|
|
|
return &responseListUserSuggestion{
|
2026-03-20 05:20:37 +00:00
|
|
|
Users: users,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|