feat(mode): Jev solo/conspiracy/split classification on nest requests (#22)
This commit is contained in:
1 parent
5b3701d6e2
commit
9205cece65
3 files changed
+131
-47
No files matched your search
+61
-38
@@ -42,20 +42,33 @@ func (s *Server) getNestMessages(w http.ResponseWriter, r *http.Request) {
|
||||
all := s.msgs.All(nestID)
|
||||
out := make([]map[string]any, 0, len(all))
|
||||
for _, m := range all {
|
||||
out = append(out, map[string]any{
|
||||
"msg_id": m.MsgID(),
|
||||
"kind": m.Kind,
|
||||
"author": m.Author,
|
||||
"text": m.Text,
|
||||
"to": m.To,
|
||||
"refers_to": m.RefersTo,
|
||||
"tagged_by": m.TaggedBy,
|
||||
"ts": m.Timestamp,
|
||||
})
|
||||
out = append(out, nestRecord(m))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// nestRecord is the EVENTS.md wire shape for one message.
|
||||
func nestRecord(m Message) map[string]any {
|
||||
rec := map[string]any{
|
||||
"msg_id": m.MsgID(),
|
||||
"kind": m.Kind,
|
||||
"author": m.Author,
|
||||
"text": m.Text,
|
||||
"to": m.To,
|
||||
"refers_to": m.RefersTo,
|
||||
"tagged_by": m.TaggedBy,
|
||||
"ts": m.Timestamp,
|
||||
}
|
||||
if m.Mode != "" {
|
||||
rec["mode"] = m.Mode
|
||||
rec["mode_tagged_by"] = m.ModeTagged
|
||||
}
|
||||
if m.Invited > 0 {
|
||||
rec["invited"] = m.Invited
|
||||
}
|
||||
return rec
|
||||
}
|
||||
|
||||
func (s *Server) postNestMessage(w http.ResponseWriter, r *http.Request) {
|
||||
nestID := r.PathValue("nest_id")
|
||||
nest, found := s.nests.Get(nestID)
|
||||
@@ -68,6 +81,7 @@ func (s *Server) postNestMessage(w http.ResponseWriter, r *http.Request) {
|
||||
Author string `json:"author"`
|
||||
Text string `json:"text"`
|
||||
Kind string `json:"kind"`
|
||||
Mode string `json:"mode"`
|
||||
To string `json:"to"`
|
||||
RefersTo string `json:"refers_to"`
|
||||
}
|
||||
@@ -115,15 +129,20 @@ func (s *Server) postNestMessage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Mode (Epic C, same rule shape as kind): Jev first, header escape, "" when unsure.
|
||||
mode, modeBy := s.tagMode(kind, body.Mode, body.Text)
|
||||
|
||||
msg := Message{
|
||||
NestID: nestID,
|
||||
Kind: kind,
|
||||
Author: body.Author,
|
||||
Text: body.Text,
|
||||
To: body.To,
|
||||
RefersTo: body.RefersTo,
|
||||
TaggedBy: taggedBy,
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
||||
NestID: nestID,
|
||||
Kind: kind,
|
||||
Author: body.Author,
|
||||
Text: body.Text,
|
||||
To: body.To,
|
||||
RefersTo: body.RefersTo,
|
||||
TaggedBy: taggedBy,
|
||||
Mode: mode,
|
||||
ModeTagged: modeBy,
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
record, err := s.msgs.Append(msg)
|
||||
if err != nil {
|
||||
@@ -139,16 +158,27 @@ func (s *Server) postNestMessage(w http.ResponseWriter, r *http.Request) {
|
||||
go s.respondToRequest(nest, record)
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, map[string]any{
|
||||
"msg_id": record.MsgID(),
|
||||
"kind": record.Kind,
|
||||
"author": record.Author,
|
||||
"text": record.Text,
|
||||
"to": record.To,
|
||||
"refers_to": record.RefersTo,
|
||||
"tagged_by": record.TaggedBy,
|
||||
"ts": record.Timestamp,
|
||||
})
|
||||
writeJSON(w, http.StatusCreated, nestRecord(record))
|
||||
}
|
||||
|
||||
var modePostable = map[string]bool{"solo": true, "conspiracy": true, "split": true}
|
||||
|
||||
// tagMode classifies a request's mode (Jev first, header escape). Only requests
|
||||
// carry a mode; other kinds get "".
|
||||
func (s *Server) tagMode(kind, header, text string) (string, string) {
|
||||
if kind != "request" {
|
||||
return "", ""
|
||||
}
|
||||
if m, ok := s.p.Flock.ClassifyMode(s.ctx, text); ok {
|
||||
if modePostable[header] && header != m {
|
||||
return m, "disagreement"
|
||||
}
|
||||
return m, "jev"
|
||||
}
|
||||
if modePostable[header] {
|
||||
return header, "header"
|
||||
}
|
||||
return "", "default"
|
||||
}
|
||||
|
||||
// tagKind runs the A1 rule: Jev first; Jev unsure -> the poster's header;
|
||||
@@ -169,16 +199,9 @@ func (s *Server) tagKind(header, text string) (string, string) {
|
||||
}
|
||||
|
||||
func (s *Server) emitNestMessage(m Message) {
|
||||
s.hub.PublishNest(m.NestID, "nest.message", map[string]any{
|
||||
"nest_id": m.NestID,
|
||||
"msg_id": m.MsgID(),
|
||||
"kind": m.Kind,
|
||||
"author": m.Author,
|
||||
"text": m.Text,
|
||||
"to": m.To,
|
||||
"refers_to": m.RefersTo,
|
||||
"tagged_by": m.TaggedBy,
|
||||
})
|
||||
rec := nestRecord(m)
|
||||
rec["nest_id"] = m.NestID
|
||||
s.hub.PublishNest(m.NestID, "nest.message", rec)
|
||||
}
|
||||
|
||||
// respondToRequest is the must-respond rule: the addressed crow answers with
|
||||
|
||||
Reference in new issue
Block a user