Allow sudo to send structured SSEs

This commit is contained in:
Eli Ribble 2026-03-13 18:31:43 +00:00
parent be1e49e524
commit 6fb964852f
No known key found for this signature in database
4 changed files with 53 additions and 10 deletions

View file

@ -218,16 +218,36 @@
value="{{ .Organization.ID }}" value="{{ .Organization.ID }}"
/> />
</div> </div>
<div class="mb-3">
<label for="type" class="form-label">Type</label>
<select class="form-select" id="type" name="type">
<option value="created">Created</option>
<option value="deleted">Deleted</option>
<option value="heartbeat">Heartbeat</option>
<option value="sudo">Sudo</option>
<option value="updated">Updated</option>
</select>
</div>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<div class="mb-3"> <div class="mb-3">
<label for="content" class="form-label">Content</label> <label for="resource" class="form-label">Resource</label>
<input <input
class="form-control" class="form-control"
id="content" id="resource"
name="content" name="resource"
placeholder="Message content"
type="text" type="text"
value="rmo:nuisance"
/>
</div>
<div class="mb-3">
<label for="uriPath" class="form-label">URI path</label>
<input
class="form-control"
id="uri-path"
name="uriPath"
type="text"
value="/report/abcd-1234"
/> />
</div> </div>
</div> </div>

View file

@ -3,6 +3,7 @@ package platform
import ( import (
"time" "time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/Gleipnir-Technology/nidus-sync/platform/event" "github.com/Gleipnir-Technology/nidus-sync/platform/event"
) )
@ -14,13 +15,14 @@ const EventTypeHeartbeat = event.EventTypeHeartbeat
func SetEventChannel(chan_events chan<- Envelope) { func SetEventChannel(chan_events chan<- Envelope) {
event.SetEventChannel(chan_events) event.SetEventChannel(chan_events)
} }
func SudoEvent(org_id int32, content string) { func SudoEvent(org_id int32, resource, type_, uri_path string) {
event_type := event.EventTypeFromString(type_)
go event.Send(event.Envelope{ go event.Send(event.Envelope{
Event: Event{ Event: Event{
Resource: "sudo", Resource: resource,
Time: time.Now(), Time: time.Now(),
Type: event.EventTypeSudo, Type: event_type,
URI: content, URI: config.MakeURLNidus(uri_path),
}, },
OrganizationID: org_id, OrganizationID: org_id,
}) })

View file

@ -30,6 +30,7 @@ const (
EventTypeDeleted EventTypeDeleted
EventTypeHeartbeat EventTypeHeartbeat
EventTypeSudo EventTypeSudo
EventTypeUnknown
EventTypeUpdated EventTypeUpdated
) )
@ -43,11 +44,29 @@ func (et EventType) String() string {
return "heartbeat" return "heartbeat"
case EventTypeSudo: case EventTypeSudo:
return "sudo" return "sudo"
case EventTypeUnknown:
return "unknown"
case EventTypeUpdated: case EventTypeUpdated:
return "updated" return "updated"
} }
return "unknown" return "unknown"
} }
func EventTypeFromString(s string) EventType {
switch s {
case "created":
return EventTypeCreated
case "deleted":
return EventTypeDeleted
case "heartbeat":
return EventTypeHeartbeat
case "sudo":
return EventTypeSudo
case "updated":
return EventTypeUpdated
default:
return EventTypeUnknown
}
}
type ResourceType int type ResourceType int

View file

@ -86,8 +86,10 @@ func postSudoSMS(ctx context.Context, r *http.Request, u platform.User, sms Form
} }
type FormSSE struct { type FormSSE struct {
Content string `schema:"content"`
OrganizationID int32 `schema:"organizationID"` OrganizationID int32 `schema:"organizationID"`
Resource string `schema:"resource"`
Type string `schema:"type"`
URIPath string `schema:"uriPath"`
} }
func postSudoSSE(ctx context.Context, r *http.Request, u platform.User, sse FormSSE) (string, *nhttp.ErrorWithStatus) { func postSudoSSE(ctx context.Context, r *http.Request, u platform.User, sse FormSSE) (string, *nhttp.ErrorWithStatus) {
@ -97,6 +99,6 @@ func postSudoSSE(ctx context.Context, r *http.Request, u platform.User, sse Form
Status: http.StatusForbidden, Status: http.StatusForbidden,
} }
} }
platform.SudoEvent(sse.OrganizationID, sse.Content) platform.SudoEvent(sse.OrganizationID, sse.Resource, sse.Type, sse.URIPath)
return "/sudo", nil return "/sudo", nil
} }